-
Notifications
You must be signed in to change notification settings - Fork 49
Use ulid library safe for graalvm native compilation #817
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: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Matheus Cruz <[email protected]>
Signed-off-by: Matheus Cruz <[email protected]>
impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowApplication.java
Outdated
Show resolved
Hide resolved
Signed-off-by: Matheus Cruz <[email protected]>
private final SecureRandom random = new SecureRandom(); | ||
private final ULID ulid = new ULID(random); | ||
private ULID.Value previousUlid; | ||
|
||
@Override | ||
public synchronized String get() { | ||
if (previousUlid == null) { | ||
previousUlid = ulid.nextValue(); | ||
} else { | ||
previousUlid = ulid.nextMonotonicValue(previousUlid); | ||
} | ||
return previousUlid.toString(); | ||
} |
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.
I think this should be more lazy.
Reason is that, with this implementaiton, if the user is replacing this factory, the secure ramdon and the ULID are still created, but never used.
There are two possibilities to achieve that:
- creare this class, as it is, (well, secureRamdon does not need to be a member in any case) in a lazy way in the WorkflowApplication (so create it only when the user has not set another WorkflowInsanceIdFactory)
- Leave WorkflowApplication as it is and create the ULID (and the SecureRamdon) when previousUlid is null.
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.
I now explain why I prefer the first one (lazy initialization of the factory in WorkflowApplication)
The get method can be simplified by creating previousUlid in the constructor, so you write a not synchronized get.
@Override
// note that you do not need to syncronized this
public String get() {
String result= currentUlid.toString();
currentUlid = ulid.nextMonotonicValue(currentUlid);
return result;
}
The constructor will look like
private final ULID ulid;
private ULID.Value currentUlid;
public MonotonicUlidWorkflowInstanceIdFactory () {
ulid = new ULID(new SecureRandom());
currentUlid = ulid.nextValue();
}
And in workflowapplicationbuilder.build you set the factory to MonoloictUlidWorkflowInstanceFactory if the id factory it is null (the user has not set its own one) rather than in builder initialization
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.
@mcruzdev Posible solution proposed, please take a look
private final SecureRandom random = new SecureRandom(); | ||
private final ULID ulid = new ULID(random); | ||
private ULID.Value previousUlid; | ||
|
||
@Override | ||
public synchronized String get() { | ||
if (previousUlid == null) { | ||
previousUlid = ulid.nextValue(); | ||
} else { | ||
previousUlid = ulid.nextMonotonicValue(previousUlid); | ||
} | ||
return previousUlid.toString(); | ||
} |
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.
I now explain why I prefer the first one (lazy initialization of the factory in WorkflowApplication)
The get method can be simplified by creating previousUlid in the constructor, so you write a not synchronized get.
@Override
// note that you do not need to syncronized this
public String get() {
String result= currentUlid.toString();
currentUlid = ulid.nextMonotonicValue(currentUlid);
return result;
}
The constructor will look like
private final ULID ulid;
private ULID.Value currentUlid;
public MonotonicUlidWorkflowInstanceIdFactory () {
ulid = new ULID(new SecureRandom());
currentUlid = ulid.nextValue();
}
And in workflowapplicationbuilder.build you set the factory to MonoloictUlidWorkflowInstanceFactory if the id factory it is null (the user has not set its own one) rather than in builder initialization
Signed-off-by: Matheus Cruz <[email protected]>
Good catch!
@fjtirado, do we need to guarantee no duplicated keys in a thread-safe way? If not, I agree with you to remove the |
@mcruzdev
Note: Im asuming nextMonotoicValue is thread-safe (it wont crash when invoked simoultaneuously from different threads), |
Signed-off-by: Matheus Cruz <[email protected]>
I added the |
Many thanks for submitting your Pull Request ❤️!
What this PR does / why we need it:
com.github.f4b6a3:ulid-creator
dependency.de.huxhorn.sulky:de.huxhorn.sulky.ulid
for ULID generation.WorkflowInstanceIdFactory
implementation (UlidWorkflowInstanceIdFactory
)Closes #812