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 #3586

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 0 additions & 4 deletions google-cloud-bigquery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@
<groupId>com.google.api</groupId>
<artifactId>gax</artifactId>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.TableResult;
import java.io.FileInputStream;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import org.threeten.bp.Clock;
import org.threeten.bp.Duration;
import org.threeten.bp.Instant;

public class Benchmark {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.threeten.bp.Instant;
import org.threeten.bp.temporal.ChronoUnit;

final class BigQueryImpl extends BaseService<BigQueryOptions> implements BigQuery {

Expand Down Expand Up @@ -450,7 +448,9 @@ public com.google.api.services.bigquery.model.Job call() {
long jobCreationTime = job.getStatistics().getCreationTime();
long jobMinStaleTime = System.currentTimeMillis();
long jobMaxStaleTime =
Instant.ofEpochMilli(jobMinStaleTime).minus(1, ChronoUnit.DAYS).toEpochMilli();
java.time.Instant.ofEpochMilli(jobMinStaleTime)
.minus(1, java.time.temporal.ChronoUnit.DAYS)
.toEpochMilli();

// Only return the job if it has been created in the past 24 hours.
// This is assuming any job older than 24 hours is a valid duplicate JobID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
import com.google.api.gax.retrying.TimedRetryAlgorithmWithContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.time.Duration;
import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.CancellationException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import org.threeten.bp.Duration;

public class BigQueryRetryAlgorithm<ResponseT> extends RetryAlgorithm<ResponseT> {
private final BigQueryRetryConfig bigQueryRetryConfig;
Expand Down Expand Up @@ -67,7 +67,7 @@ public boolean shouldRetry(
// Log retry info
int attemptCount = nextAttemptSettings == null ? 0 : nextAttemptSettings.getAttemptCount();
Duration retryDelay =
nextAttemptSettings == null ? Duration.ZERO : nextAttemptSettings.getRetryDelay();
nextAttemptSettings == null ? Duration.ZERO : nextAttemptSettings.getRetryDelayDuration();
String errorMessage = previousThrowable != null ? previousThrowable.getMessage() : "";

// Implementing shouldRetryBasedOnBigQueryRetryConfig so that we can retry exceptions based on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import org.threeten.bp.Duration;

/**
* A Google BigQuery Job.
Expand All @@ -52,20 +52,20 @@ public class Job extends JobInfo {

private static final RetrySettings DEFAULT_JOB_WAIT_SETTINGS =
RetrySettings.newBuilder()
.setTotalTimeout(Duration.ofHours(12L))
.setInitialRetryDelay(Duration.ofSeconds(1L))
.setTotalTimeoutDuration(Duration.ofHours(12L))
.setInitialRetryDelayDuration(Duration.ofSeconds(1L))
.setRetryDelayMultiplier(2.0)
.setJittered(true)
.setMaxRetryDelay(Duration.ofMinutes(1L))
.setMaxRetryDelayDuration(Duration.ofMinutes(1L))
.build();

static final RetrySettings DEFAULT_QUERY_JOB_WAIT_SETTINGS =
RetrySettings.newBuilder()
.setTotalTimeout(Duration.ofHours(12L))
.setInitialRetryDelay(Duration.ofSeconds(3L))
.setTotalTimeoutDuration(Duration.ofHours(12L))
.setInitialRetryDelayDuration(Duration.ofSeconds(3L))
.setRetryDelayMultiplier(1.0)
.setJittered(true)
.setMaxRetryDelay(Duration.ofSeconds(3L))
.setMaxRetryDelayDuration(Duration.ofSeconds(3L))
.build();

static final QueryResultsOption[] DEFAULT_QUERY_WAIT_OPTIONS = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

package com.google.cloud.bigquery;

import static org.threeten.bp.temporal.ChronoField.HOUR_OF_DAY;
import static org.threeten.bp.temporal.ChronoField.MINUTE_OF_HOUR;
import static org.threeten.bp.temporal.ChronoField.NANO_OF_SECOND;
import static org.threeten.bp.temporal.ChronoField.SECOND_OF_MINUTE;
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;

import com.google.api.core.ObsoleteApi;
import com.google.api.services.bigquery.model.QueryParameterType;
import com.google.api.services.bigquery.model.RangeValue;
import com.google.auto.value.AutoValue;
Expand All @@ -33,17 +34,17 @@
import com.google.gson.JsonObject;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.threeten.bp.Instant;
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.extra.PeriodDuration;

/**
Expand Down Expand Up @@ -345,7 +346,11 @@ public static QueryParameterValue interval(String value) {
return of(value, StandardSQLTypeName.INTERVAL);
}

/** Creates a {@code QueryParameterValue} object with a type of INTERVAL. */
/**
* Creates a {@code QueryParameterValue} object with a type of INTERVAL. This method is obsolete.
* Use {@link #interval(String)} instead.
*/
@ObsoleteApi("Use interval(String) instead")
public static QueryParameterValue interval(PeriodDuration value) {
Comment on lines +349 to 354
Copy link
Contributor Author

@diegomarquezp diegomarquezp Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lqiu96 I'm not sure if this makes sense. PeriodDuration is part of Threeten Extra which only accepts java.time objects. The class purpose is to blend a Period (years to days) with a Duration (hours to nanos).
I'm guessing we may want to keep it since it's just a helper class that happens to be from ThreeTen but not meant to be a backport (since this is not available in the JDK).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Ideally, I think we should try to migrate away from threeten-extra as well. I'm guessing from your comment that PeriodDuration doesn't exist in the JDK so we can't build a 1:1 method signature replacement with the available JDK alternative.

If interval(String) has the same behavior, then I think that probably can use that as the alternative. But would be a question for the Bigquery SMEs to confirm that they have the same behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. @PhongChuong can you please confirm whether marking this threeten-extra-based method as @ObsoleteApi makes sense? And does interval(String) as the suggested alternative make sense?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@diegomarquezp , marking it as @ObsoleteApi and the suggested alternative makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

return of(value, StandardSQLTypeName.INTERVAL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import com.google.cloud.http.HttpTransportOptions;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.threeten.bp.Duration;

/**
* Utility to create a remote BigQuery configuration for testing. BigQuery options can be obtained
Expand Down Expand Up @@ -144,13 +144,13 @@ private static RetrySettings retrySettings() {
long totalTimeOut = 120000L;
return RetrySettings.newBuilder()
.setMaxAttempts(maxAttempts)
.setMaxRetryDelay(Duration.ofMillis(maxRetryDelay))
.setTotalTimeout(Duration.ofMillis(totalTimeOut))
.setInitialRetryDelay(Duration.ofMillis(initialRetryDelay))
.setMaxRetryDelayDuration(Duration.ofMillis(maxRetryDelay))
.setTotalTimeoutDuration(Duration.ofMillis(totalTimeOut))
.setInitialRetryDelayDuration(Duration.ofMillis(initialRetryDelay))
.setRetryDelayMultiplier(retryDelayMultiplier)
.setInitialRpcTimeout(Duration.ofMillis(totalTimeOut))
.setInitialRpcTimeoutDuration(Duration.ofMillis(totalTimeOut))
.setRpcTimeoutMultiplier(retryDelayMultiplier)
.setMaxRpcTimeout(Duration.ofMillis(totalTimeOut))
.setMaxRpcTimeoutDuration(Duration.ofMillis(totalTimeOut))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@
import com.google.cloud.bigquery.JobStatistics.QueryStatistics;
import com.google.cloud.bigquery.JobStatus.State;
import com.google.common.collect.ImmutableList;
import java.time.Duration;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.MockitoRule;
import org.threeten.bp.Duration;

@RunWith(MockitoJUnitRunner.class)
public class JobTest {
Expand Down Expand Up @@ -83,8 +83,8 @@ public class JobTest {

private static final RetryOption[] TEST_RETRY_OPTIONS =
new RetryOption[] {
RetryOption.totalTimeout(Duration.ofSeconds(3)),
RetryOption.initialRetryDelay(Duration.ofMillis(1L)),
RetryOption.totalTimeoutDuration(Duration.ofSeconds(3)),
RetryOption.initialRetryDelayDuration(Duration.ofMillis(1L)),
RetryOption.jittered(false),
RetryOption.retryDelayMultiplier(1.0)
};
Expand Down Expand Up @@ -402,7 +402,8 @@ public void testWaitForWithTimeout() throws InterruptedException {
when(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).thenReturn(runningJob);
when(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).thenReturn(runningJob);
try {
job.waitFor(concat(TEST_RETRY_OPTIONS, RetryOption.totalTimeout(Duration.ofMillis(3))));
job.waitFor(
concat(TEST_RETRY_OPTIONS, RetryOption.totalTimeoutDuration(Duration.ofMillis(3))));
Assert.fail();
} catch (BigQueryException expected) {
Assert.assertNotNull(expected.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,28 @@
package com.google.cloud.bigquery;

import static com.google.common.truth.Truth.assertThat;
import static org.threeten.bp.temporal.ChronoField.HOUR_OF_DAY;
import static org.threeten.bp.temporal.ChronoField.MINUTE_OF_HOUR;
import static org.threeten.bp.temporal.ChronoField.NANO_OF_SECOND;
import static org.threeten.bp.temporal.ChronoField.SECOND_OF_MINUTE;
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;

import com.google.api.services.bigquery.model.QueryParameterType;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonObject;
import java.math.BigDecimal;
import java.text.ParseException;
import java.time.Instant;
import java.time.Period;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.threeten.bp.Instant;
import org.threeten.bp.ZoneOffset;
import org.threeten.bp.format.DateTimeFormatter;
import org.threeten.bp.format.DateTimeFormatterBuilder;
import org.threeten.bp.jdk8.Jdk8Methods;
import org.threeten.extra.PeriodDuration;

public class QueryParameterValueTest {
Expand Down Expand Up @@ -338,8 +337,8 @@ public void testTimestampFromLong() {
public void testTimestampWithFormatter() {
long timestampInMicroseconds = 1571068536842L * 1000 + 123;
long microseconds = 1_000_000;
long secs = Jdk8Methods.floorDiv(timestampInMicroseconds, microseconds);
int nano = (int) Jdk8Methods.floorMod(timestampInMicroseconds, microseconds) * 1000;
long secs = Math.floorDiv(timestampInMicroseconds, microseconds);
int nano = (int) Math.floorMod(timestampInMicroseconds, microseconds) * 1000;
Instant instant = Instant.ofEpochSecond(secs, nano);
String expected = TIMESTAMPFORMATTER.format(instant);
assertThat(expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalTime;
import java.time.Period;
Expand All @@ -195,7 +196,6 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.threeten.bp.Duration;
import org.threeten.extra.PeriodDuration;

public class ITBigQueryTest {
Expand Down Expand Up @@ -5347,7 +5347,7 @@ public void testCreateAndGetJob() throws InterruptedException, TimeoutException
assertEquals(createdJob.getSelfLink(), remoteJob.getSelfLink());
assertEquals(createdJob.getUserEmail(), remoteJob.getUserEmail());

Job completedJob = remoteJob.waitFor(RetryOption.totalTimeout(Duration.ofMinutes(1)));
Job completedJob = remoteJob.waitFor(RetryOption.totalTimeoutDuration(Duration.ofMinutes(1)));
assertNotNull(completedJob);
assertNull(completedJob.getStatus().getError());
assertTrue(createdTable.delete());
Expand Down Expand Up @@ -5415,8 +5415,8 @@ public void testCreateAndGetJobWithSelectedFields()
assertNull(remoteJob.getUserEmail());
Job completedJob =
remoteJob.waitFor(
RetryOption.initialRetryDelay(Duration.ofSeconds(1)),
RetryOption.totalTimeout(Duration.ofMinutes(1)));
RetryOption.initialRetryDelayDuration(Duration.ofSeconds(1)),
RetryOption.totalTimeoutDuration(Duration.ofMinutes(1)));
assertNotNull(completedJob);
assertTrue(createdTable.delete());
assertNull(completedJob.getStatus().getError());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
import com.google.cloud.http.HttpTransportOptions;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.time.Duration;
import java.util.concurrent.ExecutionException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.threeten.bp.Duration;

@RunWith(MockitoJUnitRunner.class)
public class RemoteBigQueryHelperTest {
Expand Down Expand Up @@ -83,8 +83,8 @@ public void testCreateFromStream() {
assertEquals(60000, ((HttpTransportOptions) options.getTransportOptions()).getConnectTimeout());
assertEquals(60000, ((HttpTransportOptions) options.getTransportOptions()).getReadTimeout());
assertEquals(10, options.getRetrySettings().getMaxAttempts());
assertEquals(Duration.ofMillis(30000), options.getRetrySettings().getMaxRetryDelay());
assertEquals(Duration.ofMillis(120000), options.getRetrySettings().getTotalTimeout());
assertEquals(Duration.ofMillis(250), options.getRetrySettings().getInitialRetryDelay());
assertEquals(Duration.ofMillis(30000), options.getRetrySettings().getMaxRetryDelayDuration());
assertEquals(Duration.ofMillis(120000), options.getRetrySettings().getTotalTimeoutDuration());
assertEquals(Duration.ofMillis(250), options.getRetrySettings().getInitialRetryDelayDuration());
}
}
Loading