Skip to content
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

feat: introduce java.time methods and variables #1671

Merged
merged 6 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ implementation 'com.google.cloud:google-cloud-datastore'
If you are using Gradle without BOM, add this to your dependencies:

```Groovy
implementation 'com.google.cloud:google-cloud-datastore:2.24.2'
implementation 'com.google.cloud:google-cloud-datastore:2.24.3'
```

If you are using SBT, add this to your dependencies:

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-datastore" % "2.24.2"
libraryDependencies += "com.google.cloud" % "google-cloud-datastore" % "2.24.3"
```

## Authentication
Expand Down Expand Up @@ -479,7 +479,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-datastore/java11.html
[stability-image]: https://img.shields.io/badge/stability-stable-green
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-datastore.svg
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-datastore/2.24.2
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-datastore/2.24.3
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
* RetrySettings.newBuilder()
* .setInitialRetryDelayDuration(Duration.ofMillis(500))
* .setRetryDelayMultiplier(1.5)
* .setMaxRetryDelay(Duration.ofMillis(5000))
* .setMaxRetryDelayDuration(Duration.ofMillis(5000))
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
* .setTotalTimeoutDuration(Duration.ofHours(24))
* .build());
* datastoreAdminSettingsBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@
import com.google.longrunning.Operation;
import com.google.protobuf.Empty;
import java.io.IOException;
import java.time.Duration;
import java.util.List;
import javax.annotation.Generated;
import org.threeten.bp.Duration;

// AUTO-GENERATED DOCUMENTATION AND CLASS.
/**
Expand Down Expand Up @@ -138,7 +138,7 @@
* RetrySettings.newBuilder()
* .setInitialRetryDelayDuration(Duration.ofMillis(500))
* .setRetryDelayMultiplier(1.5)
* .setMaxRetryDelay(Duration.ofMillis(5000))
* .setMaxRetryDelayDuration(Duration.ofMillis(5000))
* .setTotalTimeoutDuration(Duration.ofHours(24))
* .build());
* datastoreAdminSettingsBuilder
Expand Down Expand Up @@ -449,21 +449,21 @@ public static class Builder extends StubSettings.Builder<DatastoreAdminStubSetti
RetrySettings settings = null;
settings =
RetrySettings.newBuilder()
.setInitialRpcTimeout(Duration.ofMillis(60000L))
.setInitialRpcTimeoutDuration(Duration.ofMillis(60000L))
.setRpcTimeoutMultiplier(1.0)
.setMaxRpcTimeout(Duration.ofMillis(60000L))
.setTotalTimeout(Duration.ofMillis(60000L))
.setMaxRpcTimeoutDuration(Duration.ofMillis(60000L))
.setTotalTimeoutDuration(Duration.ofMillis(60000L))
.build();
definitions.put("no_retry_0_params", settings);
settings =
RetrySettings.newBuilder()
.setInitialRetryDelay(Duration.ofMillis(100L))
.setInitialRetryDelayDuration(Duration.ofMillis(100L))
.setRetryDelayMultiplier(1.3)
.setMaxRetryDelay(Duration.ofMillis(60000L))
.setInitialRpcTimeout(Duration.ofMillis(60000L))
.setMaxRetryDelayDuration(Duration.ofMillis(60000L))
.setInitialRpcTimeoutDuration(Duration.ofMillis(60000L))
.setRpcTimeoutMultiplier(1.0)
.setMaxRpcTimeout(Duration.ofMillis(60000L))
.setTotalTimeout(Duration.ofMillis(60000L))
.setMaxRpcTimeoutDuration(Duration.ofMillis(60000L))
.setTotalTimeoutDuration(Duration.ofMillis(60000L))
.build();
definitions.put("retry_policy_1_params", settings);
RETRY_PARAM_DEFINITIONS = definitions.build();
Expand Down Expand Up @@ -592,13 +592,13 @@ private static Builder initDefaults(Builder builder) {
.setPollingAlgorithm(
OperationTimedPollAlgorithm.create(
RetrySettings.newBuilder()
.setInitialRetryDelay(Duration.ofMillis(5000L))
.setInitialRetryDelayDuration(Duration.ofMillis(5000L))
.setRetryDelayMultiplier(1.5)
.setMaxRetryDelay(Duration.ofMillis(45000L))
.setInitialRpcTimeout(Duration.ZERO)
.setMaxRetryDelayDuration(Duration.ofMillis(45000L))
.setInitialRpcTimeoutDuration(Duration.ZERO)
.setRpcTimeoutMultiplier(1.0)
.setMaxRpcTimeout(Duration.ZERO)
.setTotalTimeout(Duration.ofMillis(300000L))
.setMaxRpcTimeoutDuration(Duration.ZERO)
.setTotalTimeoutDuration(Duration.ofMillis(300000L))
.build()));

builder
Expand All @@ -616,13 +616,13 @@ private static Builder initDefaults(Builder builder) {
.setPollingAlgorithm(
OperationTimedPollAlgorithm.create(
RetrySettings.newBuilder()
.setInitialRetryDelay(Duration.ofMillis(5000L))
.setInitialRetryDelayDuration(Duration.ofMillis(5000L))
.setRetryDelayMultiplier(1.5)
.setMaxRetryDelay(Duration.ofMillis(45000L))
.setInitialRpcTimeout(Duration.ZERO)
.setMaxRetryDelayDuration(Duration.ofMillis(45000L))
.setInitialRpcTimeoutDuration(Duration.ZERO)
.setRpcTimeoutMultiplier(1.0)
.setMaxRpcTimeout(Duration.ZERO)
.setTotalTimeout(Duration.ofMillis(300000L))
.setMaxRpcTimeoutDuration(Duration.ZERO)
.setTotalTimeoutDuration(Duration.ofMillis(300000L))
.build()));

builder
Expand All @@ -639,13 +639,13 @@ private static Builder initDefaults(Builder builder) {
.setPollingAlgorithm(
OperationTimedPollAlgorithm.create(
RetrySettings.newBuilder()
.setInitialRetryDelay(Duration.ofMillis(5000L))
.setInitialRetryDelayDuration(Duration.ofMillis(5000L))
.setRetryDelayMultiplier(1.5)
.setMaxRetryDelay(Duration.ofMillis(45000L))
.setInitialRpcTimeout(Duration.ZERO)
.setMaxRetryDelayDuration(Duration.ofMillis(45000L))
.setInitialRpcTimeoutDuration(Duration.ZERO)
.setRpcTimeoutMultiplier(1.0)
.setMaxRpcTimeout(Duration.ZERO)
.setTotalTimeout(Duration.ofMillis(300000L))
.setMaxRpcTimeoutDuration(Duration.ZERO)
.setTotalTimeoutDuration(Duration.ofMillis(300000L))
.build()));

builder
Expand All @@ -662,13 +662,13 @@ private static Builder initDefaults(Builder builder) {
.setPollingAlgorithm(
OperationTimedPollAlgorithm.create(
RetrySettings.newBuilder()
.setInitialRetryDelay(Duration.ofMillis(5000L))
.setInitialRetryDelayDuration(Duration.ofMillis(5000L))
.setRetryDelayMultiplier(1.5)
.setMaxRetryDelay(Duration.ofMillis(45000L))
.setInitialRpcTimeout(Duration.ZERO)
.setMaxRetryDelayDuration(Duration.ofMillis(45000L))
.setInitialRpcTimeoutDuration(Duration.ZERO)
.setRpcTimeoutMultiplier(1.0)
.setMaxRpcTimeout(Duration.ZERO)
.setTotalTimeout(Duration.ofMillis(300000L))
.setMaxRpcTimeoutDuration(Duration.ZERO)
.setTotalTimeoutDuration(Duration.ofMillis(300000L))
.build()));

return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@
*/
package com.google.cloud.datastore.models;

import static com.google.api.gax.util.TimeConversionUtils.toThreetenDuration;

import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.api.core.ObsoleteApi;
import com.google.cloud.Structs;
import com.google.common.base.Objects;
import java.util.Map;
import org.threeten.bp.Duration;

/** Model class for {@link com.google.datastore.v1.ExecutionStats} */
@BetaApi
public class ExecutionStats {
private final long resultsReturned;
private final Duration executionDuration;
private final java.time.Duration executionDuration;
private final long readOperations;
private final Map<String, Object> debugStats;

@InternalApi
public ExecutionStats(com.google.datastore.v1.ExecutionStats proto) {
this.resultsReturned = proto.getResultsReturned();
this.executionDuration = Duration.ofNanos(proto.getExecutionDuration().getNanos());
this.executionDuration = java.time.Duration.ofNanos(proto.getExecutionDuration().getNanos());
this.readOperations = proto.getReadOperations();
this.debugStats = Structs.asMap(proto.getDebugStats());
}
Expand All @@ -51,8 +53,14 @@ public Map<String, Object> getDebugStats() {
return debugStats;
}

/** This method is obsolete. Use {@link #getExecutionJavaTimeDuration()} instead. */
@ObsoleteApi("Use getExecutionJavaTimeDuration() instead")
public org.threeten.bp.Duration getExecutionDuration() {
return toThreetenDuration(getExecutionJavaTimeDuration());
}

/** Returns the total time to execute the query in the backend. */
public Duration getExecutionDuration() {
public java.time.Duration getExecutionJavaTimeDuration() {
return executionDuration;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package com.google.cloud.datastore.testing;

import static com.google.api.gax.util.TimeConversionUtils.toJavaTimeDuration;
import static com.google.common.base.MoreObjects.firstNonNull;

import com.google.api.core.InternalApi;
import com.google.api.core.ObsoleteApi;
import com.google.cloud.NoCredentials;
import com.google.cloud.ServiceOptions;
import com.google.cloud.datastore.DatastoreOptions;
Expand All @@ -38,7 +40,6 @@
import java.util.UUID;
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;
import org.threeten.bp.Duration;

/**
* Utility to start and stop local Google Cloud Datastore emulators.
Expand Down Expand Up @@ -307,6 +308,14 @@ public void reset() throws IOException {
sendPostRequest("/reset");
}

/** This method is obsolete. Use {@link #stopDuration(java.time.Duration)} instead */
@ObsoleteApi("Use stopDuration(java.time.Duration) instead")
@Override
public void stop(org.threeten.bp.Duration timeout)
throws IOException, InterruptedException, TimeoutException {
stopDuration(toJavaTimeDuration(timeout));
}

/**
* Stops the Datastore emulator.
*
Expand All @@ -319,23 +328,24 @@ public void reset() throws IOException {
* this value high to ensure proper shutdown, like 5 seconds or more.
*/
@Override
public void stop(Duration timeout) throws IOException, InterruptedException, TimeoutException {
public void stopDuration(java.time.Duration timeout)
throws IOException, InterruptedException, TimeoutException {
sendPostRequest("/shutdown");
waitForProcess(timeout);
waitForProcessDuration(timeout);
deleteRecursively(gcdPath);
}

/**
* Stops the Datastore emulator. The same as {@link #stop(Duration)} but with timeout duration of
* 20 seconds.
* Stops the Datastore emulator. The same as {@link #stopDuration(java.time.Duration)} but with
* timeout duration of 20 seconds.
*
* <p>It is important to stop the emulator. Since the emulator runs in its own process, not
* stopping it might cause it to become orphan.
*
* <p>It is not required to call {@link #reset()} before {@code stop()}.
*/
public void stop() throws IOException, InterruptedException, TimeoutException {
stop(Duration.ofSeconds(20));
stopDuration(java.time.Duration.ofSeconds(20));
}

static void deleteRecursively(Path path) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import com.google.datastore.v1.TransactionOptions;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -87,7 +88,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.threeten.bp.Duration;

@RunWith(JUnit4.class)
public class DatastoreTest {
Expand Down Expand Up @@ -193,7 +193,7 @@ public void setUp() {

@AfterClass
public static void afterClass() throws IOException, InterruptedException, TimeoutException {
helper.stop(Duration.ofMinutes(1));
helper.stopDuration(Duration.ofMinutes(1));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import com.google.common.truth.Truth;
import com.google.datastore.v1.TransactionOptions;
import com.google.datastore.v1.TransactionOptions.ReadOnly;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -103,7 +104,6 @@
import org.junit.rules.Timeout;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.threeten.bp.Duration;

@RunWith(Parameterized.class)
public class ITDatastoreTest {
Expand Down Expand Up @@ -674,7 +674,7 @@ private void assertExecutionStats(
Truth.assertThat(debugStats.get("index_entries_scanned"))
.isEqualTo(expectedIndexEntriesScanned);

Duration executionDuration = executionStats.getExecutionDuration();
Duration executionDuration = executionStats.getExecutionJavaTimeDuration();
Truth.assertThat(executionDuration).isIn(Range.greaterThan(Duration.ofMillis(0)));

long readOperations = executionStats.getReadOperations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public class ExecutionStatsTest {
@Test
public void testModel() {
Truth.assertThat(executionStats.getDebugStats()).isEqualTo(Structs.asMap(struct));
Truth.assertThat(executionStats.getExecutionDuration())
.isEqualTo(org.threeten.bp.Duration.ofNanos(duration.getNanos()));
Truth.assertThat(executionStats.getExecutionJavaTimeDuration())
.isEqualTo(java.time.Duration.ofNanos(duration.getNanos()));
Truth.assertThat(executionStats.getReadOperations()).isEqualTo(2);
Truth.assertThat(executionStats.getResultsReturned()).isEqualTo(3);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.concurrent.TimeoutException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.threeten.bp.Duration;

@RunWith(JUnit4.class)
public class ITLocalDatastoreHelperTest {
Expand Down Expand Up @@ -178,7 +178,7 @@ public void testStartStopReset() throws IOException, InterruptedException, Timeo
assertNotNull(datastore.get(key));
helper.reset();
assertNull(datastore.get(key));
helper.stop(Duration.ofMinutes(1));
helper.stopDuration(Duration.ofMinutes(1));
datastore.get(key);
Assert.fail();
} catch (DatastoreException ex) {
Expand All @@ -198,7 +198,7 @@ public void testStartStopResetWithBuilder()
assertNotNull(datastore.get(key));
helper.reset();
assertNull(datastore.get(key));
helper.stop(Duration.ofMinutes(1));
helper.stopDuration(Duration.ofMinutes(1));
datastore.get(key);
Assert.fail();
} catch (DatastoreException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import com.google.cloud.datastore.StructuredQuery;
import com.google.cloud.http.HttpTransportOptions;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import java.time.Duration;
import java.util.UUID;
import javax.annotation.Nullable;
import org.threeten.bp.Duration;

/**
* Utility to create a remote datastore configuration for testing. Datastore options can be obtained
Expand Down Expand Up @@ -110,13 +110,13 @@ public static RemoteDatastoreHelper create(String databaseId) {
private static RetrySettings retrySettings() {
return RetrySettings.newBuilder()
.setMaxAttempts(10)
.setMaxRetryDelay(Duration.ofMillis(30000L))
.setTotalTimeout(Duration.ofMillis(120000L))
.setInitialRetryDelay(Duration.ofMillis(250L))
.setMaxRetryDelayDuration(Duration.ofMillis(30000L))
.setTotalTimeoutDuration(Duration.ofMillis(120000L))
.setInitialRetryDelayDuration(Duration.ofMillis(250L))
.setRetryDelayMultiplier(1.0)
.setInitialRpcTimeout(Duration.ofMillis(120000L))
.setInitialRpcTimeoutDuration(Duration.ofMillis(120000L))
.setRpcTimeoutMultiplier(1.0)
.setMaxRpcTimeout(Duration.ofMillis(120000L))
.setMaxRpcTimeoutDuration(Duration.ofMillis(120000L))
.build();
}
}
Loading
Loading