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); }
/** thread's run() method that does all the work in the right order */ public void run() { try { if (log != null) log.println( "-------------------\ncartridge " + gwcfile.name + " started (openWIG r" + VERSION + ")\n-------------------"); prepareState(); if (doRestore) restoreGame(); else newGame(); loglevel = LOG_PROP; ui.debugMsg("Starting game...\n"); ui.start(); player.refreshLocation(); cartridge.callEvent(doRestore ? "OnRestore" : "OnStart", null); ui.refresh(); eventRunner.unpause(); mainloop(); } catch (IOException e) { ui.showError("Could not load cartridge: " + e.getMessage()); } catch (Throwable t) { stacktrace(t); } finally { ui.end(); } }
/** main loop - periodically copy location data into Lua and evaluate zone positions */ private void mainloop() { try { while (!end) { try { if (gps.getLatitude() != player.position.latitude || gps.getLongitude() != player.position.longitude || gps.getAltitude() != player.position.altitude) { player.refreshLocation(); } cartridge.tick(); } catch (Exception e) { stacktrace(e); } try { Thread.sleep(1000); } catch (InterruptedException e) { } } if (log != null) log.close(); } catch (Throwable t) { ui.end(); stacktrace(t); } finally { instance = null; state = null; if (eventRunner != null) eventRunner.kill(); eventRunner = null; } }
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"); } } }
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 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); }