Data reading method, device, equipment and storage medium
By playing back the physical log in the slave device and judging the structure change status, the problem of inconsistent data storage structure in the master-slave architecture is solved, and data consistency reading and performance improvement are achieved.
Patent Information
- Application Number
- CN202010650956.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-07-08
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2040-07-08
AI Technical Summary
In a master-slave architecture based on physical replication, when the slave database replays the structure change operation, it may cause inconsistency in the data storage structure, resulting in inconsistent data read by users or system crash.
When the physical log is played back in the slave device, the data read request is obtained, the index page is accessed, and the structure change operation mark in the physical log is used to determine whether the structure change is in progress. If it is in the change process, the preset time is performed. sleep, and then read the data after the change operation is completed to ensure the consistency of the data storage structure.
It effectively avoids system crashes caused by changes in data storage structure, achieves consistent reading of data, improves the reading performance of slave devices, and reduces storage pressure.
Smart Images

Figure CN113918535B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computers, and in particular to a data reading method, device, equipment and storage medium. Background Art
[0002] With the rapid development of computer technology, the amount of data stored in databases is increasing. To this end, the master-slave architecture of databases came into being, in which the master database is generally used for write operations and the slave database is generally used for read operations, realizing read-write separation, reducing the read-write pressure of the master database and improving performance.
[0003] The master-slave database achieves master-slave consistency based on physical replication. However, in the master-slave architecture based on physical replication, when the slave database log playback thread plays back the structure modification operation (SMO), the data storage structure, such as the B-tree structure, will be changed. If data is read during this process, it may cause inconsistent results of concurrent user read threads, that is, inconsistent data read, or system crash.
[0004] Therefore, in a master-slave architecture based on physical replication, reading data consistently from the database is a technical problem that needs to be solved urgently. Summary of the invention
[0005] In order to solve the above technical problems, the present application provides a data reading method, device, equipment and storage medium to avoid system crash due to changes in data storage structure and achieve consistent data reading.
[0006] The embodiments of the present application disclose the following technical solutions:
[0007] On the one hand, an embodiment of the present application provides a data reading method, which is executed by a slave device in a master-slave architecture based on physical replication, and the method includes:
[0008] In the process of the slave device replaying the physical log, obtaining a data read request for reading target data;
[0009] According to the data read request, accessing index pages in sequence according to the data storage structure in the slave device;
[0010] When accessing a target index page, determining whether the target index page is in a structure change operation process according to the structure change operation mark added to the physical log;
[0011] If it is determined that the target index page is in the process of a structure change operation, hibernation for a preset time;
[0012] After the hibernation ends, the step of accessing index pages in sequence according to the data reading request and the data storage structure in the slave device is re-executed to read the target data.
[0013] On the one hand, an embodiment of the present application provides a data reading device, which is deployed in a slave device in a master-slave architecture based on physical replication, and includes an acquisition unit, an access unit, a determination unit, a sleep unit, and a reading unit:
[0014] The acquisition unit is used to acquire a data read request for reading target data during the process of the slave device playing back the physical log;
[0015] The access unit is used to access the index pages in sequence according to the data storage structure in the slave device according to the data read request;
[0016] The determining unit is configured to determine whether the target index page is in a structure change operation process according to the structure change operation mark added to the physical log when the target index page is accessed;
[0017] The dormancy unit is configured to perform dormancy for a preset time if it is determined that the target index page is in the process of a structure change operation;
[0018] The reading unit is used to re-execute the step of accessing index pages in sequence according to the data storage structure in the slave device according to the data read request after the sleep ends, so as to read the target data.
[0019] In a possible implementation, the device further includes a releasing unit:
[0020] The release unit is used to release a target path accessing the target index page if it is determined that the target index page is in the process of a structure change operation, wherein the target path is a path from the parent node to the intermediate nodes corresponding to the target index page in sequence.
[0021] In a possible implementation manner, the device further includes a storage unit:
[0022] The saving unit is used to save the location information of the target data in the target index page if it is determined that the target index page is in the process of a structure change operation and if the target index page is the index page corresponding to the leaf node.
[0023] In a possible implementation, the device further includes a returning unit:
[0024] The returning unit is used to return the target data to the terminal device corresponding to the user.
[0025] On the one hand, an embodiment of the present application provides an electronic device for reading data, the electronic device comprising a processor and a memory:
[0026] The memory is used to store program code and transmit the program code to the processor;
[0027] The processor is used to execute any of the data reading methods according to the instructions in the program code.
[0028] On the one hand, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium is used to store program code, and the program code is used to execute any of the data reading methods described above.
[0029] It can be seen from the above technical solution that in the master-slave architecture based on physical replication, the slave device can achieve data consistency between the master and slave devices by replaying the physical log for physical replication. In the process of the slave device replaying the physical log for physical replication, if the user needs to read the target data through the slave device, the slave device can obtain a data read request for reading the target data, and access the index page in sequence according to the data storage structure in the slave device according to the data read request. For each index page accessed, such as the target index page, the slave device determines whether the target index page is in the process of structural change operation according to the structural change operation mark added to the physical log. If it is determined that the target index page is in the process of structural change operation, it will sleep for a preset time. Since the speed of playback by the slave device through the log playback thread is very fast, after the preset time of sleep, the playback of the structural change operation of the target index page has been completed. At this time, the step of accessing the index page in sequence according to the data read request and the data storage structure in the slave device is re-executed to read the target data. By judging in real time whether each index page accessed, such as the target index page, is in the process of a structural change operation, it can be ensured that the target index page is accessed after the structural change operation playback is completed. At this time, the data storage structure, such as the structure of the B-tree, no longer changes and is in a consistent state, thus avoiding system crashes due to changes in the data storage structure and achieving consistent data reading. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technical members in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0031] Figure 1 A schematic diagram of the system architecture of a data reading method provided in an embodiment of the present application;
[0032] Figure 2 A flowchart of a data reading method provided in an embodiment of the present application;
[0033] Figure 3 A schematic diagram of the structure of a B-tree provided in an embodiment of the present application;
[0034] Figure 4 A schematic diagram of a complete SMO log provided in an embodiment of the present application;
[0035] Figure 5 A schematic diagram of SMO log playback to SMO marking provided in an embodiment of the present application;
[0036] Figure 6 A schematic diagram of the structure of a B-tree in the SMO playback process provided in an embodiment of the present application;
[0037] Figure 7 A schematic diagram of a complete SMO log after the SMO log playback is completed provided in an embodiment of the present application;
[0038] Figure 8 A schematic diagram of the structure of the B-tree after the SMO log playback is completed provided in the embodiment of the present application;
[0039] Fig. 9 A flowchart of a data reading method provided in an embodiment of the present application;
[0040] Fig.10 A structural diagram of a data reading device provided in an embodiment of the present application;
[0041] Fig.11 A structural diagram of a terminal device provided in an embodiment of the present application;
[0042] Fig.12 A structural diagram of a server provided in an embodiment of the present application. DETAILED DESCRIPTION
[0043] The embodiments of the present application are described below in conjunction with the accompanying drawings.
[0044] Physical replication: refers to the method of master-slave replication based on the physical log (redo log) of the database. The master device transmits the physical log to the slave device, and the slave device applies the physical log to achieve master-slave data consistency.
[0045] In a master-slave architecture based on physical replication, taking the case where the data storage structure of the slave database is a B-tree, if the slave database is installed on a slave device, when the slave device replays the structure modification operation (SMO) through the log playback thread, the structure of the B-tree will be changed. If data is read during the SMO process, the concurrent user reading thread may read inconsistent data or the system may crash due to the change in the B-tree structure. Among them, B-tree is a common way of organizing data physically and also a way of indexing data; SMO refers to structure change operations, including split or merge operations, which generally involve the modification of three or more index pages. For example, the splitting of index page A into index page A and index page B is a split operation, which changes the data storage structure.
[0046] In order to solve the above technical problems, an embodiment of the present application provides a data reading method, which determines in real time whether each index page accessed, such as a target index page, is in the SMO process. This can ensure that the access is made after the SMO playback of the target index page is completed. At this time, the data storage structure, such as the structure of the B-tree, no longer changes and is in a consistent state, thereby avoiding system crashes due to changes in the data storage structure and achieving consistent data reading.
[0047] Because concurrent add, delete, modify, and query operations may occur on the data storage structure when reading data, consistent reading means that when reading, when accessing based on the data storage structure, such as when performing a B-tree scan, the data storage structure is guaranteed to be complete and unchanged, so that the read data is consistent. There is no situation where too many records are read or too few records are read.
[0048] The slave device and the host device form a master-slave architecture based on physical replication, a slave database can be installed on the slave device, a master database can be installed on the host device, and the slave device and the host device are usually servers. The data reading method provided in the embodiment of the present application can be executed by a slave device in a master-slave architecture based on physical replication.
[0049] The method provided in the embodiment of the present application relates to the field of cloud technology, for example, to a database in the field of cloud technology. A database is a place where electronic files are stored, and users can perform operations such as adding, querying, updating, and deleting data in the files. The so-called "database" is a collection of data that is stored together in a certain way, can be shared with multiple users, has as little redundancy as possible, and is independent of the application program.
[0050] The method provided in the embodiment of the present application, for example, involves data management in a database, which can be implemented through a database management system (DBMS). DBMS is a computer software system designed for managing databases, and generally has basic functions such as storage, interception, security, and backup. Database management systems can be classified according to the database models they support, such as relational, Extensible Markup Language (XML); or according to the types of computers they support, such as server clusters, mobile phones; or according to the query languages used, such as Structured Query Language (SQL), XQuery; or according to performance impulse focus, such as maximum scale, maximum operating speed; or other classification methods. Regardless of the classification method used, some DBMSs can cross categories, for example, supporting multiple query languages at the same time.
[0051] See also Figure 1 , Figure 1 A schematic diagram of the system architecture of the data reading method provided in the embodiment of the present application. The system architecture includes a terminal device 101, a slave server 102 and a host server 103, and the slave server 102 and the host server 103 constitute a master-slave architecture based on physical replication. The slave server 102 can be a slave device involved in the embodiment of the present application, and a slave database is deployed on the slave server 102; the host server 103 can be a host device involved in the embodiment of the present application, and a master database is deployed on the host server 103. The slave server 102 and the host server 103 achieve master-slave data consistency through physical replication.
[0052] The terminal device 101 accesses the slave database on the slave server 102 to read the data required by the user. The terminal device 101 connected to the slave server 102 may include one or more. When multiple terminal devices 101 are included, the multiple terminal devices 101 may read the data from the slave database in parallel.
[0053] The slave server 102 and the host server 103 are both servers, and the server can be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides cloud computing services. The terminal device 101 can be a smart phone, a tablet computer, a laptop computer, a desktop computer, a smart speaker, a smart watch, etc., but is not limited to this. The terminal device 101 and the slave server 102 can be directly or indirectly connected via wired or wireless communication, and this application does not limit this.
[0054] When a user needs to read certain data, such as target data, the terminal device 101 may send a data read request to the slave server 102 . The data read request is used to request to read the target data and may include, for example, an identifier of the target data.
[0055] If the slave server 102 obtains a data read request during the playback of the physical log through the log playback thread, the slave server 102 can access the index page in sequence according to the data storage structure in the slave device (slave server 102) based on the data read request. Since the physical log may include an SMO log, that is, the slave server 102 may access a certain index page during the playback of the SMO log and the change of the B-tree structure. Therefore, in order to avoid inconsistent data read due to the playback of the SMO log, when accessing each index page, such as the target index page, the slave server 102 can determine whether the target index page is in the process of structure change operation according to the structure change operation mark added in the physical log. If it is determined that the target index page is in the process of structure change operation, it will sleep for a preset time.
[0056] Since the playback speed of the slave device (slave server 102) through the log playback thread is very fast, after a preset period of sleep, the playback of the structural change operation of the target index page has been completed. At this time, the steps of accessing the index pages in sequence according to the data storage structure in the slave device (slave server 102) based on the data read request are re-executed to read the target data.
[0057] Since each index page is accessed after the structural change operation is played back, the data storage structure no longer changes and is in a consistent state, thus avoiding system crashes caused by changes in the data storage structure and achieving consistent data reading.
[0058] Next, the data reading method provided in the embodiment of the present application will be described in detail with reference to the accompanying drawings.
[0059] See also Figure 2 , Figure 2 A flow chart of a data reading method is shown, which can be applied to a slave device in a master-slave architecture based on physical replication, and the method includes:
[0060] S201. During the process of the slave device replaying a physical log, a data read request for reading target data is obtained.
[0061] In a master-slave architecture based on physical replication, the master device can send physical logs to the slave device, and the slave device usually needs to replay the physical logs to achieve master-slave data consistency. In this way, when a user needs to use certain data, such as target data, the user can send a data read request to the slave device through the corresponding terminal device to obtain the target data from the slave device.
[0062] The physical log records the modification of the data stored on each index page. The playback order of the physical log corresponding to each index page during the playback process can be represented by the log sequence number (LSN).
[0063] S202 . According to the data read request, access index pages in sequence according to the data storage structure in the slave device.
[0064] The slave device may obtain a data read request during the process of playing back the physical log. At this time, the slave device can access the index pages in sequence according to the data storage structure to locate and read the target data.
[0065] The data storage structure may include multiple structures, such as B-tree, R-tree, etc. The embodiment of the present application does not limit the data storage structure, and the following will mainly introduce the data storage structure as B-tree.
[0066] The structure of B-tree can be found in Figure 3 As shown, B-tree includes parent nodes, intermediate nodes and leaf nodes. There is a connection relationship between the parent node, intermediate nodes and leaf nodes. The connection relationship can be achieved through Figure 3 The connecting lines with arrows in the figure represent the parent node, intermediate node and leaf node, which correspond to different index pages. Figure 3 The node marked with 1 represents index page 1, the node marked with 2 represents index page 2, the node marked with 3 represents index page 3, the node marked with 4 represents index page 4, the node marked with 5 represents index page 5, and the node marked with 6 represents index page 6. The first-level nodes (for example, the node marked with 1) are parent nodes, the second-level nodes (for example, the nodes marked with 2 and 3) represent intermediate nodes, and the third-level nodes (for example, the nodes marked with 4, 5, and 6) are leaf nodes.
[0067] based on Figure 3In the B-tree shown, the method of accessing the index pages in sequence according to the data storage structure in S202 can be to open the cursor of the B-tree, search from the parent node to the lower node according to the search condition, and end at the leaf node. The cursor moves forward and backward between the leaf nodes until a record that does not meet the search condition is encountered. The cursor is a mechanism for querying data based on the data storage structure. The opening of the cursor indicates that the B-tree is started to be scanned from the parent node. The path passed by the cursor scan can be called the cursor path, and the cursor path can be used to indicate the path for searching the target data.
[0068] S203: When the target index page is accessed, determine whether the target index page is in the process of a structure change operation according to the structure change operation mark added in the physical log.
[0069] During the physical log playback process, the physical log may include the SMO log, and the slave server may be playing back the SMO log. Since SMO refers to the structural change operation on the data storage structure, playing back the SMO log will change the data storage structure. Therefore, when each index page, such as the target index page, is accessed, in order to avoid accessing the index page to read data during the SMO process, resulting in inconsistent data read by concurrent user read threads or system crashes, in this embodiment, it can be determined whether the target index page is in the SMO process.
[0070] In this embodiment, the SMO log is a physical log with an SMO mark. If the physical log records a structural change operation, the host device adds a new logical log type (LOG_TYPE_SMO) at the beginning of the physical log for the SMO to mark the SMO, i.e., the SMO mark. The log content is (table id, index id), indicating which index table (table) and which index (index) the SMO has occurred on. Taking the data storage structure as B-tree as an example, B-tree is also a data indexing method. Therefore, a B-tree can be considered as an index. The database includes multiple tables, and a table includes multiple B-trees. The SMO mark can be used to determine which B-tree has SMO.
[0071] If the slave device plays back a physical log with an SMO mark during the physical log playback process, and the physical log with the SMO mark includes a log of modification operations on the target index page, then when the target index page is accessed, the target index page may be in the SMO process. Therefore, the slave device can determine whether the target index page is in the SMO process based on the SMO mark.
[0072] In an embodiment of the present application, a method for determining whether a target index page is in the SMO process is provided. One method is to determine a structure change operation log, i.e., an SMO log, from a physical log according to a structure change operation mark. The SMO log is a physical log with an SMO mark. If the SMO log does not include a log of a modification operation on the target index page, it means that the target index page is not in the SMO process and the target index page can be accessed; if the SMO log includes a log of a modification operation on the target index page, it is considered that the target index page is in the SMO process.
[0073] based on Figure 3 The B-tree shown in Figure 1. A schematic diagram of a complete SMO log can be found in Figure 4 As shown, the SMO log includes a physical log header, an SMO mark, a modified index page 6, a modified index page 7, a modified index page 3, and a physical log tail. The SMO mark indicates a newly added logical log (LOG_TYPE_SMO, table id, index id); the SMO log indicates a split operation, where index page 6 is split into index page 6 and index page 7.
[0074] If the target index page is index page 6, the SMO log can be determined through the SMO mark, and the SMO log includes the physical log of modifying index page 6. Therefore, it can be considered that the target index page is in the SMO process.
[0075] In some possible embodiments, although the SMO log includes the physical log corresponding to the target index page, since the SMO log may also include the physical logs corresponding to other index pages, the slave device will play back the SMO log according to Figure 4 The sequence from the physical log head to the physical log tail shown in the figure is played back, that is to say, the target index page may have ended the SMO process, or has not yet performed the SMO process. Therefore, in order to improve the accuracy of whether the target index page is in the SMO process, another way can be to determine the structure change operation log from the physical log according to the structure change operation mark, and the structure change operation log includes the target log and the structure change operation mark corresponding to the target index page. The slave device can determine whether the target index page is in the structure change operation process according to the size relationship between the first log sequence number corresponding to the target log, the second log sequence number corresponding to the structure change operation mark, and the third log sequence number, and the third log sequence number represents the progress position of the slave device replaying the physical log.
[0076] Since the physical log corresponding to each index page has a corresponding lsn in the SMO log, the structure change operation mark also has a corresponding log sequence number, represented by sync lsn. Through sync lsn, we can know when to start changing the structure of the data storage structure. During the playback process, the progress position of the slave device playing back the physical log can also be represented by the applied log sequence number (Applied lsn). Applied lsn will be updated to the physical log tail of the SMO log only after the entire SMO log is played back. When playing back the SMO log, the schematic diagram of the SMO log playback to the SMO mark can be seen in Figure 5 As shown, at this time, Applied LSN is 100, and the log sequence number sync LSN marked by the structure change operation is 110. Based on this, the slave device can determine whether the target index page is in the process of structure change operation according to the size relationship between the first log sequence number (i.e., LSN) corresponding to the target log, the second log sequence number (i.e., sync LSN) corresponding to the structure change operation mark, and the third log sequence number (i.e., Applied LSN).
[0077] Since the physical log corresponding to each index page in the SMO log is replayed before its lsn is updated to the physical log tail of the SMO log, replaying the physical log corresponding to the index page indicates that the index page is in the SMO process. If the first log sequence number is greater than the second log sequence number, it indicates that the physical log corresponding to the target index page is being replayed, and the first log sequence number is greater than the third log sequence number, it indicates that the applied lsn has not been updated to the physical log tail, and the replay of the entire SMO log has not been completed, thereby determining that the target index page is in the SMO process. Otherwise, it is determined that the target index page is not in the SMO process.
[0078] See also Figure 6 , Figure 6 The schematic diagram of the structure of B-tree in the SMO playback process is shown. Figure 4 In the SMO log shown, the dotted boxes represent the index pages affected by this SMO process, including index page 3, index page 6, and index page 7. Index page 6 and index page 7 represented by the gray boxes indicate that the corresponding SMO logs have been applied, and the lsn of the index pages has also been updated to the physical log tail of this SMO log, that is, the lsn of index page 6 and index page 7 is 160. If the target index page is index page 6, the second log sequence number sync lsn is 110, and the third log sequence number Applied lsn is 100. At this time, the first log sequence number 160 corresponding to the target index page is greater than the sync lsn, and the first log sequence number 160 is greater than the Applied lsn, indicating that the target index page is in the SMO process.
[0079] S204: If it is determined that the target index page is in the process of a structure change operation, perform a sleep operation for a preset time.
[0080] If the slave device determines that the target index page is in the process of structural change operation, it will suspend reading data and control the user reading thread to sleep for a preset time. Since the slave device replays the log through the log replay thread very quickly, the structural change operation playback of the target index page has been completed within the preset time.
[0081] It should be noted that for different data storage structures, after determining that the target index page is in the process of structural change operation, the processing operations performed by the slave device before hibernation may be different. This embodiment is mainly introduced by taking the data storage structure of B-tree as an example.
[0082] For B-tree, when accessing the target index page, the cursor used to scan the B-tree may be in different stages, including the process of opening the cursor and the process of moving the cursor. Among them, opening the cursor means that different processing can be performed according to the different stages of the cursor before sleeping for a preset time.
[0083] If the cursor is in the process of opening the cursor when accessing the target index page, at this time, the target index page is usually the index page corresponding to the parent node or the intermediate node. Then, before hibernation for a preset time, the target path to the target index page can be released (the target path can be a cursor path). The target path is the path from the parent node to the intermediate nodes corresponding to the target index page in sequence.
[0084] If the cursor is in the process of moving when accessing the target index page, at this time, the target index page is usually the index page corresponding to the leaf node. Then, before hibernation for a preset time, the location information of the target data in the target index page can be saved (the location information can be the cursor position) so that the target data can be relocated later according to the location information.
[0085] S205 , after the sleep mode ends, re-execute the step of sequentially accessing index pages according to the data reading request and the data storage structure in the slave device to read the target data.
[0086] After the preset time of dormancy, the playback of the structure change operation of the target index page has been completed, and the Applied LSN is updated to the end of the physical log, that is, the Applied LSN is 160, see Figure 7 The schematic diagram of the B-tree structure after the B-tree SMO log playback is completed can be seen in Figure 8As shown, at this time, the LSN of each index page is also updated to 160. At this time, if the target index page is index page 6, at this time, the target index page LSN does not meet the conditions of being greater than sync LSN and greater than Applied LSN, so the steps of accessing the index pages in sequence according to the data storage structure in the slave device according to the data read request in S202 can be re-executed to read the target data.
[0087] In some possible embodiments, if it is determined through S203 that the target index page is not in the process of a structural change operation, the target index page can be accessed. Since the target index page can be a different index page in the B-tree, for example, the target index page can be an index page that stores the target data, at this time, if the target index page is not in the process of a structural change operation, the target data can be directly read. Of course, if the target index page is an index page that does not store the target data in a parent node, an intermediate node, or a leaf node, at this time, if the target index page is not in the process of a structural change operation, the next index page of the target index page can continue to be accessed according to the data storage structure (such as B-tree) until the target data is read.
[0088] After the slave device reads the target data through the above method, it can return the target data to the terminal device corresponding to the user so that the terminal device can use it.
[0089] It can be seen from the above technical solution that in the master-slave architecture based on physical replication, the slave device can achieve data consistency between the master and slave devices by replaying the physical log for physical replication. In the process of the slave device replaying the physical log for physical replication, if the user needs to read the target data through the slave device, the slave device can obtain a data read request for reading the target data, and access the index page in sequence according to the data storage structure in the slave device according to the data read request. For each index page accessed, such as the target index page, the slave device determines whether the target index page is in the process of structural change operation according to the structural change operation mark added to the physical log. If it is determined that the target index page is in the process of structural change operation, it will sleep for a preset time. Since the speed of playback by the slave device through the log playback thread is very fast, after the preset time of sleep, the playback of the structural change operation of the target index page has been completed. At this time, the step of accessing the index page in sequence according to the data read request and the data storage structure in the slave device is re-executed to read the target data. By judging in real time whether each index page accessed, such as the target index page, is in the process of a structural change operation, it can be ensured that the target index page is accessed only after the structural change operation playback is completed. At this time, the data storage structure, such as the structure of the B-tree, no longer changes and is in a consistent state, thus avoiding system crashes due to changes in the data storage structure, achieving consistent data reading, and greatly improving the slave reading performance.
[0090] Compared with the method in the related art of caching multiple versions of index pages to ensure consistent reading, this method does not need to cache multiple versions of index pages through the buffer, reduces the storage pressure of the slave device, does not affect the elimination of storage layer index pages, and the system is more robust.
[0091] In addition, this method uses index pages as the granularity to determine whether it is in the SMO process, which has finer granularity and wider application scope. In terms of granularity, it only affects the index pages related to the SMO process, and other index pages are not affected; in terms of adaptability, it is applicable during the cursor opening process and the cursor moving process. And there is no locking operation on the index page, which will not block the data read request from entering, with low delay and high efficiency.
[0092] The data reading method provided in the embodiment of the present application realizes consistent reading, which is a key technology in the master-slave architecture of one write and multiple reads, and directly affects the consistency and performance of slave data reading.
[0093] Next, the data reading method provided by the embodiment of the present application will be introduced in conjunction with actual application scenarios. In this application scenario, B-tree is used as an index method for reading data. When the slave device receives a data reading request sent by the user through the terminal device, the slave device can open a cursor and start scanning the B-tree from the parent node, accessing the index page from top to bottom until the target data is obtained in the index page corresponding to the leaf node. In the process of scanning the B-tree, that is, in the process of accessing the index page according to the B-tree, it is judged whether the accessed index page is in the SMO process. Generally, scanning the B-tree through a cursor can include two processes, namely, opening a cursor, a process from the parent node to the intermediate node, and a cursor movement process. In these two processes, the accessed index page needs to be judged whether it is in the SMO process. Based on this, the embodiment of the present application provides a data reading method, see Fig. 9 , the method comprising:
[0094] S901, enter the process of opening the cursor.
[0095] S902: Determine whether the index page in the SMO process is accessed, if so, execute S906; if not, execute S903.
[0096] S903, enter the process of moving the cursor left and right.
[0097] S904: Determine whether the index page in the SMO process is accessed, if so, execute S907, if not, execute S905.
[0098] S905: Read target data.
[0099] S906: Release the cursor path.
[0100] S907: Save the cursor position.
[0101] S908, entering into sleep mode for a preset time, and re-executing S901 after the sleep mode ends.
[0102] based on Figure 2 Corresponding to the data reading method provided in the embodiment, the embodiment of the present application also provides a data reading device, see Fig.10 The device is deployed in a slave device in a master-slave architecture based on physical replication, and includes an acquisition unit 1001, an access unit 1002, a determination unit 1003, a sleep unit 1004, and a reading unit 1005:
[0103] The acquisition unit 1001 is used to acquire a data read request for reading target data during the process of the slave device playing back the physical log;
[0104] The access unit 1002 is used to access the index pages in sequence according to the data storage structure in the slave device according to the data read request;
[0105] The determining unit 1003 is used to determine whether the target index page is in a structure change operation process according to the structure change operation mark added to the physical log when accessing the target index page;
[0106] The dormancy unit 1004 is configured to perform dormancy for a preset time if it is determined that the target index page is in the process of a structure change operation;
[0107] The reading unit 1005 is used to re-execute the step of accessing index pages in sequence according to the data storage structure in the slave device according to the data read request after the sleep ends, so as to read the target data.
[0108] In a possible implementation manner, the determining unit 1003 is configured to:
[0109] Determine a structure change operation log from the physical log according to the structure change operation mark, the structure change operation log including a target log corresponding to the target index page and the structure change operation mark;
[0110] Based on the size relationship between the first log sequence number corresponding to the target log, the second log sequence number corresponding to the structure change operation mark, and the third log sequence number, it is determined whether the target index page is in the process of structure change operation, and the third log sequence number represents the progress position of the slave device in replaying the physical log.
[0111] In a possible implementation manner, the determining unit 1003 is configured to:
[0112] If the first log sequence number is greater than the second log sequence number, and the first log sequence number is greater than the third log sequence number, it is determined that the target index page is in the process of the structure change operation;
[0113] Otherwise, it is determined that the target index page is not in the process of the structure change operation.
[0114] In a possible implementation, the reading unit 1005 is further configured to:
[0115] If it is determined according to the structural change operation mark added to the physical log that the target index page is not in the process of structural change operation, the target data is read, or the next index page of the target index page is accessed continuously according to the data storage structure until the target data is read.
[0116] In a possible implementation, the data storage structure is a B-tree, which includes a parent node, an intermediate node and a leaf node. The parent node, the intermediate node and the leaf node are connected to each other, and the parent node, the intermediate node and the leaf node correspond to different index pages respectively.
[0117] In a possible implementation, the device further includes a releasing unit:
[0118] The release unit is used to release a target path accessing the target index page if it is determined that the target index page is in the process of a structure change operation, wherein the target path is a path from the parent node to the intermediate nodes corresponding to the target index page in sequence.
[0119] In a possible implementation manner, the device further includes a storage unit:
[0120] The saving unit is used to save the location information of the target data in the target index page if it is determined that the target index page is in the process of a structure change operation and if the target index page is the index page corresponding to the leaf node.
[0121] In a possible implementation, the device further includes a returning unit:
[0122] The returning unit is used to return the target data to the terminal device corresponding to the user.
[0123] The present application also provides a terminal device, which is described below in conjunction with the accompanying drawings. Fig.11 As shown in the figure, the terminal device is a smartphone as an example:
[0124] Fig.11 The block diagram shows a partial structure of a smart phone related to the terminal device provided in the embodiment of the present application. Fig.11 The smart phone includes: a radio frequency (RF) circuit 1110, a memory 1120, an input unit 1130, a display unit 1140, a sensor 1150, an audio circuit 1160, a wireless fidelity (WiFi) module 1170, a processor 1180, and a power supply 1190. Those skilled in the art will appreciate that Fig.11 The structure of the smartphone shown in the figure does not constitute a limitation of the smartphone, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.
[0125] The memory 1120 can be used to store software programs and modules. The processor 1180 executes various functional applications and data processing of the smartphone by running the software programs and modules stored in the memory 1120. The memory 1120 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area may store data created according to the use of the smartphone (such as audio data, a phone book, etc.), etc. In addition, the memory 1120 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.
[0126] The processor 1180 is the control center of the smartphone, which uses various interfaces and lines to connect various parts of the entire smartphone, and executes various functions of the smartphone and processes data by running or executing software programs and / or modules stored in the memory 1120, and calling data stored in the memory 1120, so as to monitor the smartphone as a whole. Optionally, the processor 1180 may include one or more processing units; preferably, the processor 1180 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface, and application programs, and the modem processor mainly processes wireless communications. It is understandable that the above-mentioned modem processor may not be integrated into the processor 1180.
[0127] In this embodiment, the steps implemented by the terminal device can be based on Fig.11 The structure of the terminal device shown is implemented.
[0128] The present application embodiment provides an electronic device for data reading, which is the slave device mentioned in the above embodiment, and is used to execute the data reading method provided in the present application embodiment. The electronic device can be a server, see Fig.12 As shown, Fig.12The structural diagram of the server 1200 provided in the embodiment of the present application, the server 1200 may have relatively large differences due to different configurations or performances, and may include one or more central processing units (CPUs) 1222 (for example, one or more processors) and a memory 1232, and one or more storage media 1230 (for example, one or more mass storage devices) storing application programs 1242 or data 1244. Among them, the memory 1232 and the storage medium 1230 can be short-term storage or permanent storage. The program stored in the storage medium 1230 may include one or more modules (not shown in the figure), and each module may include a series of instruction operations on the server. Furthermore, the central processing unit 1222 can be configured to communicate with the storage medium 1230 and execute a series of instruction operations in the storage medium 1230 on the server 1200.
[0129] The server 1200 may also include one or more power supplies 1226, one or more wired or wireless network interfaces 1250, one or more input and output interfaces 1258, and / or one or more operating systems 1241, such as Windows ServerTM, Mac OS XTM, UnixTM, LinuxTM, FreeBSDTM, etc.
[0130] In this embodiment, the central processor 1222 in the server 1200 may perform the following steps:
[0131] In the process of the slave device replaying the physical log, obtaining a data read request for reading target data;
[0132] According to the data read request, accessing index pages in sequence according to the data storage structure in the slave device;
[0133] When accessing a target index page, determining whether the target index page is in a structure change operation process according to the structure change operation mark added to the physical log;
[0134] If it is determined that the target index page is in the process of a structure change operation, hibernation for a preset time;
[0135] After the hibernation ends, the step of accessing index pages in sequence according to the data reading request and the data storage structure in the slave device is re-executed to read the target data.
[0136] According to one aspect of the present application, a computer-readable storage medium is provided, wherein the computer-readable storage medium is used to store program codes, and the program codes are used to execute the data reading methods described in the above-mentioned embodiments.
[0137] According to one aspect of the present application, a computer program product or a computer program is provided, the computer program product or the computer program includes computer instructions, the computer instructions are stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the method provided in various optional implementations of the above embodiments.
[0138] The terms "first", "second", "third", "fourth", etc. (if any) in the specification of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein, for example. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0139] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units 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 an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.
[0140] The units described as separate components may or may not be physically separated, and the components shown as units 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 units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0141] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0142] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory, referred to as ROM), random access memory (Random Access Memory, referred to as RAM), disk or optical disk and other media that can store program codes.
[0143] As described above, the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, ordinary technical members in the art should understand that they can still modify the technical solutions recorded in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A data reading method, characterized in that: The method is performed by a slave device in a master-slave architecture based on physical replication, and the method includes: In the process of the slave device replaying the physical log, obtaining a data read request for reading target data; According to the data read request, accessing index pages in sequence according to the data storage structure in the slave device; When accessing a target index page, determining whether the target index page is in a structure change operation process according to the structure change operation mark added to the physical log; If it is determined that the target index page is in the process of a structure change operation, hibernation for a preset time; After the hibernation ends, the step of accessing index pages in sequence according to the data reading request and the data storage structure in the slave device is re-executed to read the target data.
2. The method according to claim 1, characterized in that: The determining whether the target index page is in the process of a structure change operation according to the structure change operation mark added to the physical log includes: Determine a structure change operation log from the physical log according to the structure change operation mark, the structure change operation log including a target log corresponding to the target index page and the structure change operation mark; Based on the size relationship between the first log sequence number corresponding to the target log, the second log sequence number corresponding to the structure change operation mark, and the third log sequence number, it is determined whether the target index page is in the process of structure change operation, and the third log sequence number represents the progress position of the slave device in replaying the physical log.
3. The method according to claim 2, characterized in that Determining whether the target index page is in a structure change operation process according to a first log sequence number corresponding to the target log, a second log sequence number corresponding to the structure change operation mark, and a third log sequence number includes: If the first log sequence number is greater than the second log sequence number, and the first log sequence number is greater than the third log sequence number, it is determined that the target index page is in the process of the structure change operation; Otherwise, it is determined that the target index page is not in the process of the structure change operation.
4. The method according to claim 1, characterized in that: The method further comprises: If it is determined according to the structural change operation mark added to the physical log that the target index page is not in the process of structural change operation, the target data is read, or the next index page of the target index page is accessed continuously according to the data storage structure until the target data is read.
5. The method according to claim 1, characterized in that The data storage structure is a B-tree, which includes a parent node, an intermediate node and a leaf node. The parent node, the intermediate node and the leaf node are connected to each other, and the parent node, the intermediate node and the leaf node correspond to different index pages respectively.
6. The method according to claim 5, characterized in that If it is determined that the target index page is in the process of a structure change operation, before performing the preset time of dormancy, the method further includes: A target path accessing the target index page is released, where the target path is a path sequentially accessed from the parent node to the intermediate nodes corresponding to the target index page.
7. The method according to claim 5, characterized in that If it is determined that the target index page is in the process of a structure change operation, before performing the preset time of dormancy, the method further includes: If the target index page is the index page corresponding to the leaf node, the location information of the target data in the target index page is saved.
8. The method according to any one of claims 1 to 7, characterized in that: The method further comprises: The target data is returned to the terminal device corresponding to the user.
9. A data reading device, characterized in that: The apparatus is deployed in a slave device in a master-slave architecture based on physical replication, and includes an acquisition unit, an access unit, a determination unit, a sleep unit, and a reading unit: The acquisition unit is used to acquire a data read request for reading target data during the process of the slave device playing back the physical log; The access unit is used to access the index pages in sequence according to the data storage structure in the slave device according to the data read request; The determining unit is configured to determine whether the target index page is in a structure change operation process according to the structure change operation mark added to the physical log when the target index page is accessed; The dormancy unit is configured to perform dormancy for a preset time if it is determined that the target index page is in the process of a structure change operation; The reading unit is used to re-execute the step of accessing index pages in sequence according to the data storage structure in the slave device according to the data read request after the sleep ends, so as to read the target data.
10. The device according to claim 9, characterized in that The determining unit is used for: Determine a structure change operation log from the physical log according to the structure change operation mark, the structure change operation log including a target log corresponding to the target index page and the structure change operation mark; Based on the size relationship between the first log sequence number corresponding to the target log, the second log sequence number corresponding to the structure change operation mark, and the third log sequence number, it is determined whether the target index page is in the process of structure change operation, and the third log sequence number represents the progress position of the slave device in replaying the physical log.
11. The device according to claim 10, characterized in that The determining unit is used for: If the first log sequence number is greater than the second log sequence number, and the first log sequence number is greater than the third log sequence number, it is determined that the target index page is in the process of the structure change operation; Otherwise, it is determined that the target index page is not in the process of the structure change operation.
12. The device according to claim 9, characterized in that The reading unit is also used for: If it is determined according to the structural change operation mark added to the physical log that the target index page is not in the process of structural change operation, the target data is read, or the next index page of the target index page is accessed continuously according to the data storage structure until the target data is read.
13. The device according to claim 9, characterized in that The data storage structure is a B-tree, which includes a parent node, an intermediate node and a leaf node. The parent node, the intermediate node and the leaf node are connected to each other, and the parent node, the intermediate node and the leaf node correspond to different index pages respectively.
14. An electronic device for reading data, characterized in that: The electronic device comprises a processor and a memory: The memory is used to store program code and transmit the program code to the processor; The processor is configured to execute the method according to any one of claims 1 to 8 according to the instructions in the program code.
15. A computer-readable storage medium, characterized in that: The computer-readable storage medium is used to store program codes, and the program codes are used to execute the method according to any one of claims 1 to 8.
