Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ public interface MetricsMasterSource extends BaseSource {
String CLUSTER_READ_REQUESTS_NAME = "clusterReadRequests";
String CLUSTER_WRITE_REQUESTS_NAME = "clusterWriteRequests";
String OLD_WAL_DIR_SIZE_NAME = "oldWALsDirSize";
String BALANCER_ENABLED_NAME = "balancerEnabled";
String NORMALIZER_ENABLED_NAME = "normalizerEnabled";
String SPLIT_ENABLED_NAME = "splitEnabled";
String MERGE_ENABLED_NAME = "mergeEnabled";
String CLEANER_CHORE_ENABLED_NAME = "cleanerChoreEnabled";
String CATALOG_JANITOR_ENABLED_NAME = "catalogJanitorEnabled";
String SNAPSHOT_CLEANUP_ENABLED_NAME = "snapshotCleanupEnabled";
String REPLICATION_PEER_MODIFICATION_ENABLED_NAME = "replicationPeerModificationEnabled";
String RPC_THROTTLE_ENABLED_NAME = "rpcThrottleEnabled";
String SWITCH_STATE_DESC = "Switch state: 1 enabled, 0 disabled";
String MASTER_ACTIVE_TIME_DESC = "Master Active Time";
String MASTER_START_TIME_DESC = "Master Start Time";
String MASTER_FINISHED_INITIALIZATION_TIME_DESC =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public void getMetrics(MetricsCollector metricsCollector, boolean all) {
String.valueOf(masterWrapper.getIsActiveMaster()))
.addGauge(Interns.info(OLD_WAL_DIR_SIZE_NAME, OLD_WAL_DIR_SIZE_DESC),
masterWrapper.getOldWALsDirSize());

masterWrapper.getSwitchStates().forEach((name, state) -> metricsRecordBuilder
.addGauge(Interns.info(name, SWITCH_STATE_DESC), state));
}

metricsRegistry.snapshot(metricsRecordBuilder, all);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,11 @@ public interface MetricsMasterWrapper {
* Get the size of old WALs directory in bytes.
*/
long getOldWALsDirSize();

/**
* Returns known switch states (1 enabled, 0 disabled), or an empty map unless this Master is
* active and initialized. Unavailable states are omitted. Implementations must not perform remote
* reads.
*/
Map<String, Integer> getSwitchStates();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.MasterSwitchType;
import org.apache.hadoop.hbase.client.RegionInfo;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
import org.apache.hadoop.hbase.procedure2.Procedure;
import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
import org.apache.hadoop.hbase.quotas.MasterQuotaManager;
import org.apache.hadoop.hbase.quotas.QuotaObserverChore;
import org.apache.hadoop.hbase.quotas.QuotaUtil;
import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot;
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
import org.apache.hadoop.hbase.util.PairOfSameType;
Expand Down Expand Up @@ -267,4 +270,34 @@ public long getOldWALsDirSize() {
}
return master.getMasterWalManager().getOldWALsDirSize();
}

@Override
public Map<String, Integer> getSwitchStates() {
if (!master.isActiveMaster() || !master.isInitialized()) {
return Collections.emptyMap();

@NihalJain NihalJain Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree on omitting these on backup masters, but for an active-but-not-yet-initialized master, would it be better to emit -1 for all keys? That matches the documented “unknown” semantics and keeps the series continuous across failover?

@junegunn junegunn Oct 13, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review. You're right that it's more consistent.

keeps the series continuous across failover?

I think emitting -1 during master restart, which can be quite frequent in some cases, could make monitoring more complicated without giving much benefit.

So on second thought, how about removing -1 state altogether and also omitting rpcThrottleEnabled if it's not ready?

@junegunn junegunn Oct 13, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've prepared the change to remove -1 and omit unavailable states, keeping the metrics strictly boolean. Please take a look and let me know what you think.

5d9fd6c

}
Map<String, Integer> states = new HashMap<>();
states.put(MetricsMasterSource.BALANCER_ENABLED_NAME, master.isBalancerOn() ? 1 : 0);
states.put(MetricsMasterSource.NORMALIZER_ENABLED_NAME, master.isNormalizerOn() ? 1 : 0);
states.put(MetricsMasterSource.SPLIT_ENABLED_NAME,
master.isSplitOrMergeEnabled(MasterSwitchType.SPLIT) ? 1 : 0);
states.put(MetricsMasterSource.MERGE_ENABLED_NAME,
master.isSplitOrMergeEnabled(MasterSwitchType.MERGE) ? 1 : 0);
states.put(MetricsMasterSource.CLEANER_CHORE_ENABLED_NAME,
master.isCleanerChoreEnabled() ? 1 : 0);
states.put(MetricsMasterSource.CATALOG_JANITOR_ENABLED_NAME,
master.isCatalogJanitorEnabled() ? 1 : 0);
states.put(MetricsMasterSource.SNAPSHOT_CLEANUP_ENABLED_NAME,
master.snapshotCleanupStateStore.get() ? 1 : 0);
states.put(MetricsMasterSource.REPLICATION_PEER_MODIFICATION_ENABLED_NAME,
master.isReplicationPeerModificationEnabled() ? 1 : 0);
MasterQuotaManager quotaManager = master.getMasterQuotaManager();
if (quotaManager != null) {
states.put(MetricsMasterSource.RPC_THROTTLE_ENABLED_NAME,
quotaManager.isRpcThrottleEnabled() ? 1 : 0);
} else if (!QuotaUtil.isQuotaEnabled(master.getConfiguration())) {
states.put(MetricsMasterSource.RPC_THROTTLE_ENABLED_NAME, 0);
}
return states;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public IsRpcThrottleEnabledResponse isRpcThrottleEnabled(IsRpcThrottleEnabledReq
}
}

public boolean isRpcThrottleEnabled() throws IOException {
public boolean isRpcThrottleEnabled() {
return initialized && masterServices.getRpcThrottleStateStore().get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.hadoop.hbase.StartTestingClusterOption;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.YouAreDeadException;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.RegionStatesCount;
import org.apache.hadoop.hbase.client.TableDescriptor;
Expand All @@ -51,6 +52,7 @@
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
import org.apache.hadoop.hbase.util.FSTableDescriptors;
import org.apache.hadoop.hbase.util.IOExceptionConsumer;
import org.apache.zookeeper.KeeperException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -147,6 +149,39 @@ public static void after() throws Exception {
TEST_UTIL.shutdownMiniCluster();
}

@Test
public void testSwitchMetricsFollowAdminChanges() throws IOException {
Admin admin = TEST_UTIL.getAdmin();
MetricsMasterSource source = master.getMasterMetrics().getMetricsSource();
Map<String, IOExceptionConsumer<Boolean>> switches = new HashMap<>();
switches.put(MetricsMasterSource.BALANCER_ENABLED_NAME,
enabled -> admin.balancerSwitch(enabled, false));
switches.put(MetricsMasterSource.NORMALIZER_ENABLED_NAME, admin::normalizerSwitch);
switches.put(MetricsMasterSource.SPLIT_ENABLED_NAME,
enabled -> admin.splitSwitch(enabled, false));
switches.put(MetricsMasterSource.MERGE_ENABLED_NAME,
enabled -> admin.mergeSwitch(enabled, false));
switches.put(MetricsMasterSource.CLEANER_CHORE_ENABLED_NAME, admin::cleanerChoreSwitch);
switches.put(MetricsMasterSource.CATALOG_JANITOR_ENABLED_NAME, admin::catalogJanitorSwitch);
switches.put(MetricsMasterSource.SNAPSHOT_CLEANUP_ENABLED_NAME,
enabled -> admin.snapshotCleanupSwitch(enabled, false));
switches.put(MetricsMasterSource.REPLICATION_PEER_MODIFICATION_ENABLED_NAME,
admin::replicationPeerModificationSwitch);
Map<String, Integer> initialStates = new MetricsMasterWrapperImpl(master).getSwitchStates();
for (Map.Entry<String, IOExceptionConsumer<Boolean>> entry : switches.entrySet()) {
try {
for (boolean enabled : new boolean[] { false, true }) {
entry.getValue().accept(enabled);
metricsHelper.assertGauge(entry.getKey(), enabled ? 1L : 0L, source);
}
} finally {
entry.getValue().accept(initialStates.get(entry.getKey()) == 1);
}
}
// Quota support is disabled by default.
metricsHelper.assertGauge(MetricsMasterSource.RPC_THROTTLE_ENABLED_NAME, 0L, source);
}

@Test
public void testClusterRequests() throws Exception {
// sending fake request to master to see how metric value has changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@
*/
package org.apache.hadoop.hbase.master;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.CompatibilityFactory;
import org.apache.hadoop.hbase.HBaseTestingUtil;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.StartTestingClusterOption;
Expand All @@ -37,6 +42,8 @@
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.quotas.QuotaUtil;
import org.apache.hadoop.hbase.test.MetricsAssertHelper;
import org.apache.hadoop.hbase.testclassification.LargeTests;
import org.apache.hadoop.hbase.testclassification.MasterTests;
import org.apache.hadoop.hbase.util.Bytes;
Expand Down Expand Up @@ -96,6 +103,33 @@ public void testNewCluster() throws Exception {
}
}

@Test
public void testSwitchMetricsWithQuotasEnabled() throws Exception {
enableMaintenanceMode();
TEST_UTIL.getConfiguration().setBoolean(QuotaUtil.QUOTA_CONF_KEY, true);
TEST_UTIL.startMiniCluster(
StartTestingClusterOption.builder().numRegionServers(0).numDataNodes(1).build());

HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
assertTrue(master.isInMaintenanceMode());
assertNull(master.getMasterQuotaManager());
MetricsMasterSource source = master.getMasterMetrics().getMetricsSource();
MetricsAssertHelper metricsHelper = CompatibilityFactory.getInstance(MetricsAssertHelper.class);
Map<String, Integer> states = new MetricsMasterWrapperImpl(master).getSwitchStates();
assertEquals(8, states.size());
assertFalse(states.containsKey(MetricsMasterSource.RPC_THROTTLE_ENABLED_NAME));
Comment on lines +118 to +120
assertFalse(
metricsHelper.checkGaugeExists(MetricsMasterSource.RPC_THROTTLE_ENABLED_NAME, source));
states.forEach((name, state) -> {
assertTrue(state == 0 || state == 1);
metricsHelper.assertGauge(name, state.longValue(), source);
});
metricsHelper.assertGauge(MetricsMasterSource.BALANCER_ENABLED_NAME, 0L, source);
metricsHelper.assertGauge(MetricsMasterSource.NORMALIZER_ENABLED_NAME, 0L, source);
metricsHelper.assertGauge(MetricsMasterSource.SPLIT_ENABLED_NAME, 0L, source);
metricsHelper.assertGauge(MetricsMasterSource.MERGE_ENABLED_NAME, 0L, source);
}

@Test
public void testExistingCluster() throws Exception {
TableName testRepairMode = TableName.valueOf(testMethodName);
Expand Down
Loading
Loading