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 to java-core #3330

Merged
merged 22 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
5 changes: 5 additions & 0 deletions java-core/google-cloud-core/clirr-ignored-differences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
<className>com/google/cloud/ReadChannel</className>
<method>long limit()</method>
</difference>
<difference>
<differenceType>7012</differenceType>
<className>com/google/cloud/testing/BaseEmulatorHelper$EmulatorRunner</className>
<method>int waitForDuration(java.time.Duration)</method>
</difference>
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
</differences>
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

package com.google.cloud;

import static com.google.api.gax.util.TimeConversionUtils.toJavaTimeDuration;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.core.BetaApi;
import com.google.api.core.ObsoleteApi;
import com.google.api.gax.retrying.RetrySettings;
import java.io.Serializable;
import org.threeten.bp.Duration;

/**
* This class represents an options wrapper around the {@link RetrySettings} class and is an
Expand Down Expand Up @@ -51,13 +52,25 @@ private RetryOption(OptionType type, Object value) {
this.value = checkNotNull(value);
}

/** See {@link RetrySettings#getTotalTimeout()}. */
public static RetryOption totalTimeout(Duration totalTimeout) {
/** This method is obsolete. Use {@link #totalTimeoutDuration(java.time.Duration)} instead */
@ObsoleteApi("Use totalTimeouDuration() instead")
public static RetryOption totalTimeout(org.threeten.bp.Duration totalTimeout) {
return totalTimeoutDuration(toJavaTimeDuration(totalTimeout));
}

/** See {@link RetrySettings#getTotalTimeoutDuration()}. */
public static RetryOption totalTimeoutDuration(java.time.Duration totalTimeout) {
return new RetryOption(OptionType.TOTAL_TIMEOUT, totalTimeout);
}

/** See {@link RetrySettings#getInitialRetryDelay()}. */
public static RetryOption initialRetryDelay(Duration initialRetryDelay) {
/** This method is obsolete. Use {@link #initialRetryDelayDuration(java.time.Duration)} instead */
@ObsoleteApi("Use initialRetryDelayDuration() instead")
public static RetryOption initialRetryDelay(org.threeten.bp.Duration initialRetryDelay) {
return initialRetryDelayDuration(toJavaTimeDuration(initialRetryDelay));
}

/** See {@link RetrySettings#getInitialRetryDelayDuration()}. */
public static RetryOption initialRetryDelayDuration(java.time.Duration initialRetryDelay) {
return new RetryOption(OptionType.INITIAL_RETRY_DELAY, initialRetryDelay);
}

Expand All @@ -66,8 +79,14 @@ public static RetryOption retryDelayMultiplier(double retryDelayMultiplier) {
return new RetryOption(OptionType.RETRY_DELAY_MULTIPLIER, retryDelayMultiplier);
}

/** See {@link RetrySettings#getMaxRetryDelay()}. */
public static RetryOption maxRetryDelay(Duration maxRetryDelay) {
/** This method is obsolete. Use {@link #maxRetryDelayDuration(java.time.Duration)} instead */
@ObsoleteApi("Use maxRetryDelayDuration() instead")
public static RetryOption maxRetryDelay(org.threeten.bp.Duration maxRetryDelay) {
return maxRetryDelayDuration(toJavaTimeDuration(maxRetryDelay));
}

/** See {@link RetrySettings#getMaxRetryDelayDuration()}. */
public static RetryOption maxRetryDelayDuration(java.time.Duration maxRetryDelay) {
return new RetryOption(OptionType.MAX_RETRY_DELAY, maxRetryDelay);
}

Expand Down Expand Up @@ -124,16 +143,16 @@ public static RetrySettings mergeToSettings(RetrySettings settings, RetryOption.
for (RetryOption option : options) {
switch (option.type) {
case TOTAL_TIMEOUT:
builder.setTotalTimeout((Duration) option.value);
builder.setTotalTimeoutDuration((java.time.Duration) option.value);
break;
case INITIAL_RETRY_DELAY:
builder.setInitialRetryDelay((Duration) option.value);
builder.setInitialRetryDelayDuration((java.time.Duration) option.value);
break;
case RETRY_DELAY_MULTIPLIER:
builder.setRetryDelayMultiplier((Double) option.value);
break;
case MAX_RETRY_DELAY:
builder.setMaxRetryDelay((Duration) option.value);
builder.setMaxRetryDelayDuration((java.time.Duration) option.value);
break;
case MAX_ATTEMPTS:
builder.setMaxAttempts((Integer) option.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@
import java.io.Serializable;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.threeten.bp.Duration;

/**
* Abstract class representing service options.
Expand Down Expand Up @@ -787,13 +787,13 @@ public static RetrySettings getNoRetrySettings() {
private static RetrySettings.Builder getDefaultRetrySettingsBuilder() {
return RetrySettings.newBuilder()
.setMaxAttempts(6)
.setInitialRetryDelay(Duration.ofMillis(1000L))
.setMaxRetryDelay(Duration.ofMillis(32_000L))
.setInitialRetryDelayDuration(Duration.ofMillis(1000L))
.setMaxRetryDelayDuration(Duration.ofMillis(32_000L))
.setRetryDelayMultiplier(2.0)
.setTotalTimeout(Duration.ofMillis(50_000L))
.setInitialRpcTimeout(Duration.ofMillis(50_000L))
.setTotalTimeoutDuration(Duration.ofMillis(50_000L))
.setInitialRpcTimeoutDuration(Duration.ofMillis(50_000L))
.setRpcTimeoutMultiplier(1.0)
.setMaxRpcTimeout(Duration.ofMillis(50_000L));
.setMaxRpcTimeoutDuration(Duration.ofMillis(50_000L));
}

protected abstract Set<String> getScopes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@

import com.google.protobuf.util.Timestamps;
import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.threeten.bp.Instant;
import org.threeten.bp.LocalDateTime;
import org.threeten.bp.ZoneOffset;
import org.threeten.bp.format.DateTimeFormatter;
import org.threeten.bp.format.DateTimeFormatterBuilder;
import org.threeten.bp.format.DateTimeParseException;
import org.threeten.bp.temporal.TemporalAccessor;

/**
* Represents a timestamp with nanosecond precision. Timestamps cover the range [0001-01-01,
Expand All @@ -54,7 +54,7 @@ public final class Timestamp implements Comparable<Timestamp>, Serializable {
new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart()
.appendOffsetId()
.appendZoneOrOffsetId()
diegomarquezp marked this conversation as resolved.
Show resolved Hide resolved
.optionalEnd()
.toFormatter()
.withZone(ZoneOffset.UTC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@

package com.google.cloud.testing;

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

import com.google.api.core.CurrentMillisClock;
import com.google.api.core.InternalApi;
import com.google.api.core.ObsoleteApi;
import com.google.cloud.ExceptionHandler;
import com.google.cloud.RetryHelper;
import com.google.cloud.ServiceOptions;
Expand Down Expand Up @@ -56,7 +60,6 @@
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.threeten.bp.Duration;

/** Utility class to start and stop a local service which is used by unit testing. */
@InternalApi
Expand Down Expand Up @@ -112,14 +115,21 @@ protected final void startProcess(String blockUntilOutput)
}
}

/** This method is obsolete. Use {@link #waitForProcessDuration(java.time.Duration)} instead */
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
@ObsoleteApi("Use waitForProcessDuration() instead")
protected final int waitForProcess(org.threeten.bp.Duration timeout)
throws IOException, InterruptedException, TimeoutException {
return waitForProcessDuration(toJavaTimeDuration(timeout));
}
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Waits for the local service's subprocess to terminate, and stop any possible thread listening
* for its output.
*/
protected final int waitForProcess(Duration timeout)
protected final int waitForProcessDuration(java.time.Duration timeout)
throws IOException, InterruptedException, TimeoutException {
if (activeRunner != null) {
int exitCode = activeRunner.waitFor(timeout);
int exitCode = activeRunner.waitForDuration(timeout);
activeRunner = null;
return exitCode;
}
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -130,7 +140,14 @@ protected final int waitForProcess(Duration timeout)
return 0;
}

private static int waitForProcess(final Process process, Duration timeout)
/** This method is obsolete. Use {@link #waitForProcessDuration(java.time.Duration)} instead */
@ObsoleteApi("Use waitForProcessDuration() instead")
private static int waitForProcess(final Process process, org.threeten.bp.Duration timeout)
throws InterruptedException, TimeoutException {
return waitForProcessDuration(process, toJavaTimeDuration(timeout));
}

private static int waitForProcessDuration(final Process process, java.time.Duration timeout)
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
throws InterruptedException, TimeoutException {
if (process == null) {
return 0;
Expand Down Expand Up @@ -180,10 +197,17 @@ public String getProjectId() {
/** Starts the local emulator. */
public abstract void start() throws IOException, InterruptedException;

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

/** Stops the local emulator. */
public void stopDuration(java.time.Duration timeout)
throws IOException, InterruptedException, TimeoutException {
stop(toThreetenDuration(timeout));
}

lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
/** Resets the internal state of the emulator. */
public abstract void reset() throws IOException;

Expand Down Expand Up @@ -226,8 +250,15 @@ protected interface EmulatorRunner {
/** Starts the emulator associated to this runner. */
void start() throws IOException;

/** This method is obsolete. Use {@link #waitForDuration(java.time.Duration)} instead */
@ObsoleteApi("Use waitForDuration() instead")
int waitFor(org.threeten.bp.Duration timeout) throws InterruptedException, TimeoutException;

/** Wait for the emulator associated to this runner to terminate, returning the exit status. */
int waitFor(Duration timeout) throws InterruptedException, TimeoutException;
default int waitForDuration(java.time.Duration timeout)
throws InterruptedException, TimeoutException {
return waitFor(toThreetenDuration(timeout));
}

/** Returns the process associated to the emulator, if any. */
Process getProcess();
Expand Down Expand Up @@ -263,12 +294,20 @@ public void start() throws IOException {
log.fine("Starting emulator via Google Cloud SDK");
process = CommandWrapper.create().setCommand(commandText).setRedirectErrorStream().start();
}

/** This method is obsolete. Use {@link #waitForDuration(java.time.Duration)} instead */
@ObsoleteApi("Use waitForDuration() instead")
@Override
public int waitFor(Duration timeout) throws InterruptedException, TimeoutException {
public int waitFor(org.threeten.bp.Duration timeout)
throws InterruptedException, TimeoutException {
return waitForProcess(process, timeout);
}

@Override
public int waitForDuration(java.time.Duration timeout)
throws InterruptedException, TimeoutException {
return waitForProcessDuration(process, timeout);
}

@Override
public Process getProcess() {
return process;
Expand Down Expand Up @@ -374,11 +413,19 @@ public Path call() throws IOException {
.start();
}

/** This method is obsolete. Use {@link #waitForDuration(java.time.Duration)} instead */
@ObsoleteApi("Use waitForDuration() instead")
@Override
public int waitFor(Duration timeout) throws InterruptedException, TimeoutException {
public int waitFor(org.threeten.bp.Duration timeout)
throws InterruptedException, TimeoutException {
return waitForProcess(process, timeout);
}

public int waitForDuration(java.time.Duration timeout)
throws InterruptedException, TimeoutException {
return waitForProcessDuration(process, timeout);
}

@Override
public Process getProcess() {
return process;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,30 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import com.google.api.gax.retrying.RetrySettings;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.threeten.bp.Duration;

class RetryOptionTest {
private static final long TOTAL_TIMEOUT_MILLIS = 420l;
private static final long INITIAL_RETRY_DELAY_MILLIS = 42l;
private static final long MAX_RETRY_DELAY_MILLIS = 100l;

private static final RetryOption TOTAL_TIMEOUT =
RetryOption.totalTimeout(Duration.ofMillis(420L));
RetryOption.totalTimeoutDuration(Duration.ofMillis(TOTAL_TIMEOUT_MILLIS));
private static final RetryOption INITIAL_RETRY_DELAY =
RetryOption.initialRetryDelay(Duration.ofMillis(42L));
RetryOption.initialRetryDelayDuration(Duration.ofMillis(INITIAL_RETRY_DELAY_MILLIS));
private static final RetryOption RETRY_DELAY_MULTIPLIER = RetryOption.retryDelayMultiplier(1.5);
private static final RetryOption MAX_RETRY_DELAY =
RetryOption.maxRetryDelay(Duration.ofMillis(100));
RetryOption.maxRetryDelayDuration(Duration.ofMillis(MAX_RETRY_DELAY_MILLIS));
private static final RetryOption MAX_ATTEMPTS = RetryOption.maxAttempts(100);
private static final RetryOption JITTERED = RetryOption.jittered(false);

private static final RetrySettings retrySettings =
RetrySettings.newBuilder()
.setTotalTimeout(Duration.ofMillis(420L))
.setInitialRetryDelay(Duration.ofMillis(42L))
.setTotalTimeoutDuration(Duration.ofMillis(420L))
.setInitialRetryDelayDuration(Duration.ofMillis(42L))
.setRetryDelayMultiplier(1.5)
.setMaxRetryDelay(Duration.ofMillis(100))
.setMaxRetryDelayDuration(Duration.ofMillis(100))
.setMaxAttempts(100)
.setJittered(false)
.build();
Expand All @@ -61,10 +64,10 @@ void testEqualsAndHashCode() {
assertNotEquals(MAX_ATTEMPTS, MAX_RETRY_DELAY);
assertNotEquals(JITTERED, MAX_ATTEMPTS);

RetryOption totalTimeout = RetryOption.totalTimeout(Duration.ofMillis(420L));
RetryOption initialRetryDelay = RetryOption.initialRetryDelay(Duration.ofMillis(42L));
RetryOption totalTimeout = RetryOption.totalTimeoutDuration(Duration.ofMillis(420L));
RetryOption initialRetryDelay = RetryOption.initialRetryDelayDuration(Duration.ofMillis(42L));
RetryOption retryDelayMultiplier = RetryOption.retryDelayMultiplier(1.5);
RetryOption maxRetryDelay = RetryOption.maxRetryDelay(Duration.ofMillis(100));
RetryOption maxRetryDelay = RetryOption.maxRetryDelayDuration(Duration.ofMillis(100));
RetryOption maxAttempts = RetryOption.maxAttempts(100);
RetryOption jittered = RetryOption.jittered(false);

Expand Down Expand Up @@ -101,17 +104,17 @@ void testMergeToSettings() {
assertEquals(retrySettings, mergedRetrySettings);

defRetrySettings =
defRetrySettings.toBuilder().setTotalTimeout(Duration.ofMillis(420L)).build();
defRetrySettings.toBuilder().setTotalTimeoutDuration(Duration.ofMillis(420L)).build();
mergedRetrySettings = RetryOption.mergeToSettings(defRetrySettings, TOTAL_TIMEOUT);
assertEquals(defRetrySettings, mergedRetrySettings);

defRetrySettings =
defRetrySettings.toBuilder().setMaxRetryDelay(Duration.ofMillis(100)).build();
defRetrySettings.toBuilder().setMaxRetryDelayDuration(Duration.ofMillis(100)).build();
mergedRetrySettings = RetryOption.mergeToSettings(defRetrySettings, MAX_RETRY_DELAY);
assertEquals(defRetrySettings, mergedRetrySettings);

defRetrySettings =
defRetrySettings.toBuilder().setInitialRetryDelay(Duration.ofMillis(42L)).build();
defRetrySettings.toBuilder().setInitialRetryDelayDuration(Duration.ofMillis(42L)).build();
mergedRetrySettings = RetryOption.mergeToSettings(defRetrySettings, INITIAL_RETRY_DELAY);
assertEquals(defRetrySettings, mergedRetrySettings);

Expand All @@ -127,4 +130,20 @@ void testMergeToSettings() {
mergedRetrySettings = RetryOption.mergeToSettings(defRetrySettings, JITTERED);
assertEquals(defRetrySettings, mergedRetrySettings);
}

@Test
public void threetenMethods_producesEquivalentJavaTimeRetryOptions() {

final RetryOption totalTimeoutThreeten =
RetryOption.totalTimeout(org.threeten.bp.Duration.ofMillis(TOTAL_TIMEOUT_MILLIS));
final RetryOption initialRetryDelayThreeten =
RetryOption.initialRetryDelay(
org.threeten.bp.Duration.ofMillis(INITIAL_RETRY_DELAY_MILLIS));
final RetryOption maxRetryDelayThreeten =
RetryOption.maxRetryDelay(org.threeten.bp.Duration.ofMillis(MAX_RETRY_DELAY_MILLIS));

assertEquals(TOTAL_TIMEOUT, totalTimeoutThreeten);
assertEquals(INITIAL_RETRY_DELAY, initialRetryDelayThreeten);
assertEquals(MAX_RETRY_DELAY, maxRetryDelayThreeten);
}
}
Loading
Loading