Ejemplo n.º 1
0
  public static void handleServiceRemoved(List<Cartridge> cartridgeList) {
    Topology topology = TopologyManager.getTopology();

    for (Cartridge cartridge : cartridgeList) {
      Service service = topology.getService(cartridge.getType());
      if (service == null) {
        log.warn("Cartridge does not exist [cartidge] " + cartridge);
        return;
      }
      if (service.getClusters().size() == 0) {
        if (topology.serviceExists(cartridge.getType())) {
          try {
            TopologyManager.acquireWriteLock();
            topology.removeService(cartridge.getType());
            TopologyManager.updateTopology(topology);
          } finally {
            TopologyManager.releaseWriteLock();
          }
          TopologyEventPublisher.sendServiceRemovedEvent(cartridgeList);
        } else {
          log.warn(String.format("Service %s does not exist..", cartridge.getType()));
        }
      } else {
        log.warn(
            "Subscription already exists. Hence not removing the service:"
                + cartridge.getType()
                + " from the topology");
      }
    }
  }
Ejemplo 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);
  }
Ejemplo n.º 3
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();
    }
  }
  /**
   * Assert application activation
   *
   * @param applicationName
   * @param tenantId
   */
  private void assertClusterWithScalingup(String applicationName, int tenantId) {
    Application application =
        ApplicationManager.getApplications().getApplicationByTenant(applicationName, tenantId);
    assertNotNull(
        String.format("Application is not found: [application-id] %s", applicationName),
        application);
    boolean clusterScaleup = false;
    String clusterId = null;
    long startTime = System.currentTimeMillis();
    while (!clusterScaleup) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException ignore) {
      }
      Set<ClusterDataHolder> clusterDataHolderSet = application.getClusterDataRecursively();
      for (ClusterDataHolder clusterDataHolder : clusterDataHolderSet) {
        String serviceUuid = clusterDataHolder.getServiceUuid();
        clusterId = clusterDataHolder.getClusterId();
        Service service = TopologyManager.getTopology().getService(serviceUuid);
        assertNotNull(
            String.format(
                "Service is not found: [application-id] %s [service] %s",
                applicationName, serviceUuid),
            service);

        Cluster cluster = service.getCluster(clusterId);
        assertNotNull(
            String.format(
                "Cluster is not found: [application-id] %s [service] %s [cluster-id] %s",
                applicationName, serviceUuid, clusterId),
            cluster);
        for (ClusterInstance instance : cluster.getInstanceIdToInstanceContextMap().values()) {
          int activeInstances = 0;
          for (Member member : cluster.getMembers()) {
            if (member.getClusterInstanceId().equals(instance.getInstanceId())) {
              if (member.getStatus().equals(MemberStatus.Active)) {
                activeInstances++;
              }
            }
          }
          clusterScaleup = activeInstances >= clusterDataHolder.getMinInstances();
          if (clusterScaleup) {
            activeInstancesAfterScaleup = activeInstances;
            break;
          }
        }
        application =
            ApplicationManager.getApplications().getApplicationByTenant(applicationName, tenantId);
        if ((System.currentTimeMillis() - startTime) > CLUSTER_SCALE_UP_TIMEOUT) {
          break;
        }
      }
    }
    assertEquals(
        true,
        clusterScaleup,
        String.format("Cluster did not get scaled up: [cluster-id] %s", clusterId));
  }
Ejemplo n.º 5
0
  public static void handleMemberMaintenance(
      InstanceMaintenanceModeEvent instanceMaintenanceModeEvent)
      throws InvalidMemberException, InvalidCartridgeTypeException {
    Topology topology = TopologyManager.getTopology();
    Service service = topology.getService(instanceMaintenanceModeEvent.getServiceName());
    // update the status of the member
    if (service == null) {
      log.warn(
          String.format(
              "Service %s does not exist", instanceMaintenanceModeEvent.getServiceName()));
      return;
    }

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

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

    MemberMaintenanceModeEvent memberMaintenanceModeEvent =
        new MemberMaintenanceModeEvent(
            instanceMaintenanceModeEvent.getServiceName(),
            instanceMaintenanceModeEvent.getClusterId(),
            instanceMaintenanceModeEvent.getClusterInstanceId(),
            instanceMaintenanceModeEvent.getMemberId(),
            instanceMaintenanceModeEvent.getNetworkPartitionId(),
            instanceMaintenanceModeEvent.getPartitionId());
    try {
      TopologyManager.acquireWriteLock();
      // try update lifecycle state
      if (!member.isStateTransitionValid(MemberStatus.In_Maintenance)) {
        log.error(
            "Invalid State Transition from "
                + member.getStatus()
                + " to "
                + MemberStatus.In_Maintenance);
        return;
      }
      member.setStatus(MemberStatus.In_Maintenance);
      log.info("member maintenance mode event adding status started");

      TopologyManager.updateTopology(topology);
    } finally {
      TopologyManager.releaseWriteLock();
    }
    // publishing data
    TopologyEventPublisher.sendMemberMaintenanceModeEvent(memberMaintenanceModeEvent);
  }
Ejemplo n.º 6
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();
    }
  }
Ejemplo n.º 7
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);
  }
Ejemplo n.º 8
0
  public static void handleApplicationClustersRemoved(
      String appId, Set<ClusterDataHolder> clusterData) {
    TopologyManager.acquireWriteLock();

    List<Cluster> removedClusters = new ArrayList<Cluster>();
    CloudControllerContext context = CloudControllerContext.getInstance();
    try {
      Topology topology = TopologyManager.getTopology();

      if (clusterData != null) {
        // remove clusters from CC topology model and remove runtime information
        for (ClusterDataHolder aClusterData : clusterData) {
          Service aService = topology.getService(aClusterData.getServiceType());
          if (aService != null) {
            removedClusters.add(aService.removeCluster(aClusterData.getClusterId()));
          } else {
            log.warn(
                "Service "
                    + aClusterData.getServiceType()
                    + " not found, "
                    + "unable to remove Cluster "
                    + aClusterData.getClusterId());
          }
          // remove runtime data
          context.removeClusterContext(aClusterData.getClusterId());

          log.info(
              "Removed application [ "
                  + appId
                  + " ]'s Cluster "
                  + "[ "
                  + aClusterData.getClusterId()
                  + " ] from the topology");
        }
        // persist runtime data changes
        CloudControllerContext.getInstance().persist();
      } else {
        log.info("No cluster data found for application " + appId + " to remove");
      }

      TopologyManager.updateTopology(topology);

    } finally {
      TopologyManager.releaseWriteLock();
    }

    // Remove cluster port mappings of application
    CloudControllerContext.getInstance().removeClusterPortMappings(appId);
    CloudControllerContext.getInstance().persist();

    TopologyEventPublisher.sendApplicationClustersRemoved(appId, clusterData);
  }
  private boolean doProcess(ClusterCreatedEvent event, Topology topology) {
    Cluster cluster = event.getCluster();
    String applicationId = cluster.getAppId();
    String serviceName = cluster.getServiceName();
    String clusterId = cluster.getClusterId();

    // Apply application filter
    if (TopologyApplicationFilter.apply(applicationId)) {
      return false;
    }

    // Apply service filter
    if (TopologyServiceFilter.apply(serviceName)) {
      return false;
    }

    // Apply cluster filter
    if (TopologyClusterFilter.apply(clusterId)) {
      return false;
    }

    // Validate event against the existing topology
    Service service = topology.getService(serviceName);
    if (service == null) {
      if (log.isWarnEnabled()) {
        log.warn(String.format("Service does not exist: [service] %s", serviceName));
      }
      return false;
    }
    if (service.clusterExists(clusterId)) {
      if (log.isDebugEnabled()) {
        log.debug(
            String.format(
                "Cluster already exists in service: [service] %s " + "[cluster] %s",
                serviceName, clusterId));
      }
    } else {

      // Apply changes to the topology
      service.addCluster(cluster);
      if (log.isInfoEnabled()) {
        log.info(String.format("Cluster created: %s", cluster.toString()));
      }
    }

    // Notify event listeners
    notifyEventListeners(event);
    return true;
  }
Ejemplo n.º 10
0
  /**
   * Remove member from topology and send member terminated event.
   *
   * @param serviceName
   * @param clusterId
   * @param networkPartitionId
   * @param partitionId
   * @param memberId
   */
  public static void handleMemberTerminated(
      String serviceName,
      String clusterId,
      String networkPartitionId,
      String partitionId,
      String memberId) {
    Topology topology = TopologyManager.getTopology();
    Service service = topology.getService(serviceName);
    Properties properties;
    if (service == null) {
      log.warn(String.format("Service %s does not exist", serviceName));
      return;
    }
    Cluster cluster = service.getCluster(clusterId);
    if (cluster == null) {
      log.warn(String.format("Cluster %s does not exist", clusterId));
      return;
    }

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

    String clusterInstanceId = member.getClusterInstanceId();

    try {
      TopologyManager.acquireWriteLock();
      properties = member.getProperties();
      cluster.removeMember(member);
      TopologyManager.updateTopology(topology);
    } finally {
      TopologyManager.releaseWriteLock();
    }
    /* @TODO leftover from grouping_poc*/
    String groupAlias = null;
    TopologyEventPublisher.sendMemberTerminatedEvent(
        serviceName,
        clusterId,
        memberId,
        clusterInstanceId,
        networkPartitionId,
        partitionId,
        properties,
        groupAlias);
  }
Ejemplo n.º 11
0
  /**
   * Add member object to the topology and publish member created event
   *
   * @param memberContext
   */
  public static void handleMemberCreatedEvent(MemberContext memberContext) {
    Topology topology = TopologyManager.getTopology();

    Service service = topology.getService(memberContext.getCartridgeType());
    String clusterId = memberContext.getClusterId();
    Cluster cluster = service.getCluster(clusterId);
    String memberId = memberContext.getMemberId();
    String clusterInstanceId = memberContext.getClusterInstanceId();
    String networkPartitionId = memberContext.getNetworkPartitionId();
    String partitionId = memberContext.getPartition().getId();
    String lbClusterId = memberContext.getLbClusterId();
    long initTime = memberContext.getInitTime();

    if (cluster.memberExists(memberId)) {
      log.warn(String.format("Member %s already exists", memberId));
      return;
    }

    try {
      TopologyManager.acquireWriteLock();
      Member member =
          new Member(
              service.getServiceName(),
              clusterId,
              memberId,
              clusterInstanceId,
              networkPartitionId,
              partitionId,
              memberContext.getLoadBalancingIPType(),
              initTime);
      member.setStatus(MemberStatus.Created);
      member.setLbClusterId(lbClusterId);
      member.setProperties(CloudControllerUtil.toJavaUtilProperties(memberContext.getProperties()));
      cluster.addMember(member);
      TopologyManager.updateTopology(topology);
    } finally {
      TopologyManager.releaseWriteLock();
    }

    TopologyEventPublisher.sendMemberCreatedEvent(memberContext);
  }
Ejemplo n.º 12
0
  public static void handleMemberStarted(InstanceStartedEvent instanceStartedEvent) {
    try {
      Topology topology = TopologyManager.getTopology();
      Service service = topology.getService(instanceStartedEvent.getServiceName());
      if (service == null) {
        log.warn(String.format("Service %s does not exist", instanceStartedEvent.getServiceName()));
        return;
      }
      if (!service.clusterExists(instanceStartedEvent.getClusterId())) {
        log.warn(
            String.format(
                "Cluster %s does not exist in service %s",
                instanceStartedEvent.getClusterId(), instanceStartedEvent.getServiceName()));
        return;
      }

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

      try {
        TopologyManager.acquireWriteLock();
        // try update lifecycle state
        if (!member.isStateTransitionValid(MemberStatus.Starting)) {
          log.error(
              "Invalid State Transition from "
                  + member.getStatus()
                  + " to "
                  + MemberStatus.Starting);
          return;
        } else {
          member.setStatus(MemberStatus.Starting);
          log.info("member started event adding status started");

          TopologyManager.updateTopology(topology);
          // memberStartedEvent.
          TopologyEventPublisher.sendMemberStartedEvent(instanceStartedEvent);
          // publishing data
          BAMUsageDataPublisher.publish(
              instanceStartedEvent.getMemberId(),
              instanceStartedEvent.getPartitionId(),
              instanceStartedEvent.getNetworkPartitionId(),
              instanceStartedEvent.getClusterId(),
              instanceStartedEvent.getServiceName(),
              MemberStatus.Starting.toString(),
              null);
        }
      } finally {
        TopologyManager.releaseWriteLock();
      }
    } catch (Exception e) {
      String message =
          String.format(
              "Could not handle member started event: [application-id] %s "
                  + "[service-name] %s [member-id] %s",
              instanceStartedEvent.getApplicationId(),
              instanceStartedEvent.getServiceName(),
              instanceStartedEvent.getMemberId());
      log.warn(message, e);
    }
  }
  @Override
  public boolean process(String type, String message, Object object) {
    Topology topology = (Topology) object;

    if (MemberTerminatedEvent.class.getName().equals(type)) {
      // Return if topology has not been initialized
      if (!topology.isInitialized()) return false;

      // Parse complete message and build event
      MemberTerminatedEvent event =
          (MemberTerminatedEvent) Util.jsonToObject(message, MemberTerminatedEvent.class);

      // Apply service filter
      if (TopologyServiceFilter.getInstance().isActive()) {
        if (TopologyServiceFilter.getInstance().serviceNameExcluded(event.getServiceName())) {
          // Service is excluded, do not update topology or fire event
          if (log.isDebugEnabled()) {
            log.debug(String.format("Service is excluded: [service] %s", event.getServiceName()));
          }
          return false;
        }
      }

      // Apply cluster filter
      if (TopologyClusterFilter.getInstance().isActive()) {
        if (TopologyClusterFilter.getInstance().clusterIdExcluded(event.getClusterId())) {
          // Cluster is excluded, do not update topology or fire event
          if (log.isDebugEnabled()) {
            log.debug(String.format("Cluster is excluded: [cluster] %s", event.getClusterId()));
          }
          return false;
        }
      }

      // Validate event against the existing topology
      Service service = topology.getService(event.getServiceName());
      if (service == null) {
        if (log.isWarnEnabled()) {
          log.warn(String.format("Service does not exist: [service] %s", event.getServiceName()));
        }
        return false;
      }
      Cluster cluster = service.getCluster(event.getClusterId());
      if (cluster == null) {
        if (log.isWarnEnabled()) {
          log.warn(
              String.format(
                  "Cluster does not exist: [service] %s [cluster] %s",
                  event.getServiceName(), event.getClusterId()));
        }
        return false;
      }
      Member member = cluster.getMember(event.getMemberId());
      if (member == null) {
        if (log.isWarnEnabled()) {
          log.warn(
              String.format(
                  "Member does not exist: [service] %s [cluster] %s [member] %s",
                  event.getServiceName(), event.getClusterId(), event.getMemberId()));
        }
        return false;
      }

      // Apply member filter
      if (TopologyMemberFilter.getInstance().isActive()) {
        if (TopologyMemberFilter.getInstance().lbClusterIdExcluded(member.getLbClusterId())) {
          if (log.isDebugEnabled()) {
            log.debug(
                String.format("Member is excluded: [lb-cluster-id] %s", member.getLbClusterId()));
          }
          return false;
        }
      }

      if (member.getStatus() == MemberStatus.Terminated) {
        if (log.isWarnEnabled()) {
          log.warn(
              String.format(
                  "Member already terminated: [service] %s [cluster] %s [member] %s",
                  event.getServiceName(), event.getClusterId(), event.getMemberId()));
        }
      } else {

        // Apply changes to the topology
        member.setStatus(MemberStatus.Terminated);
        // removing the member from the cluster
        cluster.removeMember(member);

        if (log.isInfoEnabled()) {
          log.info(
              String.format(
                  "Member terminated: [service] %s [cluster] %s [member] %s",
                  event.getServiceName(), event.getClusterId(), event.getMemberId()));
        }
      }

      notifyEventListeners(event);
      return true;

    } else {
      if (nextProcessor != null) {
        // ask the next processor to take care of the message.
        return nextProcessor.process(type, message, topology);
      } else {
        throw new RuntimeException(
            String.format(
                "Failed to process message using available message processors: [type] %s [body] %s",
                type, message));
      }
    }
  }
  private boolean doProcess(MemberMaintenanceModeEvent event, Topology topology) {

    String serviceName = event.getServiceName();
    String clusterId = event.getClusterId();

    // Apply service filter
    if (TopologyServiceFilter.apply(serviceName)) {
      return false;
    }

    // Apply cluster filter
    if (TopologyClusterFilter.apply(clusterId)) {
      return false;
    }

    // Validate event against the existing topology
    Service service = topology.getService(event.getServiceName());
    if (service == null) {
      if (log.isWarnEnabled()) {
        log.warn(String.format("Service does not exist: [service] %s", event.getServiceName()));
      }
      return false;
    }
    Cluster cluster = service.getCluster(event.getClusterId());
    if (cluster == null) {
      if (log.isWarnEnabled()) {
        log.warn(
            String.format(
                "Cluster does not exist: [service] %s [cluster] %s",
                event.getServiceName(), event.getClusterId()));
      }
      return false;
    }

    // Apply application filter
    if (TopologyApplicationFilter.apply(cluster.getAppId())) {
      return false;
    }

    Member member = cluster.getMember(event.getMemberId());
    if (member == null) {
      if (log.isWarnEnabled()) {
        log.warn(
            String.format(
                "Member does not exist: [service] %s [cluster] %s [member] %s",
                event.getServiceName(), event.getClusterId(), event.getMemberId()));
      }
      return false;
    }

    // Apply member filter
    if (TopologyMemberFilter.apply(member.getLbClusterId(), member.getNetworkPartitionId())) {
      return false;
    }

    if (member.getStatus() == MemberStatus.In_Maintenance) {
      if (log.isDebugEnabled()) {
        log.debug(
            String.format(
                "Member already updated as In_Maintenance: "
                    + "[service] %s [cluster] %s [member] %s",
                event.getServiceName(), event.getClusterId(), event.getMemberId()));
      }
    } else {

      // Apply changes to the topology
      if (!member.isStateTransitionValid(MemberStatus.In_Maintenance)) {
        log.error(
            "Invalid State Transition from "
                + member.getStatus()
                + " to "
                + MemberStatus.In_Maintenance);
      }
      member.setStatus(MemberStatus.In_Maintenance);

      if (log.isInfoEnabled()) {
        log.info(
            String.format(
                "Member updated as In_Maintenance: [service] %s [cluster] %s [member] %s",
                event.getServiceName(), event.getClusterId(), event.getMemberId()));
      }
    }

    // Notify event listeners
    notifyEventListeners(event);
    return true;
  }
Ejemplo n.º 15
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();
    }
  }
Ejemplo n.º 16
0
  public static void handleMemberReadyToShutdown(
      InstanceReadyToShutdownEvent instanceReadyToShutdownEvent)
      throws InvalidMemberException, InvalidCartridgeTypeException {
    Topology topology = TopologyManager.getTopology();
    Service service = topology.getService(instanceReadyToShutdownEvent.getServiceName());
    // update the status of the member
    if (service == null) {
      log.warn(
          String.format(
              "Service %s does not exist", instanceReadyToShutdownEvent.getServiceName()));
      return;
    }

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

    Member member = cluster.getMember(instanceReadyToShutdownEvent.getMemberId());
    if (member == null) {
      log.warn(
          String.format("Member %s does not exist", instanceReadyToShutdownEvent.getMemberId()));
      return;
    }
    MemberReadyToShutdownEvent memberReadyToShutdownEvent =
        new MemberReadyToShutdownEvent(
            instanceReadyToShutdownEvent.getServiceName(),
            instanceReadyToShutdownEvent.getClusterId(),
            instanceReadyToShutdownEvent.getClusterInstanceId(),
            instanceReadyToShutdownEvent.getMemberId(),
            instanceReadyToShutdownEvent.getNetworkPartitionId(),
            instanceReadyToShutdownEvent.getPartitionId());
    try {
      TopologyManager.acquireWriteLock();

      if (!member.isStateTransitionValid(MemberStatus.ReadyToShutDown)) {
        log.error(
            "Invalid State Transition from "
                + member.getStatus()
                + " to "
                + MemberStatus.ReadyToShutDown);
        return;
      }
      member.setStatus(MemberStatus.ReadyToShutDown);
      log.info("Member Ready to shut down event adding status started");

      TopologyManager.updateTopology(topology);
    } finally {
      TopologyManager.releaseWriteLock();
    }
    TopologyEventPublisher.sendMemberReadyToShutdownEvent(memberReadyToShutdownEvent);
    // publishing data
    BAMUsageDataPublisher.publish(
        instanceReadyToShutdownEvent.getMemberId(),
        instanceReadyToShutdownEvent.getPartitionId(),
        instanceReadyToShutdownEvent.getNetworkPartitionId(),
        instanceReadyToShutdownEvent.getClusterId(),
        instanceReadyToShutdownEvent.getServiceName(),
        MemberStatus.ReadyToShutDown.toString(),
        null);
    // termination of particular instance will be handled by autoscaler
  }
Ejemplo n.º 17
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();
    }
  }
Ejemplo n.º 18
0
  public static void handleServiceCreated(List<Cartridge> cartridgeList) {
    Service service;
    Topology topology = TopologyManager.getTopology();
    if (cartridgeList == null) {
      log.warn(String.format("Cartridge list is empty"));
      return;
    }

    try {

      TopologyManager.acquireWriteLock();
      for (Cartridge cartridge : cartridgeList) {
        if (!topology.serviceExists(cartridge.getType())) {
          ServiceType serviceType =
              cartridge.isMultiTenant() ? ServiceType.MultiTenant : ServiceType.SingleTenant;
          service = new Service(cartridge.getType(), serviceType);
          Properties properties = new Properties();

          try {
            Property[] propertyArray = null;

            if (cartridge.getProperties() != null) {
              if (cartridge.getProperties().getProperties() != null) {
                propertyArray = cartridge.getProperties().getProperties();
              }
            }

            List<Property> propertyList = new ArrayList<Property>();
            if (propertyArray != null) {
              propertyList = Arrays.asList(propertyArray);
              if (propertyList != null) {
                for (Property property : propertyList) {
                  properties.setProperty(property.getName(), property.getValue());
                }
              }
            }
          } catch (Exception e) {
            log.error(e);
          }

          service.setProperties(properties);
          if (cartridge.getPortMappings() != null) {
            List<PortMapping> portMappings = Arrays.asList(cartridge.getPortMappings());
            Port port;
            // adding ports to the event
            for (PortMapping portMapping : portMappings) {
              port =
                  new Port(
                      portMapping.getProtocol(), portMapping.getPort(), portMapping.getProxyPort());
              service.addPort(port);
            }
          }

          topology.addService(service);
          TopologyManager.updateTopology(topology);
        }
      }
    } finally {
      TopologyManager.releaseWriteLock();
    }
    TopologyEventPublisher.sendServiceCreateEvent(cartridgeList);
  }
Ejemplo n.º 19
0
  /**
   * Update member status to initialized and publish member initialized event
   *
   * @param memberContext
   */
  public static void handleMemberInitializedEvent(MemberContext memberContext) {
    Topology topology = TopologyManager.getTopology();
    Service service = topology.getService(memberContext.getCartridgeType());
    if (service == null) {
      log.warn(String.format("Service %s does not exist", memberContext.getCartridgeType()));
      return;
    }
    if (!service.clusterExists(memberContext.getClusterId())) {
      log.warn(
          String.format(
              "Cluster %s does not exist in service %s",
              memberContext.getClusterId(), memberContext.getCartridgeType()));
      return;
    }

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

    try {
      TopologyManager.acquireWriteLock();

      // Set ip addresses
      member.setDefaultPrivateIP(memberContext.getDefaultPrivateIP());
      if (memberContext.getPrivateIPs() != null) {
        member.setMemberPrivateIPs(Arrays.asList(memberContext.getPrivateIPs()));
      }
      member.setDefaultPublicIP(memberContext.getDefaultPublicIP());
      if (memberContext.getPublicIPs() != null) {
        member.setMemberPublicIPs(Arrays.asList(memberContext.getPublicIPs()));
      }

      // try update lifecycle state
      if (!member.isStateTransitionValid(MemberStatus.Initialized)) {
        log.error(
            "Invalid state transition from "
                + member.getStatus()
                + " to "
                + MemberStatus.Initialized);
        return;
      } else {
        member.setStatus(MemberStatus.Initialized);
        log.info("Member status updated to initialized");

        TopologyManager.updateTopology(topology);

        TopologyEventPublisher.sendMemberInitializedEvent(memberContext);
        // publishing data
        BAMUsageDataPublisher.publish(
            memberContext.getMemberId(),
            memberContext.getPartition().getId(),
            memberContext.getNetworkPartitionId(),
            memberContext.getClusterId(),
            memberContext.getCartridgeType(),
            MemberStatus.Initialized.toString(),
            null);
      }
    } finally {
      TopologyManager.releaseWriteLock();
    }
  }