Optimization methods, devices, electronic equipment, and readable media for blocking Lua scripts.
By obtaining the average execution time of Lua scripts in Redis and setting a flag, only reading commands are executed and forwarded to slave nodes for processing, thus solving the reconnection and interruption problems caused by Lua script execution blocking and improving service availability and accuracy.
Patent Information
- Application Number
- CN202411725903.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-28
- Publication Date
- 2025-10-31
- Estimated Expiration
- 2044-11-28
AI Technical Summary
When Lua scripts are executed in Redis, they can block the entire cache information, causing clients to constantly reconnect in high-concurrency scenarios, leading to application interruptions and impacting performance and resource usage.
When executing a Lua script, obtain its average execution time. If it exceeds a preset threshold, set a flag, execute only read commands and forward them to slave nodes for processing, terminate non-read commands, update the average execution time, and remove the flag.
This avoids reconnection issues caused by excessively long Lua script execution times, improves service availability and accuracy, and reduces erroneous high-availability module switching and application interruptions.
Smart Images

Figure CN119829129B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of middleware technology, and in particular to an optimization method for Lua script blocking, an optimization device for Lua script blocking, an electronic device, and a computer-readable medium. Background Technology
[0002] Redis uses a single-threaded, multiplexed I / O model. Executing Lua will block all cached information in the entire Redis instance, meaning that other commands cannot be executed while Lua is running. If this blocking occurs for an extended period, it will negatively impact Redis performance and resource reuse.
[0003] Especially in high-concurrency scenarios, if the application is configured improperly with Lua, Redis may reject other requests during the blocking period, causing a chain reaction that leads to continuous client reconnection and application interruption. Summary of the Invention
[0004] This invention provides an optimization method, apparatus, electronic device, and readable medium for Lua script blocking, to solve the problem of continuous reconnection and application interruption during script execution blocking.
[0005] This invention discloses an optimization method for Lua script blocking, including:
[0006] Load the Lua script into the cache;
[0007] Each time a Lua script is executed, the following steps are performed:
[0008] Get the average execution time of a Lua script;
[0009] If the average execution time exceeds a preset threshold, a flag is set;
[0010] Execute the read command in the Lua script and obtain the result of the command execution;
[0011] If the execution result of the command determines that the Lua script has finished executing, then remove the flag.
[0012] Optionally, before executing the read command in the Lua script and obtaining the result of the command execution, the following steps are also included:
[0013] Determine the type of each instruction in the Lua script, where the type is either read or non-read;
[0014] The instruction of type read is determined to be a read instruction.
[0015] Optionally, execute the read command in the Lua script to obtain the command execution result, including:
[0016] Get the slave node;
[0017] If the read command in the Lua script is successfully retrieved from the slave node, it will be forwarded to the slave node so that the slave node can execute the read command and the execution result of the slave node will be used as the execution result of the command.
[0018] Optionally, after obtaining the slave node, the following is also included:
[0019] If retrieving data from a node fails, the execution of read commands in the Lua script is terminated, and the execution result of the blocked command is obtained, with the blocking status indicating busy.
[0020] Optionally, after determining the type of each instruction in the Lua script, the following is also included:
[0021] This terminates the execution of non-read instructions in the Lua script, yielding the execution result of the blocked instructions, with the blocking status set to read-only.
[0022] Optionally, after determining the completion of this Lua script execution based on the command execution result, the following may also be included:
[0023] Determine the execution time of this Lua script;
[0024] The average execution time of updating the Lua script is (average execution time of the Lua script * historical execution count of the Lua script + current execution time) / (historical execution count of the Lua script + 1).
[0025] Optionally, if the script execution is determined to be blocked based on the instruction execution result, a disconnect and reconnection operation is triggered.
[0026] This invention also discloses an optimization device for Lua script blocking, comprising:
[0027] Load module, used to load Lua scripts into the cache;
[0028] The execution module is used to obtain the average execution time of the Lua script each time it is executed; if the average execution time is greater than a preset threshold, a flag is set; the read instructions in the Lua script are executed to obtain the instruction execution results; if the execution results determine that the Lua script has completed this execution, the flag is removed.
[0029] This invention also discloses an electronic device, including a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus;
[0030] The memory is used to store computer programs;
[0031] When the processor executes a program stored in the memory, it implements the method described in the embodiments of the present invention.
[0032] This invention also discloses one or more computer-readable media storing instructions that, when executed by one or more processors, cause the processors to perform the methods described in this invention.
[0033] The embodiments of the present invention have the following advantages:
[0034] The Lua script is loaded into a cache. Each time the Lua script is executed, the following steps are performed: the average execution time of the Lua script is obtained; if the average execution time exceeds a preset threshold, a flag is set; read instructions in the Lua script are executed, and the execution results are obtained; if the execution results determine that the Lua script has completed its current execution, the flag is removed. In each execution of the Lua script, if the average execution time exceeds the preset threshold, the Lua script only executes read instructions to obtain the execution results. In scenarios where Lua script execution times are too long, the sensitivity of instruction access is adjusted by the preset threshold, avoiding continuous reconnections and application terminal issues. Attached Figure Description
[0035] Figure 1 This is a flowchart of the steps of an optimization method for Lua script blocking provided in an embodiment of the present invention;
[0036] Figure 2 This is a flowchart illustrating the steps of a method for instruction execution when Lua script execution is blocked, provided in an embodiment of the present invention.
[0037] Figure 3 This is a flowchart of another method for instruction execution when Lua script execution is blocked, provided in an embodiment of the present invention;
[0038] Figure 4 This is a flowchart of another optimization method for Lua script blocking provided in an embodiment of the present invention;
[0039] Figure 5 This is a structural block diagram of an optimization device for Lua script blocking provided in an embodiment of the present invention;
[0040] Figure 6 This is a block diagram of an electronic device provided in an embodiment of the present invention;
[0041] Figure 7 This is a schematic diagram of a computer-readable medium provided in an embodiment of the present invention. Detailed Implementation
[0042] To make the above-mentioned objects, features and advantages of the present invention more apparent and understandable, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments.
[0043] In the process of realizing this invention, the inventors discovered that:
[0044] Redis (Remote Dictionary Server) is a high-performance, in-memory open-source database that stores data in key-value pairs and supports various data structures. Redis is commonly used for caching, session management, message queues, and other applications. In Redis, Lua scripts are typically used to perform a series of operations, such as reading and writing data, as well as performing complex logical operations. Redis Lua scripts are parsed into executable instructions so that the Redis server can execute these scripts.
[0045] Because Redis uses a single-threaded, multiplexed I / O model, executing Lua will block all cached information in the entire Redis instance, meaning that other commands cannot be executed while Lua is running. If this blocking occurs for an extended period, it will negatively impact Redis performance and resource reuse.
[0046] Especially in high-concurrency scenarios, if the application is configured improperly with Lua, Redis may reject other requests during the blocking period, causing a chain reaction that leads to continuous client reconnection and application interruption.
[0047] For example, if the cache high availability module detects that the ping operation cannot be responded to normally during the blocking period, the high availability module will mistakenly think that there is an abnormality in the Redis service node, triggering the master-slave switch operation, which will then have a negative impact on business applications.
[0048] This invention provides an optimization method, apparatus, electronic device, and readable medium for Lua script blocking. The method loads the Lua script into a cache. Each time the Lua script is executed, the following steps are performed: obtaining the average execution time of the Lua script; if the average execution time is greater than a preset threshold, setting a flag; executing read instructions in the Lua script to obtain the instruction execution result; and if the execution result determines that the Lua script has completed its current execution, canceling the flag. In this invention, if the average execution time of the Lua script is greater than a preset threshold each time it is executed, the flag is used to ensure that the Lua script only executes read instructions to obtain the instruction execution result. In scenarios where the Lua script execution time is too long, the preset threshold is used to adjust the sensitivity of instruction access, avoiding continuous reconnection and application terminal issues.
[0049] Reference Figure 1The diagram illustrates a flowchart of an optimization method for Lua script blocking provided in an embodiment of the present invention, which may specifically include the following steps:
[0050] 101. Load the Lua script into the cache.
[0051] Once a Lua script is loaded into the cache, a SHA-1 hash value is generated for that script as the key, and the Lua script content as the value. During subsequent execution, the corresponding value can be found using the key, and the Lua script within that value can then be executed.
[0052] 102. Each time a Lua script is executed, steps 201 to 204 are performed:
[0053] 201, get the average execution time of the Lua script.
[0054] The average execution time of the Lua script is updated after each execution of the Lua script. This step simply obtains the current average execution time.
[0055] 202. If the average execution time is greater than the preset threshold, then set a flag.
[0056] When the average execution time of the script is less than the preset threshold, it means that the execution time is controllable, will not cause overall service blockage, and no intervention is required.
[0057] When the average execution time exceeds the threshold, it indicates that there is a risk of blocking in the script execution, which could reduce overall availability. In this case, a read request forwarding process should be triggered to improve availability.
[0058] The threshold can be configured through configuration items, such as adding the following items: parameter1, parameter2, parameter3.
[0059] Parameter 1 indicates whether the Lua script blocking optimization method provided in this embodiment is used when the Lua script is executed, parameter 2 indicates whether to set a flag bit, and parameter 3 indicates the threshold.
[0060] For example, before executing the Lua script blocking optimization method provided in this embodiment, if the configuration item is newly added as script-analysis-disable no NULL, where parameter 1 is script-analysis-disable, parameter 2 is no, and parameter 3 is NULL (i.e., empty value), it indicates that the Lua script will not be executed using the Lua script blocking optimization method provided in this embodiment.
[0061] For example, before executing the Lua script blocking optimization method provided in this embodiment, if the configuration item is newly added as `script-analysis-enable no 10`, where parameter 1 is `script-analysis-enable`, parameter 2 is `no`, and parameter 3 is `10`, it indicates that the Lua script is executed using the Lua script blocking optimization method provided in this embodiment, and the threshold is 10 milliseconds. At this time, the flag is not set.
[0062] If, in step 202, the average execution time is determined to be greater than 10 milliseconds, then parameter 2 of the configuration item is modified to "yes" to set the flag. The modified configuration item adds the following content: `script-analysis-enable yes 10`, where parameter 1 is `script-analysis-enable`, parameter 2 is "no", and parameter 3 is "10". This indicates that the Lua script is executed using the Lua script blocking optimization method provided in this embodiment, with the flag set to a threshold of 10 milliseconds.
[0063] Since the flag is set when step 102 is executed, the fact that the flag is set indicates that the Lua script is being executed.
[0064] 203. Executes the read instruction in the Lua script and obtains the instruction execution result. Terminates the execution of non-read instructions in the Lua script, obtaining the execution result of the blocked instructions, with the blocking status indicating read-only.
[0065] In step 203, read commands in the Lua script are processed. Other commands are not executed. Therefore, read commands in the Lua script are determined before step 203 is executed.
[0066] The process of determining the read command in a Lua script is as follows:
[0067] 1. Determine the type of each instruction in the Lua script, where the type is either read or non-read.
[0068] 2. Define the instruction of type read as a read instruction.
[0069] If we categorize Lua script commands, we can group read commands into one category and non-read commands (such as write commands) into another.
[0070] In step 203, only read instructions will be executed, and non-read instructions will not be executed.
[0071] The execution process of the read instruction in step 203 is as follows:
[0072] 1. Obtain slave nodes.
[0073] 2. If the read command in the Lua script is successfully retrieved from the slave node, it will be forwarded to the slave node so that the slave node can execute the read command and the execution result of the slave node will be used as the execution result of the command.
[0074] 3. If retrieving from a node fails, the execution of read instructions in the Lua script will be terminated, and the execution result of the blocked instructions will be obtained, with the blocking status indicating busy.
[0075] For example, the command execution result is: Busy, script execution is blocked.
[0076] In other words, when executing step 203, if all the instructions in the Lua script are read instructions, all read instructions will be executed through the slave node, and the execution result of the read instructions will be used as the instruction execution result obtained by executing the read instructions in the Lua script in step 203.
[0077] If all instructions in a Lua script are non-read instructions, then the execution of all non-read instructions will be terminated, resulting in the execution of blocked instructions, with the blocking condition being read-only.
[0078] For example, the instruction execution result is: read-only, script execution is blocked.
[0079] If some commands in a Lua script are read commands and some are non-read commands, then as follows: Figure 2 As shown, by executing read instructions in the Lua script from the node, the execution result of the instructions is obtained. Then, the execution of non-read instructions in the Lua script is terminated, resulting in the execution result of the blocked instructions, and the blocking is indicated as read-only.
[0080] The process of obtaining a slave node involves executing read commands from a Lua script on the slave node, forwarding the read commands from the Lua script to the slave node so that the slave node can execute the read commands, and determining the execution result of the slave node as the command execution result.
[0081] In the specific implementation, in order to ensure that the execution of step 203 is in the execution blocking period, before executing step 203, it is also possible to confirm again whether Lua script blocking has occurred, that is, to confirm again that the average execution time is greater than the preset threshold and to set the flag bit.
[0082] If the average execution time exceeds a preset threshold and a flag is set, Lua script blocking is confirmed, and step 203 is executed. Otherwise, it is considered that no Lua script blocking has occurred, and step 203 is not executed. Instead, the existing Lua script instruction execution scheme is switched to (i.e., local execution instructions), such as... Figure 3 As shown.
[0083] In step 203, only read commands are allowed to be executed, achieving the effect of responding to read-only requests by forwarding them to the slave node. For write commands, an error message is quickly returned. Since write requests are still rejected / blocked, theoretically, read-only requests do not have master-slave data consistency issues, except for the data involved in the script, and the data accuracy can be trusted.
[0084] 204. If the execution result of the instruction determines that the Lua script has finished executing this time, the flag is canceled.
[0085] For example, changing parameter 2 of the configuration item to "no" will cancel the flag. After modification, the new content in the configuration item becomes "script-analysis-enable no 10", where parameter 1 is "script-analysis-enable", parameter 2 is "no", and parameter 3 is "10". This indicates that the Lua script is executed using the Lua script blocking optimization method provided in this embodiment, and the threshold is 10 milliseconds. At this time, the flag is not set (i.e., the flag is canceled).
[0086] Removing the flag means the Lua script has finished executing.
[0087] In addition, after the Lua script completes its execution based on the command execution results, the average execution time of the Lua script will be updated. The update process is as follows:
[0088] 1. Determine the execution time of the Lua script.
[0089] If this is the i-th execution of the Lua script, then the execution time T of the i-th Lua script will be determined. i .
[0090] 2. Update the average execution time of the Lua script to (average execution time of the Lua script * historical execution count of the Lua script + current execution time) / (historical execution count of the Lua script + 1).
[0091] The historical execution count of the Lua script does not include the current execution, meaning the historical execution count of the Lua script is i-1.
[0092] In other words, the average execution time of all i-1 executions after the first i-1 executions of the Lua script is obtained by comparing the average execution time of all i-1 executions after the first i-1 executions of the Lua script with the execution time of the i-th execution of the Lua script. The average execution time of all i executions is the average execution time of the updated Lua script.
[0093] If the average execution time of a Lua script is The update process is as follows:
[0094] In addition, if the script execution is determined to be blocked based on the instruction execution result, the application can implement retry mechanisms to avoid the application waiting for a long time and timeout, thereby triggering a disconnection and reconnection operation.
[0095] The Lua script blocking optimization method provided in this embodiment can address scenarios where complex Lua scripts take too long to execute. During the Lua script blocking phase, it allows service probe requests and read commands to be allowed, improving the overall service availability. At the same time, it counts and records the average execution time of the Lua script and adjusts the command opening sensitivity according to a preset threshold. This improves service availability during the blocking phase and avoids various problems caused by service blocking, such as avoiding high availability judgment errors and application timeout reconnection issues caused by service blocking.
[0096] The Lua script blocking optimization method provided in this embodiment enables more accurate high-availability module judgment and avoids erroneous master-slave failover. Existing Lua script execution schemes, if the Lua script execution time is very long, will also block ping requests during this period, potentially causing the high-availability module to consider the service unavailable and trigger master-slave failover logic. The Lua script blocking optimization method provided in this embodiment ensures that ping requests can still respond normally during Lua script execution, avoiding incorrect judgments.
[0097] The Lua script blocking optimization method provided in this embodiment adds a configuration switch, calculates the average script execution time, and adjusts the instruction opening sensitivity based on the configuration threshold, making the adaptation more flexible.
[0098] The Lua script blocking optimization method provided in this embodiment releases read-only instructions during Lua script execution blocking, allowing them to be processed by slave nodes. It also quickly returns a busy error based on the instruction execution result, preventing the application from waiting for extended periods and triggering high-availability logic.
[0099] The Lua script blocking optimization method provided in this embodiment can achieve compatibility with existing Lua script execution schemes. For example... Figure 4 As shown.
[0100] After executing the Lua script, the Lua script is loaded into the cache via step 101.
[0101] Then check if parameter 1 is script-analysis-enable.
[0102] If parameter 1 is not script-analysis-enable (e.g., script-analysis-disable), the existing Lua script execution scheme will be used to execute the Lua script.
[0103] If parameter 1 is script-analysis-enable, then the Lua script is executed in step 102 (that is, the Lua script is executed using the Lua script blocking optimization method provided in this embodiment).
[0104] For example, obtain the average execution time of the Lua script through step 201.
[0105] In step 202, a flag is set when the average execution time exceeds a preset threshold.
[0106] In step 203, the read instruction in the Lua script is executed, and the instruction execution result is obtained. The execution of non-read instructions in the Lua script is then terminated, resulting in the execution result of the blocked instruction, with the blocking status indicating read-only.
[0107] In step 204, after determining that the Lua script has finished executing based on the instruction execution result, the flag is removed.
[0108] Regardless of the method used to execute the Lua script, the average execution time of the Lua script will be updated after each execution.
[0109] It should be noted that, for the sake of simplicity, the method embodiments are all described as a series of actions. However, those skilled in the art should understand that the embodiments of the present invention are not limited to the described order of actions, because according to the embodiments of the present invention, some steps can be performed in other orders or simultaneously. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are preferred embodiments, and the actions involved are not necessarily essential to the embodiments of the present invention.
[0110] This embodiment provides an optimization method for Lua script blocking. The Lua script is loaded into a cache. Each time the Lua script is executed, the following steps are performed: obtain the average execution time of the Lua script; if the average execution time is greater than a preset threshold, set a flag; execute the read instruction in the Lua script to obtain the instruction execution result; if it is determined from the instruction execution result that the Lua script has completed this execution, cancel the flag.
[0111] This method, when executing a Lua script, if the average execution time of the Lua script exceeds a preset threshold, uses a flag to make the Lua script only execute read instructions to obtain the instruction execution result. In scenarios where the Lua script execution time is too long, the sensitivity of instruction access is adjusted by the preset threshold, avoiding continuous reconnection and application terminal issues.
[0112] Reference Figure 5 The diagram illustrates a structural block diagram of an optimization device for Lua script blocking provided in an embodiment of the present invention, which may specifically include the following modules:
[0113] Load module 501, used to load Lua scripts into the cache.
[0114] Module 502 is used to perform the following steps every time a Lua script is executed:
[0115] Get the average execution time of the Lua script.
[0116] If the average execution time exceeds a preset threshold, a flag is set.
[0117] Execute the read command in the Lua script and obtain the command execution result.
[0118] If the execution result of the command determines that the Lua script has finished executing, then remove the flag.
[0119] Before executing the read command in the Lua script and obtaining the execution result, the process also includes: determining the type of each command in the Lua script, where the type is either read or non-read.
[0120] The instruction of type read is determined to be a read instruction.
[0121] Specifically, executing read commands in a Lua script yields the following results:
[0122] Get the slave node.
[0123] If the read command in the Lua script is successfully retrieved from the slave node, it will be forwarded to the slave node so that the slave node can execute the read command and the execution result of the slave node will be used as the execution result of the command.
[0124] After obtaining the slave node, the process also includes:
[0125] If retrieving data from a node fails, the execution of read commands in the Lua script is terminated, and the execution result of the blocked command is obtained, with the blocking status indicating busy.
[0126] After determining the type of each instruction in the Lua script, the process also includes:
[0127] This terminates the execution of non-read instructions in the Lua script, yielding the execution result of the blocked instructions, with the blocking status set to read-only.
[0128] The process of determining the completion of the Lua script execution based on the command execution result also includes:
[0129] Determine the execution time of this Lua script.
[0130] The average execution time of updating the Lua script is (average execution time of the Lua script * historical execution count of the Lua script + current execution time) / (historical execution count of the Lua script + 1).
[0131] If the script execution is blocked based on the instruction execution result, a disconnect and reconnection operation is triggered.
[0132] As the device embodiment is basically similar to the method embodiment, the description is relatively simple, and relevant parts can be found in the description of the method embodiment.
[0133] The device provided in this embodiment, when executing a Lua script, if the average execution time of the Lua script is greater than a preset threshold, will use a flag to make the Lua script only execute read instructions to obtain the instruction execution result. In scenarios where the Lua script execution time is too long, the sensitivity of instruction opening is adjusted by the preset threshold, avoiding the problem of continuous reconnection and application terminal issues.
[0134] In addition, embodiments of the present invention also provide an electronic device, such as... Figure 6 As shown, it includes a processor 601, a communication interface 602, a memory 603, and a communication bus 604, wherein the processor 601, the communication interface 602, and the memory 603 communicate with each other through the communication bus 604.
[0135] Memory 603 is used to store computer programs;
[0136] When processor 601 executes a program stored in memory 603, it performs the following steps:
[0137] Load the Lua script into the cache.
[0138] Each time a Lua script is executed, the following steps are performed:
[0139] Get the average execution time of the Lua script.
[0140] If the average execution time exceeds a preset threshold, a flag is set.
[0141] Execute the read command in the Lua script and obtain the command execution result.
[0142] If the execution result of the command determines that the Lua script has finished executing, then remove the flag.
[0143] Optionally, before executing the read command in the Lua script and obtaining the result of the command execution, the following steps are also included:
[0144] Determine the type of each instruction in the Lua script, where the type is either read or non-read.
[0145] The instruction of type read is determined to be a read instruction.
[0146] Optionally, execute the read command in the Lua script to obtain the command execution result, including:
[0147] Get the slave node.
[0148] If the read command in the Lua script is successfully retrieved from the slave node, it will be forwarded to the slave node so that the slave node can execute the read command and the execution result of the slave node will be used as the execution result of the command.
[0149] Optionally, after obtaining the slave node, the following is also included:
[0150] If retrieving data from a node fails, the execution of read commands in the Lua script is terminated, and the execution result of the blocked command is obtained, with the blocking status indicating busy.
[0151] Optionally, after determining the type of each instruction in the Lua script, the following is also included:
[0152] This terminates the execution of non-read instructions in the Lua script, yielding the execution result of the blocked instructions, with the blocking status set to read-only.
[0153] Optionally, after determining the completion of this Lua script execution based on the command execution result, the following may also be included:
[0154] Determine the execution time of this Lua script.
[0155] The average execution time of updating the Lua script is (average execution time of the Lua script * historical execution count of the Lua script + current execution time) / (historical execution count of the Lua script + 1).
[0156] Optionally, if the script execution is determined to be blocked based on the instruction execution result, a disconnect and reconnection operation is triggered.
[0157] The communication bus mentioned above can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This communication bus can be divided into address bus, data bus, control bus, etc. For ease of illustration, only one thick line is used to represent it in the diagram, but this does not mean that there is only one bus or one type of bus.
[0158] The communication interface is used for communication between the aforementioned terminal and other devices.
[0159] The memory may include random access memory (RAM) or non-volatile memory, such as at least one disk storage device. Optionally, the memory may also be at least one storage device located remotely from the aforementioned processor.
[0160] The processors mentioned above can be general-purpose processors, including central processing units (CPUs), network processors (NPs), etc.; they can also be digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0161] The electronic device provided in this embodiment, when executing a Lua script, if the average execution time of the Lua script is greater than a preset threshold, will use a flag to make the Lua script only execute read instructions to obtain the instruction execution result. In scenarios where the Lua script execution time is too long, the sensitivity of instruction opening is adjusted by the preset threshold, avoiding the problem of continuous reconnection and application terminal issues.
[0162] like Figure 7 As shown, in another embodiment of the present invention, a computer-readable storage medium 701 is also provided, which stores instructions that, when run on a computer, cause the computer to execute the Lua script blocking optimization method described in the above embodiment.
[0163] In another embodiment of the present invention, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to execute the optimized method for blocking Lua scripts described in the above embodiments.
[0164] Specifically, the method is as follows:
[0165] Load the Lua script into the cache.
[0166] Each time a Lua script is executed, the following steps are performed:
[0167] Get the average execution time of the Lua script.
[0168] If the average execution time exceeds a preset threshold, a flag is set.
[0169] Execute the read command in the Lua script and obtain the command execution result.
[0170] If the execution result of the command determines that the Lua script has finished executing, then remove the flag.
[0171] Optionally, before executing the read command in the Lua script and obtaining the result of the command execution, the following steps are also included:
[0172] Determine the type of each instruction in the Lua script, where the type is either read or non-read.
[0173] The instruction of type read is determined to be a read instruction.
[0174] Optionally, execute the read command in the Lua script to obtain the command execution result, including:
[0175] Get the slave node.
[0176] If the read command in the Lua script is successfully retrieved from the slave node, it will be forwarded to the slave node so that the slave node can execute the read command and the execution result of the slave node will be used as the execution result of the command.
[0177] Optionally, after obtaining the slave node, the following is also included:
[0178] If retrieving data from a node fails, the execution of read commands in the Lua script is terminated, and the execution result of the blocked command is obtained, with the blocking status indicating busy.
[0179] Optionally, after determining the type of each instruction in the Lua script, the following is also included:
[0180] This terminates the execution of non-read instructions in the Lua script, yielding the execution result of the blocked instructions, with the blocking status set to read-only.
[0181] Optionally, after determining the completion of this Lua script execution based on the command execution result, the following may also be included:
[0182] Determine the execution time of this Lua script.
[0183] The average execution time of updating the Lua script is (average execution time of the Lua script * historical execution count of the Lua script + current execution time) / (historical execution count of the Lua script + 1).
[0184] Optionally, if the script execution is determined to be blocked based on the instruction execution result, a disconnect and reconnection operation is triggered.
[0185] The computer-readable storage medium provided in this embodiment, when executing a Lua script, if the average execution time of the Lua script is greater than a preset threshold, will use a flag to make the Lua script only execute read instructions to obtain the instruction execution result. In scenarios where the Lua script execution time is too long, the sensitivity of instruction access is adjusted by the preset threshold, avoiding the problem of continuous reconnection and application terminal issues.
[0186] In the above embodiments, implementation can be achieved entirely or partially through software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented entirely or partially in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of the present invention are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid state disk (SSD)).
[0187] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0188] The various embodiments in this specification are described in a related manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the system embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0189] The above description is merely a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention are included within the scope of protection of the present invention.
Claims
1. An optimization method for blocking Lua scripts, characterized in that, include: Load the Lua script into the cache; Each time the Lua script is executed, the following steps are performed: Obtain the average execution time of the Lua script; If the average execution time is greater than a preset threshold, a flag is set; Execute the read command in the Lua script to obtain the command execution result; If the execution result of the instruction determines that the Lua script has completed its current execution, the flag bit will be removed. The execution of the read instruction in the Lua script to obtain the instruction execution result includes: Get the slave node; If the slave node is successfully acquired, the read command in the Lua script is forwarded to the slave node so that the slave node executes the read command, and the execution result of the slave node is determined as the execution result of the command. If the acquisition from the node fails, the execution of the read instruction in the Lua script is terminated, and the execution result of the script execution is blocked, with the blocking status indicating busy.
2. The method according to claim 1, characterized in that, Before executing the read instruction in the Lua script and obtaining the instruction execution result, the process also includes: Determine the type of each instruction in the Lua script, where the type is either read or non-read; The instruction of type read is determined to be a read instruction.
3. The method according to claim 2, characterized in that, After determining the type of each instruction in the Lua script, the process further includes: The execution of non-read instructions in the Lua script is terminated, and the execution result of the blocked instructions is obtained, with the blocking description being read-only.
4. The method according to claim 1, characterized in that, After determining that the Lua script has completed its execution based on the result of the instruction execution, the process further includes: Determine the execution time of the current Lua script; The average execution time of the updated Lua script is (average execution time of the Lua script * historical execution count of the Lua script + current execution time) / (historical execution count of the Lua script + 1).
5. The method according to claim 1, characterized in that, If the script execution is blocked based on the result of the instruction execution, a disconnect and reconnection operation is triggered.
6. An optimization device for blocking Lua scripts, characterized in that, include: Load module, used to load Lua scripts into the cache; An execution module is used to obtain the average execution time of the Lua script each time it is executed; If the average execution time is greater than a preset threshold, a flag is set; the read instruction in the Lua script is executed to obtain the instruction execution result; if it is determined from the instruction execution result that the Lua script has completed this execution, the flag is canceled. The execution of the read instruction in the Lua script to obtain the instruction execution result includes: Get the slave node; If the slave node is successfully acquired, the read command in the Lua script is forwarded to the slave node so that the slave node executes the read command, and the execution result of the slave node is determined as the execution result of the command. If the acquisition from the node fails, the execution of the read instruction in the Lua script is terminated, and the execution result of the script execution is blocked, with the blocking status indicating busy.
7. An electronic device, characterized in that, It includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus; The memory is used to store computer programs; When the processor executes a program stored in the memory, it implements the method as described in any one of claims 1-5.
8. A computer-readable medium having instructions stored thereon that, when executed by one or more processors, cause the processors to perform the method as described in any one of claims 1-5.
Citation Information
Patent Citations
Redis-based lock-free distributed data high-concurrency processing framework system
CN117851513A
Protocol software component and test apparatus
US20120266024A1