Disclosure of Invention
In order to solve the problems in the prior art, embodiments of the present invention provide a method and an apparatus for redoing a log in a database record, so as to improve the processing speed of a database system. The technical scheme is as follows:
in one aspect, a method for redoing a log of a database record is provided, the method comprising:
receiving a log recording request, wherein the log recording request carries a transaction identifier and log information;
judging whether the log recording request is a first log recording request corresponding to the transaction identifier; if yes, selecting a part of cache space in the redo cache, establishing a transaction cache block corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block; otherwise, determining the currently used transaction cache block in the established transaction cache blocks corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block; the global sequence information records the time sequence of the log information in all log information;
and when a preset trigger event occurs, the log information in the redo cache is persisted.
Preferably, the method further comprises the following steps: dividing the redo cache into a plurality of cache blocks in advance;
selecting a part of cache space in the redo cache, and establishing a transaction cache block corresponding to the transaction identifier, specifically: and selecting a cache block which is divided in advance from the redo cache, and establishing a transaction cache block corresponding to the transaction identifier.
Preferably, the log record request further carries capacity information of the transaction cache block;
selecting a part of cache space in the redo cache, and establishing a transaction cache block corresponding to the transaction identifier, specifically: and selecting a cache space with corresponding capacity in the redo cache according to the capacity information, and establishing a transaction cache block corresponding to the transaction identifier.
Preferably, after determining the currently used transaction cache block in the established transaction cache block corresponding to the transaction identifier, the method further includes:
if the used space of the currently used transaction cache block plus the space required for recording the log information exceeds a preset first threshold value, selecting a part of cache space in the redo cache, establishing a new transaction cache block corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block;
and if the used space of the currently used transaction cache block plus the space required for recording the log information does not exceed a preset first threshold value, recording the log information and the corresponding global sequence information into the transaction cache block.
Preferably, the preset trigger event specifically includes:
the last log of the transaction is recorded in the redo cache; or,
reaching a preset time period; or,
the used space of the redo cache exceeds a preset second threshold.
Preferably, the persisting the log information in the redo cache specifically includes:
obtaining log information which is not subjected to persistence in the redo cache;
recording the acquired log information on a magnetic disk according to time sequence according to global sequence information corresponding to the acquired log information;
and releasing the transaction cache block corresponding to the transaction which has recorded the last log in the redo cache.
Preferably, the method further comprises the following steps:
when the persistence process triggered by one trigger event is completed, if the subsequent triggered persistence process is waiting, recording all the log information waiting to the disk according to the global sequence information corresponding to all the log information waiting to the disk according to the time sequence.
In another aspect, a method of redoing a log of a database record is provided, the method comprising:
when a transaction starting notice carrying a transaction identifier is received, selecting a part of cache space in a redo cache, and establishing a transaction cache block corresponding to the transaction identifier; the global sequence information records the time sequence of the log information in all log information;
when a log recording request carrying a transaction identifier and log information is received, determining a currently used transaction cache block in established transaction cache blocks corresponding to the transaction identifier, and recording the log information and corresponding global sequence information into the transaction cache block;
and when a preset trigger event occurs, the log information in the redo cache is persisted.
Preferably, the method further comprises the following steps: dividing the redo cache into a plurality of cache blocks in advance;
selecting a part of cache space in the redo cache, and establishing a transaction cache block corresponding to the transaction identifier, specifically: and selecting a cache block which is divided in advance from the redo cache, and establishing a transaction cache block corresponding to the transaction identifier.
Preferably, after determining the currently used transaction cache block in the established transaction cache block corresponding to the transaction identifier, the method further includes:
if the used space of the currently used transaction cache block plus the space required for recording the log information exceeds a preset first threshold value, selecting a part of cache space in the redo cache, establishing a new transaction cache block corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block;
and if the used space of the currently used transaction cache block plus the space required for recording the log information does not exceed a preset first threshold value, recording the log information and the corresponding global sequence information into the transaction cache block.
Preferably, the preset trigger event specifically includes:
the last log of the transaction is recorded in the redo cache; or,
reaching a preset time period; or,
the used space of the redo cache exceeds a preset second threshold.
Preferably, the persisting the log information in the redo cache specifically includes:
obtaining log information which is not subjected to persistence in the redo cache;
recording the acquired log information on a magnetic disk according to time sequence according to global sequence information corresponding to the acquired log information;
and releasing the transaction cache block corresponding to the transaction which has recorded the last log in the redo cache.
Preferably, the method further comprises the following steps:
when the persistence process triggered by one trigger event is completed, if the subsequent triggered persistence process is waiting, recording all the log information waiting to the disk according to the global sequence information corresponding to all the log information waiting to the disk according to the time sequence.
In another aspect, an apparatus for redoing a log of a database record is provided, the apparatus comprising:
the system comprises a receiving module, a sending module and a receiving module, wherein the receiving module is used for receiving a log recording request which carries a transaction identifier and log information;
the judging module is used for judging whether the log recording request is a first log recording request corresponding to the transaction identifier; if yes, selecting a part of cache space in the redo cache, establishing a transaction cache block corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block; otherwise, determining the currently used transaction cache block in the established transaction cache blocks corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block; the global sequence information records the time sequence of the log information in all log information;
and the persistence module is used for persisting the log information in the redo cache when a preset trigger event occurs.
Preferably, the apparatus further includes a dividing module, configured to divide the redo cache into a plurality of cache blocks in advance;
the determining module is specifically configured to select a cache block partitioned in advance from the redo cache, and establish a transaction cache block corresponding to the transaction identifier.
Preferably, the log record request further carries capacity information of the transaction cache block;
the judging module is specifically configured to select a cache space with a corresponding capacity from the redo cache according to the capacity information, and establish a transaction cache block corresponding to the transaction identifier.
Preferably, after determining the currently used transaction cache block in the established transaction cache blocks corresponding to the transaction identifier, the determining module is further configured to:
if the used space of the currently used transaction cache block plus the space required for recording the log information exceeds a preset first threshold value, selecting a part of cache space in the redo cache, establishing a new transaction cache block corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block;
and if the used space of the currently used transaction cache block plus the space required for recording the log information does not exceed a preset first threshold value, recording the log information and the corresponding global sequence information into the transaction cache block.
Preferably, the preset trigger event specifically includes:
the last log of the transaction is recorded in the redo cache; or,
reaching a preset time period; or,
the used space of the redo cache exceeds a preset second threshold.
Preferably, the persistence module is specifically configured to:
obtaining log information which is not subjected to persistence in the redo cache;
recording the acquired log information on a magnetic disk according to time sequence according to global sequence information corresponding to the acquired log information;
and releasing the transaction cache block corresponding to the transaction which has recorded the last log in the redo cache.
Preferably, the persistence module is further configured to:
when the persistence process triggered by one trigger event is completed, if the subsequent triggered persistence process is waiting, recording all the log information waiting to the disk according to the global sequence information corresponding to all the log information waiting to the disk according to the time sequence.
In another aspect, an apparatus for redoing a log of a database record is provided, the apparatus comprising:
the establishment module is used for selecting part of cache space in the redo cache and establishing a transaction cache block corresponding to the transaction identifier when receiving a transaction starting notice carrying the transaction identifier; the global sequence information records the time sequence of the log information in all log information;
a recording module, configured to determine, when a log recording request carrying a transaction identifier and log information is received, a currently used transaction cache block in an established transaction cache block corresponding to the transaction identifier, and record the log information and corresponding global sequence information into the transaction cache block;
and the persistence module is used for persisting the log information in the redo cache when a preset trigger event occurs.
Preferably, the apparatus further includes a dividing module, configured to divide the redo cache into a plurality of cache blocks in advance;
the establishing module is specifically configured to select a cache block partitioned in advance from the redo cache, and establish a transaction cache block corresponding to the transaction identifier.
Preferably, after determining the currently used transaction cache block in the established transaction cache blocks corresponding to the transaction identifier, the recording module is further configured to:
if the used space of the currently used transaction cache block plus the space required for recording the log information exceeds a preset first threshold value, selecting a part of cache space in the redo cache, establishing a new transaction cache block corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block;
and if the used space of the currently used transaction cache block plus the space required for recording the log information does not exceed a preset first threshold value, recording the log information and the corresponding global sequence information into the transaction cache block.
Preferably, the preset trigger event specifically includes:
the last log of the transaction is recorded in the redo cache; or,
reaching a preset time period; or,
the used space of the redo cache exceeds a preset second threshold.
Preferably, the persistence module is specifically configured to:
obtaining log information which is not subjected to persistence in the redo cache;
recording the acquired log information on a magnetic disk according to time sequence according to global sequence information corresponding to the acquired log information;
and releasing the transaction cache block corresponding to the transaction which has recorded the last log in the redo cache.
Preferably, the persistence module is further configured to:
when the persistence process triggered by one trigger event is completed, if the subsequent triggered persistence process is waiting, recording all the log information waiting to the disk according to the global sequence information corresponding to all the log information waiting to the disk according to the time sequence.
The technical scheme provided by the embodiment of the invention has the following beneficial effects:
according to the embodiment of the invention, for each transaction, an independent space is allocated in the redo cache, the corresponding transaction cache block is established, the log information of the transaction is stored, and a plurality of transactions can be allowed to record logs in the redo cache at the same time, so that the processing speed of a database system can be improved compared with the prior art.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, embodiments of the present invention will be described in detail with reference to the accompanying drawings.
Example one
An embodiment of the present invention provides a method for redoing a log in a database record, and with reference to fig. 1, a processing flow of the method may include:
step 101, receiving a log recording request, wherein the log recording request carries a transaction identifier and log information.
Step 102, judging whether the log recording request is the first log recording request corresponding to the transaction identifier, if so, executing step 103, otherwise, executing step 104.
Step 103, selecting a part of cache space in the redo cache, establishing a transaction cache block corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block. The global sequence information records the time sequence of the log information in all log information.
Step 104, determining the currently used transaction cache block in the established transaction cache blocks corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block.
And 105, when a preset trigger event occurs, persisting the log information in the redo cache.
In the embodiment of the invention, for each transaction, an independent space is allocated in the redo cache, a corresponding transaction cache block is established, the log information of the transaction is stored, and a plurality of transactions can be allowed to record logs in the redo cache at the same time, so that the processing speed of a database system can be improved compared with the prior art.
Example two
An embodiment of the present invention provides a method for redoing a log recorded in a database, where a Processing flow may be as shown in fig. 1, an execution subject of the method may be a Central Processing Unit (CPU), and the following describes the flow in fig. 1 in detail, specifically, the following contents are as follows:
step 101, receiving a log recording request, wherein the log recording request carries a transaction identifier and log information.
The transaction can comprise one or more operations, when the transaction operates on the database, a corresponding log needs to be recorded, and a log recording request can be sent to the CPU. The transaction identifier is used to identify the transaction corresponding to the log, and the log information is the content of the operation corresponding to the log, for example, the value1 (value 1) corresponding to the key1 (primary key 1) is modified to value 1'.
Step 102, determining whether the log recording request is the first log recording request corresponding to the transaction identifier (i.e. the first log recording request of the transaction), if so, executing step 103, otherwise, executing step 104.
Specifically, it may be determined whether a log recording request carrying the transaction identifier is received before, and if so, it indicates that the log recording request is not the first log recording request corresponding to the transaction identifier, otherwise, it indicates that the log recording request is the first log recording request corresponding to the transaction identifier.
Specifically, whether an established transaction cache block corresponding to the transaction identifier exists in the redo cache may be queried, and if yes, it indicates that the log recording request is not the first log recording request corresponding to the transaction identifier, otherwise, it indicates that the log recording request is the first log recording request corresponding to the transaction identifier.
Specifically, an identification bit may be further set in the logging request, so as to identify that the logging request is the second logging request of the transaction, or to identify whether the logging request is the first logging request of the transaction.
Step 103, selecting a part of cache space in the redo cache, establishing a transaction cache block corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block. After the transactional cache block is created, the newly created transactional cache block may be set as the currently used transactional cache block. Specifically, a corresponding state flag may be set for each transaction cache block to identify whether the transaction cache block is currently used or not currently used.
Specifically, the method for selecting a part of cache space in the redo cache and establishing the transaction cache block corresponding to the transaction identifier may be as follows:
one method is to divide the redo cache into a plurality of cache blocks in advance, select one of the cache blocks divided in advance from the redo cache, and establish a transaction cache block corresponding to the transaction identifier.
Preferably, the spatial sizes of the plurality of cache blocks divided in advance may be the same. Two lists may be maintained, one list may be a used list in which the identity of used cache blocks may be recorded, and the other list may be an unused list in which the identity of unused cache blocks may be recorded. When selecting the cache block, one cache block can be selected randomly or sequentially from the unused list, the identifier of the selected cache block is deleted from the unused list and is added to the used list, and the transaction identifier corresponding to the cache block can be recorded in the used list. In addition, a transaction identification bit may also be set for each cache block, and a corresponding transaction identification is recorded.
The other method is that the log record request carries the capacity information of the transaction cache block, and according to the capacity information, a cache space with corresponding capacity is selected from the redo cache, and the transaction cache block corresponding to the transaction identifier is established.
Specifically, different transactions may request cache spaces of different capacities according to different requirements, and establish corresponding transaction cache blocks for recording log information corresponding to each operation of the transaction cache blocks. A transaction identification bit may be set for each cache block, and a corresponding transaction identification may be recorded.
In this step, the global sequence information is used to record the time sequence of the log information in all log information. When the database system stores the log information, it may maintain a piece of global sequence information (e.g., a global sequence number) for all log information, and record the recording timing sequence of each log, where the global sequence number may reflect the execution sequence of the operation corresponding to the log. When data recovery is performed, the system may perform operations recorded in the log in a reverse direction according to the global sequence number and the content of the corresponding operation recorded in the log.
Step 104, determining the currently used transaction cache block in the established transaction cache block corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block.
In some cases, the redo cache may be pre-divided into a plurality of cache blocks with a large enough space, which is enough for each transaction to record all log information of the transaction, or the space size corresponding to the capacity information carried in the log recording request is enough for storing all log information of the corresponding transaction. Thus, only one transaction cache block needs to be established for each transaction to record corresponding log information. Then, for a non-first logging request of a certain transaction, it and the corresponding global sequence information can be directly recorded into the transaction cache block established for the transaction.
In some other cases, in order to improve the space utilization of the redo cache, the redo cache may be pre-divided into a plurality of cache blocks with smaller space, one transaction may correspond to a plurality of transaction cache blocks, and a plurality of log information of one transaction may be recorded in different transaction cache blocks. Thus, for a non-first log recording request of a certain transaction, after a currently used transaction cache block in an established transaction cache block corresponding to a transaction identifier is determined, if the used space of the currently used transaction cache block plus the space required for recording the log information exceeds a preset first threshold value, selecting a part of cache space in a redo cache, establishing a new transaction cache block corresponding to the transaction identifier, and recording the log information and corresponding global sequence information into the transaction cache block; if the used space of the currently used transaction cache block plus the space required for recording the log information does not exceed a preset first threshold value, recording the log information and the corresponding global sequence information into the transaction cache block. After the transaction cache block is created, the newly created transaction cache block may be set as a new currently used transaction cache block to replace the original transaction cache block that has been recorded to be full.
The space required for recording the log information is the space of the redo cache actually occupied when the log information is recorded into the redo cache, and may include the space occupied by the log information itself, the space occupied by the corresponding global sequence information, the space occupied by some corresponding identification bits, and the like.
In this way, the system may set a first threshold for storing log information for each cache block, which may preferably be a proportional threshold, such as 90% of the cache block space, corresponding to a transaction, and if the sum of the used space of the currently used transaction cache block and the space required for recording the log information exceeds 90% of the transaction cache block space, create a new transaction cache block and record the log information in the new transaction cache block, and if the sum of the used space of the currently used transaction cache block and the space required for recording the log information does not exceed 90% of the transaction cache block space, record the log information in the transaction cache block. Preferably, the first threshold may be set to 100% of the cache block space. Thus, for a transaction, the process of recording log information may be to establish a first transaction cache block and record log information therein, when the first transaction cache block is full (exceeds a first threshold), establish a second transaction cache block and record log information therein, and so on until the last log of the transaction is recorded.
And 105, when a preset trigger event occurs, persisting the log information in the redo cache.
The preset trigger event may be that the last log of the transaction is recorded in the redo cache, that is, when each transaction finishes recording the last log in the redo cache, the process of persisting the log information in the redo cache is triggered. An identification bit may be set in the logging request to identify that the logging request is the last logging request of the transaction. Or, after the transaction sends the last log recording request, a notification of completion of the transaction processing may be separately sent, and after the system receives the notification, it may be determined that the last log of the transaction is already recorded in the redo cache.
The preset trigger event may also be that a preset time period is reached, that is, the log information in the redo cache is periodically persisted.
The preset trigger event may also be that the used space of the redo cache exceeds a preset second threshold, that is, a persistence process is performed when the space of the redo cache is used to a certain extent, so that data overflow of the redo cache can be prevented.
When the log information in the redo cache is persisted, all the log information in the redo cache may be written into the disk, or part of the log information may be written into the disk, for example, all the log information corresponding to the transaction in which the last log has been recorded may be written into the disk. The log information written into the disk may be written into the disk in time sequence or out of sequence according to the corresponding global sequence information, and the global sequence information of each log information is also recorded in the disk, and may be performed according to the global sequence information when performing data recovery. After the work of writing the log information into the disk is completed, the log information written into the disk in the redo cache can be deleted, all the log information written into the disk can be deleted, or only a part of the log information can be deleted, for example, all the log information corresponding to the transaction in which the last log is recorded can be deleted, and in the case of partial deletion, the log information written into the disk but not deleted in the redo cache can be labeled to note that the log information has been written into the disk.
Preferably, the method for persisting the log information in the redo cache may be as follows: firstly, obtaining log information which is not subjected to persistence in a redo cache; then, according to global sequence information corresponding to the acquired log information, recording the acquired log information on a magnetic disk in a time sequence; and finally, releasing the transaction cache block corresponding to the transaction which has recorded the last log in the redo cache.
In the preferable persistence method, all log information which is not subjected to persistence in the redo cache is written into the disk, and the log information is written according to the time sequence, so that the log information written into the disk is ensured to be arranged according to the time sequence, and the data recovery speed can be improved to a certain extent when data recovery is carried out. When the log information written into the disk is deleted, the log information in each transaction cache block corresponding to the transaction in which the last log is recorded is deleted, and the log information corresponding to the transaction in which the last log is not recorded can be temporarily not deleted. In this case, the persistent identifier may be added to the log information that has been written to the disk but not deleted in the redo cache, and the log information that has been added with the persistent identifier may not be written to the disk in the persistence process triggered next time.
In the persistence process triggered by a trigger event, when the log information is written into the disk, a write operation needs to be initiated, and the write operation needs a certain execution time, so that in the execution process of the write operation, one or more subsequent persistence processes may have been triggered, and a corresponding write request is sent, at this time, in order to increase the processing speed, the following processing method may be adopted: when the persistence process triggered by one trigger event is completed, if the subsequent triggered persistence process is waiting, recording all the log information waiting to the disk according to the global sequence information corresponding to all the log information waiting to the disk according to the time sequence.
The system can maintain a waiting queue, when a new write request is received in the process of one-time write operation of the disk, the log information corresponding to the write request is put into the waiting queue, a plurality of subsequent write requests can be received in the process of one-time write operation, and when the execution of the write operation is finished, one-time write operation can be initiated on all the log information in the waiting queue. The processing method does not need to respectively initiate one writing operation for each writing request in waiting, but initiates one total writing operation, thereby improving the persistence speed to a certain extent.
In the embodiment of the invention, for each transaction, an independent space is allocated in the redo cache, a corresponding transaction cache block is established, the log information of the transaction is stored, and a plurality of transactions can be allowed to record logs in the redo cache at the same time, so that the processing speed of a database system can be improved compared with the prior art. Moreover, through the recording of the global sequence information, the time sequence of the log information can be ensured, namely the recoverability of the database is ensured.
EXAMPLE III
The embodiment of the invention provides a method for recording redo logs by a database, wherein an execution main body of the method can be a CPU, and the specific content of the method can be as follows:
when a transaction starting notice carrying a transaction identifier is received, selecting a part of cache space in the redo cache, and establishing a transaction cache block corresponding to the transaction identifier. And the global sequence information records the time sequence of the log information in all log information. After the transactional cache block is created, the newly created transactional cache block may be set as the currently used transactional cache block. Specifically, the specific content of step 103 in the second embodiment may be referred to in the process of creating the transaction cache block, and will not be described herein in detail.
When a log recording request carrying a transaction identifier and log information is received, determining a currently used transaction cache block in an established transaction cache block corresponding to the transaction identifier, and recording the log information and corresponding global sequence information into the transaction cache block. Specifically, the process of logging information can refer to the specific content of step 104 in embodiment two, and will not be described herein in a repeated manner.
And when a preset trigger event occurs, the log information in the redo cache is persisted. Specifically, the process of persisting the log information may refer to the specific content of step 105 in embodiment two, and will not be described herein in detail.
In the embodiment of the invention, for each transaction, an independent space is allocated in the redo cache, a corresponding transaction cache block is established, the log information of the transaction is stored, and a plurality of transactions can be allowed to record logs in the redo cache at the same time, so that the processing speed of a database system can be improved compared with the prior art. Moreover, through the recording of the global sequence information, the time sequence of the log information can be ensured, namely the recoverability of the database is ensured.
Example four
Based on the same technical concept, an embodiment of the present invention provides an apparatus for redoing a log in a database, and referring to fig. 2, the apparatus includes:
a receiving module 210, configured to receive a log recording request, where the log recording request carries a transaction identifier and log information;
a determining module 220, configured to determine whether the log recording request is a first log recording request corresponding to the transaction identifier; if yes, selecting a part of cache space in the redo cache, establishing a transaction cache block corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block; otherwise, determining the established transaction cache block corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block; the global sequence information records the time sequence of the log information in all log information;
the persistence module 230 is configured to, when a preset trigger event occurs, persist the log information in the redo cache.
Preferably, the apparatus further includes a dividing module, configured to divide the redo cache into a plurality of cache blocks in advance;
the determining module 220 is specifically configured to select a cache block partitioned in advance from the redo cache, and establish a transaction cache block corresponding to the transaction identifier.
Preferably, the log record request further carries capacity information of the transaction cache block;
the determining module 220 is specifically configured to select a cache space with a corresponding capacity from the redo cache according to the capacity information, and establish a transaction cache block corresponding to the transaction identifier.
Preferably, after determining the established transaction cache block corresponding to the transaction identifier, the determining module 220 is further configured to:
if the used space of each established transaction cache block corresponding to the transaction identifier exceeds a preset first threshold value, selecting a part of cache space in the redo cache, establishing a new transaction cache block corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block;
and if the transaction cache block with the used space not exceeding a preset first threshold exists in the established transaction cache blocks corresponding to the transaction identifier, recording the log information and the corresponding global sequence information into the transaction cache block.
Preferably, the preset trigger event specifically includes:
the last log of the transaction is recorded in the redo cache; or,
reaching a preset time period; or,
the used space of the redo cache exceeds a preset second threshold.
Preferably, the persistence module 230 is specifically configured to:
obtaining log information which is not subjected to persistence in the redo cache;
recording the acquired log information on a magnetic disk according to time sequence according to global sequence information corresponding to the acquired log information;
and releasing the transaction cache block corresponding to the transaction which has recorded the last log in the redo cache.
Preferably, the persistence module 230 is further configured to:
when the persistence process triggered by one trigger event is completed, if the subsequent triggered persistence process is waiting, recording all the log information waiting to the disk according to the global sequence information corresponding to all the log information waiting to the disk according to the time sequence.
In the embodiment of the invention, for each transaction, an independent space is allocated in the redo cache, a corresponding transaction cache block is established, the log information of the transaction is stored, and a plurality of transactions can be allowed to record logs in the redo cache at the same time, so that the processing speed of a database system can be improved compared with the prior art. Moreover, through the recording of the global sequence information, the time sequence of the log information can be ensured, namely the recoverability of the database is ensured.
EXAMPLE five
Based on the same technical concept, an embodiment of the present invention provides an apparatus for redoing a log in a database, and referring to fig. 3, the apparatus includes:
an establishing module 310, configured to select a part of cache space in a redo cache when receiving a transaction start notification carrying a transaction identifier, and establish a transaction cache block corresponding to the transaction identifier; the global sequence information records the time sequence of the log information in all log information;
a recording module 320, configured to determine, when a log recording request carrying a transaction identifier and log information is received, a currently used transaction cache block in an established transaction cache block corresponding to the transaction identifier, and record the log information and corresponding global sequence information into the transaction cache block;
the persistence module 330 is configured to persist the log information in the redo cache when a preset trigger event occurs.
Preferably, the apparatus further includes a dividing module, configured to divide the redo cache into a plurality of cache blocks in advance;
the establishing module 310 is specifically configured to select a cache block partitioned in advance from the redo cache, and establish a transaction cache block corresponding to the transaction identifier.
Preferably, after determining the currently used transaction cache block in the established transaction cache blocks corresponding to the transaction identifier, the recording module 320 is further configured to:
if the used space of the currently used transaction cache block plus the space required for recording the log information exceeds a preset first threshold value, selecting a part of cache space in the redo cache, establishing a new transaction cache block corresponding to the transaction identifier, and recording the log information and the corresponding global sequence information into the transaction cache block;
and if the used space of the currently used transaction cache block plus the space required for recording the log information does not exceed a preset first threshold value, recording the log information and the corresponding global sequence information into the transaction cache block.
Preferably, the preset trigger event specifically includes:
the last log of the transaction is recorded in the redo cache; or,
reaching a preset time period; or,
the used space of the redo cache exceeds a preset second threshold.
Preferably, the persistence module 330 is specifically configured to:
obtaining log information which is not subjected to persistence in the redo cache;
recording the acquired log information on a magnetic disk according to time sequence according to global sequence information corresponding to the acquired log information;
and releasing the transaction cache block corresponding to the transaction which has recorded the last log in the redo cache.
Preferably, the persistence module 330 is further configured to:
when the persistence process triggered by one trigger event is completed, if the subsequent triggered persistence process is waiting, recording all the log information waiting to the disk according to the global sequence information corresponding to all the log information waiting to the disk according to the time sequence.
In the embodiment of the invention, for each transaction, an independent space is allocated in the redo cache, a corresponding transaction cache block is established, the log information of the transaction is stored, and a plurality of transactions can be allowed to record logs in the redo cache at the same time, so that the processing speed of a database system can be improved compared with the prior art. Moreover, through the recording of the global sequence information, the time sequence of the log information can be ensured, namely the recoverability of the database is ensured.
It should be noted that: in the above embodiment, when the apparatus for recording redo logs in the database records redo logs, only the division of the functional modules is used for illustration, and in practical applications, the function distribution may be completed by different functional modules according to needs, that is, the internal structure of the apparatus is divided into different functional modules, so as to complete all or part of the functions described above. In addition, the apparatus for recording redo logs in a database and the method for recording redo logs in a database provided by the above embodiments belong to the same technical concept, and the specific implementation process is detailed in the method embodiments and will not be described herein again.
The above-mentioned serial numbers of the embodiments of the present invention are merely for description and do not represent the merits of the embodiments.
It will be understood by those skilled in the art that all or part of the steps for implementing the above embodiments may be implemented by hardware, or may be implemented by a program instructing relevant hardware, where the program may be stored in a computer-readable storage medium, and the above-mentioned storage medium may be a read-only memory, a magnetic disk or an optical disk, etc.
The above description is only for the purpose of illustrating the preferred embodiments of the present invention and is not to be construed as limiting the invention, and any modifications, equivalents, improvements and the like that fall within the spirit and principle of the present invention are intended to be included therein.