Safe implementation method of TickLock lock under multi-process asynchronous call

The distributed lock mechanism protects TickLock locks in a multi-process asynchronous calling environment, solves the problem of inconsistent lock status, and realizes the security of TickLock under multi-process conditions and the smoothness of asynchronous IO execution.

CN118626283APending Publication Date: 2024-09-10BEIJING FENBEIJINFU TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410722225.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-05
Publication Date
2024-09-10

AI Technical Summary

Technical Problem

Under multi-process asynchronous calling conditions, the status of the TickLock lock may be modified by multiple processes at the same time, resulting in inconsistency.

Method used

The distributed lock mechanism is used to try to obtain the only distributed lock of the current conversation. If it fails, it gives up the CPU and waits to reacquire the lock. After success, the TickLock workflow is executed, and distributed lock protection is performed in each step to ensure Consistency of TickLock state.

Benefits of technology

In a multi-process environment, ensure that the status of TickLock is not changed by the outside world, ensuring the smooth execution of asynchronous IO and the security of multi-process access.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118626283A
    Figure CN118626283A
Patent Text Reader

Abstract

The invention discloses a security implementation method of a TickLock lock under multi-process asynchronous call, and relates to the field of instant messaging and dialogue processing. Compared with a previous TickLock working process, the problem that the state of a lock is modified by a plurality of processes at the same time under the condition of multi-process asynchronous calling is solved, on the basis that asynchronous IO execution of the TickLock is reserved, basic steps of asynchronous IO execution of the TickLock are divided, distributed lock protection is conducted on the divided basic steps, and therefore the safety of the TickLock is improved. In the process of implementing asynchronous IO execution, the state change of the TickLock does not conflict among multiple processes; therefore, the use safety of the TickLock is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of big data technology, and in particular to a secure implementation method of TickLock lock under multi-process asynchronous call. Background Art

[0002] TickLock itself is a classic lock mechanism, which can ensure that requests for using the lock (requests) obtain the access opportunity to the lock in the order of queuing. When this first-come, first-served mechanism is used in the processing of conversation information, the messages sent to the same Conversation ID can be consumed sequentially according to the arrival time.

[0003] The prior art has realized asynchronous call under a single process. The working process of the TickLock of the specific single process implementation is as Figure 1 shown. It can be seen from the figure that under the implementation conditions of a single process + asynchronous IO, there is always only one execution line in the above flowchart, so there is no need to add a separate lock for protection. Executing the above flowchart in a multi-threaded manner can greatly improve the execution efficiency. However, if it is under multi-process conditions and asynchronous calls are made inside each process. The current implementation will have a situation where the state of the lock is modified by multiple processes at the same time. Suppose there are two messages of the same Conversation, named M1 and M2 respectively; their timestamps are (T1, T2); and T1 < T2. If they fall on two processes (P1, P2), that is, M1 is processed on process P1; M2 is processed on process P2; then P1 and P2 may, under certain conditions, execute any group of 1.1 - 1.3, 2.1 - 2.3, 3.1 - 3.3 in the above flowchart at the same time, which will cause the state of the TickLock protecting this Conversation to be inconsistent as seen from P1 and P2 respectively.

[0004] To solve the above problems, the present invention proposes a secure implementation method of TickLock lock under multi-process asynchronous call. Summary of the Invention

[0005] The purpose of the present invention is to propose a secure implementation method of TickLock lock under multi-process asynchronous call to solve the problems raised in the background art:

[0006] Under the condition of multi-process asynchronous call, there will be a situation where the state of the lock is modified by multiple processes at the same time.

[0007] To achieve the above purpose, the present invention adopts the following technical solutions:

[0008] A secure implementation method of TickLock lock under multi-process asynchronous call includes the following steps:

[0009] S1: Try to obtain the unique distributed lock S corresponding to the current conversation;

[0010] S2: If the acquisition fails, the current CPU is abandoned, and S1 is re-executed after waiting for t seconds. If the acquisition succeeds, the corresponding sub-steps in the TickLock workflow protected by the unique distributed lock S are executed.

[0011] S3: Release the distributed lock S.

[0012] Preferably, the sub-steps in the TickLock workflow in S2 include sub-step one, sub-step two and sub-step three.

[0013] Preferably, the sub-step 1 is specifically as follows:

[0014] S2.1.1: Get the persistent instance of the lock from the distributed storage and name it L in the memory;

[0015] S2.1.2: Apply for a ticket for the current request and insert it into L->tickets;

[0016] S2.1.3: Write L back to the distributed storage.

[0017] Preferably, the request represents a text currently input by the user and sent to the server waiting for a response / reply from the system.

[0018] Preferably, L->tickets represents a queue for storing tickets during the TickLock implementation process.

[0019] Preferably, the second sub-step is specifically as follows:

[0020] S2.2.1: Get the persistent instance of the lock from the distributed storage and name it L in the memory;

[0021] S2.2.2: Check if L->now_serving is equal to ticket,

[0022] S2.2.3: If they are not equal, give up the current CPU time slice, wait for 1 second, and re-execute S2.2.1.

[0023] Preferably, L->now_serving indicates the ticket corresponding to the request being processed in the queue.

[0024] Preferably, after the execution of sub-step 2 is completed, the request holding lock is acquired and operations unrelated to the lock are performed to wait. If L->now_serving is equal to ticket, it is the turn of the current request to hold the lock, and jump to sub-step 3.

[0025] Preferably, the sub-step three is specifically as follows:

[0026] S2.3.1: Get the persistent instance of the lock from the distributed storage and name it L in the memory;

[0027] S2.3.2: Delete the ticket currently pointed to by L->now_serving from L->tickets;

[0028] S2.3.3: Write L back to persistent storage.

[0029] Compared with the prior art, the present invention provides a method for safely implementing TickLock locks under multi-process asynchronous calls, which has the following beneficial effects:

[0030] The present invention divides the basic steps of the asynchronous IO execution of TickLock on the basis of retaining the asynchronous IO execution of TickLock, and performs distributed lock protection on the divided basic steps respectively. In the process of implementing the asynchronous IO execution, the state of TickLock cannot be changed by the outside world. While ensuring that the TickLock workflow can still work smoothly under multi-threaded conditions, the security of multi-process access to TickLock is guaranteed. BRIEF DESCRIPTION OF THE DRAWINGS

[0031] Figure 1 The flowchart of TickLock mentioned in the background technology of the present invention;

[0032] Figure 2 This is a flow chart of the method mentioned in Example 1 of the present invention;

[0033] Figure 3 This is a workflow diagram of TickLock of the ConversationLock lock application mentioned in Example 1 of the present invention. DETAILED DESCRIPTION

[0034] The technical solutions in the embodiments of the present invention will be described clearly and completely below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments.

[0035] The present invention divides the basic steps of TickLock's asynchronous IO execution on the basis of retaining the asynchronous IO execution of TickLock, and respectively performs distributed lock protection on the divided basic steps, so that the state of TickLock cannot be changed by the outside world during the asynchronous IO execution, and ensures that the TickLock workflow can still work smoothly under multi-threaded conditions, while ensuring the security of multi-process access to TickLock. Specifically includes the following contents.

[0036] Embodiment 1:

[0037] See also Figure 2-3 The present invention provides a method for safely implementing a TickLock lock under a multi-process asynchronous call, comprising:

[0038] S1: Try to obtain the unique distributed lock S corresponding to the current Conversation; the details are as follows:

[0039] Get the process corresponding to the timestamp of each named message in the current Conversation, and access the current message in the order of the timestamp. Different messages correspond to unique distributed locks.

[0040] S2: If the acquisition fails, the current CPU is abandoned, and S1 is re-executed after waiting for t seconds. If the acquisition succeeds, the corresponding sub-steps in the TickLock workflow protected by the unique distributed lock S are executed. The details are as follows:

[0041] If the unique distributed lock S corresponding to the current Conversation is not obtained, the current CPU is abandoned, and t seconds are waited to re-acquire the unique distributed lock S corresponding to the current Conversation until it is successfully acquired. If it is successfully acquired, it can be executed Figure 2 The workflow in Figure 2 The workflow in the example uses ConversationLock to perform distributed protection on each sub-step of the message in each Conversation, such as performing distributed protection on sub-step 1, sub-step 2, and sub-step 3. For details, please refer to the following pseudo code:

[0042] With ConversationLock(S):

[0043] do_step("1.1")

[0044] do_step("1.2")

[0045] do_step("1.3")

[0046] With ConversationLock(S):

[0047] while True:

[0048] do_step("2.1")

[0049]

[0050] The above pseudo code is mainly used to implement distributed protection for each sub-step, which can ensure the smooth progress of each step in the workflow.

[0051] In step 1, first obtain the persistent instance of the lock from the distributed storage and name it L in the memory; apply for a ticket for the current request and insert it into L->tickets; write L back to the distributed storage. This step is mainly used to apply for a ticket for the new user request and insert it into the ticket queue of the TicketLock representing the current conversation;

[0052] In step 2, obtain the persistent instance of the lock from the distributed storage and name it L in the memory; check whether L->now_serving is equal to ticket. If not, give up the current CPU time slice, wait for 1 second, and re-execute 2.2.1.

[0053] This step is mainly used to wait in a loop until it is the current request's turn to hold the lock. After step 2 is completed, if the lock is acquired, some operations unrelated to the lock can be performed, such as processing the current message, etc., waiting for the tasks in the lock to be completed. This step can ensure that the protection function of the lock is not affected and the TicketLock process is carried out smoothly.

[0054] In step 3, the persistent instance of the lock is also obtained from the distributed storage and named L in the memory; the ticket pointed to by the current L->now_serving is deleted from L->tickets; and L is written back to the persistent storage. This step is mainly used to delete all the tickets requested by the current user from the ticket queue of TicketLock so that they no longer occupy memory.

[0055] S3: Release the distributed lock S. The details are as follows:

[0056] When the tickets of sub-step 1 / sub-step 2 / sub-step 3 are deleted from the ticket queue of TicketLock, there are no work tasks in these sub-steps, and there is no need to perform distributed locking protection on sub-step 1 / sub-step 2 / sub-step 3, so the corresponding distributed locks S in these sub-steps can be released.

[0057] Embodiment 2:

[0058] The entire process can be implemented using Python. For example, taking a number at a bank to handle a business, in an asynchronous IO environment, even though there is only one thread, it actually appears that multiple threads are executing independently of each other. Therefore, the seemingly independent threads are divided into two roles: bank tellers and customers.

[0059] For customers, they need to go to the bank to handle business, so they first get a number at the door. If the bank teller is free, they go directly to handle the business. If the bank teller is busy, they wait for their number to be called. During the waiting process, the customer asks the bank teller when it will be his turn. The bank teller tells him that his number will be called when it is his turn, so the customer continues to wait; after waiting for several turns, the bank teller calls the customer's number, the customer goes to handle the business, and the customer leaves after the business is completed.

[0060] For the teller, he / she calls the customers' numbers in the order in which they took their numbers and handles the corresponding business for them. After completing the business of one customer, he / she calls the customers' numbers in the order in which they queue and handles the business of the next customer until all the customers' business is completed.

[0061] The above description is only a preferred specific implementation manner of the present invention, but the protection scope of the present invention is not limited thereto. Any technician familiar with the technical field can make equivalent replacements or changes according to the technical scheme and inventive concept of the present invention within the technical scope disclosed by the present invention, which should be covered by the protection scope of the present invention.

Claims

1. A method for safely implementing TickLock lock under multi-process asynchronous calls, characterized in that: The steps include: S1: Try to obtain the unique distributed lock S corresponding to the current conversation; S2: If the acquisition fails, the current CPU is abandoned, and S1 is re-executed after waiting for t seconds. If the acquisition succeeds, the corresponding sub-steps in the TickLock workflow protected by the unique distributed lock S are executed. S3: Release the distributed lock S.

2. According to the method for securely implementing TickLock lock under multi-process asynchronous call in claim 1, it is characterized in that: The sub-steps in the TickLock workflow described in S2 include sub-step one, sub-step two, and sub-step three.

3. The method for safely implementing TickLock lock under multi-process asynchronous call according to claim 2 is characterized in that: The sub-step 1 is specifically as follows: S2.1.1: Get the persistent instance of TickLock from the distributed storage and name it L in the memory; S2.1.2: Apply for a ticket for the current request and insert it into L->tickets; S2.1.3: Write L back to the distributed storage.

4. The method for safely implementing TickLock in a multi-process asynchronous call according to claim 3 is characterized in that: The request represents a text currently input by the user and sent to the server waiting for a response / reply from the system.

5. The method for safely implementing TickLock in a multi-process asynchronous call according to claim 3 is characterized in that: The L->tickets represents a queue for storing tickets during the TickLock implementation process.

6. The method for safely implementing TickLock in a multi-process asynchronous call according to claim 2, characterized in that: The sub-step 2 is specifically as follows: S2.2.1: Get the persistent instance of the lock from the distributed storage and name it L in the memory; S2.2.2: Check whether L->now_serving (L->now_serving indicates the ticket corresponding to the lock currently occupying the TickLock request) is equal to the ticket corresponding to the current request; S2.2.3: If they are not equal, abandon the current CPU time slice, wait for 1 second, and return to re-execute S2.2.

1.

7. The method for safely implementing TickLock in a multi-process asynchronous call according to claim 6, characterized in that: The L->now_serving indicates the ticket corresponding to the request being processed in the queue.

8. The method for safely implementing TickLock in a multi-process asynchronous call according to claim 7, characterized in that: After the execution of the second sub-step is completed, the request holding lock is obtained and operations unrelated to the lock are performed to wait. If L->now_serving is equal to the ticket, it is the turn of the current request to hold the lock, and then jump to the third sub-step.

9. The method for safely implementing TickLock in a multi-process asynchronous call according to claim 2, characterized in that: The sub-step three is as follows: S2.3.1: Get the persistent instance of the lock from the distributed storage and name it L in the memory; S2.3.2: Delete the claim form of the ticket currently pointed to by L->now_serving from L->tickets; S2.3.3: Write L back to persistent storage.