Skip to content

Commit

Permalink
Bigtable: wrap proto enums (googleapis#3659)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbernstein2 authored Sep 18, 2018
1 parent 6f1a105 commit 43d53a8
Show file tree
Hide file tree
Showing 12 changed files with 529 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
package com.google.cloud.bigtable.admin.v2.models;

import com.google.api.core.InternalApi;
import com.google.bigtable.admin.v2.Cluster.State;
import com.google.bigtable.admin.v2.ClusterName;
import com.google.bigtable.admin.v2.LocationName;
import com.google.bigtable.admin.v2.StorageType;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
Expand All @@ -32,8 +30,63 @@
* in the instance.
*/
public class Cluster {
public enum State {
/** The state of the cluster could not be determined. */
NOT_KNOWN(com.google.bigtable.admin.v2.Cluster.State.STATE_NOT_KNOWN),
/** The cluster has been successfully created and is ready to serve requests. */
READY(com.google.bigtable.admin.v2.Cluster.State.READY),
/**
* The cluster is currently being created, and may be destroyed if the creation process
* encounters an error. A cluster may not be able to serve requests while being created.
*/
CREATING(com.google.bigtable.admin.v2.Cluster.State.CREATING),
/**
* The cluster is currently being resized, and may revert to its previous node count if the
* process encounters an error. A cluster is still capable of serving requests while being
* resized, but may exhibit performance as if its number of allocated nodes is between the
* starting and requested states.
*/
RESIZING(com.google.bigtable.admin.v2.Cluster.State.RESIZING),
/**
* The cluster has no backing nodes. The data (tables) still exist, but no operations can be
* performed on the cluster.
*/
DISABLED(com.google.bigtable.admin.v2.Cluster.State.DISABLED),
/** The state of the cluster is not known by this client. Please upgrade your client. */
UNRECOGNIZED(com.google.bigtable.admin.v2.Cluster.State.UNRECOGNIZED);

private final com.google.bigtable.admin.v2.Cluster.State proto;

/**
* Wraps the protobuf. This method is considered an internal implementation detail and not meant
* to be used by applications.
*/
@InternalApi
public static State fromProto(com.google.bigtable.admin.v2.Cluster.State proto) {
for (State state : values()) {
if (state.proto.equals(proto)) {
return state;
}
}
return UNRECOGNIZED;
}

State(com.google.bigtable.admin.v2.Cluster.State proto) {
this.proto = proto;
}

/**
* Creates the request protobuf. This method is considered an internal implementation detail and
* not meant to be used by applications.
*/
@InternalApi
public com.google.bigtable.admin.v2.Cluster.State toProto() {
return proto;
}
}

@Nonnull
private final com.google.bigtable.admin.v2.Cluster proto;
private final com.google.bigtable.admin.v2.Cluster stateProto;

/**
* Wraps a protobuf response.
Expand All @@ -49,7 +102,7 @@ public static Cluster fromProto(com.google.bigtable.admin.v2.Cluster proto) {
private Cluster(@Nonnull com.google.bigtable.admin.v2.Cluster proto) {
Preconditions.checkNotNull(proto);
Preconditions.checkArgument(!proto.getName().isEmpty(), "Name must be set");
this.proto = proto;
this.stateProto = proto;
}


Expand All @@ -58,7 +111,7 @@ private Cluster(@Nonnull com.google.bigtable.admin.v2.Cluster proto) {
public String getId() {
// Constructor ensures that name is not null
ClusterName fullName = Verify.verifyNotNull(
ClusterName.parse(proto.getName()),
ClusterName.parse(stateProto.getName()),
"Name can never be null");
//noinspection ConstantConditions
return fullName.getCluster();
Expand All @@ -69,7 +122,7 @@ public String getId() {
public String getInstanceId() {
// Constructor ensures that name is not null
ClusterName fullName = Verify.verifyNotNull(
ClusterName.parse(proto.getName()),
ClusterName.parse(stateProto.getName()),
"Name can never be null");
//noinspection ConstantConditions
return fullName.getInstance();
Expand All @@ -80,16 +133,15 @@ public String getInstanceId() {
/** Get the zone where this cluster is located. */
@SuppressWarnings("WeakerAccess")
public String getZone() {
LocationName location = Verify.verifyNotNull(LocationName.parse(proto.getLocation()));
LocationName location = Verify.verifyNotNull(LocationName.parse(stateProto.getLocation()));
//noinspection ConstantConditions
return location.getLocation();
}

/** Gets the current state of the cluster */
// TODO(igorbernstein2): try to avoid exposing proto enums
@SuppressWarnings("WeakerAccess")
public State getState() {
return proto.getState();
return State.fromProto(stateProto.getState());
}

/**
Expand All @@ -98,17 +150,16 @@ public State getState() {
*/
@SuppressWarnings("WeakerAccess")
public int getServeNodes() {
return proto.getServeNodes();
return stateProto.getServeNodes();
}

/**
* The type of storage used by this cluster to serve its parent instance's tables, unless
* explicitly overridden.
*/
// TODO(igorbernstein2): try to avoid exposing proto enums
@SuppressWarnings("WeakerAccess")
public StorageType getStorageType() {
return proto.getDefaultStorageType();
return StorageType.fromProto(stateProto.getDefaultStorageType());
}

@Override
Expand All @@ -120,11 +171,11 @@ public boolean equals(Object o) {
return false;
}
Cluster cluster = (Cluster) o;
return Objects.equal(proto, cluster.proto);
return Objects.equal(stateProto, cluster.stateProto);
}

@Override
public int hashCode() {
return Objects.hashCode(proto);
return Objects.hashCode(stateProto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@
*/
package com.google.cloud.bigtable.admin.v2.models;

import static com.google.cloud.bigtable.admin.v2.models.StorageType.SSD;

import com.google.api.core.InternalApi;
import com.google.bigtable.admin.v2.InstanceName;
import com.google.bigtable.admin.v2.LocationName;
import com.google.bigtable.admin.v2.ProjectName;
import com.google.bigtable.admin.v2.StorageType;
import com.google.common.base.Preconditions;
import javax.annotation.Nonnull;

/**
* Parameters for creating a new Bigtable cluster.
*
* <p>A cluster represents the actual Cloud Bigtable service. Each cluster belongs to a single Cloud
* Bigtable instance. When your application sends requests to a Cloud Bigtable instance, those
* <p>A cluster represents the actual Cloud Bigtable service. Each cluster belongs to a single
* Cloud Bigtable instance. When your application sends requests to a Cloud Bigtable instance, those
* requests are actually handled by one of the clusters in the instance.
*
* <p>Each cluster is located in a single zone. An instance's clusters must be in unique zones that
* are within the same region. For example, if the first cluster is in us-east1-b, then us-east1-c
* is a valid zone for the second cluster. For a list of zones and regions where Cloud Bigtable is
* available, see <a href="https://cloud.google.com/bigtable/docs/locations">Cloud Bigtable Locations</a>.
* available, see <a href="https://cloud.google.com/bigtable/docs/locations">Cloud Bigtable
* Locations</a>.
*
*
* Examples:
Expand All @@ -44,7 +48,8 @@
* .setStorageType(StorageType.SSD);
* }</pre>
*
* @see <a href="https://cloud.google.com/bigtable/docs/instances-clusters-nodes#clusters">For more details</a>
* @see <a href="https://cloud.google.com/bigtable/docs/instances-clusters-nodes#clusters">For more
* details</a>
*/
public final class CreateClusterRequest {
private final com.google.bigtable.admin.v2.CreateClusterRequest.Builder proto = com.google.bigtable.admin.v2.CreateClusterRequest
Expand All @@ -57,15 +62,16 @@ public final class CreateClusterRequest {

/**
* Builds a new request to create a new cluster to the specified instance with the specified
* cluster id. */
* cluster id.
*/
public static CreateClusterRequest of(String instanceId, String clusterId) {
return new CreateClusterRequest(instanceId, clusterId);
}

private CreateClusterRequest(String instanceId, String clusterId) {
this.instanceId = instanceId;
proto.setClusterId(clusterId);
proto.getClusterBuilder().setDefaultStorageType(StorageType.SSD);
proto.getClusterBuilder().setDefaultStorageType(SSD.toProto());
}

/**
Expand All @@ -78,22 +84,27 @@ public CreateClusterRequest setZone(String zone) {
return this;
}

/** Sets the number of nodes allocated to this cluster. More nodes enable higher throughput and
* more consistent performance. */
/**
* Sets the number of nodes allocated to this cluster. More nodes enable higher throughput and
* more consistent performance.
*/
@SuppressWarnings("WeakerAccess")
public CreateClusterRequest setServeNodes(int numNodes) {
proto.getClusterBuilder().setServeNodes(numNodes);
return this;
}

/**
* Sets the type of storage used by this cluster to serve its parent instance's tables.
* Defaults to {@code SSD}.
* Sets the type of storage used by this cluster to serve its parent instance's tables. Defaults
* to {@code SSD}.
*/
// TODO(igorbernstein2): try to avoid leaking protobuf generated enums
@SuppressWarnings("WeakerAccess")
public CreateClusterRequest setStorageType(StorageType storageType) {
proto.getClusterBuilder().setDefaultStorageType(storageType);
public CreateClusterRequest setStorageType(@Nonnull StorageType storageType) {
Preconditions.checkNotNull(storageType);
Preconditions.checkArgument(storageType != StorageType.UNRECOGNIZED,
"StorageType can't be UNRECOGNIZED");

proto.getClusterBuilder().setDefaultStorageType(storageType.toProto());
return this;
}

Expand All @@ -104,7 +115,8 @@ public CreateClusterRequest setStorageType(StorageType storageType) {
@InternalApi
public com.google.bigtable.admin.v2.CreateClusterRequest toProto(ProjectName projectName) {
proto.setParent(InstanceName.of(projectName.getProject(), instanceId).toString());
proto.getClusterBuilder().setLocation(LocationName.of(projectName.getProject(), zone).toString());
proto.getClusterBuilder()
.setLocation(LocationName.of(projectName.getProject(), zone).toString());

return proto.build();
}
Expand All @@ -130,7 +142,8 @@ String getClusterId() {
*/
@InternalApi
com.google.bigtable.admin.v2.Cluster toEmbeddedProto(ProjectName projectName) {
proto.getClusterBuilder().setLocation(LocationName.of(projectName.getProject(), zone).toString());
proto.getClusterBuilder()
.setLocation(LocationName.of(projectName.getProject(), zone).toString());

return proto.getClusterBuilder().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.google.api.core.InternalApi;
import com.google.bigtable.admin.v2.Instance.Type;
import com.google.bigtable.admin.v2.ProjectName;
import com.google.bigtable.admin.v2.StorageType;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.List;
Expand All @@ -31,13 +30,13 @@
* which do all of the real work. Instances come in 2 flavors:
*
* <dl>
* <dt>Production
* <dd>A standard instance with either 1 or 2 clusters, as well as 3 or more nodes in each cluster.
* You cannot downgrade a production instance to a development instance.
* <dt>Production
* <dd>A standard instance with either 1 or 2 clusters, as well as 3 or more nodes in each cluster.
* You cannot downgrade a production instance to a development instance.
*
* <dt>Development
* <dd>A low-cost instance for development and testing, with performance limited to the equivalent
* of a 1-node cluster. Development instances only support a single 1 node cluster.
* <dt>Development
* <dd>A low-cost instance for development and testing, with performance limited to the equivalent
* of a 1-node cluster. Development instances only support a single 1 node cluster.
* </dl>
*
* When creating an Instance, you must create at least one cluster in it.
Expand All @@ -56,7 +55,8 @@
*
* }</pre>
*
* @see <a href="https://cloud.google.com/bigtable/docs/instances-clusters-nodes#instances">For more details</a>
* @see <a href="https://cloud.google.com/bigtable/docs/instances-clusters-nodes#instances">For more
* details</a>
*/
public final class CreateInstanceRequest {
private final com.google.bigtable.admin.v2.CreateInstanceRequest.Builder builder =
Expand Down Expand Up @@ -93,11 +93,11 @@ public CreateInstanceRequest setDisplayName(@Nonnull String displayName) {
* <p>Can be either DEVELOPMENT or PRODUCTION. Defaults to PRODUCTION.
* Please see class javadoc for details.
*/
// TODO(igorbernstein2): try to avoid leaking protobuf generated enums
@SuppressWarnings("WeakerAccess")
public CreateInstanceRequest setType(@Nonnull Type type) {
public CreateInstanceRequest setType(@Nonnull Instance.Type type) {
Preconditions.checkNotNull(type);
builder.getInstanceBuilder().setType(type);
Preconditions.checkArgument(type != Instance.Type.UNRECOGNIZED, "Type is unrecognized");
builder.getInstanceBuilder().setType(type.toProto());
return this;
}

Expand All @@ -107,7 +107,8 @@ public CreateInstanceRequest setType(@Nonnull Type type) {
* <p>Labels are key-value pairs that you can use to group related instances and store metadata
* about an instance.
*
* @see <a href="https://cloud.google.com/bigtable/docs/creating-managing-labels">For more details</a>
* @see <a href="https://cloud.google.com/bigtable/docs/creating-managing-labels">For more
* details</a>
*/
@SuppressWarnings("WeakerAccess")
public CreateInstanceRequest addLabel(@Nonnull String key, @Nonnull String value) {
Expand All @@ -125,12 +126,14 @@ public CreateInstanceRequest addLabel(@Nonnull String key, @Nonnull String value
*
* @param clusterId The name of the cluster.
* @param zone The zone where the cluster will be created.
* @param serveNodes The number of nodes that cluster will contain. DEVELOPMENT instance clusters must have exactly one node.
* @param storageType The type of storage used by this cluster to serve its parent instance's tables.
* @param serveNodes The number of nodes that cluster will contain. DEVELOPMENT instance clusters
* must have exactly one node.
* @param storageType The type of storage used by this cluster to serve its parent instance's
* tables.
*/
// TODO(igorbernstein2): try to avoid leaking protobuf generated enums
@SuppressWarnings("WeakerAccess")
public CreateInstanceRequest addCluster(@Nonnull String clusterId, @Nonnull String zone, int serveNodes, @Nonnull StorageType storageType) {
public CreateInstanceRequest addCluster(@Nonnull String clusterId, @Nonnull String zone,
int serveNodes, @Nonnull StorageType storageType) {
CreateClusterRequest clusterRequest = CreateClusterRequest
.of("ignored-instance-id", clusterId)
.setZone(zone)
Expand All @@ -152,7 +155,8 @@ public com.google.bigtable.admin.v2.CreateInstanceRequest toProto(ProjectName pr
.clearClusters();

for (CreateClusterRequest clusterRequest : clusterRequests) {
builder.putClusters(clusterRequest.getClusterId(), clusterRequest.toEmbeddedProto(projectName));
builder
.putClusters(clusterRequest.getClusterId(), clusterRequest.toEmbeddedProto(projectName));
}

return builder.build();
Expand Down
Loading

0 comments on commit 43d53a8

Please sign in to comment.