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

Flesh out the slice API for the user code #9

Merged
merged 5 commits into from
Aug 22, 2023
Merged

Conversation

huntc
Copy link
Collaborator

@huntc huntc commented Aug 22, 2023

This PR fleshes out the slice API from a user code perspective. While the source provider is presently sourcing a local commit log, it doesn't make sense to have multiple projection instances i.e. there's no cluster. However, we have this API so that we can start up projection instances on the cloud side when communicating over gRPC.

An EventEnvelope specifically for the commit log has also been introduced, along with the start of one for gRPC. The commit log adapter for persistence will adapt between the entity manager's envelope and its own.

Finally, I had fun implementing some partition id functions, particularly the JDK string hashing one. :-)

An EventEnvelope specifically for the commit log has been introduced, along with the start of one for gRPC. The commit log adapter for persistence will adapt between the entity manager's envelope and its own.
@huntc huntc added the enhancement New feature or request label Aug 22, 2023
@huntc huntc requested a review from patriknw August 22, 2023 08:15
@huntc huntc self-assigned this Aug 22, 2023
Sets up the source provider using slices.
assert_eq!(jdk_string_hashcode(""), 0);
assert_eq!(jdk_string_hashcode("howtodoinjava.com"), 1894145264);
assert_eq!(jdk_string_hashcode("hello world"), 1794106052);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These were taken from a JDK example.

#[test]
fn test_slice_ranges() {
assert_eq!(slice_ranges(4), vec![0..256, 256..512, 512..768, 768..1024]);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note that Range is non-inclusive in terms of its max i.e. it is "upto". I could've used RangeInclusive instead, but then I'd have do an additional subtraction when constructing each range. :-)

@huntc huntc changed the title Source provider impl Flesh out the slice API for the user code Aug 22, 2023
Copy link
Member

@patriknw patriknw left a comment

Choose a reason for hiding this comment

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

looking good

Self::with_separator(entity_type, entity_id, '|')
}

pub fn with_separator(entity_type: EntityType, entity_id: EntityId, separator: char) -> Self {
Copy link
Member

Choose a reason for hiding this comment

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

In practise we only support the | separator on the jvm side. I think we should remove the option to use another separator since we would anyway have to translate it to | in all persistenceIds sent to jvm.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed in commit 045b1e9

/// evenly distribute all persistence ids over the slices and be able to query the
/// events for a range of slices.
pub fn slice_for_persistence_id(persistence_id: &PersistenceId) -> u32 {
jdk_string_hashcode(&persistence_id.to_string()) % NUMBER_OF_SLICES
Copy link
Member

Choose a reason for hiding this comment

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

note that hashCode can be negative and to be compatible with jvm this should correspond to:

math.abs(persistenceId.hashCode % numberOfSlices)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Now using an i32 per commit 6a1febf


// Implementation of the JDK8 string hashcode:
// https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#hashCode
fn jdk_string_hashcode(s: &str) -> u32 {
Copy link
Member

Choose a reason for hiding this comment

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

good, important that we use the same

Copy link
Member

@patriknw patriknw left a comment

Choose a reason for hiding this comment

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

LGTM

SmolStr::from("some-entity-type"),
SmolStr::from("some-entity-id")
)),
451
Copy link
Member

Choose a reason for hiding this comment

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

I have verified that it's the same slice in jvm

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Excellent!

@huntc huntc merged commit 2ab7d96 into main Aug 22, 2023
1 check passed
@huntc huntc deleted the source-provider-impl branch August 22, 2023 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants