Skip to content

Commit 7c6f31e

Browse files
committed
Retry calls that open resumable upload in WriteChannel
1 parent ba74dc3 commit 7c6f31e

File tree

4 files changed

+84
-8
lines changed

4 files changed

+84
-8
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableDataWriteChannel.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@
2424
import com.google.cloud.RetryHelper;
2525
import com.google.cloud.WriteChannel;
2626

27+
import java.util.concurrent.Callable;
28+
2729
/**
2830
* WriteChannel implementation to stream data into a BigQuery table.
2931
*/
3032
class TableDataWriteChannel extends BaseWriteChannel<BigQueryOptions, WriteChannelConfiguration> {
3133

3234
TableDataWriteChannel(BigQueryOptions options,
3335
WriteChannelConfiguration writeChannelConfiguration) {
34-
this(options, writeChannelConfiguration, options.rpc().open(writeChannelConfiguration.toPb()));
36+
this(options, writeChannelConfiguration, open(options, writeChannelConfiguration));
3537
}
3638

3739
TableDataWriteChannel(BigQueryOptions options, WriteChannelConfiguration config,
@@ -58,6 +60,20 @@ protected StateImpl.Builder stateBuilder() {
5860
return StateImpl.builder(options(), entity(), uploadId());
5961
}
6062

63+
private static String open(final BigQueryOptions options,
64+
final WriteChannelConfiguration writeChannelConfiguration) {
65+
try {
66+
return runWithRetries(new Callable<String>() {
67+
@Override
68+
public String call() {
69+
return options.rpc().open(writeChannelConfiguration.toPb());
70+
}
71+
}, options.retryParams(), BigQueryImpl.EXCEPTION_HANDLER, options.clock());
72+
} catch (RetryHelper.RetryHelperException e) {
73+
throw BigQueryException.translateAndThrow(e);
74+
}
75+
}
76+
6177
static class StateImpl
6278
extends BaseWriteChannel.BaseState<BigQueryOptions, WriteChannelConfiguration> {
6379

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableDataWriteChannelTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@
3939
import org.easymock.CaptureType;
4040
import org.junit.After;
4141
import org.junit.Before;
42+
import org.junit.Rule;
4243
import org.junit.Test;
44+
import org.junit.rules.ExpectedException;
4345

4446
import java.io.IOException;
47+
import java.net.SocketException;
4548
import java.nio.ByteBuffer;
4649
import java.util.Arrays;
4750
import java.util.Random;
@@ -63,6 +66,9 @@ public class TableDataWriteChannelTest {
6366
private static final int CUSTOM_CHUNK_SIZE = 4 * MIN_CHUNK_SIZE;
6467
private static final Random RANDOM = new Random();
6568

69+
@Rule
70+
public ExpectedException thrown = ExpectedException.none();
71+
6672
private BigQueryOptions options;
6773
private BigQueryRpcFactory rpcFactoryMock;
6874
private BigQueryRpc bigqueryRpcMock;
@@ -72,8 +78,7 @@ public class TableDataWriteChannelTest {
7278
public void setUp() {
7379
rpcFactoryMock = createMock(BigQueryRpcFactory.class);
7480
bigqueryRpcMock = createMock(BigQueryRpc.class);
75-
expect(rpcFactoryMock.create(anyObject(BigQueryOptions.class)))
76-
.andReturn(bigqueryRpcMock);
81+
expect(rpcFactoryMock.create(anyObject(BigQueryOptions.class))).andReturn(bigqueryRpcMock);
7782
replay(rpcFactoryMock);
7883
options = BigQueryOptions.builder()
7984
.projectId("projectid")
@@ -94,6 +99,24 @@ public void testCreate() {
9499
assertTrue(writer.isOpen());
95100
}
96101

102+
@Test
103+
public void testCreateRetryableError() {
104+
BigQueryException exception = new BigQueryException(new SocketException("Socket closed"));
105+
expect(bigqueryRpcMock.open(LOAD_CONFIGURATION.toPb())).andThrow(exception);
106+
expect(bigqueryRpcMock.open(LOAD_CONFIGURATION.toPb())).andReturn(UPLOAD_ID);
107+
replay(bigqueryRpcMock);
108+
writer = new TableDataWriteChannel(options, LOAD_CONFIGURATION);
109+
assertTrue(writer.isOpen());
110+
}
111+
112+
@Test
113+
public void testCreateNonRetryableError() {
114+
expect(bigqueryRpcMock.open(LOAD_CONFIGURATION.toPb())).andThrow(new RuntimeException());
115+
replay(bigqueryRpcMock);
116+
thrown.expect(RuntimeException.class);
117+
new TableDataWriteChannel(options, LOAD_CONFIGURATION);
118+
}
119+
97120
@Test
98121
public void testWriteWithoutFlush() throws IOException {
99122
expect(bigqueryRpcMock.open(LOAD_CONFIGURATION.toPb())).andReturn(UPLOAD_ID);

google-cloud-storage/src/main/java/com/google/cloud/storage/BlobWriteChannel.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
import com.google.cloud.storage.spi.StorageRpc;
2727

2828
import java.util.Map;
29+
import java.util.concurrent.Callable;
2930

3031
/**
3132
* Write channel implementation to upload Google Cloud Storage blobs.
3233
*/
3334
class BlobWriteChannel extends BaseWriteChannel<StorageOptions, BlobInfo> {
3435

3536
BlobWriteChannel(StorageOptions options, BlobInfo blob, Map<StorageRpc.Option, ?> optionsMap) {
36-
this(options, blob, options.rpc().open(blob.toPb(), optionsMap));
37+
this(options, blob, open(options, blob, optionsMap));
3738
}
3839

3940
BlobWriteChannel(StorageOptions options, BlobInfo blobInfo, String uploadId) {
@@ -58,6 +59,20 @@ protected StateImpl.Builder stateBuilder() {
5859
return StateImpl.builder(options(), entity(), uploadId());
5960
}
6061

62+
private static String open(final StorageOptions options, final BlobInfo blob,
63+
final Map<StorageRpc.Option, ?> optionsMap) {
64+
try {
65+
return runWithRetries(new Callable<String>() {
66+
@Override
67+
public String call() {
68+
return options.rpc().open(blob.toPb(), optionsMap);
69+
}
70+
}, options.retryParams(), StorageImpl.EXCEPTION_HANDLER, options.clock());
71+
} catch (RetryHelper.RetryHelperException e) {
72+
throw StorageException.translateAndThrow(e);
73+
}
74+
}
75+
6176
static class StateImpl extends BaseWriteChannel.BaseState<StorageOptions, BlobInfo> {
6277

6378
private static final long serialVersionUID = -9028324143780151286L;

google-cloud-storage/src/test/java/com/google/cloud/storage/BlobWriteChannelTest.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import static org.junit.Assert.fail;
3232

3333
import com.google.cloud.RestorableState;
34-
import com.google.cloud.RetryParams;
3534
import com.google.cloud.WriteChannel;
3635
import com.google.cloud.storage.spi.StorageRpc;
3736
import com.google.cloud.storage.spi.StorageRpcFactory;
@@ -41,9 +40,12 @@
4140
import org.easymock.CaptureType;
4241
import org.junit.After;
4342
import org.junit.Before;
43+
import org.junit.Rule;
4444
import org.junit.Test;
45+
import org.junit.rules.ExpectedException;
4546

4647
import java.io.IOException;
48+
import java.net.SocketException;
4749
import java.nio.ByteBuffer;
4850
import java.util.Arrays;
4951
import java.util.Map;
@@ -61,6 +63,9 @@ public class BlobWriteChannelTest {
6163
private static final int CUSTOM_CHUNK_SIZE = 4 * MIN_CHUNK_SIZE;
6264
private static final Random RANDOM = new Random();
6365

66+
@Rule
67+
public ExpectedException thrown = ExpectedException.none();
68+
6469
private StorageOptions options;
6570
private StorageRpcFactory rpcFactoryMock;
6671
private StorageRpc storageRpcMock;
@@ -70,13 +75,11 @@ public class BlobWriteChannelTest {
7075
public void setUp() {
7176
rpcFactoryMock = createMock(StorageRpcFactory.class);
7277
storageRpcMock = createMock(StorageRpc.class);
73-
expect(rpcFactoryMock.create(anyObject(StorageOptions.class)))
74-
.andReturn(storageRpcMock);
78+
expect(rpcFactoryMock.create(anyObject(StorageOptions.class))).andReturn(storageRpcMock);
7579
replay(rpcFactoryMock);
7680
options = StorageOptions.builder()
7781
.projectId("projectid")
7882
.serviceRpcFactory(rpcFactoryMock)
79-
.retryParams(RetryParams.noRetries())
8083
.build();
8184
}
8285

@@ -93,6 +96,25 @@ public void testCreate() {
9396
assertTrue(writer.isOpen());
9497
}
9598

99+
@Test
100+
public void testCreateRetryableError() {
101+
StorageException exception = new StorageException(new SocketException("Socket closed"));
102+
expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andThrow(exception);
103+
expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID);
104+
replay(storageRpcMock);
105+
writer = new BlobWriteChannel(options, BLOB_INFO, EMPTY_RPC_OPTIONS);
106+
assertTrue(writer.isOpen());
107+
}
108+
109+
@Test
110+
public void testCreateNonRetryableError() {
111+
expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS))
112+
.andThrow(new RuntimeException());
113+
replay(storageRpcMock);
114+
thrown.expect(RuntimeException.class);
115+
new BlobWriteChannel(options, BLOB_INFO, EMPTY_RPC_OPTIONS);
116+
}
117+
96118
@Test
97119
public void testWriteWithoutFlush() throws IOException {
98120
expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID);

0 commit comments

Comments
 (0)