001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.master.normalizer; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertTrue; 023 024import java.util.regex.Matcher; 025import java.util.regex.Pattern; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.HBaseTestingUtil; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.conf.ConfigurationManager; 031import org.apache.hadoop.hbase.master.MasterServices; 032import org.apache.hadoop.hbase.testclassification.MasterTests; 033import org.apache.hadoop.hbase.testclassification.SmallTests; 034import org.junit.Before; 035import org.junit.ClassRule; 036import org.junit.Test; 037import org.junit.experimental.categories.Category; 038import org.mockito.Mock; 039import org.mockito.MockitoAnnotations; 040 041import org.apache.hbase.thirdparty.com.google.common.util.concurrent.RateLimiter; 042 043/** 044 * Test that configuration changes are propagated to all children. 045 */ 046@Category({ MasterTests.class, SmallTests.class }) 047public class TestRegionNormalizerManagerConfigurationObserver { 048 049 @ClassRule 050 public static final HBaseClassTestRule CLASS_RULE = 051 HBaseClassTestRule.forClass(TestRegionNormalizerManagerConfigurationObserver.class); 052 053 private static final HBaseTestingUtil testUtil = new HBaseTestingUtil(); 054 private static final Pattern rateLimitPattern = 055 Pattern.compile("RateLimiter\\[stableRate=(?<rate>.+)qps]"); 056 057 private Configuration conf; 058 private SimpleRegionNormalizer normalizer; 059 @Mock 060 private MasterServices masterServices; 061 @Mock 062 private RegionNormalizerStateStore stateStore; 063 @Mock 064 private RegionNormalizerChore chore; 065 @Mock 066 private RegionNormalizerWorkQueue<TableName> queue; 067 private RegionNormalizerWorker worker; 068 private ConfigurationManager configurationManager; 069 070 @Before 071 public void before() { 072 MockitoAnnotations.initMocks(this); 073 conf = testUtil.getConfiguration(); 074 normalizer = new SimpleRegionNormalizer(); 075 worker = new RegionNormalizerWorker(conf, masterServices, normalizer, queue); 076 final RegionNormalizerManager normalizerManager = 077 new RegionNormalizerManager(stateStore, chore, queue, worker); 078 configurationManager = new ConfigurationManager(); 079 configurationManager.registerObserver(normalizerManager); 080 } 081 082 @Test 083 public void test() { 084 assertTrue(normalizer.isMergeEnabled()); 085 assertEquals(3, normalizer.getMergeMinRegionCount()); 086 assertEquals(1_000_000L, parseConfiguredRateLimit(worker.getRateLimiter())); 087 088 final Configuration newConf = new Configuration(conf); 089 // configs on SimpleRegionNormalizer 090 newConf.setBoolean("hbase.normalizer.merge.enabled", false); 091 newConf.setInt("hbase.normalizer.merge.min.region.count", 100); 092 // config on RegionNormalizerWorker 093 newConf.set("hbase.normalizer.throughput.max_bytes_per_sec", "12g"); 094 095 configurationManager.notifyAllObservers(newConf); 096 assertFalse(normalizer.isMergeEnabled()); 097 assertEquals(100, normalizer.getMergeMinRegionCount()); 098 assertEquals(12_884L, parseConfiguredRateLimit(worker.getRateLimiter())); 099 } 100 101 /** 102 * The {@link RateLimiter} class does not publicly expose its currently configured rate. It does 103 * offer this information in the {@link RateLimiter#toString()} method. It's fragile, but parse 104 * this value. The alternative would be to track the value explicitly in the worker, and the 105 * associated coordination overhead paid at runtime. See the related note on 106 * {@link RegionNormalizerWorker#getRateLimiter()}. 107 */ 108 private static long parseConfiguredRateLimit(final RateLimiter rateLimiter) { 109 final String val = rateLimiter.toString(); 110 final Matcher matcher = rateLimitPattern.matcher(val); 111 assertTrue(matcher.matches()); 112 final String parsedRate = matcher.group("rate"); 113 return (long) Double.parseDouble(parsedRate); 114 } 115}