-
Notifications
You must be signed in to change notification settings - Fork 835
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
bulk insert batch requests #3628
Merged
axsaucedo
merged 2 commits into
SeldonIO:master
from
michaelcheah:request-logger-bulk-update-batches
Oct 2, 2021
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
import array | ||
import traceback | ||
from flask import jsonify | ||
from elasticsearch import Elasticsearch, helpers | ||
|
||
MAX_PAYLOAD_BYTES = 300000 | ||
app = Flask(__name__) | ||
|
@@ -146,29 +147,9 @@ def process_and_update_elastic_doc( | |
if type(new_content_part["instance"]) == type([]) and not (new_content_part["dataType"] == "json"): | ||
# if we've a list then this is batch | ||
# we assume first dimension is always batch | ||
|
||
no_items_in_batch = len(new_content_part["instance"]) | ||
index = 0 | ||
elements = None | ||
if "elements" in new_content_part: | ||
elements = new_content_part["elements"] | ||
|
||
for num, item in enumerate(new_content_part["instance"],start=0): | ||
|
||
item_body = doc_body.copy() | ||
|
||
item_body[message_type]["instance"] = item | ||
|
||
if type(elements) == type([]) and len(elements) > num: | ||
item_body[message_type]["elements"] = elements[num] | ||
|
||
item_request_id = build_request_id_batched( | ||
request_id, no_items_in_batch, index | ||
) | ||
added_content.append(upsert_doc_to_elastic( | ||
elastic_object, message_type, item_body, item_request_id, index_name | ||
)) | ||
index = index + 1 | ||
item_body = doc_body.copy() | ||
bulk_upsert_doc_to_elastic(elastic_object, message_type, item_body, | ||
item_body[message_type].copy(), request_id, index_name) | ||
else: | ||
#not batch so don't batch elements either | ||
if "elements" in new_content_part and type(new_content_part["elements"]) == type([]): | ||
|
@@ -301,6 +282,40 @@ def upsert_doc_to_elastic( | |
return new_content | ||
|
||
|
||
def bulk_upsert_doc_to_elastic( | ||
elastic_object: Elasticsearch, message_type, doc_body, new_content_part, request_id, index_name | ||
): | ||
log_mapping.get_log_metadata(elastic_object, message_type, doc_body, request_id, index_name) | ||
no_items_in_batch = len(new_content_part["instance"]) | ||
elements = None | ||
if "elements" in new_content_part: | ||
elements = new_content_part["elements"] | ||
|
||
def gen_data(): | ||
for num, item in enumerate(new_content_part["instance"], start=0): | ||
print(f"bulk inserting item {num}") | ||
item_body = doc_body.copy() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this redundant? |
||
item_body[message_type]["instance"] = item | ||
if type(elements) == type([]) and len(elements) > num: | ||
item_body[message_type]["elements"] = elements[num] | ||
|
||
item_request_id = build_request_id_batched( | ||
request_id, no_items_in_batch, num | ||
) | ||
|
||
yield { | ||
"_index": index_name, | ||
"_type": "_doc", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use helper function |
||
"_op_type": "update", | ||
"_id": item_request_id, | ||
"_source": {"doc_as_upsert": True, "doc": item_body}, | ||
} | ||
|
||
helpers.bulk( | ||
elastic_object, gen_data(), refresh=True | ||
) | ||
|
||
|
||
# take request or response part and process it by deriving metadata | ||
def process_content(message_type, content, headers): | ||
|
||
|
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.
print the
item_request_id
that yielded