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; 023import java.util.regex.Matcher; 024import java.util.regex.Pattern; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.HBaseTestingUtility; 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.apache.hadoop.hbase.zookeeper.RegionNormalizerTracker; 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; 040import org.apache.hbase.thirdparty.com.google.common.util.concurrent.RateLimiter; 041 042/** 043 * Test that configuration changes are propagated to all children. 044 */ 045@Category({ MasterTests.class, SmallTests.class}) 046public class TestRegionNormalizerManagerConfigurationObserver { 047 048 @ClassRule 049 public static final HBaseClassTestRule CLASS_RULE = 050 HBaseClassTestRule.forClass(TestRegionNormalizerManagerConfigurationObserver.class); 051 052 private static final HBaseTestingUtility testUtil = new HBaseTestingUtility(); 053 private static final Pattern rateLimitPattern = 054 Pattern.compile("RateLimiter\\[stableRate=(?<rate>.+)qps]"); 055 056 private Configuration conf; 057 private SimpleRegionNormalizer normalizer; 058 @Mock private MasterServices masterServices; 059 @Mock private RegionNormalizerTracker tracker; 060 @Mock private RegionNormalizerChore chore; 061 @Mock private RegionNormalizerWorkQueue<TableName> queue; 062 private RegionNormalizerWorker worker; 063 private ConfigurationManager configurationManager; 064 065 @Before 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(tracker, 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.getMinRegionCount()); 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.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.getMinRegionCount()); 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}