-
Notifications
You must be signed in to change notification settings - Fork 107
Handle eventhub cardinality #527
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
Changes from all commits
Commits
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
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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# Capabilities | ||
RAW_HTTP_BODY_BYTES = "RawHttpBodyBytes" | ||
TYPED_DATA_COLLECTION = "TypedDataCollection" |
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
13 changes: 13 additions & 0 deletions
13
tests/endtoend/eventhub_batch_functions/eventhub_multiple/__init__.py
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,13 @@ | ||
import json | ||
|
||
|
||
def main(events): | ||
table_entries = [] | ||
for event in events: | ||
json_entry = event.get_body() | ||
table_entry = json.loads(json_entry) | ||
table_entries.append(table_entry) | ||
|
||
table_json = json.dumps(table_entries) | ||
|
||
return table_json |
22 changes: 22 additions & 0 deletions
22
tests/endtoend/eventhub_batch_functions/eventhub_multiple/function.json
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,22 @@ | ||
{ | ||
"scriptFile": "__init__.py", | ||
|
||
"bindings": [ | ||
{ | ||
"type": "eventHubTrigger", | ||
"name": "events", | ||
"direction": "in", | ||
"cardinality": "many", | ||
"dataType": "string", | ||
"eventHubName": "python-worker-ci-eventhub-batch", | ||
"connection": "AzureWebJobsEventHubConnectionString" | ||
}, | ||
{ | ||
"direction": "out", | ||
"type": "table", | ||
"name": "$return", | ||
"tableName": "EventHubBatchTest", | ||
"connection": "AzureWebJobsStorage" | ||
} | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/endtoend/eventhub_batch_functions/eventhub_output_batch/__init__.py
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,6 @@ | ||
import azure.functions as func | ||
|
||
|
||
def main(req: func.HttpRequest) -> str: | ||
events = req.get_body().decode('utf-8') | ||
return events |
18 changes: 18 additions & 0 deletions
18
tests/endtoend/eventhub_batch_functions/eventhub_output_batch/function.json
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,18 @@ | ||
{ | ||
"scriptFile": "__init__.py", | ||
|
||
"bindings": [ | ||
{ | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "req" | ||
}, | ||
{ | ||
"type": "eventHub", | ||
"name": "$return", | ||
"direction": "out", | ||
"eventHubName": "python-worker-ci-eventhub-batch", | ||
"connection": "AzureWebJobsEventHubConnectionString" | ||
} | ||
] | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/endtoend/eventhub_batch_functions/get_eventhub_batch_triggered/__init__.py
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,5 @@ | ||
import azure.functions as func | ||
|
||
|
||
def main(req: func.HttpRequest, testEntities): | ||
return func.HttpResponse(status_code=200, body=testEntities) |
27 changes: 27 additions & 0 deletions
27
tests/endtoend/eventhub_batch_functions/get_eventhub_batch_triggered/function.json
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,27 @@ | ||
{ | ||
"scriptFile": "__init__.py", | ||
"bindings": [ | ||
{ | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"authLevel": "anonymous", | ||
"methods": [ | ||
"get" | ||
], | ||
"name": "req" | ||
}, | ||
{ | ||
"direction": "in", | ||
"type": "table", | ||
"name": "testEntities", | ||
"partitionKey": "WillBePopulated", | ||
"tableName": "EventHubBatchTest", | ||
"connection": "AzureWebJobsStorage" | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "$return" | ||
} | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import json | ||
import time | ||
import pathlib | ||
|
||
from azure_functions_worker import testutils | ||
|
||
|
||
class TestEventHubFunctions(testutils.WebHostTestCase): | ||
|
||
@classmethod | ||
def get_script_dir(cls): | ||
return testutils.E2E_TESTS_FOLDER / 'eventhub_batch_functions' | ||
|
||
def test_eventhub_multiple(self): | ||
NUM_EVENTS = 3 | ||
all_row_keys_seen = dict([(str(i), True) for i in range(NUM_EVENTS)]) | ||
partition_key = str(round(time.time())) | ||
|
||
# Dynamically rewrite function.json to point to new partition key | ||
# for recording EventHub state | ||
old_partition_key = self._get_table_partition_key() | ||
self._set_table_partition_key(partition_key) | ||
|
||
# wait for host to restart after change | ||
time.sleep(1) | ||
|
||
docs = [] | ||
for i in range(NUM_EVENTS): | ||
doc = {'PartitionKey': partition_key, 'RowKey': i} | ||
docs.append(doc) | ||
|
||
r = self.webhost.request('POST', 'eventhub_output_batch', | ||
data=json.dumps(docs)) | ||
self.assertEqual(r.status_code, 200) | ||
|
||
max_retries = 30 | ||
|
||
row_keys = [str(i) for i in range(NUM_EVENTS)] | ||
seen = [False] * NUM_EVENTS | ||
row_keys_seen = dict(zip(row_keys, seen)) | ||
for try_no in range(max_retries): | ||
# Allow trigger to fire. | ||
time.sleep(2) | ||
|
||
try: | ||
r = self.webhost.request('GET', | ||
'get_eventhub_batch_triggered') | ||
self.assertEqual(r.status_code, 200) | ||
entries = r.json() | ||
for entry in entries: | ||
self.assertEqual(entry['PartitionKey'], partition_key) | ||
row_key = entry['RowKey'] | ||
row_keys_seen[row_key] = True | ||
|
||
self.assertDictEqual(all_row_keys_seen, row_keys_seen) | ||
|
||
except AssertionError as e: | ||
if try_no == max_retries - 1: | ||
self._cleanup(old_partition_key) | ||
raise | ||
else: | ||
break | ||
|
||
self._cleanup(old_partition_key) | ||
|
||
def _cleanup(self, old_partition_key): | ||
self._set_table_partition_key(old_partition_key) | ||
|
||
def _get_table_partition_key(self): | ||
func_dict = self._get_table_function_json_dict() | ||
partition_key = func_dict['bindings'][1]['partitionKey'] | ||
return partition_key | ||
|
||
def _set_table_partition_key(self, partition_key): | ||
full_json_path = self._get_table_function_json_path() | ||
|
||
func_dict = self._get_table_function_json_dict() | ||
func_dict['bindings'][1]['partitionKey'] = partition_key | ||
|
||
with open(full_json_path, 'w') as f: | ||
json.dump(func_dict, f, indent=2) | ||
|
||
def _get_table_function_json_dict(self): | ||
full_json_path = self._get_table_function_json_path() | ||
|
||
with open(full_json_path, 'r') as f: | ||
func_dict = json.load(f) | ||
|
||
return func_dict | ||
|
||
def _get_table_function_json_path(self): | ||
script_dir = pathlib.Path(self.get_script_dir()) | ||
json_path = pathlib.Path('get_eventhub_batch_triggered/function.json') | ||
return testutils.TESTS_ROOT / script_dir / json_path |
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 not specify dataType. String is default
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.
Looks like we are failing on a JSON deserialization step when
dataType
is not specified asstring
. The SDK must be assuming that we are writing a JSON deserializable string by default. I was able to repro in Node. Will open an issue against the SDK.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.
Opened an issue on the EventHub extension: Azure/azure-functions-eventhubs-extension#17.