-
Notifications
You must be signed in to change notification settings - Fork 44
FLAG-69: Improve the performance of patient flag evaluation #59
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,7 @@ | |
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
|
|
@@ -95,23 +96,41 @@ public List<Flag> generateFlagsForPatient(Patient patient, Map<Object, Object> c | |
| /** | ||
| * @see org.openmrs.module.patientflags.api.FlagService#generateFlagsForPatient(Patient, Filter, Map<Object, Object>) | ||
| */ | ||
| public List<Flag> generateFlagsForPatient(Patient patient, Filter filter, Map<Object, Object> context) { | ||
| List<Flag> results = new ArrayList<Flag>(); | ||
| public List<Flag> generateFlagsForPatient(final Patient patient, Filter filter, final Map<Object, Object> context) { | ||
| final List<Flag> results = new ArrayList<Flag>(); | ||
|
|
||
| // we can get rid of this once onStartup is implemented | ||
| if (!isInitialized) | ||
| refreshCache(); | ||
|
|
||
|
|
||
| executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); | ||
| // test each Flag in the cache against the specific Patient | ||
| for (Flag flag : filter.filter(flagCache)) { | ||
| for (final Flag flag : filter.filter(flagCache)) { | ||
| // trap bad flags so that they don't hang the system | ||
| try { | ||
| if (flag.eval(patient, context)) | ||
| results.add(flag); | ||
| } | ||
| catch (Exception e) { | ||
| log.error("Unable to test flag " + flag.getName() + " on patient #" + patient.getId(), e); | ||
| executor.submit(() -> { | ||
| try { | ||
| Context.openSession(); | ||
| if (flag.eval(patient, context)) { | ||
| synchronized (results) { | ||
| results.add(flag); | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| log.error("Unable to test flag " + flag.getName() + " on patient #" + patient.getId(), e); | ||
| } finally { | ||
| Context.closeSession(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| executor.shutdown(); | ||
| try { | ||
| if (!executor.awaitTermination(Integer.MAX_VALUE, TimeUnit.NANOSECONDS)) { | ||
| log.error("Executor service did not terminate"); | ||
| } | ||
| } catch (InterruptedException e) { | ||
| log.error("Thread pool was interrupted while waiting for flag evaluation tasks to complete", e); | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| return results; | ||
| } | ||
|
|
@@ -688,10 +707,8 @@ private List<Flag> getFlags(List<PatientFlag> patientFlags) { | |
|
|
||
| @Override | ||
| public Future<?> evaluateAllFlags() { | ||
| if (executor == null) { | ||
| executor = Executors.newSingleThreadExecutor(); | ||
| } | ||
|
|
||
| executor = Executors.newSingleThreadExecutor(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why did you remove the null check here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in this line , I initialized the executor as a thread pool. If the |
||
|
|
||
| return executor.submit(PatientFlagTask.evaluateAllFlags()); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we switch to streams here?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated the code with forEach