Return-Path: X-Original-To: apmail-accumulo-commits-archive@www.apache.org Delivered-To: apmail-accumulo-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 4FEA7101BB for ; Tue, 22 Oct 2013 19:43:29 +0000 (UTC) Received: (qmail 68787 invoked by uid 500); 22 Oct 2013 19:43:26 -0000 Delivered-To: apmail-accumulo-commits-archive@accumulo.apache.org Received: (qmail 68626 invoked by uid 500); 22 Oct 2013 19:43:20 -0000 Mailing-List: contact commits-help@accumulo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@accumulo.apache.org Delivered-To: mailing list commits@accumulo.apache.org Received: (qmail 68475 invoked by uid 99); 22 Oct 2013 19:43:16 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 22 Oct 2013 19:43:16 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 0AD64540B2; Tue, 22 Oct 2013 19:43:16 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: ecn@apache.org To: commits@accumulo.apache.org Date: Tue, 22 Oct 2013 19:43:19 -0000 Message-Id: <5397a10ec57142e09082b336f6847064@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [5/6] git commit: Merge branch '1.4.5-SNAPSHOT' into 1.5.1-SNAPSHOT Merge branch '1.4.5-SNAPSHOT' into 1.5.1-SNAPSHOT Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/b7786fcc Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/b7786fcc Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/b7786fcc Branch: refs/heads/1.5.1-SNAPSHOT Commit: b7786fccbe4876f2b9713e118d1edc04b2b15a86 Parents: 0fe0a5a f975926 Author: Eric Newton Authored: Tue Oct 22 15:42:48 2013 -0400 Committer: Eric Newton Committed: Tue Oct 22 15:42:48 2013 -0400 ---------------------------------------------------------------------- .../apache/accumulo/server/metrics/MetricsConfiguration.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/accumulo/blob/b7786fcc/server/src/main/java/org/apache/accumulo/server/metrics/MetricsConfiguration.java ---------------------------------------------------------------------- diff --cc server/src/main/java/org/apache/accumulo/server/metrics/MetricsConfiguration.java index 2d462e9,0000000..446a548 mode 100644,000000..100644 --- a/server/src/main/java/org/apache/accumulo/server/metrics/MetricsConfiguration.java +++ b/server/src/main/java/org/apache/accumulo/server/metrics/MetricsConfiguration.java @@@ -1,232 -1,0 +1,232 @@@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.accumulo.server.metrics; + +import java.io.File; +import java.util.Iterator; + +import org.apache.accumulo.core.util.Daemon; +import org.apache.commons.configuration.AbstractFileConfiguration; +import org.apache.commons.configuration.Configuration; +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.EnvironmentConfiguration; +import org.apache.commons.configuration.SystemConfiguration; +import org.apache.commons.configuration.XMLConfiguration; +import org.apache.commons.configuration.event.ConfigurationEvent; +import org.apache.commons.configuration.event.ConfigurationListener; +import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; +import org.apache.commons.lang.builder.ToStringBuilder; + +public class MetricsConfiguration { + + private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(MetricsConfiguration.class); + - private static final String metricsFileName = "conf/accumulo-metrics.xml"; ++ private static final String metricsFileName = "accumulo-metrics.xml"; + + private static boolean alreadyWarned = false; + + private boolean notFound = false; + + private int notFoundCount = 0; + + private static SystemConfiguration sysConfig = null; + + private static EnvironmentConfiguration envConfig = null; + + private XMLConfiguration xConfig = null; + + private Configuration config = null; + + private final Object lock = new Object(); + + private boolean needsReloading = false; + + private long lastCheckTime = 0; + + private static long CONFIG_FILE_CHECK_INTERVAL = 1000 * 60 * 10; // 10 minutes + + private static int CONFIG_FILE_CHECK_COUNTER = 100; + + public final static long CONFIG_FILE_RELOAD_DELAY = 60000; + + private MetricsConfigWatcher watcher = null; + + private boolean enabled = false; + + private String enabledName = null; + + /** + * Background thread that pokes the XMLConfiguration file to see if it has changed. If it has, then the Configuration Listener will get an event. + * + */ + private class MetricsConfigWatcher extends Daemon { + public MetricsConfigWatcher() {} + + public void run() { + while (this.isAlive()) { + try { + Thread.sleep(MetricsConfiguration.CONFIG_FILE_RELOAD_DELAY); + } catch (InterruptedException ie) { + // Do Nothing + } + xConfig.getBoolean("master.enabled"); + } + } + } + + /** + * ConfigurationListener that sets a flag to reload the XML config file + */ + private class MetricsConfigListener implements ConfigurationListener { + public void configurationChanged(ConfigurationEvent event) { + if (event.getType() == AbstractFileConfiguration.EVENT_RELOAD) + needsReloading = true; + } + } + + public MetricsConfiguration(String name) { + // We are going to store the "enabled" parameter for this + // name as a shortcut so that it doesn't have to be looked + // up in the configuration so much. + this.enabledName = name + ".enabled"; + getMetricsConfiguration(); + } + + public Configuration getEnvironmentConfiguration() { + if (null == envConfig) + envConfig = new EnvironmentConfiguration(); + return envConfig; + } + + public Configuration getSystemConfiguration() { + if (null == sysConfig) + sysConfig = new SystemConfiguration(); + return sysConfig; + } + + public Configuration getMetricsConfiguration() { + if (notFound) { + if (notFoundCount <= CONFIG_FILE_CHECK_COUNTER) { + return null; + } else if ((notFoundCount > CONFIG_FILE_CHECK_COUNTER) && ((System.currentTimeMillis() - lastCheckTime) > CONFIG_FILE_CHECK_INTERVAL)) { + notFoundCount = 0; + lastCheckTime = System.currentTimeMillis(); + notFound = false; + } else { + notFoundCount++; + } + } + if (null == config || needsReloading) + synchronized (lock) { + if (needsReloading) { + loadConfiguration(); + } else if (null == config) { + loadConfiguration(); + } + needsReloading = false; + } + return config; + } + + private void loadConfiguration() { + // Check to see if ACCUMULO_HOME environment variable is set. - String ACUHOME = getEnvironmentConfiguration().getString("ACCUMULO_HOME"); ++ String ACUHOME = getEnvironmentConfiguration().getString("ACCUMULO_CONF_DIR"); + if (null != ACUHOME) { + // Try to load the metrics properties file + File mFile = new File(ACUHOME, metricsFileName); + if (mFile.exists()) { + if (log.isDebugEnabled()) + log.debug("Loading config file: " + mFile.getAbsolutePath()); + try { + xConfig = new XMLConfiguration(mFile); + xConfig.append(getEnvironmentConfiguration()); + xConfig.addConfigurationListener(new MetricsConfigListener()); + xConfig.setReloadingStrategy(new FileChangedReloadingStrategy()); + + // Start a background Thread that checks a property from the XMLConfiguration + // every so often to force the FileChangedReloadingStrategy to fire. + if (null == watcher || !watcher.isAlive()) { + watcher = new MetricsConfigWatcher(); + watcher.start(); + } + notFound = false; + alreadyWarned = false; + } catch (ConfigurationException ce) { + log.error("Error reading accumulo-metrics.xml file."); + notFound = true; + return; + } + } else { + if (!alreadyWarned) + log.warn("Unable to find metrics file: " + mFile.getAbsolutePath()); + alreadyWarned = true; + notFound = true; + return; + } + } else { + if (!alreadyWarned) - log.warn("ACCUMULO_HOME variable not found in environment. Metrics collection will be disabled."); ++ log.warn("ACCUMULO_CONF_DIR variable not found in environment. Metrics collection will be disabled."); + alreadyWarned = true; + notFound = true; + return; + } + if (xConfig != null) { + config = xConfig.interpolatedConfiguration(); + // set the enabled boolean from the configuration + enabled = config.getBoolean(enabledName); + if (log.isDebugEnabled()) + log.debug("Metrics collection enabled=" + enabled); + } else { + enabled = false; + } + + } + + public boolean isEnabled() { + // Force reload if necessary + if (null == getMetricsConfiguration()) + return false; + return enabled; + } + + public static String toStringValue(Configuration config) { + ToStringBuilder tsb = new ToStringBuilder(MetricsConfiguration.class); + Iterator keys = config.getKeys(); + while (keys.hasNext()) { + tsb.append("\n"); + String k = (String) keys.next(); + Object v = config.getString(k); + if (null == v) + v = config.getList(k); + tsb.append(k, v.toString()); + } + return tsb.toString(); + } + + public static void main(String[] args) throws Exception { + MetricsConfiguration mc = new MetricsConfiguration("master"); + while (true) { + // System.out.println(MetricsConfiguration.toStringValue(getSystemConfiguration())); + System.out.println("------------------------------------------------------------------------------------------------"); + // System.out.println(MetricsConfiguration.toStringValue()); + long t1 = System.currentTimeMillis(); + System.out.println(mc.isEnabled() + " took: " + (System.currentTimeMillis() - t1)); + Thread.sleep(1000); + } + } + +}