A method-level idempotency solution, device, and storage medium
By adding idempotency annotations and AOP interception to the JVM stack, a compressed string is generated to determine method-level idempotency. This solves the problems of poor web request performance and database dependence in existing technologies, realizes method-level idempotency verification, and improves development efficiency and flexibility.
Patent Information
- Application Number
- CN202111156706.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-09-30
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2041-09-30
AI Technical Summary
Existing idempotency solutions suffer from poor performance in web requests, reliance on database validation, and the need for front-end cooperation, making it difficult to achieve method-level idempotency validation.
By adding an idempotent annotation to the JVM stack, AOP intercepts method execution, generates compressed strings according to rules, and uses a configured strategy to determine whether the method has already been executed, simplifying the implementation of method-level idempotency verification to a single step using the @ColiIdempotentHandler annotation.
It improves software development efficiency, has a wider range of applicable scenarios, reduces repetitive code, enhances readability and flexibility, and supports multiple compression algorithms.
Smart Images

Figure CN113805855B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software development technology, and in particular to a method-level idempotency solution, device, and storage medium. Background Art
[0002] Existing idempotency solutions on the market:
[0003] 1. Using redis distributed lock
[0004] When a user makes a request, the browser carries a global unique ID. After receiving the request, the server determines whether the unique ID has been processed (usually stored in redis to determine whether it exists in redis. If it exists, it has been processed.). If it has been processed, it will not be processed.
[0005] Disadvantages: 1) The front end needs an algorithm to generate a globally unique ID, 2) consumes the client's CPU resources, 3) requires cooperation from the front end and business development to complete.
[0006] 2. Calibration using database
[0007] Such as 1) unique index 2) pessimistic lock 3) optimistic lock 4) state machine idempotence.
[0008] Disadvantages: These all rely on the database to complete the corresponding idempotence check. The performance will be relatively poor, or abnormal alarms will appear that should not appear.
[0009] The above technologies are mainly for Web requests. If you want to achieve idempotence at the method level, you still need to redevelop the corresponding program according to the above principles. Summary of the invention
[0010] The present application provides a method-level idempotency solution, device, and storage medium to improve the efficiency of software development.
[0011] In a first aspect, the present application provides a method-level idempotency solution, the method comprising:
[0012] Add idempotency annotations to methods that need to implement idempotency through the JVM stack;
[0013] AOP intercepts method execution;
[0014] The program concatenates the fields of the idempotence strategy and generates the specified string according to the rules and the specified compression level;
[0015] The program checks the policy configured when adding idempotence;
[0016] Determine whether the configured policy has been executed.
[0017] In a second aspect, the present application further provides an idempotency solution device, the device comprising: a memory and a processor;
[0018] The memory is used to store programs;
[0019] The processor is used to execute the program and implement the above-mentioned method-level idempotence solution when executing the program.
[0020] In a third aspect, the present application further provides a storage medium storing processor-executable instructions, which are used to execute the above-mentioned method-level idempotence solution when executed by the processor.
[0021] The present application discloses a method-level idempotency solution, device and storage medium, which adds idempotency annotations to the method that needs to achieve idempotency through the JVM stack; AOP intercepts the execution of the method; the program splices the fields of the idempotency strategy and generates a specified string according to the rules and the specified compression level; the program checks the strategy configured when adding idempotency; and determines whether it has been executed through the configured strategy. The present application improves software development efficiency, is more flexible, and has stronger readability. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0023] Figure 1 is a schematic flow chart of a method for training a target recognition model provided in an embodiment of the present application;
[0024] Figure 2 A schematic block diagram of the structure of an idempotence solution device provided in one embodiment of the present application. DETAILED DESCRIPTION
[0025] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0026] The flowcharts shown in the accompanying drawings are only examples and do not necessarily include all the contents and operations / steps, nor must they be executed in the order described. For example, some operations / steps may also be decomposed, combined or partially merged, so the actual execution order may change according to actual conditions.
[0027] The embodiments of the present application provide a method-level idempotency solution, device, and storage medium, wherein the method-level idempotency solution can be applied to a terminal or a server to improve the efficiency of software development.
[0028] In conjunction with the accompanying drawings, some embodiments of the present application are described in detail below. In the absence of conflict, the following embodiments and features in the embodiments can be combined with each other.
[0029] See also Figure 1 , Figure 1 This is a schematic flow chart of a method-level idempotency solution provided by an embodiment of the present application. The method includes steps S101 to S105.
[0030] S101. Add idempotency annotations to methods that need to implement idempotency through the JVM stack.
[0031] Specifically, the @Aspect of the Spring framework is used to implement the annotation, which is used in conjunction with Spring. It is itself an extended annotation of Spring's annotation and can be used directly by annotating it.
[0032] S102, AOP interception method execution.
[0033] Specifically, the idempotence annotation added above is automatically intercepted through AOP, and then the JVM runs to query the user's current idempotence configuration strategy and performs idempotence verification according to different strategies.
[0034] S103. The program concatenates the fields of the idempotence strategy and generates a specified string according to the rules and the specified compression level.
[0035] Specifically, the program splicing rules are as follows:
[0036] 1) Parameter name ASCII codes are sorted from small to large (lexicographic order);
[0037] 2) If the parameter value is empty, it will not be serialized;
[0038] 3) Parameter names are case-sensitive;
[0039] After concatenation is completed, the string is compressed according to the specified compression level. The compression algorithm can be selected as java.util.zip. The compression range is 0 to 9, the default level is 5, and -1 means no compression. The higher the level, the more CPU is consumed. The lower the level, the faster the performance is, but it will consume network space and redis memory storage space.
[0040] S104. The program checks the policy configured when adding idempotence.
[0041] Specifically, check whether the jvm method input parameter exists, no exception is thrown, the input parameter exists, and there is only one input parameter, check whether the property field is specified, if specified, use the specified field to perform the splicing and compression field described in S103. If not specified, directly compress the entire object by splicing the input parameter as described in S103.
[0042] If there are multiple input parameters, they need to be specified through parameter. If multiple parameters need to be specified, they need to be separated by English characters ','.
[0043] Then put the concatenated and compressed string into redis for comparison.
[0044] S105. Determine whether the configured policy has been executed.
[0045] Specifically, through the configured strategy, it is determined whether it has been executed. If it has been executed, it will not be executed again. If it has not been executed, this task will be executed. When executing, redis setnx is used to implement the check whether it has been executed. The Setnx command sets the value of the key to value. If and only if the key does not exist, the task that has been executed will be executed. If the given key already exists, Setnx does not take any action.
[0046] The method-level idempotency solution provided in the above embodiment is to add idempotency annotations to the methods that need to achieve idempotency through the jvm stack; AOP intercepts method execution; the program splices the fields of the idempotency strategy and generates a specified string according to the rules and the specified compression level; the program checks the strategy configured when adding idempotency; and determines whether it has been executed based on the configured strategy. This application has the following advantages:
[0047] 1. Most of the existing technologies and solutions are for Web requests. This application is targeted at the method level and is applicable to a wider range of scenarios, such as MQ consumption, scheduled task scheduling, RPC service calls, etc.
[0048] 2. Improved development efficiency: Previously, developers needed to declare the redis service, obtain a parameter and extract certain fields, and finally execute the redis setnx command to implement idempotence verification; a total of three steps, now only one step is required to use the annotation @ColiIdempotentHandler to implement it.
[0049] 3. Better readability. Declarative annotations can express in one line that you want the method to implement idempotence verification.
[0050] 4. Reduce duplicate code. Developers do not need to declare the redis service, obtain a parameter and take out certain fields by themselves, and finally execute the three steps of the redis setnx command. Only one annotation is needed.
[0051] 5. Use compression algorithms flexibly. Developers can choose compression or no compression, and can choose the compression level. The higher the compression level, the more CPU is consumed but network bandwidth and redis memory are saved. The lower the level, the faster the performance but network space and redis memory are consumed. These are left to developers to freely choose according to their own scenarios.
[0052] In an optional embodiment, the configured strategy includes a redis strategy or a mysql strategy. When the configured strategy is a redis strategy, judging whether the configured strategy has been executed includes:
[0053] Check whether the compressed string exists in redis;
[0054] If it exists, it has been executed, otherwise it will not be executed. If it does not exist, it has not been executed, and it will be stored in redis with an expiration time specified.
[0055] When the configured policy is the MySQL policy, the configuration policy is used to determine whether it has been executed, including:
[0056] Insert the compressed string into the table specified by MySQL;
[0057] If the insertion fails, it will not be executed. If the insertion is successful, the subsequent logic will be executed.
[0058] In this embodiment, the annotation @ColiIdempotentHandler(parameter="", property="", leaseTime="300, compressionLevel="5").
[0059] Among them, parameter="" specifies the parameter based on which the method implements idempotence. If there is only one input parameter or the parameter you want to specify is the first input parameter, then parameter can be left blank.
[0060] property="" is used together with the first parameter to specify which field of the parameter input can be used for idempotence verification. If it is not filled in, it means that the entire object specified by parameter is serialized as the specified object.
[0061] LeaseTime is the release time, which indicates the approximate time required for the task to execute. It is the time when the key disappears in redis. The default value is 300ms.
[0062] compressLevel is the compression level, ranging from 0 to 9, with the default level of 5, and -1 means no compression. The higher the level, the more CPU is consumed, and the lower the level, the faster the performance, but it will consume network space and redis memory storage space.
[0063] See also Figure 2 , Figure 2 The present invention provides an idempotency solution device provided in an embodiment of the present invention. The idempotency solution device may be a server or a terminal.
[0064] See also Figure 2 The idempotence solution device includes a processor, a memory, and a network interface connected through a system bus, wherein the memory may include a non-volatile storage medium and an internal memory.
[0065] The non-volatile storage medium can store an operating system and a computer program. The computer program includes program instructions, and when the program instructions are executed, the processor can execute any lightweight task scheduling method.
[0066] The processor is used to provide computing and control capabilities and support the operation of the entire computer equipment.
[0067] The internal memory provides an environment for the operation of the computer program in the non-volatile storage medium. When the computer program is executed by the processor, the processor can execute any lightweight task scheduling method.
[0068] The network interface is used for network communication, such as sending assigned tasks, etc. Those skilled in the art will understand that Figure 2 The structure shown in the figure is only a block diagram of a partial structure related to the solution of the present application, and does not constitute a limitation on the idempotency solution device to which the solution of the present application is applied. The specific idempotency solution device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0069] It should be understood that the processor may be a central processing unit (CPU), and the processor may also be other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. Among them, the general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.
[0070] In one embodiment, the processor is used to run a computer program stored in the memory to implement the following steps:
[0071] Add idempotency annotations to methods that need to implement idempotency through the JVM stack;
[0072] AOP intercepts method execution;
[0073] The program concatenates the fields of the idempotence strategy and generates the specified string according to the rules and the specified compression level;
[0074] The program checks the policy configured when adding idempotence;
[0075] Determine whether the configured policy has been executed.
[0076] A storage medium is also provided in an embodiment of the present application, in which processor-executable instructions are stored, characterized in that the processor-executable instructions are used to execute the above-mentioned lightweight task scheduling method when executed by the processor.
[0077] The storage medium may be an internal storage unit of the idempotency solution device of the aforementioned embodiment, such as a hard disk or memory of the idempotency solution device. The storage medium may also be an external storage device of the idempotency solution device, such as a plug-in hard disk, a smart memory card (SmartMedia Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. equipped on the idempotency solution device.
[0078] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed in the present application, and these modifications or replacements should be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be based on the protection scope of the claims.
Claims
1. A method-level idempotency solution, characterized in that: include: Add idempotency annotations to methods that need to implement idempotency through the JVM stack; AOP intercepts method execution; The program concatenates the fields of the idempotence strategy and generates the specified string according to the rules and the specified compression level. The program concatenation rules are as follows: 1) Parameter name ASCII codes are sorted from small to large; 2) If the parameter value is empty, it will not be serialized; 3) Parameter names are case-sensitive; After concatenation is completed, the string is compressed according to the specified compression level. The compression algorithm is java.util.zip. The compression range is 0 to 9 levels, with 5 being the default. -1 means no compression. The higher the level, the more CPU is consumed. The lower the level, the faster the performance is, but it will consume network space and redis memory storage space. The program checks the policy configured when adding idempotence; Determine whether the configured policy has been executed. The configured strategy includes a redis strategy or a mysql strategy; If the configured policy is a redis policy, then judging whether the configured policy has been executed includes: Check whether the compressed string exists in redis; If it exists, it has been executed, otherwise it will not be executed. If it does not exist, it has not been executed, and it will be stored in redis with an expiration time specified. If the configured policy is a mysql policy, then judging whether the configured policy has been executed includes: Insert the compressed string into the table specified by MySQL; If the insertion fails, it will not be executed. If the insertion is successful, the subsequent logic will be executed.
2. A device for solving idempotency problem, characterized in that: including memory and processor; The memory is used to store programs; The processor is used to execute the program and implement the method-level idempotency solution according to claim 1 when executing the program.
3. A storage medium storing instructions executable by a processor, characterized in that: The processor executable instructions are used to perform the method-level idempotence solution as claimed in claim 1 when executed by the processor.
