|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.datastore.spi.v1; |
| 18 | + |
| 19 | +import static com.google.cloud.datastore.DatastoreUtils.isLocalHost; |
| 20 | +import static com.google.cloud.datastore.DatastoreUtils.removeScheme; |
| 21 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 22 | + |
| 23 | +import com.google.api.core.ApiFunction; |
| 24 | +import com.google.api.core.InternalApi; |
| 25 | +import com.google.api.gax.core.BackgroundResource; |
| 26 | +import com.google.api.gax.core.GaxProperties; |
| 27 | +import com.google.api.gax.grpc.GrpcCallContext; |
| 28 | +import com.google.api.gax.grpc.GrpcTransportChannel; |
| 29 | +import com.google.api.gax.rpc.ClientContext; |
| 30 | +import com.google.api.gax.rpc.HeaderProvider; |
| 31 | +import com.google.api.gax.rpc.NoHeaderProvider; |
| 32 | +import com.google.api.gax.rpc.TransportChannel; |
| 33 | +import com.google.api.gax.rpc.UnaryCallSettings; |
| 34 | +import com.google.cloud.NoCredentials; |
| 35 | +import com.google.cloud.ServiceOptions; |
| 36 | +import com.google.cloud.datastore.DatastoreException; |
| 37 | +import com.google.cloud.datastore.DatastoreOptions; |
| 38 | +import com.google.cloud.datastore.v1.DatastoreSettings; |
| 39 | +import com.google.cloud.datastore.v1.stub.DatastoreStubSettings; |
| 40 | +import com.google.cloud.datastore.v1.stub.GrpcDatastoreStub; |
| 41 | +import com.google.cloud.grpc.GrpcTransportOptions; |
| 42 | +import com.google.common.base.Strings; |
| 43 | +import com.google.datastore.v1.AllocateIdsRequest; |
| 44 | +import com.google.datastore.v1.AllocateIdsResponse; |
| 45 | +import com.google.datastore.v1.BeginTransactionRequest; |
| 46 | +import com.google.datastore.v1.BeginTransactionResponse; |
| 47 | +import com.google.datastore.v1.CommitRequest; |
| 48 | +import com.google.datastore.v1.CommitResponse; |
| 49 | +import com.google.datastore.v1.LookupRequest; |
| 50 | +import com.google.datastore.v1.LookupResponse; |
| 51 | +import com.google.datastore.v1.ReserveIdsRequest; |
| 52 | +import com.google.datastore.v1.ReserveIdsResponse; |
| 53 | +import com.google.datastore.v1.RollbackRequest; |
| 54 | +import com.google.datastore.v1.RollbackResponse; |
| 55 | +import com.google.datastore.v1.RunAggregationQueryRequest; |
| 56 | +import com.google.datastore.v1.RunAggregationQueryResponse; |
| 57 | +import com.google.datastore.v1.RunQueryRequest; |
| 58 | +import com.google.datastore.v1.RunQueryResponse; |
| 59 | +import io.grpc.CallOptions; |
| 60 | +import io.grpc.ManagedChannel; |
| 61 | +import io.grpc.ManagedChannelBuilder; |
| 62 | +import java.io.IOException; |
| 63 | +import java.util.Collections; |
| 64 | + |
| 65 | +@InternalApi |
| 66 | +public class GrpcDatastoreRpc implements AutoCloseable, DatastoreRpc { |
| 67 | + |
| 68 | + private final GrpcDatastoreStub datastoreStub; |
| 69 | + private final ClientContext clientContext; |
| 70 | + private boolean closed; |
| 71 | + |
| 72 | + public GrpcDatastoreRpc(DatastoreOptions datastoreOptions) throws IOException { |
| 73 | + |
| 74 | + try { |
| 75 | + clientContext = |
| 76 | + isEmulator(datastoreOptions) |
| 77 | + ? getClientContextForEmulator(datastoreOptions) |
| 78 | + : getClientContext(datastoreOptions); |
| 79 | + ApiFunction<UnaryCallSettings.Builder<?, ?>, Void> retrySettingsSetter = |
| 80 | + builder -> { |
| 81 | + builder.setRetrySettings(datastoreOptions.getRetrySettings()); |
| 82 | + return null; |
| 83 | + }; |
| 84 | + DatastoreStubSettings datastoreStubSettings = |
| 85 | + DatastoreStubSettings.newBuilder(clientContext) |
| 86 | + .applyToAllUnaryMethods(retrySettingsSetter) |
| 87 | + .build(); |
| 88 | + datastoreStub = GrpcDatastoreStub.create(datastoreStubSettings); |
| 89 | + } catch (IOException e) { |
| 90 | + throw new IOException(e); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void close() throws Exception { |
| 96 | + if (!closed) { |
| 97 | + datastoreStub.close(); |
| 98 | + for (BackgroundResource resource : clientContext.getBackgroundResources()) { |
| 99 | + resource.close(); |
| 100 | + } |
| 101 | + closed = true; |
| 102 | + } |
| 103 | + for (BackgroundResource resource : clientContext.getBackgroundResources()) { |
| 104 | + resource.awaitTermination(1, SECONDS); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public AllocateIdsResponse allocateIds(AllocateIdsRequest request) { |
| 110 | + return datastoreStub.allocateIdsCallable().call(request); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public BeginTransactionResponse beginTransaction(BeginTransactionRequest request) |
| 115 | + throws DatastoreException { |
| 116 | + return datastoreStub.beginTransactionCallable().call(request); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public CommitResponse commit(CommitRequest request) { |
| 121 | + return datastoreStub.commitCallable().call(request); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public LookupResponse lookup(LookupRequest request) { |
| 126 | + return datastoreStub.lookupCallable().call(request); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public ReserveIdsResponse reserveIds(ReserveIdsRequest request) { |
| 131 | + return datastoreStub.reserveIdsCallable().call(request); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public RollbackResponse rollback(RollbackRequest request) { |
| 136 | + return datastoreStub.rollbackCallable().call(request); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public RunQueryResponse runQuery(RunQueryRequest request) { |
| 141 | + return datastoreStub.runQueryCallable().call(request); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public RunAggregationQueryResponse runAggregationQuery(RunAggregationQueryRequest request) { |
| 146 | + return datastoreStub.runAggregationQueryCallable().call(request); |
| 147 | + } |
| 148 | + |
| 149 | + private boolean isEmulator(DatastoreOptions datastoreOptions) { |
| 150 | + return isLocalHost(datastoreOptions.getHost()) |
| 151 | + || NoCredentials.getInstance().equals(datastoreOptions.getCredentials()); |
| 152 | + } |
| 153 | + |
| 154 | + private ClientContext getClientContextForEmulator(DatastoreOptions datastoreOptions) |
| 155 | + throws IOException { |
| 156 | + ManagedChannel managedChannel = |
| 157 | + ManagedChannelBuilder.forTarget(removeScheme(datastoreOptions.getHost())) |
| 158 | + .usePlaintext() |
| 159 | + .build(); |
| 160 | + TransportChannel transportChannel = GrpcTransportChannel.create(managedChannel); |
| 161 | + return ClientContext.newBuilder() |
| 162 | + .setCredentials(null) |
| 163 | + .setTransportChannel(transportChannel) |
| 164 | + .setDefaultCallContext(GrpcCallContext.of(managedChannel, CallOptions.DEFAULT)) |
| 165 | + .setBackgroundResources(Collections.singletonList(transportChannel)) |
| 166 | + .build(); |
| 167 | + } |
| 168 | + |
| 169 | + private ClientContext getClientContext(DatastoreOptions datastoreOptions) throws IOException { |
| 170 | + HeaderProvider internalHeaderProvider = |
| 171 | + DatastoreSettings.defaultApiClientHeaderProviderBuilder() |
| 172 | + .setClientLibToken( |
| 173 | + ServiceOptions.getGoogApiClientLibName(), |
| 174 | + GaxProperties.getLibraryVersion(datastoreOptions.getClass())) |
| 175 | + .setResourceToken(getResourceToken(datastoreOptions)) |
| 176 | + .build(); |
| 177 | + |
| 178 | + DatastoreSettingsBuilder settingsBuilder = |
| 179 | + new DatastoreSettingsBuilder(DatastoreSettings.newBuilder().build()); |
| 180 | + settingsBuilder.setCredentialsProvider( |
| 181 | + GrpcTransportOptions.setUpCredentialsProvider(datastoreOptions)); |
| 182 | + settingsBuilder.setTransportChannelProvider( |
| 183 | + GrpcTransportOptions.setUpChannelProvider( |
| 184 | + DatastoreSettings.defaultGrpcTransportProviderBuilder(), datastoreOptions)); |
| 185 | + settingsBuilder.setInternalHeaderProvider(internalHeaderProvider); |
| 186 | + settingsBuilder.setHeaderProvider( |
| 187 | + datastoreOptions.getMergedHeaderProvider(new NoHeaderProvider())); |
| 188 | + ClientContext clientContext = ClientContext.create(settingsBuilder.build()); |
| 189 | + return clientContext; |
| 190 | + } |
| 191 | + |
| 192 | + private String getResourceToken(DatastoreOptions datastoreOptions) { |
| 193 | + StringBuilder builder = new StringBuilder("project_id="); |
| 194 | + builder.append(datastoreOptions.getProjectId()); |
| 195 | + if (!Strings.isNullOrEmpty(datastoreOptions.getDatabaseId())) { |
| 196 | + builder.append("&database_id="); |
| 197 | + builder.append(datastoreOptions.getDatabaseId()); |
| 198 | + } |
| 199 | + return builder.toString(); |
| 200 | + } |
| 201 | + |
| 202 | + // This class is needed solely to get access to protected method setInternalHeaderProvider() |
| 203 | + private static class DatastoreSettingsBuilder extends DatastoreSettings.Builder { |
| 204 | + |
| 205 | + private DatastoreSettingsBuilder(DatastoreSettings settings) { |
| 206 | + super(settings); |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + protected DatastoreSettings.Builder setInternalHeaderProvider( |
| 211 | + HeaderProvider internalHeaderProvider) { |
| 212 | + return super.setInternalHeaderProvider(internalHeaderProvider); |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments