Redis Design and Implementation Learning Note-lua Script

Source: Internet
Author: User
Tags redis server

Redis supports LUA scripts from 2.6, similar to the functionality of transactions, where multiple Redis commands can be executed by the Lua script atom. Redis provides the eval and Evalsha commands to execute LUA scripts.
Create and modify LUA's bad environment

Redis has a LUA environment embedded in the server, and a series of modifications are made to ensure that the LUA environment can meet the needs of the Redis server by creating and modifying Lua's bad environment through the following steps:

    1. Create a base LUA bad environment by calling Lua's C API function Lua_open.
    2. Load multiple libraries into the LUA environment so that LUA scripts can use these functions for data manipulation. Includes LUA core functions (assert, error, pairs, tostring, Pcall, etc.), table libraries (such as Table.concat, Table.insert, etc.), string libraries (string.x), math Libraries (math.x), Debug libraries (debug.x), Cjson libraries, struct libraries, cmsgpack libraries.
    3. Create a global table Redis, which contains functions for working with Redis, such as the Redis.call and Redis.pcall functions for performing redis commands in LUA scripts, redis.log functions for logging.
    4. Using the Redis self-made random function to replace the original LUA side effect random function, the replacement random function will always produce the same random number sequence for the same seed.
    5. To create a sort helper function, the LUA environment uses this helper function to sort the results of a subset of REDIS commands, eliminating the uncertainty of the command, such as Smembers, where multiple executions of the command may produce different results, sorting the results by auxiliary functions so that they produce the same results each time.
    6. Creates an Error reporting helper function for the Redis.pcall function, providing detailed error information.
    7. Protection of global variables in LUA's bad environment prevents users from adding additional variables to the LUA environment during the execution of LUA scripts, but Redis does not prevent users from modifying existing global variables, so be careful when using LUA scripts to avoid altering existing global variables incorrectly.
    8. Save the modified LUA bad to the server state of the LUA attribute (properties of the redisserver structure) and wait for the Lua script from the server to execute.
The LUA bad environment Collaboration Component Redis Server has created two components for collaborating with LUA's bad environments, which are pseudo-clients responsible for performing REDIS commands in Lua scripts, and lua_scripts dictionaries for saving LUA scripts.
The pseudo-client pseudo-client handles the Redis commands contained in the Lua script, and the Lua script executes a redis command using the Redis.call or Redis.pcall function, which requires the following steps:
    1. The LUA bad environment passes the command that the Redis.call or Redis.pcall function wants to execute to the pseudo-client.
    2. The pseudo-client passes the command that the script wants to execute to the command executor.
    3. The command executor executes the command passed to it by the pseudo-client and returns the execution result of the command to the client.
    4. The pseudo-client receives the command result returned by the command executor and returns the command result to the LUA bad environment.
    5. LUA bad environment Returns the result to Redis.call or Redis.pcall after receiving the command result.
    6. The Redis.call or Redis.pcall function that receives the result returns the command result as a function return value to the caller of the script.
The lua_scripts dictionary is stored in the REDISSERVER structure as a property, the key of the dictionary is the SHA1 checksum of the Lua script, and the dictionary value is SHA1 checksum corresponding to the Lua script, and the Redis server will execute all LUA scripts executed by the eval command. And all Lua scripts loaded with the script load command are saved to the Lua_scripts dictionary.
The eval Command eval command implementation consists of three steps:
1, according to the Lua script given by the client, define a LUA function in the LUA bad environment, the function name consists of the f_ prefix plus the SHA1 checksum (40 characters) of the script, the function body is the script itself.
2. Save the script given by the client to the Lua_scripts dictionary.
3. Execute the client-given LUA script by executing the function defined in step 1 as follows:

    • Save the key name arguments and script arguments passed in the eval command to the keys array and the ARGV array, and then pass the two arrays as global variables into the LUA bad environment.
    • For LUA bad load timeout processing hooks, this hook can let the client stop the script via the script kill command or shut down the server directly with the shutdown command when the scripts run out of time.
    • Executes the script function.
    • Remove the time-out hooks that were loaded before.
    • Saves the result of executing the script function to the output buffer of the client state, waiting for the server to return the result to the client.
    • Perform garbage collection operations on LUA's bad environment.

Evalsha command

Each LUA script executed successfully by the Eval command generates a SHA1 checksum, in order to conserve bandwidth resources, the next time you execute the same script, you can use the Evalsha command to upload the previously generated checksum to the server.

Script Management Commands
    • Script FLUSH, which clears all information about LUA scripts on the server: releases the lua_scripts dictionary, closes the existing LUA environment, and re-creates a LUA bad environment.
    • Script EXISTS, based on the input SHA1 checksum, checks to see if the corresponding script exists, by checking the lua_scripts dictionary.
    • Script LOAD, save the scripts in the Lua_scripts dictionary.
    • Script KILL, when the server sets the Lua-time-limit configuration option, the server will set a timeout hook in the LUA bad environment each time the Lua script executes, and the timeout hook is checked during the script run, once the script executes more than Lua-time-limit , the hooks will periodically see if there is a script kill or shutdown command in the gap between the scripts running. If a timed-out script does not perform any write operations, the client can instruct the server to stop executing the script through script kill, and if a write operation is performed, the client can only stop the server with the shutdown nosave command to prevent illegal data from being written to the database.
Script replication when the server is running in copy mode, the eval command, script FLUSH, script LOAD, Evalsha command are copied to the slave server, except for the Evalsha command, the other three commands are propagated directly, in the propagation Evalsha command, First check whether the command has been loaded from the server, if the direct copy has been loaded, if it has not been loaded, the Evalsha command must be converted to the equivalent eval command before it is propagated.

Redis Design and Implementation Learning Note-lua Script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.