Background
In the information-oriented society of today, people generally use database technology to store a large amount of data, and when some data needs to be used, the corresponding data is accessed through a database management system.
In the prior database technology, one of the characteristics is that the data sharing performance is high, and because the data stored and managed in the database is oriented to the overall user, the data can be shared and used by a plurality of users or application programs. However, this brings a problem that data consistency is difficult to guarantee, so in the prior art, data currently accessed by a thread is usually locked in a database by using a database lock (i.e., an exclusive lock), so that when other threads access the data, the data cannot be accessed because the data is already locked, and the consistency of the data in the database is guaranteed.
However, for data frequently accessed by a large number of threads (i.e., hot spot data), since the database locking method is adopted, a large number of threads cannot access the locked data, so that a large number of threads are backlogged (i.e., threads that cannot access the data are queued to wait for accessing the data according to the chronological order of accessing the data), and the backlogged threads cannot perform subsequent operations.
For example, assuming that it takes 20s for a thread to access data a and perform the corresponding operation, the number of operations for the data a is at most 3 times in 1 minute, that is, the upper limit of the operation frequency for the data a is 3 times/minute, and the thread backlog is caused as long as the frequency of accessing the data a by the thread exceeds the frequency.
Even if the non-waiting database lock is adopted for the hot spot data, the thread can only be ensured to continue to execute other operations after reading the non-waiting database lock of the data, the thread cannot access the data and execute corresponding operations without waiting for accessing the data, so the thread does not finish accessing and operating the data substantially, and the thread needs to continuously try to access and operate the data subsequently.
Therefore, in the prior art, people configure the hot spot data in a database management system as a cache record mode for the hot spot data, then record the operation information of each thread on the hot spot data in a cache record mode, and periodically write the hot spot record according to each recorded operation information.
Specifically, a worker determines each piece of data frequently accessed by a large number of threads as hot spot data according to manual experience, and configures a cache identifier for each piece of hot spot data in a database management system, so that when the threads access the data, the hot spot data can be determined as a cache recording mode according to the cache identifier (that is, each piece of hot spot data is configured as a cache recording mode). When the thread accesses the hot data and determines that the hot data is in a cache record mode, the thread can record operation information to be executed in a cache record table of the database according to the cache record mode, then subsequent operations can be continuously executed, the database management system drags the operation record in the cache record table according to a fixed period, carries out uniform calculation on the operation information corresponding to the hot data aiming at each hot data in the cache record table, determines a calculation result, and finally modifies the hot data according to the calculation result through the thread. The process of recording each operation information in the cache record table is also referred to as a recording process, and the process of calculating the operation information in the cache record table and modifying the corresponding hot spot data according to the calculation result is also referred to as a complementary writing process.
For example, assuming that the database management system configures data a into a cache record mode, and assuming that 90 threads initiate an access request to the data a within 1 minute, the database management system needs to record the operations that the 90 threads need to perform on the data a in a cache record table of the database. Then, when the database management system fetches the operation records in the cache record table according to a fixed time period, for the data a, the database management system may determine 90 operation records, perform calculation on the 90 operation records, obtain a calculation result, and finally access the data a through a thread and execute a corresponding operation according to the calculation result.
Therefore, multiple operations on the hot spot data can be unified into one operation in a cache recording mode, the problem of thread backlog caused by frequent access of the same hot spot data by a thread is reduced, and meanwhile, the problem of frequent access failure of the hot spot data by the thread is solved as the operation information is recorded in the cache recording table and is equivalent to the operation information which is already operated.
However, the method of using the cache record needs to determine the hot spot data manually, and the worker needs to manually configure the hot spot data in the database management system in advance as the cache record method, so that the method of using the cache record is complex to operate and low in efficiency. Moreover, some hot data may be frequently accessed only in a period of time, and are idle in other periods of time, but the database management system still adopts a cache record mode for the operation request for accessing the hot data as long as the access mode of the hot data is not changed. Furthermore, in the cache record mode in the prior art, actually, operations of the database management system on data processing are added, for each operation requiring cache record, when the operation is recorded in the cache record table, a recording process is added, and when the operation record in the cache record table is fished, a reading process is added, so that the method using the cache record is no longer suitable for the hot data with no busy access request, and therefore, the access mode of the hot data also needs to be manually restored, so that the maintenance work of the database is increased, and the operation efficiency of the database is low.
Therefore, in the prior art, for hot data, both a traditional database locking mode and a cache recording mode have the problems of low database operation efficiency, need of manually adjusting a data access mode and inflexibility in use.
Detailed Description
In the embodiment of the application, when an operation request for data in a database is received, a lock corresponding to the data is acquired according to the operation instruction, whether the lock is acquired is judged, if yes, the data is locked, the data is operated according to operation information carried by the operation request, and if not, the data is operated in a cache recording mode. According to the method, whether the data needs to be operated in the cache recording mode or not is determined according to the result of obtaining the lock of each data, so that the advantage that the data needs to be operated in the cache recording mode according to the hotspot data is guaranteed, and the problems that in the prior art, the operation efficiency of a database is low and the maintenance cost is high due to the fact that the data which is operated in the cache recording mode needs to be manually configured in advance and the data access mode needs to be manually restored are solved.
In order to make the objects, technical solutions and advantages of the present application more apparent, the technical solutions of the present application will be described in detail and completely with reference to the following specific embodiments of the present application and the accompanying drawings. It should be apparent that the described embodiments are only some of the embodiments of the present application, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
Fig. 1 is a data recording process provided in an embodiment of the present application, which specifically includes the following steps:
s101: an operation request for data in a database is received.
Since the data recording process may be a process of performing corresponding operation on data according to a received operation request in a database, in this embodiment of the present application, an operation request for the data in the database may be received by a database management system independent from the database, or an operation request for the data in the database may be directly received by the database, and the following description will be given by taking only an example that the database management system receives an operation request for the data in the database. The operation request carries operation information on the data (e.g., operations such as adding, deleting, and changing the data).
In addition, the operation request may be an operation request initiated by a thread according to a currently executed database transaction, that is, the operation request belongs to one database transaction, and since the operation request is for data in a database, the operation request carries an identifier of the data, so that the database operating system may determine data corresponding to the operation request according to the identifier of the data, so that the thread initiating the operation request may access the corresponding data.
The database may be a single device, or may be a system including a plurality of devices.
Further, since a cache record mode needs to be adopted in the method of the present application, a corresponding cache record table should exist in the database. The cache record table may be pre-established, or may be established according to the use requirement when the cache record table needs to be used in the subsequent operation.
For example, suppose that in an account database for recording account funds, the database management system of the database receives a data operation request a of a thread α for an account a, wherein the operation information of the operation request is that 10 elements are transferred to the account a, and the identifier of the data is: account a.
S102: and acquiring a lock corresponding to the data according to the operation request.
In this embodiment, after the database management system receives the operation request, in order to ensure the consistency of the data in the database, the database management system may perform the same lock acquisition operation as in the prior art on the thread.
In addition, in the present application, since data frequently accessed to a thread needs to be operated in a cache record manner, in order to avoid backlogging of the thread when the thread needs to operate the data in the cache record manner, the acquired database lock may be a non-waiting database lock.
Specifically, consistent with the prior art, the database management system determines, according to the received operation request, an identifier of data carried in the operation request, determines, according to the identifier of the data, data that needs to be operated, and performs, through the thread, an operation of obtaining a non-waiting database lock corresponding to the data on the data.
Continuing with the above example, if the database management system determines that the identifier of the data for which the operation request a is directed is account a according to the operation request a, the database management system performs an operation of acquiring a non-waiting database lock of the account a on the account a through a thread α.
S103: and judging whether the lock is acquired, if so, executing step S104, and otherwise, executing step S105.
In the embodiment of the present application, for each data frequently accessed by each thread, that is, for hotspot data, because there are many threads accessing each hotspot data, there is a high probability that the hotspot data is in a locked state by other threads, and further, when one thread accesses one hotspot data, the failure probability of the lock executed by the thread in step S102 to acquire the hotspot data is also high. That is, after step S102 is executed, if it is determined that acquiring the non-waiting database lock corresponding to the data fails, the probability that the data is hot data is high.
As can be seen, for each hot data, when the thread executes the operation of acquiring the non-waiting database lock corresponding to each hot data, the failure probability of acquiring the non-waiting database lock is very high (that is, the non-waiting database lock is not acquired), and thus in the present application, it may be determined whether the data is the hot data according to the result of acquiring the non-waiting database lock corresponding to the data by the thread. However, when the thread acquires the non-waiting database lock, due to reasons such as network timeout and data error, the thread may also fail to acquire the non-waiting database lock corresponding to the data, so when determining whether the data is hot data, it is also necessary to determine that the reason why the thread fails to acquire the non-waiting database lock corresponding to the data is that the data is already locked, and it is determined that the data has a high probability of being hot data, and then the data may be operated in a cache record manner.
In addition, when each thread locks each data in the application, each thread uses a non-waiting database lock, so that after each thread fails to acquire the non-waiting database lock, the hot data can be continuously operated in a cache record mode subsequently, and the situation that subsequent operation cannot be performed due to thread backlog is avoided.
By the method, the database management system can automatically identify the hot spot data without pre-configuring the hot spot data.
Specifically, the database management system determines whether the thread acquires the non-waiting database lock corresponding to the data according to the result of the thread executing the non-waiting database lock corresponding to the data in step S102, if the thread acquires the non-waiting database lock corresponding to the data, step S104 is executed, and if the thread does not acquire the non-waiting database lock corresponding to the data and the reason why the thread does not acquire the non-waiting database lock corresponding to the data is that the data is already locked by other threads, step S105 is executed.
In addition, if the thread does not acquire the non-waiting database lock corresponding to the data and the reason is not that the data is locked by other threads, the database management system can display prompt information of operation failure and display the failure reason, as in the prior art.
Continuing with the above example, assuming that the thread α does not acquire the non-waiting database lock of the account a, the database management system determines, according to the result of the thread α acquiring the non-waiting database lock of the account a, that the thread α has failed to acquire the non-waiting database lock, and the reason for the failure is that the account a has been locked by other threads, then step S105 is executed.
S104: and locking the data and operating the data according to the operation information.
In the embodiment of the present application, as in the prior art, when a thread successfully acquires a non-waiting database lock of the data, the non-waiting database lock may be used to lock the data, and then the data may be operated according to the operation information carried in the operation request.
S105: and operating the data in a cache recording mode.
In this embodiment, as for the reason described in step S103, only when it is determined that the reason that the thread fails to acquire the non-waiting database lock corresponding to the data is that the data is already locked, it can be determined that the data is hot data, that is, only if the reason is satisfied, the data needs to be operated in a cache record manner. That is, when it is determined that the reason that the thread does not acquire the non-waiting database lock corresponding to the data is that the data is already locked, it is determined that the data has a high probability of being hot data, and the data needs to be operated in a cache record manner.
Specifically, in the recording process, the database management system records the operation information in the cache record table through the thread. However, at this time, since the operation information is merely recorded and the data has not been operated according to the operation information, it is also necessary to perform a post-write process.
In the write-back process, firstly, the database management system can salvage the specified amount of operation information in the cache record table according to a preset time period. And secondly, determining each data corresponding to each operation information according to each operation information obtained by fishing. Then, for each determined data, according to the retrieved operation information corresponding to the data, determining the time sequence of the operation information corresponding to the data, and calculating the complementary writing operation information corresponding to the data according to the time sequence of the operation information. And finally, executing the complementary writing process on the data corresponding to each operation information obtained by calculation according to the complementary writing operation information.
In addition, for each determined data that needs to execute the write-once process, the data management system needs to start a thread to execute the process, and the thread may first acquire a non-waiting database lock corresponding to the data and then update the data according to the write-once operation information. Moreover, when the data is updated, the thread only needs to perform one operation on the data according to the write-once operation information, so that the operation efficiency of the database can be greatly improved, but at the same time, only an operation record (i.e. only one operation record) performed according to the write-once operation information is recorded in the log of the data, and the operation record of this time is actually calculated according to each operation information, if no operation record corresponding to each operation information exists in the data log, the operation record in the log of the data is incomplete, that is, there is no detailed operation record corresponding to each operation information, and the operation record corresponding to each operation information is actually important data (for example, for account data, account details are one item of data which is important, that is, the operation record corresponding to each operation information is important), therefore, in the present application, the log information of the data may be updated according to each operation information corresponding to the write-once operation information.
And after the data is updated according to the complementary writing operation information and the log of the data is updated according to each operation information corresponding to the complementary writing operation information, the operation information in the cache record table can be updated, and each operation information corresponding to the complementary writing operation information is deleted to prevent repeated operation on the data and complete the complementary writing process.
Further, it should be noted that, when recording each operation information in the cache record table, each operation information may be recorded in the cache record table according to the time sequence of each operation information, so that when performing the complementary writing process, the complementary writing process may be performed according to the time sequence of each operation information recorded in the cache record table.
Continuing with the above example, assuming that it is determined to operate the account a in a cache record manner, the operation information a will be recorded in a cache record table in the database. It is assumed that, when the database management system fetches a specified amount of operation information from the cache record table according to a preset time period, the fetched operation information is shown in table 1.
Operation information
|
Identification of data
|
Operation information a
|
Account A
|
Operation information b
|
Account E
|
Operation information c
|
Account A
|
Operation information d
|
Account A
|
Operation information e
|
Account G
|
Operation information f
|
Account B |
TABLE 1
It can be seen that the data corresponding to the operation information obtained by the database management system at this time is: account a, account B, account E, account G, and according to the correspondence between the operation information and each data shown in table 1, for account a, 3 pieces of operation information may be determined: operation information a, operation information c, and operation information d.
Further assume that the operation information c and the operation information d are: transfer 20 out of account a and transfer 100 into account a. Then the operation information a according to the previous assumption is: transferring 10 yuan to account A, and calculating the complementary writing operation information aiming at the account A as follows: transfer 90 dollars to account a. Then the write-once process can be subsequently performed according to the write-once operation information.
With the data recording method shown in fig. 1, when an operation request for data in a database is received, whether the data needs to be operated by using cache recording is determined by obtaining different results of locks corresponding to the data. According to the method, the hot spot data does not need to be checked manually, the cache recording mode is configured for each hot spot data, and the data needing to be operated in the cache recording mode can be automatically determined. The advantages of the cache recording mode are guaranteed, meanwhile, the problems that data need to be manually configured in advance and the access mode of the data needs to be manually restored are solved, the operation efficiency of the database is improved, and the maintenance cost is reduced.
In addition, when the data management system salvages the specified amount of operation information in the cache record table according to the preset time period, in order to prevent the problem that the operation information is repeatedly salvaged by different threads and repeated operation is caused to the data due to the fact that the time consumption of the process of rewriting each data is long at the last time, in the application, the time required by the rewriting process corresponding to the specified amount of operation information does not exceed the preset time period.
Specifically, first, the database management system may determine the average time required for fishing one operation information per time based on the pre-recorded time taken for fishing each operation information (i.e., the time taken from the beginning of fishing to the end of fishing is determined based on the time stamp in the history of fishing each operation information), determining the time required for calculating the operation information once on average (e.g., calculating two operation information and determining the time of the complementary writing operation information corresponding to the two operation information) based on the time taken for calculating each operation information and determining the corresponding complementary writing operation information recorded in advance (i.e., determining the time taken from the start of calculation to the end of calculation based on the time stamp in the history of calculating each operation information), the time required to perform the one-time complementary writing process on average is determined according to the pre-recorded time consumed by each complementary writing process.
Then, the longest time required for the write-once process corresponding to a specified number of pieces of operation information to be fished at one time can be determined according to the average time required for fishing one piece of operation information each time, the average time required for calculating one piece of operation information, and the average time required for executing one write-once process (for example, when the specified number of pieces of operation information is 100 pieces of operation information, it is determined that 100 pieces of operation information are fished at one time, and the 100 pieces of operation information all correspond to the same data, that is, the time required for calculating 100 times).
And finally, judging whether the maximum time required by the rewriting process exceeds the preset time period according to the preset time period for fishing the operation information in the cache record table, if so, determining that the quantity of the operation information required by the rewriting process needs to be reduced until the maximum time required by the rewriting process does not exceed the preset time period, otherwise, determining that the value of the specified quantity is available, and fishing the operation information according to the specified quantity. Wherein the initial value of the specified number may be preset manually.
And, in order to improve the efficiency of the write-back process, when it is determined that the maximum time required for the write-back process does not exceed the preset time period, the database management system may further gradually increase the value of the specified number until the maximum time required for the write-back process exceeds the preset time period, and then it is determined that the value of the specified number does not exceed the current value of the specified number.
Of course, the actual usage value of the specified number may also be set directly according to manual experience. Furthermore, considering that the number of threads in the database system is limited, it is considered that when the data corresponding to the retrieved operation information are different, each thread may need to perform a plurality of write-back processes for different data within the preset time period. Therefore, when the value of the specified number is determined, the number of times of the complementary writing process which can be executed by all threads in the preset time period can be determined according to the number of the threads in the database, and the number is used as the value for determining the specified number. (e.g., assuming a total of 50 threads in the database, each time the write-back process takes a minimum of 10 seconds, it can be determined that a total of 50 threads in the database can execute within 1 minute
A second complementary write process, so the value of the number of executions may not exceed 300). Of course, the value of the designated number may also be determined by weighting and calculating a plurality of parameters, and how to determine the value of the designated number is not particularly limited in this application.
For example, if a preset time period in a certain database management system is 1 minute, and if the operation information in the cache record table is fished for the mth time, and the fished operation information is more, it takes 1 minute and 10 seconds to complete the write-back process, when the operation information in the cache record table is fished for the M +1 time, because the write-back process after the mth time is not completed, the operation information fished for the M +1 time is the same as the operation information fished for the mth time, resulting in repeated operation on data, and other operation information in the cache record table cannot be fished subsequently. Therefore, it is required to ensure that the time required by the complementary writing process corresponding to the specified amount of operation information does not exceed the preset time period.
Further, since the specified number of values is determined, it cannot be guaranteed that the time required for the overwriting process does not exceed the preset time period in actual use. Therefore, in order to prevent each operation information from being repeatedly operated, in the present application, before each time each operation information in the cache record table is fished, it may be further determined whether the last overwriting process has ended, if not, the current fishing of each operation information in the cache record table is stopped, and if so, each operation information in the cache record table is normally fished. When it is determined that the latest write-once process is not finished, the value of the specified number may be determined to be too large, so that the time required by the write-once process exceeds the preset time period, and then, the database management system may further reduce the value of the specified number at this time to prevent the time required by the write-once process from exceeding the preset time period.
Furthermore, in the present application, since the write-back process is also required to be executed by a thread, when the operation information retrieved by the database management system corresponds to a plurality of data, a thread needs to be started for each data to execute the write-back process, so that the plurality of write-back processes occupy the plurality of threads. However, in the database management system, the number of threads is fixed, so when the database management system operates data in a cache record manner, the corresponding write-back process may occupy all threads, and further, if the number of operation information to be written back is large, no thread may be used to execute other database transactions. Therefore, in order to prevent all threads in the database management system from being used in the write-once process, in the present application, the database management system may suspend the write-once process within a preset sleep time duration, where the sleep time may be set to start when each write-once process ends, that is, after each write-once process ends, the write-once process is suspended within the sleep time duration.
Still further, in step S103, when it is determined that the thread does not acquire the non-waiting database lock corresponding to the data, in addition to determining the probability of the hot data when the data is acquired according to the reason that the thread acquires the non-waiting database lock, in this application, the access frequency of each data accessed by each thread within a certain time range may be determined, then the average frequency of the data accessed by the thread in the database is determined, when the frequency of the data accessed by the thread is higher than the average frequency, it is determined that the data has a high probability of being hot data, the data may be operated in a cache recording manner, and when the frequency of the data accessed by the thread is lower than the average frequency, it is determined that the data has a high probability of not being hot data, and the data does not need to be operated in a cache recording manner.
Based on the data recording process shown in fig. 1, an embodiment of the present application further provides a data recording apparatus, as shown in fig. 2.
Fig. 2 is a schematic structural diagram of a data recording apparatus according to an embodiment of the present application, which specifically includes:
a receiving module 201, configured to receive an operation request for data in a database, where the operation request carries operation information;
an acquire lock module 202, configured to acquire a lock corresponding to the data according to the operation request;
the judgment execution module 203 is configured to judge whether the lock is acquired, if so, lock the data, and operate the data according to the operation information, and if not, operate the data in a cache recording manner.
The judgment execution module 203 is specifically configured to lock the data by using a non-waiting database lock.
The judgment execution module 203 is further configured to determine, before executing the data operation in the cache record manner, that the reason why the lock is not acquired is that the data has been locked.
The judgment execution module 203 is specifically configured to record the operation information in a cache record table, retrieve a specified number of operation information in the cache record table according to a preset time period, and execute a write-back process on data corresponding to each retrieved operation information according to each retrieved operation information.
And the time required by the complementary writing process corresponding to the specified amount of operation information does not exceed the preset time period.
The judgment execution module 203 is further configured to suspend the overwriting procedure within a preset sleep time length according to the sleep time length.
Specifically, the data recording apparatus shown in fig. 2 may be located in a database, or may be located in a database management system independent of the database, where the database may be a single device, or may be a system composed of multiple devices.
In a typical configuration, a computing device includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
The memory may include forms of volatile memory in a computer readable medium, Random Access Memory (RAM) and/or non-volatile memory, such as Read Only Memory (ROM) or flash memory (flash RAM). Memory is an example of a computer-readable medium.
Computer-readable media, including both non-transitory and non-transitory, removable and non-removable media, may implement information storage by any method or technology. The information may be computer readable instructions, data structures, modules of a program, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), Static Random Access Memory (SRAM), Dynamic Random Access Memory (DRAM), other types of Random Access Memory (RAM), Read Only Memory (ROM), Electrically Erasable Programmable Read Only Memory (EEPROM), flash memory or other memory technology, compact disc read only memory (CD-ROM), Digital Versatile Discs (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information that can be accessed by a computing device. As defined herein, a computer readable medium does not include a transitory computer readable medium such as a modulated data signal and a carrier wave.
It should also be noted that the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
As will be appreciated by one skilled in the art, embodiments of the present application may be provided as a method, system, or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The above description is only an example of the present application and is not intended to limit the present application. Various modifications and changes may occur to those skilled in the art. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of the claims of the present application.