示例#1
0
  public static void handleClusterTerminatedEvent(ClusterStatusClusterTerminatedEvent event) {

    TopologyManager.acquireWriteLock();

    try {
      Topology topology = TopologyManager.getTopology();
      Service service = topology.getService(event.getServiceName());

      // update the status of the cluster
      if (service == null) {
        log.warn(String.format("Service %s does not exist", event.getServiceName()));
        return;
      }

      Cluster cluster = service.getCluster(event.getClusterId());
      if (cluster == null) {
        log.warn(String.format("Cluster %s does not exist", event.getClusterId()));
        return;
      }

      ClusterInstance context = cluster.getInstanceContexts(event.getInstanceId());
      if (context == null) {
        log.warn(
            "Cluster Instance Context is not found for [cluster] "
                + event.getClusterId()
                + " [instance-id] "
                + event.getInstanceId());
        return;
      }
      ClusterStatus status = ClusterStatus.Terminated;
      if (context.isStateTransitionValid(status)) {
        context.setStatus(status);
        log.info(
            "Cluster Terminated adding status started for and removing the cluster instance"
                + cluster.getClusterId());
        cluster.removeInstanceContext(event.getInstanceId());
        TopologyManager.updateTopology(topology);
        // publishing data
        ClusterInstanceTerminatedEvent clusterTerminatedEvent =
            new ClusterInstanceTerminatedEvent(
                event.getAppId(),
                event.getServiceName(),
                event.getClusterId(),
                event.getInstanceId());

        TopologyEventPublisher.sendClusterTerminatedEvent(clusterTerminatedEvent);
      } else {
        log.error(
            String.format(
                "Cluster state transition is not valid: [cluster-id] %s "
                    + " [instance-id] %s [current-status] %s [status-requested] %s",
                event.getClusterId(), event.getInstanceId(), context.getStatus(), status));
        return;
      }
    } finally {
      TopologyManager.releaseWriteLock();
    }
  }