示例#1
0
  public static void handleApplicationClustersCreated(String appId, List<Cluster> appClusters) {

    TopologyManager.acquireWriteLock();

    try {
      Topology topology = TopologyManager.getTopology();
      for (Cluster cluster : appClusters) {
        Service service = topology.getService(cluster.getServiceName());
        if (service == null) {
          log.error(
              "Service "
                  + cluster.getServiceName()
                  + " not found in Topology, unable to create Application cluster");
        } else {
          service.addCluster(cluster);
          log.info("Application Cluster " + cluster.getClusterId() + " created in CC topology");
        }
      }
      TopologyManager.updateTopology(topology);
    } finally {
      TopologyManager.releaseWriteLock();
    }

    log.debug("Creating cluster port mappings: [appication-id] " + appId);
    for (Cluster cluster : appClusters) {
      String cartridgeType = cluster.getServiceName();
      Cartridge cartridge = CloudControllerContext.getInstance().getCartridge(cartridgeType);
      if (cartridge == null) {
        throw new CloudControllerException(
            "Cartridge not found: [cartridge-type] " + cartridgeType);
      }

      for (PortMapping portMapping : cartridge.getPortMappings()) {
        ClusterPortMapping clusterPortMapping =
            new ClusterPortMapping(
                appId,
                cluster.getClusterId(),
                portMapping.getName(),
                portMapping.getProtocol(),
                portMapping.getPort(),
                portMapping.getProxyPort());
        if (portMapping.getKubernetesPortType() != null) {
          clusterPortMapping.setKubernetesServiceType(portMapping.getKubernetesPortType());
        }
        CloudControllerContext.getInstance().addClusterPortMapping(clusterPortMapping);
        log.debug("Cluster port mapping created: " + clusterPortMapping.toString());
      }
    }

    // Persist cluster port mappings
    CloudControllerContext.getInstance().persist();

    // Send application clusters created event
    TopologyEventPublisher.sendApplicationClustersCreated(appId, appClusters);
  }
示例#2
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();
    }
  }
示例#3
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();
    }
  }
示例#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();
    }
  }
示例#5
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();
    }
  }