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.apache.hadoop.hbase.zookeeper.RegionNormalizerTracker;
035import org.junit.Before;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039import org.mockito.Mock;
040import org.mockito.MockitoAnnotations;
041
042import org.apache.hbase.thirdparty.com.google.common.util.concurrent.RateLimiter;
043
044/**
045 * Test that configuration changes are propagated to all children.
046 */
047@Category({ MasterTests.class, SmallTests.class })
048public class TestRegionNormalizerManagerConfigurationObserver {
049
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052    HBaseClassTestRule.forClass(TestRegionNormalizerManagerConfigurationObserver.class);
053
054  private static final HBaseTestingUtil testUtil = new HBaseTestingUtil();
055  private static final Pattern rateLimitPattern =
056    Pattern.compile("RateLimiter\\[stableRate=(?<rate>.+)qps]");
057
058  private Configuration conf;
059  private SimpleRegionNormalizer normalizer;
060  @Mock
061  private MasterServices masterServices;
062  @Mock
063  private RegionNormalizerTracker tracker;
064  @Mock
065  private RegionNormalizerChore chore;
066  @Mock
067  private RegionNormalizerWorkQueue<TableName> queue;
068  private RegionNormalizerWorker worker;
069  private ConfigurationManager configurationManager;
070
071  @Before
072  public void before() {
073    MockitoAnnotations.initMocks(this);
074    conf = testUtil.getConfiguration();
075    normalizer = new SimpleRegionNormalizer();
076    worker = new RegionNormalizerWorker(conf, masterServices, normalizer, queue);
077    final RegionNormalizerManager normalizerManager =
078      new RegionNormalizerManager(tracker, chore, queue, worker);
079    configurationManager = new ConfigurationManager();
080    configurationManager.registerObserver(normalizerManager);
081  }
082
083  @Test
084  public void test() {
085    assertTrue(normalizer.isMergeEnabled());
086    assertEquals(3, normalizer.getMergeMinRegionCount());
087    assertEquals(1_000_000L, parseConfiguredRateLimit(worker.getRateLimiter()));
088
089    final Configuration newConf = new Configuration(conf);
090    // configs on SimpleRegionNormalizer
091    newConf.setBoolean("hbase.normalizer.merge.enabled", false);
092    newConf.setInt("hbase.normalizer.merge.min.region.count", 100);
093    // config on RegionNormalizerWorker
094    newConf.set("hbase.normalizer.throughput.max_bytes_per_sec", "12g");
095
096    configurationManager.notifyAllObservers(newConf);
097    assertFalse(normalizer.isMergeEnabled());
098    assertEquals(100, normalizer.getMergeMinRegionCount());
099    assertEquals(12_884L, parseConfiguredRateLimit(worker.getRateLimiter()));
100  }
101
102  /**
103   * The {@link RateLimiter} class does not publicly expose its currently configured rate. It does
104   * offer this information in the {@link RateLimiter#toString()} method. It's fragile, but parse
105   * this value. The alternative would be to track the value explicitly in the worker, and the
106   * associated coordination overhead paid at runtime. See the related note on
107   * {@link RegionNormalizerWorker#getRateLimiter()}.
108   */
109  private static long parseConfiguredRateLimit(final RateLimiter rateLimiter) {
110    final String val = rateLimiter.toString();
111    final Matcher matcher = rateLimitPattern.matcher(val);
112    assertTrue(matcher.matches());
113    final String parsedRate = matcher.group("rate");
114    return (long) Double.parseDouble(parsedRate);
115  }
116}