Exemplo n.º 1
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();
    }
  }
Exemplo n.º 2
0
  public static void handleClusterRemoved(ClusterContext ctxt) {
    Topology topology = TopologyManager.getTopology();
    Service service = topology.getService(ctxt.getCartridgeType());
    String deploymentPolicy;
    if (service == null) {
      log.warn(String.format("Service %s does not exist", ctxt.getCartridgeType()));
      return;
    }

    if (!service.clusterExists(ctxt.getClusterId())) {
      log.warn(
          String.format(
              "Cluster %s does not exist for service %s",
              ctxt.getClusterId(), ctxt.getCartridgeType()));
      return;
    }

    try {
      TopologyManager.acquireWriteLock();
      Cluster cluster = service.removeCluster(ctxt.getClusterId());
      deploymentPolicy = cluster.getDeploymentPolicyName();
      TopologyManager.updateTopology(topology);
    } finally {
      TopologyManager.releaseWriteLock();
    }
    TopologyEventPublisher.sendClusterRemovedEvent(ctxt, deploymentPolicy);
  }
Exemplo n.º 3
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();
    }
  }
Exemplo n.º 4
0
  public static void handleMemberActivated(InstanceActivatedEvent instanceActivatedEvent) {
    Topology topology = TopologyManager.getTopology();
    Service service = topology.getService(instanceActivatedEvent.getServiceName());
    if (service == null) {
      log.warn(String.format("Service %s does not exist", instanceActivatedEvent.getServiceName()));
      return;
    }

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

    Member member = cluster.getMember(instanceActivatedEvent.getMemberId());
    if (member == null) {
      log.warn(String.format("Member %s does not exist", instanceActivatedEvent.getMemberId()));
      return;
    }

    MemberActivatedEvent memberActivatedEvent =
        new MemberActivatedEvent(
            instanceActivatedEvent.getServiceName(),
            instanceActivatedEvent.getClusterId(),
            instanceActivatedEvent.getClusterInstanceId(),
            instanceActivatedEvent.getMemberId(),
            instanceActivatedEvent.getNetworkPartitionId(),
            instanceActivatedEvent.getPartitionId());

    // grouping - set grouid
    // TODO
    memberActivatedEvent.setApplicationId(null);
    try {
      TopologyManager.acquireWriteLock();
      // try update lifecycle state
      if (!member.isStateTransitionValid(MemberStatus.Active)) {
        log.error(
            "Invalid state transition from ["
                + member.getStatus()
                + "] to ["
                + MemberStatus.Active
                + "]");
        return;
      } else {
        member.setStatus(MemberStatus.Active);

        // Set member ports
        try {
          Cartridge cartridge =
              CloudControllerContext.getInstance().getCartridge(service.getServiceName());
          if (cartridge == null) {
            throw new RuntimeException(
                String.format(
                    "Cartridge not found: [cartridge-type] %s", service.getServiceName()));
          }

          Port port;
          int portValue;
          List<PortMapping> portMappings = Arrays.asList(cartridge.getPortMappings());
          String clusterId = cluster.getClusterId();
          ClusterContext clusterContext =
              CloudControllerContext.getInstance().getClusterContext(clusterId);
          List<KubernetesService> kubernetesServices = clusterContext.getKubernetesServices();

          for (PortMapping portMapping : portMappings) {
            if (kubernetesServices != null) {
              portValue = findKubernetesServicePort(clusterId, kubernetesServices, portMapping);
            } else {
              portValue = portMapping.getPort();
            }
            port = new Port(portMapping.getProtocol(), portValue, portMapping.getProxyPort());
            member.addPort(port);
            memberActivatedEvent.addPort(port);
          }
        } catch (Exception e) {
          String message =
              String.format(
                  "Could not add member ports: [service-name] %s [member-id] %s",
                  memberActivatedEvent.getServiceName(), memberActivatedEvent.getMemberId());
          log.error(message, e);
        }

        // Set member ip addresses
        memberActivatedEvent.setDefaultPrivateIP(member.getDefaultPrivateIP());
        memberActivatedEvent.setMemberPrivateIPs(member.getMemberPrivateIPs());
        memberActivatedEvent.setDefaultPublicIP(member.getDefaultPublicIP());
        memberActivatedEvent.setMemberPublicIPs(member.getMemberPublicIPs());
        TopologyManager.updateTopology(topology);

        // Publish member activated event
        TopologyEventPublisher.sendMemberActivatedEvent(memberActivatedEvent);

        // Publish statistics data
        BAMUsageDataPublisher.publish(
            memberActivatedEvent.getMemberId(),
            memberActivatedEvent.getPartitionId(),
            memberActivatedEvent.getNetworkPartitionId(),
            memberActivatedEvent.getClusterId(),
            memberActivatedEvent.getServiceName(),
            MemberStatus.Active.toString(),
            null);
      }
    } finally {
      TopologyManager.releaseWriteLock();
    }
  }