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

Add parallel upload for BulkImportWriter #134

Merged
merged 6 commits into from
Aug 29, 2024
Merged

Add parallel upload for BulkImportWriter #134

merged 6 commits into from
Aug 29, 2024

Conversation

chezou
Copy link
Member

@chezou chezou commented Aug 16, 2024

This patch introduces parallel upload capability to BulkImportWriter.

It makes a chunk of 10,000 records and uploads in parallel. Also, it reduces memory consumption for msgpack format uploading.

Upload speed becomes x4.4 faster, and memory consumption can be controlled by using Temporary file and chunk_record_size number.

Dummy data creation for benchmark

>>> import numpy as np; import pandas as pd
>>> def fake_data(n):
...    users = np.random.choice([0., 1., 2.], (n, 1))
...    items = np.random.choice([0., 1., 2.], (n, 1))
...    weight = np.random.rand(n,1)
...    return np.concatenate((users, items, weight), axis=1)
...
>>> d1 = fake_data(10_000_000)
>>> df = pd.DataFrame(d1, columns=["users", "items", "scores"])
>>> import pytd; import os
>>> client=pytd.Client(database="aki", apikey=os.environ["TD_API_KEY"])

Upload with a single thread

>>> import time
>>> s=time.time()
>>> client.load_table_from_dataframe(df, "aki.pytd_bi_test", writer="bulk_import", if_exists="overwrite", fmt="msgpack", max_workers=1, chunk_record_size=10_000_000)
>>> print(f"elapsed time:{time.time() - s} sec")
uploading data converted into a msgpack file
uploaded data in 64.06 sec
performing a bulk import job
[job id 2172780406] imported 10000000 records.
elapsed time:281.57144117355347 sec

Upload with 6 threads

>>> import pytd; import os
>>> import time
>>> s=time.time()
>>> client.load_table_from_dataframe(df, "aki.pytd_bi_test", writer="bulk_import", if_exists="overwrite", fmt="msgpack", max_workers=6, chunk_record_size=1_000_000)
>>> print(f"elapsed time:{time.time() - s} sec")
uploading data converted into a msgpack file
uploaded data in 14.56 sec
performing a bulk import job
[job id 2172780795] imported 10000000 records.
elapsed time:209.5141899585724 sec

@chezou chezou requested a review from tung-vu-td August 17, 2024 03:46
This change is to avoid the need to keep the entire msgpack in memory,
which can be a problem for large data sets.
@chezou chezou requested a review from shroman August 27, 2024 17:08
pytd/writer.py Outdated Show resolved Hide resolved
pytd/writer.py Outdated
@@ -449,7 +482,7 @@ def _bulk_import(self, table, file_like, if_exists, fmt="csv"):
table : :class:`pytd.table.Table`
Target table.

file_like : File like object
file_like : List of file like objects
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
file_like : List of file like objects
file_likes : List of file like objects

pytd/writer.py Outdated
stack.close()

def _bulk_import(self, table, file_like, if_exists, fmt="csv"):
def _bulk_import(self, table, file_like, if_exists, fmt="csv", max_workers=5):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def _bulk_import(self, table, file_like, if_exists, fmt="csv", max_workers=5):
def _bulk_import(self, table, file_likes, if_exists, fmt="csv", max_workers=5):

pytd/writer.py Outdated
# To skip API._prepare_file(), which recreate msgpack again.
bulk_import.upload_part("part", file_like, size)
with ThreadPoolExecutor(max_workers=max_workers) as executor:
for i, fp in enumerate(file_like):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for i, fp in enumerate(file_like):
for i, fp in enumerate(file_likes):

pytd/writer.py Outdated
else:
bulk_import.upload_file("part", fmt, file_like)
fp = file_like[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
fp = file_like[0]
fp = file_likes[0]

@@ -542,7 +591,9 @@ def _write_msgpack_stream(self, items, stream):
mp = packer.pack(normalized_msgpack(item))
gz.write(mp)

stream.seek(0)
Copy link
Contributor

Choose a reason for hiding this comment

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

did this #seek become unnecessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it's moved to here https://github.com/treasure-data/pytd/pull/134/files#diff-7e3490636bfbd0197f47ef9694662f2621beb294fb178b5185c8f257082c53e7R533
This is because to get file size bystream.tell() properly, #seek should happen later.

df.to_dict(orient="records"), fp
)
api_client.create_bulk_import().upload_part.assert_called_with(
"part-0", ANY, 62
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe I missed something, but where this 62 comes from?

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to get the file size from fp, but I gave up because it doesn't match with the actual file size in the _write_msgpack_stream() method.

@chezou chezou merged commit b560475 into master Aug 29, 2024
21 checks passed
@chezou chezou deleted the parallel-upload branch August 29, 2024 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants