Skip to content

Commit 49cbb0f

Browse files
author
Praful Makani
authored
feat: add reservation usage in job statistics (#1018)
1 parent 24a6a3b commit 49cbb0f

File tree

2 files changed

+154
-2
lines changed

2 files changed

+154
-2
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobStatistics.java

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public abstract class JobStatistics implements Serializable {
4343
private final Long numChildJobs;
4444
private final String parentJobId;
4545
private final ScriptStatistics scriptStatistics;
46+
private final List<ReservationUsage> reservationUsage;
4647

4748
/** A Google BigQuery Copy Job statistics. */
4849
public static class CopyStatistics extends JobStatistics {
@@ -1047,6 +1048,118 @@ static ScriptStatistics fromPb(
10471048
}
10481049
}
10491050

1051+
/** ReservationUsage contains information about a job's usage of a single reservation. */
1052+
public static class ReservationUsage {
1053+
1054+
static final Function<
1055+
com.google.api.services.bigquery.model.JobStatistics.ReservationUsage, ReservationUsage>
1056+
FROM_PB_FUNCTION =
1057+
new Function<
1058+
com.google.api.services.bigquery.model.JobStatistics.ReservationUsage,
1059+
ReservationUsage>() {
1060+
@Override
1061+
public ReservationUsage apply(
1062+
com.google.api.services.bigquery.model.JobStatistics.ReservationUsage usage) {
1063+
return ReservationUsage.fromPb(usage);
1064+
}
1065+
};
1066+
1067+
static final Function<
1068+
ReservationUsage, com.google.api.services.bigquery.model.JobStatistics.ReservationUsage>
1069+
TO_PB_FUNCTION =
1070+
new Function<
1071+
ReservationUsage,
1072+
com.google.api.services.bigquery.model.JobStatistics.ReservationUsage>() {
1073+
@Override
1074+
public com.google.api.services.bigquery.model.JobStatistics.ReservationUsage apply(
1075+
ReservationUsage usage) {
1076+
return usage.toPb();
1077+
}
1078+
};
1079+
1080+
private final String name;
1081+
private final Long slotMs;
1082+
1083+
public static class Builder {
1084+
1085+
private String name;
1086+
private Long slotMs;
1087+
1088+
private Builder() {};
1089+
1090+
Builder setName(String name) {
1091+
this.name = name;
1092+
return this;
1093+
}
1094+
1095+
Builder setSlotMs(Long slotMs) {
1096+
this.slotMs = slotMs;
1097+
return this;
1098+
}
1099+
1100+
ReservationUsage build() {
1101+
return new ReservationUsage(this);
1102+
}
1103+
}
1104+
1105+
private ReservationUsage(Builder builder) {
1106+
this.name = builder.name;
1107+
this.slotMs = builder.slotMs;
1108+
}
1109+
1110+
// Return mame indicates the utilized reservation name, or "unreserved" for ondemand usage.
1111+
public String getName() {
1112+
return name;
1113+
}
1114+
1115+
// Returns slotMs reports the slot milliseconds utilized within in the given reservation.
1116+
public Long getSlotMs() {
1117+
return slotMs;
1118+
}
1119+
1120+
static Builder newBuilder() {
1121+
return new Builder();
1122+
}
1123+
1124+
ToStringHelper toStringHelper() {
1125+
return MoreObjects.toStringHelper(this).add("name", name).add("slotMs", slotMs);
1126+
}
1127+
1128+
@Override
1129+
public String toString() {
1130+
return toStringHelper().toString();
1131+
}
1132+
1133+
@Override
1134+
public boolean equals(Object obj) {
1135+
return obj == this
1136+
|| obj != null
1137+
&& obj.getClass().equals(ReservationUsage.class)
1138+
&& Objects.equals(toPb(), ((ReservationUsage) obj).toPb());
1139+
}
1140+
1141+
@Override
1142+
public int hashCode() {
1143+
return Objects.hash(name, slotMs);
1144+
}
1145+
1146+
com.google.api.services.bigquery.model.JobStatistics.ReservationUsage toPb() {
1147+
com.google.api.services.bigquery.model.JobStatistics.ReservationUsage usage =
1148+
new com.google.api.services.bigquery.model.JobStatistics.ReservationUsage();
1149+
usage.setName(name);
1150+
usage.setSlotMs(slotMs);
1151+
return usage;
1152+
}
1153+
1154+
static ReservationUsage fromPb(
1155+
com.google.api.services.bigquery.model.JobStatistics.ReservationUsage usage) {
1156+
Builder builder = newBuilder();
1157+
builder.setName(usage.getName());
1158+
builder.setSlotMs(usage.getSlotMs());
1159+
return builder.build();
1160+
}
1161+
}
1162+
10501163
abstract static class Builder<T extends JobStatistics, B extends Builder<T, B>> {
10511164

10521165
private Long creationTime;
@@ -1055,6 +1168,7 @@ abstract static class Builder<T extends JobStatistics, B extends Builder<T, B>>
10551168
private Long numChildJobs;
10561169
private String parentJobId;
10571170
private ScriptStatistics scriptStatistics;
1171+
private List<ReservationUsage> reservationUsage;
10581172

10591173
protected Builder() {}
10601174

@@ -1067,6 +1181,10 @@ protected Builder(com.google.api.services.bigquery.model.JobStatistics statistic
10671181
if (statisticsPb.getScriptStatistics() != null) {
10681182
this.scriptStatistics = ScriptStatistics.fromPb(statisticsPb.getScriptStatistics());
10691183
}
1184+
if (reservationUsage != null) {
1185+
this.reservationUsage =
1186+
Lists.transform(statisticsPb.getReservationUsage(), ReservationUsage.FROM_PB_FUNCTION);
1187+
}
10701188
}
10711189

10721190
@SuppressWarnings("unchecked")
@@ -1099,6 +1217,7 @@ protected JobStatistics(Builder builder) {
10991217
this.numChildJobs = builder.numChildJobs;
11001218
this.parentJobId = builder.parentJobId;
11011219
this.scriptStatistics = builder.scriptStatistics;
1220+
this.reservationUsage = builder.reservationUsage;
11021221
}
11031222

11041223
/** Returns the creation time of the job in milliseconds since epoch. */
@@ -1137,14 +1256,20 @@ public ScriptStatistics getScriptStatistics() {
11371256
return scriptStatistics;
11381257
}
11391258

1259+
/** ReservationUsage contains information about a job's usage of a single reservation. */
1260+
public List<ReservationUsage> getReservationUsage() {
1261+
return reservationUsage;
1262+
}
1263+
11401264
ToStringHelper toStringHelper() {
11411265
return MoreObjects.toStringHelper(this)
11421266
.add("creationTime", creationTime)
11431267
.add("endTime", endTime)
11441268
.add("startTime", startTime)
11451269
.add("numChildJobs", numChildJobs)
11461270
.add("parentJobId", parentJobId)
1147-
.add("scriptStatistics", scriptStatistics);
1271+
.add("scriptStatistics", scriptStatistics)
1272+
.add("reservationUsage", reservationUsage);
11481273
}
11491274

11501275
@Override
@@ -1154,7 +1279,13 @@ public String toString() {
11541279

11551280
final int baseHashCode() {
11561281
return Objects.hash(
1157-
creationTime, endTime, startTime, numChildJobs, parentJobId, scriptStatistics);
1282+
creationTime,
1283+
endTime,
1284+
startTime,
1285+
numChildJobs,
1286+
parentJobId,
1287+
scriptStatistics,
1288+
reservationUsage);
11581289
}
11591290

11601291
final boolean baseEquals(JobStatistics jobStatistics) {
@@ -1172,6 +1303,10 @@ com.google.api.services.bigquery.model.JobStatistics toPb() {
11721303
if (scriptStatistics != null) {
11731304
statistics.setScriptStatistics(scriptStatistics.toPb());
11741305
}
1306+
if (reservationUsage != null) {
1307+
statistics.setReservationUsage(
1308+
Lists.transform(reservationUsage, ReservationUsage.TO_PB_FUNCTION));
1309+
}
11751310
return statistics;
11761311
}
11771312

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobStatisticsTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.cloud.bigquery.JobStatistics.ExtractStatistics;
2424
import com.google.cloud.bigquery.JobStatistics.LoadStatistics;
2525
import com.google.cloud.bigquery.JobStatistics.QueryStatistics;
26+
import com.google.cloud.bigquery.JobStatistics.ReservationUsage;
2627
import com.google.cloud.bigquery.JobStatistics.ScriptStatistics;
2728
import com.google.cloud.bigquery.JobStatistics.ScriptStatistics.ScriptStackFrame;
2829
import com.google.cloud.bigquery.QueryStage.QueryStep;
@@ -58,6 +59,8 @@ public class JobStatisticsTest {
5859
private static final Long CREATION_TIME = 10L;
5960
private static final Long END_TIME = 20L;
6061
private static final Long START_TIME = 15L;
62+
private static final String NAME = "reservation-name";
63+
private static final Long SLOTMS = 12545L;
6164
private static final CopyStatistics COPY_STATISTICS =
6265
CopyStatistics.newBuilder()
6366
.setCreationTimestamp(CREATION_TIME)
@@ -200,6 +203,8 @@ public class JobStatisticsTest {
200203
.setEvaluationKind(EVALUATIONKIND_TYPE_EXPRESSION)
201204
.setStackFrames(ImmutableList.of(EXPRESSION_STACK_FRAME))
202205
.build();
206+
private static final ReservationUsage RESERVATION_USAGE =
207+
ReservationUsage.newBuilder().setName(NAME).setSlotMs(SLOTMS).build();
203208

204209
@Test
205210
public void testBuilder() {
@@ -268,6 +273,8 @@ public void testBuilder() {
268273
assertEquals(EVALUATIONKIND_TYPE_EXPRESSION, EXPRESSION_SCRIPT_STATISTICS.getEvaluationKind());
269274
assertEquals(
270275
ImmutableList.of(EXPRESSION_STACK_FRAME), EXPRESSION_SCRIPT_STATISTICS.getStackFrames());
276+
assertEquals(NAME, RESERVATION_USAGE.getName());
277+
assertEquals(SLOTMS, RESERVATION_USAGE.getSlotMs());
271278
}
272279

273280
@Test
@@ -292,6 +299,7 @@ public void testToPbAndFromPb() {
292299
for (ScriptStackFrame stackFrame : EXPRESSION_SCRIPT_STATISTICS.getStackFrames()) {
293300
compareStackFrames(stackFrame, ScriptStackFrame.fromPb(stackFrame.toPb()));
294301
}
302+
compareReservation(RESERVATION_USAGE, ReservationUsage.fromPb(RESERVATION_USAGE.toPb()));
295303
}
296304

297305
@Test
@@ -392,4 +400,13 @@ private void compareStackFrames(
392400
assertEquals(expected.getStartLine(), value.getStartLine());
393401
assertEquals(expected.getText(), value.getText());
394402
}
403+
404+
private void compareReservation(ReservationUsage expected, ReservationUsage value) {
405+
assertEquals(expected, value);
406+
assertEquals(expected.hashCode(), value.hashCode());
407+
assertEquals(expected.toString(), value.toString());
408+
assertEquals(expected.toPb(), value.toPb());
409+
assertEquals(expected.getName(), value.getName());
410+
assertEquals(expected.getSlotMs(), value.getSlotMs());
411+
}
395412
}

0 commit comments

Comments
 (0)