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(); } }
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); }
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(); } }
/** * 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); }
/** * 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); }
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(); } }
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 }
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(); } }
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); } }
/** * 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(); } }