Data updating method, device, electronic device and readable storage medium
The Bloom filter is used to determine the row lock status of multiple rows of data updated by concurrent threads in the database, which solves the inconsistency problem when concurrent threads update and ensures the correctness and consistency of database update results.
Patent Information
- Application Number
- CN202111487188.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-07
- Publication Date
- 2024-10-25
- Estimated Expiration
- 2041-12-07
AI Technical Summary
The existing technology cannot guarantee the consistency of the final result when multiple concurrent threads simultaneously update the same batch of multiple rows of data in the database, resulting in incorrect update results.
Use the Bloom filter to determine the row lock status of the data to be updated to ensure that the data is updated when it is not occupied by other threads, and wait or process update instructions in layers when necessary to ensure that the same batch of data is modified by the same concurrent thread.
It effectively solves the inconsistency problem when concurrent threads update the same batch of data, ensuring the correctness and consistency of the final update results.
Smart Images

Figure CN114138808B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of data storage, and in particular to a data updating method, device, electronic device and readable storage medium. Background Art
[0002] As the scale of data in the information age is growing explosively, there are more and more cases where multiple users simultaneously issue update operation requests to the same data row in the database. In order to avoid conflicts between users' write operations on the database, row locks are usually used to achieve the atomicity of updates.
[0003] In the prior art, when updating row-level data in a database, a thread needs to first obtain the row lock of the row, then modify and update the row data, and then release the row lock after the update is completed. Then, other threads can obtain the row data. Therefore, the update operations for the same row data are all serial operations, that is, if a row data is modified by other threads, it is necessary to wait until the other threads finish modifying the data before the update operation can be performed.
[0004] However, when the above method executes multiple concurrent threads to simultaneously update multiple rows of data in the same batch, it is possible that the final result of a row is modified by concurrent thread A, and another row is modified by concurrent thread B. The consistency of the final results of the multiple rows of data submitted for update in the same batch cannot be guaranteed, resulting in an incorrect update result finally submitted. Summary of the invention
[0005] The present application provides a data updating method, device, electronic device and readable storage medium, which can solve the problem of inconsistency when multiple concurrent threads modify the same batch of multiple rows of data in a database at the same time. When multiple concurrent threads update the same batch of multiple rows of data at the same time, it can ensure that the same batch of multiple rows of data submitted for update are modified and updated by the same concurrent thread, ensuring the consistency of the final result, making the final submitted update result correct, and improving effectiveness.
[0006] In a first aspect, the present application provides a data updating method, the method comprising:
[0007] Obtaining an update instruction, where the update instruction is used to indicate at least one row of data to be updated;
[0008] According to the update instruction, determining whether the row where the at least one row of data is located has a row lock;
[0009] If it is determined that the row where the at least one row of data is located does not have a row lock, the at least one row of data is updated, and a value corresponding to the row where the at least one row of data is located is adjusted.
[0010] Optionally, determining whether the row where the at least one row of data is located has a row lock includes:
[0011] A Bloom filter is used to determine whether the row where each row of data to be updated is located belongs to an element in a set; wherein the set includes rows with row locks.
[0012] Optionally, a Bloom filter is used to determine whether the row where each row of data to be updated belongs to an element in the set, including:
[0013] For each row of data to be updated, determine a plurality of flag bits corresponding to the row where the data is located;
[0014] Check whether the corresponding multiple flag bits in the Bloom filter include a first value;
[0015] If the first value is included, it is determined that the row where the data is located belongs to an element in the set.
[0016] Optionally, determining a plurality of flag bits corresponding to the row where the data is located includes:
[0017] Using K different hash functions to perform hash operations on the row where the data is located to obtain K hash values;
[0018] K addresses are obtained according to the K hash values, and the K addresses are used to indicate a mark bit corresponding to the row where the data is located in the Bloom filter.
[0019] Optionally, adjusting a value corresponding to a row where the at least one row of data resides includes:
[0020] Before updating any row of data, set a plurality of flag bits corresponding to the row where the data is located to a first value;
[0021] After any row of data is updated, a plurality of flag bits corresponding to the row where the data is located are set to a second value.
[0022] Optionally, before obtaining the update instruction, it also includes:
[0023] Determine whether the number of rows of at least one row of data to be updated is less than a preset row value;
[0024] If yes, encapsulate the instructions for at least one row of data to be updated to generate an update instruction;
[0025] If not, the at least one row of data to be updated is layered based on a preset row value to obtain N layers of data to be updated, and the instructions of the N layers of data to be updated are packaged in sequence to generate N update instructions, where N is a positive integer greater than 1.
[0026] Optionally, the method further includes:
[0027] If it is determined that the mark bit corresponding to the row where any row of data is located contains the first value, waiting is performed, and after the mark bits corresponding to the row where the at least one row of data is located do not contain the first value, the at least one row of data is updated.
[0028] Optionally, if it is determined that the mark bit corresponding to the row where any row of data is located contains the first value, waiting is performed, and after the mark bits corresponding to the row where the at least one row of data is located do not contain the first value, the at least one row of data is updated, including:
[0029] If it is determined through the Bloom filter that the mark bit corresponding to the row where any row of data is located contains the first value, it is determined whether the number of waiting times exceeds the preset number;
[0030] If the number of times does not exceed the preset number, the waiting time is calculated according to the waiting number, the waiting number is increased by 1, and after the waiting time has passed, the Bloom filter is used again to determine whether the mark bit corresponding to the row where any row of data is located contains the first value, so as to update the at least one row of data when the first value is not contained;
[0031] If the preset number of times is exceeded, a prompt message is sent to the user terminal to prompt the user that an error occurs when executing the update instruction.
[0032] In a second aspect, the present application provides a data updating device, the device comprising:
[0033] An acquisition module, used for acquiring an update instruction, wherein the update instruction is used for indicating at least one row of data to be updated;
[0034] A judging module, configured to judge whether a row where the at least one row of data is located has a row lock according to the update instruction;
[0035] The update module is used to update the at least one row of data and adjust the value corresponding to the row of the at least one row of data when it is determined that the row where the at least one row of data is located does not have a row lock.
[0036] In a third aspect, the present application provides an electronic device, comprising: a processor, and a memory communicatively connected to the processor;
[0037] The memory stores computer-executable instructions;
[0038] The processor executes the computer-executable instructions stored in the memory to implement the method as described in any one of the first aspects.
[0039] In a fourth aspect, the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are executed by a processor, they are used to implement the data updating method as described in any one of the first aspects.
[0040] In a fifth aspect, the present application provides a computer program, comprising a program code. When a computer runs the computer program, the program code executes the method as described in any one of the first aspects.
[0041] In summary, the present application provides a data updating method, device, electronic device and readable storage medium, which can obtain an update instruction, and the update instruction is used to indicate at least one row of data to be updated; further, according to the obtained update instruction, further determine whether the row where at least one row of data is located has a row lock; when it is determined that the row where at least one row of data is located does not have a row lock, update at least one row of data, and adjust the value corresponding to the row where at least one row of data is located. In this way, the above method can effectively solve the problem of inconsistency when multiple concurrent threads modify the same batch of multiple rows of data in the database at the same time. When multiple concurrent threads update the same batch of multiple rows of data at the same time, it can ensure that the same batch of multiple rows of data submitted for update are modified and updated by the same concurrent thread, ensuring the consistency of the final update result submitted by each concurrent thread, so that the final submitted update result is correct, and the effectiveness is improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] 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.
[0043] Figure 1 A schematic diagram of an application scenario provided for an embodiment of the present application;
[0044] Figure 2 A flowchart of a data updating method provided in an embodiment of the present application;
[0045] Figure 3 A flowchart of a specific data updating method provided in an embodiment of the present application;
[0046] Figure 4 A schematic diagram of the structure of a data updating device provided in an embodiment of the present application;
[0047] Figure 5 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application.
[0048] The above drawings have shown clear embodiments of the present application, which will be described in more detail later. These drawings and text descriptions are not intended to limit the scope of the present invention in any way, but to illustrate the concept of the present application to those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION
[0049] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the appended claims.
[0050] The term "multiple" in this application refers to two or more than two. The term "and / or" in this application is only a description of the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. In addition, the character " / " in this application generally indicates that the previous and next associated objects are in an "or" relationship; in the formula, the character " / " indicates that the previous and next associated objects are in a "division" relationship. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single items or plural items. For example, at least one of a, b, or c can represent: a, b, c, ab, ac, bc, or abc, where a, b, c can be single or multiple.
[0051] In order to facilitate the clear description of the technical solutions of the embodiments of the present application, in the embodiments of the present application, the words "first", "second" and the like are used to distinguish the same items or similar items with substantially the same functions and effects. For example, the first device and the second device are only used to distinguish different devices, and their order is not limited. Those skilled in the art can understand that the words "first", "second" and the like do not limit the quantity and execution order, and the words "first", "second" and the like do not necessarily limit them to be different.
[0052] It should be noted that, in this application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in this application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a specific way.
[0053] It should be understood that the various numerical numbers involved in the embodiments of the present application are only used for the convenience of description and are not used to limit the scope of the embodiments of the present application.
[0054] It should be understood that in the embodiments of the present application, the sequence numbers of the following processes do not imply the order of execution. The order of execution of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0055] The embodiments of the present application are introduced below with reference to the accompanying drawings. Figure 1 A schematic diagram of an application scenario provided in an embodiment of the present application. A data updating method provided in the present application can be applied to the following examples: Figure 1 In the application scenario shown, the application scenario includes a terminal device 101, a first server 102 and a second server 103; wherein each server has its own corresponding database, and the terminal device 101 establishes a communication connection with the first server 102. After the terminal device 101 performs a certain operation, it is necessary to change certain data in the database of the first server 102. Furthermore, the first server 102 obtains an update instruction and calls the corresponding thread according to the update instruction to modify and update certain data in the first server 102.
[0056] It is understandable that after the terminal device 101 performs a certain operation, certain data in the database of the second server 103 needs to be changed. Then, after the first server 102 obtains the update instruction, it sends the update instruction to the second server 103. Further, the second server 103 calls the corresponding thread to modify and update certain data in the second server 103.
[0057] It should be noted that the application scenario of the data update method proposed in this application may include more servers, and the update instruction may also be used to update certain data in the database of other servers, which is not specifically limited in this embodiment of the application. Among them, the type of database may be HBase database, MySQL database, etc., which is not specifically limited in this embodiment of the application.
[0058] The above-mentioned terminal equipment can be a wireless terminal or a wired terminal. The wireless terminal can be a device that provides voice and / or other business data connectivity to users, a handheld device with wireless connection function, or other processing equipment connected to a wireless modem. The wireless terminal can communicate with one or more core network devices via a radio access network (Radio Access Network, RAN). The wireless terminal can be a mobile terminal, such as a mobile phone (or "cellular" phone) and a computer with a mobile terminal. For example, it can be a portable, pocket-sized, handheld, computer-built-in or vehicle-mounted mobile device that exchanges language and / or data with the radio access network. For another example, the wireless terminal can also be a personal communication service (Personal Communication Service, PCS) phone, a cordless phone, a Session Initiation Protocol (Session Initiation Protocol, SIP) phone, a wireless local loop (Wireless Local Loop, WLL) station, a personal digital assistant (Personal Digital Assistant, PDA) and other devices. The wireless terminal may also be referred to as a system, a subscriber unit, a subscriber station, a mobile station, a mobile station, a remote station, a remote terminal, an access terminal, a user terminal, a user agent, a user device or user equipment, without limitation. Optionally, the terminal device may also be a smart watch, a tablet computer or other device.
[0059] In the prior art, when updating row-level data in a database, a thread needs to first obtain the row lock of the row, then modify and update the row data, and then release the row lock after the update is completed. Then, other threads can obtain the row data. Therefore, the update operations for the same row data are all serial operations, that is, if a row data is modified by other threads, it is necessary to wait until the other threads finish modifying the data before the update operation can be performed.
[0060] However, when the above method executes multiple concurrent threads to simultaneously update multiple rows of data in the same batch, it is possible that the final result of a row is modified by concurrent thread A, and another row is modified by concurrent thread B. The consistency of the final results of the multiple rows of data submitted for update in the same batch cannot be guaranteed, resulting in an incorrect update result finally submitted.
[0061] It should be noted that when multiple concurrent threads simultaneously update multiple rows of data in the same batch, the above method does not determine whether the data involved is being modified concurrently by other threads before execution. Each row of records will be judged during the execution process. If a row is modified by other concurrent threads, wait until the modification of other concurrent threads is completed before modifying the above data.
[0062] Therefore, the present application provides a data updating method. To ensure the consistency of data in the same batch database modified by multiple concurrent threads, before modifying the data, it is determined whether the row where the data is located is being modified, and a flag is determined whether the row where the data is located is being modified. Furthermore, it can be determined that the rows where the data to be updated are located do not have a flag that they are being modified. In this case, the data is updated and the flag corresponding to the row where the data is located is adjusted.
[0063] For example, Figure 2 A flow chart of a data updating method provided in an embodiment of the present application is shown as follows: Figure 2 As shown, the method of the embodiment of the present application includes:
[0064] S201. Obtain an update instruction, where the update instruction is used to indicate at least one row of data to be updated.
[0065] In this step, the data is updated in units of rows, that is, the data to be updated is composed of at least one row of data, and all columns in a row represent one piece of updated data.
[0066] For example, in Figure 1 In the application scenario, the first server 102 can obtain an update instruction, which can be to update certain data in the database of the first server 102 or to update certain data in the database of the second server 103. The above data are stored in units of one line.
[0067] S202: According to the update instruction, determine whether the row where the at least one row of data is located has a row lock.
[0068] In this step, whether the row where the at least one row of data is located has a row lock can be determined based on a Bloom filter, wherein the Bloom filter can refer to a very long binary vector and a series of random mapping functions. It can be used to retrieve whether an element is in a set. The storage space and insertion / query time of the Bloom filter are constants, and the Bloom filter does not need to store the element itself, so the space efficiency and query time of the Bloom filter are better.
[0069] Specifically, the Bloom filter algorithm is as follows: first, k hash functions are needed, each function can hash the key (element, which can refer to the row where the data is located in this application) into an integer; during initialization, an array of length n bits is required, and each bit is initialized to 0; when a key is added to a set, k hash functions are used to calculate k hash values, and the corresponding bit positions in the array are set to 1; when judging whether a key is in the set, k hash functions are used to calculate k hash values, and the corresponding bits in the array are queried. If all bits are 1, it is considered to be in the set; among them, the hash function refers to the transformation of an input of any length into an output of a fixed length through a hash algorithm. The output is the hash value, which can be understood as a function that maps a message of any length to an element storage location.
[0070] It should be noted that when determining whether a specified key already exists in a specified set, if the Bloom filter determines that the key does not exist, then the key is definitely not in the specified set; if the Bloom filter determines that the key exists, then the key may not be in the specified set.
[0071] In this step, the row lock may also be called a pessimistic lock, which may refer to locking the row where the data being accessed (ie, the data being modified and updated) is located. Before the row where the locked data is located is modified and updated, other threads cannot modify the locked data.
[0072] Exemplarily, the Bloom filter can record which data in at least one row of data to be updated in the Hbase cluster is being modified and updated, and which data is intended to be modified. Specifically, the Bloom filter records the bit position corresponding to the data with a row lock as 1, and the bit position corresponding to the data without a row lock as 0. Furthermore, the server can determine whether there is a row lock by judging whether the bit position corresponding to the row where at least one row of data is located is 1 through the Bloom filter based on the update instruction.
[0073] S203: If it is determined that the row where the at least one row of data is located does not have a row lock, update the at least one row of data and adjust the value corresponding to the row where the at least one row of data is located.
[0074] In this step, the value corresponding to the row is the corresponding mark added by the Bloom filter according to whether at least one row of data is occupied and modified by other threads, that is, if at least one row of data is occupied and modified by other threads, the bit position corresponding to the row is 1; if the data is not occupied and modified by other threads, the bit position corresponding to the row is 0; accordingly, if at least one row of data is occupied and modified by other threads, after the update is completed, the bit position "1" corresponding to the row needs to be adjusted to "0".
[0075] Exemplarily, before a thread updates at least one row of data to be updated, it can be determined through a Bloom filter that at least one row of data to be updated does not have a row lock, that is, all data to be updated are not occupied by other threads for modification and update. In this case, the thread can be used to modify at least one row of data and adjust the mark corresponding to the row where the at least one row of data is located.
[0076] It can be understood that if it is determined that the row where the at least one row of data is located has a row lock, the at least one row of data is not updated, but the update operation is waited for the data with the row lock to complete. After the row lock is released, it is determined again whether the row where the at least one row of data is located has a row lock. The update operation is not performed on the data to be updated until the row where the at least one row of data is located does not have a row lock.
[0077] Therefore, the data updating method provided in the embodiment of the present application can effectively solve the problem of inconsistency when multiple concurrent threads simultaneously modify multiple rows of data in the same batch in a database. When multiple concurrent threads simultaneously update multiple rows of data in the same batch, it can ensure that the multiple rows of data submitted for update in the same batch are modified and updated by the same concurrent thread, ensuring the consistency of the final update result submitted by each concurrent thread, so that the final submitted update result is correct, thereby improving effectiveness.
[0078] Optionally, determining whether the row where the at least one row of data is located has a row lock includes:
[0079] A Bloom filter is used to determine whether the row where each row of data to be updated is located belongs to an element in a set; wherein the set includes rows with row locks.
[0080] In this step, a Bloom filter may be used to retrieve whether an element is in a set, where an element refers to a row where each row of data to be updated is located.
[0081] Therefore, using a Bloom filter to query whether a row containing at least one row of data has a row lock can improve the query rate and save storage space.
[0082] Optionally, a Bloom filter is used to determine whether the row where each row of data to be updated belongs to an element in the set, including:
[0083] For each row of data to be updated, determine a plurality of flag bits corresponding to the row where the data is located;
[0084] Check whether the corresponding multiple flag bits in the Bloom filter include a first value;
[0085] If the first value is included, it is determined that the row where the data is located belongs to an element in the set.
[0086] In this step, multiple flag bits may refer to the flag positions set for each row where at least one row of data to be updated is located. The flag bits corresponding to different rows of data are different. The corresponding values may be added to the flag bits according to the execution status of at least one row of data (i.e., whether it is being modified by a thread). The flag bits may refer to the bits described in the above embodiment. For example, if a row of data is being modified by thread A, a first value may be added to the data of this row. The embodiment of the present application does not limit the specific value of the first value. For example, the first value may be 1 or 2.
[0087] Exemplarily, after receiving an update instruction, the server first determines in the Bloom filter whether the data row to be modified has been modified by a concurrent thread before updating the data based on the update instruction. Specifically, for each row of data to be updated, the Bloom filter is first used to determine the multiple mark bits corresponding to the row where the data is located, and checks whether the corresponding multiple mark bits in the Bloom filter contain the value "1". If they contain the value "1", it can be determined that the row where the data to be modified is located belongs to an element in the set, that is, the row has a row lock, indicating that the row of data is being modified by a concurrent thread.
[0088] Therefore, when judging whether the row where at least one row of data to be updated is located has a row lock through the Bloom filter, the judgment result can be obtained in a shorter query time, thereby improving the processing speed.
[0089] Optionally, determining a plurality of flag bits corresponding to the row where the data is located includes:
[0090] Using K different hash functions to perform hash operations on the row where the data is located to obtain K hash values;
[0091] K addresses are obtained according to the K hash values, and the K addresses are used to indicate a mark bit corresponding to the row where the data is located in the Bloom filter.
[0092] In an embodiment of the present application, a hash function may refer to a function that changes an input string of any length into an output string of a fixed length, and may be used to calculate the hash value of stored data. Different hash functions may be designed according to the type of stored data. Specifically, hash value = calculated storage value / size of the hash table, wherein the structure constructed using the conversion function is called a hash table, which may also be called a hash table. The conversion function is a compression mapping, that is, a function that compresses a message of any length into a message digest of a fixed length, and the hash table uses the idea of function mapping to associate the storage location of a record with the keyword of the record, so as to quickly locate the record to be found.
[0093] It can be understood that there is a corresponding mapping relationship between the hash value and the data storage address. By finding the hash value, the address corresponding to a certain data can be found.
[0094] For example, when querying multiple mark bits corresponding to the row where the data to be updated is located in the Bloom filter, the row where the data to be updated is located can be input into K different hash functions in sequence for hash operations to obtain K hash values, and the mark bit corresponding to each row can be found based on the K hash values.
[0095] Therefore, the Bloom filter can be used to determine the mark bit corresponding to each row of data to be updated. The Bloom filter stores the bit array used by the data, which can not only save space but also improve the processing rate.
[0096] Optionally, adjusting a value corresponding to a row where the at least one row of data resides includes:
[0097] Before updating any row of data, set a plurality of flag bits corresponding to the row where the data is located to a first value;
[0098] After any row of data is updated, a plurality of flag bits corresponding to the row where the data is located are set to a second value.
[0099] In this step, the second value is a value added to the flag bit based on the data to be updated being in the proposed update state (i.e., not modified by the thread). For example, if a row of data is not modified by the thread, the second value can be added to the data of this row. The embodiment of the present application does not limit the specific value of the second value. For example, the second value can be 0, but the second value needs to be distinguished from the first value, and the values of the two must be different.
[0100] Exemplarily, before the server performs an update operation on a batch of data to be updated, that is, before updating any row of data, the Bloom filter can set multiple mark bits corresponding to the row where the data to be updated is located to "1", indicating that this batch of data is being modified by the thread, and other threads cannot modify this batch of data. Furthermore, after the server completes the update operation on the data to be updated, it can set multiple mark bits corresponding to the row where the batch of data is located to "0", that is, restore the original mark in the Bloom filter (that is, the second value).
[0101] It should be noted that, when the Bloom filter is initialized, each mark is initialized to 0, that is, the original mark corresponds to the second value.
[0102] Therefore, the data updating method proposed in the present application can restore the original mark after the data is modified and updated for use by other threads, thereby improving flexibility.
[0103] Optionally, before obtaining the update instruction, it also includes:
[0104] Determine whether the number of rows of at least one row of data to be updated is less than a preset row value;
[0105] If yes, encapsulate the instructions for at least one row of data to be updated to generate an update instruction;
[0106] If not, the at least one row of data to be updated is layered based on a preset row value to obtain N layers of data to be updated, and the instructions of the N layers of data to be updated are packaged in sequence to generate N update instructions, where N is a positive integer greater than 1.
[0107] In an embodiment of the present application, the preset row value may refer to the maximum number of rows of data to be updated each time the data is packaged. For example, the preset row value is 10 rows. If the data to be updated is 20 rows, and the 20 rows are greater than 10 rows, the data to be updated can be divided into two layers for packaging, and 2 update instructions are generated and executed in sequence.
[0108] Encapsulation can refer to encapsulating the resources required for the operation of an object in a program object, hiding the properties and implementation details of the object, and only exposing the application programming interface (API) to control the access level of reading and modifying the properties in the program, combining the abstracted data and behavior (or function) to form an organic whole. For example, encapsulate the put or putList API, and then expose the encapsulated interface, where put indicates that the data to be updated is a command for a row of data, and putList indicates that the data to be updated is a command for multiple rows of data.
[0109] Exemplarily, before the server obtains the update instruction, it is necessary to determine whether the number of rows of at least one row of data to be updated is less than the preset row value of 10 rows. If it is less than, the instructions of at least one row of data to be updated are packaged to generate the required update instructions; if it is greater, such as if there are 20 rows of data to be updated, the data to be updated are layered according to 10 rows to obtain 2 layers of data to be updated, and further, the instructions of the 2 layers of data to be updated are packaged in sequence to generate 2 update instructions.
[0110] Specifically, the packaging process is as follows: if the scenario is to encapsulate the putList API, batch insertion is performed, the number of rows of putList is obtained, and the number of rows is compared with the preset row value. If the number of rows is less than the preset row value, the putList API is encapsulated. If the number of rows is greater than the preset row value, putList is layered based on the preset row value, and each time the number of rows is full, the instructions of a layer of data to be updated are encapsulated until all the instructions corresponding to the updated data are encapsulated; if the scenario is to encapsulate the put API, a single insertion is performed, and the put API is directly encapsulated.
[0111] Optionally, the method further includes:
[0112] If it is determined that the mark bit corresponding to the row where any row of data is located contains the first value, waiting is performed, and after the mark bits corresponding to the row where the at least one row of data is located do not contain the first value, the at least one row of data is updated.
[0113] In this step, the Bloom filter can be used to determine whether the mark bit corresponding to the row where any row of data is located contains the first value. If it contains the first value, it is necessary to wait for any row of data to complete the last update operation of other threads, and then determine again whether the mark bit corresponding to the row where any row of data is located contains the first value. Only after it is determined that the mark bits corresponding to all rows of the data to be updated do not contain the first value, the update operation can be performed on the data to be updated.
[0114] It can be understood that when the Bloom filter is used to determine whether the mark bit corresponding to the row where any row of data is located contains a first value, the row where any row of data is located does not necessarily have a row lock, but only the Bloom filter is used to determine whether the mark bit corresponding to the row where any row of data is located contains the first value, and then the data update method provided in the present application is further executed.
[0115] Therefore, the data updating method provided by the present application can wait until at least one row of data to be updated has not been updated by any thread, and then perform an update operation on at least one row of data, thereby improving the success rate of the update operation.
[0116] Optionally, if it is determined that the mark bit corresponding to the row where any row of data is located contains the first value, waiting is performed, and after the mark bits corresponding to the row where the at least one row of data is located do not contain the first value, the at least one row of data is updated, including:
[0117] If it is determined through the Bloom filter that the mark bit corresponding to the row where any row of data is located contains the first value, it is determined whether the number of waiting times exceeds the preset number;
[0118] If the number of times does not exceed the preset number, the waiting time is calculated according to the waiting number, the waiting number is increased by 1, and after the waiting time has passed, the Bloom filter is used again to determine whether the mark bit corresponding to the row where any row of data is located contains the first value, so as to update the at least one row of data when the first value is not contained;
[0119] If the preset number of times is exceeded, a prompt message is sent to the user terminal to prompt the user that an error occurs when executing the update instruction.
[0120] It should be noted that the present application can use a Bloom filter to determine whether the mark bit corresponding to the row where any row of data is located contains the first value, or it can determine whether the mark bit corresponding to the row where any row of data is located contains the first value by setting a predetermined strategy or using other algorithms, and there is no specific limitation on this.
[0121] In an embodiment of the present application, the preset number of times may refer to the upper limit of the number of waiting times for executing the update operation after waiting for more than a preset time. For example, the preset time is 100 milliseconds. After the first waiting of 100 milliseconds, the data update method is executed once. If the execution is not successful, a second waiting is performed, and so on. The waiting time is the time interval for executing the data update method again after executing it once. The waiting time and the number of waiting times may be positively correlated. That is, if the first waiting is not successful, the waiting time for the second execution will be longer accordingly.
[0122] Preferably, the preset number of times is 10. The preset number of times may be preset in advance or may be manually modified, and this embodiment of the present application does not specifically limit this.
[0123] In this step, the prompt information can be used to prompt that a certain thread has an error when executing an update instruction. The method of sending the prompt information is not specifically limited in the embodiment of the present application. It can be a message prompt box displayed on the display screen carried by the server, and the message prompt box displays "Thread 1 has an error when executing the update instruction" to prompt the user to check in time. The server can also send a message prompt to the user terminal device, and the message prompt is "Thread 1 has an error when executing the update instruction" to prompt the user to check in time.
[0124] Exemplarily, when updating the data to be updated, the Bloom filter can be used to determine whether the mark bit corresponding to the row where any row of data is located contains the first value "1". If it contains "1", it is further necessary to determine whether the number of times the update operation is waited for exceeds 10 times; if it does not exceed 10 times, the waiting time is calculated according to the number of waiting times this time, and further, the number of waiting times is increased by 1, and after the waiting time has passed, the Bloom filter is used again to determine whether the mark bit corresponding to the row where any row of data is located contains "1". When it is determined that the mark bit corresponding to the row where the data to be updated is located does not contain "1", the update operation is performed on the data to be updated to update the data; if it exceeds 10 times, a message prompt is sent to the user terminal device, and the message prompt is "Thread 1 encountered an error when executing the update instruction" to prompt the user to check in time.
[0125] Specifically, the process of calculating the waiting time is as follows: set the index value corresponding to each waiting, such as index value A(i)={1,2,4,8,16,32,64,128,256}, and further calculate the waiting time according to the formula T(i)=100*A(i)*B*0.01.
[0126] Among them, B is the number of waiting times, 100 is the preset time, A is the index value corresponding to the set i-th waiting time, which is positively correlated with the number of waiting times, T is the waiting time, and 0.01 is the set probability value.
[0127] It should be noted that the setting of A can be as described above, so that the waiting time and the number of waiting times are positively correlated, or it can be set to other corresponding relationships. The embodiment of the present application does not make specific limitations on this, and any value can be set. The above algorithm is only an example, so the waiting time and the number of waiting times can also be other corresponding relationships.
[0128] Therefore, if the data row to be updated is in the updated but uncommitted state, it is necessary to wait for a certain period of time and then retry the process described in the data update method until the upper limit of the waiting times is reached, then an error will be reported and the update will be exited, thereby improving the processing speed and success rate.
[0129] In combination with the above embodiments, Figure 3 A flow chart of a specific data updating method provided in an embodiment of the present application is as follows: Figure 3 As shown, the specific execution method steps of the embodiment of the present application include:
[0130] Step S1: Encapsulate put or putList to generate an update instruction. Put is an encapsulation of an API for updating a row of data, and putList is an encapsulation of an API for updating multiple rows of data. Further, the server obtains the update instruction and executes step S2.
[0131] Step S2: Determine whether the row where the at least one row of data to be updated is located is the last row. If so, execute step S3; if not, execute step S4.
[0132] Step S3: Mark all data lines to be modified as being modified through the Bloom filter, and perform corresponding update operations. After the update is completed, restore the mark in the Bloom filter.
[0133] Step S4: Go to the Bloom filter to determine whether the data row to be modified is already being modified by a concurrent thread. If so, wait for N milliseconds and determine again whether the data row to be modified is already being modified by a concurrent thread. Until the data row to be modified is no longer being modified by a concurrent thread, execute step S2. If not, execute step S2.
[0134] In the foregoing embodiments, the data updating method provided by the embodiments of the present application is introduced, and in order to implement the various functions in the method provided by the embodiments of the present application, the electronic device as the execution subject may include a hardware structure and / or a software module, and implement the above functions in the form of a hardware structure, a software module, or a hardware structure plus a software module. Whether one of the above functions is executed in the form of a hardware structure, a software module, or a hardware structure plus a software module depends on the specific application and design constraints of the technical solution.
[0135] For example, Figure 4 A schematic diagram of a data updating device provided in an embodiment of the present application is shown in FIG. Figure 4 As shown, the device includes: an acquisition module 410, a judgment module 420 and an update module 430, wherein the acquisition module 410 is used to acquire an update instruction, and the update instruction is used to indicate at least one row of data to be updated;
[0136] A determination module 420, configured to determine whether a row where the at least one row of data is located has a row lock according to the update instruction;
[0137] The updating module 430 is configured to update the at least one row of data and adjust a value corresponding to the row of the at least one row of data when it is determined that the row where the at least one row of data is located does not have a row lock.
[0138] Optionally, the judgment module 420 is specifically configured to judge, through a Bloom filter, whether the row where each row of data to be updated is located belongs to an element in a set; wherein the set includes rows with row locks.
[0139] Optionally, the judgment module 420 includes a determination unit, a viewing unit and a judgment unit;
[0140] Specifically, the determining unit is used to determine, for each row of data to be updated, a plurality of flag bits corresponding to the row where the data is located;
[0141] The checking unit is used to check whether the corresponding multiple flag bits in the Bloom filter contain a first value;
[0142] The judging unit is used to judge whether the row where the data is located belongs to an element in a set when the first value is included.
[0143] Optionally, the determining unit is specifically configured to:
[0144] Using K different hash functions to perform hash operations on the row where the data is located to obtain K hash values;
[0145] K addresses are obtained according to the K hash values, and the K addresses are used to indicate a mark bit corresponding to the row where the data is located in the Bloom filter.
[0146] Optionally, the update module 430 is specifically configured to:
[0147] Before updating any row of data, set a plurality of flag bits corresponding to the row where the data is located to a first value;
[0148] After any row of data is updated, a plurality of flag bits corresponding to the row where the data is located are set to a second value.
[0149] Optionally, the device further includes a packaging module, wherein the packaging module is configured to: before obtaining the update instruction:
[0150] Determine whether the number of rows of at least one row of data to be updated is less than a preset row value;
[0151] If yes, encapsulate the instructions for at least one row of data to be updated to generate an update instruction;
[0152] If not, the at least one row of data to be updated is layered based on a preset row value to obtain N layers of data to be updated, and the instructions of the N layers of data to be updated are packaged in sequence to generate N update instructions, where N is a positive integer greater than 1.
[0153] Optionally, the device further includes a waiting module, wherein the waiting module is configured to:
[0154] When it is determined that the mark bit corresponding to the row where any row of data is located contains the first value, waiting is performed, and after the mark bits corresponding to the row where the at least one row of data is located do not contain the first value, the at least one row of data is updated.
[0155] Optionally, the waiting module is specifically used for:
[0156] If it is determined through the Bloom filter that the mark bit corresponding to the row where any row of data is located contains the first value, it is determined whether the number of waiting times exceeds the preset number;
[0157] If the number of times does not exceed the preset number, the waiting time is calculated according to the waiting number, the waiting number is increased by 1, and after the waiting time has passed, the Bloom filter is used again to determine whether the mark bit corresponding to the row where any row of data is located contains the first value, so as to update the at least one row of data when the first value is not contained;
[0158] If the preset number of times is exceeded, a prompt message is sent to the user terminal to prompt the user that an error occurs when executing the update instruction.
[0159] The specific implementation principles and effects of the data updating device provided in the embodiments of the present application can be found in the relevant descriptions and effects corresponding to the above embodiments, and will not be elaborated here.
[0160] Exemplarily, an embodiment of the present application further provides a structural diagram of an electronic device, Figure 5 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application is shown in FIG. Figure 5 As shown, the electronic device may include: a processor 502 and a memory 501 communicatively connected to the processor; the memory 501 stores a computer program; the processor 502 executes the computer program stored in the memory 501, so that the processor 502 executes the method described in any of the above embodiments.
[0161] The memory 501 and the processor 502 may be connected via a bus 503 .
[0162] An embodiment of the present application also provides a computer-readable storage medium, which stores computer program execution instructions. When the computer program execution instructions are executed by a processor, they are used to implement the data updating method in any of the aforementioned embodiments of the present application.
[0163] An embodiment of the present application also provides a chip for executing instructions, which is used to execute the data updating method executed by the electronic device in any of the aforementioned embodiments of the present application.
[0164] An embodiment of the present application further provides a computer program, including a program code. When a computer runs the computer program, the program code executes the data updating method executed by the electronic device in any of the aforementioned embodiments of the present application.
[0165] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of modules is only a logical function division. There may be other division methods in actual implementation, such as multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or modules, which can be electrical, mechanical or other forms.
[0166] The modules described as separate components may or may not be physically separated, and the components shown as modules may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to implement the solution of this embodiment.
[0167] In addition, each functional module in each embodiment of the present application can be integrated into one processing unit, or each module can exist physically separately, or two or more modules can be integrated into one unit. The above-mentioned module-composed unit can be implemented in the form of hardware or in the form of hardware plus software functional units.
[0168] The above-mentioned integrated module implemented in the form of a software function module can be stored in a computer-readable storage medium. The above-mentioned software function module is stored in a storage medium, including a number of instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to perform some steps of the method described in each embodiment of the present application.
[0169] It should be understood that the processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application specific integrated circuits (ASIC), etc. A general-purpose processor may be a microprocessor or the processor may be any conventional processor, etc. The steps of the method disclosed in the application may be directly implemented as being executed by a hardware processor, or may be implemented by a combination of hardware and software modules in the processor.
[0170] The memory may include high-speed random access memory (RAM), and may also include non-volatile memory (NVM), such as at least one disk memory, and may also be a USB flash drive, a mobile hard disk, a read-only memory, a disk or an optical disk, etc.
[0171] The bus can be an industry standard architecture (ISA) bus, a peripheral component interconnect (PCI) bus, or an extended industry standard architecture (EISA) bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, the bus in the drawings of this application is not limited to only one bus or one type of bus.
[0172] The above storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk. The storage medium can be any available medium that can be accessed by a general or special purpose computer.
[0173] An exemplary storage medium is coupled to a processor so that the processor can read information from the storage medium and write information to the storage medium. Of course, the storage medium can also be a component of the processor. The processor and the storage medium can be located in an application specific integrated circuit (ASIC). Of course, the processor and the storage medium can also exist as discrete components in an electronic device or a main control device.
[0174] The above is only a specific implementation of the embodiment of the present application, but the protection scope of the embodiment of the present application is not limited thereto, and any changes or replacements within the technical scope disclosed in the embodiment of the present application should be included in the protection scope of the embodiment of the present application. Therefore, the protection scope of the embodiment of the present application should be based on the protection scope of the claims.
Claims
1. A data updating method, characterized in that: include: Obtaining an update instruction, where the update instruction is used to indicate at least one row of data to be updated; According to the update instruction, determining whether the row where the at least one row of data is located has a row lock; If it is determined that the row where the at least one row of data is located does not have a row lock, updating the at least one row of data and adjusting the value corresponding to the row where the at least one row of data is located; Adjusting the value corresponding to the row where the at least one row of data resides includes: Before updating any row of data, set a plurality of flag bits corresponding to the row where the data is located to a first value; After updating any row of data, multiple flag bits corresponding to the row where the data is located are set to a second value; wherein the second value is a value corresponding to the flag bit added based on the data to be updated being in the proposed update state; If it is determined that the row where the at least one row of data is located has a row lock, wait for the data with the row lock to complete the update operation. After the row lock is released, it is determined again whether the row where the at least one row of data is located has a row lock. The update operation is not performed on the at least one row of data until the row where the at least one row of data is located does not have a row lock.
2. The method according to claim 1, characterized in that Determining whether the row where the at least one row of data is located has a row lock includes: A Bloom filter is used to determine whether the row where each row of data to be updated is located belongs to an element in a set; wherein the set includes rows with row locks.
3. The method according to claim 2, characterized in that The Bloom filter is used to determine whether each row of data to be updated belongs to an element in the set, including: For each row of data to be updated, determine a plurality of flag bits corresponding to the row where the data is located; Check whether the corresponding multiple flag bits in the Bloom filter include a first value; If the first value is included, it is determined that the row where the data is located belongs to an element in the set.
4. The method according to claim 3, characterized in that Determining a plurality of flag bits corresponding to the row where the data is located includes: Using K different hash functions to perform hash operations on the row where the data is located to obtain K hash values; K addresses are obtained according to the K hash values, and the K addresses are used to indicate a mark bit corresponding to the row where the data is located in the Bloom filter.
5. The method according to claim 1, characterized in that Before getting the update instructions, also include: Determine whether the number of rows of at least one row of data to be updated is less than a preset row value; If yes, encapsulate the instructions for at least one row of data to be updated to generate an update instruction; If not, the at least one row of data to be updated is layered based on a preset row value to obtain N layers of data to be updated, and the instructions of the N layers of data to be updated are packaged in sequence to generate N update instructions, where N is a positive integer greater than 1.
6. The method according to any one of claims 1 to 5, characterized in that: Also includes: If it is determined that the mark bit corresponding to the row where any row of data is located contains the first value, waiting is performed, and after the mark bits corresponding to the row where the at least one row of data is located do not contain the first value, the at least one row of data is updated.
7. The method according to claim 6, characterized in that If it is determined that the mark bit corresponding to the row where any row of data is located contains the first value, waiting is performed, and after the mark bits corresponding to the row where the at least one row of data is located do not contain the first value, the at least one row of data is updated, including: If it is determined through the Bloom filter that the mark bit corresponding to the row where any row of data is located contains the first value, it is determined whether the number of waiting times exceeds the preset number; If the number of times does not exceed the preset number, the waiting time is calculated according to the waiting number, the waiting number is increased by 1, and after the waiting time has passed, the Bloom filter is used again to determine whether the mark bit corresponding to the row where any row of data is located contains the first value, so as to update the at least one row of data when the first value is not contained; If the preset number of times is exceeded, a prompt message is sent to the user terminal to prompt the user that an error occurs when executing the update instruction.
8. A data updating device, characterized in that: The device comprises: An acquisition module, used for acquiring an update instruction, wherein the update instruction is used for indicating at least one row of data to be updated; A judging module, configured to judge, according to the update instruction, whether the row where the at least one row of data is located has a row lock; An update module, configured to update the at least one row of data and adjust a value corresponding to the row of the at least one row of data when it is determined that the row of the at least one row of data does not have a row lock; The update module is specifically used for: Before updating any row of data, set a plurality of flag bits corresponding to the row where the data is located to a first value; After updating any row of data, multiple flag bits corresponding to the row where the data is located are set to a second value; wherein the second value is a value corresponding to the flag bit added based on the data to be updated being in the proposed update state; The update module is also used to wait for the data with the row lock to complete the update operation when it is determined that the row where the at least one row of data is located has a row lock, and after releasing the row lock, again determine whether the row where the at least one row of data is located has a row lock, and only perform the update operation on the at least one row of data after the row where the at least one row of data is located does not have a row lock.
9. An electronic device, characterized in that: include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executable instructions; The processor executes the computer-executable instructions stored in the memory to implement the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are executed by a processor, they are used to implement the data updating method according to any one of claims 1 to 7.
11. A computer program product, characterized in that The computer program comprises program codes, and when a computer runs the computer program, the program codes execute the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Task processing method and processing device
CN108132831A
Database modification method, device and equipment and storage medium
CN109933606A
Method and device for backing up SQLite database
CN111159156A
Task processing method, device and equipment and computer readable storage medium
CN111400330A
Data updating method, device and equipment, storage medium and program product
CN112487009A
