-
Notifications
You must be signed in to change notification settings - Fork 23
Impression seenAt #124
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
Merged
Merged
Impression seenAt #124
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1f1a2e5
Merge pull request #118 from splitio/development
mmelograno af66ac7
Test code analysis
688843f
add seenAt field
mredolatti 15536c9
add murmur3_64 test suite;
mredolatti d5c5455
Merge branch 'master' into improvement/impressionSeenAt
mredolatti 5dfc51c
use pt instead of seenAt
mredolatti 1a3cf53
Merge branch 'improvement/impressionSeenAt' of github.com:splitio/jav…
mredolatti 7d3fd31
use treatment as well
mredolatti 76e8a5c
remove unnecessary sync
mredolatti 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 hidden or 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,52 @@ | ||
| name: "Code scanning - action" | ||
|
|
||
| on: | ||
| push: | ||
| pull_request: | ||
| schedule: | ||
| - cron: '0 21 * * 3' | ||
|
|
||
| jobs: | ||
| CodeQL-Build: | ||
|
|
||
| # CodeQL runs on ubuntu-latest and windows-latest | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v2 | ||
| with: | ||
| # We must fetch at least the immediate parents so that if this is | ||
| # a pull request then we can checkout the head. | ||
| fetch-depth: 2 | ||
|
|
||
| # If this run was triggered by a pull request event, then checkout | ||
| # the head of the pull request instead of the merge commit. | ||
| - run: git checkout HEAD^2 | ||
| if: ${{ github.event_name == 'pull_request' }} | ||
|
|
||
| # Initializes the CodeQL tools for scanning. | ||
| - name: Initialize CodeQL | ||
| uses: github/codeql-action/init@v1 | ||
| # Override language selection by uncommenting this and choosing your languages | ||
| # with: | ||
| # languages: go, javascript, csharp, python, cpp, java | ||
|
|
||
| # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). | ||
| # If this step fails, then you should remove it and run the build manually (see below) | ||
| - name: Autobuild | ||
| uses: github/codeql-action/autobuild@v1 | ||
|
|
||
| # ℹ️ Command-line programs to run using the OS shell. | ||
| # 📚 https://git.io/JvXDl | ||
|
|
||
| # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines | ||
| # and modify them (or add more) to build your code if your project | ||
| # uses a compiled language | ||
|
|
||
| #- run: | | ||
| # make bootstrap | ||
| # make release | ||
|
|
||
| - name: Perform CodeQL Analysis | ||
| uses: github/codeql-action/analyze@v1 |
This file contains hidden or 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 hidden or 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
30 changes: 30 additions & 0 deletions
30
client/src/main/java/io/split/client/impressions/ImpressionHasher.java
This file contains hidden or 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,30 @@ | ||
| package io.split.client.impressions; | ||
|
|
||
| import io.split.client.dtos.KeyImpression; | ||
| import io.split.client.utils.MurmurHash3; | ||
|
|
||
| public class ImpressionHasher { | ||
|
|
||
| private static final String HASHABLE_FORMAT = "%s:%s:%s:%s:%d"; | ||
| private static final String UNKNOWN = "UNKNOWN"; | ||
|
|
||
| private static String unknownIfNull(String s) { | ||
| return (s == null) ? UNKNOWN : s; | ||
| } | ||
|
|
||
| private static Long zeroIfNull(Long l) { | ||
| return (l == null) ? 0 : l; | ||
| } | ||
|
|
||
| public static Long process(KeyImpression impression) { | ||
| if (null == impression) { | ||
| return null; | ||
| } | ||
| return MurmurHash3.hash128x64(String.format(HASHABLE_FORMAT, | ||
| unknownIfNull(impression.keyName), | ||
| unknownIfNull(impression.feature), | ||
| unknownIfNull(impression.treatment), | ||
| unknownIfNull(impression.label), | ||
| zeroIfNull(impression.changeNumber)).getBytes())[0]; | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
client/src/main/java/io/split/client/impressions/ImpressionObserver.java
This file contains hidden or 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,38 @@ | ||
| package io.split.client.impressions; | ||
|
|
||
| import com.google.common.cache.Cache; | ||
| import com.google.common.cache.CacheBuilder; | ||
| import io.split.client.dtos.KeyImpression; | ||
| import org.apache.http.annotation.NotThreadSafe; | ||
|
|
||
| /* | ||
| According to guava's docs (https://guava.dev/releases/18.0/api/docs/com/google/common/annotations/Beta.html), | ||
| the @Beta decorator only means that the api is not frozen, and has nothing to do with behaviour stability, but | ||
| rather to a non-frozen API which may introduce breaking changes at any time in future versions. | ||
| Since the library is shaded and should not be exposed to users of the SDK, it's safe to use it here. | ||
| */ | ||
|
|
||
| @SuppressWarnings("UnstableApiUsage") | ||
| @NotThreadSafe | ||
| public class ImpressionObserver { | ||
|
|
||
| private final Cache<Long, Long> _cache; | ||
|
|
||
| public ImpressionObserver(long size) { | ||
| _cache = CacheBuilder.newBuilder() | ||
| .maximumSize(size) | ||
| .concurrencyLevel(4) // Just setting the default value explicitly | ||
| .build(); | ||
| } | ||
|
|
||
| public Long testAndSet(KeyImpression impression) { | ||
| if (null == impression) { | ||
| return null; | ||
| } | ||
|
|
||
| Long hash = ImpressionHasher.process(impression); | ||
| Long previous = _cache.getIfPresent(hash); | ||
| _cache.put(hash, impression.time); | ||
| return previous; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
73 changes: 73 additions & 0 deletions
73
client/src/test/java/io/split/client/impressions/ImpressionHasherTest.java
This file contains hidden or 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,73 @@ | ||
| package io.split.client.impressions; | ||
|
|
||
| import io.split.client.dtos.KeyImpression; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.hamcrest.Matchers.*; | ||
| import static org.junit.Assert.*; | ||
|
|
||
|
|
||
| public class ImpressionHasherTest { | ||
|
|
||
| @Test | ||
| public void works() { | ||
| KeyImpression imp1 = new KeyImpression(); | ||
| imp1.feature = "someFeature"; | ||
| imp1.keyName = "someKey"; | ||
| imp1.changeNumber = 123L; | ||
| imp1.label = "someLabel"; | ||
| imp1.treatment = "someTreatment"; | ||
|
|
||
| // Different feature | ||
| KeyImpression imp2 = new KeyImpression(); | ||
| imp2.feature = "someOtherFeature"; | ||
| imp2.keyName = "someKey"; | ||
| imp2.changeNumber = 123L; | ||
| imp2.label = "someLabel"; | ||
| assertThat(ImpressionHasher.process(imp1), not(equalTo(ImpressionHasher.process(imp2)))); | ||
|
|
||
| // different key | ||
| imp2.feature = imp1.feature; | ||
| imp2.keyName = "someOtherKey"; | ||
| assertThat(ImpressionHasher.process(imp1), not(equalTo(ImpressionHasher.process(imp2)))); | ||
|
|
||
| // different changeNumber | ||
| imp2.keyName = imp1.keyName; | ||
| imp2.changeNumber = 456L; | ||
| assertThat(ImpressionHasher.process(imp1), not(equalTo(ImpressionHasher.process(imp2)))); | ||
|
|
||
| // different label | ||
| imp2.changeNumber = imp1.changeNumber; | ||
| imp2.label = "someOtherLabel"; | ||
| assertThat(ImpressionHasher.process(imp1), not(equalTo(ImpressionHasher.process(imp2)))); | ||
|
|
||
| // different treatment | ||
| imp2.label = imp1.label; | ||
| imp2.treatment = "someOtherTreatment"; | ||
| assertThat(ImpressionHasher.process(imp1), not(equalTo(ImpressionHasher.process(imp2)))); | ||
| } | ||
|
|
||
| @Test | ||
| public void doesNotCrash() { | ||
| KeyImpression imp1 = new KeyImpression(); | ||
| imp1.feature = null; | ||
| imp1.keyName = "someKey"; | ||
| imp1.changeNumber = 123L; | ||
| imp1.label = "someLabel"; | ||
| assertNotNull(ImpressionHasher.process(imp1)); | ||
|
|
||
| imp1.keyName = null; | ||
| assertNotNull(ImpressionHasher.process(imp1)); | ||
|
|
||
| imp1.changeNumber = null; | ||
| assertNotNull(ImpressionHasher.process(imp1)); | ||
|
|
||
| imp1.label = null; | ||
| assertNotNull(ImpressionHasher.process(imp1)); | ||
|
|
||
| imp1.treatment = null; | ||
| assertNotNull(ImpressionHasher.process(imp1)); | ||
|
|
||
| assertNull(ImpressionHasher.process(null)); | ||
| } | ||
| } |
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.
do we need to set the concurrency level?
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.
how about the expiration settings?
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.
Regarding cuncurrency level: the cache is only used from the impressions manager which is executed within a singleThreadedExecutor, so we should not have race conditions here, nor benefit from partitioning to minimize contention. I can however set the default value (4) explicitly, in case it changes sometime, or we decide to go multithreaded here
Regarding expiration settings: We discussed this thoroughly with nico, and in order to minimize memory usage, we decided not to attach an extra TTL to each cache item, since the difference between sending
nullor sending an old timestamp doesn't really make a difference. We let the events server decide whether the timestamp is recent enough to filter the impression from the exp pipeline or not