Skip to content

Commit 3ca4123

Browse files
committed
chore: update GrpcStorageOptions to preserve the StorageSettings used to construct the StorageClient when zero copy is used
1 parent 8553cb9 commit 3ca4123

File tree

1 file changed

+69
-11
lines changed

1 file changed

+69
-11
lines changed

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

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.google.api.gax.grpc.GrpcCallSettings;
3232
import com.google.api.gax.grpc.GrpcInterceptorProvider;
3333
import com.google.api.gax.grpc.GrpcStubCallableFactory;
34+
import com.google.api.gax.grpc.GrpcTransportChannel;
3435
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
3536
import com.google.api.gax.retrying.RetrySettings;
3637
import com.google.api.gax.rpc.BidiStreamingCallable;
@@ -79,6 +80,7 @@
7980
import com.google.storage.v2.StorageSettings;
8081
import com.google.storage.v2.stub.GrpcStorageCallableFactory;
8182
import com.google.storage.v2.stub.GrpcStorageStub;
83+
import com.google.storage.v2.stub.StorageStub;
8284
import com.google.storage.v2.stub.StorageStubSettings;
8385
import io.grpc.ClientInterceptor;
8486
import io.grpc.Detachable;
@@ -810,15 +812,17 @@ public Storage create(StorageOptions options) {
810812
Opts<UserProject> defaultOpts = t.y();
811813
if (ZeroCopyReadinessChecker.isReady()) {
812814
LOGGER.config("zero-copy protobuf deserialization available, using it");
813-
StorageStubSettings stubSettings =
815+
StorageStubSettings baseSettings =
814816
(StorageStubSettings) storageSettings.getStubSettings();
815-
ClientContext clientContext = ClientContext.create(stubSettings);
816-
GrpcStorageCallableFactory grpcStorageCallableFactory =
817-
new GrpcStorageCallableFactory();
818-
InternalZeroCopyGrpcStorageStub stub =
819-
new InternalZeroCopyGrpcStorageStub(
820-
stubSettings, clientContext, grpcStorageCallableFactory);
821-
StorageClient client = new InternalStorageClient(stub);
817+
InternalStorageStubSettings.Builder internalStorageStubSettingsBuilder =
818+
new InternalStorageStubSettings.Builder(baseSettings);
819+
InternalStorageSettings.Builder settingsBuilder =
820+
new InternalStorageSettings.Builder(internalStorageStubSettingsBuilder);
821+
InternalStorageSettings internalStorageSettingsBuilder =
822+
new InternalStorageSettings(settingsBuilder);
823+
InternalStorageClient client =
824+
new InternalStorageClient(internalStorageSettingsBuilder);
825+
InternalZeroCopyGrpcStorageStub stub = client.getStub();
822826
GrpcStorageImpl grpcStorage =
823827
new GrpcStorageImpl(
824828
grpcStorageOptions,
@@ -928,8 +932,8 @@ private Object readResolve() {
928932

929933
private static final class InternalStorageClient extends StorageClient {
930934

931-
private InternalStorageClient(InternalZeroCopyGrpcStorageStub stub) {
932-
super(stub);
935+
private InternalStorageClient(StorageSettings settings) throws IOException {
936+
super(settings);
933937
}
934938

935939
@Override
@@ -939,7 +943,7 @@ public void shutdownNow() {
939943
// instead hook in here to close out the zero-copy marshaller
940944
//noinspection EmptyTryBlock
941945
try (ZeroCopyResponseMarshaller<ReadObjectResponse> ignore1 =
942-
getStub().readObjectResponseMarshaller;
946+
getStub().readObjectResponseMarshaller;
943947
ZeroCopyResponseMarshaller<BidiReadObjectResponse> ignore2 =
944948
getStub().bidiReadObjectResponseMarshaller) {
945949
// use try-with to do the close dance for us
@@ -957,6 +961,60 @@ public InternalZeroCopyGrpcStorageStub getStub() {
957961
}
958962
}
959963

964+
private static final class InternalStorageSettings extends StorageSettings {
965+
966+
private InternalStorageSettings(Builder settingsBuilder) throws IOException {
967+
super(settingsBuilder);
968+
}
969+
970+
private static final class Builder extends StorageSettings.Builder {
971+
private Builder(StorageStubSettings.Builder stubSettings) {
972+
super(stubSettings);
973+
}
974+
975+
@Override
976+
public InternalStorageSettings build() throws IOException {
977+
return new InternalStorageSettings(this);
978+
}
979+
}
980+
}
981+
982+
private static final class InternalStorageStubSettings extends StorageStubSettings {
983+
984+
private InternalStorageStubSettings(Builder settingsBuilder) throws IOException {
985+
super(settingsBuilder);
986+
}
987+
988+
@Override
989+
public StorageStub createStub() throws IOException {
990+
if (!getTransportChannelProvider()
991+
.getTransportName()
992+
.equals(GrpcTransportChannel.getGrpcTransportName())) {
993+
throw new UnsupportedOperationException(
994+
String.format(
995+
"Transport not supported: %s", getTransportChannelProvider().getTransportName()));
996+
}
997+
998+
ClientContext clientContext = ClientContext.create(this);
999+
GrpcStorageCallableFactory grpcStorageCallableFactory = new GrpcStorageCallableFactory();
1000+
InternalZeroCopyGrpcStorageStub stub =
1001+
new InternalZeroCopyGrpcStorageStub(this, clientContext, grpcStorageCallableFactory);
1002+
return stub;
1003+
}
1004+
1005+
private static final class Builder extends StorageStubSettings.Builder {
1006+
1007+
private Builder(StorageStubSettings settings) {
1008+
super(settings);
1009+
}
1010+
1011+
@Override
1012+
public InternalStorageStubSettings build() throws IOException {
1013+
return new InternalStorageStubSettings(this);
1014+
}
1015+
}
1016+
}
1017+
9601018
// DanglingJavadocs are for breadcrumbs to source of copied generated code
9611019
@SuppressWarnings("DanglingJavadoc")
9621020
private static final class InternalZeroCopyGrpcStorageStub extends GrpcStorageStub

0 commit comments

Comments
 (0)