/** * @return The cluster associated with the given project and instance. We expect instance and * cluster to have one-to-one relationship. * @throws IllegalStateException if the cluster is not found or if there are many clusters in this * instance. */ public static Cluster lookupCluster(String projectId, String instanceId) throws IOException { BigtableClusterUtilities utils; try { utils = BigtableClusterUtilities.forInstance(projectId, instanceId); } catch (GeneralSecurityException e) { throw new RuntimeException("Could not initialize BigtableClusterUtilities", e); } try { return utils.getSingleCluster(); } finally { try { utils.close(); } catch (Exception e) { logger.warn("Error closing BigtableClusterUtilities: ", e); } } }
public static String getZoneId(Cluster cluster) { Preconditions.checkState(cluster != null, "Cluster doesn't exist"); return getZoneId(cluster.getLocation()); }
/** * Sets a cluster size to a specific size in an instance with a single cluster * @throws InterruptedException if the cluster is in the middle of updating, and an interrupt was * received */ public void setClusterSize(int newSize) throws InterruptedException { setClusterSize(getSingleCluster().getName(), newSize); }
@Override public void run() { try { // [START scale_bigtable] double latestValue = getLatestValue().getValue().getDoubleValue(); if (latestValue < CPU_PERCENT_TO_DOWNSCALE) { int clusterSize = clusterUtility.getClusterNodeCount(clusterId, zoneId); if (clusterSize > MIN_NODE_COUNT) { clusterUtility.setClusterSize(clusterId, zoneId, Math.max(clusterSize - SIZE_CHANGE_STEP, MIN_NODE_COUNT)); } } else if (latestValue > CPU_PERCENT_TO_UPSCALE) { int clusterSize = clusterUtility.getClusterNodeCount(clusterId, zoneId); if (clusterSize <= MAX_NODE_COUNT) { clusterUtility.setClusterSize(clusterId, zoneId, Math.min(clusterSize + SIZE_CHANGE_STEP, MAX_NODE_COUNT)); } } // [END scale_bigtable] } catch (Exception e) { e.printStackTrace(); } } };
/** * Constructor for the auto-scaler with the minimum required information: project and instance ids. * @param projectId * @param instanceId * @throws GeneralSecurityException * @throws IOException */ public MetricScaler(String projectId, String instanceId) throws GeneralSecurityException, IOException { clusterUtility = BigtableClusterUtilities.forInstance(projectId, instanceId); Cluster cluster = clusterUtility.getSingleCluster(); this.clusterId = new BigtableClusterName(cluster.getName()).getClusterId(); this.zoneId = BigtableClusterUtilities.getZoneId(cluster); // Instantiates a client metricServiceClient = MetricServiceClient.create(); projectName = ProjectName.create(projectId); }
/** * @return The instance id associated with the given project, zone and cluster. We expect instance * and cluster to have one-to-one relationship. * * @throws IllegalStateException if the cluster is not found */ public static String lookupInstanceId(String projectId, String clusterId, String zoneId) throws IOException { BigtableClusterUtilities utils; try { utils = BigtableClusterUtilities.forAllInstances(projectId); } catch (GeneralSecurityException e) { throw new RuntimeException("Could not initialize BigtableClusterUtilities", e); } try { Cluster cluster = utils.getCluster(clusterId, zoneId); return new BigtableClusterName(cluster.getName()).getInstanceId(); } finally { try { utils.close(); } catch (Exception e) { logger.warn("Error closing BigtableClusterUtilities: ", e); } } }
/** * Gets the serve node count of an instance with a single cluster. * @return the {@link Cluster#getServeNodes()} of the clusterId. */ public int getClusterSize() { return getSingleCluster().getServeNodes(); }
/** * Sets a cluster size to a specific size. * @param clusterId * @param zoneId * @param newSize * @throws InterruptedException if the cluster is in the middle of updating, and an interrupt was * received */ public void setClusterSize(String clusterId, String zoneId, int newSize) throws InterruptedException { setClusterSize(instanceName.toClusterName(clusterId).getClusterName(), newSize); }
/** * Snapshot operations need various aspects of a {@link BigtableClusterName}. This method gets a * clusterId from either a lookup (projectId and instanceId translate to a single clusterId when * an instance has only one cluster). */ public synchronized BigtableClusterName getClusterName() throws IOException { if (this.clusterName == null) { try (BigtableClusterUtilities util = new BigtableClusterUtilities(options)) { ListClustersResponse clusters = util.getClusters(); Preconditions.checkState(clusters.getClustersCount() == 1, String.format( "Project '%s' / Instance '%s' has %d clusters. There must be exactly 1 for this operation to work.", options.getProjectId(), options.getInstanceId(), clusters.getClustersCount())); clusterName = new BigtableClusterName(clusters.getClusters(0).getName()); } catch (GeneralSecurityException e) { throw new IOException("Could not get cluster Id.", e); } } return clusterName; }
/** * Gets the current number of nodes allocated to the cluster. * @param clusterId * @param zoneId * @return the serveNode count of the cluster. */ public int getClusterNodeCount(String clusterId, String zoneId) { return getCluster(clusterId, zoneId).getServeNodes(); }
/** * @return a Single Cluster for the project and instance. * @throws IllegalStateException for any project / instance combination that does not return * exactly 1 cluster. */ public Cluster getSingleCluster() { ListClustersResponse response = getClusters(); Preconditions.checkState(response.getClustersCount() != 0, "The instance does not exist."); Preconditions.checkState(response.getClustersCount() == 1, "There can only be one cluster for this method to work."); return response.getClusters(0); }
/** * Creates a {@link BigtableClusterUtilities} for all instances in a projectId. * * @param projectId * * @return a {@link BigtableClusterUtilities} for a all instances in a projectId. * @throws GeneralSecurityException if ssl configuration fails * @throws IOException if some aspect of the connection fails. */ public static BigtableClusterUtilities forAllInstances(String projectId) throws IOException, GeneralSecurityException { // '-' means all instanceids. return new BigtableClusterUtilities( BigtableOptions.builder().setProjectId(projectId).setInstanceId("-").build()); }
/** * Gets the serve node count of the cluster. * @param clusterId * @param zoneId * @return the {@link Cluster#getServeNodes()} of the clusterId. * @deprecated Use {@link #getCluster(String, String)} or {@link #getSingleCluster()} and then * call {@link Cluster#getServeNodes()}. */ @Deprecated public int getClusterSize(String clusterId, String zoneId) { Cluster cluster = getCluster(clusterId, zoneId); String message = String.format("Cluster %s/%s was not found.", clusterId, zoneId); Preconditions.checkNotNull(cluster, message); return cluster.getServeNodes(); }
/** * Gets the current configuration of the cluster as encapsulated by a {@link Cluster} object. * * @param clusterId * @param zoneId * @return the {@link Cluster} if it was set. If the cluster is not found, throw a {@link * NullPointerException}. */ public Cluster getCluster(String clusterId, String zoneId) { Cluster response = null; for (Cluster cluster : getClusters().getClustersList()) { if (cluster.getName().endsWith("/clusters/" + clusterId) && cluster.getLocation().endsWith("/locations/" + zoneId)) { if (response == null) { response = cluster; } else { throw new IllegalStateException( String.format("Got multiple clusters named %s in zone %z.", clusterId, zoneId)); } } } return Preconditions.checkNotNull(response, String.format("Cluster %s in zone %s was not found.", clusterId, zoneId)); }
/** * Creates a {@link BigtableClusterUtilities} for a projectId and an instanceId. * * @param projectId * @param instanceId * * @return a {@link BigtableClusterUtilities} for a specific projectId/instanceId. * @throws GeneralSecurityException if ssl configuration fails * @throws IOException if some aspect of the connection fails. */ public static BigtableClusterUtilities forInstance(String projectId, String instanceId) throws IOException, GeneralSecurityException { return new BigtableClusterUtilities( BigtableOptions.builder().setProjectId(projectId).setInstanceId(instanceId).build()); }