Example #1
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);
  }
Example #2
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
  }
Example #3
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);
    }
  }
Example #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();
    }
  }
Example #5
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();
    }
  }