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