Summary of the invention
In view of this, the purpose of the embodiment of the present invention is to propose a kind of method of data synchronization and device, it can be for notData are carried out with database node or different types of database node to synchronize, and maintain the data of global table in database consistentProperty.
Based on above-mentioned purpose, the one side of the embodiment of the present invention provides a kind of method of data synchronization, is applied to database,The following steps are included:
Configuration database middleware simultaneously makes database middleware enabling table broadcast mechanism;
Database middleware is set to receive the database statement of data manipulation language;
Global table logic whether is needed to be implemented according to configuration determination database statement;
When database statement needs to be implemented global table logic, database statement table is broadcast to institute using table broadcast mechanismThere is database node;
When database statement does not need to execute global table logic, all database nodes are sent simultaneously for database statementExecute database statement.
In some embodiments, table broadcast mechanism includes:
A database host node is determined in all database nodes, and other database nodes are determined as databaseFrom node;
Database middleware is set to send the broadcast about database statement to database host node;
Make database host node that database statement table is broadcast to all databases from node.
In some embodiments, make database host node that database statement table is broadcast to all databases from node packetIt includes:
Saving database host node includes the binary message file of database statement, and to all databases from nodePush binary message file;
Make all databases that binary message file be dumped to all databases by dedicated worker thread from nodeFrom the respective relaying file of node;
Make database host node and all databases from node respectively according to binary message file and respective relaying textPart executes database statement.
In some embodiments, binary message file is dumped into corresponding relaying text from node in each databaseAfter in part, dedicated worker thread is recorded in binary message file;All numbers are recorded in binary message fileAfter all having carried out unloading from node according to library, database host node sends to all databases from node and executes database statementBroadcast.
In some embodiments, when dedicated worker thread cannot read binary message file, corresponding dataLibrary enters suspend mode from node and waits new broadcast.
In some embodiments, dedicated worker thread binary message file is written in a serial fashion correspondingRelay file.
In some embodiments, judge whether database statement needs to be implemented global table logic and include:
Judge whether the type of database statement is one of the following: insertion is updated, is deleted, and is then continued in this way;
Whether the Object table type for judging database statement is global table, is then continued in this way;
Judge whether database statement is configured to enable single node insertion, is that judgement needs to be implemented global table logic, noThen determine not needing.
In some embodiments, configuration database middleware includes:
The table label for judging table type is added in configuration file into database middleware;
The single node insertion attribute for judging whether enabling table broadcast mechanism is added into table label.
In some embodiments, database middleware Mycat, database MySQL.
The another aspect of the embodiment of the present invention additionally provides a kind of data synchronization unit, including processor and memory, instituteIt states memory and is stored with the executable instruction of processor, described instruction realizes the above method when executed.
The another aspect of the embodiment of the present invention additionally provides a kind of computer readable storage medium, computer-readable storageMedia storage has computer program, and above-mentioned method is executed when computer program is executed by processor.
The another aspect of the embodiment of the present invention, additionally provides a kind of computer program product, and computer program product includesThe calculation procedure being stored on computer readable storage medium, calculation procedure include instruction, when executed by the processor, are madeComputer executes the above method.
The present invention has following advantageous effects: method of data synchronization and device provided in an embodiment of the present invention pass throughUsing enabling table broadcast mechanism, receive data manipulation language database statement, judge whether database statement needs to be implemented entirelyOffice's table logic will count when using table broadcast mechanism database statement table being broadcast to all database nodes when needing, do not neededAll database nodes are sent to according to library sentence and execute the technical solution of database statement, can be directed to disparate databases nodeOr different types of database node carries out data and synchronizes, and maintains the data consistency of global table in database.
Specific embodiment
To make the objectives, technical solutions, and advantages of the present invention clearer, below in conjunction with specific embodiment, and referenceThe embodiment of the present invention is further described in attached drawing.
It should be noted that all statements for using " first " and " second " are for differentiation two in the embodiment of the present inventionThe non-equal entity of a same names or non-equal parameter, it is seen that " first " " second " only for the convenience of statement, does not answerIt is interpreted as the restriction to the embodiment of the present invention, subsequent embodiment no longer illustrates this one by one.
Based on above-mentioned purpose, the first aspect of the embodiment of the present invention, disparate databases section can be directed to by proposing one kindPoint or different types of database node carry out the embodiment of the synchronous method of data.Fig. 2 shows be number provided by the inventionAccording to the flow diagram of the embodiment of synchronous method.
The method of data synchronization is optionally applied to database, comprising the following steps:
Step S201, configuration database middleware simultaneously make database middleware enabling table broadcast mechanism;
Step S203 makes database middleware receive the database statement of data manipulation language;
Whether step S205 needs to be implemented global table logic according to configuration determination database statement;
Step S207, when database statement needs to be implemented global table logic, using table broadcast mechanism by database statementTable is broadcast to all database nodes;
Step S209 sends all numbers for database statement when database statement does not need to execute global table logicAccording to library node and execute database statement.
Those of ordinary skill in the art will appreciate that realizing all or part of the process in above-described embodiment method, Ke YitongComputer program is crossed to instruct related hardware and complete, the program can be stored in a computer-readable storage medium,The program is when being executed, it may include such as the process of the embodiment of above-mentioned each method.Wherein, the storage medium can for magnetic disk,CD, read-only memory (ROM) or random access memory (RAM) etc..The embodiment of the computer program, Ke YidaThe effect identical or similar to corresponding aforementioned any means embodiment.
The embodiment of the present invention will be acted on by changing the global table logic of Mycat (database middleware) when DML is accessedIt is changed to the operation to host node in the operation of all nodes, then the data between each node are completed by table broadcast mechanism and are synchronized, is comeRealize the data consistency between each node.
In some embodiments, table broadcast mechanism includes:
A database host node is determined in all database nodes, and other database nodes are determined as databaseFrom node;
Database middleware is set to send the broadcast about database statement to database host node;
Make database host node that database statement table is broadcast to all databases from node.
In some embodiments, make database host node that database statement table is broadcast to all databases from node packetIt includes:
Saving database host node includes the binary message file of database statement, and to all databases from nodePush binary message file;
Make all databases that binary message file be dumped to all databases by dedicated worker thread from nodeFrom the respective relaying file of node;
Make database host node and all databases from node respectively according to binary message file and respective relaying textPart executes database statement.
In addition, disclosed method is also implemented as the computer program executed by CPU according to embodiments of the present invention, it shouldComputer program may be stored in a computer readable storage medium.When the computer program is executed by CPU, the present invention is executedThe above-mentioned function of being limited in method disclosed in embodiment.Above method step and system unit also can use controller andFor storing so that controller realizes that the computer readable storage medium of the computer program of above-mentioned steps or Elementary Function is realized.
As shown in figure 3, in embodiments of the present invention, realizing that table broadcast mechanism includes three steps:
(1) user sends DML request, and host receives order, sends and broadcasts to host node.Host node receives message,By Message Record to binary file, and other are transmitted the message to from node;
(2) message is read from node, and message is written to the relaying log of oneself.
(3) event in relaying log is reformed from node, and changes the data of oneself.
In addition, typically, the embodiment of the present invention discloses described database middleware and node etc. can be whole for various electronicsEnd equipment, such as mobile phone, personal digital assistant (PDA), tablet computer (PAD), smart television etc., are also possible to large-scale terminal and setIt is standby, such as server, therefore protection scope disclosed by the embodiments of the present invention should not limit as certain certain types of device, setIt is standby.The embodiment of the present invention discloses the client and can be is answered with the combining form of electronic hardware, computer software or bothFor in any one of the above electric terminal equipment.
In some embodiments, binary message file is dumped into corresponding relaying text from node in each databaseAfter in part, dedicated worker thread is recorded in binary message file;All numbers are recorded in binary message fileAfter all having carried out unloading from node according to library, database host node sends to all databases from node and executes database statementBroadcast.
Host is broadcasted after receiving request to host node transmission, and host node receives message and simultaneously records binary file.EachBefore affairs more new data is completed, host node records these changes in binary file.MySQL thread serially writes affairsEnter binary file, after the completion of event write-in, host node notifies storage engines to submit affairs and send to other from node wideIt broadcasts.
It host node as described herein and can be volatile memory or nonvolatile memory from node, or can wrapInclude both volatile memory and nonvolatile memory.As an example and not restrictive, nonvolatile memory can wrapInclude read-only memory (ROM), programming ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM(EEPROM) or flash memory.Volatile memory may include random access memory (RAM), which can serve as outerPortion's cache memory.As an example and not restrictive, RAM can be obtained in a variety of forms, such as synchronous random access memory(DRAM), dynamic ram (DRAM), synchronous dram (SDRAM), double data rate SDRAM (DDR SDRAM), enhancing SDRAM(ESDRAM), synchronization link DRAM (SLDRAM) and directly Rambus RAM (DRRAM).The storage of disclosed aspect is setThe standby memory for being intended to including but not limited to these and other suitable type.
In some embodiments, when dedicated worker thread cannot read binary message file, corresponding dataLibrary enters suspend mode from node and waits new broadcast.In some embodiments, dedicated worker thread in a serial fashion willCorresponding relaying file is written in binary message file.
Message is received from node, the binary file of host node is copied to the relaying log of oneself.First from nodeStart a worker thread (I/O thread), I/O thread opens a common connection on the primary node, then starts binary systemUnloading.Binary system loading program reads message event from the binary file of host node, if not new message, it can sleepIt sleeps and host node is waited to send new broadcast.SQL from thread from relaying log read message event, and reset event therein withThe data from node are updated, keep it consistent with the data in host node.
In some embodiments, judge whether database statement needs to be implemented global table logic and include:
Judge whether the type of database statement is one of the following: insertion is updated, is deleted, and is to continue next step;
Whether the Object table type for judging database statement is global table, is to continue next step;
Judge whether database statement is configured to enable single node insertion, is that judgement needs to be implemented global table logic, noThen determine not needing.
As shown in figure 4, the table broadcast mechanism in Mycat in the execution process Mycat of table broadcast mechanism is to be directed to the overall situationThe DML of table is operated and is proposed, and only in writeOneNode=true, its function can just come into force, it is therefore desirable to be increasedNew SQL executes logic.
As shown in figure 4, Mycat carries out following judgement to the SQL statement received:
(1) whether the type of SQL statement is insert, update or delete;
(2) whether the table being inserted into is global table;
It (3) whether is single node write-in, i.e. writeOneNode=true.
Only meeting simultaneously there are three condition makes, and original multinode insertions function is just revised as single node insertion, is enabledTable broadcast capability.
Various illustrative logical blocks, module, circuit and algorithm steps in conjunction with described in disclosure herein can be implementedFor the combination of electronic hardware, computer software or both.In order to clearly demonstrate this interchangeability of hardware and software,General description has been carried out to it with regard to the function of various exemplary components, square, module, circuit and step.This function isSoftware is implemented as also to be implemented as hardware depending on concrete application and be applied to the design constraint of whole system.This fieldTechnical staff can realize the function in various ways for every kind of concrete application, but determine should not be by for this realizationIt is construed to lead to be detached from range disclosed by the embodiments of the present invention.
In some embodiments, configuration database middleware includes:
The table label for judging table type is added in configuration file into database middleware;
The single node insertion attribute for judging whether enabling table broadcast mechanism is added into table label.
Table broadcast mechanism in Mycat is for global table.Configuring Mycat is to have increased an attribute newly for global table,Change the execution logic of original SQL statement when the attribute value is true.Mycat receives DML order and sends extensively to host nodeIt broadcasts, host node, which executes, orders and save binary message file simultaneously to from node PUSH message, and it is same to receive broadcast from nodeWalk the data of host node.When there is node exception when modifying data in the Mycat that the embodiment of the present invention realizes table broadcast mechanismJournal file, the synchrodata that storage message can be actively read after releasing extremely, ensure that strong consistency.
Specifically, configuration file is read first when Mycat starts, judged according to the setting information of wherein table labelThe type of table.Increase an attribute " writeOneNode " in the table label of configuration file schema.xml as enablingThe mark of table broadcast mechanism just enables table broadcast capability when the type of logical table is global table i.e. " type=global ".NewlyThe attribute of increasing is Boolean type, and default value is " false ".As " writeOneNode=true ", the SQL of filtering DML operationSentence acts on host node by table broadcast;As " writeOneNode=false ", the SQL statement of DML operation is according to originalBeginning logical action is in all nodes.
When on the other hand, such as by java application accessing XML, Java object is first converted to XML file(marshall), then by the content transformation in XML file at corresponding Java object (unmarshal), which needs to useJAXB (the Java architecture of XML binding) explains to mark.Therefore it needs to modify to explain class Schemas.java, inside itThe attribute setting that " writeOneNode " is added in class, make content unmarshal in configuration file " schema.xml " atIt include the corresponding information of " writeOneNode " field when the object of Table type.Initialize the class of XML configuration fileIn XmlToYaml.java, increase the function of initializing to parameter " writeOneNode ", the load configuration text in startup programPart initializes the relevant parameter being arranged in configuration file, by parsing XML configuration file in each interior of SchemasThis class object is instantiated in portion's static class, the Key-value key-value pair configured required for finally being stored again by HashMap,This class instance object is externally provided.
The step of method in conjunction with described in disclosure herein or algorithm, can be directly contained in hardware, be held by processorIn capable software module or in combination of the two.Software module may reside within RAM memory, flash memory, ROM storageDevice, eprom memory, eeprom memory, register, hard disk, removable disk, CD-ROM or known in the art it is any itsIn the storage medium of its form.Illustrative storage medium is coupled to processor, enables a processor to from the storage mediumInformation is written to the storage medium in middle reading information.In an alternative, the storage medium can be with processor collectionAt together.Pocessor and storage media may reside in ASIC.ASIC may reside in user terminal.It is replaced at oneIn scheme, it is resident in the user terminal that pocessor and storage media can be used as discrete assembly.
In some embodiments, database middleware Mycat, database MySQL.
From above-described embodiment as can be seen that method of data synchronization provided in an embodiment of the present invention, by using enabling, table is wideIt broadcasts mechanism, the database statement for receiving data manipulation language, judge whether database statement needs to be implemented global table logic, needsWhen send database statement to when database statement table being broadcast to all database nodes, do not needed using table broadcast mechanismAll database nodes and the technical solution for executing database statement can be directed to disparate databases node or different types of numberData are carried out according to library node to synchronize, and are changed Mycat when DML is accessed to the execution logic of global table, be will act on all nodesOperation to be changed to the data completing each node by table broadcast mechanism again to the operation of host node synchronous, to maintain databaseThe data consistency of middle global table.
It is important to note that each step in each embodiment of above-mentioned method of data synchronization can be handed over mutuallyIt pitches, replace, increase, delete, therefore, should also be as in method of data synchronization of these reasonable permutation and combination transformation belongs to the present inventionProtection scope, and protection scope of the present invention should not be confined on the embodiment.
Based on above-mentioned purpose, the second aspect of the embodiment of the present invention, disparate databases section can be directed to by proposing one kindPoint or different types of database node carry out the embodiment of the synchronous device of data.The data synchronization unit has used above-mentionedMethod of data synchronization.
Various illustrative logical blocks, module and circuit, which can use, in conjunction with described in disclosure herein is designed toThe following component of function described here is executed to realize or execute: general processor, digital signal processor (DSP), dedicated collectionAt circuit (ASIC), field programmable gate array (FPGA) or other programmable logic device, discrete gate or transistor logic, divideAny combination of vertical hardware component or these components.General processor can be microprocessor, but alternatively, processingDevice can be any conventional processors, controller, microcontroller or state machine.Processor also may be implemented as calculating equipmentCombination, for example, the combination of DSP and microprocessor, multi-microprocessor, one or more microprocessors combination DSP and/or anyOther this configurations.
Data synchronization unit provided in an embodiment of the present invention by using enabling table broadcast mechanism, receives data manipulation languageThe database statement of speech judges will count when whether database statement needs to be implemented global table logic, needs using table broadcast mechanismAll database nodes are sent by database statement when being broadcast to all database nodes according to library statement list, do not need and are executedIt is same can to carry out data for disparate databases node or different types of database node for the technical solution of database statementStep changes Mycat when DML is accessed to the execution logic of global table, and the operation that will act on all nodes is changed to host nodeOperation again by table broadcast mechanism complete the data between each node synchronization, to maintain the data of global table in database consistentProperty.
It is important to note that the embodiment of above-mentioned data synchronization unit uses the implementation of the method for data synchronizationExample illustrates the course of work of each module, and those skilled in the art can be it is readily conceivable that by these module applications to instituteIt states in the other embodiments of method of data synchronization.Certainly, due to each step in the method for data synchronization embodimentTo intersect, replace, increase, delete, therefore, these reasonable permutation and combination transformation are also answered in the data synchronization unitWhen belonging to the scope of protection of the present invention, and protection scope of the present invention should not be confined on the embodiment.
The data synchronization unit includes memory, at least one processor, and memory, which is stored with, to be transported on a processorCapable computer program, processor execute any one of the above method when executing program.
As shown in figure 5, the hardware structural diagram of one embodiment for data synchronization unit provided by the invention.
It include several processors 501 and several in the computer equipment by taking computer equipment as shown in Figure 5 as an exampleMemory 502, and can also include: input unit 503 and output device 504.
Processor 501, memory 502, input unit 503 and output device 504 can pass through bus or other modesIt connects, in Fig. 5 for being connected by bus.
Memory 502 is used as a kind of non-volatile computer readable storage medium storing program for executing, can be used for storing non-volatile software journeySequence, non-volatile computer executable program and module, as the method for data synchronization in the embodiment of the present application is correspondingProgram instruction/module.Non-volatile software program, instruction and the mould that processor 501 is stored in memory 502 by operationBlock realizes the data side of synchronization of above method embodiment thereby executing the various function application and data processing of serverMethod.
Memory 502 may include storing program area and storage data area, wherein storing program area can store operation systemApplication program required for system, at least one function;Storage data area can be stored to be created according to using for data synchronization unitData etc..In addition, memory 502 may include high-speed random access memory, it can also include nonvolatile memory, exampleSuch as at least one disk memory, flush memory device or other non-volatile solid state memory parts.In some embodiments, it depositsOptional reservoir 502 includes the memory remotely located relative to processor 501, these remote memories can pass through network connectionTo local module.The example of above-mentioned network include but is not limited to internet, intranet, local area network, mobile radio communication and itsCombination.
Input unit 503 can receive the number or character information of input, and generates and set with the user of data synchronization unitIt sets and the related key signals of function control inputs.Output device 504 may include that display screen etc. shows equipment.
Corresponding program instruction/the module of one or more of method of data synchronization is stored in the memory 502,When being executed by the processor 501, the method for data synchronization in above-mentioned any means embodiment is executed.
Any one embodiment of the computer equipment for executing the method for data synchronization, can achieve and be corresponding to itThe identical or similar effect of aforementioned any means embodiment.
Based on above-mentioned purpose, the third aspect of the embodiment of the present invention proposes a kind of computer readable storage medium, instituteStating computer-readable recording medium storage has computer executable instructions, which can be performed above-mentioned any sideThe data synchronization unit in method of data synchronization and the above-mentioned any device embodiment of realization in method embodiment.The computer canThe embodiment for reading storage medium, it is identical or similar as Installation practice to can achieve corresponding aforementioned any meansEffect.
Based on above-mentioned purpose, the 4th aspect of the embodiment of the present invention proposes a kind of computer program product, the calculatingMachine program product includes the calculation procedure being stored on computer readable storage medium, which includes instruction, when thisInstruction is when being computer-executed, and makes that the computer executes the method for data synchronization in above-mentioned any means embodiment and realization is above-mentionedData synchronization unit in any device embodiment.The embodiment of the computer program product can achieve correspondingThe aforementioned any means effect identical or similar with Installation practice.
It is exemplary embodiment disclosed by the invention above, it should be noted that in the sheet limited without departing substantially from claimUnder the premise of inventive embodiments scope of disclosure, it may be many modifications and modify.According to open embodiment described hereinThe function of claim to a method, step and/or movement be not required to the execution of any particular order.In addition, although the present invention is implementedElement disclosed in example can be described or be required in the form of individual, but be unless explicitly limited odd number, it is understood that be multiple.
It should be understood that it is used in the present context, unless the context clearly supports exceptions, singular " oneIt is a " it is intended to also include plural form.It is to be further understood that "and/or" used herein refers to including one or oneAny and all possible combinations of a above project listed in association.The embodiment of the present invention discloses embodiment sequence numberDescription, does not represent the advantages or disadvantages of the embodiments.
It should be understood by those ordinary skilled in the art that: the discussion of any of the above embodiment is exemplary only, notIt is intended to imply that range disclosed by the embodiments of the present invention (including claim) is limited to these examples;In the think of of the embodiment of the present inventionUnder road, it can also be combined between the technical characteristic in above embodiments or different embodiments, and exist as described aboveMany other variations of the different aspect of the embodiment of the present invention, for simplicity, they are not provided in details.Therefore, all at thisWithin the spirit and principle of inventive embodiments, any omission, modification, equivalent replacement, improvement for being made etc. should be included in this hairWithin the protection scope of bright embodiment.