Data synchronization method, device, storage medium and electronic device
By combining the state switching switch with the data synchronization strategy, the problems of data confusion and interruption during database replacement are solved, the reliability and consistency of data synchronization are achieved, and the security and reliability of database switching are ensured.
Patent Information
- Application Number
- CN202211649495.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-21
- Publication Date
- 2025-09-16
- Estimated Expiration
- 2042-12-21
AI Technical Summary
During the application upgrade process, data confusion, service interruption or errors are likely to occur when the database is replaced, making it difficult to ensure the consistency of data synchronization.
By setting the state switching switch, defining the data synchronization strategy corresponding to different states, and performing corresponding data synchronization operations between the master database and the slave database, we ensure that there is only one master database in each stage, and perform state switching according to the execution results of the synchronization strategy to achieve flexibility and consistency of data synchronization.
It ensures the reliability and consistency of data synchronization, avoids interruptions and confusion during the data synchronization process, and improves the efficiency and security of data synchronization.
Smart Images

Figure CN115934842B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of electronic devices, and in particular to a data synchronization method, device, storage medium and electronic device. Background Art
[0002] As technology develops, applications installed on electronic devices need to be continuously upgraded and improved to meet user needs.
[0003] Among them, during the application upgrade process, the database may need to be replaced. When replacing the database, a series of problems may occur, such as data confusion, service interruption or errors, making it difficult to ensure consistency when synchronizing data between different databases. Summary of the Invention
[0004] Embodiments of the present application provide a data synchronization method, device, storage medium, and electronic device, which can improve the consistency of data synchronization.
[0005] In a first aspect, an embodiment of the present application provides a data synchronization method, the method comprising:
[0006] Get the current state of the state switch, where different states of the state switch correspond to different data synchronization strategies;
[0007] Determine a data synchronization strategy, a master database, and a slave database corresponding to the current state, wherein the master database and the slave database are determined from a first database and a second database corresponding to different versions of the same application;
[0008] Execute data synchronization strategy on the slave database based on the master database;
[0009] The state switching operation of the state switching switch is determined based on the execution result of the data synchronization strategy. The state switching operation includes switching to the next state or switching to the previous state.
[0010] In a second aspect, an embodiment of the present application further provides a data synchronization device, including:
[0011] A state identification module is used to obtain the current state of the state switch, where different states of the state switch correspond to different data synchronization strategies;
[0012] A policy matching module is used to determine a data synchronization policy, a master database, and a slave database corresponding to a current state, wherein the master database and the slave database are determined from a first database and a second database corresponding to different versions of the same application;
[0013] A policy execution module is used to execute data synchronization policies on the slave database based on the master database;
[0014] The state switching module is used to determine the state switching operation of the state switching switch based on the execution result of the data synchronization strategy. The state switching operation includes switching to the next state or switching to the previous state.
[0015] In a third aspect, an embodiment of the present application further provides a computer-readable storage medium on which a computer program is stored. When the computer program runs on a computer, the computer executes a data synchronization method as provided in any embodiment of the present application.
[0016] In a fourth aspect, an embodiment of the present application further provides an electronic device comprising a processor and a memory, wherein the memory contains a computer program, and the processor calls the computer program to execute a data synchronization method as provided in any embodiment of the present application.
[0017] The technical solution provided by the embodiments of this application sets data synchronization policies, master databases, and slave databases corresponding to different switch states, thereby specifically switching the master database or data synchronization policy. This allows the master database to execute the data synchronization policy on the slave database based on the master database, thereby meeting the requirements of data synchronization at different stages and ensuring data synchronization reliability. Furthermore, only one master database is set for data synchronization policies corresponding to different states, thus ensuring data synchronization consistency. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0019] Figure 1 A first flow chart of the data synchronization method provided in an embodiment of the present application.
[0020] Figure 2 A second flow chart of the data synchronization method provided in an embodiment of the present application.
[0021] Figure 3 This is a third flow chart of the data synchronization method provided in an embodiment of the present application.
[0022] Figure 4 A schematic diagram of the structure of a data synchronization device provided in an embodiment of the present application.
[0023] Figure 5 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0024] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the embodiments described are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present application.
[0025] References herein to "embodiments" mean that a particular feature, structure, or characteristic described in connection with the embodiments may be included in at least one embodiment of the present application. The appearance of this phrase in various places in the specification does not necessarily refer to the same embodiment, nor does it constitute an independent or alternative embodiment that is mutually exclusive of other embodiments. It is understood, both explicitly and implicitly, by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0026] The present invention provides a data synchronization method. The execution subject of the data synchronization method can be a data synchronization device provided in the present invention, or an electronic device integrated with the data synchronization device. The data synchronization device can be implemented in hardware or software, and the electronic device can be a tablet computer, desktop computer, or server.
[0027] First, see Figure 1 , Figure 1 Schematic diagram of an application scenario of the data synchronization method provided in an embodiment of the present application. This application scenario involves database synchronization and updating. As shown in the figure, the old version of Database A is on the left, and the new version of Database B is on the right. The process of data synchronization between the two versions of the database is the process of data interaction to achieve data consistency between Database A and Database B.
[0028] Specifically, see Figure 2 , Figure 2 This is a flow chart of the data synchronization method provided in the embodiment of the present application. The specific flow of the data synchronization method provided in the embodiment of the present application can be as follows:
[0029] 101. Obtain the current state of the state switch, where different states of the state switch correspond to different data synchronization strategies.
[0030] A virtual state switch is set to control the switching of its state, specifically, the switching of the master database and the switching of the data synchronization strategy. The state switch can have one, two or more states, and different states can be identified by different characters.
[0031] 102. Determine a data synchronization strategy, a master database, and a slave database corresponding to the current state, wherein the master database and the slave database are determined from a first database and a second database corresponding to different versions of the same application.
[0032] Different versions of the same application can be the old version and the new version before and after the application is upgraded. The database corresponding to the old version of the application is called the old database, and the database corresponding to the new version of the application is called the new database. The first database and the second database are not limited here to be old databases or new databases. It should only be noted that the first database and the second database correspond to different versions of the database.
[0033] By pre-setting corresponding master and slave databases for different states of the state switch, the master and slave databases can be switched based on the current state of the state switch. In certain states of the state switch, a first database can function as the master database, with a corresponding second database acting as the slave database. Similarly, in other states of the state switch, the second database can function as the master database, with the first database acting as the slave database. This ensures that the state switch only has one designated database as the master database during state switching.
[0034] In addition, different data synchronization policies are configured for different states of the state switch. After identifying the current state of the state switch, the corresponding data synchronization policy can be used. Data synchronization policies include but are not limited to: synchronous read and write, asynchronous read and write, partial synchronous read and write, partial asynchronous read and write, synchronous read + partial write, synchronous write + partial read, read only, write only, etc.
[0035] The data synchronization strategy corresponding to different states may be one of the aforementioned strategies, or a combination of two or more of the aforementioned strategies, and the specific implementation is not limited here.
[0036] 103. Execute data synchronization strategy on the slave database based on the master database.
[0037] The data synchronization strategy is executed based on the data in the master database, and the data synchronization strategy is executed on the data in the slave database to achieve data synchronization between the master data and the slave data.
[0038] 104. Determine a state switching operation of the state switch based on the execution result of the data synchronization policy. The state switching operation includes switching to a next state or switching to a previous state.
[0039] By controlling the state of the state switch based on the execution results of the data synchronization policy, the state of the state switch can be switched, thereby switching the data synchronization policy. This allows the database data synchronization stage to switch to the next stage or roll back to the previous stage, allowing for flexible response to a variety of problems that arise during the data synchronization process, thereby ensuring data synchronization consistency.
[0040] During specific implementation, the present application is not limited by the execution order of the various steps described. If no conflict occurs, some steps can be performed in other orders or simultaneously.
[0041] Through the data synchronization method in the embodiment of the present application, the master database and the data synchronization strategy can be switched by controlling the state of the state switching switch, so that the data synchronization strategy can be executed on the slave database based on the corresponding database in the current state corresponding to the state switching switch, so as to implement data synchronization operations in each data synchronization link, making the data synchronization operation more comprehensive and complete, thereby ensuring the data consistency of different databases during the data synchronization process.
[0042] In some embodiments, determining a master database and a slave database corresponding to a current state includes:
[0043] If the current state is one of the first state, the second state, the third state, and the fourth state, the first database is determined as the master database, and the second database is determined as the slave database;
[0044] If the current state is the fifth state or the sixth state, the second database is determined as the master database, and the first database is determined as the slave database.
[0045] Exemplarily, the states of the state switch include a first state, a second state, a third state, a fourth state, a fifth state, and a sixth state. Different states of the state switch correspond to corresponding master and slave databases, wherein the master and slave databases are determined from a first database and a second database corresponding to different versions of the same application. The first and second databases are relative. In this embodiment, the version of the first database is set to be earlier than that of the second database, that is, the first database is an old version database and the second database is a new version database.
[0046] Among them, when performing database switching, a master database can be set to have at least one slave database, and the number of slave databases is not limited here. In the embodiment of the present application, only one slave database is used as an example to illustrate the solution provided in the embodiment of the present application. When there are multiple slave databases, the execution method is similar, so the implementation method with two or more slave databases will not be introduced in detail below.
[0047] The state switch has a corresponding master database, slave database, and data synchronization strategy. Please refer to the following table for details:
[0048] The state of the state toggle switch Master database Data synchronization strategy First state First Database First data synchronization strategy Second state First Database Second data synchronization strategy The third state First Database Third data synchronization strategy Fourth State First Database Fourth data synchronization strategy Fifth State Second database Fifth data synchronization strategy Sixth State Second database Sixth Data Synchronization Strategy
[0049] The data synchronization strategies corresponding to different states may be partially the same or completely different, and this is not limited here. In addition, as the number of states of the state switching switch increases, the corresponding data synchronization strategies also increase accordingly. Therefore, the embodiments of this application do not limit the number of states of the state switching switch and the number of data synchronization strategies. It is only necessary to point out that any method of setting different data synchronization strategies corresponding to different states of the state switching switch should fall within the scope of protection required by this application.
[0050] In some embodiments, executing a data synchronization strategy on a slave database based on a master database includes:
[0051] If the current state is the first state, the historical data of the master database is synchronized to the slave database;
[0052] If the current state is the second state, the data update information of the master database is obtained, and an asynchronous thread is initiated based on the data update information to perform a data update operation on the slave database;
[0053] If the current state is the third state, synchronous read and write operations are performed on the master database and the slave database;
[0054] If the current state is the fourth state, obtain the configured read and write rates, and perform data query operations on the master database or the slave database according to the read and write rates;
[0055] If the current state is the fifth state, perform synchronous write operations on the slave database and synchronous read and write operations on the master database;
[0056] If the current state is the sixth state, the data writing operation to the slave database is stopped.
[0057] This embodiment provides data synchronization strategies corresponding to different states of a state switching switch, as well as solutions for how to execute the corresponding data synchronization strategies.
[0058] In this embodiment, a database primary key is also pre-configured to support operations such as querying, inserting, updating, and deleting data in the master database or the slave database, thereby assisting in completing data synchronization operations.
[0059] For example, the database primary key can be generated in the application code using a snowflake algorithm, or a new sequence table can be created in a new version of the database, and the database primary key can be generated in batches using a database lock mechanism and stored in the application cache.
[0060] As an implementation, when the state switch is in the first state, the historical data of the master database can be synchronized to the slave database, that is, the historical data of the old database can be migrated to the new database.
[0061] For example, before synchronizing the master database's historical data with the slave database, you can first determine whether the table structures of the master and slave databases are consistent. If they are consistent, the master database's historical data is directly synchronized to the slave database. If they are inconsistent, incremental data synchronization technology (change data capture, abbreviated as CDC, subscribing to MySQL Binlog or Oracle Golden Gate) is used to migrate the master database's historical data to the slave database.
[0062] After the synchronization of the historical data of the main database is completed, the state of the state switch is switched from the first state to the second state.
[0063] As another embodiment, when the state switching switch is in the second state, data is queried or updated on the master database, and the query or update result is used as data update information, thereby starting an asynchronous thread, and initiating data query or update on the slave database based on the asynchronous thread.
[0064] After the master database and the slave database execute data update operations, the data update information of both databases is sent to an access interface (such as an access interface such as DAO_COMPARE topic) through a message queue (MQ).
[0065] For example, if an error is reported during a data query or update on the master database, a transaction rollback is executed to resolve the data error and restore the data to its last correct state. If an error is reported during a data query or update on the slave database, an MQ error message is sent to an exception logging table (e.g., a message in the DB_EXCEPTION topic to record the exception).
[0066] After the master database and the slave database complete data updating, the state of the state switch is switched from the second state to the third state.
[0067] As another embodiment, when the state switch is in the third state, synchronous reading and writing are performed on the master database and the slave database, so that the state of the state switch is switched from the third state to the fourth state after the synchronous reading and writing is completed. The synchronous reading and writing can be performed in a parallel reading and writing manner.
[0068] As another embodiment, when the state switching switch is in the fourth state, data query is performed on the master database or the slave database through a pre-configured read and write rate to verify the query result. When the verification is successful, the state of the state switching switch is switched from the fourth state to the fifth state.
[0069] Exemplarily, the read / write rate corresponds to the master database and the slave database. When the read / write rate is less than a preset threshold, a data query is performed on the master database. When the read / write rate is not less than the preset threshold, a data query is performed on the slave database. For example, if the read / write rate is 0%, a data query is performed on the master database. If the read / write rate is 100%, a data query is performed on the slave database. By setting the read / write rate, data queries can be selectively performed on either the master database or the slave database.
[0070] For example, in this embodiment, when a data query needs to be performed on the slave database, if the preset threshold is 5%, the read / write rate can be set to 10% to perform the data query operation on the slave database.
[0071] After selectively querying data on the master or slave database in this manner, it is also possible to determine whether the data can be queried normally at the current read and write rate. If so, the query result is determined to be successfully verified, and the state of the status switch can be switched from the fourth state to the fifth state. If not, the query result is determined to have failed verification, and the state of the status switch can be switched back from the fourth state to the third state, or maintained in the fourth state to investigate the cause of the verification failure. A data query period can also be set, and data query operations can be periodically performed within the data query period. The data query period can be 2 days, 3 days, 4 days, etc., and is not limited here.
[0072] In another embodiment, when the state switch is in the fifth state, the master database switches from the first database to the second database. Synchronous read and write operations are performed on the master database, and synchronous write operations are performed on the slave database based on the master database to complete the data synchronization process in the fifth state. Thereafter, the state of the state switch can be switched from the fifth state to the sixth state.
[0073] For example, if an error occurs during synchronous read and write operations on the master database, a transaction rollback is performed to resolve the data error and restore the data to its last correct state. If an error occurs during synchronous write operations on the slave database, an MQ error message is sent to the DB_EXCEPTION topic message.
[0074] As another implementation, when the state switch is in the sixth state, writing data to the slave database is stopped, and data synchronization between the master database and the slave database is completed.
[0075] In some embodiments, determining a state switching operation of a state switching switch based on an execution result of a data synchronization policy includes:
[0076] If the data synchronization policy corresponding to the first state is executed, the state of the state switch is switched to the next state;
[0077] If the data synchronization policy corresponding to one of the second state, the third state, the fourth state, and the fifth state is executed, it is determined whether the execution result meets the preset conditions. If so, the state of the state switch is switched to the next state; if not, the state of the state switch is switched to the previous state;
[0078] If the data synchronization policy corresponding to the sixth state is executed, the state of the state switching switch is maintained.
[0079] Specifically, see Figure 3 , Figure 3 A schematic diagram of a flow chart for controlling a state switching switch to switch states in a data synchronization method provided in an embodiment of the present application. Figure 3 The left side shows the status bar of the status switch, and the right side shows the data synchronization policy executed in this state.
[0080] In the first state, the first database serves as the primary database. After synchronizing the historical data of the first database to the second database, the control state switch enters the second state.
[0081] In the second state, the first database continues to serve as the primary database, obtaining data update information from the first database and initiating an asynchronous thread based on the data update information to update data in the second database. Subsequently, a determination is made as to whether the execution result meets a preset condition. If so, the state switch is controlled to enter the third state; if not, the state switch is controlled to revert to the first state.
[0082] In the third state, the first database continues to be used as the primary database, and synchronous reading and writing are performed on the first and second databases. Afterwards, it is determined whether the execution result meets the preset conditions. If so, the state switch is controlled to enter the fourth state. If not, the state switch is controlled to revert to the second state.
[0083] In the fourth state, the first database continues to be used as the primary database, and data queries are performed on the primary database or the secondary database at the configured read and write rates. After the query result is successfully verified, it is determined whether the execution result meets the preset conditions. If so, the state switch is controlled to enter the fifth state. If not, the state switch is controlled to revert to the third state. When the query result is verified and identified, the state switch is also controlled to revert to the third state.
[0084] In the fifth state, the primary database is switched to the second database, synchronous write operations are performed on the first database, and synchronous read and write operations are performed on the second database. Subsequently, a determination is made as to whether the execution results meet preset conditions. If so, the state switch is controlled to enter the sixth state. If not, the state switch is controlled to revert to the fourth state, and the primary database is switched back to the first database.
[0085] In the sixth state, data writing to the first database is stopped, and the data synchronization process ends.
[0086] In this embodiment, by executing the data synchronization strategy corresponding to each state and judging the state switching operation of the state switching switch based on the execution result, the process flow is controlled to transfer to the next stage when the execution result of each stage is normal, thereby smoothly performing data synchronization operations on different databases. When the execution result of a certain stage is abnormal, the process can be controlled to roll back to handle the exception, thereby avoiding process interruption or error or data confusion during the data synchronization process.
[0087] In some embodiments, determining whether the execution result satisfies a preset condition includes:
[0088] Get error information from the database;
[0089] Determine the error rate according to the error information, and determine the error rate as the execution result;
[0090] If the error rate is less than the preset threshold, it is determined that the execution result meets the preset conditions;
[0091] If the error rate is not less than the preset threshold, it is determined that the execution result does not meet the preset conditions.
[0092] Specifically, when executing the data synchronization policy for each state, if an error is reported, the error message is recorded by sending the MQ message to the DB_EXCEPTION topic message. During the data synchronization policy execution, the DB_EXCEPTION topic message can also be checked to see if there is an error record corresponding to that state. If so, the ratio of the number of data errors to the total number of data synchronized is calculated as the error rate. If not, the execution result can be directly determined to meet the preset conditions.
[0093] A preset threshold is also set to determine whether the execution result meets the preset conditions. The preset threshold can be a value such as 10%, 5%, or 2%. The specific value can be selected according to actual needs and is not limited here. When the error rate is less than the preset threshold, it means that there are very few or no errors (the error rate is 0), and it can be determined that the execution result meets the preset conditions. If not, it is determined that the execution result does not meet the preset conditions.
[0094] It is understandable that the error rate can be obtained by counting the total error data of the master database and the slave database, or by counting the two separately. The specific implementation method can be set according to actual needs.
[0095] In some embodiments, after obtaining the error information from the database, the method further includes:
[0096] Perform data compensation processing on the slave database according to the error information;
[0097] Record the number of compensations and determine whether the number of compensations reaches the preset number;
[0098] If so, proceed to the manual compensation process.
[0099] The error information recorded in each state includes error information from the slave database. The slave database is the second database in the second state to the fourth state, and is the first database in the fifth state.
[0100] In each state, data compensation processing is performed on the corresponding error information, wherein the data compensation processing includes: adding missing data from the database, or deleting duplicate data from the database, etc.
[0101] After each data compensation attempt, a check is performed to determine whether the compensation was successful. If the compensation was successful, the next data compensation process is not performed. If the compensation failed, the next data compensation process is performed. When the compensation process is repeated a preset number of times, the automatic data compensation process for the slave database is stopped, and the manual compensation process is switched to manual compensation. The preset number of times can be 2, 3, 4, or 5, and can be set according to actual needs.
[0102] In this embodiment, data consistency between the master database and the slave database is ensured by performing compensation processing on the data, thereby improving the efficiency and accuracy of data synchronization.
[0103] In some embodiments, determining whether the execution result satisfies a preset condition includes:
[0104] Perform a full comparison of the data in the master database and the slave database to obtain a probability of consistency, which is then used as the execution result.
[0105] If the probability of consistency is less than the preset probability, it is determined that the execution result does not meet the preset conditions, and data repair processing is performed on the slave database based on the master database;
[0106] If the probability of the comparison being consistent is not less than the preset probability, it is determined that the execution result meets the preset conditions.
[0107] Among them, after executing the data synchronization strategy corresponding to each state, the data of the master database and the slave database are fully compared to filter out inconsistent data, and then the ratio of the amount of inconsistent data to the total amount of data of the two is used as the probability of comparison inconsistency, so as to obtain the probability of comparison consistency based on the probability of comparison inconsistency.
[0108] For example, a preset probability is set to determine whether the execution result corresponding to each state meets the preset conditions. The preset probability can be 95%, 96%, 97%, 98%, 99%, etc., and can be set according to actual needs. If the probability of consistency is not less than the preset probability, it means that the execution result meets the preset conditions. If the probability of consistency is not less than the preset probability, it means that the execution result does not meet the preset conditions.
[0109] Furthermore, data repair can be performed on the slave database based on the data in the master database, specifically repairing inconsistent data. The slave database is the second database in the second to fourth states, and is the first database in the fifth state.
[0110] A data repair cycle can also be set to periodically repair the data in the database, where the data repair cycle can be 1 day, 1 week, etc. During data repair, it can be automatically repaired based on inconsistent data or manually repaired, and the specific implementation method is not limited here.
[0111] For example, in the second state, only the data update information of the master database and the slave database can be compared to determine the probability of inconsistency between the two. Then, when the probability of consistency is small, data repair processing is performed on the data of the slave database based on the data update information of the master database, thereby reducing the computational complexity of data comparison and improving data comparison efficiency.
[0112] In some embodiments, determining whether the execution result satisfies a preset condition includes:
[0113] Determine the performance of executing the data synchronization strategy from the database, and determine the performance as the execution result;
[0114] If the performance meets the preset performance, it is determined that the execution result meets the preset conditions;
[0115] If the performance does not meet the preset performance, it is determined that the execution result does not meet the preset conditions.
[0116] Performance can be assessed by measuring the time and load of the slave database when executing data synchronization policies. For example, longer time means lower performance, shorter time means higher performance, higher load means lower performance, and lower load means higher performance.
[0117] The performance of the slave database is evaluated by setting a preset time and / or load. The evaluation standard is that when the time is short and the load is low, the performance of the slave database is determined to be high and can reach the preset performance. The execution result meets the preset conditions; otherwise, the preset conditions are not met.
[0118] In this embodiment, by setting the evaluation conditions for the performance of data synchronization from the database, data synchronization failures can be avoided, thereby ensuring the security and reliability of data synchronization.
[0119] As described above, two or more of the aforementioned execution results can also be combined to determine whether the execution result meets the preset conditions. Taking the combination of the three as an example, for example, if the error rate is less than a preset threshold, the probability of comparison consistency is not less than a preset threshold, and the performance of the slave database meets the preset performance, the execution result is determined to meet the preset conditions. Otherwise, the execution result is determined to not meet the preset conditions.
[0120] As can be seen from the above, the data synchronization method proposed in the embodiment of the present invention can have only one master database at each stage of data synchronization, and execute the data synchronization strategy corresponding to the current state on the slave database based on the master database, and can also perform data compensation and repair when an error occurs during the execution process, thereby ensuring the consistency of the data synchronization results. In addition, after the execution of the data synchronization strategy is completed, the data of the master database and the slave database are fully compared to control the state switching of the state switch and repair the inconsistent data, thereby ensuring the security and reliability when the database is switched. In addition, the workload of manual comparison is reduced by automatically comparing the databases, thereby improving the efficiency of data comparison. Furthermore, when the execution result does not meet the preset conditions, it can also fall back to the previous step to deal with various emergencies, avoiding the need to stop the data synchronization service, thereby avoiding data loss or data confusion during the database switching process.
[0121] In one embodiment, a data synchronization device is also provided. Figure 4 , Figure 4This is a structural diagram of a data synchronization device 200 provided in an embodiment of the present application. The data synchronization device 200 is applied to an electronic device, and the data synchronization device 200 includes:
[0122] A state identification module 201 is used to obtain the current state of the state switch, wherein different states of the state switch correspond to different data synchronization strategies;
[0123] A policy matching module 202 is configured to determine a data synchronization policy, a master database, and a slave database corresponding to a current state, wherein the master database and the slave database are determined from a first database and a second database corresponding to different versions of the same application;
[0124] A policy execution module 203 is configured to execute a data synchronization policy on a slave database based on the master database;
[0125] The state switching module 204 is configured to determine a state switching operation of the state switching switch based on the execution result of the data synchronization policy. The state switching operation includes switching to the next state or switching to the previous state.
[0126] In some embodiments, the state of the state switch includes a first state, a second state, a third state, a fourth state, a fifth state, and a sixth state, and the version of the first database is earlier than the version of the second database; the policy matching module 202 is further configured to:
[0127] If the current state is one of the first state, the second state, the third state, and the fourth state, the first database is determined as the master database, and the second database is determined as the slave database;
[0128] If the current state is the fifth state or the sixth state, the second database is determined as the master database, and the first database is determined as the slave database.
[0129] In some embodiments, the policy execution module 203 is further configured to:
[0130] If the current state is the first state, the historical data of the master database is synchronized to the slave database;
[0131] If the current state is the second state, the data update information of the master database is obtained, and an asynchronous thread is initiated based on the data update information to perform a data update operation on the slave database;
[0132] If the current state is the third state, synchronous read and write operations are performed on the master database and the slave database;
[0133] If the current state is the fourth state, obtain the configured read and write rates, and perform data query operations on the master database or the slave database according to the read and write rates;
[0134] If the current state is the fifth state, perform synchronous write operations on the slave database and synchronous read and write operations on the master database;
[0135] If the current state is the sixth state, the data writing operation to the slave database is stopped.
[0136] In some embodiments, the state switching module 204 is further configured to:
[0137] If the data synchronization policy corresponding to the first state is executed, the state of the state switch is switched to the next state;
[0138] If the data synchronization policy corresponding to one of the second state, the third state, the fourth state, and the fifth state is executed, it is determined whether the execution result meets the preset conditions. If so, the state of the state switch is switched to the next state; if not, the state of the state switch is switched to the previous state;
[0139] If the data synchronization policy corresponding to the sixth state is executed, the state of the state switching switch is maintained.
[0140] In some embodiments, the state switching module 204 is further configured to:
[0141] Get error information from the database;
[0142] Determine the error rate according to the error information, and determine the error rate as the execution result;
[0143] If the error rate is less than the preset threshold, it is determined that the execution result meets the preset conditions;
[0144] If the error rate is not less than the preset threshold, it is determined that the execution result does not meet the preset conditions.
[0145] In some embodiments, after obtaining the error information from the database, the policy execution module 203 is further configured to:
[0146] Perform data compensation processing on the slave database according to the error information;
[0147] Record the number of compensations and determine whether the number of compensations reaches the preset number;
[0148] If so, proceed to the manual compensation process.
[0149] In some embodiments, the state switching module 204 is further configured to:
[0150] Perform a full comparison of the data in the master database and the slave database to obtain a probability of consistency, which is then used as the execution result.
[0151] If the probability of consistency is less than the preset probability, it is determined that the execution result does not meet the preset conditions, and data repair processing is performed on the slave database based on the master database;
[0152] If the probability of the comparison being consistent is not less than the preset probability, it is determined that the execution result meets the preset conditions.
[0153] It should be noted that the data synchronization device 200 provided in the embodiment of the present application belongs to the same concept as the data synchronization method in the above embodiment. Any method provided in the data synchronization method embodiment can be implemented through the data synchronization device 200. The specific implementation process is detailed in the data synchronization method embodiment and will not be repeated here.
[0154] As can be seen from the above, the data synchronization device proposed in the embodiment of the present application can have only one master database at each stage of data synchronization, and execute the data synchronization strategy corresponding to the current state on the slave database based on the master database, and can also perform data compensation and repair when an error occurs during the execution process, thereby ensuring the consistency of the data synchronization results. In addition, after the execution of the data synchronization strategy is completed, the data of the master database and the slave database are fully compared to control the state switching of the state switch and repair the inconsistent data, thereby ensuring the security and reliability when the database is switched. In addition, the workload of manual comparison is reduced by automatically comparing the databases, thereby improving the efficiency of data comparison. Furthermore, when the execution result does not meet the preset conditions, it can also fall back to the previous step to deal with various emergencies, avoiding the need to stop the data synchronization service, thereby avoiding data loss or data confusion during the database switching process.
[0155] The embodiment of the present application also provides an electronic device, which can be a tablet computer, a desktop computer, or a server. Figure 5 As shown, Figure 5 Schematic diagram of the structure of an electronic device provided in an embodiment of the present application. The electronic device 300 includes a processor 301 having one or more processing cores, a memory 302 having one or more computer-readable storage media, and a computer program stored in the memory 302 and executable on the processor. The processor 301 is electrically connected to the memory 302. It will be understood by those skilled in the art that the electronic device structure shown in the figure does not constitute a limitation of the electronic device, and may include more or fewer components than shown, or combine certain components, or arrange the components differently.
[0156] The processor 301 is the control center of the electronic device 300. It uses various interfaces and lines to connect various parts of the entire electronic device 300. By running or loading software programs and / or modules stored in the memory 302 and calling data stored in the memory 302, it executes various functions of the electronic device 300 and processes data, thereby monitoring the electronic device 300 as a whole.
[0157] In the embodiment of the present application, the processor 301 in the electronic device 300 loads instructions corresponding to one or more application processes into the memory 302 according to the following steps, and the processor 301 runs the application stored in the memory 302 to implement various functions:
[0158] Get the current state of the state switch, where different states of the state switch correspond to different data synchronization strategies;
[0159] Determine a data synchronization strategy, a master database, and a slave database corresponding to the current state, wherein the master database and the slave database are determined from a first database and a second database corresponding to different versions of the same application;
[0160] Execute data synchronization strategy on the slave database based on the master database;
[0161] The state switching operation of the state switching switch is determined based on the execution result of the data synchronization strategy. The state switching operation includes switching to the next state or switching to the previous state.
[0162] The specific implementation of the above operations can be found in the previous embodiments and will not be repeated here.
[0163] In the above embodiments, the description of each embodiment has its own focus. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0164] As can be seen from the above, the electronic device provided by this embodiment can have only one master database at each stage of data synchronization, and execute the data synchronization strategy corresponding to the current state on the slave database based on the master database, and can also perform data compensation and repair when an error occurs during the execution process, thereby ensuring the consistency of the data synchronization results. In addition, after the execution of the data synchronization strategy is completed, the data of the master database and the slave database are fully compared to control the state switching of the state switch and repair the inconsistent data, thereby ensuring the security and reliability when the database is switched. In addition, the workload of manual comparison is reduced by automatically comparing the databases, thereby improving the efficiency of data comparison. Furthermore, when the execution result does not meet the preset conditions, it can also fall back to the previous step to deal with various emergencies, avoiding the need to stop the data synchronization service, thereby avoiding data loss or data confusion during the database switching process.
[0165] Those skilled in the art will appreciate that all or part of the steps in the various methods of the above embodiments may be accomplished by instructions, or by controlling related hardware through instructions. The instructions may be stored in a computer-readable storage medium and loaded and executed by a processor.
[0166] To this end, the embodiments of the present application provide a computer-readable storage medium. Persons skilled in the art will appreciate that all or part of the steps in the above-described embodiment methods can be implemented by instructing related hardware through a program. The program can be stored in a computer-readable storage medium. When executed, the program includes the following steps:
[0167] Get the current state of the state switch, where different states of the state switch correspond to different data synchronization strategies;
[0168] Determine a data synchronization strategy, a master database, and a slave database corresponding to the current state, wherein the master database and the slave database are determined from a first database and a second database corresponding to different versions of the same application;
[0169] Execute data synchronization strategy on the slave database based on the master database;
[0170] The state switching operation of the state switching switch is determined based on the execution result of the data synchronization strategy. The state switching operation includes switching to the next state or switching to the previous state.
[0171] The specific implementation of the above operations can be found in the previous embodiments and will not be repeated here.
[0172] The above-mentioned storage medium may be ROM / RAM, a magnetic disk, an optical disk, etc. Since the computer program stored in the storage medium can execute the steps of any of the data synchronization methods provided in the embodiments of the present application, the beneficial effects that can be achieved by any of the data synchronization methods provided in the embodiments of the present application can be achieved. Please refer to the previous embodiments for details and will not be repeated here.
[0173] The above is a detailed introduction to a data synchronization method, device, storage medium and electronic device provided in the embodiments of the present application. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea; at the same time, for technical personnel in this field, based on the ideas of the present application, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
Claims
1. A data synchronization method, characterized in that: The method comprises: Obtaining a current state of a state switch, wherein different states of the state switch correspond to different data synchronization strategies, and the states of the state switch include a first state, a second state, a third state, a fourth state, a fifth state, and a sixth state; Determining a data synchronization strategy, a master database, and a slave database corresponding to the current state, wherein the master database and the slave database are determined from a first database and a second database corresponding to different versions of the same application, and the version of the first database is earlier than the version of the second database; Executing the data synchronization strategy on the slave database based on the master database; Determining a state switching operation of the state switching switch based on an execution result of the data synchronization strategy, wherein the state switching operation includes switching to a next state or switching to a previous state; The step of determining the master database and slave database corresponding to the current state includes: If the current state is one of the first state, the second state, the third state, and the fourth state, determining the first database as the master database and determining the second database as the slave database; If the current state is the fifth state or the sixth state, determining the second database as the master database and determining the first database as the slave database; The executing the data synchronization strategy on the slave database based on the master database includes: If the current state is the first state, synchronizing the historical data of the master database to the slave database; If the current state is the second state, acquiring data update information of the master database, and initiating an asynchronous thread based on the data update information to perform a data update operation on the slave database; If the current state is the third state, performing synchronous read and write operations on the master database and the slave database; If the current state is the fourth state, obtaining a configured read / write rate, and performing a data query operation on the master database or the slave database according to the read / write rate; If the current state is the fifth state, performing a synchronous write operation on the slave database and performing a synchronous read and write operation on the master database; If the current state is the sixth state, the data writing operation on the slave database is stopped.
2. The data synchronization method according to claim 1, wherein: The determining the state switching operation of the state switching switch based on the execution result of the data synchronization strategy includes: If the data synchronization policy corresponding to the first state is executed, the state of the state switch is switched to the next state; If the data synchronization policy corresponding to one of the second state, the third state, the fourth state, and the fifth state is executed, determining whether the execution result meets the preset condition, if so, switching the state of the state switch to the next state, if not, switching the state of the state switch to the previous state; If the data synchronization policy corresponding to the sixth state is executed, the state of the state switching switch is maintained.
3. The data synchronization method according to claim 2, wherein: Determining whether the execution result meets the preset conditions includes: Obtaining the error information from the database; Determine an error rate according to the error information, and determine the error rate as the execution result; If the error rate is less than a preset threshold, determining that the execution result meets the preset condition; If the error rate is not less than the preset threshold, it is determined that the execution result does not meet the preset condition.
4. The data synchronization method according to claim 3, characterized in that: After obtaining the error information from the database, the method further includes: Performing data compensation processing on the slave database according to the error information; Recording the number of compensations and determining whether the number of compensations reaches a preset number; If so, proceed to the manual compensation process.
5. The data synchronization method according to claim 2, characterized in that: Determining whether the execution result meets the preset conditions includes: Performing a full comparison of the data in the master database and the data in the slave database to obtain a probability of consistency between the comparisons, and determining the probability of consistency between the comparisons as the execution result; If the probability of the comparison being consistent is less than a preset probability, determining that the execution result does not meet the preset condition, and performing data repair processing on the slave database based on the master database; If the probability of the comparison being consistent is not less than the preset probability, it is determined that the execution result meets the preset condition.
6. A data synchronization device, characterized in that: include: A state identification module is used to obtain the current state of the state switching switch, wherein different states of the state switching switch correspond to different data synchronization strategies, and the states of the state switching switch include a first state, a second state, a third state, a fourth state, a fifth state, and a sixth state; a policy matching module, configured to determine a data synchronization policy, a master database, and a slave database corresponding to the current state, wherein the master database and the slave database are determined from a first database and a second database corresponding to different versions of the same application, the version of the first database being earlier than the version of the second database; A policy execution module, configured to execute the data synchronization policy on the slave database based on the master database; A state switching module, configured to determine a state switching operation of the state switching switch based on an execution result of the data synchronization strategy, wherein the state switching operation includes switching to a next state or switching to a previous state; The strategy matching module is used to execute: If the current state is one of the first state, the second state, the third state, and the fourth state, determining the first database as the master database and determining the second database as the slave database; If the current state is the fifth state or the sixth state, determining the second database as the master database and determining the first database as the slave database; The policy execution module is used to execute: If the current state is the first state, synchronizing the historical data of the master database to the slave database; If the current state is the second state, acquiring data update information of the master database, and initiating an asynchronous thread based on the data update information to perform a data update operation on the slave database; If the current state is the third state, performing synchronous read and write operations on the master database and the slave database; If the current state is the fourth state, obtaining a configured read / write rate, and performing a data query operation on the master database or the slave database according to the read / write rate; If the current state is the fifth state, performing a synchronous write operation on the slave database and performing a synchronous read and write operation on the master database; If the current state is the sixth state, the data writing operation on the slave database is stopped.
7. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed on a computer, the computer is enabled to execute the data synchronization method according to any one of claims 1 to 5.
8. An electronic device comprising a processor and a memory, wherein the memory stores a computer program, wherein: The processor is configured to execute the data synchronization method according to any one of claims 1 to 5 by calling the computer program.
Citation Information
Patent Citations
Database switching method, device and system, electronic device and storage medium
CN110442650A
Database switching method, device and equipment
CN110471906A