-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[Buffered Event Hubs Producer] Implements Parition Key to Partition ID mapping #18331
Merged
deyaaeldeen
merged 7 commits into
feature/eh-buffered-producer
from
eh-input-partition-ids
Nov 4, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5cfac50
[Buffered Event Hubs Producer] Implements Parition Key to Partition I…
deyaaeldeen 48abf3e
fix the implementation
deyaaeldeen 0b5e5be
factor out the hashing logic
deyaaeldeen 752e6f9
remove unused import
deyaaeldeen c856d84
the test pass but the implementation needs to be simplified
deyaaeldeen f899ae4
simplified implementation
deyaaeldeen f595e5d
address feedback
deyaaeldeen 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
130 changes: 130 additions & 0 deletions
130
sdk/eventhub/event-hubs/src/impl/patitionKeyToIdMapper.ts
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,130 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
/* eslint-disable no-fallthrough */ | ||
|
||
import os from "os"; | ||
|
||
export function mapPartitionKeyToId(partitionKey: string, partitionCount: number): number { | ||
const hash = computeHash(Buffer.from(partitionKey, "utf8")); | ||
const hashedParitionKey = castToInt16(hash.c ^ hash.b); | ||
return Math.abs(hashedParitionKey % partitionCount); | ||
} | ||
|
||
function readUInt32(data: Buffer, offset: number): number { | ||
return os.endianness() === "BE" ? data.readUInt32BE(offset) : data.readUInt32LE(offset); | ||
} | ||
|
||
function castToInt16(n: number): number { | ||
return new Int16Array([n])[0]; | ||
} | ||
|
||
function computeHash(data: Buffer, seed1: number = 0, seed2: number = 0): any { | ||
let a: number, b: number, c: number; | ||
|
||
a = b = c = 0xdeadbeef + data.length + seed1; | ||
c += seed2; | ||
|
||
let index = 0, | ||
size = data.length; | ||
while (size > 12) { | ||
a += readUInt32(data, index); | ||
b += readUInt32(data, index + 4); | ||
c += readUInt32(data, index + 8); | ||
|
||
a -= c; | ||
a ^= (c << 4) | (c >>> 28); | ||
c += b; | ||
|
||
b -= a; | ||
b ^= (a << 6) | (a >>> 26); | ||
a += c; | ||
|
||
c -= b; | ||
c ^= (b << 8) | (b >>> 24); | ||
b += a; | ||
|
||
a -= c; | ||
a ^= (c << 16) | (c >>> 16); | ||
c += b; | ||
|
||
b -= a; | ||
b ^= (a << 19) | (a >>> 13); | ||
a += c; | ||
|
||
c -= b; | ||
c ^= (b << 4) | (b >>> 28); | ||
b += a; | ||
|
||
index += 12; | ||
size -= 12; | ||
} | ||
|
||
let curr = size; | ||
switch (curr) { | ||
case 12: | ||
a += readUInt32(data, index); | ||
b += readUInt32(data, index + 4); | ||
c += readUInt32(data, index + 8); | ||
break; | ||
case 11: | ||
c += data[index + 10] << 16; | ||
curr = 10; | ||
case 10: | ||
c += data[index + 9] << 8; | ||
curr = 9; | ||
case 9: | ||
c += data[index + 8]; | ||
curr = 8; | ||
case 8: | ||
b += readUInt32(data, index + 4); | ||
a += readUInt32(data, index); | ||
break; | ||
case 7: | ||
b += data[index + 6] << 16; | ||
curr = 6; | ||
case 6: | ||
b += data[index + 5] << 8; | ||
curr = 5; | ||
case 5: | ||
b += data[index + 4]; | ||
curr = 4; | ||
case 4: | ||
a += readUInt32(data, index); | ||
break; | ||
case 3: | ||
a += data[index + 2] << 16; | ||
curr = 2; | ||
case 2: | ||
a += data[index + 1] << 8; | ||
curr = 1; | ||
case 1: | ||
a += data[index]; | ||
break; | ||
case 0: | ||
return { b: b >>> 0, c: c >>> 0 }; | ||
} | ||
|
||
c ^= b; | ||
c -= (b << 14) | (b >>> 18); | ||
|
||
a ^= c; | ||
a -= (c << 11) | (c >>> 21); | ||
|
||
b ^= a; | ||
b -= (a << 25) | (a >>> 7); | ||
|
||
c ^= b; | ||
c -= (b << 16) | (b >>> 16); | ||
|
||
a ^= c; | ||
a -= (c << 4) | (c >>> 28); | ||
|
||
b ^= a; | ||
b -= (a << 14) | (a >>> 18); | ||
|
||
c ^= b; | ||
c -= (b << 24) | (b >>> 8); | ||
|
||
return { b: b >>> 0, c: c >>> 0 }; | ||
} |
27 changes: 27 additions & 0 deletions
27
sdk/eventhub/event-hubs/test/internal/impl/mapPartitionKeyToId.spec.ts
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,27 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import { assert } from "chai"; | ||
import { mapPartitionKeyToId } from "../../../src/impl/patitionKeyToIdMapper"; | ||
|
||
/** | ||
* These unit tests have been created from outputs received from the C# implementation | ||
* of Jenkins lookup3 that the Event Hubs service uses. | ||
*/ | ||
describe("mapPartitionKeyToId", () => { | ||
it("short key, small partitions count", () => { | ||
assert.equal(mapPartitionKeyToId("alphabet", 3), 0); | ||
}); | ||
|
||
it("short key, large partitions count", () => { | ||
assert.equal(mapPartitionKeyToId("alphabet", 11), 4); | ||
}); | ||
|
||
it("long key, small partitions count", () => { | ||
assert.equal(mapPartitionKeyToId("TheBestParitionEver", 4), 2); | ||
}); | ||
|
||
it("long key, large partitions count", () => { | ||
assert.equal(mapPartitionKeyToId("TheWorstParitionEver", 15), 1); | ||
}); | ||
}); |
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
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.
os might not work in browser?
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.
Yeah many things here are node specific such as
os
andBuffer
but the tests passed so I assumed there is polyfilling going on. I will revisit the browser support.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.
rollup shims perhaps