@Override protected void run() { while (isRunning()) { try { collectOperationalStats(); if (!isRunning()) { // Need to check here before sleep as the collectOperationStats may swallow interrupted exception break; } TimeUnit.SECONDS.sleep(statsRefreshInterval); } catch (InterruptedException e) { // Expected on stopping. So just break the loop break; } } }
@Override public Set<OperationalExtensionId> getSupportedTypesForProvider(OperationalStats operationalStats) { OperationalExtensionId operationalExtensionId = OperationalStatsUtils.getOperationalExtensionId(operationalStats); return operationalExtensionId == null ? Collections.<OperationalExtensionId>emptySet() : Collections.singleton(operationalExtensionId); } }
@Nullable private ObjectName getObjectName(OperationalStats operationalStats) { OperationalExtensionId operationalExtensionId = OperationalStatsUtils.getOperationalExtensionId(operationalStats); if (operationalExtensionId == null) { return null; } Hashtable<String, String> properties = new Hashtable<>(); properties.put(OperationalStatsUtils.SERVICE_NAME_KEY, operationalExtensionId.getServiceName()); properties.put(OperationalStatsUtils.STAT_TYPE_KEY, operationalExtensionId.getStatType()); try { return new ObjectName(OperationalStatsUtils.JMX_DOMAIN, properties); } catch (MalformedObjectNameException e) { // should never happen, since we're constructing a valid domain name, and properties is non-empty throw Throwables.propagate(e); } } }
/** * Collects stats from all {@link OperationalStats}. */ private void collectOperationalStats() throws InterruptedException { LOG.trace("Running operational stats extension service iteration"); for (Map.Entry<OperationalExtensionId, OperationalStats> entry : operationalStatsLoader.getAll().entrySet()) { if (!isRunning()) { return; } OperationalStats stats = entry.getValue(); LOG.trace("Collecting stats for service {} of type {}", stats.getServiceName(), stats.getStatType()); try { stats.collect(); } catch (Throwable t) { Throwables.propagateIfInstanceOf(t, InterruptedException.class); Throwable rootCause = Throwables.getRootCause(t); if (rootCause instanceof ServiceUnavailableException || rootCause instanceof TException) { // Required service (for example DatasetService in case of ServiceUnavailableException // or Transaction Service in case of TException) is not running yet. // Return without logging. return; } if (rootCause instanceof InterruptedException) { throw (InterruptedException) rootCause; } READ_FAILURE_LOG.warn("Failed to collect stats for service {} of type {} due to {}", stats.getServiceName(), stats.getStatType(), rootCause.getMessage()); } } }
@Override protected void shutDown() throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); for (Map.Entry<OperationalExtensionId, OperationalStats> entry : operationalStatsLoader.getAll().entrySet()) { OperationalStats operationalStats = entry.getValue(); ObjectName objectName = getObjectName(operationalStats); if (objectName == null) { LOG.warn("Found an operational extension with null service name and stat type while unregistering - {}. " + "Ignoring this extension.", operationalStats.getClass().getName()); continue; } try { mbs.unregisterMBean(objectName); } catch (InstanceNotFoundException e) { LOG.warn("MBean {} not found while un-registering. Ignoring.", objectName); } catch (MBeanRegistrationException e) { LOG.warn("Error while un-registering MBean {}.", e); } operationalStats.destroy(); } LOG.info("Successfully shutdown operational stats service."); }
/** * Registers all JMX {@link MXBean MXBeans} from {@link OperationalStats} extensions in the extensions directory. */ @Override protected void startUp() throws Exception { runThread = Thread.currentThread(); MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); for (Map.Entry<OperationalExtensionId, OperationalStats> entry : operationalStatsLoader.getAll().entrySet()) { OperationalStats operationalStats = entry.getValue(); ObjectName objectName = getObjectName(operationalStats); if (objectName == null) { LOG.warn("Found an operational extension with null service name and stat type - {}. Ignoring this extension.", OperationalStats.class.getName()); continue; } LOG.debug("Registering operational extension: {}; extension id: {}", operationalStats, entry.getKey()); // initialize operational stats operationalStats.initialize(injector); // register MBean mbs.registerMBean(operationalStats, objectName); } LOG.info("Successfully started Operational Stats Service..."); }
@Nullable static OperationalExtensionId getOperationalExtensionId(OperationalStats operationalStats) { String serviceName = operationalStats.getServiceName(); String statType = operationalStats.getStatType(); if (Strings.isNullOrEmpty(serviceName) && Strings.isNullOrEmpty(statType)) { return null; } if (!Strings.isNullOrEmpty(serviceName)) { serviceName = serviceName.toLowerCase(); } else { LOG.warn("Found operational stat without service name - {}. This stat will not be discovered by service name.", operationalStats.getClass().getName()); } if (!Strings.isNullOrEmpty(statType)) { statType = statType.toLowerCase(); } else { LOG.warn("Found operational stat without stat type - {}. This stat will not be discovered by stat type.", operationalStats.getClass().getName()); } return new OperationalExtensionId(serviceName, statType); }
@Inject OperationalStatsService(CConfiguration cConf, Injector injector) { this.operationalStatsLoader = new OperationalStatsLoader(cConf); this.statsRefreshInterval = cConf.getLong(Constants.OperationalStats.REFRESH_INTERVAL_SECS); this.injector = injector; }
operationalStatsService.startAndWait();
operationalStatsService.stopAndWait();
/** * Collects stats from all {@link OperationalStats}. */ private void collectOperationalStats() throws InterruptedException { LOG.trace("Running operational stats extension service iteration"); for (Map.Entry<OperationalExtensionId, OperationalStats> entry : operationalStatsLoader.getAll().entrySet()) { if (!isRunning()) { return; } OperationalStats stats = entry.getValue(); LOG.trace("Collecting stats for service {} of type {}", stats.getServiceName(), stats.getStatType()); try { stats.collect(); } catch (Throwable t) { Throwables.propagateIfInstanceOf(t, InterruptedException.class); Throwable rootCause = Throwables.getRootCause(t); if (rootCause instanceof ServiceUnavailableException || rootCause instanceof TException) { // Required service (for example DatasetService in case of ServiceUnavailableException // or Transaction Service in case of TException) is not running yet. // Return without logging. return; } if (rootCause instanceof InterruptedException) { throw (InterruptedException) rootCause; } READ_FAILURE_LOG.warn("Failed to collect stats for service {} of type {} due to {}", stats.getServiceName(), stats.getStatType(), rootCause.getMessage()); } } }
@Nullable private ObjectName getObjectName(OperationalStats operationalStats) { OperationalExtensionId operationalExtensionId = OperationalStatsUtils.getOperationalExtensionId(operationalStats); if (operationalExtensionId == null) { return null; } Hashtable<String, String> properties = new Hashtable<>(); properties.put(OperationalStatsUtils.SERVICE_NAME_KEY, operationalExtensionId.getServiceName()); properties.put(OperationalStatsUtils.STAT_TYPE_KEY, operationalExtensionId.getStatType()); try { return new ObjectName(OperationalStatsUtils.JMX_DOMAIN, properties); } catch (MalformedObjectNameException e) { // should never happen, since we're constructing a valid domain name, and properties is non-empty throw Throwables.propagate(e); } } }
@Override protected void shutDown() throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); for (Map.Entry<OperationalExtensionId, OperationalStats> entry : operationalStatsLoader.getAll().entrySet()) { OperationalStats operationalStats = entry.getValue(); ObjectName objectName = getObjectName(operationalStats); if (objectName == null) { LOG.warn("Found an operational extension with null service name and stat type while unregistering - {}. " + "Ignoring this extension.", operationalStats.getClass().getName()); continue; } try { mbs.unregisterMBean(objectName); } catch (InstanceNotFoundException e) { LOG.warn("MBean {} not found while un-registering. Ignoring.", objectName); } catch (MBeanRegistrationException e) { LOG.warn("Error while un-registering MBean {}.", e); } operationalStats.destroy(); } LOG.info("Successfully shutdown operational stats service."); }
/** * Registers all JMX {@link MXBean MXBeans} from {@link OperationalStats} extensions in the extensions directory. */ @Override protected void startUp() throws Exception { runThread = Thread.currentThread(); MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); for (Map.Entry<OperationalExtensionId, OperationalStats> entry : operationalStatsLoader.getAll().entrySet()) { OperationalStats operationalStats = entry.getValue(); ObjectName objectName = getObjectName(operationalStats); if (objectName == null) { LOG.warn("Found an operational extension with null service name and stat type - {}. Ignoring this extension.", OperationalStats.class.getName()); continue; } LOG.debug("Registering operational extension: {}; extension id: {}", operationalStats, entry.getKey()); // initialize operational stats operationalStats.initialize(injector); // register MBean mbs.registerMBean(operationalStats, objectName); } LOG.info("Successfully started Operational Stats Service..."); }
@Nullable static OperationalExtensionId getOperationalExtensionId(OperationalStats operationalStats) { String serviceName = operationalStats.getServiceName(); String statType = operationalStats.getStatType(); if (Strings.isNullOrEmpty(serviceName) && Strings.isNullOrEmpty(statType)) { return null; } if (!Strings.isNullOrEmpty(serviceName)) { serviceName = serviceName.toLowerCase(); } else { LOG.warn("Found operational stat without service name - {}. This stat will not be discovered by service name.", operationalStats.getClass().getName()); } if (!Strings.isNullOrEmpty(statType)) { statType = statType.toLowerCase(); } else { LOG.warn("Found operational stat without stat type - {}. This stat will not be discovered by stat type.", operationalStats.getClass().getName()); } return new OperationalExtensionId(serviceName, statType); }
@Override protected void run() { while (isRunning()) { try { collectOperationalStats(); if (!isRunning()) { // Need to check here before sleep as the collectOperationStats may swallow interrupted exception break; } TimeUnit.SECONDS.sleep(statsRefreshInterval); } catch (InterruptedException e) { // Expected on stopping. So just break the loop break; } } }
@Override public Set<OperationalExtensionId> getSupportedTypesForProvider(OperationalStats operationalStats) { OperationalExtensionId operationalExtensionId = OperationalStatsUtils.getOperationalExtensionId(operationalStats); return operationalExtensionId == null ? Collections.<OperationalExtensionId>emptySet() : Collections.singleton(operationalExtensionId); } }
@Inject OperationalStatsService(CConfiguration cConf, Injector injector) { this.operationalStatsLoader = new OperationalStatsLoader(cConf); this.statsRefreshInterval = cConf.getLong(Constants.OperationalStats.REFRESH_INTERVAL_SECS); this.injector = injector; }
operationalStatsService.startAndWait();
operationalStatsService.stopAndWait();