-
Notifications
You must be signed in to change notification settings - Fork 50
refactor: Swap usage of HttpDatastoreRpc with GrpcDatastoreRpc #1240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jainsahab
merged 26 commits into
googleapis:V3-experimental
from
jainsahab:V3-experimental-workspace
Nov 27, 2023
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
e17ba34
Create basic structure of GrpcDatastoreRpc and using it in DatastoreO…
jainsahab f9e455e
applying unary settings to all the unary methods
jainsahab 231deef
Configuring header provider for GrpcDatastoreRpc
jainsahab 806f997
fixing emulator tests to be able to run successfully with grpc now
jainsahab 0c8704c
ignoring one more test which will be fixed in actionable error implem…
jainsahab d6ed231
Making HttpDatastoreRpc completely unused
jainsahab e257a91
Making GrpcDatastoreRpc implement AutoCloseable
jainsahab 2984146
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] acfbaef
incorporating feedbacks
jainsahab 60ee45d
pinging emulator after each test for debugging
jainsahab d42e3b9
Revert "pinging emulator after each test for debugging"
jainsahab 1d9d7e6
Reapply "pinging emulator after each test for debugging"
jainsahab d7b652e
more debugging
jainsahab 9827fb9
Constant ping to avoid flaky behaviour of /shutdown endpoint
jainsahab 38725f0
fixing test
jainsahab b15a9a9
checking if emulator is running before sending a shutdown command
jainsahab ec38885
fix lint
jainsahab 7bd2c55
implement helper method for localhost
jainsahab 0517cb6
fix header lint
jainsahab 7f4ce8d
moving emulator health check to src/test
jainsahab ef5f002
fix lint
jainsahab 9b43798
adding no extra headers
jainsahab cb80dd1
minor cleanup
jainsahab c86a702
using mutlipleAttemptsRule in DatastoreTest
jainsahab 1351c97
Revert "adding no extra headers"
jainsahab e5b2565
using classRule
jainsahab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * Copyright 2023 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.datastore; | ||
|
|
||
| import com.google.api.core.InternalApi; | ||
| import com.google.common.base.Strings; | ||
| import java.net.InetAddress; | ||
| import java.net.URL; | ||
|
|
||
| @InternalApi | ||
| public class DatastoreUtils { | ||
|
|
||
| public static boolean isLocalHost(String host) { | ||
| if (Strings.isNullOrEmpty(host)) { | ||
| return false; | ||
| } | ||
| try { | ||
| String normalizedHost = "http://" + removeScheme(host); | ||
| InetAddress hostAddr = InetAddress.getByName(new URL(normalizedHost).getHost()); | ||
| return hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress(); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| public static String removeScheme(String url) { | ||
| if (url != null) { | ||
| if (url.startsWith("https://")) { | ||
| return url.substring("https://".length()); | ||
| } else if (url.startsWith("http://")) { | ||
| return url.substring("http://".length()); | ||
| } | ||
| } | ||
| return url; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
215 changes: 215 additions & 0 deletions
215
google-cloud-datastore/src/main/java/com/google/cloud/datastore/spi/v1/GrpcDatastoreRpc.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| /* | ||
| * Copyright 2023 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.datastore.spi.v1; | ||
|
|
||
| import static com.google.cloud.datastore.DatastoreUtils.isLocalHost; | ||
| import static com.google.cloud.datastore.DatastoreUtils.removeScheme; | ||
| import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
|
||
| import com.google.api.core.ApiFunction; | ||
| import com.google.api.core.InternalApi; | ||
| import com.google.api.gax.core.BackgroundResource; | ||
| import com.google.api.gax.core.GaxProperties; | ||
| import com.google.api.gax.grpc.GrpcCallContext; | ||
| import com.google.api.gax.grpc.GrpcTransportChannel; | ||
| import com.google.api.gax.rpc.ClientContext; | ||
| import com.google.api.gax.rpc.HeaderProvider; | ||
| import com.google.api.gax.rpc.NoHeaderProvider; | ||
| import com.google.api.gax.rpc.TransportChannel; | ||
| import com.google.api.gax.rpc.UnaryCallSettings; | ||
| import com.google.cloud.NoCredentials; | ||
| import com.google.cloud.ServiceOptions; | ||
| import com.google.cloud.datastore.DatastoreException; | ||
| import com.google.cloud.datastore.DatastoreOptions; | ||
| import com.google.cloud.datastore.v1.DatastoreSettings; | ||
| import com.google.cloud.datastore.v1.stub.DatastoreStubSettings; | ||
| import com.google.cloud.datastore.v1.stub.GrpcDatastoreStub; | ||
| import com.google.cloud.grpc.GrpcTransportOptions; | ||
| import com.google.common.base.Strings; | ||
| import com.google.datastore.v1.AllocateIdsRequest; | ||
| import com.google.datastore.v1.AllocateIdsResponse; | ||
| import com.google.datastore.v1.BeginTransactionRequest; | ||
| import com.google.datastore.v1.BeginTransactionResponse; | ||
| import com.google.datastore.v1.CommitRequest; | ||
| import com.google.datastore.v1.CommitResponse; | ||
| import com.google.datastore.v1.LookupRequest; | ||
| import com.google.datastore.v1.LookupResponse; | ||
| import com.google.datastore.v1.ReserveIdsRequest; | ||
| import com.google.datastore.v1.ReserveIdsResponse; | ||
| import com.google.datastore.v1.RollbackRequest; | ||
| import com.google.datastore.v1.RollbackResponse; | ||
| import com.google.datastore.v1.RunAggregationQueryRequest; | ||
| import com.google.datastore.v1.RunAggregationQueryResponse; | ||
| import com.google.datastore.v1.RunQueryRequest; | ||
| import com.google.datastore.v1.RunQueryResponse; | ||
| import io.grpc.CallOptions; | ||
| import io.grpc.ManagedChannel; | ||
| import io.grpc.ManagedChannelBuilder; | ||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
|
|
||
| @InternalApi | ||
| public class GrpcDatastoreRpc implements AutoCloseable, DatastoreRpc { | ||
jainsahab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private final GrpcDatastoreStub datastoreStub; | ||
| private final ClientContext clientContext; | ||
| private boolean closed; | ||
|
|
||
| public GrpcDatastoreRpc(DatastoreOptions datastoreOptions) throws IOException { | ||
|
|
||
| try { | ||
| clientContext = | ||
| isEmulator(datastoreOptions) | ||
| ? getClientContextForEmulator(datastoreOptions) | ||
| : getClientContext(datastoreOptions); | ||
| ApiFunction<UnaryCallSettings.Builder<?, ?>, Void> retrySettingsSetter = | ||
| builder -> { | ||
| builder.setRetrySettings(datastoreOptions.getRetrySettings()); | ||
| return null; | ||
| }; | ||
| DatastoreStubSettings datastoreStubSettings = | ||
| DatastoreStubSettings.newBuilder(clientContext) | ||
| .applyToAllUnaryMethods(retrySettingsSetter) | ||
| .build(); | ||
| datastoreStub = GrpcDatastoreStub.create(datastoreStubSettings); | ||
| } catch (IOException e) { | ||
| throw new IOException(e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws Exception { | ||
| if (!closed) { | ||
| datastoreStub.close(); | ||
| for (BackgroundResource resource : clientContext.getBackgroundResources()) { | ||
| resource.close(); | ||
| } | ||
| closed = true; | ||
| } | ||
| for (BackgroundResource resource : clientContext.getBackgroundResources()) { | ||
| resource.awaitTermination(1, SECONDS); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public AllocateIdsResponse allocateIds(AllocateIdsRequest request) { | ||
| return datastoreStub.allocateIdsCallable().call(request); | ||
| } | ||
|
|
||
| @Override | ||
| public BeginTransactionResponse beginTransaction(BeginTransactionRequest request) | ||
| throws DatastoreException { | ||
| return datastoreStub.beginTransactionCallable().call(request); | ||
| } | ||
|
|
||
| @Override | ||
| public CommitResponse commit(CommitRequest request) { | ||
| return datastoreStub.commitCallable().call(request); | ||
| } | ||
|
|
||
| @Override | ||
| public LookupResponse lookup(LookupRequest request) { | ||
| return datastoreStub.lookupCallable().call(request); | ||
| } | ||
|
|
||
| @Override | ||
| public ReserveIdsResponse reserveIds(ReserveIdsRequest request) { | ||
| return datastoreStub.reserveIdsCallable().call(request); | ||
| } | ||
|
|
||
| @Override | ||
| public RollbackResponse rollback(RollbackRequest request) { | ||
| return datastoreStub.rollbackCallable().call(request); | ||
| } | ||
|
|
||
| @Override | ||
| public RunQueryResponse runQuery(RunQueryRequest request) { | ||
| return datastoreStub.runQueryCallable().call(request); | ||
| } | ||
|
|
||
| @Override | ||
| public RunAggregationQueryResponse runAggregationQuery(RunAggregationQueryRequest request) { | ||
| return datastoreStub.runAggregationQueryCallable().call(request); | ||
| } | ||
|
|
||
| private boolean isEmulator(DatastoreOptions datastoreOptions) { | ||
kolea2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return isLocalHost(datastoreOptions.getHost()) | ||
| || NoCredentials.getInstance().equals(datastoreOptions.getCredentials()); | ||
| } | ||
|
|
||
| private ClientContext getClientContextForEmulator(DatastoreOptions datastoreOptions) | ||
| throws IOException { | ||
| ManagedChannel managedChannel = | ||
| ManagedChannelBuilder.forTarget(removeScheme(datastoreOptions.getHost())) | ||
| .usePlaintext() | ||
| .build(); | ||
| TransportChannel transportChannel = GrpcTransportChannel.create(managedChannel); | ||
| return ClientContext.newBuilder() | ||
| .setCredentials(null) | ||
kolea2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .setTransportChannel(transportChannel) | ||
| .setDefaultCallContext(GrpcCallContext.of(managedChannel, CallOptions.DEFAULT)) | ||
| .setBackgroundResources(Collections.singletonList(transportChannel)) | ||
| .build(); | ||
| } | ||
|
|
||
| private ClientContext getClientContext(DatastoreOptions datastoreOptions) throws IOException { | ||
| HeaderProvider internalHeaderProvider = | ||
| DatastoreSettings.defaultApiClientHeaderProviderBuilder() | ||
| .setClientLibToken( | ||
| ServiceOptions.getGoogApiClientLibName(), | ||
| GaxProperties.getLibraryVersion(datastoreOptions.getClass())) | ||
| .setResourceToken(getResourceToken(datastoreOptions)) | ||
| .build(); | ||
|
|
||
| DatastoreSettingsBuilder settingsBuilder = | ||
| new DatastoreSettingsBuilder(DatastoreSettings.newBuilder().build()); | ||
| settingsBuilder.setCredentialsProvider( | ||
| GrpcTransportOptions.setUpCredentialsProvider(datastoreOptions)); | ||
| settingsBuilder.setTransportChannelProvider( | ||
| GrpcTransportOptions.setUpChannelProvider( | ||
| DatastoreSettings.defaultGrpcTransportProviderBuilder(), datastoreOptions)); | ||
| settingsBuilder.setInternalHeaderProvider(internalHeaderProvider); | ||
| settingsBuilder.setHeaderProvider( | ||
| datastoreOptions.getMergedHeaderProvider(new NoHeaderProvider())); | ||
| ClientContext clientContext = ClientContext.create(settingsBuilder.build()); | ||
| return clientContext; | ||
| } | ||
|
|
||
| private String getResourceToken(DatastoreOptions datastoreOptions) { | ||
kolea2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| StringBuilder builder = new StringBuilder("project_id="); | ||
| builder.append(datastoreOptions.getProjectId()); | ||
| if (!Strings.isNullOrEmpty(datastoreOptions.getDatabaseId())) { | ||
| builder.append("&database_id="); | ||
| builder.append(datastoreOptions.getDatabaseId()); | ||
| } | ||
| return builder.toString(); | ||
| } | ||
|
|
||
| // This class is needed solely to get access to protected method setInternalHeaderProvider() | ||
| private static class DatastoreSettingsBuilder extends DatastoreSettings.Builder { | ||
|
|
||
| private DatastoreSettingsBuilder(DatastoreSettings settings) { | ||
| super(settings); | ||
| } | ||
|
|
||
| @Override | ||
| protected DatastoreSettings.Builder setInternalHeaderProvider( | ||
| HeaderProvider internalHeaderProvider) { | ||
| return super.setInternalHeaderProvider(internalHeaderProvider); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.