Skip to content

Commit

Permalink
Adding retry if concurrent update exception during indexing build (#1152
Browse files Browse the repository at this point in the history
)

* adding retry if concurrent update

* moving comment down

* Adding sleep on retry
  • Loading branch information
freemabd authored Jan 30, 2025
1 parent aa86005 commit 6dd4c62
Showing 1 changed file with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.api.services.bigquery.model.TableFieldSchema;
import com.google.api.services.bigquery.model.TableRow;
import com.google.api.services.bigquery.model.TableSchema;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.Table;
import jakarta.annotation.Nullable;
import java.util.ArrayList;
Expand Down Expand Up @@ -159,9 +160,36 @@ public void run(boolean isDryRun) {
LOGGER.info("Rollup counts source SQL is defined. Skipping Dataflow job.");
}

// Dataflow jobs can only write new rows to BigQuery, so in this second step, copy over the
// count values to the corresponding column in the index entity main table.
copyFieldsToEntityTable(isDryRun);
final int maxRetries = 5;
var attempt = 0;

while (attempt < maxRetries) {
try {
// Dataflow jobs can only write new rows to BigQuery, so in this second step, copy over the
// count values to the corresponding column in the index entity main table.
copyFieldsToEntityTable(isDryRun);
return; // Exit if successful
} catch (BigQueryException e) {
if (e.getMessage().contains("due to concurrent update")) {
attempt++;
LOGGER.info("Attempt {} failed: {}", attempt, e.getMessage());

if (attempt == maxRetries) {
LOGGER.info("Max retries reached. Giving up.");
throw e; // Rethrow after max attempts
}

try {
Thread.sleep(1000); // Sleep for 1 second
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException("Retry interrupted", ie);
}
} else {
throw e; // Rethrow if it's a different exception
}
}
}
}

@Override
Expand Down

0 comments on commit 6dd4c62

Please sign in to comment.