Introduction
Conflict scenarios:
- Update-update
Occurs when the same object was updated at more than one node. - Update-delete
Occurs if a row was updated at one node, but the same row was deleted at another node. - Delete-delete
Occurs when a row was deleted from more than one node.
Conflict Detection
We need a new table in a database of each instance to store hashcodes of objects pulled from this instance's Parent.
PULL:
- At first, when we pull an object from Parent to Child instance (where it does not exist yet):
- calculate hashcode of an object pulled from Parent,
- look for an object on Child instance with UUID of pulled object,
- such object on Child instance does not exist, so object is created on Child instance,
- object's hashcode is saved in Child's database as the latest version of this object on Parent instance,
- there is NO conflict.
- Object is NOT modified on Parent instance.
- When we pull the same object from Parent the second time (this object already exist on Child instance):
- calculate hashcode of an object pulled from Parent,
- look for an object on Child instance with UUID of pulled object,
- such object on Child instance exists, so compare calculated hashcode with a hashcode that is saved in Child's database,
- they are equal, which means that object on Child instance is up to date with corresponding object on Parent instance,
- there is NO conflict.
- Object is modified both on Parent and/or Child instance.
- When we pull the same object from Parent the third time (this object already exist on Child instance):
- calculate hashcode of an object pulled from Parent,
- look for an object on Child instance with UUID of pulled object,
- such object on Child instance exists, so compare calculated hashcode with a hashcode that is saved in Child's database,
- they are NOT equal, which means that object on Child instance is NOT up to date with corresponding object on Parent instance
- there is a conflict, which is resolved using rule RULE 1
PUSH:
- At first, when we push an object from Child to Parent instance (where it does not exist yet):
- In SyncPushServiceImpl class during execution of readAndPushObjectToParent method we call shouldPushObject method, where we pull corresponding object from Parent instance,
- this object does not exist on Parent instance yet, shouldPushObject returns true,
- object is pushed to Parent,
- object's hashcode is saved in Child's database as the latest version of this object on Parent instance.
- Object is modified on Child instance.
- When we push this object from Child to Parent instance (where it already exists):
- In SyncPushServiceImpl class during execution of readAndPushObjectToParent method we call shouldPushObject method, where we pull corresponding object from Parent instance,
- this object already exists on Parent instance,
- calculate hashcode of an object pulled from Parent,
- compare calculated hashcode with a hashcode that is saved in Child's database,
- they are equal, which means that Child modified the latest version of object on Parent instance,
- object is pushed to Parent,
- object's hashcode is updated in Child's database as the last version of this object.
- Object is modified both on Parent and/or Child instance.
- When we push this object from Child to Parent instance (where it already exists):
- In SyncPushServiceImpl class during execution of readAndPushObjectToParent method we call shouldPushObject method, where we pull corresponding object from Parent instance,
- this object already exists on Parent instance,
- calculate hashcode of an object pulled from Parent,
- compare calculated hashcode with a hashcode that is saved in Child's database,
- they are NOT equal, which means that object on Child instance is NOT up to date with corresponding object on Parent instance (someone modified this object on Parent instance),
- there is a conflict, which is resolved using rule RULE 2.
Conflict Resolution
PULL:
- RULE 1
If hashcode of an object on Parent instance is NOT equal to hashcode of an object on Child instance* it means that there is a new version of this object on Parent →- if object on Child instance was NOT modified (calculated hashcode of modified object is equal to hashcode saved on Child's instance*) →
- void current object's fields on Child instance,
- save fields of object from Parent instance to Child.
- if object on Child instance was modified (calculated hashcode of modified object is NOT equal to hashcode saved on Child's instance*) →
- these two objects have to be merged**, then
- saved on Child and Parent instance;
- hashcodes on both Child and Parent instance have to be updated.
- if object on Child instance was NOT modified (calculated hashcode of modified object is equal to hashcode saved on Child's instance*) →
* REMEMBER: This is a hashcode of an object that was calculated and saved on the last pull of this object from Parent. This hashcode is stored in a seperate table than pulled object.
** See Merge
PUSH:
- RULE 2
If hashcode of an object on Parent instance is NOT equal to hashcode of an object on Child instance* it means that the object was updated on Parent instance after the last pull to Child (we are pushing modifications of outdated version of object) →- if object on Child instance was NOT modified (calculated hashcode of modified object is equal to hashcode saved on Child's instance*) → PUSH operation of this object will not take place
- if object on Child instance was modified (calculated hashcode of modified object is NOT equal to hashcode saved on Child's instance*) →
- these two objects have to be merged**, then
- saved on Child and Parent instance;
- hashcodes on both Child and Parent instance have to be updated.
- if object on Child instance was NOT modified (calculated hashcode of modified object is equal to hashcode saved on Child's instance*) → PUSH operation of this object will not take place
* REMEMBER: This is a hashcode of an object that was calculated and saved on the last pull of this object from Parent. This hashcode is stored in a seperate table than pulled object.
** See Merge
Merge
Once a conflict was detected two objects will have to be merged. To do this, a new database table "Conflicts" will have to be created.
This table will have columns: openmrs_class, first_object, second_object and more if needed.
It will be possible to insert any type of object into child_object and parent_object due to its type: it will be either JSON, (TINY)BLOB, TEXT or else.
All conflicted objects will be put into this table and queued just like AuditLog does this.
However, there should be only the most recent merge conflict for any object. The old ones should be voided.
Then either chosen strategy or UI will be used to:
- determine if child_object, parent_object or mixed object will be preserved,
- determine what operation should be taken after merging (push / pull / ...)
When the conflict is resolved we should save parent's object as voided in child's database.
UI for merging will use openmrs_class field to determine what class are child_object and parent_object.
Fields of determined class will be displayed as textboxes or buttons for each of two objects.
User will be able to choose which value he wants to keep.