Index: hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerMetrics2.java =================================================================== --- hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerMetrics2.java (revision 0) +++ hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerMetrics2.java (revision 0) @@ -0,0 +1,101 @@ +/** + * 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.hadoop.hbase.regionserver; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.CompatibilityFactory; +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.MediumTests; +import org.apache.hadoop.hbase.MiniHBaseCluster; +import org.apache.hadoop.hbase.test.MetricsAssertHelper; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Threads; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.junit.After; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(MediumTests.class) +public class TestRegionServerMetrics2 { + private static final Log LOG = LogFactory.getLog(TestRegionServerMetrics2.class); + private static final MetricsAssertHelper metricsHelper = CompatibilityFactory + .getInstance(MetricsAssertHelper.class); + + static { + Logger.getLogger("org.apache.hadoop.hbase").setLevel(Level.DEBUG); + } + + private MiniHBaseCluster cluster; + private HRegionServer rs; + private Configuration conf; + private HBaseTestingUtility TEST_UTIL; + + private void startCluster() throws Exception{ + LOG.info("Starting cluster"); + conf = HBaseConfiguration.create(); + conf.getLong("hbase.splitlog.max.resubmit", 0); + // Make the failure test faster + conf.setInt("zookeeper.recovery.retry", 0); + conf.setInt(HConstants.REGIONSERVER_INFO_PORT, -1); + conf.setFloat(HConstants.LOAD_BALANCER_SLOP_KEY, (float) 100.0); // no load balancing + conf.setBoolean(HConstants.DISTRIBUTED_LOG_SPLITTING_KEY, true); + // Make it report metrics every 1 second. NOTE: unit-test logic depends on this + conf.setLong("hbase.regionserver.msginterval", 1000L); + TEST_UTIL = new HBaseTestingUtility(conf); + TEST_UTIL.startMiniCluster(1, 1); + cluster = TEST_UTIL.getHBaseCluster(); + LOG.info("Waiting for active/ready master"); + cluster.waitForActiveAndReadyMaster(); + + while (cluster.getLiveRegionServerThreads().size() < 1) { + Threads.sleep(1); + } + + rs = cluster.getRegionServer(0); + } + + @After + public void after() throws Exception { + if (TEST_UTIL != null) { + TEST_UTIL.shutdownMiniCluster(); + } + } + + @Test(timeout=300000) + public void testClusterRequests() throws Exception { + startCluster(); + + // Sleeping 2 seconds to make sure most recent metrics values are reported + Threads.sleep(2000); + + long regions = metricsHelper.getGaugeLong("regions", rs.getMetrics2().getMetricsSource()); + // Creating a table should add one region + TEST_UTIL.createTable(Bytes.toBytes("table"), Bytes.toBytes("cf")); + + // Sleeping 2 seconds to make sure most recent metrics values are reported + Threads.sleep(2000); + + metricsHelper.assertGauge("regions", + regions + 1, rs.getMetrics2().getMetricsSource()); + } +} Index: hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetrics2.java =================================================================== --- hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetrics2.java (revision 0) +++ hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetrics2.java (revision 0) @@ -0,0 +1,58 @@ +/** + * 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.hadoop.hbase.regionserver.metrics; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.CompatibilitySingletonFactory; + +/** + * This class is for maintaining the various regionserver statistics + * and publishing them through the metrics interfaces. + *
+ * This class has a number of metrics variables that are publicly accessible; + * these variables (objects) have methods to update their values. + */ +@InterfaceStability.Evolving +@InterfaceAudience.Private +public class RegionServerMetrics2 { + private final Log LOG = LogFactory.getLog(this.getClass()); + private RegionServerMetricsSource regionServerMetricsSource; + + public RegionServerMetrics2(RegionServerMetricsWrapper rsWrapper) { + regionServerMetricsSource = + CompatibilitySingletonFactory.getInstance(RegionServerMetricsSourceFactory.class) + .create(rsWrapper); + } + + // for unit-test usage + public RegionServerMetricsSource getMetricsSource() { + return regionServerMetricsSource; + } + + /** + * Set the number of regions carried by this RegionServer. + * @param count Amount to set. + */ + public void setRegions(final int count) { + regionServerMetricsSource.setRegions(count); + + } +} Index: hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsWrapperImpl.java =================================================================== --- hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsWrapperImpl.java (revision 0) +++ hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsWrapperImpl.java (revision 0) @@ -0,0 +1,57 @@ +/** + * 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.hadoop.hbase.regionserver.metrics; + +import org.apache.hadoop.hbase.regionserver.HRegionServer; + +/** + * Impl for exposing HRegionServer Information through JMX + */ +public class RegionServerMetricsWrapperImpl implements RegionServerMetricsWrapper { + + private final HRegionServer regionServer; + + public RegionServerMetricsWrapperImpl(final HRegionServer regionServer) { + this.regionServer = regionServer; + } + + @Override + public String getClusterId() { + return regionServer.getClusterId(); + } + + @Override + public long getRegionServerStartTime() { + return regionServer.getRegionServerStartTime(); + } + + @Override + public String getZookeeperQuorum() { + return regionServer.getZooKeeperWatcher().getQuorum(); + } + + @Override + public String[] getCoprocessors() { + return regionServer.getCoprocessors(); + } + + @Override + public String getServerName() { + return regionServer.getServerName().getServerName(); + } +} Index: hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java =================================================================== --- hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java (revision 1388066) +++ hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java (working copy) @@ -184,6 +184,8 @@ import org.apache.hadoop.hbase.regionserver.metrics.RegionMetricsStorage; import org.apache.hadoop.hbase.regionserver.metrics.RegionServerDynamicMetrics; import org.apache.hadoop.hbase.regionserver.metrics.RegionServerMetrics; +import org.apache.hadoop.hbase.regionserver.metrics.RegionServerMetrics2; +import org.apache.hadoop.hbase.regionserver.metrics.RegionServerMetricsWrapperImpl; import org.apache.hadoop.hbase.regionserver.metrics.SchemaMetrics; import org.apache.hadoop.hbase.regionserver.metrics.SchemaMetrics.StoreMetricType; import org.apache.hadoop.hbase.regionserver.wal.HLog; @@ -370,6 +372,8 @@ private RegionServerDynamicMetrics dynamicMetrics; + private RegionServerMetrics2 metrics2; + /* * Check for compactions requests. */ @@ -446,7 +450,12 @@ */ private final QosFunction qosFunction; + /** + * Time stamps for when a hregionserver was started + */ + private long regionServerStartTime; + /** * Starts a HRegionServer at the default location * @@ -550,6 +559,19 @@ } } + + /** + * @return timestamp in millis when HRegionServer was started. + */ + public long getRegionServerStartTime() { + return regionServerStartTime; + } + + public String getClusterId() { + // TODO: cache in this class field? + return this.conf.get(HConstants.CLUSTER_ID); + } + @Retention(RetentionPolicy.RUNTIME) protected @interface QosPriority { int priority() default 0; @@ -834,6 +856,8 @@ * The HRegionServer sticks in this loop until closed. */ public void run() { + regionServerStartTime = System.currentTimeMillis(); + try { // Do pre-registration initializations; zookeeper, lease threads, etc. preRegistrationInitialization(); @@ -1199,6 +1223,7 @@ // Init in here rather than in constructor after thread name has been set this.metrics = new RegionServerMetrics(); this.dynamicMetrics = RegionServerDynamicMetrics.newInstance(); + this.metrics2 = new RegionServerMetrics2(new RegionServerMetricsWrapperImpl(this)); startServiceThreads(); LOG.info("Serving as " + this.serverNameFromMasterPOV + ", RPC listening on " + this.isa + @@ -1443,6 +1468,7 @@ protected void metrics() { this.metrics.regions.set(this.onlineRegions.size()); + this.metrics2.setRegions(this.onlineRegions.size()); this.metrics.incrementRequests(this.requestCount.get()); this.metrics.requests.intervalHeartBeat(); // Is this too expensive every three seconds getting a lock on onlineRegions @@ -1598,6 +1624,10 @@ return this.metrics; } + RegionServerMetrics2 getMetrics2() { + return this.metrics2; + } + /** * @return Master address tracker instance. */ Index: hbase-hadoop1-compat/src/test/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceImplTest.java =================================================================== --- hbase-hadoop1-compat/src/test/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceImplTest.java (revision 0) +++ hbase-hadoop1-compat/src/test/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceImplTest.java (revision 0) @@ -0,0 +1,41 @@ +/** + * 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.hadoop.hbase.regionserver.metrics; + +import org.apache.hadoop.hbase.CompatibilitySingletonFactory; +import org.junit.Test; + +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +/** + * Test for RegionServerMetricsSourceImpl + */ +public class RegionServerMetricsSourceImplTest { + + @Test + public void testGetInstance() throws Exception { + RegionServerMetricsSourceFactory regionServerMetricsSourceFactory = CompatibilitySingletonFactory + .getInstance(RegionServerMetricsSourceFactory.class); + RegionServerMetricsSource regionServerMetricsSource = regionServerMetricsSourceFactory.create(null); + assertTrue(regionServerMetricsSource instanceof RegionServerMetricsSourceImpl); + assertSame(regionServerMetricsSourceFactory, CompatibilitySingletonFactory.getInstance(RegionServerMetricsSourceFactory.class)); + } + +} Index: hbase-hadoop1-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceImpl.java =================================================================== --- hbase-hadoop1-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceImpl.java (revision 0) +++ hbase-hadoop1-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceImpl.java (revision 0) @@ -0,0 +1,86 @@ +/** + * 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.hadoop.hbase.regionserver.metrics; + +import org.apache.hadoop.hbase.metrics.BaseMetricsSourceImpl; +import org.apache.hadoop.metrics2.MetricsBuilder; +import org.apache.hadoop.metrics2.MetricsRecordBuilder; +import org.apache.hadoop.metrics2.lib.MetricMutableGaugeLong; + +/** + * Hadoop1 implementation of RegionServerMetricsSource. + */ +public class RegionServerMetricsSourceImpl + extends BaseMetricsSourceImpl implements RegionServerMetricsSource { + + MetricMutableGaugeLong regions; + + private final RegionServerMetricsWrapper rsWrapper; + + public RegionServerMetricsSourceImpl(RegionServerMetricsWrapper rsWrapper) { + this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT, rsWrapper); + } + + public RegionServerMetricsSourceImpl(String metricsName, + String metricsDescription, + String metricsContext, + String metricsJmxContext, + RegionServerMetricsWrapper rsWrapper) { + super(metricsName, metricsDescription, metricsContext, metricsJmxContext); + + this.rsWrapper = rsWrapper; + } + + @Override + public void init() { + super.init(); + + regions = metricsRegistry.newGauge(NUMBER_OF_REGIONS_NAME, NUMBER_OF_REGIONS_DESC, 0L); + } + + public void setRegions(final int count) { + regions.set(count); + } + + + /** + * Method to export all the metrics. + * + * @param metricsBuilder Builder to accept metrics + * @param all push all or only changed? + */ + @Override + public void getMetrics(MetricsBuilder metricsBuilder, boolean all) { + + MetricsRecordBuilder metricsRecordBuilder = metricsBuilder.addRecord(metricsName) + .setContext(metricsContext); + + // rsWrapper can be null because this function is called inside of init. + if (rsWrapper != null) { + metricsRecordBuilder + .addGauge(RS_START_TIME_NAME, + RS_START_TIME_DESC, rsWrapper.getRegionServerStartTime()) + .tag(ZOOKEEPER_QUORUM_NAME, ZOOKEEPER_QUORUM_DESC, rsWrapper.getZookeeperQuorum()) + .tag(SERVER_NAME_NAME, SERVER_NAME_DESC, rsWrapper.getServerName()) + .tag(CLUSTER_ID_NAME, CLUSTER_ID_DESC, rsWrapper.getClusterId()); + } + + metricsRegistry.snapshot(metricsRecordBuilder, all); + } +} Index: hbase-hadoop1-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceFactoryImpl.java =================================================================== --- hbase-hadoop1-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceFactoryImpl.java (revision 0) +++ hbase-hadoop1-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceFactoryImpl.java (revision 0) @@ -0,0 +1,37 @@ +/** + * 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.hadoop.hbase.regionserver.metrics; + +/** + * Factory to create RegionServerMetricsSource when given a RegionServerMetricsWrapper + */ +public class RegionServerMetricsSourceFactoryImpl implements RegionServerMetricsSourceFactory { + private static enum FactoryStorage { + INSTANCE; + RegionServerMetricsSource source; + } + + @Override + public synchronized RegionServerMetricsSource create(RegionServerMetricsWrapper beanWrapper) { + if (FactoryStorage.INSTANCE.source == null ) { + FactoryStorage.INSTANCE.source = new RegionServerMetricsSourceImpl(beanWrapper); + } + return FactoryStorage.INSTANCE.source; + } +} Index: hbase-hadoop1-compat/src/main/resources/META-INF/services/org.apache.hadoop.hbase.regionserver.metrics.RegionServerMetricsSourceFactory =================================================================== --- hbase-hadoop1-compat/src/main/resources/META-INF/services/org.apache.hadoop.hbase.regionserver.metrics.RegionServerMetricsSourceFactory (revision 0) +++ hbase-hadoop1-compat/src/main/resources/META-INF/services/org.apache.hadoop.hbase.regionserver.metrics.RegionServerMetricsSourceFactory (revision 0) @@ -0,0 +1 @@ +org.apache.hadoop.hbase.regionserver.metrics.RegionServerMetricsSourceFactoryImpl Index: hbase-hadoop2-compat/src/test/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceImplTest.java =================================================================== --- hbase-hadoop2-compat/src/test/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceImplTest.java (revision 0) +++ hbase-hadoop2-compat/src/test/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceImplTest.java (revision 0) @@ -0,0 +1,41 @@ +/** + * 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.hadoop.hbase.regionserver.metrics; + +import org.apache.hadoop.hbase.CompatibilitySingletonFactory; +import org.junit.Test; + +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +/** + * Test for RegionServerMetricsSourceImpl + */ +public class RegionServerMetricsSourceImplTest { + + @Test + public void testGetInstance() throws Exception { + RegionServerMetricsSourceFactory regionServerMetricsSourceFactory = CompatibilitySingletonFactory + .getInstance(RegionServerMetricsSourceFactory.class); + RegionServerMetricsSource regionServerMetricsSource = regionServerMetricsSourceFactory.create(null); + assertTrue(regionServerMetricsSource instanceof RegionServerMetricsSourceImpl); + assertSame(regionServerMetricsSourceFactory, CompatibilitySingletonFactory.getInstance(RegionServerMetricsSourceFactory.class)); + } + +} Index: hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceImpl.java =================================================================== --- hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceImpl.java (revision 0) +++ hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceImpl.java (revision 0) @@ -0,0 +1,90 @@ +/** + * 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.hadoop.hbase.regionserver.metrics; + +import org.apache.hadoop.hbase.metrics.BaseMetricsSourceImpl; +import org.apache.hadoop.metrics2.MetricsCollector; +import org.apache.hadoop.metrics2.MetricsRecordBuilder; +import org.apache.hadoop.metrics2.lib.Interns; +import org.apache.hadoop.metrics2.lib.MutableGaugeLong; + +/** + * Hadoop1 implementation of RegionServerMetricsSource. + */ +public class RegionServerMetricsSourceImpl + extends BaseMetricsSourceImpl implements RegionServerMetricsSource { + + MutableGaugeLong regions; + + private final RegionServerMetricsWrapper rsWrapper; + + public RegionServerMetricsSourceImpl(RegionServerMetricsWrapper rsWrapper) { + this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT, rsWrapper); + } + + public RegionServerMetricsSourceImpl(String metricsName, + String metricsDescription, + String metricsContext, + String metricsJmxContext, + RegionServerMetricsWrapper rsWrapper) { + super(metricsName, metricsDescription, metricsContext, metricsJmxContext); + + this.rsWrapper = rsWrapper; + } + + @Override + public void init() { + super.init(); + + regions = metricsRegistry.newGauge(NUMBER_OF_REGIONS_NAME, NUMBER_OF_REGIONS_DESC, 0L); + } + + public void setRegions(final int count) { + regions.set(count); + } + + + /** + * Method to export all the metrics. + * + * @param metricsCollector collector to accept metrics + * @param all push all or only changed? + */ + @Override + public void getMetrics(MetricsCollector metricsCollector, boolean all) { + + MetricsRecordBuilder metricsRecordBuilder = metricsCollector.addRecord(metricsName) + .setContext(metricsContext); + + // rsWrapper can be null because this function is called inside of init. + if (rsWrapper != null) { + metricsRecordBuilder + .addGauge(Interns.info(RS_START_TIME_NAME, + RS_START_TIME_DESC), rsWrapper.getRegionServerStartTime()) + .tag(Interns.info(ZOOKEEPER_QUORUM_NAME, + ZOOKEEPER_QUORUM_DESC), rsWrapper.getZookeeperQuorum()) + .tag(Interns.info(SERVER_NAME_NAME, + SERVER_NAME_DESC), rsWrapper.getServerName()) + .tag(Interns.info(CLUSTER_ID_NAME, + CLUSTER_ID_DESC), rsWrapper.getClusterId()); + } + + metricsRegistry.snapshot(metricsRecordBuilder, all); + } +} Index: hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceFactoryImpl.java =================================================================== --- hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceFactoryImpl.java (revision 0) +++ hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceFactoryImpl.java (revision 0) @@ -0,0 +1,37 @@ +/** + * 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.hadoop.hbase.regionserver.metrics; + +/** + * Factory to create RegionServerMetricsSource when given a RegionServerMetricsWrapper + */ +public class RegionServerMetricsSourceFactoryImpl implements RegionServerMetricsSourceFactory { + private static enum FactoryStorage { + INSTANCE; + RegionServerMetricsSource source; + } + + @Override + public synchronized RegionServerMetricsSource create(RegionServerMetricsWrapper beanWrapper) { + if (FactoryStorage.INSTANCE.source == null ) { + FactoryStorage.INSTANCE.source = new RegionServerMetricsSourceImpl(beanWrapper); + } + return FactoryStorage.INSTANCE.source; + } +} Index: hbase-hadoop2-compat/src/main/resources/META-INF/services/org.apache.hadoop.hbase.regionserver.metrics.RegionServerMetricsSourceFactory =================================================================== --- hbase-hadoop2-compat/src/main/resources/META-INF/services/org.apache.hadoop.hbase.regionserver.metrics.RegionServerMetricsSourceFactory (revision 0) +++ hbase-hadoop2-compat/src/main/resources/META-INF/services/org.apache.hadoop.hbase.regionserver.metrics.RegionServerMetricsSourceFactory (revision 0) @@ -0,0 +1 @@ +org.apache.hadoop.hbase.regionserver.metrics.RegionServerMetricsSourceImpl Index: hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/regionserver/metrics/TestRegionServerMetricsSourceFactory.java =================================================================== --- hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/regionserver/metrics/TestRegionServerMetricsSourceFactory.java (revision 0) +++ hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/regionserver/metrics/TestRegionServerMetricsSourceFactory.java (revision 0) @@ -0,0 +1,35 @@ +/** + * 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.hadoop.hbase.regionserver.metrics; + +import org.apache.hadoop.hbase.CompatibilitySingletonFactory; +import org.junit.Test; + +/** + * Test for the CompatibilitySingletonFactory and building RegionServerMetricsSource + */ +public class TestRegionServerMetricsSourceFactory { + + @Test(expected=RuntimeException.class) + public void testGetInstanceNoHadoopCompat() throws Exception { + //This should throw an exception because there is no compat lib on the class path. + CompatibilitySingletonFactory.getInstance(RegionServerMetricsSourceFactory.class); + + } +} Index: hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSource.java =================================================================== --- hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSource.java (revision 0) +++ hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSource.java (revision 0) @@ -0,0 +1,67 @@ +/** + * 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.hadoop.hbase.regionserver.metrics; + +import org.apache.hadoop.hbase.metrics.BaseMetricsSource; + +/** + * Interface that classes that expose metrics about the regionserver will implement. + */ +public interface RegionServerMetricsSource extends BaseMetricsSource { + + /** + * The name of the metrics + */ + public static final String METRICS_NAME = "HRegionServer"; + + /** + * The name of the metrics context that metrics will be under. + */ + public static final String METRICS_CONTEXT = "HRegionServer,sub=Dynamic"; + + /** + * Description + */ + public static final String METRICS_DESCRIPTION = "Metrics about HBase RegionServer"; + + /** + * The name of the metrics context that metrics will be under in jmx + */ + public static final String METRICS_JMX_CONTEXT = "HRegionServer"; + + // Strings used for exporting to metrics system. + public static final String NUMBER_OF_REGIONS_NAME = "regions"; + public static final String NUMBER_OF_REGIONS_DESC = "Number of regions"; + + public static final String RS_START_TIME_NAME = "regionServerStartTime"; + public static final String ZOOKEEPER_QUORUM_NAME = "zookeeperQuorum"; + public static final String SERVER_NAME_NAME = "serverName"; + public static final String CLUSTER_ID_NAME = "clusterId"; + public static final String RS_START_TIME_DESC = "RegionServer Start Time"; + public static final String ZOOKEEPER_QUORUM_DESC = "Zookeeper Quorum"; + public static final String SERVER_NAME_DESC = "Server Name"; + public static final String CLUSTER_ID_DESC = "Cluster Id"; + + + /** + * Set the number of regions carried by this regionserver. + * @param count Amount to set. + */ + public void setRegions(final int count); +} Index: hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsWrapper.java =================================================================== --- hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsWrapper.java (revision 0) +++ hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsWrapper.java (revision 0) @@ -0,0 +1,55 @@ +/** + * 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.hadoop.hbase.regionserver.metrics; + +/** + * This is the interface that will expose RegionServer information to hadoop1/hadoop2 implementations of the + * RegionServerMetricsSource. + */ +public interface RegionServerMetricsWrapper { + + /** + * Get ServerName + */ + public String getServerName(); + + /** + * Get the Cluster ID + * @return Cluster ID + */ + public String getClusterId(); + + /** + * Get the Zookeeper Quorum Info + * @return Zookeeper Quorum Info + */ + public String getZookeeperQuorum(); + + /** + * Get the co-processors + * @return Co-processors + */ + public String[] getCoprocessors(); + + /** + * Get HRegionServer start time + * @return Start time of RegionServer in milliseconds + */ + public long getRegionServerStartTime(); +} Index: hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceFactory.java =================================================================== --- hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceFactory.java (revision 0) +++ hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/RegionServerMetricsSourceFactory.java (revision 0) @@ -0,0 +1,28 @@ +/** + * 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.hadoop.hbase.regionserver.metrics; + +/** + * Interface of a factory to create RegionServerMetricsSource when given a RegionServerMetricsWrapper + */ +public interface RegionServerMetricsSourceFactory { + + public RegionServerMetricsSource create(RegionServerMetricsWrapper beanWrapper); + +}