Data processing method and device based on distributed optimistic lock, equipment and medium
By using distributed optimistic locks in the cache of distributed systems for version number matching and updating, the problem of inconsistency between cache and database data is solved, and data consistency and high concurrency performance are achieved.
Patent Information
- Application Number
- CN202510026372.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-08
- Publication Date
- 2025-02-14
- Estimated Expiration
- 2045-01-08
AI Technical Summary
In distributed systems, optimism locking is prone to inconsistent cache and database data.
Update data in structured database and cache by obtaining distributed optimistic locks in the cache and performing version number matching when data is written to requests, updating the optimistic lock version number, and releasing the lock after the update is completed.
It effectively prevents data coverage problems caused by multiple threads to modify the same data item at the same time, maintains data consistency, and improves concurrency performance and read and write speed.
Smart Images

Figure CN119441244A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of distributed systems, and in particular to a data processing method, apparatus, device and medium based on distributed optimistic locking. Background Art
[0002] Multi-Version Concurrency Control (MVCC) in MySQL is a typical way to implement optimistic locking. It allows read operations without waiting for write operations to complete, thereby improving the concurrency performance of the system. To support this mechanism, a version field is usually added to the table structure in database design to identify the version information of each record. However, in a distributed system environment, due to the communication between multiple nodes and the collaboration between different services, there may be a cache layer between the client and the server in a distributed system. If the cache is not updated in time, the client will read expired data, causing inconsistencies between the cache and database data. Therefore, optimistic locking that relies on the database level is prone to inconsistencies between the cache and database data.
[0003] The above contents are only used to assist in understanding the technical solution of the present application and do not constitute an admission that the above contents are prior art. Summary of the invention
[0004] The main purpose of the present application is to provide a data processing method, device, equipment and medium based on distributed optimistic locking, aiming to solve the technical problem that the application of optimistic locking is prone to inconsistency between cache and database data.
[0005] To achieve the above purpose, the present application proposes a data processing method based on distributed optimistic locking, which is applied to the current thread. The data processing method based on distributed optimistic locking includes: When a data write request is received, a distributed optimistic lock is acquired from the cache; Matching the request version number carried in the data write request with the optimistic lock version number of the distributed optimistic lock record; If the match is successful, the optimistic lock version number of the distributed optimistic lock record is updated, and after the update is completed, the distributed optimistic lock is released; Target write data is acquired from the data write request, and database data in the structured database and cache data in the cache are updated according to the target write data.
[0006] In one embodiment, the step of updating the optimistic lock version number of the distributed optimistic lock record includes: Determine the key-value pair data corresponding to the distributed optimistic lock, wherein the primary key of the key-value pair data is a device identifier, and the value of the key-value pair data records the optimistic lock version number; Extracting an optimistic lock version number from the key-value pair data; Increment the optimistic lock version number based on a preset version update rule to obtain an updated version number; The updated version number is stored as the optimistic lock version number in the key-value pair data corresponding to the distributed optimistic lock.
[0007] In one embodiment, the step of updating the database data in the structured database and the cache data in the cache according to the target write data includes: Parse the target write data to determine the table name, field and condition to be written; Construct a database update statement based on the table name, field and condition; Based on the database update statement, the database data in the structured database is updated, and the version number field of each database data is incrementally updated corresponding to the database version number, to determine the updated database version number of each database data; The cached data in the cache is updated according to the updated database data, and the version number field of each database data is incrementally updated corresponding to the cache version number to determine the cache version number after the update of each database data, wherein the initial values of the optimistic lock version number, cache version number and database version number are the same.
[0008] In one embodiment, when a data write request is received, the step of acquiring a distributed optimistic lock from the cache further includes: Obtain a preset synchronization lock, wherein, when a thread obtains the preset synchronization lock, only the current thread is allowed to execute operations of obtaining a distributed optimistic lock and updating an optimistic lock version number of the distributed optimistic lock record; The step of updating the optimistic lock version number of the distributed optimistic lock record and releasing the distributed optimistic lock after the update is completed also includes: The preset synchronization lock is released.
[0009] In one embodiment, the data processing method based on distributed optimistic locking further includes: When a data read request is received, a distributed optimistic lock is obtained from the cache; Matching the request version number carried in the data read request with the updated optimistic lock version number of the distributed optimistic lock record; If the match is successful, obtaining the target data corresponding to each of the data read requests from the cache data in the cache; If the match fails, the match failure information is returned.
[0010] In one embodiment, after the step of acquiring the target data corresponding to each of the data read requests from the cached data of the cache, the method further includes: If the target data does not exist in the cache data of the cache, the target data corresponding to each of the data read requests is obtained from the structured database, and the cache data in the cache is updated according to each of the target data.
[0011] In addition, to achieve the above purpose, the present application also proposes a data processing device based on distributed optimistic locking, and the data processing device based on distributed optimistic locking includes: The lock acquisition module is used to acquire the distributed optimistic lock from the cache when receiving a data write request; A matching module, used to match the request version number carried in the data write request with the optimistic lock version number of the distributed optimistic lock record; An update module, configured to update the optimistic lock version number of the distributed optimistic lock record if the match is successful, and release the distributed optimistic lock after the update is completed; A writing module is used to obtain target writing data from the data writing request, and update the database data in the structured database and the cache data in the cache according to the target writing data.
[0012] In addition, to achieve the above-mentioned objectives, the present application also proposes a data processing device based on distributed optimistic locking, the device comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the data processing method based on distributed optimistic locking as described above.
[0013] In addition, to achieve the above-mentioned purpose, the present application also proposes a storage medium, which is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by the processor, the steps of the data processing method based on distributed optimistic locking as described above are implemented.
[0014] In addition, to achieve the above-mentioned purpose, the present application also provides a computer program product, which includes a computer program, and when the computer program is executed by a processor, it implements the steps of the data processing method based on distributed optimistic locking as described above.
[0015] This application obtains a distributed optimistic lock from the cache when a data write request is received; matches the request version number carried in the data write request with the optimistic lock version number of the distributed optimistic lock record; if the match is successful, the optimistic lock version number of the distributed optimistic lock record is updated, and after the update is completed, the distributed optimistic lock is released; obtains the target write data from the data write request, and updates the database data in the structured database and the cache data in the cache according to the target write data. This application sets an optimistic lock in the cache instead of in the structured database, which improves the concurrency performance and read-write speed. Through the version number matching mechanism, it ensures that updates are allowed only when the version number carried by the request is consistent with the version number of the current record, effectively preventing data overwriting problems caused by multiple threads modifying the same data item at the same time, thereby maintaining data consistency. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0017] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0018] Figure 1 A flow chart of the first embodiment of the data processing method based on distributed optimistic locking provided in the present application; Figure 2 A brief flowchart of a data processing method based on distributed optimistic locking provided in Example 2 of the present application; Figure 3 A flow chart of the second embodiment of the data processing method based on distributed optimistic locking provided in this application; Figure 4 This is a schematic diagram of the module structure of a data processing device based on distributed optimistic locking in an embodiment of the present application; Figure 5 Schematic diagram of the device structure of the hardware operating environment involved in the data processing method based on distributed optimistic locking in the embodiment of the present application.
[0019] The purpose, features and advantages of this application will be further described in conjunction with the embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0020] It should be understood that the specific embodiments described herein are only used to explain the technical solutions of the present application and are not used to limit the present application.
[0021] In order to better understand the technical solution of the present application, a detailed description will be given below in conjunction with the accompanying drawings and specific implementation methods.
[0022] It should be noted that the execution subject of this embodiment may be a computing service device with data processing, network communication and program running functions, such as a tablet computer, a personal computer, a mobile phone, etc., or an electronic device capable of implementing the above functions, a thread, etc. The following takes the current thread as an example to illustrate this embodiment and the following embodiments.
[0023] Based on this, the embodiment of the present application provides a data processing method based on distributed optimistic locking, referring to Figure 1 , Figure 1 This is a flow chart of the first embodiment of the data processing method based on distributed optimistic locking in this application.
[0024] In this embodiment, the data processing method based on distributed optimistic locking includes steps S10 to S40: Step S10, when a data write request is received, a distributed optimistic lock is obtained from the cache.
[0025] It should be noted that the execution subject of this embodiment is the current thread. In a distributed system, when an application receives a data write request, it will assign this request to an available thread for processing. The thread will call the API (Application Programming Interface) provided by the Redis (Remote Dictionary Server) client library to try to acquire the lock. Each thread is independent when processing write requests, and they each try to acquire the lock without interfering with each other. Even if multiple threads try to acquire the lock of the same resource at the same time, only the first successful thread can continue to perform the update operation, and other threads will be blocked or required to retry. In this embodiment, the cache refers to Redis. Redis is an open source, high-performance in-memory database, usually used as a data structure server. All data is stored in memory, so Redis provides extremely high read and write speeds and is suitable for application scenarios that require fast response.
[0026] First, you need to determine whether there is a lock key corresponding to the target resource in the cache. The lock key usually consists of a resource ID (Identity document) and a version number. Redis provides the SETNX (Set if NotExists) command, which will only set the key-value pair when the key does not exist. If 1 is returned, it means that the lock is successfully acquired; if 0 is returned, it means that the lock is already held by another client. By acquiring the lock, you can ensure that only one thread can modify the database at the same time, preventing data conflicts or overwriting caused by multiple threads updating at the same time.
[0027] Step S20, matching the request version number carried in the data write request with the optimistic lock version number of the distributed optimistic lock record.
[0028] It should be noted that the request version number is extracted from the data write request. This version number is usually obtained when reading data and sent to the server together with subsequent write requests for data update. The optimistic lock version number of the distributed optimistic lock record is obtained from the cache. This version number reflects the latest status of the current resource. The request version number is strictly compared with the optimistic lock version number as a numeric value or string. If the two are equal, the versions are considered to match; otherwise, the versions do not match. If the version numbers match, the update operation is allowed and the version number is incremented to reflect the new status; if the version numbers do not match, the update request is rejected, and an error is usually returned to the client, prompting the user to try again or take other measures.
[0029] If a version number mismatch occurs, it may be because other threads have updated the same resource, causing the optimistic lock version number to change, or because of network delays or other factors, the request version number read by the client may be expired. If the lock is set with TTL (Time To Live), it may be automatically released due to timeout, causing other threads to acquire the lock and update the version number.
[0030] By matching the version number, we can ensure that updates are allowed only when the request version number carried by the request is consistent with the optimistic lock version number of the current record, effectively preventing data overwriting problems caused by multiple threads modifying the same data item at the same time. The version number mechanism is not only used to control concurrent access, but also helps track historical changes in data, facilitating auditing and rollback operations.
[0031] Step S30: If the match is successful, the optimistic lock version number of the distributed optimistic lock record is updated, and after the update is completed, the distributed optimistic lock is released.
[0032] It should be noted that the operations of updating the optimistic lock version number and releasing the optimistic lock are both executed by the current thread. The optimistic lock version number of the distributed optimistic lock record is updated to reflect the latest status of the resource. The version number is incremented after each successful update to ensure that each version is unique, thereby maintaining data consistency and historical traceability.
[0033] By updating the version number and then releasing the lock, you can ensure that only one thread can modify a specific resource at the same time, avoiding data overwrite problems caused by multiple threads updating at the same time.
[0034] In a feasible implementation manner, step S30 may include steps A10 to A40: Step A10, determining the key-value pair data corresponding to the distributed optimistic lock, wherein the primary key of the key-value pair data is the device identifier, and the value of the key-value pair data records the optimistic lock version number.
[0035] It should be noted that distributed optimistic locking exists in the form of key-value pair data, such as id_1, (id_1+v_2), where the primary key id_1 is the device identifier and the value (id_1+v_2) is the combination of the device identifier and the optimistic locking version number.
[0036] Step A20, extracting the optimistic lock version number from the key-value pair data.
[0037] Step A30, incrementing the optimistic lock version number based on a preset version update rule to obtain an updated version number.
[0038] It should be noted that the most common case of the preset version update rule is to simply add 1 to second_version. This rule is suitable for most scenarios because it can effectively track the number of resource changes.
[0039] Step A40, storing the updated version number as the optimistic lock version number in the key-value pair data corresponding to the distributed optimistic lock.
[0040] It should be noted that, for the original distributed optimistic lock, it is necessary to replace the original optimistic lock version number in the key-value pair data with the updated update version number, so as to update the distributed optimistic lock.
[0041] In a feasible implementation manner, while executing step S10, step S01 may also be included: Step S01, obtaining a preset synchronization lock, wherein, when a thread obtains the preset synchronization lock, only the current thread is allowed to execute operations of obtaining a distributed optimistic lock and updating an optimistic lock version number of a distributed optimistic lock record.
[0042] It should be noted that in this embodiment, the preset synchronization lock can be implemented by synchronized. Synchronized can be applied to methods or code blocks to limit the access of multiple threads to shared resources. Only one thread can enter the synchronized protected method or code block at the same time, and other threads must wait until the current thread releases the lock.
[0043] Then, after step S30, step S02 is further included: Step S02, releasing the preset synchronization lock.
[0044] In Java, when a thread leaves a synchronized method or code block, the Java virtual machine automatically releases the lock held by the thread. The synchronized method locks the current instance object, while the synchronized code block can specify any object as the lock object. Once the thread leaves these methods or code blocks, the lock will be automatically released.
[0045] In a distributed system or a high-concurrency environment, this implementation acquires a preset synchronization lock before a thread attempts to perform a critical operation to ensure that only one thread can perform these operations at the same time, which can further ensure thread safety and data consistency.
[0046] Step S40, obtaining target write data from the data write request, and updating database data in the structured database and cache data in the cache according to the target write data.
[0047] Specifically, parse the target data to be updated from the data write request. Usually, the request will contain the data items to be updated and their version number information. According to the parsed target data, build the corresponding SQL (Structured Query Language) update statement and ensure that all related changes are either successful or rolled back. While updating the structured database, increment the version number to reflect the latest data status. After updating the structured database, immediately read the latest data from the database instead of directly using the data in the write request. This ensures that the data in the cache is up to date and avoids data inconsistency caused by concurrent updates. Update the latest data to the cache to ensure that the cache is synchronized with the persistence layer.
[0048] It is understandable that if you update the cache first and then the structured database, some reasons (such as network failure, server crash) may lead to partial updates, that is, only the cache is updated but the database is not updated, or only the database is updated but the cache is not updated. Therefore, always update the structured database first and then the cache. Even if the cache update fails, it will not affect the data consistency of the persistence layer. Because the version number matching mechanism is used to ensure that each update is based on the latest data status, there will be no concurrent conflicts caused by multiple threads trying to update the same resource.
[0049] Specifically, the cache contains a distributed optimistic lock, which is a key-value pair data. The primary key of the key-value pair data is a device identifier, and the value of the key-value pair data records the optimistic lock version number.
[0050] It should be noted that distributed optimistic locking is a concurrency control mechanism that ensures that no conflict occurs when multiple clients update the same data in a high-concurrency environment. It tracks data changes through version numbers or other identifiers, allowing the system to check whether other transactions have modified the data before performing an update operation. The device identifier is used to uniquely identify each device, and the optimistic lock version number records the version number used by the optimistic lock.
[0051] The cache also contains multiple cache data, which are caches of database data. Each cache data contains a version number field, and the version number field of the cache data is used to record the cache version number.
[0052] It should be noted that the cached data is a copy of the data in the structured database, which is used to speed up the read operation. The cache version number records the version of the cached data to ensure consistency with the data in the structured database. The cache version number ensures that the data in the cache is the latest and consistent with the data in the structured database.
[0053] The structured database contains multiple database data, each of which contains a version number field. The version number field of the database data is used to record the database version number. The optimistic lock version number, cache version number and database version number have the same initial value.
[0054] It should be noted that the database data stores actual business data, including multiple fields such as id, name, value, etc. The database version number records the current version of the record to support the optimistic locking mechanism. By incrementing the database version number, it ensures that each update is based on the latest data version to prevent concurrent conflicts.
[0055] The initial values of the request version number, cache version number, and database version number are the same, usually 1 or other specified starting values. The optimistic lock version number is incremented when the distributed optimistic lock in the cache is requested to write data, ensuring that only one thread is allowed to perform data write operations; the cache version number is incremented when the cached data is updated in the cache to ensure consistency with the structured database; the database version number is incremented each time the structured database is updated to ensure that each update is based on the latest version of the data.
[0056] In a feasible implementation manner, step S40 may include steps B10 to B40: Step B10, parse the target write data and determine the table name, field and conditions to be written.
[0057] It should be noted that users submit update requests through some kind of interface, carrying the data to be updated, usually in JSON (JavaScript Object Notation, JavaScript, object representation) or XML (Extensible Markup Language, Extensible Markup Language) format, including specific update information. The target write data contains a variety of information, including: table name, that is, the name of the database table to be updated; field, the specific field to be updated and its new value; condition, that is, which records should be updated, usually a WHERE clause in a SQL query, used to accurately match the records to be updated.
[0058] Step B20, constructing a database update statement based on the table name, fields and conditions.
[0059] It should be noted that we start with a basic SQL update statement template, including the UPDATE keyword, table name, SET clause, and WHERE clause. We dynamically build the SET clause based on the parsed fields and their new values. We dynamically build the WHERE clause based on the parsed conditions. We merge the SET clause and the WHERE clause together to form a complete SQL update statement.
[0060] Step B30, updating the database data in the structured database based on the database update statement, and incrementally updating the version number field of each database data corresponding to the database version number, to determine the updated database version number of each database data.
[0061] It should be noted that a complete SQL update statement has been constructed in step B20. Now you need to execute this statement through JDBC (Java Database Connectivity) or other database connection libraries. Include the increment operation of the version number field in the SET clause, such as fifth_version=fifth_version+1, to ensure that the version number is automatically incremented each time the update is successful.
[0062] Step B40, update the cached data in the cache according to the updated database data, and incrementally update the version number field of each database data corresponding to the cache version number to determine the updated cache version number of each database data, wherein the initial values of the optimistic lock version number, cache version number and database version number are the same.
[0063] Specifically, execute a query in the structured database to obtain the data that has just been updated, map the data obtained from the database to the cache object in the cache, and update the data in the cache through Redis commands or client libraries. Maintain an additional version number field (such as fourth_version) for each record in the cache for synchronous updates. Each time the cache is updated, the value of the fourth_version field is incremented. After the update operation is completed, query and confirm the latest fourth_version.
[0064] This implementation can ensure data consistency and integrity in a high-concurrency environment while improving system performance and user experience. It not only solves potential data conflicts and inconsistencies, but also significantly improves system performance and reliability.
[0065] This embodiment obtains a distributed optimistic lock from the cache when it detects that the current thread has received a data write request; matches the request version number carried in the data write request with the optimistic lock version number of the distributed optimistic lock record; if the match is successful, the optimistic lock version number of the distributed optimistic lock record is updated, and after the update is completed, the distributed optimistic lock is released; the target write data is obtained from the data write request, and the structured database and cache are updated according to the target write data. This embodiment sets the optimistic lock in the cache instead of in the structured database, which improves the concurrency performance and read-write speed. Through the version number matching mechanism, it ensures that updates are allowed only when the version number carried by the request is consistent with the version number of the current record, effectively preventing data overwriting problems caused by multiple threads modifying the same data item at the same time, thereby maintaining data consistency.
[0066] For example, in order to help understand the implementation process of the data processing method based on distributed optimistic locking obtained by combining this embodiment with the above-mentioned embodiment 1, please refer to Figure 2, Figure 2 A brief flow diagram of a data processing method based on distributed optimistic lock is provided. Specifically: the process starts from the "start" node. Thread A gets the lock, matches the version, and passes; at the same time, thread B also tries to get the lock, but because the lock is already occupied by thread A, thread B enters a blocked state; thread A changes the version to v_2; thread A temporarily releases the lock, enters a blocked state, and waits for subsequent operations; thread B tries to get the lock again, but at this time the version has been updated to v_2, which does not match the expected version of thread B, so thread B returns directly, indicating failure; thread A is unblocked, continues to execute, and modifies the data in the structured database; thread A updates the cached data in the unstructured database (such as Redis); the process ends.
[0067] Based on the first embodiment of the present application, in the second embodiment of the present application, the same or similar contents as those in the above-mentioned embodiment 1 can be referred to the above introduction, and will not be repeated in the following. Figure 3 , the data processing method based on distributed optimistic locking also includes steps S50 to S80: Step S50, when a data read request is received, a distributed optimistic lock is obtained from the cache.
[0068] It should be noted that after a data read request is sent, if the optimistic lock version number is modified, it means that other threads have updated the resource. At this time, reading data directly from the cache may result in reading expired data. By acquiring an optimistic lock and matching the version number, the read operation is ensured to be based on the latest data status. Although the read operation itself does not require locking, in this case, acquiring an optimistic lock can ensure that only one thread can successfully read the latest data at the same time, and subsequent write operations can also be performed based on the latest version.
[0069] Step S60: Match the request version number carried in the data read request with the updated optimistic lock version number of the distributed optimistic lock record.
[0070] It should be noted that the request version number is the version number carried by the client when initiating a read request, which is usually obtained from a previous read or write operation. The optimistic lock version number is the latest version number of the distributed optimistic lock record in the cache, reflecting the latest status of the resource.
[0071] Step S70: if the match is successful, the target data corresponding to each data read request is obtained from the cached data in the cache.
[0072] It should be noted that the cache is usually used as a cache layer to store copies of data in a structured database. Each cached data contains a version number field, which is used to record the optimistic lock version number synchronized with the structured database. When the version number matches successfully, the server will search the cache for the corresponding data based on the resource identifier in the request. If the corresponding data exists in the cache, it will be directly returned to the client.
[0073] In addition, after the data is read successfully, the read version number will also be returned to the client at the same time, so that the client can use it as the request version number or optimistic lock version number when writing or reading data next time.
[0074] In a feasible implementation manner, after step S70, step C10 may also be included: Step C10: if the target data does not exist in the cached data, the target data corresponding to each data read request is obtained from the structured database, and the cached data in the cache is updated according to each target data.
[0075] It should be noted that if the corresponding data does not exist in the cache, it may be because the data has not been cached or the cache has expired. At this time, it is necessary to re-read the latest data from the structured database and update it to the cache to ensure that the cache is consistent with the persistence layer.
[0076] Step S80: If the matching fails, a matching failure message is returned.
[0077] Other threads or clients update the same resource within the same time window, causing the version number to change, or due to network delays or other factors, the requested version number read by the client may be outdated.
[0078] This embodiment obtains a distributed optimistic lock from the cache when it detects that multiple threads have received a data read request; matches the request version number carried in the data read request with the updated optimistic lock version number of the distributed optimistic lock record; if the match is successful, the target data corresponding to each data read request is obtained from the cached data of the cache; if the match fails, the match failure information and the updated optimistic lock version number are returned, and the updated data read request is received based on the updated optimistic lock version number, and the execution step is returned according to the updated data read request: obtain the distributed optimistic lock from the cache. This embodiment ensures that each read is based on the latest data status through the version number matching mechanism, effectively preventing the "dirty read" problem, ensuring the consistency and integrity of data in a high concurrency environment, and improving the system performance and user experience.
[0079] It should be noted that the above examples are only used to understand the present application and do not constitute a limitation on the data processing method based on distributed optimistic locking in the present application. More simple transformations based on this technical concept are all within the scope of protection of the present application.
[0080] This application also provides a data processing device based on distributed optimistic locking, please refer to Figure 4 , the data processing device based on distributed optimistic locking includes: The lock acquisition module 10 is used to acquire the distributed optimistic lock from the cache when receiving a data write request; A matching module 20, configured to match the request version number carried in the data write request with the optimistic lock version number of the distributed optimistic lock record; An updating module 30 is used to update the optimistic lock version number of the distributed optimistic lock record if the match is successful, and release the distributed optimistic lock after the update is completed; The write module 40 is used to obtain the target write data from the data write request, and update the database data in the structured database and the cache data in the cache according to the target write data. The data processing device based on distributed optimistic locking provided by the present application adopts the data processing method based on distributed optimistic locking in the above-mentioned embodiment, which can solve the technical problem that the application of optimistic locking is prone to inconsistency between cache and database data. Compared with the prior art, the beneficial effects of the data processing device based on distributed optimistic locking provided by the present application are the same as the beneficial effects of the data processing method based on distributed optimistic locking provided by the above-mentioned embodiment, and other technical features in the data processing device based on distributed optimistic locking are the same as the features disclosed in the above-mentioned embodiment method, which will not be repeated here.
[0081] The present application provides a data processing device based on distributed optimistic locking, and the data processing device based on distributed optimistic locking includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the data processing method based on distributed optimistic locking in the above-mentioned embodiment one.
[0082] Reference below Figure 5, which shows a schematic diagram of the structure of a data processing device based on distributed optimistic locking suitable for implementing the embodiment of the present application. The data processing device based on distributed optimistic locking in the embodiment of the present application may include but is not limited to mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Descriptions), PMPs (Portable Media Players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 5 The data processing device based on distributed optimistic locking shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0083] like Figure 5 As shown, the data processing device based on distributed optimistic locking may include a processing device 1001 (such as a central processing unit, a graphics processing unit, etc.), which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM: Read Only Memory) 1002 or a program loaded from a storage device 1003 to a random access memory (RAM: Random Access Memory) 1004. Various programs and data required for the operation of the data processing device based on distributed optimistic locking are also stored in RAM1004. The processing device 1001, ROM1002 and RAM1004 are connected to each other via a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Typically, the following systems can be connected to the I / O interface 1006: input devices 1007 including, for example, a touch screen, a touchpad, a keyboard, a mouse, an image sensor, a microphone, an accelerometer, a gyroscope, etc.; output devices 1008 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; storage devices 1003 including, for example, a magnetic tape, a hard disk, etc.; and communication devices 1009. The communication device 1009 can allow the data processing device based on distributed optimistic locking to communicate with other devices wirelessly or by wire to exchange data. Although the figure shows a data processing device based on distributed optimistic locking with various systems, it should be understood that it is not required to implement or have all the systems shown. More or fewer systems may be implemented or have alternatively.
[0084] In particular, according to the embodiments disclosed in the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present application include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device, or installed from a storage device 1003, or installed from a ROM 1002. When the computer program is executed by the processing device 1001, the above-mentioned functions defined in the method of the embodiment disclosed in the present application are executed.
[0085] The data processing device based on distributed optimistic locking provided by the present application adopts the data processing method based on distributed optimistic locking in the above embodiment, which can solve the technical problem that the application of optimistic locking is prone to inconsistency between cache and database data. Compared with the prior art, the beneficial effects of the data processing device based on distributed optimistic locking provided by the present application are the same as the beneficial effects of the data processing method based on distributed optimistic locking provided by the above embodiment, and other technical features in the data processing device based on distributed optimistic locking are the same as the features disclosed in the method of the previous embodiment, which will not be repeated here.
[0086] It should be understood that the various parts disclosed in this application can be implemented by hardware, software, firmware or a combination thereof. In the description of the above embodiments, specific features, structures, materials or characteristics can be combined in any one or more embodiments or examples in a suitable manner.
[0087] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
[0088] The present application provides a computer-readable storage medium having computer-readable program instructions (ie, computer programs) stored thereon, and the computer-readable program instructions are used to execute the data processing method based on distributed optimistic locking in the above-mentioned embodiment.
[0089] The computer-readable storage medium provided in the present application may be, for example, a USB flash drive, but is not limited to electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems or devices, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM: Random Access Memory), a read-only memory (ROM: Read Only Memory), an erasable programmable read-only memory (EPROM: Erasable Programmable Read Only Memory or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM: CD-Read Only Memory), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this embodiment, the computer-readable storage medium may be any tangible medium containing or storing a program, which may be used by or in combination with an instruction execution system or device. The program code contained on the computer-readable storage medium may be transmitted using any appropriate medium, including but not limited to: wires, optical cables, RF (Radio Frequency: Radio Frequency), etc., or any suitable combination of the above.
[0090] The computer-readable storage medium may be included in a data processing device based on distributed optimistic locking; or may exist independently without being assembled into a data processing device based on distributed optimistic locking.
[0091] The above-mentioned computer-readable storage medium carries one or more programs. When the above-mentioned one or more programs are executed by a data processing device based on distributed optimistic locking, the data processing device based on distributed optimistic locking: when receiving a data write request, obtains the distributed optimistic lock from the cache; matches the request version number carried in the data write request with the optimistic lock version number of the distributed optimistic lock record; if the match is successful, updates the optimistic lock version number of the distributed optimistic lock record, and releases the distributed optimistic lock after the update is completed; obtains the target write data from the data write request, and updates the database data in the structured database and the cache data in the cache according to the target write data.
[0092] Computer program code for performing the operations of the present application may be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, C++, and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0093] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present application. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0094] The modules involved in the embodiments described in this application may be implemented by software or hardware, wherein the name of the module does not constitute a limitation on the unit itself in some cases.
[0095] The readable storage medium provided by the present application is a computer-readable storage medium, which stores computer-readable program instructions (i.e., computer programs) for executing the above-mentioned data processing method based on distributed optimistic locking, and can solve the technical problem that the application of optimistic locking is prone to inconsistency between cache and database data. Compared with the prior art, the beneficial effects of the computer-readable storage medium provided by the present application are the same as the beneficial effects of the data processing method based on distributed optimistic locking provided by the above-mentioned embodiment, and will not be repeated here.
[0096] The present application also provides a computer program product, including a computer program, which implements the steps of the above-mentioned data processing method based on distributed optimistic locking when executed by a processor.
[0097] The computer program product provided by the present application can solve the technical problem that the application of optimistic locking is prone to inconsistency between cache and database data. Compared with the prior art, the beneficial effects of the computer program product provided by the present application are the same as the beneficial effects of the data processing method based on distributed optimistic locking provided by the above embodiment, and will not be repeated here.
[0098] The above descriptions are only some embodiments of the present application, and are not intended to limit the patent scope of the present application. All equivalent structural changes made using the contents of the present application specification and drawings under the technical concept of the present application, or direct / indirect applications in other related technical fields are included in the patent protection scope of the present application.
Claims
1. A data processing method based on distributed optimistic locking, characterized in that: Applied to the current thread, the data processing method based on distributed optimistic locking includes: When a data write request is received, a distributed optimistic lock is acquired from the cache; Matching the request version number carried in the data write request with the optimistic lock version number of the distributed optimistic lock record; If the match is successful, the optimistic lock version number of the distributed optimistic lock record is updated, and after the update is completed, the distributed optimistic lock is released; Target write data is acquired from the data write request, and database data in the structured database and cache data in the cache are updated according to the target write data.
2. The data processing method based on distributed optimistic locking according to claim 1, characterized in that: The step of updating the optimistic lock version number of the distributed optimistic lock record includes: Determine the key-value pair data corresponding to the distributed optimistic lock, wherein the primary key of the key-value pair data is a device identifier, and the value of the key-value pair data records the optimistic lock version number; Extracting an optimistic lock version number from the key-value pair data; Increment the optimistic lock version number based on a preset version update rule to obtain an updated version number; The updated version number is stored as the optimistic lock version number in the key-value pair data corresponding to the distributed optimistic lock.
3. The data processing method based on distributed optimistic locking according to claim 2, characterized in that: The step of updating the database data in the structured database and the cache data in the cache according to the target write data comprises: Parse the target write data to determine the table name, field and condition to be written; Construct a database update statement based on the table name, field and condition; Based on the database update statement, the database data in the structured database is updated, and the version number field of each database data is incrementally updated corresponding to the database version number, to determine the updated database version number of each database data; The cached data in the cache is updated according to the updated database data, and the version number field of each database data is incrementally updated corresponding to the cache version number to determine the cache version number after the update of each database data, wherein the initial values of the optimistic lock version number, cache version number and database version number are the same.
4. The data processing method based on distributed optimistic locking according to claim 1, characterized in that: The step of acquiring a distributed optimistic lock from the cache when a data write request is received also includes: Obtain a preset synchronization lock, wherein, when a thread obtains the preset synchronization lock, only the current thread is allowed to execute operations of obtaining a distributed optimistic lock and updating an optimistic lock version number of the distributed optimistic lock record; The step of updating the optimistic lock version number of the distributed optimistic lock record and releasing the distributed optimistic lock after the update is completed also includes: The preset synchronization lock is released.
5. The data processing method based on distributed optimistic locking according to claim 1, characterized in that: The data processing method based on distributed optimistic locking also includes: When a data read request is received, a distributed optimistic lock is obtained from the cache; Matching the request version number carried in the data read request with the updated optimistic lock version number of the distributed optimistic lock record; If the match is successful, obtaining the target data corresponding to each of the data read requests from the cache data in the cache; If the match fails, the match failure information is returned.
6. The data processing method based on distributed optimistic locking according to claim 5, characterized in that: After the step of acquiring the target data corresponding to each of the data read requests from the cached data in the cache, the method further includes: If the target data does not exist in the cache data of the cache, the target data corresponding to each of the data read requests is obtained from the structured database, and the cache data in the cache is updated according to each of the target data.
7. A data processing device based on distributed optimistic locking, characterized in that: The data processing device based on distributed optimistic locking includes: The lock acquisition module is used to acquire the distributed optimistic lock from the cache when receiving a data write request; A matching module, used to match the request version number carried in the data write request with the optimistic lock version number of the distributed optimistic lock record; An update module, configured to update the optimistic lock version number of the distributed optimistic lock record if the match is successful, and release the distributed optimistic lock after the update is completed; A writing module is used to obtain target writing data from the data writing request, and update the database data in the structured database and the cache data in the cache according to the target writing data.
8. A data processing device based on distributed optimistic locking, characterized in that: The device comprises: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the data processing method based on distributed optimistic locking as described in any one of claims 1 to 6.
9. A storage medium, characterized in that: The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, the steps of the data processing method based on distributed optimistic locking according to any one of claims 1 to 6 are implemented.
10. A computer program product, characterized in that The computer program product comprises a computer program, and when the computer program is executed by a processor, the steps of the data processing method based on distributed optimistic locking are implemented as claimed in any one of claims 1 to 6.
Citation Information
Patent Citations
Optimistic locking in a distributed file system replication environment
CN102236700A
Service data processing method and device and electronic equipment
CN113220669A
Database apparatus and data access method
JP2015230689A
System and method for an optimistic database access
US20060036574A1