@Override public String getId(String alias) throws Exception { Node node = clusterManager.findNodeByAlias(alias); if (node != null) { return node.getId(); } return null; }
@Override protected Object doExecute() throws Exception { if (idLookup != null) { Node node = clusterManager.findNodeById(idLookup); System.out.println(node.getAlias()); return null; } if (aliasLookup != null) { Node node = clusterManager.findNodeByAlias(aliasLookup); System.out.println(node.getId()); return null; } if (alias != null) { if (clusterManager.findNodeByAlias(alias) != null) { System.err.println("Alias " + alias + " already exists"); return null; } clusterManager.setNodeAlias(alias); } else { Node node = clusterManager.getNode(); if (node.getAlias() == null) { System.out.println(""); } else { System.out.println(node.getAlias()); } } return null; }
@Override public TabularData getNodes() throws Exception { CompositeType nodeType = new CompositeType("Node", "Karaf Cellar cluster node", new String[]{ "id", "alias", "hostname", "port", "local" }, new String[]{ "ID of the node", "Alias of the node", "Hostname of the node", "Port number of the node", "Flag defining if the node is local" }, new OpenType[]{ SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.INTEGER, SimpleType.BOOLEAN }); TabularType tableType = new TabularType("Nodes", "Table of all Karaf Cellar nodes", nodeType, new String[]{ "id" }); TabularData table = new TabularDataSupport(tableType); Set<Node> nodes = clusterManager.listNodes(); for (Node node : nodes) { boolean local = (node.equals(clusterManager.getNode())); CompositeData data = new CompositeDataSupport(nodeType, new String[]{ "id", "alias", "hostname", "port", "local" }, new Object[]{ node.getId(), node.getAlias(), node.getHost(), node.getPort(), local }); table.put(data); } return table; }
@Override public String getAlias(String id) throws Exception { Node node = clusterManager.findNodeById(id); if (node != null) { return node.getAlias(); } return null; }
public String constructLocation(String alias) { String httpHost = clusterManager.getNode().getHost(); String httpPort = null; try { Configuration configuration = configurationAdmin.getConfiguration("org.ops4j.pax.web", null); if (configuration != null) { Dictionary properties = configuration.getProperties(); if (properties != null) { httpPort = (String) properties.get("org.osgi.service.http.port"); } } } catch (Exception e) { LOGGER.warn("CELLAR HTTP BALANCER: can't get HTTP port number from configuration", e); } if (httpPort == null) httpPort = "8181"; String location = "http://" + httpHost + ":" + httpPort + alias; return location; }
@Override public String getAlias(String id) throws Exception { Node node = clusterManager.findNodeById(id); if (node != null) { return node.getAlias(); } return null; }
@Override public String getId(String alias) throws Exception { Node node = clusterManager.findNodeByAlias(alias); if (node != null) { return node.getId(); } return null; }
@Override public TabularData getGroups() throws Exception { Set<Group> allGroups = groupManager.listAllGroups(); CompositeType groupType = new CompositeType("Group", "Karaf Cellar cluster group", new String[]{ "name", "members"}, new String[]{ "Name of the cluster group", "Members of the cluster group" }, new OpenType[]{ SimpleType.STRING, SimpleType.STRING }); TabularType tableType = new TabularType("Groups", "Table of all Karaf Cellar groups", groupType, new String[]{ "name" }); TabularData table = new TabularDataSupport(tableType); for (Group group : allGroups) { StringBuffer members = new StringBuffer(); for (Node node : group.getNodes()) { // display only up and running nodes in the cluster if (clusterManager.findNodeById(node.getId()) != null) { if (node.getAlias() != null) { members.append(node.getAlias()); } else { members.append(node.getId()); } members.append(" "); } } CompositeData data = new CompositeDataSupport(groupType, new String[]{ "name", "members" }, new Object[]{ group.getName(), members.toString() }); table.put(data); } return table; }
@Override public TabularData getNodes() throws Exception { CompositeType nodeType = new CompositeType("Node", "Karaf Cellar cluster node", new String[]{ "id", "alias", "hostname", "port", "local" }, new String[]{ "ID of the node", "Alias of the node", "Hostname of the node", "Port number of the node", "Flag defining if the node is local" }, new OpenType[]{ SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.INTEGER, SimpleType.BOOLEAN }); TabularType tableType = new TabularType("Nodes", "Table of all Karaf Cellar nodes", nodeType, new String[]{ "id" }); TabularData table = new TabularDataSupport(tableType); Set<Node> nodes = clusterManager.listNodes(); for (Node node : nodes) { boolean local = (node.equals(clusterManager.getNode())); CompositeData data = new CompositeDataSupport(nodeType, new String[]{ "id", "alias", "hostname", "port", "local" }, new Object[]{ node.getId(), node.getAlias(), node.getHost(), node.getPort(), local }); table.put(data); } return table; }
public Map<String, List<String>> getServices() { Map<String, List<String>> services = new HashMap<String, List<String>>(); ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); Map<String, EndpointDescription> remoteEndpoints = clusterManager.getMap(Constants.REMOTE_ENDPOINTS); if (remoteEndpoints != null && !remoteEndpoints.isEmpty()) { for (Map.Entry<String, EndpointDescription> entry : remoteEndpoints.entrySet()) { EndpointDescription endpointDescription = entry.getValue(); String serviceClass = endpointDescription.getServiceClass(); Set<Node> nodes = endpointDescription.getNodes(); LinkedList<String> providers = new LinkedList<String>(); for (Node node : nodes) { providers.add(node.getId()); } services.put(serviceClass, providers); } } } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } return services; }
@Override public TabularData getGroups() throws Exception { Set<Group> allGroups = groupManager.listAllGroups(); CompositeType groupType = new CompositeType("Group", "Karaf Cellar cluster group", new String[]{ "name", "members"}, new String[]{ "Name of the cluster group", "Members of the cluster group" }, new OpenType[]{ SimpleType.STRING, SimpleType.STRING }); TabularType tableType = new TabularType("Groups", "Table of all Karaf Cellar groups", groupType, new String[]{ "name" }); TabularData table = new TabularDataSupport(tableType); for (Group group : allGroups) { StringBuffer members = new StringBuffer(); for (Node node : group.getNodes()) { // display only up and running nodes in the cluster if (clusterManager.findNodeById(node.getId()) != null) { if (node.getAlias() != null) { members.append(node.getAlias()); } else { members.append(node.getId()); } members.append(" "); } } CompositeData data = new CompositeDataSupport(groupType, new String[]{ "name", "members" }, new Object[]{ group.getName(), members.toString() }); table.put(data); } return table; }
@Override protected Object doExecute() throws Exception { Set<Node> nodes = clusterManager.listNodes(); if (nodes != null && !nodes.isEmpty()) { ShellTable table = new ShellTable(); table.column(" "); table.column("Id"); table.column("Alias"); table.column("Host Name"); table.column("Port"); for (Node node : nodes) { String local = ""; if (node.equals(clusterManager.getNode())) local = "x"; table.addRow().addContent(local, node.getId(), node.getAlias(), node.getHost(), node.getPort()); } table.print(System.out); } else { System.err.println("No node found in the cluster"); } return null; }
@Override public Object doExecute() throws Exception { ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); try { Map<ClusterLogKey, ClusterLogRecord> clusterLog = clusterManager.getMap(LogAppender.LOG_MAP); if (nodeIdOrAlias == null) { clusterLog.clear(); } else { Node node = clusterManager.findNodeByIdOrAlias(nodeIdOrAlias); if (node == null) { System.err.println("Node " + nodeIdOrAlias + " doesn't exist"); return null; } for (ClusterLogKey key : clusterLog.keySet()) { if (key.getNodeId().equals(node.getId())) { clusterLog.remove(key); } } } } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } return null; }
/** * Check if there is a match for the current {@link ListenerInfo}. * * @param listenerInfo the listener info. */ private void checkListener(ListenerInfo listenerInfo) { ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); // iterate through known services and import them if needed Set<EndpointDescription> matches = new LinkedHashSet<EndpointDescription>(); for (Map.Entry<String, EndpointDescription> entry : remoteEndpoints.entrySet()) { EndpointDescription endpointDescription = entry.getValue(); if (endpointDescription.matches(listenerInfo.getFilter()) && !endpointDescription.getNodes().contains(clusterManager.getNode().getId())) { matches.add(endpointDescription); } } for (EndpointDescription endpoint : matches) { importService(endpoint, listenerInfo); } } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } }
@Override public void doStart() throws Exception { ClusterManager clusterManager = getTrackedService(ClusterManager.class); if (clusterManager == null) return; String nodeId = clusterManager.getNode().getId(); GreeterImpl greeter = new GreeterImpl(nodeId); Hashtable props = new Hashtable(); props.put("service.exported.interfaces", "*"); register(Greeter.class, greeter, props); }
@Override public TabularData producerStatus() throws Exception { ProducerSwitchCommand command = new ProducerSwitchCommand(clusterManager.generateId()); command.setStatus(null); Map<Node, ProducerSwitchResult> results = executionContext.execute(command); CompositeType compositeType = new CompositeType("Event Producer", "Karaf Cellar cluster event producer", new String[]{"node", "status", "local"}, new String[]{"Node hosting event producer", "Current status of the event producer", "True if the node is local"}, new OpenType[]{SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN}); TabularType tableType = new TabularType("Event Producers", "Table of Karaf Cellar cluster event producers", compositeType, new String[]{"node"}); TabularDataSupport table = new TabularDataSupport(tableType); for (Node node : results.keySet()) { boolean local = (node.equals(clusterManager.getNode())); ProducerSwitchResult producerSwitchResult = results.get(node); String nodeName = node.getAlias(); if (nodeName == null) { nodeName = node.getId(); } CompositeDataSupport data = new CompositeDataSupport(compositeType, new String[]{"node", "status", "local"}, new Object[]{nodeName, producerSwitchResult.getStatus(), local}); table.put(data); } return table; }
private void updateSystemStats() { final RuntimeMXBean remoteRuntime = ManagementFactory.getRuntimeMXBean(); long uptime = remoteRuntime.getUptime(); ObjectName operatingSystemMXBeanName = ManagementFactory.getOperatingSystemMXBean().getObjectName(); Double systemCpuLoad = null; try { systemCpuLoad = (Double) ManagementFactory.getPlatformMBeanServer().getAttribute(operatingSystemMXBeanName, "SystemCpuLoad"); } catch (MBeanException e) { logger.error("Error retrieving system CPU load", e); } catch (AttributeNotFoundException e) { logger.error("Error retrieving system CPU load", e); } catch (InstanceNotFoundException e) { logger.error("Error retrieving system CPU load", e); } catch (ReflectionException e) { logger.error("Error retrieving system CPU load", e); } final OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean(); double systemLoadAverage = operatingSystemMXBean.getSystemLoadAverage(); ClusterSystemStatisticsEvent clusterSystemStatisticsEvent = new ClusterSystemStatisticsEvent("org.apache.unomi.cluster.system.statistics"); Map<String,Serializable> systemStatistics = new TreeMap<>(); ArrayList<Double> systemLoadAverageArray = new ArrayList<>(); systemLoadAverageArray.add(systemLoadAverage); systemStatistics.put("systemLoadAverage", systemLoadAverageArray); systemStatistics.put("systemCpuLoad", systemCpuLoad); systemStatistics.put("uptime", uptime); clusterSystemStatisticsEvent.setStatistics(systemStatistics); nodeSystemStatistics.put(karafCellarClusterManager.getNode().getId(), systemStatistics); sendEvent(clusterSystemStatisticsEvent); }
@Override public TabularData consumerStatus() throws Exception { ConsumerSwitchCommand command = new ConsumerSwitchCommand(clusterManager.generateId()); command.setStatus(null); Map<Node, ConsumerSwitchResult> results = executionContext.execute(command); CompositeType compositeType = new CompositeType("Event Consumer", "Karaf Cellar cluster event consumer", new String[]{"node", "status", "local"}, new String[]{"Node hosting event consumer", "Current status of the event consumer", "True if the node is local"}, new OpenType[]{SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.BOOLEAN}); TabularType tableType = new TabularType("Event Consumers", "Table of Karaf Cellar cluster event consumers", compositeType, new String[]{"node"}); TabularDataSupport table = new TabularDataSupport(tableType); for (Node node : results.keySet()) { boolean local = (node.equals(clusterManager.getNode())); ConsumerSwitchResult consumerSwitchResult = results.get(node); String nodeName = node.getAlias(); if (nodeName == null) { nodeName = node.getId(); } CompositeDataSupport data = new CompositeDataSupport(compositeType, new String[]{"node", "status", "local"}, new Object[]{nodeName, consumerSwitchResult.getStatus(), local}); table.put(data); } return table; }