Skip to content

Commit

Permalink
feat: Adding new fields for Instance Create Time and Update Time (#1913)
Browse files Browse the repository at this point in the history
* feat: Adding new fields for Instance Create Time and Update Time
  • Loading branch information
gauravpurohit06 authored Jul 15, 2022
1 parent 6ea1e2e commit 2c71e02
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.api.gax.longrunning.OperationFuture;
import com.google.api.gax.paging.Page;
import com.google.cloud.Policy;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.Options.ListOption;
import com.google.longrunning.Operation;
import com.google.spanner.admin.database.v1.CreateDatabaseMetadata;
Expand Down Expand Up @@ -61,6 +62,18 @@ public Builder setDisplayName(String displayName) {
return this;
}

@Override
Builder setUpdateTime(Timestamp updateTime) {
infoBuilder.setUpdateTime(updateTime);
return this;
}

@Override
Builder setCreateTime(Timestamp createTime) {
infoBuilder.setCreateTime(createTime);
return this;
}

@Override
public Builder setNodeCount(int nodeCount) {
infoBuilder.setNodeCount(nodeCount);
Expand Down Expand Up @@ -205,6 +218,8 @@ static Instance fromProto(
.setInstanceConfigId(InstanceConfigId.of(proto.getConfig()))
.setDisplayName(proto.getDisplayName())
.setNodeCount(proto.getNodeCount())
.setCreateTime(Timestamp.fromProto(proto.getCreateTime()))
.setUpdateTime(Timestamp.fromProto(proto.getUpdateTime()))
.setProcessingUnits(proto.getProcessingUnits());
State state;
switch (proto.getState()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.cloud.FieldSelector;
import com.google.cloud.Timestamp;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.FieldMask;
Expand Down Expand Up @@ -77,6 +78,14 @@ public abstract static class Builder {

public abstract Builder setDisplayName(String displayName);

Builder setUpdateTime(Timestamp updateTime) {
throw new UnsupportedOperationException("Unimplemented");
}

Builder setCreateTime(Timestamp createTime) {
throw new UnsupportedOperationException("Unimplemented");
}

/**
* Sets the number of nodes for the instance. Exactly one of processing units or node count must
* be set when creating a new instance.
Expand Down Expand Up @@ -110,6 +119,8 @@ static class BuilderImpl extends Builder {
private int processingUnits;
private State state;
private Map<String, String> labels;
private Timestamp updateTime;
private Timestamp createTime;

BuilderImpl(InstanceId id) {
this.id = id;
Expand All @@ -124,6 +135,8 @@ static class BuilderImpl extends Builder {
this.processingUnits = instance.processingUnits;
this.state = instance.state;
this.labels = new HashMap<>(instance.labels);
this.updateTime = instance.updateTime;
this.createTime = instance.createTime;
}

@Override
Expand All @@ -138,6 +151,18 @@ public BuilderImpl setDisplayName(String displayName) {
return this;
}

@Override
Builder setUpdateTime(Timestamp updateTime) {
this.updateTime = updateTime;
return this;
}

@Override
Builder setCreateTime(Timestamp createTime) {
this.createTime = createTime;
return this;
}

@Override
public BuilderImpl setNodeCount(int nodeCount) {
this.nodeCount = nodeCount;
Expand Down Expand Up @@ -181,6 +206,8 @@ public InstanceInfo build() {
private final int processingUnits;
private final State state;
private final ImmutableMap<String, String> labels;
private final Timestamp updateTime;
private final Timestamp createTime;

InstanceInfo(BuilderImpl builder) {
this.id = builder.id;
Expand All @@ -190,6 +217,8 @@ public InstanceInfo build() {
this.processingUnits = builder.processingUnits;
this.state = builder.state;
this.labels = ImmutableMap.copyOf(builder.labels);
this.updateTime = builder.updateTime;
this.createTime = builder.createTime;
}

/** Returns the identifier of the instance. */
Expand All @@ -207,6 +236,14 @@ public String getDisplayName() {
return displayName;
}

public Timestamp getUpdateTime() {
return updateTime;
}

public Timestamp getCreateTime() {
return createTime;
}

/** Returns the node count of the instance. */
public int getNodeCount() {
return nodeCount;
Expand Down Expand Up @@ -241,6 +278,8 @@ public String toString() {
.add("processingUnits", processingUnits)
.add("state", state)
.add("labels", labels)
.add("createTime", createTime)
.add("updateTime", updateTime)
.toString();
}

Expand All @@ -259,12 +298,23 @@ public boolean equals(Object o) {
&& nodeCount == that.nodeCount
&& processingUnits == that.processingUnits
&& state == that.state
&& Objects.equals(labels, that.labels);
&& Objects.equals(labels, that.labels)
&& Objects.equals(updateTime, that.updateTime)
&& Objects.equals(createTime, that.createTime);
}

@Override
public int hashCode() {
return Objects.hash(id, configId, displayName, nodeCount, processingUnits, state, labels);
return Objects.hash(
id,
configId,
displayName,
nodeCount,
processingUnits,
state,
labels,
updateTime,
createTime);
}

com.google.spanner.admin.instance.v1.Instance toProto() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.google.cloud.Timestamp;
import com.google.common.testing.EqualsTester;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -41,6 +42,8 @@ public void testEmptyBuilder() {
assertEquals(0, info.getNodeCount());
assertEquals(0, info.getProcessingUnits());
assertTrue(info.getLabels().isEmpty());
assertNull(info.getUpdateTime());
assertNull(info.getCreateTime());
}

@Test
Expand All @@ -56,6 +59,8 @@ public void testBuildInstanceInfo() {
.setState(InstanceInfo.State.READY)
.addLabel("env", "prod")
.addLabel("region", "us")
.setUpdateTime(Timestamp.ofTimeMicroseconds(86000))
.setCreateTime(Timestamp.ofTimeMicroseconds(46000))
.build();
assertThat(info.getId()).isEqualTo(id);
assertThat(info.getInstanceConfigId()).isEqualTo(configId);
Expand All @@ -64,6 +69,8 @@ public void testBuildInstanceInfo() {
assertThat(info.getProcessingUnits()).isEqualTo(2000);
assertThat(info.getState()).isEqualTo(InstanceInfo.State.READY);
assertThat(info.getLabels()).containsExactly("env", "prod", "region", "us");
assertEquals(Timestamp.ofTimeMicroseconds(86000), info.getUpdateTime());
assertEquals(Timestamp.ofTimeMicroseconds(46000), info.getCreateTime());

info = info.toBuilder().setDisplayName("new test instance").build();
assertThat(info.getId()).isEqualTo(id);
Expand All @@ -73,6 +80,8 @@ public void testBuildInstanceInfo() {
assertThat(info.getProcessingUnits()).isEqualTo(2000);
assertThat(info.getState()).isEqualTo(InstanceInfo.State.READY);
assertThat(info.getLabels()).containsExactly("env", "prod", "region", "us");
assertEquals(Timestamp.ofTimeMicroseconds(86000), info.getUpdateTime());
assertEquals(Timestamp.ofTimeMicroseconds(46000), info.getCreateTime());
}

@Test
Expand All @@ -88,6 +97,8 @@ public void testToBuilder() {
.setState(InstanceInfo.State.READY)
.addLabel("env", "prod")
.addLabel("region", "us")
.setUpdateTime(Timestamp.ofTimeMicroseconds(86000))
.setCreateTime(Timestamp.ofTimeMicroseconds(46000))
.build();

InstanceInfo rebuilt = info.toBuilder().setDisplayName("new test instance").build();
Expand All @@ -98,6 +109,8 @@ public void testToBuilder() {
assertThat(rebuilt.getProcessingUnits()).isEqualTo(2000);
assertThat(rebuilt.getState()).isEqualTo(InstanceInfo.State.READY);
assertThat(rebuilt.getLabels()).containsExactly("env", "prod", "region", "us");
assertEquals(Timestamp.ofTimeMicroseconds(86000), rebuilt.getUpdateTime());
assertEquals(Timestamp.ofTimeMicroseconds(46000), rebuilt.getCreateTime());
}

@Test
Expand All @@ -115,6 +128,8 @@ public void testEquals() {
.setState(InstanceInfo.State.READY)
.addLabel("env", "prod")
.addLabel("region", "us")
.setUpdateTime(Timestamp.ofTimeMicroseconds(86000))
.setCreateTime(Timestamp.ofTimeMicroseconds(46000))
.build();
InstanceInfo instance2 =
InstanceInfo.newBuilder(id)
Expand All @@ -125,6 +140,8 @@ public void testEquals() {
.setState(InstanceInfo.State.READY)
.addLabel("region", "us")
.addLabel("env", "prod")
.setUpdateTime(Timestamp.ofTimeMicroseconds(86000))
.setCreateTime(Timestamp.ofTimeMicroseconds(46000))
.build();
InstanceInfo instance3 =
InstanceInfo.newBuilder(id)
Expand All @@ -134,6 +151,8 @@ public void testEquals() {
.setProcessingUnits(2000)
.setState(InstanceInfo.State.READY)
.addLabel("env", "prod")
.setUpdateTime(Timestamp.ofTimeMicroseconds(8000))
.setCreateTime(Timestamp.ofTimeMicroseconds(4000))
.build();
EqualsTester tester = new EqualsTester();
tester.addEqualityGroup(instance, instance2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package com.google.cloud.spanner;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.MockitoAnnotations.initMocks;

import com.google.cloud.Identity;
import com.google.cloud.Policy;
import com.google.cloud.Role;
import com.google.cloud.Timestamp;
import com.google.common.testing.EqualsTester;
import java.util.Collections;
import org.junit.Before;
Expand Down Expand Up @@ -56,6 +58,8 @@ public void buildInstance() {
.setState(InstanceInfo.State.READY)
.addLabel("env", "prod")
.addLabel("region", "us")
.setUpdateTime(Timestamp.ofTimeMicroseconds(86000))
.setCreateTime(Timestamp.ofTimeMicroseconds(46000))
.build();
assertThat(instance.getId()).isEqualTo(id);
assertThat(instance.getInstanceConfigId()).isEqualTo(configId);
Expand All @@ -64,6 +68,8 @@ public void buildInstance() {
assertThat(instance.getProcessingUnits()).isEqualTo(2000);
assertThat(instance.getState()).isEqualTo(InstanceInfo.State.READY);
assertThat(instance.getLabels()).containsExactly("env", "prod", "region", "us");
assertEquals(Timestamp.ofTimeMicroseconds(86000), instance.getUpdateTime());
assertEquals(Timestamp.ofTimeMicroseconds(46000), instance.getCreateTime());

instance = instance.toBuilder().setDisplayName("new test instance").build();
assertThat(instance.getId()).isEqualTo(id);
Expand All @@ -73,6 +79,8 @@ public void buildInstance() {
assertThat(instance.getProcessingUnits()).isEqualTo(2000);
assertThat(instance.getState()).isEqualTo(InstanceInfo.State.READY);
assertThat(instance.getLabels()).containsExactly("env", "prod", "region", "us");
assertEquals(Timestamp.ofTimeMicroseconds(86000), instance.getUpdateTime());
assertEquals(Timestamp.ofTimeMicroseconds(46000), instance.getCreateTime());
}

@Test
Expand All @@ -89,6 +97,8 @@ public void equality() {
.setState(InstanceInfo.State.READY)
.addLabel("env", "prod")
.addLabel("region", "us")
.setUpdateTime(Timestamp.ofTimeMicroseconds(86000))
.setCreateTime(Timestamp.ofTimeMicroseconds(46000))
.build();
Instance instance2 =
new Instance.Builder(instanceClient, dbClient, id)
Expand All @@ -99,6 +109,8 @@ public void equality() {
.setState(InstanceInfo.State.READY)
.addLabel("region", "us")
.addLabel("env", "prod")
.setUpdateTime(Timestamp.ofTimeMicroseconds(86000))
.setCreateTime(Timestamp.ofTimeMicroseconds(46000))
.build();
Instance instance3 =
new Instance.Builder(instanceClient, dbClient, id)
Expand All @@ -108,6 +120,8 @@ public void equality() {
.setProcessingUnits(2000)
.setState(InstanceInfo.State.READY)
.addLabel("env", "prod")
.setUpdateTime(Timestamp.ofTimeMicroseconds(8000))
.setCreateTime(Timestamp.ofTimeMicroseconds(4000))
.build();
EqualsTester tester = new EqualsTester();
tester.addEqualityGroup(instance, instance2);
Expand Down

0 comments on commit 2c71e02

Please sign in to comment.