General coordination work, let zookeeper to do that is the best, the problem is you have to deploy zookeeper. What if there is no ability to emulate a facilitator? This is what this article needs to cover.
Background
There is a contract agreement, if the total consumption reached a certain amount, the need to start a discount, reached another higher limit, will start another discount. The user's consumption records are small, such as 1 cents a sum, the discount needs to be detailed to each deal. This data is now available in large quantities and the total price calculation is required. The application deploys 3 machines, How to make 3 machines can participate in processing, and the consumption data of a contract can only be processed by one machine, and if this machine processes part of the contract data dead, Need other machines to continue processing?
Here, the database is used as the coordinator to coordinate the state between the machines, which are described in detail below:
1, the agreed heartbeat time of 1 minutes
2. Create Schedule Task Table
Field |
Description |
Task type |
This corresponds to the batch price contract ID, the unique ID |
Last Update time |
|
Version number |
Or a time stamp, a meaning |
3, each machine initiates a heart jumper, thread found a last 1 minutes (heartbeat unit) not updated task record (update time and the current difference of more than 1 minutes)
4. Update this record, execute SQL: Update Task table set last update time = system time, version number =Step3 Query out the version number +1 where task type = query out the task type and version number =step3 query out the version number
5, judge the execution result, if the update succeeds (SQL affects the number of rows =1) means that the machine can start to perform the task, otherwise it means that the task is preempted by other machines.
Further optimization:
Step4 only performs a heartbeat recognition task processing right, can have a few more heartbeat time, such as 3 , and so on after the successful update, then confirm the processing rights, and then start processing.
Analysis Ideas:
1, There is no connection between the 3 machines deployed, so to achieve mutual coordination, there must be an intermediary coordinator.
2, Coordination 2 minutes, active and passive, ① three servers passively accept the coordination results of the Coordinator, there is no; ② three servers actively ask for a shared resource, which uses a database.
3, reference the database concurrency processing characteristics, is that version number, this is very important.
A simple database-based implementation of multi-machine task preemption scheduling