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

Key by paneindex and reshuffle before loading files. #34324

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions sdks/python/apache_beam/io/gcp/bigquery_file_loads.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,19 +664,14 @@ def start_bundle(self):
self.pending_jobs = []
self.schema_cache = {}

def process(
self,
element,
load_job_name_prefix,
pane_info=beam.DoFn.PaneInfoParam,
Copy link
Contributor

Choose a reason for hiding this comment

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

is this safe to remove pane_info?

Copy link
Collaborator Author

@claudevdm claudevdm Mar 17, 2025

Choose a reason for hiding this comment

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

We are still using pane info, but just adding it earlier as part of the key that gets processed by TriggerLoadJob

*schema_side_inputs):
def process(self, element, load_job_name_prefix, *schema_side_inputs):
# Each load job is assumed to have files respecting these constraints:
# 1. Total size of all files < 15 TB (Max size for load jobs)
# 2. Total no. of files in a single load job < 10,000
# This assumption means that there will always be a single load job
# triggered for each partition of files.
destination = element[0]
partition_key, files = element[1]
partition_key, files, pane_index = element[1]

if callable(self.schema):
schema = self.schema(destination, *schema_side_inputs)
Expand Down Expand Up @@ -705,7 +700,7 @@ def process(
table_reference.datasetId,
table_reference.tableId))
job_name = '%s_%s_pane%s_partition%s' % (
load_job_name_prefix, destination_hash, pane_info.index, partition_key)
load_job_name_prefix, destination_hash, pane_index, partition_key)
_LOGGER.info('Load job has %s files. Job name is %s.', len(files), job_name)

create_disposition = self.create_disposition
Expand Down Expand Up @@ -1104,6 +1099,8 @@ def _load_data(
# Load data using temp tables
trigger_loads_outputs = (
partitions_using_temp_tables
| "KeyByPaneIndexWithTempTables" >> beam.ParDo(KeyByPaneIndex())
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to worry about the update compatibility?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I will make those changes before merging this

| "ReshuffleBeforeLoadWithTempTables" >> beam.Reshuffle()
| "TriggerLoadJobsWithTempTables" >> beam.ParDo(
TriggerLoadJobs(
schema=self.schema,
Expand Down Expand Up @@ -1186,6 +1183,8 @@ def _load_data(
# Load data directly to destination table
destination_load_job_ids_pc = (
partitions_direct_to_destination
| "KeyByPaneIndexWithoutTempTables" >> beam.ParDo(KeyByPaneIndex())
| "ReshuffleBeforeLoadWithoutTempTables" >> beam.Reshuffle()
| "TriggerLoadJobsWithoutTempTables" >> beam.ParDo(
TriggerLoadJobs(
schema=self.schema,
Expand Down Expand Up @@ -1313,3 +1312,9 @@ def expand(self, pcoll):
self.DESTINATION_FILE_PAIRS: all_destination_file_pairs_pc,
self.DESTINATION_COPY_JOBID_PAIRS: destination_copy_job_ids_pc,
}


class KeyByPaneIndex(beam.DoFn):
def process(self, element, pane_info=beam.DoFn.PaneInfoParam):
destination, (partition_key, files) = element
return [(destination, (partition_key, files, pane_info.index))]
Copy link
Contributor

Choose a reason for hiding this comment

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

This is very similar to a first attempt to fix the same issue in Java - #28272 add pane info in "WritePartition" so it is survived in ReShuffle. In earlier discussion it was decided to go with fixing ReShuffle itself (and that PR was closed). Shall we go for same decision here?

Copy link
Contributor

@Abacn Abacn Mar 18, 2025

Choose a reason for hiding this comment

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

One of the original concern was upgrade compatibility. However because a ReShuffle is added here, pipeline graph will be changed anyways

Copy link
Contributor

Choose a reason for hiding this comment

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

Does it make sense to do this keying by pane in the upstream PartitionFiles DoFn? i.e. instead of creating a new dedicated step.

I think that'll make it easier to revert this quick fix when Reshuffle gets fixed (which should be the longterm solution)

8 changes: 4 additions & 4 deletions sdks/python/apache_beam/io/gcp/bigquery_file_loads_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,8 @@ def test_wait_for_load_job_completion(self, sleep_mock):
bq_client.jobs.Get.side_effect = [
job_1_waiting, job_2_done, job_1_done, job_2_done
]
partition_1 = ('project:dataset.table0', (0, ['file0']))
partition_2 = ('project:dataset.table1', (1, ['file1']))
partition_1 = ('project:dataset.table0', (0, ['file0'], 0))
partition_2 = ('project:dataset.table1', (1, ['file1'], 0))
bq_client.jobs.Insert.side_effect = [job_1, job_2]
test_job_prefix = "test_job"

Expand Down Expand Up @@ -636,8 +636,8 @@ def test_one_load_job_failed_after_waiting(self, sleep_mock):
bq_client.jobs.Get.side_effect = [
job_1_waiting, job_2_done, job_1_error, job_2_done
]
partition_1 = ('project:dataset.table0', (0, ['file0']))
partition_2 = ('project:dataset.table1', (1, ['file1']))
partition_1 = ('project:dataset.table0', (0, ['file0'], 0))
partition_2 = ('project:dataset.table1', (1, ['file1'], 0))
bq_client.jobs.Insert.side_effect = [job_1, job_2]
test_job_prefix = "test_job"

Expand Down
Loading