Data writing method and apparatus, computer device, and storage medium
By allowing any thread to expand the data page in the database when no available memory page is obtained, the problem of untimely data table expansion when multi-threaded concurrent writes is solved, and the writing performance and efficiency are improved.
Patent Information
- Application Number
- PCT/CN2024/081747
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2023-06-16
- Filing Date
- 2024-03-14
- Publication Date
- 2024-12-19
AI Technical Summary
In the database, when multiple threads write data concurrently, the data table may not be expanded in time, resulting in poor writing performance, especially when the last free memory page is used up and other threads need to wait for one thread to expand, resulting in The waiting time is longer.
Provides a data writing method, allowing any thread to expand N free data pages by itself when it has not obtained the available memory page, and writes data in the extended free memory page, avoiding waiting for other threads to expand the data page, thereby improving Data writing performance.
By expanding the data pages by itself, the thread blocking time is shortened, data writing performance is improved, and expansion can be triggered in advance when there are a small number of free memory pages in the data table, improving the data table expansion efficiency.
Smart Images

Figure CN2024081747_19122024_PF_FP_ABST
Abstract
Description
Data writing method, device, computer equipment and storage medium
[0001] This application claims priority to the Chinese patent application filed with the State Intellectual Property Office on June 16, 2023, with application number 202310723602.9 and application name “Data Writing Method, Device, Computer Equipment and Storage Medium”, all contents of which are incorporated by reference into this application. Technical Field
[0002] The present application relates to the field of databases, and in particular to a data writing method, apparatus, computer equipment, and storage medium. Background Art
[0003] Currently, databases typically store files in the form of data tables. A data table consists of multiple data pages, which are persistently stored on disk. Each data page corresponds to a memory page. When a computer updates a data page, it loads the data page into the corresponding memory page for the update.
[0004] In related art, a binary page tree can be used to manage the free memory pages corresponding to free data pages in a data table. When multiple threads in a computer device want to write data to the same data table, they can use the binary page tree to search for available memory pages with sufficient space based on the amount of data to be written. This way, multiple threads may find the same available memory page. In this case, the multiple threads can queue up and sequentially apply a write lock to the available memory page, and write data to the available memory page after successfully applying the write lock. Based on this, if the available memory page is the last free memory page corresponding to the data table, and after a thread finishes writing data, the remaining capacity of the available memory page is insufficient for subsequent threads to write data, the subsequent threads will simultaneously request to expand the data table. In this case, one thread will simultaneously expand multiple free data pages for the multiple threads requesting data table expansion, based on the number of threads requesting data table expansion, while the other threads will wait, resulting in delayed data table expansion.
[0005] Summary of the Invention
[0006] The present application provides a data writing method, apparatus, computer device, and storage medium for improving data writing performance when multiple threads concurrently write data to a data table.
[0007] To achieve the above objectives, this application adopts the following technical solutions:
[0008] In a first aspect, a data writing method is provided, which can be applied to a first thread in a computer device, where the first thread is any one of multiple threads running in the computer device to write data, and the method includes: if no available memory page is obtained from multiple free memory pages corresponding to a target data table, then N free data pages are expanded for the first thread in the target data table, where N is a preset integer greater than 0; free memory pages are expanded for the target data table based on the N free data pages; and data is written in the expanded free memory pages.
[0009] In the present application, when any thread in the computer device fails to obtain an available memory page from the multiple free memory pages corresponding to the target data table, it can expand N free data pages for itself, and expand the free memory pages based on the N free data pages, and then write data in the expanded free memory pages. It can be seen that any thread that fails to obtain an available memory page can trigger the data table expansion process by itself to promptly expand N data pages for itself for its own use. Moreover, since there is no need to expand data pages for other threads, the time required for each thread to expand the data page is shorter. Compared with the solution in which one thread centrally expands data pages for all threads that have not obtained available memory pages, while other threads are waiting, the thread blocking time can be shortened, thereby improving data writing performance.
[0010] Optionally, the implementation process of extending N free data pages for the first thread in the target data table may include: adding a write lock to the target data table; if adding a write lock to the target data table is successful, applying for N extended storage areas for the target data table in the persistent storage device; writing 0 in the N extended storage areas to obtain the N free data pages; and releasing the write lock of the target data table.
[0011] In the present application, when the first thread expands the free data page, it first locks the target data table to prevent other threads from accessing the target data table during the expansion process and causing a read-write conflict. In addition, the process of the first thread expanding the data table includes two sub-processes: expanding the free data page in the persistent storage device and allocating memory pages for the free data page and updating the page binary tree. Based on this, when the first thread expands the free data page in the persistent storage device, it adds a write lock to the target data table, but does not add a write lock to the page binary tree corresponding to the target data table. In this way, in the process of the first thread expanding the free data page, other threads that have completed the data page expansion can update the page binary tree. In this way, different threads can execute different sub-processes in the data table expansion process in a time-sharing manner, thereby improving the expansion efficiency of the data table.
[0012] Optionally, the implementation process of expanding the free memory pages for the target data table based on the N free data pages may include: allocating corresponding memory pages to the N free data pages; based on the memory pages corresponding to the N free data pages, updating the page binary tree corresponding to the target data table, the page binary tree corresponding to the target data table includes multiple page nodes, and each page node is used to indicate a free memory page corresponding to the target data table.
[0013] Optionally, the implementation process of updating the page binary tree corresponding to the target data table based on the memory pages corresponding to the N free data pages may include: adding a write lock to the page binary tree corresponding to the target data table; if adding a write lock to the page binary tree corresponding to the target data table is successful, adding page nodes corresponding to the memory pages corresponding to the N free data pages in the page binary tree corresponding to the target data table; and releasing the write lock of the page binary tree corresponding to the target data table.
[0014] In the present application, the first thread first adds a write lock to the page binary tree before updating the page binary tree to prevent multiple threads from updating the page binary tree of the target data table at the same time and causing conflicts. In addition, the process of the first thread expanding the data table includes two sub-processes: expanding the data page in the persistent storage device and allocating memory pages for the expanded data page and updating the page binary tree. Based on this, when the first thread updates the page binary tree, it adds a write lock to the page binary tree, but does not add a write lock to the target data table. In this way, while the first thread is updating the page binary tree, other threads that trigger the data table expansion process can expand the data page. In this way, different threads can execute different sub-processes in the table expansion process in a time-sharing manner, thereby improving the expansion efficiency of the data table.
[0015] Optionally, the method further includes: searching for an available memory page in a plurality of free memory pages corresponding to the target data table; if adding a write lock to the currently found available memory page fails, counting the cumulative number of available memory pages that failed to add a write lock; if the cumulative number of available memory pages that failed to add a write lock is greater than a reference threshold, determining that no available memory page is obtained from the plurality of free memory pages corresponding to the target data table.
[0016] In this application, the first thread does not need to wait until it determines that the last free memory page has been used up before expanding the data table. Instead, it can directly trigger the data table expansion process when it fails to add write locks to the found available memory pages multiple times and the number of failures exceeds a threshold. In other words, the first thread can trigger data table expansion in advance when there are still a few free memory pages in the data table. This can alleviate the problem of long waiting times caused by one thread performing expansion after the last free memory page is used up, while other threads are concentrated waiting.
[0017] In a second aspect, a data writing method is provided. The method can be applied to a first thread of a computer device, where the first thread is any one of multiple threads running in the computer device and to which data is to be written. The first thread can search for an available memory page among multiple free memory pages corresponding to a target data table; if a write lock fails to be applied to the currently found available memory page, the first thread can search for another available memory page among the multiple free memory pages; and then write data to the other available memory page for which the write lock was successfully applied.
[0018] The first thread is a transaction thread of a database application running on a computer device, and the transaction thread is used to write data in a target data table. Based on this, the first thread can search for an available memory page in multiple free memory pages corresponding to the target data table. If a write lock fails for the currently found available memory page, it means that other threads are accessing the available memory page. In this case, the first thread does not wait for other threads to release the lock on the available memory page, but directly searches for other available memory pages in multiple free memory pages and writes data to the other available memory pages that have been successfully locked. This can shorten the waiting time of the first thread, thereby improving data writing performance.
[0019] Optionally, when the first thread searches for other available memory pages among the multiple free memory pages, it can start searching for the target page node from the next page node of the first page node in the page binary tree corresponding to the target data table, the page binary tree corresponding to the target data table includes multiple page nodes, each page node is used to indicate a free memory page corresponding to the target data table, the first page node is used to indicate the available memory page that currently fails to be locked, and the remaining capacity of the free memory page indicated by the target page node is not less than the amount of data to be written; the free memory page indicated by the target page node is used as the other available memory page.
[0020] That is to say, in the present application, after the first thread fails to add a write lock to the currently found available memory page, it can continue to search for other available memory pages starting from the next node of the page node corresponding to the available memory page on which the current write lock failed to be added, instead of re-searching from the root node of the page binary tree. In this way, the search time can be shortened.
[0021] Optionally, if adding a write lock to the currently found available memory page fails, the first thread can first count the cumulative number of available memory pages that failed to add a write lock; if the cumulative number of available memory pages that failed to add a write lock is not greater than a reference threshold, then execute the step of searching for other available memory pages from the multiple free memory pages.
[0022] If the cumulative number of available memory pages that failed to add write locks is not greater than the reference threshold, it means that the first thread has failed to add write locks a small number of times, and there may be other available memory pages with sufficient space for the first thread to write data in the multiple free memory pages corresponding to the target data table. In this case, the first thread can continue to search for other available memory pages in the multiple free memory pages.
[0023] Optionally, if adding a write lock to the currently found available memory page fails, the first thread may first count the cumulative number of available memory pages on which adding a write lock fails; if the cumulative number of available memory pages on which adding a write lock fails is greater than the reference threshold, N free data pages are expanded for the first thread in the target data table, where N is a preset integer greater than 0; based on the N free data pages, the free memory pages are expanded for the target data table, and data is written in the expanded free memory pages.
[0024] In the present application, the first thread does not need to wait until it is determined that the last free memory page is used up before expanding the data table. Instead, when the write lock fails to be added to the found available memory page multiple times and the number of failures exceeds the threshold, the data table expansion process can be directly triggered. That is, the first thread can trigger the data table expansion in advance when there are still a small number of free memory pages in the data table. In this way, the problem of a long waiting time caused by one thread performing the expansion after the last free memory page is used up and other threads waiting together can be improved. Moreover, since each thread can directly trigger the data table expansion process under the above conditions, the efficiency of data table expansion can be improved, thereby increasing the data writing speed.
[0025] Optionally, the implementation process of expanding the free memory pages for the target data table based on the N free data pages may include: allocating corresponding memory pages to the N free data pages, and updating a page binary tree corresponding to the target data table based on the memory pages corresponding to the N free data pages, so that each thread can use the updated page binary tree to search for the expanded free memory pages and write data. The page binary tree includes multiple page nodes, each page node being used to indicate a free memory page corresponding to the target data table.
[0026] Optionally, when the first thread expands N free data pages for itself in the target data table, it can first add a write lock to the target data table. If adding the write lock to the target data table is successful, N extended storage areas are applied for the target data table in the persistent storage device; 0 is written in the N extended storage areas to obtain the N free data pages; thereafter, the write lock of the target data table is released.
[0027] In the present application, when the first thread expands the data page, it first adds a write lock to the target data table to prevent other threads from accessing the target data table during the expansion process and causing a read-write conflict. In addition, the process of the first thread expanding the data table includes two sub-processes: expanding the data page in the persistent storage device and allocating memory pages for the expanded data page and updating the page binary tree. Based on this, when the first thread expands the data page in the persistent storage device, it adds a write lock to the target data table, but does not add a write lock to the page binary tree corresponding to the target data table. In this way, during the process of the first thread expanding the data page, other threads that have completed the data page expansion can update the page binary tree. In this way, different threads can execute different sub-processes in the table expansion process in a time-sharing manner, thereby improving the expansion efficiency of the data table.
[0028] Optionally, the implementation process of the first thread updating the page binary tree corresponding to the target data table based on the memory pages corresponding to the N free data pages may include: adding a write lock to the page binary tree corresponding to the target data table; if adding a write lock to the page binary tree corresponding to the target data table is successful, then adding page nodes corresponding to the memory pages corresponding to the N free data pages in the page binary tree corresponding to the target data table; and releasing the write lock of the page binary tree corresponding to the target data table.
[0029] In the present application, the first thread first adds a write lock to the page binary tree before updating the page binary tree to prevent multiple threads from updating the page binary tree of the target data table at the same time and causing conflicts. In addition, the process of the first thread expanding the data table includes two sub-processes: expanding the data page in the persistent storage device and allocating memory pages for the expanded data page and updating the page binary tree. Based on this, when the first thread updates the page binary tree, it adds a write lock to the page binary tree, but does not add a write lock to the target data table. In this way, while the first thread is updating the page binary tree, other threads that trigger the data table expansion process can expand the data page. In this way, different threads can execute different sub-processes in the table expansion process in a time-sharing manner, thereby improving the expansion efficiency of the data table.
[0030] In a third aspect, a data writing device is provided, comprising at least one module configured to execute the data writing method described in the first aspect via a first thread in a computer device. Exemplarily, the at least one module may include a first expansion module, a second expansion module, and a writing module.
[0031] Among them, the first expansion module is used to expand N free data pages for the first thread in the target data table through the first thread if no available memory page is obtained from the multiple free memory pages corresponding to the target data table, wherein N is a preset integer greater than 0, and the first thread is any one of the multiple threads to be written to data running in the computer device; the second expansion module is used to expand the free memory pages for the target data table based on the N free data pages through the first thread; the writing module is used to write data in the expanded free memory pages through the first thread.
[0032] Optionally, the first expansion module is specifically used to: add a write lock to the target data table; if the write lock for the target data table is successfully added, apply for N extended storage areas for the target data table in the persistent storage device; write 0 in the N extended storage areas to obtain the N free data pages; and release the write lock of the target data table.
[0033] Optionally, the second expansion module is specifically used to: allocate corresponding memory pages to the N free data pages; based on the memory pages corresponding to the N free data pages, update the page binary tree corresponding to the target data table, the page binary tree corresponding to the target data table includes multiple page nodes, each page node is used to indicate a free memory page corresponding to the target data table.
[0034] Optionally, the second expansion module is specifically used to: add a write lock to the page binary tree corresponding to the target data table; if the write lock is successfully added to the page binary tree corresponding to the target data table, then add the page nodes corresponding to the memory pages corresponding to the N free data pages in the page binary tree corresponding to the target data table; and release the write lock of the page binary tree corresponding to the target data table.
[0035] Optionally, the device also includes: a search module, used to search for available memory pages in multiple free memory pages corresponding to the target data table through the first thread; a statistics module, used to count the cumulative number of available memory pages that failed to add a write lock to the currently found available memory page through the first thread if the write lock fails; and a determination module, used to determine that the first thread has not obtained an available memory page in the multiple free memory pages corresponding to the target data table if the cumulative number of available memory pages that failed to add a write lock is greater than a reference threshold.
[0036] In a fourth aspect, a data writing device is provided, comprising at least one module configured to execute the data writing method described in the second aspect through a first thread in a computer device. Exemplarily, the at least one module may include a search module and a write module.
[0037] Among them, the search module is used to search for available memory pages in multiple free memory pages corresponding to the target data table through the first thread; if the write lock fails to be added to the currently found available memory page, then search for other available memory pages in the multiple free memory pages, and the first thread is any one of the multiple threads to be written to the data running in the computer device; the writing module is used to write data in other available memory pages to which the write lock is successfully added through the first thread.
[0038] Optionally, the search module is specifically used to: search for the target page node starting from the next page node of the first page node in the page binary tree corresponding to the target data table, the page binary tree corresponding to the target data table includes multiple page nodes, each page node is used to indicate a free memory page corresponding to the target data table, the first page node is used to indicate the available memory page that currently fails to be locked, and the remaining capacity of the free memory page indicated by the target page node is not less than the amount of data to be written; the free memory page indicated by the target page node is used as another available memory page.
[0039] Optionally, the search module is specifically used to: if adding a write lock to the currently found available memory page fails, then counting the cumulative number of available memory pages that failed to add a write lock; if the cumulative number of available memory pages that failed to add a write lock is not greater than a reference threshold, then searching for other available memory pages from the multiple free memory pages.
[0040] Optionally, the device also includes: a first expansion module, used to expand N free data pages for the first thread in the target data table through the first thread if the cumulative number of available memory pages that failed to add write locks is greater than the reference threshold; a second expansion module, used to allocate corresponding memory pages for the N free data pages through the first thread; based on the memory pages corresponding to the N free data pages, updating the page binary tree corresponding to the target data table, the page binary tree corresponding to the target data table includes multiple page nodes, each page node is used to indicate a free memory page corresponding to the target data table.
[0041] Optionally, the first expansion module is specifically used to: add a write lock to the target data table; if the write lock for the target data table is successfully added, apply for N extended storage areas for the target data table in the persistent storage device; write 0 in the N extended storage areas to obtain the N free data pages; and release the write lock of the target data table.
[0042] Optionally, the second expansion module is specifically used to: add a write lock to the page binary tree corresponding to the target data table; if the write lock is successfully added to the page binary tree corresponding to the target data table, then add the page nodes corresponding to the memory pages corresponding to the N free data pages in the page binary tree corresponding to the target data table; and release the write lock of the page binary tree corresponding to the target data table.
[0043] In a fifth aspect, a computer device is provided, comprising a processor and a memory, wherein the processor is configured to execute at least one program instruction or code stored in the memory to implement the data writing method described in the first or second aspect. Optionally, the memory is a memory, and the computer device may further include a persistent storage device, which may be a hard disk.
[0044] In a sixth aspect, a computer-readable storage medium is provided, wherein instructions are stored in the computer-readable storage medium. When the instructions are executed on a computer device, the computer device executes the data writing method described in the first or second aspect above.
[0045] In a seventh aspect, a computer program product comprising instructions is provided. When the computer program product is run on a computer device, the computer device executes the data writing method described in the first or second aspect above.
[0046] The technical effects obtained in the above-mentioned third to seventh aspects are similar to the technical effects obtained by the corresponding technical means in the first or second aspect, and will not be repeated here.
[0047] In an embodiment of the present application, when any thread in the computer device fails to obtain an available memory page from the multiple free memory pages corresponding to the target data table, it can expand N free data pages for itself, and expand the free memory pages based on the N free data pages, and then write data in the expanded free memory pages. It can be seen that any thread that fails to obtain an available memory page can trigger the data table expansion process by itself to promptly expand N data pages for its own use. Moreover, since there is no need to expand data pages for other threads, the time required for each thread to expand the data page is shorter. Compared with the solution in which one thread centrally expands data pages for all threads that have not obtained available memory pages, while other threads are waiting, the thread blocking time can be shortened, thereby improving data writing performance. BRIEF DESCRIPTION OF THE DRAWINGS
[0048] FIG1 is a diagram illustrating an architecture of a storage system according to an embodiment of the present invention;
[0049] FIG2 is a diagram of an operational architecture of a database application provided in an embodiment of the present application;
[0050] FIG3 is a diagram of another storage system architecture involved in the data writing method provided in an embodiment of the present application;
[0051] FIG4 is a flow chart of a data writing method provided in an embodiment of the present application;
[0052] FIG5 is a schematic diagram of a lock state variable corresponding to a memory page provided in an embodiment of the present application;
[0053] FIG6 is a schematic diagram of another lock state variable corresponding to a memory page provided in an embodiment of the present application;
[0054] FIG7 is a schematic diagram of a multi-thread extended data table provided in an embodiment of the present application;
[0055] FIG8 is a flowchart of another data writing method provided in an embodiment of the present application;
[0056] FIG9 is a schematic diagram of a page binary tree provided in an embodiment of the present application;
[0057] FIG10 is a flowchart of another data writing method provided in an embodiment of the present application;
[0058] FIG11 is a data writing performance comparison curve provided by an embodiment of the present application;
[0059] FIG12 is a schematic structural diagram of a data writing device provided in an embodiment of the present application;
[0060] FIG13 is a schematic structural diagram of another data writing device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0061] In order to make the objectives, technical solutions and advantages of the embodiments of the present application clearer, the implementation methods of the present application will be further described in detail below with reference to the accompanying drawings.
[0062] Before explaining the embodiments of the present application in detail, the application scenarios involved in the embodiments of the present application are first introduced.
[0063] In a database, files are usually stored in the form of data tables. A data table includes multiple data pages, which are persistently stored in a persistent storage device, such as a disk of a computer device. The size of each data page can be equal, for example, 8KB, that is, each data page can include 8KB of data. In addition, each data page can correspond to a memory page in the computer device, wherein a memory page can be understood as a storage area in the memory of the computer device. When a computer device accesses a data page, the computer device can load the data page from the persistent storage device to the corresponding memory page. For example, when the computer device wants to write data to a data page in a data table, it can load the data page into the corresponding memory page and write the data in the memory page. After the data writing is completed, the computer device can store the data in the memory page into the persistent storage device.
[0064] Currently, computer devices typically access databases through transactions. Furthermore, the free memory pages corresponding to data pages with free space (i.e., data pages that are not fully written) within a data table in the database can be managed using a binary page tree. Therefore, when a transaction thread in a computer device wants to write data to a data table, it uses the binary page tree to search for an available memory page with sufficient space. After finding an available memory page, the transaction thread applies a write lock to the available memory page. If the write lock is successfully acquired, the data is written to the available memory page. If the write lock acquisition fails, it indicates that another transaction thread has already locked the available memory page. In this case, the transaction thread waits for the other transaction thread to release the lock on the available memory page. After the other transaction thread releases the lock on the available memory page, the transaction thread determines whether there is sufficient space on the available memory page to write the data. If so, the transaction thread locks the available memory page again and, if the lock is successfully acquired, writes the data to the available memory page.
[0065] In some scenarios, multiple transaction threads running in a computer device may write data to the same data table at the same time. In the embodiments of the present application, "at the same time" can refer to the same time or a very short time interval. In this scenario, when multiple transaction threads search for available memory pages through the page binary tree, there is a high probability that they will find the same available memory page. In this case, after a transaction thread among the multiple transaction threads successfully locks the available memory page, the other transaction threads must wait until the transaction thread completes writing the data before they can continue to write data to the available memory page. In other words, the multiple transaction threads must write data to the available memory page in sequence in a serial manner, which is inefficient. Moreover, during the serial writing process, it is possible that after a transaction thread writes data, the free space of the available memory page is insufficient. In this way, other transaction threads waiting to write data will no longer be able to write data to the available memory page. In this case, the other transaction threads waiting to write data need to re-search for available memory pages starting from the root node of the page binary tree, which will further increase the write time of these transaction threads.
[0066] Furthermore, if the available memory page is the last available memory page with sufficient space among the free memory pages managed by the binary page tree, then subsequent transaction threads waiting to write data will find no available memory pages after re-searching for available memory pages starting from the root node of the binary page tree. In this case, these transaction threads will simultaneously request to expand the data table, thereby adding free data pages to the data table. When a transaction thread successfully applies to expand the data table, the other transaction threads all enter a waiting state. This transaction thread determines the number of pages to be expanded based on the number of transaction threads currently applying to expand the data table, adds a corresponding number of data pages to the data table, and allocates corresponding memory pages for each data page. This allows subsequent transaction threads applying to expand the data table to find the free memory pages corresponding to the expanded free data pages through the binary page tree and write data there. This shows that when multiple transaction threads simultaneously request to expand the data table, one transaction thread actually expands the required data pages for all of them at once, while the other transaction threads wait, delaying the timely expansion of the data table. In this case, the more transaction threads that apply for data table expansion at the same time, the more data pages that need to be expanded at one time. In this way, the waiting time of other transaction threads is also longer, resulting in a longer thread blocking time.
[0067] Based on the above problems, an embodiment of the present application provides a data writing method, which can be applied to scenarios where multiple transaction threads concurrently write a large amount of data in the same data table, so as to improve the data writing speed in this scenario and improve the problem of untimely data table expansion, thereby improving the single-table writing performance.
[0068] Next, the system architecture involved in the data writing method provided in the embodiment of the present application is introduced.
[0069] FIG1 is an architectural diagram of a storage system involved in a data writing method provided in an embodiment of the present application. As shown in FIG1 , the storage system includes one or more servers 10 (three servers 10 are shown in FIG1 , but are not limited to three servers 10), and each server 10 can communicate with each other. The server 10 is a device that has both computing power and storage capacity, such as a server, a desktop computer, etc. At the hardware level, as shown in FIG1 , the server 10 includes at least a processor 101, a memory 102, a network card 103, and a hard disk 104. The processor 101, the memory 102, the network card 103, and the hard disk 104 are connected by a bus. At the software level, an application program, such as a database application, can be deployed in the server 10 to process read and write operations of data pages in the database. It should be noted that the above-mentioned application program can be stored in the memory 102 of the server 10 and executed by the processor 101 to implement the corresponding function.
[0070] Specifically, the processor 101 is a central processing unit (CPU) that is used to process data access requests from outside the server 10 (application server or other server 10), and is also used to process requests generated inside the server 10. For example, when the processor 101 receives a write data request, it temporarily stores the data in these write data requests in the memory 102. When the total amount of data in the memory 102 reaches a certain threshold, the processor 101 sends the data stored in the memory 102 to the hard disk 104 for persistent storage. In addition, the processor 101 is also used for data calculation or processing, such as metadata management, deduplication, data compression, data verification, virtualized storage space, and address conversion. Figure 1 only shows one CPU 101. In actual applications, there are often multiple CPUs 101, and one CPU 101 has one or more CPU cores. The embodiments of the present application do not limit the number of CPUs and the number of CPU cores.
[0071] Memory 102 refers to internal memory that directly exchanges data with the processor. The storage space of memory 102 can be divided into multiple memory pages. It can read and write data at any time and at a high speed, serving as temporary data storage for the operating system or other running programs. Memory includes at least two types of memory. For example, memory can be either random access memory (RAM) or read-only memory (ROM). For example, random access memory is dynamic random access memory (DRAM) or storage class memory (SCM). DRAM is a type of semiconductor memory that, like most random access memory (RAM), is a volatile memory device. SCM is a hybrid storage technology that combines the characteristics of traditional storage devices and memory. Storage class memory can provide faster read and write speeds than hard drives, but has slower access speeds than DRAM and is also cheaper than DRAM. However, DRAM and SCM are merely exemplary in this embodiment. Memory can also include other random access memories, such as static random access memory (SRAM). As for the read-only memory, for example, it can be a programmable read only memory (PROM), an erasable programmable read only memory (EPROM), etc. In addition, the memory 102 can also be a dual in-line memory module or a dual-line memory module (DIMM), that is, a module composed of DRAM, or a solid state disk (SSD). In actual applications, multiple memories 102 and different types of memories 102 can be configured in the server 10. This embodiment does not limit the number and type of memory 102. In addition, the memory 102 can be configured to have a power-saving function. The power-saving function means that when the system loses power and then powers on again, the data stored in the memory 102 will not be lost. A memory with a power-saving function is called a non-volatile memory.
[0072] Hard disk 104 is used to provide storage resources, for example, storing database data in the form of data pages, where each data page can be mapped to a memory page in memory 102. Hard disk 104 can be a magnetic disk or other type of storage medium, such as a solid-state drive or a shingled magnetic recording hard disk. Network card 103 is used to communicate with other application servers 10.
[0073] Exemplarily, FIG2 is a schematic diagram of a database application running on a server 10 provided by an embodiment of the present application. As shown in FIG2 , a database main process may be run in the processor, and the database main process includes a main thread group, a data disk thread group, and a redo log (redolog) thread group. Among them, when a database update request or a data insert request is received, the transaction thread in the main thread group can load the data page stored in the hard disk into the memory page of the memory, and then write the data to be written to the memory page. The redo log thread group includes a write log (write log) thread and a redo log thread. The write log thread can generate a transaction log that records the change information of the data in the memory page, and the redo log thread can restore the database by reading the transaction log of the persistent storage. The data disk thread group includes a checkpoint (check point) thread and a write page (page write) thread. Among them, the checkpoint thread can trigger the write page thread to write the data of the memory page to the hard disk for persistent storage.
[0074] The above is a system architecture of an exemplary storage system applied by the data writing method in the embodiment of the present application. In addition, the data writing method can also be applied to other types of storage systems, for example, a storage system with separated storage and computing. The so-called separation of storage and computing refers to the separation of computing functions and storage functions, and their deployment on different devices. Among them, the device mainly used to implement the computing function is called a computing node, and the device mainly providing the data storage function is called a storage node. For example, Figure 3 is an architectural diagram of a storage system with separated storage and computing, and the data writing method provided in the embodiment of the present application can also be applied to the storage system. As shown in Figure 3, the storage system may include multiple computing nodes 30 and multiple storage nodes 31. Among them, each computing node 30 can communicate with each other through the network, and each computing node 30 can access any storage node 31 through the network.
[0075] Computing node 30 is a computing device, such as a server or desktop computer. At the hardware level, as shown in Figure 3, computing node 30 includes at least a processor 301, memory 302, and a network interface card 303. At the software level, computing node 30 may be deployed with applications. For example, a database application client may be deployed on computing node 30 to handle read and write operations on data pages in the database. It should be noted that these applications may be stored in memory 302 of computing node 30 and executed by processor 301 to implement the corresponding functions.
[0076] Specifically, processor 301 is a central processing unit (CPU) that processes data access requests from outside of computing node 30 or requests generated within computing node 30. For example, when processor 301 receives write data requests from users, it temporarily stores the data in these write data requests in memory 302. When the total amount of data in memory 302 reaches a certain threshold, processor 301 sends the data stored in memory 302 to storage node 31 for persistent storage. For example, taking processor 301 running a database application as an example, processor 301 can run a main thread, a data write thread, and a logging thread. When a database update request or data insert request is received, the main thread can load a data page stored in storage node 31 into a memory page of memory 302, and then write the data to be written to the memory page. The logging thread can record changes to the data in the memory page. When the total amount of data in the memory page reaches a certain threshold, the data write thread can send the data in the memory page to storage node 31 for persistent storage.
[0077] In addition, processor 301 is used for data calculation or processing, such as metadata management, data deduplication, data compression, virtualized storage space, and address translation. FIG3 shows only one CPU 301. In actual applications, there are often multiple CPUs 301, each of which has one or more CPU cores. This embodiment does not limit the number of CPUs or CPU cores.
[0078] Memory 302 refers to internal storage that directly exchanges data with processor 301. The storage space of memory 302 can be divided into multiple memory pages. It can read and write data at any time and at a high speed, serving as temporary data storage for the operating system or other running programs. The detailed implementation of memory 302 can be found in the description of the memory in the server shown in FIG1 , and will not be further described in detail in this embodiment of the present application.
[0079] The network card 303 is used to communicate with the storage node 31. For example, when the total amount of data in the memory 302 reaches a certain threshold, the computing node 30 can send a request to the storage node 31 through the network card 303 to persistently store the data. In addition, the computing node 30 may also include a bus for communication between the components inside the computing node 30. Functionally, since the main function of the computing node 30 in Figure 3 is computing business, remote storage can be used to achieve persistent storage when storing data, so it has less local storage than a conventional server, thereby saving cost and space. However, this does not mean that the computing node 30 cannot have local storage. In actual implementation, the computing node 30 can also have a small number of hard disks built in, or a small number of external hard disks.
[0080] Any computing node 30 can access any storage node 31 via the network. The storage system in the embodiment of the present application includes multiple storage nodes 31 (three storage nodes 31 are shown in Figure 3, but are not limited to three storage nodes 31). The storage node 31 may include one or more controllers 311, a network card 312, and multiple hard disks 313. The network card 312 is used to communicate with the computing node 30. The hard disk 313 is used to store data, for example, to store data in a database in the form of data pages, where each data page can be mapped to a memory page in the memory 102. The hard disk 313 can be a disk or other type of storage medium, such as a solid-state drive or a shingled magnetic recording hard disk. The controller 311 is used to write data to the hard disk 313 or read data from the hard disk 313 according to the read / write data request sent by the computing node 30. During the process of reading and writing data, the controller 311 needs to convert the address carried in the read / write data request into an address that the hard disk can recognize. It can be seen that the controller 311 also has some simple computing functions.
[0081] In practical applications, controller 311 can have various forms. In one embodiment, controller 311 includes a CPU and memory. The CPU is used to perform operations such as address translation and reading and writing data. The memory is used to temporarily store data to be written to hard disk 313 or read from hard disk 313 to be sent to computing node 30.
[0082] In another embodiment, the functions of controller 311 can be offloaded to network card 312. In other words, storage node 31 does not have controller 311 internally. Instead, network card 312 performs data reading and writing, address translation, and other computing functions. In this case, network card 312 is a smart network card. It can include a CPU and memory. The CPU is used to perform operations such as address translation and data reading and writing. The memory is used to temporarily store data to be written to hard disk 313, or data read from hard disk 313 to be sent to compute node 30.
[0083] In addition, in an embodiment of the present application, at the software level, a storage program may be deployed in the storage node 31. For example, a server of a database application may be deployed to provide functions such as data page access and access permission management in the database. It should be noted that the stored program may be stored in the memory of the controller 311, and the CPU of the controller 311 may execute the stored program to implement the functions of the server of the database application. Alternatively, the stored program may be stored in the memory of the network card 312, and the CPU in the network card 312 may execute the stored program to implement the functions of the server of the database application. It should be noted that when a database application is run in the storage system shown in FIG3 above, the operation architecture diagram of the database application may refer to the operation architecture diagram shown in FIG2 above. In this case, the memory in FIG2 may be the memory 302 in the computing node 30, and the hard disk may be the hard disk 313 in the storage node 31.
[0084] Next, the data writing method provided in the embodiment of the present application is explained in detail.
[0085] The data writing method provided in the embodiment of the present application can be executed by a computer device. Exemplarily, when the data writing method is applied to the storage system shown in Figure 1, the computer device may refer to any server in the storage system. When the data writing method is applied to the storage system shown in Figure 3, the computer device may refer to a computing node in the storage system. Among them, multiple threads to be written to data can be run in the computer device, and the method can be executed by any one of the multiple threads. For example, a database application can be deployed in the computer device, and the operating architecture of the database application can be as shown in Figure 2. The multiple threads can be multiple transaction threads included in the main thread group in the database main process. For the convenience of explanation, the first thread among the multiple threads is used as the execution subject for introduction in the following embodiments.
[0086] FIG4 is a flow chart of a data writing method provided in an embodiment of the present application. Referring to FIG4 , the method includes the following steps:
[0087] Step 401: If no available memory page is obtained from the plurality of free memory pages corresponding to the target data table, N free data pages are expanded for the first thread in the target data table, where N is a preset integer greater than 0.
[0088] In an embodiment of the present application, the data in the database can be stored in a persistent storage device in the form of a data table, wherein the persistent storage device can be a hard disk included in the computer device itself, or a hard disk in another device other than the computer device.
[0089] In addition, a data table may include multiple data pages, and the size of each data page indicates the amount of data contained in the corresponding data page. The sizes of the data pages can be equal, for example, 8 KB each. Furthermore, each data page corresponds to a memory page. Free data pages with free space in the data table also have free space in the corresponding memory pages, and are therefore referred to as free memory pages.
[0090] When a first thread in a computer device wants to write data to a target data table, it first retrieves an available memory page from the multiple free memory pages corresponding to the target data table. If an available memory page is retrieved, the first thread can write the data there. If no available memory page is retrieved, the first thread can trigger a data table expansion process to expand N free data pages for itself in the target data table. The target data table is any data table stored in a database.
[0091] In one possible implementation, the first thread may search for available memory pages in the free memory pages corresponding to the target data table; if adding a write lock to the currently found available memory page fails, the cumulative number of available memory pages that failed to add a write lock is counted; if the cumulative number of available memory pages that failed to add a write lock is greater than a reference threshold, it is determined that no available memory page is obtained in the multiple free memory pages corresponding to the target data table.
[0092] The free memory pages corresponding to the free data pages of a data table can be managed through a page binary tree. The page binary tree may include multiple page nodes, each page node is used to indicate a free memory page. Exemplarily, each page node may record a page identifier and remaining capacity of a free memory page. Based on this, the first thread can search for available memory pages in the multiple free memory pages corresponding to the multiple free data pages in the target data table through the page binary tree corresponding to the target data table.
[0093] Exemplarily, the first thread can start from the root node of the page binary tree or the next page node of the first page node corresponding to the available memory page that failed to add a write lock most recently, and search for page nodes whose recorded remaining capacity is not less than the amount of data to be written, and use the free memory page indicated by the first page node found that meets the above conditions as the currently found available memory page.
[0094] Optionally, the free memory pages corresponding to the free data pages in the data table can also be managed in other ways. For example, the computer device can store the page information of each free memory page in a certain order, wherein the page information includes the page identifier and remaining capacity of the memory page. On this basis, the first thread can start from the first page information or the page information of the available memory page that failed to add a write lock in the storage order, and search for page information including a remaining capacity that is not less than the amount of data to be written, and use the free memory page identified by the page identifier in the first page information found that meets the above conditions as the currently found available memory page.
[0095] After finding the available memory page, the first thread may add a write lock to the currently found available memory page.
[0096] It should be noted that each memory page may correspond to a lock state variable. The lock state variable may include a write lock status bit and a read lock count bit. When the value on the write lock status bit is the first value, it indicates that a certain thread currently holds the write lock of the memory page corresponding to the lock state variable. A write lock can also be called an exclusive lock. When a thread holds the write lock of the memory page, it indicates that the thread is writing data in the memory page. In this case, in order to avoid read-write conflicts, other threads will not be able to add a write lock to the memory page, nor will they be able to add a read lock to the memory page. In other words, other threads will not be able to hold the write lock and read lock of the memory page. When the value on the write lock status bit is the second value, it indicates that no thread currently holds the write lock of the memory page. The first value may be 1, and the second value may be 0. Alternatively, the first value may be 0, and the second value may be 1.
[0097] In addition, when the count value indicated by the read lock count bit of the lock state variable is 0, it means that the memory page corresponding to the lock state variable is not currently read locked. When the count value indicated by the read lock count bit is not 0, it means that the memory page corresponding to the lock state variable is currently read locked. At this time, the number of threads holding read locks on the memory page is equal to the aforementioned count value. If one or more threads hold read locks on the memory page, other threads cannot write lock the memory page to avoid read-write conflicts.
[0098] For example, referring to Figures 5 and 6, the lock state variable can be an 8-bit variable, in which the highest bit is the write lock state bit and the remaining bits are read lock count bits. As shown in Figure 5, when the write lock state bit is 1, it means that a thread currently holds the write lock of the memory page corresponding to the lock state variable. At this time, the count value of the read lock count bit is 0, that is, no thread holds the read lock of the memory page corresponding to the lock state variable. In this case, other threads will no longer be able to add write locks and read locks to the memory page. As shown in Figure 6, the write lock state bit is 0, indicating that no thread currently holds the write lock of the memory page corresponding to the lock state variable, and the count value of the read lock count bit is 3, indicating that there are currently 3 threads holding the read lock of the memory page corresponding to the lock state variable. In this case, other threads can continue to add read locks to the memory page, but cannot add write locks to the memory page.
[0099] Based on the above introduction, in an embodiment of the present application, the first thread can determine whether the write lock status bit in the lock status variable corresponding to the currently found available memory page is the second value, and whether the count value indicated by the read lock count bit is 0.
[0100] If the write lock status bit in the lock status variable corresponding to the currently found available memory page is the second value, and the count value indicated by the read lock count bit is 0, it means that there is currently neither a thread holding a write lock nor a thread holding a read lock on the available memory page. In this case, the first thread can set the write lock status bit of the lock status variable corresponding to the available memory page to the first value to implement a write lock on the available memory page. After the first thread successfully sets the write lock status bit to the first value, it means that the first thread has successfully added a write lock to the available memory page. In this case, it can be considered that the first thread has obtained the available memory page, and at this time, the first thread can write data in the available memory page.
[0101] If the write lock status bit in the lock status variable corresponding to the currently found available memory page is not the second value, or the count value indicated by the read lock count bit is not equal to 0, it means that other threads currently hold the write lock or read lock of the available memory page. In this case, the first thread will not be able to add a write lock to the available memory page. Therefore, the first thread can determine that adding a write lock to the available memory page has failed.
[0102] After failing to add a write lock to the currently found available memory page, the first thread can count the cumulative number of available memory pages that failed to add a write lock; if the cumulative number of available memory pages that failed to add a write lock is greater than a reference threshold, it means that the number of available memory pages that the first thread failed to add a write lock is already relatively large, and there may be very few available memory pages with enough space for the first thread to write data in the multiple free memory pages corresponding to the target data table, that is, there are very few free data pages in the target data table available for the first thread to write data. In this case, the first thread can determine that no available memory page is obtained, thereby triggering the data table expansion process, that is, the first thread can expand N free data pages for itself in the target data table. The reference threshold can be a preset threshold, and the reference threshold can be set according to the size of N. For example, the reference threshold can be a positive integer not less than N.
[0103] Optionally, if the cumulative number of available memory pages that failed to acquire a write lock is not greater than a reference threshold, the first thread may continue to search for other available memory pages using the aforementioned method.
[0104] In another possible implementation, the first thread can search for an available memory page in the free memory page corresponding to the target data table; if the write lock for the currently found available memory page fails, the thread waits; when the write lock of the currently found available memory page is released and the first thread successfully adds a write lock to the available memory page again, the first thread can determine whether the remaining capacity of the available memory page is less than the amount of data to be written. If the available memory page is less than the amount of data to be written, and the available memory page is the last available memory page of the first thread in the target data table, the first thread can determine that no available memory page is obtained in the multiple free memory pages corresponding to the target data table. In this case, the first thread can trigger the extended data table process to expand N free data pages for itself. That is, when the first thread determines that the remaining capacity of the last available memory page after other threads have written data is not enough for itself to write data, it can trigger the extended data table process. Alternatively, after the first thread fails to add a write lock to the currently found available memory page, it may not wait, but directly search for other available memory pages in the remaining free memory pages that have not been searched. If no other available memory pages are found, it means that the available memory page most recently found is the last available memory page. In this case, the first thread can trigger the process of expanding the data table.
[0105] When expanding the target data table, the first thread may add a write lock to the target data table; if adding the write lock to the target data table is successful, N extended storage areas are applied for the target data table in the persistent storage device; 0 is written into the N extended storage areas to obtain N free data pages; and then the write lock of the target data table is released.
[0106] Among them, each data table in the database can correspond to a lock state variable. The first thread can refer to the aforementioned method of locking the memory page and set the write lock state bit of the lock state variable corresponding to the target data table to add a write lock to the target data table. If the write lock is successfully added to the target data table, other threads will not be able to load the data pages in the target data table from the hard disk into the memory page for reading and writing, and will not be able to persist the data pages in the memory page corresponding to the target data table to the hard disk. In this way, it can prevent other threads from accessing the target data table during the process of expanding the data table, causing read and write conflicts.
[0107] If the write lock is successfully added to the target data table, the first thread may apply for N extended storage areas for the data table from the free storage area of the persistent storage device according to the size of the data page in the target data table, wherein the size of each extended storage area is equal to the size of the data page.
[0108] It should be noted that the above-mentioned N is the number of data pages that need to be expanded preset for each thread, that is, the N refers to the number of data pages that need to be expanded for a thread. In other words, any thread among the multiple threads running by the computer device that triggers the data table expansion process by failing to obtain available memory pages can expand N data pages, and the N will not increase as the number of threads that want to expand the data table increases. Among them, N can be set according to the maximum amount of data that the computer device can write in the persistent storage device within a reference time. For example, the reference time can be the operation time required for the computer device to write a data page in the persistent storage device.
[0109] Because random data may be stored in the extended storage area, after applying for N extended storage areas, the computer device can write 0 to each extended storage area, that is, clear the data in each extended storage area. At this time, each extended storage area stores a free data page, and the data in the free data page is all 0, that is, the free data page is an empty data page. The N free data pages stored in the N extended storage areas are the data pages obtained by the first thread for its own expansion.
[0110] For example, assuming the size of a data page in the target data table is 8KB, the time required for a computer device to write an 8KB data page to the persistent storage device is equal to the time required to write 128KB of data. Therefore, N can be set to 16. Based on this, the first thread can request 16 8KB extended storage areas from the free storage area of the persistent storage device, and then write zeros into these 16 extended storage areas, thereby obtaining 16 free data pages. Since the time required for the computer device to write an 8KB data page is equal to the time required to write 128KB of data, the time required for the first thread to concentrate on writing zeros into the 16 8KB extended storage areas is equal to the time required to write data into a single 8KB storage area. Therefore, when N is set according to the method described above, the threads can request a corresponding number of extended storage areas at once, and then concentrate on writing data into the requested extended storage areas to obtain multiple free data pages, thereby reducing the number of input and output (IO) operations and lowering IO latency.
[0111] Optionally, in a possible situation, the first thread may also apply for an extended storage area each time, that is, write 0 into the extended storage area, thereby obtaining a free data page.
[0112] After expanding N free data pages for itself, the first thread can release the write lock of the target data table by modifying the write lock status bit of the lock status variable corresponding to the target data table, so that other threads can subsequently access the data table.
[0113] Step 402: Expand free memory pages for the target data table based on the N free data pages.
[0114] After the first thread expands N free data pages for itself, it can allocate corresponding memory pages for the N free data pages; then, based on the memory pages corresponding to the N free data pages, it updates the page binary tree corresponding to the target data table. The page binary tree corresponding to the target data table includes multiple page nodes, and each page node is used to indicate a free memory page corresponding to the target data table.
[0115] The first thread may allocate corresponding memory pages for the N data pages from a free storage area of the memory, so that when accessing these free data pages subsequently, these free data pages may be loaded into corresponding free memory pages for operation.
[0116] Since the corresponding memory pages allocated to the N free data pages are free memory pages, when the free memory pages of the target data table are managed by the page binary tree, after the first thread allocates corresponding memory pages to the N free data pages, it can also update the information of these free memory pages to the page binary tree, so that other subsequent threads can find these free memory pages through the page binary tree.
[0117] For example, the first thread may first apply a write lock to the page binary tree corresponding to the target data table. If the write lock is successfully applied to the page binary tree, the first thread may add the page nodes corresponding to the memory pages corresponding to the N free data pages to the page binary tree. Thereafter, the first thread may release the write lock on the page binary tree.
[0118] Among them, the first thread can refer to the method of adding write locks to available memory pages introduced above, and add write locks to the page binary tree corresponding to the target data table. In this way, it can prevent multiple threads from updating the page binary tree of the target data table at the same time and causing conflicts.
[0119] If the first thread successfully adds a write lock to the page binary tree, the first thread can add corresponding page nodes in the page binary tree according to the generation rules of the page binary tree and the page information of the memory page corresponding to each expanded free data page. For example, the first thread can start adding page nodes for indicating the memory page corresponding to the free data page from the leaf node at the bottom of the page binary tree. Each page node can record the page information of the indicated memory page, such as the page ID and the remaining capacity. Since the memory page corresponding to the free data page at this time is a newly added free memory page that has not yet been written with data, the remaining capacity recorded in the newly added page node for indicating the memory page corresponding to the free data page can be equal to the capacity of the memory page.
[0120] After updating the page binary tree of the target data table, the first thread may release the write lock of the page binary tree.
[0121] Optionally, when managing the page information of the free memory pages corresponding to the target data table by other means, after allocating corresponding memory pages to the extended N free data pages, the first thread can also update the page information of multiple free memory pages corresponding to the managed target data table based on the page information of the memory pages corresponding to the N free data pages. The embodiments of the present application will not be repeated here.
[0122] From the above, it can be seen that in an embodiment of the present application, when the first thread expands the data table, it first locks the target data table so as to expand the data page in the persistent storage device. After the data page expansion is completed, the write lock of the target data table is released, and then the page binary tree is written to update the page binary tree. That is, the embodiment of the present application controls the two sub-processes of page expansion and binary tree update in the data table expansion process through two write locks. In this way, as shown in Figure 7, when the first thread expands the data page, it does not affect the update of the page binary tree by other threads that have completed the data page expansion before (the second thread as shown in Figure 7); when the first thread updates the page binary tree after expanding the data page, it does not affect the subsequent expansion of the data page by other threads (the third thread as shown in Figure 7). In other words, the first thread and other threads can execute different sub-processes in the data table expansion process in parallel, so that the expansion efficiency of the data table can be improved.
[0123] Alternatively, in some possible implementations, as described above, the first thread can write 0s to each extended storage area each time it requests an extension, thereby obtaining a free data page. Based on this, the first thread can also allocate a corresponding memory page to each free data page obtained during expansion, and update the page binary tree based on the memory page corresponding to the free data page. Thereafter, the first thread expands the next free data page.
[0124] Step 403: Write data into the expanded free memory page.
[0125] After expanding the target data table, the first thread may write data into the memory pages corresponding to the N free data pages expanded by the first thread.
[0126] The first thread can randomly determine an idle memory page from the memory pages corresponding to the N idle data pages. This idle memory page is the available memory page. The first thread can then apply a write lock to the available memory page using the aforementioned method, and after the write lock is successfully applied, write data to the available memory page.
[0127] Optionally, the first thread may continue searching for available memory pages starting from the next page node of the page node corresponding to the memory page on which the write lock failed most recently in the page binary tree corresponding to the target data table, or may start searching for available memory pages from the root node of the page binary tree. Since a page node indicating the memory page corresponding to the N extended free data pages has been newly added to the page binary tree, the first thread will find one of the extended free memory pages as an available memory page, and then apply a write lock to the available memory page using the method described above, and write data to the available memory page after the write lock is successfully applied.
[0128] In an embodiment of the present application, when any thread in the computer device fails to obtain an available memory page from the multiple free memory pages corresponding to the target data table, it can expand N free data pages for itself, and expand the free memory pages based on the N free data pages, and then write data in the expanded free memory pages. It can be seen that any thread that fails to obtain an available memory page can trigger the data table expansion process by itself to promptly expand N data pages for itself for its own use. Moreover, since there is no need to expand data pages for other threads, the time required for each thread to expand data pages is shorter. Compared with the solution in which one thread centrally expands data pages for all threads that have not obtained available memory pages while other threads are waiting, the thread blocking time can be shortened.
[0129] In addition, in the embodiment of the present application, there is no need to wait until the last free memory page is used up before expanding the data table. Instead, when the first thread fails to add a write lock to the found available memory page multiple times and the number of failures exceeds a threshold, the data table expansion process can be directly triggered. In other words, the embodiment of the present application can trigger the data table expansion in advance when there are still a small number of free memory pages in the data table. On this basis, the data table expansion will be carried out by different threads in a time-sharing manner, rather than by one thread carrying out the expansion and the other threads waiting together. In this way, the efficiency of data table expansion can be improved, the thread waiting time can be shortened, and the data writing speed can be increased.
[0130] FIG8 is a flow chart of another data writing method provided in an embodiment of the present application. Referring to FIG8 , the method includes the following steps:
[0131] Step 801: Search for available memory pages in a plurality of free memory pages corresponding to a target data table.
[0132] In an embodiment of the present application, the data in the database can be stored in a persistent storage device in the form of a data table, wherein the persistent storage device can be a hard disk included in the computer device itself, or a hard disk in another device other than the computer device.
[0133] In addition, a data table may include multiple data pages, and the size of each data page indicates the amount of data contained in the corresponding data page. The sizes of the data pages can be equal, for example, 8 KB each. Furthermore, each data page corresponds to a memory page. Free data pages with free space in the data table also have free space in the corresponding memory pages, and are therefore referred to as free memory pages.
[0134] In an embodiment of the present application, a page binary tree can be used to manage the free memory pages corresponding to the free data pages of a data table. The page binary tree may include multiple page nodes, each of which is used to indicate a free memory page. For example, each page node may record a page identifier and remaining capacity of a free memory page.
[0135] Based on this, when the first thread in the computer device wants to write data into the target data table, the first thread can first search for available memory pages in the multiple free memory pages corresponding to the multiple free data pages in the target data table through the page binary tree corresponding to the target data table.
[0136] Exemplarily, the first thread can search for page nodes whose recorded remaining capacity is not less than the amount of data to be written, starting from the root node of the page binary tree, based on the amount of data to be written, and use the free memory page indicated by the first page node found that meets the above conditions as the currently found available memory page.
[0137] Optionally, the free memory pages corresponding to the free data pages in the data table can also be managed in other ways. For example, the computer device can store the page information of each free memory page in a certain order, wherein the page information includes the page identifier and remaining capacity of the memory page. On this basis, the first thread can start from the first page information in the storage order and sequentially search for page information including a remaining capacity that is not less than the amount of data to be written, and use the free memory page identified by the page identifier in the first page information found that meets the above conditions as the currently found available memory page.
[0138] Step 802: If adding a write lock to the currently found available memory page fails, search for other available memory pages in multiple free memories.
[0139] After finding the available memory page, the first thread may add a write lock to the currently found available memory page. The implementation method of the first thread adding a write lock to the currently found available memory page can refer to the relevant implementation method introduced in the aforementioned step 401, and the embodiment of the present application will not be repeated here.
[0140] If the first thread successfully adds a write lock to the currently found available memory page, the first thread can write data into the available memory page.
[0141] If the first thread fails to add a write lock to the currently found available memory page, the first thread may search again for other available memory pages in multiple free memory pages corresponding to multiple free data pages in the target data table.
[0142] For example, taking the case of managing free memory pages through a page binary tree as an example, the first thread can start searching for the target page node from the next page node of the first page node in the page binary tree corresponding to the target data table, wherein the first page node is used to indicate the available memory page that currently fails to be locked, and the remaining capacity of the free memory page indicated by the target page node is not less than the amount of data to be written; the free memory page indicated by the target page node is used as another available memory page.
[0143] That is, the first thread can start the search again from the root node of the page binary tree. Instead, it can start from the page node next to the first page node corresponding to the available memory page that failed to add the write lock, and search one by one for page nodes whose remaining capacity is not less than the amount of data to be written. The first page node found that meets the above conditions is used as the target page node, and the free memory page indicated by the target page node is used as the newly found available memory page. In this case, the newly found available memory page will be different from the previously found available memory page.
[0144] After re-finding other available memory pages, the first thread can refer to the method described above to add a write lock to the available memory page. If adding the write lock fails, the first thread can again search for other available memory pages from multiple free memory pages until the write lock is successfully added to the found available memory page. The first thread then executes the following step 803.
[0145] For example, Figure 9 shows a schematic diagram of searching for available memory pages through a page binary tree corresponding to a target data table. As shown by the dotted arrow in Figure 9, assuming that the first thread starts from the root node of the page binary tree and finds the page node n2 indicating the available memory page P2, after failing to add a write lock to the available memory page P2, the first thread can start from the next node n4 of n2 to search for other available memory pages again. Assuming that the first thread re-finds the page node n8 indicating the available memory page P8, and still fails to add a write lock to the available memory page P8, the first thread can continue to search for other available memory pages from the next node of n8, and so on, until successfully adding a write lock to the found available memory page.
[0146] Optionally, after the first thread fails to add a write lock to the currently found available memory page, it can also count the cumulative number of available memory pages that failed to add a write lock; if the cumulative number of available memory pages that failed to add a write lock is not greater than a reference threshold, then perform the step of searching for other available memory pages from multiple free memory pages. If the cumulative number of available memory pages that failed to add a write lock is greater than the reference threshold, then expand N free data pages for itself in the target data table; then, based on the N free data pages, expand the free memory pages for the target data table, and then write data in the expanded free memory pages. Wherein, N is a preset integer greater than 0.
[0147] As can be seen from the above description, whenever the first thread finds an available memory page, it can add a write lock to the available memory page. If adding the write lock fails, the first thread can count the cumulative number of available memory pages on which adding the write lock failed. This cumulative number is the total number of times the first thread failed to add the write lock. If this cumulative number is not greater than the reference threshold, it means that the number of available memory pages on which the first thread failed to add the write lock is small, and there may be other available memory pages with sufficient space for the first thread to write data in the multiple free memory pages corresponding to the target data table. In this case, the first thread can continue to search for other available memory pages in the multiple free memory pages.
[0148] If the cumulative number is greater than the reference threshold, it indicates that the number of available memory pages on which the first thread failed to acquire a write lock is relatively large, and there may be only a few available memory pages with sufficient space for the first thread to write data in the multiple free memory pages corresponding to the target data table. In other words, there are only a few free data pages in the target data table available for the first thread to write data. In this case, the first thread can expand the target data table to increase the number of free data pages.
[0149] When expanding the target data table, the implementation process of the first thread extending N free data pages for itself in the target data table can refer to the relevant description in the aforementioned step 401. Thereafter, the implementation process of the first thread extending free memory pages for the target data table based on the N free data pages can refer to the relevant description in the aforementioned embodiment of step 402, which will not be repeated in this embodiment of the present application.
[0150] After the target data table is expanded, the first thread may write data into the expanded free memory pages.
[0151] The first thread may directly obtain a free memory page at random from the free memory pages corresponding to the expanded N free data pages, use the free memory page as an available memory page, add a write lock to the available memory page, and execute step 403 after the write lock is added successfully.
[0152] Optionally, the first thread may continue searching for available memory pages starting from the page node next to the page node corresponding to the memory page on which the write lock failed most recently, or may start searching for available memory pages from the root node of the page binary tree. Since the page binary tree has been newly added with a page node indicating the memory page corresponding to the extended data page, the first thread will find the memory page corresponding to the extended data page as an available memory page, and then apply a write lock to the available memory page using the method described above, until the write lock is successfully applied, and then execute step 403.
[0153] Optionally, in some possible situations, after the first thread fails to add a write lock to the currently found available memory page, it may also perform different operations according to different reasons for the failure to add the write lock.
[0154] For example, if the first thread fails to add a write lock because the currently found available memory page has been read-locked, the first thread can decide whether to wait or continue to search for other available memory pages based on the number of threads currently holding the read lock of the available memory page. For example, if the number of threads holding the read lock of the available memory page is not greater than the first threshold, the first thread can wait until all threads holding the read lock of the available memory page release the read lock before adding a write lock to the available memory page to write data. If the number of threads holding the read lock of the available memory page is greater than the first threshold, the first thread can continue to search for other available memory pages with reference to the method described above.
[0155] If the first thread fails to obtain a write lock because the currently found available memory page is already write-locked, the first thread may determine whether there are other threads currently waiting to obtain a write lock for the available memory page. If there are waiting threads, the first thread may count the number of waiting threads. If the number of waiting threads is not greater than a second threshold, the first thread may wait in line for other threads to write data and release the write lock before obtaining a write lock for the available memory page to write data. If the number of waiting threads is greater than the second threshold, the first thread may continue to search for other available memory pages using the method described above.
[0156] Step 803: Write data into other available memory pages to which write locks have been successfully added.
[0157] After the first thread successfully adds a write lock to the other found available memory page, the first thread can write data into the available memory page.
[0158] In an embodiment of the present application, when the first thread in the computer device writes data to the target data table, it can search for available memory pages in multiple free memory pages corresponding to the target data table. If the write lock for the currently found available memory page fails, it means that other threads are accessing the available memory page. In this case, the first thread does not wait for other threads to release the lock of the available memory page, but directly searches for other available memory pages in multiple free memory pages, and writes data in other available memory pages that have successfully added write locks. This can shorten the waiting time of the first thread, thereby improving data writing performance. Not only that, in an embodiment of the present application, when the first thread searches for other available memory pages, it directly starts searching from the next memory page of the available memory page that failed to be locked according to the page binary tree. In this way, compared to re-searching from the root node of the page binary tree, the search time is shortened.
[0159] In addition, in the embodiment of the present application, there is no need to wait until the last free memory page is used up before expanding the data table. Instead, when the first thread fails to add a write lock to the found available memory page multiple times and the number of failures exceeds the reference threshold, the data table expansion process can be directly triggered. In other words, the embodiment of the present application can trigger the data table expansion in advance when there are still a small number of free memory pages in the data table. On this basis, the data table expansion will be carried out by different threads in a time-sharing manner, rather than by one thread carrying out the expansion and the other threads waiting together. In this way, the efficiency of data table expansion can be improved, the thread waiting time can be shortened, and the data writing speed can be increased.
[0160] In conjunction with the data writing method described in the embodiments shown in Figures 4 and 8 above, the present application also provides a detailed example of a data writing method. As shown in Figure 10, the method includes the following steps:
[0161] Step 1001: Search for available memory pages starting from the root node of the page binary tree corresponding to the target data table.
[0162] The implementation of this step may refer to the implementation of step 801 in the aforementioned embodiment.
[0163] Step 1002: Add a write lock to the currently found available memory page. If adding the write lock succeeds, execute step 1003; if adding the write lock fails, execute step 1004.
[0164] The implementation of this step may refer to the relevant introduction in step 401 in the aforementioned embodiment.
[0165] Step 1003: Write data into the available memory page that has been successfully write-locked.
[0166] Step 1004: Count the cumulative number of available memory pages on which the first thread fails to acquire a write lock.
[0167] Step 1005: Determine whether the cumulative number of available memory pages that failed to add a write lock is greater than a reference threshold. If not, execute step 1006; if greater than the reference threshold, execute step 1007.
[0168] Step 1006: Start searching for other available memory pages from the next page node of the page node indicating the available memory page that failed to add a write lock in the page binary tree corresponding to the target data table, and then return to step 1002.
[0169] Step 1007: N free data pages are expanded for the target data table, and free memory pages are expanded for the target data table based on the N free data pages.
[0170] The implementation of this step may refer to the relevant implementations of step 401 and step 402 in the aforementioned embodiment.
[0171] Step 1008: Write data into the expanded free memory page.
[0172] The implementation of this step may refer to the relevant implementation of step 403 in the aforementioned embodiment.
[0173] Figure 11 is a curve comparison diagram of the data writing performance obtained by testing in a certain data writing scenario. In this data writing scenario, the size of the data to be written by each transaction thread is 0.75KB, and the amount of data written in the same data table per second is 300MB. The CPU of the computer device includes 48 physical cores and can concurrently process up to 96 transaction threads. Based on this, when writing data using the method in the related art, the relationship between the number of concurrent threads and the amount of transactions processed per second (transaction per second, TPS) is shown as the dotted line in Figure 11, and when writing data using the data writing method provided by the embodiment of the application, the relationship between the number of concurrent threads and TPS is shown as the solid line in Figure 11, wherein, TPS is the number of transactions that can be processed per second, wherein, a transaction is used to write a piece of data, so, the amount of data written per second can be indicated by TPS. It can be seen from this that in the scenario of concurrently writing a large amount of data in a single table, the data writing method provided by the embodiment of the application is used compared to the data writing method using the related art, and data writing performance is significantly improved.
[0174] Next, the data writing device provided in the embodiment of the present application is introduced.
[0175] FIG12 is a schematic diagram of the structure of a data writing device 1200 provided in an embodiment of the present application. The device can be deployed in a computer device, for example, in each server in the storage system shown in FIG1 , or in each computing node in the storage system shown in FIG3 . As shown in FIG12 , the data writing device 1200 can include a first expansion module 1201, a second expansion module 1202, and a writing module 1203.
[0176] A first expansion module 1201 is configured to execute step 401 in the aforementioned embodiment through a first thread;
[0177] A second expansion module 1202 is configured to execute step 402 in the aforementioned embodiment through a first thread;
[0178] The writing module 1203 is configured to execute step 403 in the aforementioned embodiment through the first thread.
[0179] Optionally, the first expansion module 1201 is specifically used to: add a write lock to the target data table; if the write lock is successfully added to the target data table, apply for N extended storage areas for the target data table in the persistent storage device; write 0 in the N extended storage areas to obtain N free data pages; and release the write lock of the target data table.
[0180] Optionally, the second expansion module 1202 is specifically used to: allocate corresponding memory pages for N free data pages; based on the memory pages corresponding to the N free data pages, update the page binary tree corresponding to the target data table, the page binary tree corresponding to the target data table includes multiple page nodes, each page node is used to indicate a free memory page corresponding to the target data table.
[0181] Optionally, the second expansion module 1202 is specifically used to: add a write lock to the page binary tree corresponding to the target data table; if the write lock is successfully added to the page binary tree corresponding to the target data table, then add page nodes corresponding to the memory pages corresponding to N free data pages in the page binary tree corresponding to the target data table; and release the write lock of the page binary tree corresponding to the target data table.
[0182] Optionally, the device 1200 also includes: a search module, used to search for available memory pages in multiple free memory pages corresponding to the target data table through the first thread; a statistics module, used to count the cumulative number of available memory pages that failed to add a write lock to the currently found available memory page through the first thread if the write lock fails; and a determination module, used to determine that the first thread has not obtained an available memory page in the multiple free memory pages corresponding to the target data table if the cumulative number of available memory pages that failed to add a write lock is greater than a reference threshold.
[0183] In an embodiment of the present application, when any thread in the computer device fails to obtain an available memory page from the multiple free memory pages corresponding to the target data table, it can expand N free data pages for itself, and expand the free memory pages based on the N free data pages, and then write data in the expanded free memory pages. It can be seen that any thread that fails to obtain an available memory page can trigger the data table expansion process by itself to promptly expand N data pages for itself for its own use. Moreover, since there is no need to expand data pages for other threads, the time required for each thread to expand data pages is shorter. Compared with the solution in which one thread centrally expands data pages for all threads that have not obtained available memory pages while other threads are waiting, the thread blocking time can be shortened.
[0184] FIG13 is a schematic diagram of the structure of a data writing device 1300 provided in an embodiment of the present application. The device can be deployed in a computer device, for example, in each server in the storage system shown in FIG1 , or in each computing node in the storage system shown in FIG3 . As shown in FIG13 , the data writing device includes a search module 1301 and a write module 1302 .
[0185] A search module 1301 is configured to execute steps 801 and 802 in the aforementioned embodiment through a first thread;
[0186] The writing module 1302 is configured to execute step 803 in the aforementioned embodiment through a first thread.
[0187] Optionally, the search module 1301 is specifically used to: search for the target page node starting from the next page node of the first page node in the page binary tree corresponding to the target data table, the page binary tree corresponding to the target data table includes multiple page nodes, each page node is used to indicate a free memory page corresponding to the target data table, the first page node is used to indicate the available memory page that currently fails to be locked, and the remaining capacity of the free memory page indicated by the target page node is not less than the amount of data to be written; the free memory page indicated by the target page node is used as another available memory page.
[0188] Optionally, the search module 1301 is specifically used to: if the write lock fails to be added to the currently found available memory page, then count the cumulative number of available memory pages that failed to add the write lock; if the cumulative number of available memory pages that failed to add the write lock is not greater than a reference threshold, then search for other available memory pages from multiple free memory pages.
[0189] Optionally, referring to Figure 13, the device 1300 may further include: a first expansion module 1303, for expanding N free data pages for the first thread in the target data table through the first thread if the cumulative number of available memory pages that failed to add write locks is greater than a reference threshold; a second expansion module 1304, for allocating corresponding memory pages to the N free data pages through the first thread, and updating the page binary tree corresponding to the target data table based on the memory pages corresponding to the N free data pages, the page binary tree corresponding to the target data table including multiple page nodes, each page node being used to indicate a free memory page corresponding to the target data table.
[0190] Optionally, the first expansion module 1303 is specifically used to: add a write lock to the target data table; if the write lock is successfully added to the target data table, apply for N extended storage areas for the target data table in the persistent storage device; write 0 in the N extended storage areas to obtain N free data pages; and release the write lock of the target data table.
[0191] Optionally, the second expansion module 1304 is specifically used to: add a write lock to the page binary tree corresponding to the target data table; if the write lock is successfully added to the page binary tree corresponding to the target data table, then add page nodes corresponding to the memory pages corresponding to N free data pages in the page binary tree corresponding to the target data table; and release the write lock of the page binary tree corresponding to the target data table.
[0192] In an embodiment of the present application, a computer device may search for an available memory page in a plurality of free memory pages corresponding to a target data table through a first thread. If adding a write lock to the currently found available memory page fails, it indicates that other threads are accessing the available memory page. In this case, the first thread does not wait for the other threads to release the lock on the available memory page, but directly searches for other available memory pages in the plurality of free memory pages and writes data to the other available memory pages for which the write lock is successfully added. This can shorten the waiting time of the first thread, thereby improving data writing performance.
[0193] It should be noted that the division of modules in the data writing device provided in the above embodiment is schematic and is only a logical functional division. In actual implementation, other division methods may be used. In addition, the functional modules in the various embodiments of the present application can be integrated into a processor, or can exist physically separately, or two or more modules can be integrated into one module. The above-mentioned integrated modules can be implemented in the form of hardware or software functional modules.
[0194] If the integrated module is implemented in the form of a software function module 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 embodiment 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 for enabling a network device (which may be a router or switch, etc.) or a processor (processor) to execute all or part of the steps of the method of each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0195] In addition, the data writing device and the data writing method provided in the above embodiments belong to the same concept, and their specific implementation processes are detailed in the method embodiments, which will not be repeated here.
[0196] In the above embodiments, all or part of the embodiments can be implemented by software, hardware, firmware, or any combination thereof. When implemented using software, all or part of the embodiments can be implemented in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of the present application are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via a wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) method. The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server or data center that includes one or more available media integrated therein. The available medium may be a magnetic medium (eg, a floppy disk, a hard disk, a magnetic tape), an optical medium (eg, a digital versatile disc (DVD)), or a semiconductor medium (eg, a solid state disk (SSD)).
Claims
1. A data writing method, It is characterized in that Applied to a first thread in a computer device, the first thread being any one of a plurality of threads to be written to data running in the computer device, the method comprising: If no available memory page is obtained from the multiple free memory pages corresponding to the target data table, N free data pages are expanded for the first thread in the target data table, where N is a preset integer greater than 0; Expanding free memory pages for the target data table based on the N free data pages; Write data in the extended free memory page.
2. The method according to claim 1, It is characterized in that The step of extending N idle data pages for the first thread in the target data table includes: Add a write lock to the target data table; If adding a write lock to the target data table is successful, applying for N extended storage areas for the target data table in the persistent storage device; Writing 0 into the N extended storage areas to obtain the N free data pages; Release the write lock of the target data table.
3. The method according to claim 1 or 2, It is characterized in that The step of extending the free memory pages for the target data table based on the N free data pages includes: Allocate corresponding memory pages to the N idle data pages; Based on the memory pages corresponding to the N free data pages, the page binary tree corresponding to the target data table is updated, wherein the page binary tree corresponding to the target data table includes multiple page nodes, each page node is used to indicate a free memory page corresponding to the target data table.
4. The method according to claim 3, It is characterized in that The updating of the page binary tree corresponding to the target data table based on the memory pages corresponding to the N free data pages includes: Add a write lock to the page binary tree corresponding to the target data table; If adding a write lock to the page binary tree corresponding to the target data table is successful, adding a page node corresponding to the memory page corresponding to the N free data pages in the page binary tree corresponding to the target data table; Release the write lock of the page binary tree corresponding to the target data table.
5. The method according to any one of claims 1 to 4, It is characterized in that The method further comprises: Searching for an available memory page among a plurality of free memory pages corresponding to the target data table; If adding a write lock to the currently found available memory page fails, the cumulative number of available memory pages that failed to add a write lock is counted; If the accumulated number of available memory pages that failed to add a write lock is greater than a reference threshold, it is determined that no available memory page is acquired from the plurality of free memory pages corresponding to the target data table.
6. A method for writing data, It is characterized in that Applied to a first thread in a computer device, the first thread being any one of a plurality of threads to be written to data running in the computer device, the method comprising: Search for available memory pages in multiple free memory pages corresponding to the target data table; If adding a write lock to the currently found available memory page fails, searching for other available memory pages in the plurality of free memory pages; Write data to other available memory pages that have successfully added write locks.
7. The method according to claim 6, It is characterized in that The searching for other available memory pages in the plurality of free memory pages includes: Search the target page node from the next page node of the first page node in the page binary tree corresponding to the target data table. point, the page binary tree corresponding to the target data table includes multiple page nodes, each page node is used to indicate a free memory page corresponding to the target data table, the first page node is used to indicate the available memory page that currently fails to be locked, and the remaining capacity of the free memory page indicated by the target page node is not less than the amount of data to be written; The free memory page indicated by the target page node is used as another available memory page.
8. The method according to claim 6 or 7, It is characterized in that If adding a write lock to the currently found available memory page fails, searching for other available memory pages in the plurality of free memory pages includes: If adding a write lock to the currently found available memory page fails, the cumulative number of available memory pages that failed to add a write lock is counted; If the accumulated number of available memory pages that fail to add a write lock is not greater than a reference threshold, other available memory pages are searched from the plurality of free memory pages.
9. The method according to claim 8, It is characterized in that The method further comprises: If the cumulative number of available memory pages that failed to add a write lock is greater than the reference threshold, N free data pages are expanded for the first thread in the target data table; Allocate corresponding memory pages to the N idle data pages; Based on the memory pages corresponding to the N free data pages, the page binary tree corresponding to the target data table is updated, wherein the page binary tree corresponding to the target data table includes multiple page nodes, each page node is used to indicate a free memory page corresponding to the target data table.
10. The method according to claim 9, It is characterized in that The step of extending N idle data pages for the first thread in the target data table includes: Add a write lock to the target data table; If adding a write lock to the target data table is successful, applying for N extended storage areas for the target data table in the persistent storage device; Writing 0 into the N extended storage areas to obtain the N free data pages; Release the write lock of the target data table.
11. The method according to claim 9 or 10, It is characterized in that The updating of the page binary tree corresponding to the target data table based on the memory pages corresponding to the N free data pages includes: Add a write lock to the page binary tree corresponding to the target data table; If adding a write lock to the page binary tree corresponding to the target data table is successful, adding a page node corresponding to the memory page corresponding to the N free data pages in the page binary tree corresponding to the target data table; Release the write lock of the page binary tree corresponding to the target data table.
12. A data writing device, It is characterized in that The device comprises: A first expansion module is used for expanding N free data pages for the first thread in the target data table through a first thread if no available memory page is obtained from the multiple free memory pages corresponding to the target data table, wherein N is a preset integer greater than 0, and the first thread is any one of the multiple threads to be written to the data running in the computer device; A second expansion module, configured to expand free memory pages for the target data table based on the N free data pages through the first thread; A writing module is used to write data in the extended free memory page through the first thread.
13. The device according to claim 12, It is characterized in that The first expansion module is specifically used for: Add a write lock to the target data table; If adding a write lock to the target data table is successful, applying for N extended storage areas for the target data table in the persistent storage device; Writing 0 into the N extended storage areas to obtain the N free data pages; Release the write lock of the target data table.
14. The device according to claim 12 or 13, It is characterized in that The second expansion module is specifically used for: Allocate corresponding memory pages to the N idle data pages; Based on the memory pages corresponding to the N free data pages, the page binary tree corresponding to the target data table is updated, wherein the page binary tree corresponding to the target data table includes multiple page nodes, each page node is used to indicate a free memory page corresponding to the target data table.
15. The device according to claim 14, It is characterized in that The second expansion module is specifically used for: Add a write lock to the page binary tree corresponding to the target data table; If adding a write lock to the page binary tree corresponding to the target data table is successful, adding a page node corresponding to the memory page corresponding to the N free data pages in the page binary tree corresponding to the target data table; Release the write lock of the page binary tree corresponding to the target data table.
16. The device according to any one of claims 12 to 15, It is characterized in that The device also includes: A search module, configured to search for an available memory page in a plurality of free memory pages corresponding to the target data table through the first thread; A statistics module, configured to count the cumulative number of available memory pages that failed to add a write lock if adding a write lock to the currently found available memory page fails through the first thread; The determination module is configured to determine that the first thread has not obtained any available memory page from the plurality of free memory pages corresponding to the target data table if the accumulated number of available memory pages that have failed to add a write lock is greater than a reference threshold.
17. A data writing device, It is characterized in that The device comprises: A search module, configured to search for an available memory page in a plurality of free memory pages corresponding to a target data table through a first thread; if adding a write lock to a currently found available memory page fails, searching for other available memory pages in the plurality of free memory pages, wherein the first thread is any one of a plurality of threads to be written to data running in a computer device; A writing module is used to write data in other available memory pages that have successfully added a write lock through the first thread.
18. The device according to claim 17, It is characterized in that The search module is specifically used for: Searching for a target page node from the next page node of the first page node in the page binary tree corresponding to the target data table, the page binary tree corresponding to the target data table includes a plurality of page nodes, each page node is used to indicate a free memory page corresponding to the target data table, the first page node is used to indicate an available memory page that currently fails to be locked, and the remaining capacity of the free memory page indicated by the target page node is not less than the amount of data to be written; The free memory page indicated by the target page node is used as another available memory page.
19. The device according to claim 17 or 18, It is characterized in that The search module is specifically used for: If adding a write lock to the currently found available memory page fails, the cumulative number of available memory pages that failed to add a write lock is counted; If the accumulated number of available memory pages that fail to add a write lock is not greater than a reference threshold, other available memory pages are searched from the plurality of free memory pages.
20. The device according to claim 19, It is characterized in that The device also includes: A first expansion module, configured to expand N free data pages for the first thread in the target data table through the first thread if the accumulated number of available memory pages that fail to add a write lock is greater than the reference threshold; The second expansion module is used to allocate corresponding memory pages to the N free data pages through the first thread; based on the memory pages corresponding to the N free data pages, the page binary tree corresponding to the target data table is updated, and the page binary tree corresponding to the target data table includes multiple page nodes, each page node is used to indicate a free memory page corresponding to the target data table.
21. The device according to claim 20, It is characterized in that The first expansion module is specifically used for: Add a write lock to the target data table; If adding a write lock to the target data table is successful, applying for N extended storage areas for the target data table in the persistent storage device; Writing 0 into the N extended storage areas to obtain the N free data pages; Release the write lock of the target data table.
22. The device according to claim 20 or 21, It is characterized in that The second expansion module is specifically used for: Add a write lock to the page binary tree corresponding to the target data table; If adding a write lock to the page binary tree corresponding to the target data table is successful, adding a page node corresponding to the memory page corresponding to the N free data pages in the page binary tree corresponding to the target data table; Release the write lock of the page binary tree corresponding to the target data table.
23. A computer device, It is characterized in that The computer device includes a processor and a memory, the memory stores at least one program instruction or code, and the processor is used to execute the at least one program instruction or code to implement the data writing method according to any one of claims 1 to 5 or claims 6 to 11.
24. A computer-readable storage medium, It is characterized in that The computer-readable storage medium stores instructions, and when the instructions are executed on a computer device, the computer device executes the data writing method according to any one of claims 1 to 5 or claims 6 to 11.
Citation Information
Patent Citations
Data writing method and device, computer equipment and storage medium
CN119149550A
Method and device for processing distributed transaction
CN105955804A
Shentong database three-layer cache segment search management structure and page search method
CN115145961A
Data page processing method, data page processing device, electronic equipment and storage medium
CN115543970A
Allocation locks and their use
US20050289188A1
Cited By
Data management method and device, electronic equipment and storage medium
CN119669198A
