Data writing and reading method and device
By introducing the structure of master nodes and slave nodes in the distributed service platform, the lock competition and write conflict problems when multiple nodes write data in parallel are solved, and the reliability and stability of data writing are improved.
Patent Information
- Application Number
- CN202411667117.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-20
- Publication Date
- 2025-02-25
AI Technical Summary
In the existing distributed service platform, when multiple service nodes write data to the database in parallel, lock competition and write conflict are prone to problems, resulting in instability and unreliability of data writing.
In the distributed service platform, the structure of master nodes and slave nodes is introduced. After all slave nodes receive data write requests, they will send the data to be written to the master node, and the master node is responsible for writing data to the database.
In this way, lock competition and write conflicts are avoided when multiple nodes write data in parallel, and the reliability and stability of data writing are improved.
Smart Images

Figure CN119513122A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, in particular to the field of data processing technology, and more particularly to a method and device for writing and reading data. Background Art
[0002] The distributed service platform is configured with a database and multiple distributed service nodes. Each service node can interact with the client and receive data write requests or data read requests sent by the user through the client. After receiving a data write request, the service node can write the data to be written in the request into the database. After receiving a data read request, it can read the data from the database and feed the read data back to the client. Summary of the invention
[0003] The present disclosure provides a data writing and reading method and device.
[0004] In a first aspect, an embodiment of the present disclosure provides a data writing method, which is applied to a first slave node in a distributed service platform, wherein the distributed service platform includes a database and multiple service nodes, one of the multiple service nodes is a master node, and the other nodes are slave nodes, and the first slave node is any one of the slave nodes;
[0005] The method comprises:
[0006] receiving a data writing request sent by a client and carrying first data to be written;
[0007] The first data is sent to the master node so that the master node writes the first data into the database.
[0008] In a second aspect, an embodiment of the present disclosure provides another data writing method, which is applied to a master node in a distributed service platform, wherein the distributed service platform includes a database and multiple service nodes, wherein one of the multiple service nodes is a master node and the other nodes are slave nodes;
[0009] The method comprises:
[0010] Obtaining first data sent by the slave node, wherein the first data is: data to be written carried in a data write request sent by a client to the slave node;
[0011] The first data is written into the database.
[0012] In a third aspect, an embodiment of the present disclosure provides a data reading method, which is applied to a target service node in a distributed service platform, wherein the distributed service platform includes a database and multiple service nodes;
[0013] The method comprises:
[0014] Receive a data read request sent by a client for data to be read;
[0015] Check whether the data to be read exists in its own cache;
[0016] If the data to be read does not exist in its own cache, searching other service nodes to see whether the data to be read is cached;
[0017] In the case that it is found that the data to be read is not cached in other service nodes, searching whether the data to be read is stored in the database;
[0018] In case of successful retrieval, the data to be read is obtained, and the data to be read is fed back to the client;
[0019] The successful retrieval situation includes one of the following three situations:
[0020] It is retrieved that the data to be read exists in its own cache, it is retrieved that the data to be read exists in the cache of other service nodes, and it is retrieved that the data to be read exists in the database.
[0021] In a fourth aspect, an embodiment of the present disclosure provides a data writing device, which is applied to a first slave node in a distributed service platform, wherein the distributed service platform includes a database and multiple service nodes, wherein one of the multiple service nodes is a master node, and the other nodes are slave nodes, and the first slave node is any one of the slave nodes;
[0022] The device comprises:
[0023] A write request receiving module, used for receiving a data write request sent by a client and carrying first data to be written;
[0024] A data sending module is used to send the first data to the master node so that the master node writes the first data into the database.
[0025] In a fifth aspect, an embodiment of the present disclosure provides another data writing device, which is applied to a master node in a distributed service platform, wherein the distributed service platform includes a database and multiple service nodes, wherein one of the multiple service nodes is a master node and the other nodes are slave nodes;
[0026] The device comprises:
[0027] A data acquisition module, used for acquiring first data sent by the slave node, wherein the first data is: data to be written carried in a data write request sent by a client to the slave node;
[0028] A data writing module is used to write the first data into the database.
[0029] In a sixth aspect, an embodiment of the present disclosure provides a data reading device, which is applied to a target service node in a distributed service platform, wherein the distributed service platform includes a database and a plurality of service nodes;
[0030] The device comprises:
[0031] A read request receiving module, used for receiving a data read request sent by a client for data to be read;
[0032] A first retrieval module is used to retrieve whether the to-be-read data exists in its own cache;
[0033] A second search module searches whether the data to be read is cached in other service nodes when the data to be read does not exist in its own cache;
[0034] A third retrieval module is used to retrieve whether the data to be read is stored in the database when it is found that the data to be read is not cached in other service nodes;
[0035] A data feedback module, used for obtaining the data to be read and feeding back the data to be read to the client in case of successful retrieval;
[0036] The successful retrieval situation includes one of the following three situations:
[0037] It is retrieved that the data to be read exists in its own cache, it is retrieved that the data to be read exists in the cache of other service nodes, and it is retrieved that the data to be read exists in the database.
[0038] In a seventh aspect, an embodiment of the present disclosure provides a service node, including:
[0039] at least one processor; and
[0040] a memory communicatively connected to the at least one processor; wherein,
[0041] The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the method described in the first aspect, the second aspect, or the third aspect.
[0042] In an eighth aspect, an embodiment of the present disclosure provides a non-transitory computer-readable storage medium storing computer instructions, wherein the computer instructions are used to enable the computer to execute the method described in the first aspect, the second aspect or the third aspect above.
[0043] In a ninth aspect, an embodiment of the present disclosure provides a computer program product, including a computer program, which implements the method described in the first aspect, the second aspect or the third aspect when executed by a processor.
[0044] It can be seen from the above that when writing data using the solution provided by the embodiment of the present disclosure, the distributed service platform includes a master node. After each slave node receives a data write request, it will send the first data to be written to the master node, and the master node will write the data to the database. This can solve the problem of lock competition and write conflicts when multiple nodes write data to the database in parallel, thereby improving the reliability of data writing.
[0045] It should be understood that the content described in this section is not intended to identify the key or important features of the embodiments of the present disclosure, nor is it intended to limit the scope of the present disclosure. Other features of the present disclosure will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] The accompanying drawings are used to better understand the present solution and do not constitute a limitation of the present disclosure.
[0047] Figure 1a It is a structural diagram of a distributed service platform in the prior art;
[0048] Figure 1b A schematic diagram of the structure of a distributed service platform provided in an embodiment of the present disclosure;
[0049] Figure 2 A schematic diagram of a flow chart of a first data writing method provided in an embodiment of the present disclosure;
[0050] Figure 3 A schematic diagram of a flow chart of a second data writing method provided in an embodiment of the present disclosure;
[0051] Figure 4 A flowchart of a data reading method provided by an embodiment of the present disclosure;
[0052] Figure 5 A schematic diagram of the structure of a first data writing device provided in an embodiment of the present disclosure;
[0053] Figure 6 A schematic diagram of the structure of a second data writing device provided in an embodiment of the present disclosure;
[0054] Figure 7 A structural schematic diagram of a data reading device provided in an embodiment of the present disclosure;
[0055] Figure 8It is a block diagram of a service node used to implement the data writing and reading method of the embodiment of the present disclosure. DETAILED DESCRIPTION
[0056] The following is a description of exemplary embodiments of the present disclosure in conjunction with the accompanying drawings, including various details of the embodiments of the present disclosure to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present disclosure. Similarly, for the sake of clarity and conciseness, descriptions of well-known functions and structures are omitted in the following description.
[0057] In the existing distributed service platform, each service node can write data to the database and read data from the database. When multiple service nodes write data to the database at the same time, access conflicts will occur. In addition, if the database is configured with a locking mechanism, there will be lock contention problems.
[0058] In order to solve the above technical problems, the embodiments of the present disclosure provide a data writing and reading method and device, which are described in detail below.
[0059] First, a comparison is made between the distributed service platform in the prior art and the distributed service platform in the embodiments of the present disclosure.
[0060] See also Figure 1a , Figure 1a The structure diagram of the distributed service platform in the prior art is shown in FIG. Each service node in the existing distributed service platform can interact with the client, receive the client's request, and feedback the request result, and each service node can interact with the database, write data to the database, or read data from the database.
[0061] See also Figure 1b , Figure 1b The structure diagram of the distributed service platform in the embodiment of the present disclosure. The distributed service platform in the embodiment of the present disclosure includes a database and multiple service nodes, among which one node is a master node and the other nodes are slave nodes. Each slave node interacts with the master node. Each slave node does not write data directly to the database, but sends the data to be written to the master node, and the master node writes the data to be written into the database. In addition, the slave node can read data from the database.
[0062] Figure 1a , Figure 1b The distributed service platform shown includes three service nodes. In addition, the distributed service platform may also include other numbers of service nodes. The embodiment of the present disclosure does not limit the number of service nodes.
[0063] The data writing and reading methods and devices provided by the embodiments of the present disclosure are described one by one through specific embodiments below.
[0064] See also Figure 2 , Figure 2 This is a flow chart of a first data writing method provided in an embodiment of the present disclosure. In this embodiment, the above method is applied to a first slave node in a distributed service platform.
[0065] The above-mentioned first slave node is any one of the slave nodes included in the distributed service platform. It can be understood that each slave node included in the distributed service platform can execute the data writing method provided by the embodiment of the present disclosure.
[0066] The above method includes the following steps S201-S202.
[0067] Step S201: receiving a data writing request sent by a client and carrying first data to be written.
[0068] There may be multiple slave nodes in the distributed service platform, and the multiple slave nodes may establish a communication connection with the client to interact with each other. Therefore, the first slave node may receive a data write request sent by the client with which the communication connection has been established.
[0069] In addition, multiple slave nodes may be distributed and arranged to interact with clients located in different regions and receive data writing requests from clients in different regions. The first slave node may receive a data writing request sent by a client in its region.
[0070] For example, slave node 1 is deployed in city 1 to interact with clients in city 1 and receive write requests from clients in city 1; slave node 2 is deployed in city 2 to interact with clients in city 2 and receive write requests from clients in city 2.
[0071] Step S202: Send the first data to the master node so that the master node writes the first data into the database.
[0072] After receiving the data write request, the first slave node can obtain the first data carried in the data write request and send the first data to the master node. After obtaining the first data, the master node can write the first data into the database.
[0073] It can be seen from the above that when writing data using the solution provided by the embodiment of the present disclosure, the distributed service platform includes a master node. After each slave node receives a data write request, it will send the first data to be written to the master node, and the master node will write the data to the database. This can solve the problem of lock competition and write conflicts when multiple nodes write data to the database in parallel, thereby improving the reliability of data writing.
[0074] The slave node can cache some data in the database. For example, after the slave node reads data from the database based on the data read request sent by the client, it can feed back the read data to the client and cache the data. For another example, after the slave node receives a data write request, it can send the data to be written in the request to the master node and cache the data.
[0075] When a slave node receives a data read request sent by a client, it can first query the data in its own cache. If the data is not found, it can query the data in the database, read the queried data from the database, and feed it back to the client.
[0076] In this case, in one embodiment of the present disclosure, the first slave node can obtain information about the second data sent by the master node after writing the second data to the database, and detect whether there is a historical version of the second data in its own cache based on the obtained information. If so, the second data stored in the database is obtained, and the historical version of the second data in its own cache is updated to the second data.
[0077] The second data may be data to be written sent by the first slave node or other slave nodes to the master node, or data to be written carried in a data write request received by the master node.
[0078] The information of the second data may include the name, identification, writing time, etc. of the second data.
[0079] Specifically, after the master node obtains the second data, it can write the second data into the database, and the written second data can be regarded as the latest version of the second data. If the database and the first slave node originally store historical versions of the second data, when the master node writes the second data into the database, the database can update the stored historical version of the second data to the latest version, while the first slave node still caches the historical version of the second data.
[0080] In this case, if the client sends a data read request to read the second data to the first slave node that caches the historical version of the second data, the first slave node first queries the second data in its own cache, and can query the historical version of the second data, thereby directly feeding back the historical version of the second data to the client, rather than the latest version of the second data.
[0081] To avoid the above problems, after writing the second data to the database, the master node can send information about the written second data to each slave node. After obtaining the information about the second data, the first slave node can detect whether the second data exists in its own cache based on the information about the second data. If it is detected that the second data exists in its own cache, it means that the cached data is a historical version of the second data. At this time, the first slave node can obtain the second data stored in the database. The second data stored in the database is the latest version. After the first slave node obtains the latest version of the second data from the database, it can update the historical version of the second data in its own cache to the latest version of the second data. In this way, if the first slave node subsequently receives a data read request from the client to read the second data, it can still query the second data in its own cache and feedback the latest version of the second data to the client.
[0082] It can be seen that each time the master node writes data, the slave node that caches the historical version of the written data can update the data in its own cache. This can ensure that the data cached in the slave node is always the latest version of the data, thereby ensuring that the data provided by the slave node to the client is always the latest version of the data, thereby improving the reliability and accuracy of data reading.
[0083] The master node may fail during operation due to equipment damage, network reasons, etc. If the master node fails, it will be difficult to write data to the database.
[0084] To prevent the problem of difficulty in writing data into the database, in one embodiment of the present disclosure, if the first slave node detects that it has not received the heartbeat information of the master node within the most recent first time period, it applies to other nodes for election to become the new master node, receives the election results fed back by other nodes, and determines whether it is elected as the new master node based on the received election results. If it is determined that it is elected as the new master node, it sends heartbeat information to other nodes.
[0085] The first duration is greater than the heartbeat information transmission period. Furthermore, the first durations corresponding to different slave nodes may be different.
[0086] Specifically, for the master node, the master node can periodically send heartbeat information to each slave node, and when the master node fails, the master node will not send heartbeat information.
[0087] For each slave node, when the slave node receives the heartbeat information, it can be determined that the master node is still running normally, and the slave node can start timing after receiving the heartbeat information, and the timing will be reset each time the heartbeat information is received. Under normal circumstances, the slave node receives a heartbeat information every other sending cycle. Therefore, the timing duration is reset every time the sending cycle is reached. In the event of a failure in the master node, the timing duration will not be reset after reaching the sending cycle, but will continue to count until the above-mentioned first duration is reached.
[0088] When the timing duration of the first slave node reaches the first duration, the first slave node can determine that the master node has failed. At this time, the first slave node can send a request to other nodes to apply to become a new master node. Based on the request, other nodes can judge whether to agree with the first slave node to become the new master node according to the preset election conditions, and feedback the election results to the first slave node. After receiving the election results fed back by other nodes, the first slave node can count the results of the election results that agree to itself become the new master node, obtain the statistical results, and then determine whether it is elected as the new master node according to the statistical results.
[0089] For example, if other nodes agree that the first slave node is the new master node, they can vote for the first slave node. After the first slave node obtains votes from other nodes, it can count the votes and determine whether it is elected as the new master node based on the number of votes obtained.
[0090] If the first slave node determines that it has been elected as the new master node, it sends heartbeat information to other nodes to notify them that the first slave node has become the new master node. When other nodes receive the heartbeat information again, they can be sure that there is a master node at this time and that the master node is running normally, so they will not apply to become the master node again.
[0091] It can be seen from the above that when the scheme provided by the embodiment of the present disclosure is applied to write data, if the first slave node has not received the heartbeat information of the master node within the most recent first time period, it can be determined that the master node has failed, so that the first slave node can apply for election to become a new master node. When the first slave node becomes the new master node, it sends heartbeat information to other nodes, and other nodes can also send data to be written to the new master node for data writing. It can be seen that when the data writing scheme provided by the embodiment of the present disclosure is applied, the above-mentioned election system can ensure that there is always a master node in the platform, thereby improving the reliability of data writing.
[0092] The first slave node may apply to become a new master node in the manner described in the following embodiments.
[0093] In one embodiment of the present disclosure, when the first slave node applies to other nodes for election to become a new master node, the first slave node can generate its own term number indicating that it is elected as the new master node based on the locally recorded master node term number; generate a first election request carrying its own term number; send the first election request to other nodes; obtain votes from other nodes indicating their agreement to the first slave node becoming the new master node based on feedback from the first election request; and determine whether itself is elected as the new master node based on the number of votes obtained.
[0094] Each node has a master node term number recorded locally, which is used to indicate that it has been elected as the new master node. The larger the master node term number, the higher the priority of being elected as the new master node, and the smaller the master node term number, the lower the priority of being elected as the new master node.
[0095] The initial value of the term number may be pre-configured. For example, the initial value of the term number of each node may be 0, 1 or other values.
[0096] Specifically, when the first slave node detects that it has not received heartbeat information within the first time period, it can increase the term number based on the locally recorded term number of the master node, and obtain the increased term number of the master node as its own term number. For example, if the original locally recorded term number of the master node is 1, the term number of the master node can be increased by 1, and the increased term number of the master node is 2, that is, its own term number is 2.
[0097] After obtaining its own term number, it generates a first election request carrying its own term number, and sends the first election request to other nodes. After receiving the first election request, other nodes can compare the master node term number recorded locally with their own term number carried in the first election request. If the master node term number recorded locally by other nodes is smaller than their own term number carried in the first election request, other nodes can feedback to the first slave node a vote indicating their agreement with the first slave node to become the new master node. If the master node term number recorded locally by other nodes is larger than their own term number carried in the first election request, other nodes will not vote for the first slave node.
[0098] The first slave node can count the number of votes received and determine whether it is elected as the new master node based on the number of votes received.
[0099] The specific implementation method of determining whether it is elected as the new master node according to the number of votes obtained can be found in the subsequent embodiments and will not be described in detail here.
[0100] It can be seen from the above that when the solution provided by the embodiment of the present disclosure is applied to write data, the first slave node sends a first election request carrying its own term number to other nodes, obtains votes fed back by other nodes, and can accurately determine whether itself is elected as the new master node based on the number of votes obtained. In this way, a new master node can be accurately elected, thereby ensuring that there is always a master node on the platform and improving the reliability of data writing.
[0101] The following describes two implementation methods for determining whether to be elected as the new master node based on the number of votes received.
[0102] In the first implementation method, the first slave node can determine the number of votes obtained by each other node, compare the number of votes obtained by each other node with the number of votes obtained by itself, and if the number of votes obtained by itself is greater than the number of votes obtained by each other node, the first slave node determines that it is elected as the new master node.
[0103] Specifically, there may be multiple slave nodes applying for election to become the new master node. For the convenience of distinction, the slave node that does not apply for election to become the new master node is called the follower node, the slave node that applies for election to become the new master node is called the candidate node, and the master node is called the leader node.
[0104] If a follower node detects that it has not received any heartbeat information within the first time period, the node will be transformed into a candidate node to participate in the election. Among them, if the first slave node detects that it has not received any heartbeat information within the first time period, its identity will be transformed from a follower node to a candidate node.
[0105] During the election process, each candidate node can obtain votes. After counting the votes they have obtained, each candidate node can send their votes to each other, so that each candidate node can compare the number of votes it has obtained with the number of votes obtained by other nodes. When one of the candidate nodes compares the number of votes it has obtained and finds that it is greater than the number of votes obtained by other nodes, the node can determine that it has been elected as the new leader node.
[0106] In this method, by comparing the number of votes obtained by the nodes, the node with the most votes can be accurately determined as the new master node. That is, by comparing the number of votes, the new master node can be accurately elected, thereby ensuring that there is always a master node on the platform and improving the reliability of data writing.
[0107] In the second implementation, the first slave node can detect whether the number of votes it obtains exceeds a preset threshold, and if so, it determines that it is elected as the new master node.
[0108] The quantity threshold may be determined according to the number of service nodes included in the platform. For example, the quantity threshold may be half, one third, or two thirds of the number of service nodes included in the platform.
[0109] Specifically, after the first slave node counts the number of votes it has obtained, it can compare the number of votes it has obtained with the preset number threshold. If the number of votes it has obtained is greater than the number threshold, it means that a relatively large number of nodes in the platform agree that the first slave node will become the new master node. In this way, the first slave node can determine that it has been elected as the new master node. It can be seen that the use of the number threshold can accurately determine whether the first slave node itself has been elected as the new master node, so that the new master node can be accurately elected, thereby ensuring that there is always a master node on the platform and improving the reliability of data writing.
[0110] In the above-mentioned second implementation method of determining whether itself is elected as the new master node, if the first slave node determines that the number of votes obtained by itself does not exceed the quantity threshold, subsequent processing can be performed in the manner mentioned in the following embodiment.
[0111] In one embodiment of the present disclosure, if a slave node determines that the number of votes it has obtained does not exceed the quantity threshold and meets the preset re-election conditions, it will re-execute the steps of generating its own term number representing its election as the new master node based on the locally recorded master node term number until a new master node is elected.
[0112] The following is an explanation of the various re-election conditions.
[0113] The first re-election condition is that the time from the start of the election to the current time exceeds the preset election cycle.
[0114] The election start time is any one of the time when the term number is generated, the time when the election request is generated, and the time when the election request is sent.
[0115] The above election period is a preset period, for example, the period can be any duration from 100ms to 30ms.
[0116] When the above-mentioned election start time is the time when its own term number is generated, the first slave node can start timing at the time when its own term number is generated. If the number of votes obtained by the first slave node itself does not exceed the quantity threshold, then when the timing duration reaches the election cycle, it means that the first slave node election has timed out, that is, the re-election conditions are met. At this time, the first slave node can regenerate its own term number and conduct the next round of elections until a new master node is elected. In this way, a new master node can be accurately elected, thereby ensuring that there is always a master node on the platform, thereby improving the reliability of data writing.
[0117] When the above-mentioned election start time is the time when the election request is generated, the first slave node starts timing from the time when the election request is generated. When the number of votes obtained by the first slave node itself does not exceed the quantity threshold and the timing duration reaches the election cycle, the next round of elections will be carried out until a new master node is elected. In this way, a new master node can be accurately elected, thereby ensuring that there is always a master node on the platform, thereby improving the reliability of data writing.
[0118] When the above-mentioned election start time is the time when the election request is sent, the first slave node starts timing from the time when the election request is sent. When the number of votes obtained by the first slave node itself does not exceed the quantity threshold and the timing duration reaches the election cycle, the next round of elections will be carried out until a new master node is elected. In this way, a new master node can be accurately elected, thereby ensuring that there is always a master node on the platform, thereby improving the reliability of data writing.
[0119] The second election condition is that the number of votes obtained by each node in the election does not exceed the quantity threshold.
[0120] In this case, for each node in the election, if the node detects that the number of votes it has obtained does not exceed the quantity threshold, it can send a notification to other nodes to inform them that the number of votes it has obtained does not exceed the quantity threshold. If the first slave node learns that the number of votes obtained by each node does not exceed the quantity threshold, it can re-conduct the next round of elections until a new master node is elected. In this way, a new master node can be accurately elected, thereby ensuring that there is always a master node on the platform, thereby improving the reliability of data writing.
[0121] It can be seen from the above that when the solution provided by the embodiment of the present disclosure is applied to write data, a new master node can be eventually elected through cyclic election, thereby ensuring that a master node always exists on the platform, thereby improving the reliability of data writing.
[0122] The first slave node can not only obtain votes from other nodes, but also vote for other nodes.
[0123] In one embodiment of the present disclosure, the first slave node can receive a second election request sent by the second slave node to apply for election to become the new master node; if the locally recorded master node term number is smaller than the term number carried in the second election request, the second slave node is voted for.
[0124] The second slave node is a slave node that is elected except the first slave node.
[0125] Other slave nodes except the first slave node may also participate in the election when they detect that they have not received heartbeat information for a long period of time. For example, when other slave nodes detect that they have not received heartbeat information within the most recent second period of time, they apply to nodes other than themselves for election to become the new master node. At this time, the other slave node is the second slave node.
[0126] If the term number of the master node recorded locally by the first slave node is less than the term number carried in the second election request, it means that the priority of the second slave node to become the new master node is higher than that of the first slave node. Therefore, the first slave node can vote for the second slave node to elect the second slave node as the new master node. It can be seen that when the scheme provided by the embodiment of the present disclosure is applied to write data, the first slave node also participates in the election of other nodes, which can increase the number of nodes participating in the election, thereby improving the reliability of the election, and then accurately electing a new master node, ensuring that there is always a master node on the platform, and improving the reliability of data writing.
[0127] In addition to voting based on term numbers, you can also vote based on term numbers and other information.
[0128] In one embodiment of the present disclosure, the second election request also carries: the processing time of the second slave node's most recent processing of the user request.
[0129] In this case, after obtaining the second election request sent by the second slave node, the first slave node can first make a judgment based on the term number to determine the size of the locally recorded master node term number and the term number carried in the second election request. If the two term numbers are equal, then compare the first processing time of the first slave node's own most recent processing of the user request with the second processing time carried in the second election request; the first processing time and the second processing time can be respectively considered as the time to determine that the first slave node and the second slave node are still operating normally. If the first processing time is not later than the second processing time, it means that the second slave node is still operating normally recently. At this time, the first slave node can vote for the second slave node, which can ensure that the elected master node is a normally operating node, thereby improving the reliability of the election and then improving the reliability of data writing.
[0130] When the first slave node votes for the second slave node, the voting may be performed in the manner mentioned in the following embodiments.
[0131] In one embodiment of the present disclosure, if the first slave node detects that the locally recorded master node term number is smaller than the term number carried in the second election request and has not voted in the current voting cycle, it votes for the second slave node.
[0132] Specifically, the first slave node can be set with a voting cycle, and only one node is allowed to vote in one voting cycle. If the first slave node has voted in a voting cycle, it needs to wait until the next voting cycle to continue voting. In this case, after receiving the election request, the first slave node can first determine whether it has not voted in the current voting cycle. If it has voted in the current voting cycle, it can directly not compare the term number and not vote for the second slave node. If it has not voted in the current voting cycle, it can compare the term number carried in the request with the locally recorded master node term number. It can be seen that the voting cycle is introduced to determine whether to vote for the second slave node. If it has voted in the current voting cycle, it will not vote for the second slave node. In this way, the first slave node only votes for one node in a period of time, avoiding confusion when voting for multiple nodes at the same time, and improving election efficiency.
[0133] In one embodiment of the present disclosure, when the first slave node detects that the locally recorded master node term number is smaller than the term number carried in the second election request, it can not only vote for the second slave node, but also adjust the locally recorded master node term number to the term number carried in the second election request. This can be considered as increasing the master node term number recorded locally by the first slave node, or changing the master node term number recorded locally by the first slave node, which can make the term numbers recorded by different slave nodes different, so that in the subsequent operation process, if a new master node needs to be elected, the new master node can be elected more quickly. Therefore, applying the solution provided in the embodiment of the present disclosure for data writing can improve the efficiency of determining the master node, so that there is always a master node on the platform, thereby improving the reliability of data writing.
[0134] Corresponding to the above-mentioned data writing method applied to the slave node, the embodiment of the present disclosure also provides a data writing method applied to the master node.
[0135] In one embodiment of the present disclosure, see Figure 3 , a flow chart of a second data writing method is provided. In this embodiment, the above method is applied to a master node in a distributed service platform, including the following steps S301-S302.
[0136] Step S301: Obtain first data sent from a node.
[0137] The first data is: the data to be written carried in the data write request sent by the client to the slave node.
[0138] Specifically, after the slave node obtains the data write request sent by the client, it can obtain the first data carried in the request and send the first data to the master node, so that the master node can obtain the first data sent by the slave node.
[0139] Step S302: writing first data into the database.
[0140] After obtaining the first data, the master node may immediately write the first data into the database, or temporarily cache the first data into its own cache. When the data writing condition is met, the data in the cache is written into the database.
[0141] It can be seen from the above that when the solution provided by the embodiment of the present disclosure is applied to write data, the distributed service platform includes a master node. After each slave node receives a data write request, it will send the first data to be written to the master node, and the master node will write the data to the database. This can solve the problem of lock competition and write conflict when multiple nodes write data to the database in parallel, thereby improving the reliability of data writing.
[0142] In one embodiment of the present disclosure, after the master node obtains the first data, the first data can be cached in the write cache of the master node; when the write cache meets the preset database persistence conditions, the data stored in the write cache is transferred to the database.
[0143] When the master node obtains the first data, it first caches the first data in the write cache of the master node. When the write cache meets the database persistence conditions, the data stored in the write cache is transferred to the database. Compared with the master node performing a write operation every time it obtains a piece of data to be written, this can reduce the number of data writes and save platform operating resources.
[0144] The above database persistence conditions are introduced below.
[0145] The above database persistence conditions may include at least one of the following conditions:
[0146] The time from the last time the data was transferred to the current time reaches the preset transfer period;
[0147] The amount of data stored in the write cache reaches a preset data amount threshold.
[0148] For the first database persistence condition mentioned above, it can be considered that the master node writes the data in the write cache to the database every preset transfer cycle. In this way, the master node can write data to the database periodically. Compared with the master node performing a write operation every time it obtains a piece of data to be written, the periodic writing method can reduce the number of data writes and save platform operating resources.
[0149] For example, if the transfer period is 1 second, the master node writes the data in the write cache to the database every 1 second.
[0150] For the second database persistence condition mentioned above, it can be considered that the master node will write data to the database only when a certain amount of data is stored in the write cache of the master node. Compared with the master node performing a write operation every time it obtains a piece of data to be written, this method can also reduce the number of data writes and save platform operating resources.
[0151] In one embodiment of the present disclosure, the master node can periodically send heartbeat information representing itself as the master node to each slave node to notify each slave node that the node sending the information is the master node. It can be seen from the aforementioned method embodiment applied to the slave node that if the slave node has not received the heartbeat information within the most recent first time period, it will start an election. Therefore, the master node periodically sends heartbeat information to each slave node, and can also prevent each slave node from conducting an election, maintain its own status as the master node, and ensure that there is only one master node in the platform, thereby avoiding node access conflicts and lock contention, and improving the stability and reliability of data writing.
[0152] When the slave node caches some data in the database, the master node may also execute the method mentioned in the following embodiment.
[0153] In one embodiment of the present disclosure, after writing the first data into the database, the master node synchronizes the information of the first data to each slave node, so that the slave node that caches the historical version of the first data obtains the first data stored in the database and updates the cached historical version of the first data to the first data.
[0154] The information of the first data may include the name, identification, writing time, etc. of the second data.
[0155] Specifically, after the master node writes the first data into the database, it synchronizes the information of the first data to each slave node. The slave node can detect whether it has cached the first data based on the information of the first data. If yes, it means that the slave node caches the historical version of the first data. At this time, the slave node can obtain the first data stored in the database and update the cached historical version of the first data to the first data, so that the data cached in the slave node is always the latest version of the data, thereby ensuring that the data provided by the slave node to the client is always the latest version of the data, thereby improving the reliability and accuracy of data reading.
[0156] Corresponding to the above data writing method, the embodiment of the present disclosure also provides a data reading method.
[0157] In one embodiment of the present disclosure, see Figure 4 , a flow chart of a data reading method is provided. In this embodiment, the above method is applied to a target service node in a distributed service platform, and the target service node can be a master node or a slave node.
[0158] The above method includes the following steps S401-S405.
[0159] Step S401: receiving a data read request sent by a client for data to be read.
[0160] Each node in the distributed service platform can interact with the client and receive data reading requests sent by the client.
[0161] Step S402: Check whether there is data to be read in its own cache. If yes, execute step S405; if not, execute step S403.
[0162] Each node in the distributed service platform may have a cache for caching part of the data in the database.
[0163] After receiving the data read request, the target service node can search whether there is data to be read in its own cache based on the data read request. If there is data to be read, the data to be read stored in its own cache is directly fed back to the client. At this time, step S405 is executed; if there is no data to be read, the following step S403 can be executed to search in other service nodes.
[0164] Step S403: Check whether other service nodes have cached data to be read. If yes, execute step S405; if no, execute step S404.
[0165] In a distributed service platform, each node can broadcast to other nodes what data it has cached, so that each node can determine the data cached by each node.
[0166] In this case, when the target service node fails to retrieve the data to be read in its own cache, it can search whether there is the data to be read in the data cached by other nodes determined by itself. If it is found that there is a service node cached with the data to be read, step S405 can be executed to obtain the data to be read and the data to be read can be fed back to the client. If the target service node still fails to retrieve the data to be read in the data cached by other nodes, the following step S404 can be executed to search in the database.
[0167] Step S404: Check whether there is data to be read stored in the database. If yes, execute step S405.
[0168] Specifically, if the target service node retrieves the data to be read stored in the database, step S405 can be executed at this time to obtain the data to be read from the database and feedback the data to be read to the client. If the data to be read is not retrieved in the database, the data reading is terminated, and the target service node can notify the client that the data to be read has not been read.
[0169] Step S405: in case of successful retrieval, the data to be read is obtained and the data to be read is fed back to the client.
[0170] Among them, successful retrieval includes one of the following three situations:
[0171] The data to be read is retrieved from its own cache, the data to be read is retrieved from the cache of other service nodes, and the data to be read is retrieved from the database.
[0172] Specifically, if the target service node retrieves the data to be read in its own cache, it can obtain the data to be read from its own cache; if the target service node retrieves the data to be read in the cache of other service nodes, it can obtain the data to be read from the other service nodes; if the target service node retrieves the data to be read stored in the database, it can obtain the data to be read from the database. After the target service node obtains the data to be read, it feeds back the data to be read to the client.
[0173] From the above, it can be seen that when the solution provided by the embodiment of the present disclosure is applied to read data, the data to be read can be retrieved in its own cache, other service nodes and the database. If the data to be read is retrieved and read in its own cache or other service nodes, it is not necessary to read the data in the database, which can reduce the reading pressure of the database. At the same time, if the data to be read is retrieved in the target service node's own cache, the data can be directly obtained from its own cache, which can improve the data reading efficiency.
[0174] The target service node may be a master node or a slave node. Based on the aforementioned data reading scheme, the master node and the slave node may execute different methods mentioned in the following embodiments.
[0175] In one embodiment of the present disclosure, when the target service node is the master node, the master node can periodically count the data reading information of the target data read by the user through each service node; determine the high-frequency access data in each target data based on the data reading information; update the data stored in its own cache based on the determined high-frequency access data, and synchronize the information of the high-frequency access data to each slave node.
[0176] Specifically, when a user reads data through a service node, the service node can, on the one hand, feed back the read data to the client used by the user, and on the other hand, feed back data reading information to the master node. The data reading information can include information about the data read by the user, so that the master node can determine the target data read by the user based on the acquired data reading information, count the number of reads, reading frequency, etc. of these target data, and according to the number of reads, reading frequency, etc. of the target data, determine, in each target data, the data whose number of reads exceeds a number threshold or whose reading frequency exceeds a frequency threshold as high-frequency access data.
[0177] After determining the high-frequency access data, the master node can retain the high-frequency access data in its own cache, delete other data, and cache other high-frequency access data that is not cached. In addition, the master node can also synchronize the information of high-frequency access data to each slave node, so that the slave node can also update the data stored in the node cache.
[0178] When the target service node is a slave node, the slave node can receive information about high-frequency access data synchronized by the master node, and update the data stored in its own cache according to the information about the high-frequency access data.
[0179] Specifically, after receiving information from the frequently accessed data, the slave node can determine the frequently accessed data in its own cache, retain the determined data, delete other data, and can also determine the frequently accessed data that it has not cached, thereby reading and caching the data in the database.
[0180] It can be seen from the above that when the solution provided by the embodiment of the present disclosure is applied to read data, after the master node determines the high-frequency access data, the master node and the slave node can update the data stored in their own caches according to the high-frequency access data or its information, so that the data cached by each node are all high-frequency access data. Since high-frequency access data can be considered as the data most frequently accessed by users, when high-frequency access data is cached in each node, each node can quickly obtain data in its own cache and feedback to the user. It can be seen that the data reading solution provided by the embodiment of the present disclosure can improve data reading efficiency and reduce the reading pressure of the database.
[0181] Corresponding to the above-mentioned data writing method, the embodiment of the present disclosure also provides a data writing device.
[0182] In one embodiment of the present disclosure, see Figure 5 , a structural schematic diagram of a first data writing device is provided, which is applied to a first slave node in a distributed service platform, wherein the distributed service platform includes a database and multiple service nodes, wherein one of the multiple service nodes is a master node, and the other nodes are slave nodes, and the first slave node is any one of the slave nodes;
[0183] The device comprises:
[0184] The write request receiving module 501 is used to receive a data write request sent by a client and carrying first data to be written;
[0185] The data sending module 502 is used to send the first data to the master node, so that the master node writes the first data into the database.
[0186] It can be seen from the above that when writing data using the solution provided by the embodiment of the present disclosure, the distributed service platform includes a master node. After each slave node receives a data write request, it will send the first data to be written to the master node, and the master node will write the data to the database. This can solve the problem of lock competition and write conflicts when multiple nodes write data to the database in parallel, thereby improving the reliability of data writing.
[0187] In one embodiment of the present disclosure, the device further includes:
[0188] An information obtaining module, used for obtaining information of the second data sent by the master node after writing the second data into the database;
[0189] A version detection module, used for detecting whether there is a historical version of the second data in its own cache according to the obtained information, and if yes, triggering a version update module;
[0190] The version updating module is used to obtain the second data stored in the database, and update the historical version of the second data in its own cache to the second data.
[0191] It can be seen that each time the master node writes data, the slave node that caches the historical version of the written data can update the data in its own cache. This can ensure that the data cached in the slave node is always the latest version of the data, thereby ensuring that the data provided by the slave node to the client is always the latest version of the data, thereby improving the reliability and accuracy of data reading.
[0192] In one embodiment of the present disclosure, the device further includes:
[0193] An election trigger module is used to apply to other nodes for election as a new master node if it detects that the heartbeat information of the master node has not been received within a recent first time period, receive the election results fed back by other nodes, and determine whether it is elected as the new master node based on the received election results, wherein the first time period is greater than the sending period of the heartbeat information;
[0194] The first information sending module is used to send heartbeat information to other nodes if it is determined that it is elected as the new master node.
[0195] From the above, it can be seen that when the scheme provided by the embodiment of the present disclosure is applied to write data, if the slave node has not received the heartbeat information of the master node within the most recent first time period, it can be determined that the master node has failed, so that the slave node can apply for election to become a new master node. When the slave node becomes the new master node, it sends heartbeat information to other nodes, and other nodes can also send data to be written to the new master node for data writing. It can be seen that when the data writing scheme provided by the embodiment of the present disclosure is applied, the above-mentioned election system can ensure that there is always a master node in the platform, thereby improving the reliability of data writing.
[0196] In one embodiment of the present disclosure, the election trigger module includes:
[0197] A term number generation submodule, for generating its own term number indicating that it has been elected as the new master node based on the locally recorded term number of the master node if it detects that no heartbeat information of the master node has been received within the most recent first period of time;
[0198] A request generation submodule, used for generating a first election request carrying the term number of the election itself;
[0199] A request sending submodule, used for sending the first election request to other nodes;
[0200] A ballot acquisition submodule, used to obtain ballots of other nodes agreeing that the first slave node will become the new master node based on the representation of the first election request feedback;
[0201] The election determination submodule is used to determine whether it is elected as the new master node based on the number of votes obtained.
[0202] It can be seen from the above that when the solution provided by the embodiment of the present disclosure is applied to write data, the first slave node sends a first election request carrying its own term number to other nodes, obtains votes fed back by other nodes, and can accurately determine whether itself is elected as the new master node based on the number of votes obtained. In this way, a new master node can be accurately elected, thereby ensuring that there is always a master node on the platform and improving the reliability of data writing.
[0203] In one embodiment of the present disclosure, the election determination submodule is specifically used to:
[0204] Check whether the number of votes it obtains exceeds the preset threshold;
[0205] If it exceeds, it determines that it is elected as the new master node.
[0206] It can be seen that the use of the quantity threshold can accurately determine whether the slave node itself is elected as the new master node, so that the new master node can be accurately elected, thereby ensuring that there is always a master node on the platform and improving the reliability of data writing.
[0207] In one embodiment of the present disclosure, the election determination submodule is further used to:
[0208] If the number of votes obtained by itself does not exceed the threshold number and meets the preset re-election conditions, the term number generation submodule will be re-triggered until a new master node is elected.
[0209] It can be seen from the above that when the solution provided by the embodiment of the present disclosure is applied to write data, a new master node can be eventually elected through cyclic election, thereby ensuring that a master node always exists on the platform, thereby improving the reliability of data writing.
[0210] In one embodiment of the present disclosure, the re-election condition is: the duration from the election start time to the current time exceeds the preset election cycle, wherein the election start time is: any one of the time when the term number itself is generated, the time when the election request is generated, and the time when the election request is sent.
[0211] This solution can accurately elect a new master node, thereby ensuring that there is always a master node on the platform, thereby improving the reliability of data writing.
[0212] In one embodiment of the present disclosure, the election determination submodule is specifically used to:
[0213] Determine the number of votes received by other nodes;
[0214] If the number of votes it receives is greater than the number of votes it receives, it is determined that it is elected as the new master node.
[0215] In this scheme, by comparing the number of votes obtained by the nodes, the node with the most votes can be accurately determined as the new master node. That is, by comparing the number of votes, the new master node can be accurately elected, thereby ensuring that there is always a master node on the platform and improving the reliability of data writing.
[0216] In one embodiment of the present disclosure, the device further includes:
[0217] An election request receiving module, used to receive a second election request sent by a second slave node to apply for election to become a new master node, wherein the second slave node is: a slave node other than the first slave node that is being elected;
[0218] The first voting module is used to vote for the second slave node if the locally recorded term number of the master node is smaller than the term number carried in the second election request.
[0219] When applying the solution provided by the embodiment of the present disclosure to write data, the first slave node also participates in the election of other nodes, which can increase the number of nodes participating in the election, thereby improving the reliability of the election, and then accurately electing a new master node, ensuring that there is always a master node on the platform, thereby improving the reliability of data writing.
[0220] In one embodiment of the present disclosure, the second election request also carries: the processing time of the second slave node's most recent processing of a user request;
[0221] The device also includes:
[0222] A time comparison module is used to vote for the second slave node if the locally recorded term number of the master node is equal to the term number carried in the second election request, and the first processing time of its most recent processing of a user request is not later than the second processing time carried in the second election request.
[0223] This solution can ensure that the elected master node is a normally operating node, thereby improving the reliability of the election and further improving the reliability of data writing.
[0224] In one embodiment of the present disclosure, the first voting module is specifically used to:
[0225] If the locally recorded term number of the master node is smaller than the term number carried in the second election request and no vote has been cast in the current voting cycle, then the second slave node is voted for.
[0226] In this scheme, a voting cycle is introduced to determine whether to vote for the second slave node. If the vote has been cast in the current voting cycle, the second slave node will not be voted. In this way, the first slave node only votes for one node within a period of time, avoiding confusion when voting for multiple nodes at the same time and improving election efficiency.
[0227] In one embodiment of the present disclosure, the device further includes:
[0228] A term number adjustment module is used to adjust the locally recorded term number of the master node to the term number carried in the second election request if the locally recorded term number of the master node is smaller than the term number carried in the second election request.
[0229] Applying the solution provided in the embodiment of the present disclosure to write data can improve the efficiency of determining the master node, so that the platform always has a master node, thereby improving the reliability of data writing.
[0230] In one embodiment of the present disclosure, see Figure 6 , a structural schematic diagram of a second data writing device is provided, which is applied to a master node in a distributed service platform, wherein the distributed service platform includes a database and a plurality of service nodes, wherein one of the plurality of service nodes is a master node and the other nodes are slave nodes;
[0231] The device comprises:
[0232] The data acquisition module 601 is used to obtain the first data sent by the slave node, wherein the first data is: the data to be written carried in the data write request sent by the client to the slave node;
[0233] The data writing module 602 is used to write the first data into the database.
[0234] It can be seen from the above that when the solution provided by the embodiment of the present disclosure is applied to write data, the distributed service platform includes a master node. After each slave node receives a data write request, it will send the first data to be written to the master node, and the master node will write the data to the database. This can solve the problem of lock competition and write conflict when multiple nodes write data to the database in parallel, thereby improving the reliability of data writing.
[0235] In one embodiment of the present disclosure, the device further includes:
[0236] The second information sending module is used to synchronize the information of the first data to each slave node after writing the first data into the database, so that the slave node that caches the historical version of the first data obtains the first data stored in the database and updates the cached historical version of the first data to the first data.
[0237] This solution can ensure that the data cached in the slave node is always the latest version of the data, thereby ensuring that the data provided by the slave node to the client is always the latest version of the data, thereby improving the reliability and accuracy of data reading.
[0238] In one embodiment of the present disclosure, the device further includes:
[0239] The third information sending module is used to periodically send heartbeat information indicating that the slave node is a master node to each slave node.
[0240] The master node periodically sends heartbeat information to each slave node, and can also prevent each slave node from electing, maintain its own master node status, and ensure that there is only one master node in the platform, thereby avoiding node access conflicts and lock competition, and improving the stability and reliability of data writing.
[0241] In one embodiment of the present disclosure, the data writing module 602 is specifically used to:
[0242] Cache the first data into the write cache of the master node;
[0243] When the write cache meets the preset database persistence condition, the data stored in the write cache is transferred to the database.
[0244] Compared with the scheme in which the master node performs a write operation every time it obtains a piece of data to be written, this scheme can reduce the number of data writes and save the platform's operating resources.
[0245] In one embodiment of the present disclosure, the database persistence condition includes at least one of the following conditions:
[0246] The time from the last time the data was transferred to the current time reaches the preset transfer period;
[0247] The amount of data stored in the write cache reaches a preset data amount threshold.
[0248] Applying at least one of the above two conditions can reduce the number of data writing times and save platform operating resources.
[0249] In one embodiment of the present disclosure, see Figure 7, a structural schematic diagram of a data reading device is provided, which is applied to a target service node in a distributed service platform, wherein the distributed service platform includes a database and a plurality of service nodes;
[0250] The device comprises:
[0251] The read request receiving module 701 is used to receive a data read request sent by a client for data to be read;
[0252] A first retrieval module 702 is used to retrieve whether the to-be-read data exists in its own cache;
[0253] The second search module 703 searches whether the data to be read is cached in other service nodes when the data to be read does not exist in the cache of the service node itself;
[0254] The third retrieval module 704 is used to retrieve whether the data to be read is stored in the database when it is found that the data to be read is not cached in other service nodes;
[0255] The data feedback module 705 is used to obtain the data to be read and feed back the data to be read to the client in case of successful retrieval;
[0256] The successful retrieval situation includes one of the following three situations:
[0257] It is retrieved that the data to be read exists in its own cache, it is retrieved that the data to be read exists in the cache of other service nodes, and it is retrieved that the data to be read exists in the database.
[0258] From the above, it can be seen that when the solution provided by the embodiment of the present disclosure is applied to read data, the data to be read can be retrieved in its own cache, other service nodes and the database. If the data to be read is retrieved and read in its own cache or other service nodes, it is not necessary to read the data in the database, which can reduce the reading pressure of the database. At the same time, if the data to be read is retrieved in the target service node's own cache, the data can be directly obtained from its own cache, which can improve the data reading efficiency.
[0259] In one embodiment of the present disclosure, one of the multiple service nodes of the distributed service platform is a master node, and the other nodes are slave nodes;
[0260] The device also includes:
[0261] The first update module is used to periodically count the data reading information of the target data read by the user through each service node when the target service node is the master node; determine the high-frequency access data in each target data according to the data reading information of each target data; update the data stored in its own cache according to the determined high-frequency access data, and synchronize the information of the high-frequency access data to each slave node;
[0262] The second updating module is used for receiving the information of the high-frequency access data synchronized by the master node when the target service node is a slave node, and updating the data stored in its own cache according to the information of the high-frequency access data.
[0263] It can be seen from the above that when the solution provided by the embodiment of the present disclosure is applied to read data, after the master node determines the high-frequency access data, the master node and the slave node can update the data stored in their own caches according to the high-frequency access data or its information, so that the data cached by each node are all high-frequency access data. Since high-frequency access data can be considered as the data most frequently accessed by users, when high-frequency access data is cached in each node, each node can quickly obtain data in its own cache and feedback to the user. It can be seen that the data reading solution provided by the embodiment of the present disclosure can improve data reading efficiency and reduce the reading pressure of the database.
[0264] According to an embodiment of the present disclosure, the present disclosure also provides a service node, a readable storage medium and a computer program product.
[0265] In one embodiment of the present disclosure, a service node is provided, including:
[0266] at least one processor; and
[0267] a memory communicatively connected to the at least one processor; wherein,
[0268] The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the data writing and data reading methods provided by the aforementioned method embodiment.
[0269] Figure 8A schematic block diagram of an example service node 800 that can be used to implement an embodiment of the present disclosure is shown. The service node is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The service node can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present disclosure described and / or required herein.
[0270] like Figure 8 As shown, the service node 800 includes a computing unit 801, which can perform various appropriate actions and processes according to a computer program stored in a read-only memory (ROM) 802 or a computer program loaded from a storage unit 808 to a random access memory (RAM) 803. In the RAM 803, various programs and data required for the operation of the service node 800 can also be stored. The computing unit 801, the ROM 802, and the RAM 803 are connected to each other via a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.
[0271] A number of components in the service node 800 are connected to the I / O interface 805, including: an input unit 806, such as a keyboard, a mouse, etc.; an output unit 807, such as various types of displays, speakers, etc.; a storage unit 808, such as a disk, an optical disk, etc.; and a communication unit 809, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 809 allows the service node 800 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.
[0272] The computing unit 801 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the computing unit 801 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, digital signal processors (DSPs), and any appropriate processors, controllers, microcontrollers, etc. The computing unit 801 performs the various methods and processes described above, such as data writing and reading methods. For example, in some embodiments, the data writing and reading methods may be implemented as a computer software program, which is tangibly contained in a machine-readable medium, such as a storage unit 808. In some embodiments, part or all of the computer program may be loaded and / or installed on the service node 800 via the ROM 802 and / or the communication unit 809. When the computer program is loaded into the RAM 803 and executed by the computing unit 801, one or more steps of the data writing and reading methods described above may be performed. Alternatively, in other embodiments, the computing unit 801 may be configured to perform data writing and reading methods in any other appropriate manner (e.g., by means of firmware).
[0273] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0274] The program code for implementing the method of the present disclosure may be written in any combination of one or more programming languages. These program codes may be provided to a processor or controller of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that the program code, when executed by the processor or controller, enables the functions / operations specified in the flow chart and / or block diagram to be implemented. The program code may be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.
[0275] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or device, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0276] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the computer. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).
[0277] The systems and techniques described herein may be implemented in a computing system that includes back-end components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes front-end components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such back-end components, middleware components, or front-end components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), and the Internet.
[0278] A computer system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The relationship of client and server is generated by computer programs running on respective computers and having a client-server relationship with each other. The server may be a cloud server, a server of a distributed system, or a server combined with a blockchain.
[0279] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps recorded in this disclosure can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solutions disclosed in this disclosure can be achieved, and this document does not limit this.
[0280] The above specific implementations do not constitute a limitation on the protection scope of the present disclosure. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present disclosure shall be included in the protection scope of the present disclosure.
Claims
1. A data writing method, applied to a first slave node in a distributed service platform, wherein the distributed service platform comprises a database and a plurality of service nodes, wherein one of the plurality of service nodes is a master node, and the other nodes are slave nodes, and the first slave node is any one of the slave nodes; The method comprises: receiving a data writing request sent by a client and carrying first data to be written; The first data is sent to the master node so that the master node writes the first data into the database.
2. The method according to claim 1, further comprising: Obtaining information of the second data sent by the master node after writing the second data to the database; According to the obtained information, detecting whether there is a historical version of the second data in its own cache; If yes, the second data stored in the database is obtained, and the historical version of the second data in its own cache is updated to the second data.
3. The method according to claim 1 or 2, further comprising: If it is detected that the heartbeat information of the master node is not received within the most recent first time period, apply to other nodes for election to become a new master node, receive the election results fed back by other nodes, and determine whether it is elected as the new master node according to the received election results, wherein the first time period is greater than the sending period of the heartbeat information; If it is determined that it has been elected as the new master node, it will send heartbeat information to other nodes.
4. The method according to claim 3, wherein: The applying to other nodes for election to become a new master node, receiving election results fed back by other nodes, and determining whether the node itself is elected as a new master node according to the received election results, includes: Based on the locally recorded master node term number, it generates its own term number, which indicates that it has been elected as the new master node. Generate a first election request carrying the term number of the own; Sending the first election request to other nodes; Obtaining votes from other nodes based on the first election request, indicating approval for the first slave node to become the new master node; Based on the number of votes obtained, it determines whether it is elected as the new master node.
5. The method according to claim 4, wherein: Determining whether to be elected as the new master node according to the number of votes obtained includes: Check whether the number of votes it obtains exceeds the preset threshold; If it exceeds, it determines that it is elected as the new master node.
6. The method according to claim 5, further comprising: If the number of votes it obtains does not exceed the threshold and meets the preset re-election conditions, it will re-execute the steps of generating its own term number representing its election as the new master node based on the local record until a new master node is elected.
7. The method according to claim 6, wherein: The re-election condition is: the time from the election start time to the current time exceeds the preset election cycle, wherein the election start time is: any one of the time when the term number itself is generated, the time when the election request is generated, and the time when the election request is sent.
8. The method according to claim 4, wherein: Determining whether to be elected as the new master node according to the number of votes obtained includes: Determine the number of votes each other node receives; If the number of votes it receives is greater than the number of votes received by other nodes, it is determined that it is elected as the new master node.
9. The method according to claim 1 or 2, further comprising: receiving a second election request sent by a second slave node to apply for election to become a new master node, wherein the second slave node is: a slave node other than the first slave node that is being elected; If the locally recorded term number of the master node is smaller than the term number carried in the second election request, a vote is performed on the second slave node.
10. The method according to claim 9, wherein: The second election request also carries: the processing time of the second slave node's most recent processing of a user request; The method further comprises: If the locally recorded master node term number is equal to the term number carried in the second election request, and the first processing time of its most recent processing of a user request is not later than the second processing time carried in the second election request, then vote for the second slave node.
11. The method according to claim 9, wherein: If the term number of the master node recorded locally is smaller than the term number carried in the second election request, voting for the second slave node includes: If the locally recorded term number of the master node is smaller than the term number carried in the second election request and no vote has been cast in the current voting cycle, then the second slave node is voted for.
12. The method according to claim 9, further comprising: If the locally recorded term number of the master node is smaller than the term number carried in the second election request, the locally recorded term number of the master node is adjusted to the term number carried in the second election request.
13. A data writing method, applied to a master node in a distributed service platform, wherein the distributed service platform comprises a database and a plurality of service nodes, wherein one of the plurality of service nodes is a master node and the other nodes are slave nodes; The method comprises: Obtaining first data sent by the slave node, wherein the first data is: data to be written carried in a data write request sent by a client to the slave node; The first data is written into the database.
14. The method according to claim 13, further comprising: After writing the first data into the database, information of the first data is synchronized to each slave node so that the slave node that caches the historical version of the first data obtains the first data stored in the database and updates the cached historical version of the first data to the first data.
15. The method according to claim 13, further comprising: Periodically sends heartbeat information to each slave node indicating that it is the master node.
16. The method according to any one of claims 13 to 15, wherein: The step of writing the first data into the database comprises: Cache the first data into the write cache of the master node; When the write cache meets the preset database persistence condition, the data stored in the write cache is transferred to the database.
17. The method according to claim 16, wherein: The database persistence condition includes at least one of the following conditions: The time from the last time the data was transferred to the current time reaches the preset transfer period; The amount of data stored in the write cache reaches a preset data amount threshold.
18. A data reading method, applied to a target service node in a distributed service platform, wherein the distributed service platform comprises a database and a plurality of service nodes; The method comprises: Receive a data read request sent by a client for data to be read; Check whether the data to be read exists in its own cache; If the data to be read does not exist in its own cache, searching other service nodes to see whether the data to be read is cached; In the case that it is found that the data to be read is not cached in other service nodes, searching whether the data to be read is stored in the database; In case of successful retrieval, the data to be read is obtained, and the data to be read is fed back to the client; The successful retrieval situation includes one of the following three situations: It is retrieved that the data to be read exists in its own cache, it is retrieved that the data to be read exists in the cache of other service nodes, and it is retrieved that the data to be read exists in the database.
19. The method according to claim 18, wherein: One of the multiple service nodes of the distributed service platform is a master node, and the other nodes are slave nodes; The method further comprises: When the target service node is a master node, periodically count the data reading information of the target data read by the user through each service node; determine the high-frequency access data in each target data according to the data reading information of each target data; update the data stored in its own cache according to the determined high-frequency access data, and synchronize the information of the high-frequency access data to each slave node; When the target service node is a slave node, it receives the information of the high-frequency access data synchronized by the master node, and updates the data stored in its own cache according to the information of the high-frequency access data.
20. A data writing device, applied to a first slave node in a distributed service platform, the distributed service platform comprising a database and a plurality of service nodes, one of the plurality of service nodes being a master node, the other nodes being slave nodes, the first slave node being any one of the slave nodes; The device comprises: A write request receiving module, used for receiving a data write request sent by a client and carrying first data to be written; A data sending module is used to send the first data to the master node so that the master node writes the first data into the database.
21. The apparatus according to claim 20, further comprising: An information obtaining module, used for obtaining information of the second data sent by the master node after writing the second data into the database; A version detection module, used for detecting whether there is a historical version of the second data in its own cache according to the obtained information, and if yes, triggering a version update module; The version updating module is used to obtain the second data stored in the database, and update the historical version of the second data in its own cache to the second data.
22. The device according to claim 20 or 21, further comprising: An election trigger module is used to apply to other nodes for election as a new master node if it detects that the heartbeat information of the master node has not been received within a recent first time period, receive the election results fed back by other nodes, and determine whether it is elected as the new master node based on the received election results, wherein the first time period is greater than the sending period of the heartbeat information; The first information sending module is used to send heartbeat information to other nodes if it is determined that it is elected as the new master node.
23. The device according to claim 22, wherein: The election trigger module includes: A term number generation submodule, for generating its own term number indicating that it has been elected as the new master node based on the locally recorded term number of the master node if it detects that no heartbeat information of the master node has been received within the most recent first period of time; A request generation submodule, used for generating a first election request carrying the term number of the election itself; A request sending submodule, used for sending the first election request to other nodes; A ballot acquisition submodule, used to obtain ballots from other nodes based on the first election request, indicating that they agree with the first slave node to become the new master node; The election determination submodule is used to determine whether it is elected as the new master node based on the number of votes obtained.
24. The device according to claim 23, wherein: The election determination submodule is specifically used for: Check whether the number of votes it obtains exceeds the preset threshold; If it exceeds, it determines that it is elected as the new master node.
25. The apparatus according to claim 24, wherein the election determination submodule is further configured to: If the number of votes obtained by itself does not exceed the threshold number and meets the preset re-election conditions, the term number generation submodule will be re-triggered until a new master node is elected.
26. The device according to claim 25, wherein The re-election condition is: the time from the election start time to the current time exceeds the preset election cycle, wherein the election start time is: any one of the time when the term number itself is generated, the time when the election request is generated, and the time when the election request is sent.
27. The device according to claim 23, wherein: The election determination submodule is specifically used for: Determine the number of votes each other node receives; If the number of votes it receives is greater than the number of votes received by other nodes, it is determined that it is elected as the new master node.
28. The apparatus according to claim 20 or 21, further comprising: An election request receiving module, configured to receive a second election request sent by a second slave node to apply for election to become a new master node, wherein the second slave node is: a slave node other than the first slave node that is being elected; The first voting module is used to vote for the second slave node if the locally recorded term number of the master node is smaller than the term number carried in the second election request.
29. The device according to claim 28, wherein The second election request also carries: the processing time of the second slave node's most recent processing of a user request; The device also includes: A time comparison module is used to vote for the second slave node if the locally recorded term number of the master node is equal to the term number carried in the second election request, and the first processing time of its most recent processing of a user request is not later than the second processing time carried in the second election request.
30. The device according to claim 28, wherein The first voting module is specifically used for: If the locally recorded term number of the master node is smaller than the term number carried in the second election request and no vote has been cast in the current voting cycle, then the second slave node is voted for.
31. The apparatus of claim 28, further comprising: A term number adjustment module is used to adjust the locally recorded term number of the master node to the term number carried in the second election request if the locally recorded term number of the master node is smaller than the term number carried in the second election request.
32. A data writing device, applied to a master node in a distributed service platform, the distributed service platform comprising a database and a plurality of service nodes, one of the plurality of service nodes being a master node and the other nodes being slave nodes; The device comprises: A data acquisition module, used for acquiring first data sent by the slave node, wherein the first data is: data to be written carried in a data write request sent by a client to the slave node; A data writing module is used to write the first data into the database.
33. The apparatus of claim 32, further comprising: The second information sending module is used to synchronize the information of the first data to each slave node after writing the first data into the database, so that the slave node that caches the historical version of the first data obtains the first data stored in the database and updates the cached historical version of the first data to the first data.
34. The apparatus of claim 32, further comprising: The third information sending module is used to periodically send heartbeat information indicating that the slave node is a master node to each slave node.
35. The device according to any one of claims 32 to 34, wherein: The data writing module is specifically used for: Cache the first data into the write cache of the master node; When the write cache meets the preset database persistence condition, the data stored in the write cache is transferred to the database.
36. The device according to claim 35, wherein The database persistence condition includes at least one of the following conditions: The time from the last time the data was transferred to the current time reaches the preset transfer period; The amount of data stored in the write cache reaches a preset data amount threshold.
37. A data reading device, applied to a target service node in a distributed service platform, wherein the distributed service platform comprises a database and a plurality of service nodes; The device comprises: A read request receiving module, used for receiving a data read request sent by a client for data to be read; A first retrieval module is used to retrieve whether the to-be-read data exists in its own cache; A second search module searches whether the data to be read is cached in other service nodes when the data to be read does not exist in its own cache; A third retrieval module is used to retrieve whether the data to be read is stored in the database when it is found that the data to be read is not cached in other service nodes; A data feedback module, used for obtaining the data to be read and feeding back the data to be read to the client in case of successful retrieval; The successful retrieval situation includes one of the following three situations: It is retrieved that the data to be read exists in its own cache, it is retrieved that the data to be read exists in the cache of other service nodes, and it is retrieved that the data to be read exists in the database.
38. The device according to claim 37, wherein One of the multiple service nodes of the distributed service platform is a master node, and the other nodes are slave nodes; The device also includes: The first update module is used to periodically count the data reading information of the target data read by the user through each service node when the target service node is the master node; determine the high-frequency access data in each target data according to the data reading information of each target data; update the data stored in its own cache according to the determined high-frequency access data, and synchronize the information of the high-frequency access data to each slave node; The second updating module is used for receiving the information of the high-frequency access data synchronized by the master node when the target service node is a slave node, and updating the data stored in its own cache according to the information of the high-frequency access data.
39. A service node, comprising: at least one processor; as well as a memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the method of any one of claims 1-12, or 13-17, or 18-19.
40. A non-transitory computer-readable storage medium storing computer instructions, wherein: The computer instructions are used to cause the computer to execute the method according to any one of claims 1-12, or 13-17, or 18-19.
41. A computer program product comprising a computer program, which, when executed by a processor, implements the method according to any one of claims 1-12, or 13-17, or 18-19.
Citation Information
Patent Citations
Method and device for selecting master node in distributed system
CN106911728A
Data storage method and a server applied to distributed server cluster
CN107295080A
Distributed database and data writing method
CN110928943A
Data storage method and device and computer readable storage medium
CN111198662A
High-availability cluster state monitoring method and system
CN114064414A
Cited By
Write-in conflict processing method and device, equipment, medium and program product
CN120973433A