def add_cluster(self, cluster): """add a cluster to helix""" if cluster in self.get_clusters(): raise HelixAlreadyExistsException( "Cluster {0} already exists".format(cluster)) for path in HELIX_ZOOKEEPER_PATHS.get("cluster"): self.zk.ensure_path( self._build_path(path.format(clusterName=cluster))) data = CLUSTER_CONFIG_TEMPLATE data["id"] = cluster try: self.zk.create( self._build_path( CLUSTER_CONFIG_PATH.format(clusterName=cluster)), json.dumps(data)) except NodeExistsError: # Ignore existing cluster pass # Insert state defs if they don't exist for state_def in STATE_DEF_MAP: if not self.zk.exists( self._build_path( STATE_MODEL_DEF_PATH.format( clusterName=cluster, stateModelName=state_def))): self.zk.create( self._build_path( STATE_MODEL_DEF_PATH.format(clusterName=cluster, stateModelName=state_def)), json.dumps(STATE_DEF_MAP[state_def])) return True
def add_instance(host, cluster, instances, port): """add a list of instances to a cluster""" if cluster not in get_clusters(host): raise HelixDoesNotExistException( "Cluster {0} does not exist".format(cluster)) if not isinstance(instances, list): instances = [instances] instances = ["{0}:{1}".format(instance, port) for instance in instances] try: newinstances = set(instances) oldinstances = set( [x["id"].replace('_', ':') for x in get_instances(host, cluster)]) instances = list(newinstances - oldinstances) except HelixException: # this will get thrown if instances is empty, # which if we're just populating should happen pass if instances: data = {"command": "addInstance", "instanceNames": ";".join(instances)} instance_path = "/clusters/{0}/instances".format(cluster) # print "adding to", instance_path page = _post_payload(host, instance_path, data) return page else: raise HelixAlreadyExistsException( "All instances given already exist in cluster")
def add_cluster(self, cluster): """add a cluster to helix""" if cluster in self.get_clusters(): raise HelixAlreadyExistsException( "Cluster {0} already exists".format(cluster)) data = {"command": "addCluster", "clusterName": cluster} page = self._post_payload("/clusters", data) return page
def add_resource(host, cluster, resource, partitions, state_model_def, mode=""): """Add given resource group""" if resource in get_resource_groups(host, cluster): raise HelixAlreadyExistsException( "ResourceGroup {0} already exists".format(resource)) data = {"command": "addResource", "resourceGroupName": resource, "partitions": partitions, "stateModelDefRef": state_model_def} if mode: data["mode"] = mode return _post_payload(host, "/clusters/{0}/resourceGroups".format(cluster), data)
def add_instance(self, cluster, instances, port): """add a list of instances to a cluster""" if cluster not in self.get_clusters(): raise HelixDoesNotExistException( "Cluster {0} does not exist".format(cluster)) if not isinstance(instances, list): instances = [instances] instances = [ "{instance}:{port}".format(instance=instance, port=port) for instance in instances ] try: newinstances = set(instances) oldinstances = set([ x["id"].replace('_', ':') for x in self.get_instances(cluster) ]) instances = list(newinstances - oldinstances) except HelixException: # this will get thrown if instances is empty, # which if we're just populating should happen pass if instances: for instance in instances: data = self._build_instance_entry(instance) self.zk.create( self._build_path( PARTICIPANT_CONFIG_PATH.format( clusterName=cluster, instanceName=instance.replace(':', '_'))), json.dumps(data)) for path in HELIX_ZOOKEEPER_PATHS.get("instance"): self.zk.ensure_path( self._build_path( path.format(clusterName=cluster, instanceName=instance.replace( ':', '_')))) return True else: raise HelixAlreadyExistsException( "All instances given already exist in cluster")
def add_instance_tag(self, cluster, instance, tag): """add tag to an instance""" instance_data, instance_meta = self._get_instance_detail( cluster, instance) instance_tags = instance_data.get("listFields").get("TAG_LIST", []) if tag in instance_tags: raise HelixAlreadyExistsException( "Tag ({tag}) already exists for instance ({instance}).".format( tag=tag, instance=instance)) instance_tags.append(tag) instance_data["listFields"]["TAG_LIST"] = instance_tags # XXX: Apply some retry logic here self.zk.set(self._build_path( PARTICIPANT_CONFIG_PATH.format(clusterName=cluster, instanceName=instance)), json.dumps(instance_data), version=instance_meta.version) return True
def add_resource(self, cluster, resource, partitions, state_model_def, mode="", state_model_factory_name="DEFAULT"): """Add given resource group""" if resource in self.get_resource_groups(cluster): raise HelixAlreadyExistsException( "ResourceGroup {0} already exists".format(resource)) data = { "id": resource, "mapFields": {}, "listFields": {}, "simpleFields": { "IDEAL_STATE_MODE": "AUTO", "NUM_PARTITIONS": partitions, "REBALANCE_MODE": mode, "REPLICAS": "0", "STATE_MODEL_DEF_REF": state_model_def, "STATE_MODEL_FACTORY_NAME": state_model_factory_name } } if mode: if mode in RESOURCE_MODES: data["mode"] = mode else: raise ValueError("Invalid mode ({mode})".format(mode=mode)) self.zk.create( self._build_path( RESOURCE_IDEAL_STATE_PATH.format(clusterName=cluster, resourceName=resource)), json.dumps(data)) return True