8000 BigQuery: insertAll always retries operation by ajaaym · Pull Request #4165 · googleapis/google-cloud-java · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.base.Supplier;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -548,17 +548,18 @@ public InsertAllResponse insertAll(InsertAllRequest request) {
// an anonymous inner class.
final boolean[] allInsertIdsSet = {true};
List<Rows> rowsPb =
Lists.transform(
request.getRows(),
new Function<RowToInsert, Rows>() {
@Override
public Rows apply(RowToInsert rowToInsert) {
allInsertIdsSet[0] &= rowToInsert.getId() != null;
return new Rows()
.setInsertId(rowToInsert.getId())
.setJson(rowToInsert.getContent());
}
});
FluentIterable.from(request.getRows())
.transform(
new Function<RowToInsert, Rows>() {
@Override
public Rows apply(RowToInsert rowToInsert) {
allInsertIdsSet[0] &= rowToInsert.getId() != null;
return new Rows()
.setInsertId(rowToInsert.getId())
.setJson(rowToInsert.getContent());
}
})
.toList();
requestPb.setRows(rowsPb);

TableDataInsertAllResponse responsePb;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ public void testUpdateTableWithSelectedFields() {
}

@Test
public void testInsertAll() {
public void testInsertAllWithRowIdShouldRetry() {
Map<String, Object> row1 = ImmutableMap.<String, Object>of("field", "value1");
Map<String, Object> row2 = ImmutableMap.<String, Object>of("field", "value2");
List<RowToInsert> rows =
Expand Down Expand Up @@ -836,17 +836,65 @@ public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) {
new TableDataInsertAllResponse.InsertErrors()
.setIndex(0L)
.setErrors(ImmutableList.of(new ErrorProto().setMessage("ErrorMessage")))));
EasyMock.expect(bigqueryRpcMock.insertAll(PROJECT, DATASET, TABLE, requestPb))
.andThrow(new BigQueryException(500, "InternalError"));
EasyMock.expect(bigqueryRpcMock.insertAll(PROJECT, DATASET, TABLE, requestPb))
.andReturn(responsePb);
EasyMock.replay(bigqueryRpcMock);
bigquery = options.getService();
bigquery =
options
.toBuilder()
.setRetrySettings(ServiceOptions.getDefaultRetrySettings())
.build()
.getService();
InsertAllResponse response = bigquery.insertAll(request);
assertNotNull(response.getErrorsFor(0L));
assertNull(response.getErrorsFor(1L));
assertEquals(1, response.getErrorsFor(0L).size());
assertEquals("ErrorMessage", response.getErrorsFor(0L).get(0).getMessage());
}

@Test
public void testInsertAllWithoutRowIdShouldNotRetry() {
Map<String, Object> row1 = ImmutableMap.<String, Object>of("field", "value1");
Map<String, Object> row2 = ImmutableMap.<String, Object>of("field", "value2");
List<RowToInsert> rows = ImmutableList.of(RowToInsert.of(row1), RowToInsert.of(row2));
InsertAllRequest request =
InsertAllRequest.newBuilder(TABLE_ID)
.setRows(rows)
.setSkipInvalidRows(false)
.setIgnoreUnknownValues(true)
.setTemplateSuffix("suffix")
.build();
TableDataInsertAllRequest requestPb =
new TableDataInsertAllRequest()
.setRows(
Lists.transform(
rows,
new Function<RowToInsert, TableDataInsertAllRequest.Rows>() {
@Override
public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) {
return new TableDataInsertAllRequest.Rows()
.setInsertId(rowToInsert.getId())
.setJson(rowToInsert.getContent());
}
}))
.setSkipInvalidRows(false)
.setIgnoreUnknownValues(true)
.setTemplateSuffix("suffix");
EasyMock.expect(bigqueryRpcMock.insertAll(PROJECT, DATASET, TABLE, requestPb))
.andThrow(new BigQueryException(500, "InternalError"));
EasyMock.replay(bigqueryRpcMock);
bigquery =
options
.toBuilder()
.setRetrySettings(ServiceOptions.getDefaultRetrySettings())
.build()
.getService();
thrown.expect(BigQueryException.class);
bigquery.insertAll(request);
}

@Test
public void testInsertAllWithProject() {
Map<String, Object> row1 = ImmutableMap.<String, Object>of("field", "value1");
Expand Down
0