Esempio n. 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();
    }
  }
Esempio n. 2
0
  public static void handleClusterTerminatingEvent(ClusterStatusClusterTerminatingEvent event) {

    TopologyManager.acquireWriteLock();

    try {
      Topology topology = TopologyManager.getTopology();
      Cluster cluster =
          topology.getService(event.getServiceName()).getCluster(event.getClusterId());

      if (!cluster.isStateTransitionValid(ClusterStatus.Terminating, event.getInstanceId())) {
        log.error(
            "Invalid state transfer from "
                + cluster.getStatus(event.getInstanceId())
                + " to "
                + ClusterStatus.Terminating);
      }
      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.Terminating;
      if (context.isStateTransitionValid(status)) {
        context.setStatus(status);
        log.info("Cluster Terminating started for " + cluster.getClusterId());
        TopologyManager.updateTopology(topology);
        // publishing data
        ClusterInstanceTerminatingEvent clusterTerminaingEvent =
            new ClusterInstanceTerminatingEvent(
                event.getAppId(),
                event.getServiceName(),
                event.getClusterId(),
                event.getInstanceId());

        TopologyEventPublisher.sendClusterTerminatingEvent(clusterTerminaingEvent);

        // Remove kubernetes services if available
        ClusterContext clusterContext =
            CloudControllerContext.getInstance().getClusterContext(event.getClusterId());
        if (StringUtils.isNotBlank(clusterContext.getKubernetesClusterId())) {
          KubernetesIaas.removeKubernetesServices(event.getAppId(), event.getClusterId());
        }
      } 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));
      }
    } finally {
      TopologyManager.releaseWriteLock();
    }
  }
Esempio n. 3
0
  public static void handleClusterInstanceCreated(
      String serviceType,
      String clusterId,
      String alias,
      String instanceId,
      String partitionId,
      String networkPartitionId) {

    TopologyManager.acquireWriteLock();

    try {
      Topology topology = TopologyManager.getTopology();
      Service service = topology.getService(serviceType);
      if (service == null) {
        log.error(
            "Service "
                + serviceType
                + " not found in Topology, unable to update the cluster status to Created");
        return;
      }

      Cluster cluster = service.getCluster(clusterId);
      if (cluster == null) {
        log.error(
            "Cluster "
                + clusterId
                + " not found in Topology, unable to update "
                + "status to Created");
        return;
      }

      if (cluster.getInstanceContexts(instanceId) != null) {
        log.warn(
            "The Instance context for the cluster already exists for [cluster] "
                + clusterId
                + " [instance-id] "
                + instanceId);
        return;
      }

      ClusterInstance clusterInstance = new ClusterInstance(alias, clusterId, instanceId);
      clusterInstance.setNetworkPartitionId(networkPartitionId);
      clusterInstance.setPartitionId(partitionId);
      cluster.addInstanceContext(instanceId, clusterInstance);
      TopologyManager.updateTopology(topology);

      ClusterInstanceCreatedEvent clusterInstanceCreatedEvent =
          new ClusterInstanceCreatedEvent(serviceType, clusterId, clusterInstance);
      clusterInstanceCreatedEvent.setPartitionId(partitionId);
      TopologyEventPublisher.sendClusterInstanceCreatedEvent(clusterInstanceCreatedEvent);

    } finally {
      TopologyManager.releaseWriteLock();
    }
  }
Esempio n. 4
0
  public static void handleClusterActivatedEvent(
      ClusterStatusClusterActivatedEvent clusterStatusClusterActivatedEvent) {

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

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

    String clusterId = cluster.getClusterId();
    ClusterContext clusterContext =
        CloudControllerContext.getInstance().getClusterContext(clusterId);
    if (clusterContext == null) {
      log.warn("Cluster context not found: [cluster-id] " + clusterId);
      return;
    }

    ClusterInstanceActivatedEvent clusterInstanceActivatedEvent =
        new ClusterInstanceActivatedEvent(
            clusterStatusClusterActivatedEvent.getAppId(),
            clusterStatusClusterActivatedEvent.getServiceName(),
            clusterStatusClusterActivatedEvent.getClusterId(),
            clusterStatusClusterActivatedEvent.getInstanceId());
    try {
      TopologyManager.acquireWriteLock();
      List<KubernetesService> kubernetesServices = clusterContext.getKubernetesServices();
      cluster.setKubernetesServices(kubernetesServices);

      if (kubernetesServices != null) {
        // Set kubernetes services
        cluster.setKubernetesServices(kubernetesServices);
        try {
          // Generate access URLs for kubernetes services
          for (KubernetesService kubernetesService : kubernetesServices) {

            if (kubernetesService.getServiceType().equals(KubernetesConstants.NODE_PORT)) {
              // Public IP = Kubernetes minion public IP
              String[] publicIPs = kubernetesService.getPublicIPs();
              if ((publicIPs != null) && (publicIPs.length > 0)) {
                for (String publicIP : publicIPs) {
                  // There can be a String array with null values
                  if (publicIP != null) {
                    // Using type URI since only http, https, ftp, file, jar protocols are supported
                    // in URL
                    URI accessURL =
                        new URI(
                            kubernetesService.getProtocol(),
                            null,
                            publicIP,
                            kubernetesService.getPort(),
                            null,
                            null,
                            null);
                    cluster.addAccessUrl(accessURL.toString());
                    clusterInstanceActivatedEvent.addAccessUrl(accessURL.toString());
                  } else {
                    log.error(
                        String.format(
                            "Could not create access URL for [Kubernetes-service] %s , "
                                + "since Public IP is not available",
                            kubernetesService.getId()));
                  }
                }
              }
            }
          }
        } catch (URISyntaxException e) {
          log.error("Could not create access URLs for Kubernetes services", e);
        }
      }

      ClusterInstance context =
          cluster.getInstanceContexts(clusterStatusClusterActivatedEvent.getInstanceId());

      if (context == null) {
        log.warn(
            "Cluster instance context is not found for [cluster] "
                + clusterStatusClusterActivatedEvent.getClusterId()
                + " [instance-id] "
                + clusterStatusClusterActivatedEvent.getInstanceId());
        return;
      }
      ClusterStatus status = ClusterStatus.Active;
      if (context.isStateTransitionValid(status)) {
        context.setStatus(status);
        log.info("Cluster activated adding status started for " + cluster.getClusterId());
        TopologyManager.updateTopology(topology);
        // publish event
        TopologyEventPublisher.sendClusterActivatedEvent(clusterInstanceActivatedEvent);
      } else {
        log.error(
            String.format(
                "Cluster state transition is not valid: [cluster-id] %s "
                    + " [instance-id] %s [current-status] %s [status-requested] %s",
                clusterStatusClusterActivatedEvent.getClusterId(),
                clusterStatusClusterActivatedEvent.getInstanceId(),
                context.getStatus(),
                status));
        return;
      }
    } finally {
      TopologyManager.releaseWriteLock();
    }
  }