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