Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize sync workers #2385

Merged
merged 11 commits into from
Jan 31, 2024
Merged

Synchronize sync workers #2385

merged 11 commits into from
Jan 31, 2024

Conversation

MJ1998
Copy link
Collaborator

@MJ1998 MJ1998 commented Dec 21, 2023

IMPORTANT: All PRs must be linked to an issue (except for extremely trivial and straightforward changes).

Fixes #2361

Description
We use the recommended way for mutual exclusion: link

Solution: Wrap the FhirSynchronizer#synchronize with mutex.withLock

mutex.withLock will suspend the successive invocations of FhirSynchronizer#synchronize, without blocking the thread, until the lock is released. Hence all work requests will be processed - and in FIFO order.

Alternative(s) considered
Alternative: One alternative involves using a ReentrantLock within a try-catch-finally block. This approach essentially replicates mutex-like behavior to manage concurrent access to shared resources. It would require maintaining a job queue to handle work requests sequentially. Without a queue, workers would need to return Result.retry, leading to repeated attempts every 30 seconds.

Another alternative was to wrap FhirSyncWorker#doWork with mutex.withLock but based on this comment and also for the fact that FhirSynchronizer#synchronize should run sequentially irrespective of whether FhirSyncWorker uses it or not.

Type
Bug fix

Screenshots (if applicable)

Checklist

  • I have read and acknowledged the Code of conduct.
  • I have read the Contributing page.
  • I have signed the Google Individual CLA, or I am covered by my company's Corporate CLA.
  • I have discussed my proposed solution with code owners in the linked issue(s) and we have agreed upon the general approach.
  • I have run ./gradlew spotlessApply and ./gradlew spotlessCheck to check my code follows the style guide of this project.
  • I have run ./gradlew check and ./gradlew connectedCheck to test my changes locally.
  • I have built and run the demo app(s) to verify my change fixes the issue and/or does not break the demo app(s).

@MJ1998 MJ1998 requested review from santosh-pingle and a team as code owners December 21, 2023 20:09
@MJ1998 MJ1998 requested a review from yigit December 21, 2023 20:09
Copy link
Collaborator

@aditya-07 aditya-07 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test to check the change.

@MJ1998
Copy link
Collaborator Author

MJ1998 commented Jan 29, 2024

I am not sure if automation test for this is a good idea. Because it takes lot of execution time to be able to verify the synchronicity of any concurrent jobs. See how concurrent behavior is verified here.

Such long execution time, however, is very likely in case of long sync time in an actual app

@aditya-07

Copy link
Collaborator

@jingtang10 jingtang10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please feel free to merge once tested manually again.

@MJ1998
Copy link
Collaborator Author

MJ1998 commented Jan 31, 2024

I figured how to test the mutex locking.
@aditya-07 @jingtang10

@MJ1998 MJ1998 changed the title Synchronize sync workers Process FhirSynchronizer#synhronize requests sequentially Jan 31, 2024
@MJ1998 MJ1998 changed the title Process FhirSynchronizer#synhronize requests sequentially Synchronize sync workers Jan 31, 2024
@jingtang10 jingtang10 enabled auto-merge (squash) January 31, 2024 11:21
@jingtang10 jingtang10 merged commit d601844 into google:master Jan 31, 2024
3 checks passed
sharon2719 pushed a commit to opensrp/android-fhir that referenced this pull request Feb 6, 2024
* mutex is enough

* Mutex is actually good

* Remove extra mutex

* remove extra spotless changes

* spotless

* mutex test added

* adding more asserts

* test modified

* note for flaky test possibility
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

Concurrent Sync Worker execution can lead to data inconsistencies
3 participants