forked from icatproject/topcat
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Implement queuing priority #56 #58
Merged
+467
−49
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e1ac190
Merge branch '38_prepare_queued_downloads' into 56_queue_priority
patrick-austin d061400
Implement queuing priority #56
patrick-austin e4c6edd
Merge branch '36_queuing' into 56_queue_priority
patrick-austin a183af2
Refactor setting authenticatedPriority = defaultPriority outside if b…
patrick-austin 36230d1
Merge branch '36_queuing' into 56_queue_priority
patrick-austin d25db61
Revert "Refactor setting authenticatedPriority = defaultPriority outs…
patrick-austin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package org.icatproject.topcat; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.util.HashMap; | ||
|
||
import org.icatproject.topcat.exceptions.InternalException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import jakarta.json.Json; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonReader; | ||
|
||
public class PriorityMap { | ||
|
||
private static PriorityMap instance = null; | ||
|
||
public synchronized static PriorityMap getInstance() throws InternalException { | ||
if (instance == null) { | ||
instance = new PriorityMap(); | ||
} | ||
return instance; | ||
} | ||
|
||
private int defaultPriority; | ||
private int authenticatedPriority; | ||
private HashMap<Integer, String> mapping = new HashMap<>(); | ||
private Logger logger = LoggerFactory.getLogger(PriorityMap.class); | ||
|
||
public PriorityMap() { | ||
Properties properties = Properties.getInstance(); | ||
|
||
String defaultString = properties.getProperty("queue.priority.default", "0"); | ||
defaultPriority = Integer.valueOf(defaultString); | ||
|
||
String authenticatedString = properties.getProperty("queue.priority.authenticated", defaultString); | ||
setAuthenticatedPriority(authenticatedString); | ||
|
||
String property = "queue.priority.investigationUser.default"; | ||
String investigationUserString = properties.getProperty(property, authenticatedString); | ||
updateMapping(Integer.valueOf(investigationUserString), "user.investigationUsers IS NOT EMPTY"); | ||
|
||
property = "queue.priority.instrumentScientist.default"; | ||
String instrumentScientistString = properties.getProperty(property, authenticatedString); | ||
updateMapping(Integer.valueOf(instrumentScientistString), "user.instrumentScientists IS NOT EMPTY"); | ||
|
||
String investigationUserProperty = properties.getProperty("queue.priority.investigationUser.roles"); | ||
String investigationUserCondition = "EXISTS ( SELECT o FROM InvestigationUser o WHERE o.role='"; | ||
parseObject(investigationUserProperty, investigationUserCondition); | ||
|
||
String instrumentScientistProperty = properties.getProperty("queue.priority.instrumentScientist.instruments"); | ||
String instrumentScientistCondition = "EXISTS ( SELECT o FROM InstrumentScientist o WHERE o.instrument.name='"; | ||
parseObject(instrumentScientistProperty, instrumentScientistCondition); | ||
} | ||
|
||
/** | ||
* Set the minimum priority for all authenticated Users. This cannot be lower | ||
* than the defaultPriority, which will be used instead if this is the case. | ||
* | ||
* @param authenticatedString The value read from the run.properties file | ||
*/ | ||
private void setAuthenticatedPriority(String authenticatedString) { | ||
authenticatedPriority = Integer.valueOf(authenticatedString); | ||
if (authenticatedPriority < 1 && defaultPriority >= 1) { | ||
String msg = "queue.priority.authenticated disabled with value " + authenticatedString; | ||
msg += " but queue.priority.default enabled with value " + defaultPriority; | ||
msg += "\nAuthenticated users will use default priority if no superseding priority applies"; | ||
logger.warn(msg); | ||
authenticatedPriority = defaultPriority; | ||
} else if (authenticatedPriority >= 1 && authenticatedPriority > defaultPriority) { | ||
String msg = "queue.priority.authenticated enabled with value " + authenticatedString; | ||
msg += " but queue.priority.default supersedes with value " + defaultPriority; | ||
msg += "\nAuthenticated users will use default priority if no superseding priority applies"; | ||
logger.warn(msg); | ||
authenticatedPriority = defaultPriority; | ||
} | ||
} | ||
|
||
/** | ||
* Extracts each key from a JsonObject, and appends this to the JPQL condition | ||
* for this priority level with OR. | ||
* | ||
* @param propertyString String representing a JsonObject from the | ||
* run.properties file, or null | ||
* @param conditionPrefix JPQL condition which will be formatted with each key | ||
* in the object | ||
*/ | ||
private void parseObject(String propertyString, String conditionPrefix) { | ||
if (propertyString == null) { | ||
return; | ||
} | ||
JsonReader reader = Json.createReader(new ByteArrayInputStream(propertyString.getBytes())); | ||
JsonObject object = reader.readObject(); | ||
for (String key : object.keySet()) { | ||
int priority = object.getInt(key); | ||
updateMapping(priority, conditionPrefix + key + "' AND o.user=user )"); | ||
} | ||
} | ||
|
||
/** | ||
* Appends the newCondition to the mapping at the specified priority level using | ||
* OR. | ||
* | ||
* @param priority Priority of the new condition | ||
* @param newCondition Fully formatted JPQL condition | ||
*/ | ||
private void updateMapping(int priority, String newCondition) { | ||
if (priority < 1) { | ||
logger.warn("Non-positive priority found in mapping, ignoring entry"); | ||
return; | ||
} else if (authenticatedPriority >= 1 && priority >= authenticatedPriority) { | ||
logger.warn("Priority set in mapping would be superseded by queue.priority.authenticated, ignoring entry"); | ||
return; | ||
} | ||
|
||
String oldCondition = mapping.get(priority); | ||
if (oldCondition != null) { | ||
mapping.put(priority, oldCondition + " OR " + newCondition); | ||
} else { | ||
mapping.put(priority, newCondition); | ||
} | ||
} | ||
|
||
/** | ||
* @return Mapping of priority level to a JPQL condition which defines the Users | ||
* who have this priority | ||
*/ | ||
public HashMap<Integer, String> getMapping() { | ||
return mapping; | ||
} | ||
|
||
/** | ||
* @return The priority which applies to all authenticated users | ||
*/ | ||
public int getAuthenticatedPriority() { | ||
return authenticatedPriority; | ||
} | ||
|
||
/** | ||
* @return The priority which applies to all users, included anonymous access | ||
*/ | ||
public int getDefaultPriority() { | ||
return defaultPriority; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
These last 3 lines in both the if and the else could go below the if/else as they are common.
Although you would also need to initialise msg above the if/else, so that line is borderline worth/not worth it.
However, the final two lines are probably worth moving.
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.
Have refactored the last three lines, this also means introducing an
else
clause so we can return early don't warn/set default when we don't meet either criteria (corresponding to a "normal" value forauthenticatedPriority
).