From 7642cb263c13e849c94140aace5dd97adc1ab1cc Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Thu, 26 Apr 2018 16:43:14 -0700 Subject: [PATCH 1/8] Basic TimelineSample impl/test. TODO: solve dep issue, Add list to query statistics --- .../google/cloud/bigquery/TimelineSample.java | 181 ++++++++++++++++++ .../cloud/bigquery/TimelineSampleTest.java | 39 ++++ 2 files changed, 220 insertions(+) create mode 100644 google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java create mode 100644 google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java new file mode 100644 index 000000000000..2a55c65859e1 --- /dev/null +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java @@ -0,0 +1,181 @@ +package com.google.cloud.bigquery; + +import com.google.cloud.bigquery.QueryStage.Builder; +import com.google.cloud.bigquery.QueryStage.QueryStep; +import com.google.common.collect.ImmutableList; +import java.io.Serializable; +import com.google.api.services.bigquery.model.QueryTimelineSample; +import com.google.common.base.Function; +import com.google.common.base.MoreObjects; +import java.util.Objects; + + +public class TimelineSample implements Serializable { + + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public TimelineSample apply(QueryTimelineSample pb) { + return TimelineSample.fromPb(pb); + } + }; + + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public QueryTimelineSample apply (TimelineSample sample) { + return sample.toPb(); + } + }; + + + + + + /** + * Returns the sample time as milliseconds elapsed since the start of query execution. + */ + public long getElapsedMs() { + return this.elapsedMs; + } + + /** + * Returns the total number of work units currently being processed. + */ + public long getActiveUnits() { + return this.activeUnits; + } + + /** + * Returns the total number of work units completed by this query. + */ + public long getCompletedUnits() { + return this.completedUnits; + } + + /** + * Returns the number of work units remaining for the currently active stages. + */ + public long getPendingUnits() { + return this.pendingUnits; + } + + /** + * Returns the cumulative slot-milliseconds consumed by the query. + */ + public long getSlotMillis() { + return this.slotMillis; + } + + private final long elapsedMs; + private final long activeUnits; + private final long completedUnits; + private final long pendingUnits; + private final long slotMillis; + + static final class Builder { + + private long elapsedMs; + private long activeUnits; + private long completedUnits; + private long pendingUnits; + private long slotMillis; + + private Builder() { + } + + Builder setElapsedMs(long elapsedMs) { + this.elapsedMs = elapsedMs; + return this; + } + + Builder setActiveUnits(long activeUnits) { + this.activeUnits = activeUnits; + return this; + } + + Builder setCompletedUnits(long completedUnits) { + this.completedUnits = completedUnits; + return this; + } + + Builder setPendingUnits(long pendingUnits) { + this.pendingUnits = pendingUnits; + return this; + } + + Builder setSlotMillis(long slotMillis) { + this.slotMillis = slotMillis; + return this; + } + + TimelineSample build() { + return new TimelineSample(this); + } + + } + + TimelineSample(Builder builder) { + elapsedMs = builder.elapsedMs; + activeUnits = builder.activeUnits; + completedUnits = builder.completedUnits; + pendingUnits = builder.completedUnits; + slotMillis = builder.slotMillis; + } + + static TimelineSample.Builder newBuilder() { + return new TimelineSample.Builder(); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("elapsedMs", elapsedMs) + .add("activeUnits", activeUnits) + .add("completedUnits", completedUnits) + .add("pendingUnits", pendingUnits) + .add("slotMillis", slotMillis) + .toString(); + } + + @Override + public final int hashCode() { + return Objects.hash(elapsedMs, activeUnits, completedUnits, pendingUnits, slotMillis); + + } + + @Override + public final boolean equals (Object obj) { + if (obj == this) { + return true; + } + if (obj == null || !obj.getClass().equals(TimelineSample.class)) { + return false; + } + TimelineSample other = (TimelineSample) obj; + return elapsedMs == other.elapsedMs + && activeUnits == other.activeUnits + && completedUnits == other.completedUnits + && pendingUnits == other.pendingUnits + && slotMillis == other.slotMillis; + } + static TimelineSample fromPb(com.google.api.services.bigquery.model.QueryTimelineSample sample) { + Builder builder = new TimelineSample.Builder(); + builder.setElapsedMs(sample.getElapsedMs()); + builder.setActiveUnits(sample.getActiveInputs()); + builder.setCompletedUnits(sample.getCompletedInputs()); + builder.setPendingUnits(sample.getPendingInputs()); + builder.setSlotMillis(sample.getTotalSlotMs()); + return builder.build(); + } + + QueryTimelineSample toPb() { + QueryTimelineSample sample = new QueryTimelineSample() + .setElapsedMs(elapsedMs) + .setActiveInputs(activeUnits) + .setCompletedInputs(completedUnits) + .setPendingInputs(pendingUnits) + .setTotalSlotMs(slotMillis); + return sample; + } +} \ No newline at end of file diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java new file mode 100644 index 000000000000..c2fd3dc5098c --- /dev/null +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java @@ -0,0 +1,39 @@ +package com.google.cloud.bigquery; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class TimelineSampleTest { + + private static long ELAPSED_MS = 1001; + private static long ACTIVE_UNITS = 500; + private static long COMPLETED_UNITS = 843; + private static long PENDING_UNITS = 14; + private static long SLOT_MILLIS = 1220012; + + private static TimelineSample TIMELINE_SAMPLE = TimelineSample.newBuilder() + .setElapsedMs(ELAPSED_MS) + .setActiveUnits(ACTIVE_UNITS) + .setCompletedUnits(COMPLETED_UNITS) + .setPendingUnits(PENDING_UNITS) + .setSlotMillis(SLOT_MILLIS) + .build(); + + + + @Test + public void testTimelineSampleBuilder() { + assertEquals(ELAPSED_MS, TIMELINE_SAMPLE.getElapsedMs()); + assertEquals(ACTIVE_UNITS, TIMELINE_SAMPLE.getActiveUnits()); + assertEquals(COMPLETED_UNITS, TIMELINE_SAMPLE.getCompletedUnits()); + assertEquals(PENDING_UNITS, TIMELINE_SAMPLE.getPendingUnits()); + assertEquals(SLOT_MILLIS, TIMELINE_SAMPLE.getSlotMillis()); + } + + @Test + public void TestEquals() { + assertEquals(TIMELINE_SAMPLE, TIMELINE_SAMPLE); + } + +} From 3495669c45811b84c2c19113557b6fb1ee168399 Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Thu, 26 Apr 2018 17:09:00 -0700 Subject: [PATCH 2/8] Add timeline to query statistics. TODO - deps (still picking up old sample fields) --- .../google/cloud/bigquery/JobStatistics.java | 25 +++++++++++++++++++ .../google/cloud/bigquery/TimelineSample.java | 2 +- .../cloud/bigquery/JobStatisticsTest.java | 18 +++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobStatistics.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobStatistics.java index 05e0856e5235..175e5f47f1fb 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobStatistics.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobStatistics.java @@ -323,6 +323,7 @@ public static class QueryStatistics extends JobStatistics { private final Long totalBytesBilled; private final Long totalBytesProcessed; private final List queryPlan; + private final List timeline; private final Schema schema; static final class Builder extends JobStatistics.Builder { @@ -332,6 +333,7 @@ static final class Builder extends JobStatistics.Builder queryPlan; + private List timeline; private Schema schema; private Builder() {} @@ -348,6 +350,11 @@ private Builder(com.google.api.services.bigquery.model.JobStatistics statisticsP Lists.transform( statisticsPb.getQuery().getQueryPlan(), QueryStage.FROM_PB_FUNCTION); } + if (statisticsPb.getQuery().getTimeline() != null) { + this.timeline = + Lists.transform( + statisticsPb.getQuery().getTimeline(), TimelineSample.FROM_PB_FUNCTION); + } if (statisticsPb.getQuery().getSchema() != null) { this.schema = Schema.fromPb(statisticsPb.getQuery().getSchema()); } @@ -379,6 +386,11 @@ Builder setQueryPlan(List queryPlan) { return self(); } + Builder setTimeline(List timeline) { + this.timeline = timeline; + return self(); + } + Builder setSchema(Schema schema) { this.schema = schema; return self(); @@ -397,6 +409,7 @@ private QueryStatistics(Builder builder) { this.totalBytesBilled = builder.totalBytesBilled; this.totalBytesProcessed = builder.totalBytesProcessed; this.queryPlan = builder.queryPlan; + this.timeline = builder.timeline; this.schema = builder.schema; } @@ -448,6 +461,14 @@ public List getQueryPlan() { return queryPlan; } + /** + * Return the timeline for the query, as a list of timeline samples. Each sample provides + * information about the overall progress of the query. Information includes time of the + * sample, progress reporting on active, completed, and + * pending units of work, as well as the cumulative estimation of slot-milliseconds consumed + * by the query. + */ + public List getTimeline() { return timeline; } /** * Returns the schema for the query result. Present only for successful dry run of @@ -465,6 +486,7 @@ ToStringHelper toStringHelper() { .add("totalBytesBilled", totalBytesBilled) .add("totalBytesProcessed", totalBytesProcessed) .add("queryPlan", queryPlan) + .add("timeline", timeline) .add("schema", schema); } @@ -492,6 +514,9 @@ com.google.api.services.bigquery.model.JobStatistics toPb() { if (queryPlan != null) { queryStatisticsPb.setQueryPlan(Lists.transform(queryPlan, QueryStage.TO_PB_FUNCTION)); } + if (timeline != null) { + queryStatisticsPb.setTimeline(Lists.transform(timeline, TimelineSample.TO_PB_FUNCTION)); + } if (schema != null) { queryStatisticsPb.setSchema(schema.toPb()); } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java index 2a55c65859e1..3202d523e72c 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java @@ -119,7 +119,7 @@ TimelineSample build() { elapsedMs = builder.elapsedMs; activeUnits = builder.activeUnits; completedUnits = builder.completedUnits; - pendingUnits = builder.completedUnits; + pendingUnits = builder.pendingUnits; slotMillis = builder.slotMillis; } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java index 573318b17cb5..44947658f6e8 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java @@ -88,6 +88,22 @@ public class JobStatisticsTest { .setWriteRatioAvg(9.9) .setWriteRatioMax(10.10) .build(); + + private static final TimelineSample TIMELINE_SAMPLE1 = TimelineSample.newBuilder() + .setElapsedMs(1001) + .setActiveUnits(100) + .setCompletedUnits(200) + .setPendingUnits(50) + .setSlotMillis(12345) + .build(); + private static final TimelineSample TIMELINE_SAMPLE2 = TimelineSample.newBuilder() + .setElapsedMs(2002) + .setActiveUnits(48) + .setCompletedUnits(302) + .setPendingUnits(0) + .setSlotMillis(23456) + .build(); + private static final List TIMELINE = ImmutableList.of(TIMELINE_SAMPLE1, TIMELINE_SAMPLE2); private static final List QUERY_PLAN = ImmutableList.of(QUERY_STAGE); private static final Schema SCHEMA = Schema.of(Field.of("column", LegacySQLTypeName.DATETIME)); private static final QueryStatistics QUERY_STATISTICS = QueryStatistics.newBuilder() @@ -99,6 +115,7 @@ public class JobStatisticsTest { .setTotalBytesBilled(TOTAL_BYTES_BILLED) .setTotalBytesProcessed(TOTAL_BYTES_PROCESSED) .setQueryPlan(QUERY_PLAN) + .setTimeline(TIMELINE) .setSchema(SCHEMA) .build(); private static final QueryStatistics QUERY_STATISTICS_INCOMPLETE = QueryStatistics.newBuilder() @@ -133,6 +150,7 @@ public void testBuilder() { assertEquals(TOTAL_BYTES_PROCESSED, QUERY_STATISTICS.getTotalBytesProcessed()); assertEquals(TOTAL_BYTES_PROCESSED, QUERY_STATISTICS.getTotalBytesProcessed()); assertEquals(QUERY_PLAN, QUERY_STATISTICS.getQueryPlan()); + assertEquals(TIMELINE, QUERY_STATISTICS.getTimeline()); assertEquals(CREATION_TIME, LOAD_STATISTICS_INCOMPLETE.getCreationTime()); assertEquals(START_TIME, LOAD_STATISTICS_INCOMPLETE.getStartTime()); From 696c89ed2644f0b32eba872a7ea0a805d315a1af Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Thu, 26 Apr 2018 17:35:58 -0700 Subject: [PATCH 3/8] rev dependencies and update signatures. --- .../com/google/cloud/bigquery/TimelineSample.java | 12 ++++++------ pom.xml | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java index 3202d523e72c..6d45c2138728 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java @@ -162,9 +162,9 @@ public final boolean equals (Object obj) { static TimelineSample fromPb(com.google.api.services.bigquery.model.QueryTimelineSample sample) { Builder builder = new TimelineSample.Builder(); builder.setElapsedMs(sample.getElapsedMs()); - builder.setActiveUnits(sample.getActiveInputs()); - builder.setCompletedUnits(sample.getCompletedInputs()); - builder.setPendingUnits(sample.getPendingInputs()); + builder.setActiveUnits(sample.getActiveUnits()); + builder.setCompletedUnits(sample.getCompletedUnits()); + builder.setPendingUnits(sample.getPendingUnits()); builder.setSlotMillis(sample.getTotalSlotMs()); return builder.build(); } @@ -172,9 +172,9 @@ static TimelineSample fromPb(com.google.api.services.bigquery.model.QueryTimelin QueryTimelineSample toPb() { QueryTimelineSample sample = new QueryTimelineSample() .setElapsedMs(elapsedMs) - .setActiveInputs(activeUnits) - .setCompletedInputs(completedUnits) - .setPendingInputs(pendingUnits) + .setActiveUnits(activeUnits) + .setCompletedUnits(completedUnits) + .setPendingUnits(pendingUnits) .setTotalSlotMs(slotMillis); return sample; } diff --git a/pom.xml b/pom.xml index 4379b2b7dcef..e909d6540de2 100644 --- a/pom.xml +++ b/pom.xml @@ -164,7 +164,7 @@ com.google.apis google-api-services-bigquery - v2-rev377-1.23.0 + v2-rev383-1.23.0 com.google.apis From b9f3dfc7eabe8ac6ba6fa581c4775600680adefc Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Thu, 26 Apr 2018 18:04:55 -0700 Subject: [PATCH 4/8] address codacy auto-reviews --- .../google/cloud/bigquery/TimelineSample.java | 23 ++++++++----------- .../cloud/bigquery/TimelineSampleTest.java | 10 ++++---- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java index 6d45c2138728..2e045cfe2d1f 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java @@ -1,8 +1,5 @@ package com.google.cloud.bigquery; -import com.google.cloud.bigquery.QueryStage.Builder; -import com.google.cloud.bigquery.QueryStage.QueryStep; -import com.google.common.collect.ImmutableList; import java.io.Serializable; import com.google.api.services.bigquery.model.QueryTimelineSample; import com.google.common.base.Function; @@ -12,6 +9,14 @@ public class TimelineSample implements Serializable { + private static final long serialVersionUID = 1L; + + private final long elapsedMs; + private final long activeUnits; + private final long completedUnits; + private final long pendingUnits; + private final long slotMillis; + static final Function FROM_PB_FUNCTION = new Function() { @Override @@ -28,10 +33,6 @@ public QueryTimelineSample apply (TimelineSample sample) { } }; - - - - /** * Returns the sample time as milliseconds elapsed since the start of query execution. */ @@ -67,11 +68,7 @@ public long getSlotMillis() { return this.slotMillis; } - private final long elapsedMs; - private final long activeUnits; - private final long completedUnits; - private final long pendingUnits; - private final long slotMillis; + static final class Builder { @@ -159,7 +156,7 @@ public final boolean equals (Object obj) { && pendingUnits == other.pendingUnits && slotMillis == other.slotMillis; } - static TimelineSample fromPb(com.google.api.services.bigquery.model.QueryTimelineSample sample) { + static TimelineSample fromPb(QueryTimelineSample sample) { Builder builder = new TimelineSample.Builder(); builder.setElapsedMs(sample.getElapsedMs()); builder.setActiveUnits(sample.getActiveUnits()); diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java index c2fd3dc5098c..d74384f8025c 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java @@ -6,11 +6,11 @@ public class TimelineSampleTest { - private static long ELAPSED_MS = 1001; - private static long ACTIVE_UNITS = 500; - private static long COMPLETED_UNITS = 843; - private static long PENDING_UNITS = 14; - private static long SLOT_MILLIS = 1220012; + private static final long ELAPSED_MS = 1001; + private static final long ACTIVE_UNITS = 500; + private static final long COMPLETED_UNITS = 843; + private static final long PENDING_UNITS = 14; + private static final long SLOT_MILLIS = 1220012; private static TimelineSample TIMELINE_SAMPLE = TimelineSample.newBuilder() .setElapsedMs(ELAPSED_MS) From a0d8a55b21cda000c7b6cb09dbc12c1e19eb489d Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Thu, 26 Apr 2018 18:11:09 -0700 Subject: [PATCH 5/8] add license headers to new files --- .../com/google/cloud/bigquery/TimelineSample.java | 15 +++++++++++++++ .../google/cloud/bigquery/TimelineSampleTest.java | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java index 2e045cfe2d1f..66026bc147a8 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 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.bigquery; import java.io.Serializable; diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java index d74384f8025c..bf8f89ae7ad5 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 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.bigquery; import static org.junit.Assert.assertEquals; From 8fc74b583955f738fd020b77e4c7c3cfc702c105 Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Thu, 26 Apr 2018 18:15:51 -0700 Subject: [PATCH 6/8] final --- .../src/main/java/com/google/cloud/bigquery/TimelineSample.java | 1 - .../test/java/com/google/cloud/bigquery/TimelineSampleTest.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java index 66026bc147a8..d63eee8a2c9c 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java @@ -153,7 +153,6 @@ public String toString() { @Override public final int hashCode() { return Objects.hash(elapsedMs, activeUnits, completedUnits, pendingUnits, slotMillis); - } @Override diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java index bf8f89ae7ad5..03e9ba40bec8 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java @@ -27,7 +27,7 @@ public class TimelineSampleTest { private static final long PENDING_UNITS = 14; private static final long SLOT_MILLIS = 1220012; - private static TimelineSample TIMELINE_SAMPLE = TimelineSample.newBuilder() + private static final TimelineSample TIMELINE_SAMPLE = TimelineSample.newBuilder() .setElapsedMs(ELAPSED_MS) .setActiveUnits(ACTIVE_UNITS) .setCompletedUnits(COMPLETED_UNITS) From 13c443e60a73ff2ee85468b11422c9b31ff2c1db Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Fri, 27 Apr 2018 13:46:43 -0700 Subject: [PATCH 7/8] Change TimelineSample to @AutoValue-based implementation --- .../google/cloud/bigquery/TimelineSample.java | 191 ++++++------------ .../cloud/bigquery/JobStatisticsTest.java | 20 +- .../cloud/bigquery/TimelineSampleTest.java | 10 +- 3 files changed, 77 insertions(+), 144 deletions(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java index d63eee8a2c9c..157bde143daf 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java @@ -15,163 +15,79 @@ */ package com.google.cloud.bigquery; -import java.io.Serializable; import com.google.api.services.bigquery.model.QueryTimelineSample; import com.google.common.base.Function; -import com.google.common.base.MoreObjects; -import java.util.Objects; +import com.google.auto.value.AutoValue; +import javax.annotation.Nullable; - -public class TimelineSample implements Serializable { +/** + * A specific timeline sample. This instruments work progress at a given point in time, providing + * information about work units active/pending/completed as well as cumulative slot-milliseconds. + */ +@AutoValue +public abstract class TimelineSample { private static final long serialVersionUID = 1L; - private final long elapsedMs; - private final long activeUnits; - private final long completedUnits; - private final long pendingUnits; - private final long slotMillis; + @AutoValue.Builder + public abstract static class Builder { - static final Function FROM_PB_FUNCTION = - new Function() { - @Override - public TimelineSample apply(QueryTimelineSample pb) { - return TimelineSample.fromPb(pb); - } - }; + public abstract Builder setElapsedMs(Long elapsedMs); - static final Function TO_PB_FUNCTION = - new Function() { - @Override - public QueryTimelineSample apply (TimelineSample sample) { - return sample.toPb(); - } - }; + public abstract Builder setActiveUnits(Long activeUnits); + + public abstract Builder setCompletedUnits(Long completedUnits); + + public abstract Builder setPendingUnits(Long pendingUnits); + + public abstract Builder setSlotMillis(Long slotMillis); + + public abstract TimelineSample build(); + + } /** * Returns the sample time as milliseconds elapsed since the start of query execution. */ - public long getElapsedMs() { - return this.elapsedMs; - } + @Nullable + public abstract Long getElapsedMs(); /** * Returns the total number of work units currently being processed. */ - public long getActiveUnits() { - return this.activeUnits; - } + @Nullable + + public abstract Long getActiveUnits(); /** * Returns the total number of work units completed by this query. */ - public long getCompletedUnits() { - return this.completedUnits; - } + @Nullable + public abstract Long getCompletedUnits(); /** * Returns the number of work units remaining for the currently active stages. */ - public long getPendingUnits() { - return this.pendingUnits; - } + @Nullable + public abstract Long getPendingUnits(); /** * Returns the cumulative slot-milliseconds consumed by the query. */ - public long getSlotMillis() { - return this.slotMillis; - } - - - - static final class Builder { - - private long elapsedMs; - private long activeUnits; - private long completedUnits; - private long pendingUnits; - private long slotMillis; - - private Builder() { - } + @Nullable + public abstract Long getSlotMillis(); - Builder setElapsedMs(long elapsedMs) { - this.elapsedMs = elapsedMs; - return this; - } - - Builder setActiveUnits(long activeUnits) { - this.activeUnits = activeUnits; - return this; - } - - Builder setCompletedUnits(long completedUnits) { - this.completedUnits = completedUnits; - return this; - } - - Builder setPendingUnits(long pendingUnits) { - this.pendingUnits = pendingUnits; - return this; - } - - Builder setSlotMillis(long slotMillis) { - this.slotMillis = slotMillis; - return this; - } - - TimelineSample build() { - return new TimelineSample(this); - } - - } - - TimelineSample(Builder builder) { - elapsedMs = builder.elapsedMs; - activeUnits = builder.activeUnits; - completedUnits = builder.completedUnits; - pendingUnits = builder.pendingUnits; - slotMillis = builder.slotMillis; - } - - static TimelineSample.Builder newBuilder() { - return new TimelineSample.Builder(); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("elapsedMs", elapsedMs) - .add("activeUnits", activeUnits) - .add("completedUnits", completedUnits) - .add("pendingUnits", pendingUnits) - .add("slotMillis", slotMillis) - .toString(); - } + /** + * return a builder for the {@TimelineSample} object. + */ + public abstract Builder toBuilder(); - @Override - public final int hashCode() { - return Objects.hash(elapsedMs, activeUnits, completedUnits, pendingUnits, slotMillis); + static Builder newBuilder() { + return new AutoValue_TimelineSample.Builder(); } - @Override - public final boolean equals (Object obj) { - if (obj == this) { - return true; - } - if (obj == null || !obj.getClass().equals(TimelineSample.class)) { - return false; - } - TimelineSample other = (TimelineSample) obj; - return elapsedMs == other.elapsedMs - && activeUnits == other.activeUnits - && completedUnits == other.completedUnits - && pendingUnits == other.pendingUnits - && slotMillis == other.slotMillis; - } static TimelineSample fromPb(QueryTimelineSample sample) { - Builder builder = new TimelineSample.Builder(); + Builder builder = newBuilder(); builder.setElapsedMs(sample.getElapsedMs()); builder.setActiveUnits(sample.getActiveUnits()); builder.setCompletedUnits(sample.getCompletedUnits()); @@ -182,11 +98,28 @@ static TimelineSample fromPb(QueryTimelineSample sample) { QueryTimelineSample toPb() { QueryTimelineSample sample = new QueryTimelineSample() - .setElapsedMs(elapsedMs) - .setActiveUnits(activeUnits) - .setCompletedUnits(completedUnits) - .setPendingUnits(pendingUnits) - .setTotalSlotMs(slotMillis); + .setElapsedMs(getElapsedMs()) + .setActiveUnits(getActiveUnits()) + .setCompletedUnits(getCompletedUnits()) + .setPendingUnits(getPendingUnits()) + .setTotalSlotMs(getSlotMillis()); return sample; } + + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public TimelineSample apply(QueryTimelineSample pb) { + return TimelineSample.fromPb(pb); + } + }; + + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public QueryTimelineSample apply(TimelineSample sample) { + return sample.toPb(); + } + }; + } \ No newline at end of file diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java index 44947658f6e8..be738c89b355 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java @@ -90,18 +90,18 @@ public class JobStatisticsTest { .build(); private static final TimelineSample TIMELINE_SAMPLE1 = TimelineSample.newBuilder() - .setElapsedMs(1001) - .setActiveUnits(100) - .setCompletedUnits(200) - .setPendingUnits(50) - .setSlotMillis(12345) + .setElapsedMs(1001L) + .setActiveUnits(100L) + .setCompletedUnits(200L) + .setPendingUnits(50L) + .setSlotMillis(12345L) .build(); private static final TimelineSample TIMELINE_SAMPLE2 = TimelineSample.newBuilder() - .setElapsedMs(2002) - .setActiveUnits(48) - .setCompletedUnits(302) - .setPendingUnits(0) - .setSlotMillis(23456) + .setElapsedMs(2002L) + .setActiveUnits(48L) + .setCompletedUnits(302L) + .setPendingUnits(0L) + .setSlotMillis(23456L) .build(); private static final List TIMELINE = ImmutableList.of(TIMELINE_SAMPLE1, TIMELINE_SAMPLE2); private static final List QUERY_PLAN = ImmutableList.of(QUERY_STAGE); diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java index 03e9ba40bec8..b64f0cef93b8 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java @@ -21,11 +21,11 @@ public class TimelineSampleTest { - private static final long ELAPSED_MS = 1001; - private static final long ACTIVE_UNITS = 500; - private static final long COMPLETED_UNITS = 843; - private static final long PENDING_UNITS = 14; - private static final long SLOT_MILLIS = 1220012; + private static final Long ELAPSED_MS = 1001L; + private static final Long ACTIVE_UNITS = 500L; + private static final Long COMPLETED_UNITS = 843L; + private static final Long PENDING_UNITS = 14L; + private static final Long SLOT_MILLIS = 1220012L; private static final TimelineSample TIMELINE_SAMPLE = TimelineSample.newBuilder() .setElapsedMs(ELAPSED_MS) From 17d0810a6845bf933acd556c38f4d001293977be Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Mon, 30 Apr 2018 12:28:58 -0700 Subject: [PATCH 8/8] Address review comments --- .../main/java/com/google/cloud/bigquery/TimelineSample.java | 5 ++--- .../java/com/google/cloud/bigquery/TimelineSampleTest.java | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java index 157bde143daf..ddc7cf289f32 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TimelineSample.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Google LLC + * Copyright 2018 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,7 +56,6 @@ public abstract static class Builder { * Returns the total number of work units currently being processed. */ @Nullable - public abstract Long getActiveUnits(); /** @@ -122,4 +121,4 @@ public QueryTimelineSample apply(TimelineSample sample) { } }; -} \ No newline at end of file +} diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java index b64f0cef93b8..8cf610ef45cd 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Google LLC + * Copyright 2018 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.