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.assignment;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertNull;
023import static org.junit.Assert.assertTrue;
024import java.util.List;
025import java.util.concurrent.atomic.AtomicBoolean;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.ServerName;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.Put;
032import org.apache.hadoop.hbase.client.RegionInfo;
033import org.apache.hadoop.hbase.client.Result;
034import org.apache.hadoop.hbase.client.Table;
035import org.apache.hadoop.hbase.master.RegionState;
036import org.apache.hadoop.hbase.regionserver.HRegion;
037import org.apache.hadoop.hbase.testclassification.MasterTests;
038import org.apache.hadoop.hbase.testclassification.MediumTests;
039import org.apache.hadoop.hbase.util.Bytes;
040import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
041import org.junit.AfterClass;
042import org.junit.BeforeClass;
043import org.junit.ClassRule;
044import org.junit.Test;
045import org.junit.experimental.categories.Category;
046import org.slf4j.Logger;
047import org.slf4j.LoggerFactory;
048
049
050@Category({ MasterTests.class, MediumTests.class })
051public class TestRegionStateStore {
052
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055    HBaseClassTestRule.forClass(TestRegionStateStore.class);
056
057  private static final Logger LOG = LoggerFactory.getLogger(TestRegionStateStore.class);
058
059  private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
060
061  @BeforeClass
062  public static void beforeClass() throws Exception {
063    UTIL.startMiniCluster();
064  }
065
066  @AfterClass
067  public static void tearDown() throws Exception {
068    UTIL.shutdownMiniCluster();
069  }
070
071  @Test
072  public void testVisitMetaForRegionExistingRegion() throws Exception {
073    final TableName tableName = TableName.valueOf("testVisitMetaForRegion");
074    UTIL.createTable(tableName, "cf");
075    final List<HRegion> regions = UTIL.getHBaseCluster().getRegions(tableName);
076    final String encodedName = regions.get(0).getRegionInfo().getEncodedName();
077    final RegionStateStore regionStateStore = UTIL.getHBaseCluster().getMaster().
078      getAssignmentManager().getRegionStateStore();
079    final AtomicBoolean visitorCalled = new AtomicBoolean(false);
080    regionStateStore.visitMetaForRegion(encodedName, new RegionStateStore.RegionStateVisitor() {
081      @Override
082      public void visitRegionState(Result result, RegionInfo regionInfo, RegionState.State state,
083        ServerName regionLocation, ServerName lastHost, long openSeqNum) {
084        assertEquals(encodedName, regionInfo.getEncodedName());
085        visitorCalled.set(true);
086      }
087    });
088    assertTrue("Visitor has not been called.", visitorCalled.get());
089  }
090
091  @Test
092  public void testVisitMetaForBadRegionState() throws Exception {
093    final TableName tableName = TableName.valueOf("testVisitMetaForBadRegionState");
094    UTIL.createTable(tableName, "cf");
095    final List<HRegion> regions = UTIL.getHBaseCluster().getRegions(tableName);
096    final String encodedName = regions.get(0).getRegionInfo().getEncodedName();
097    final RegionStateStore regionStateStore = UTIL.getHBaseCluster().getMaster().
098        getAssignmentManager().getRegionStateStore();
099
100    // add the BAD_STATE which does not exist in enum RegionState.State
101    Put put = new Put(regions.get(0).getRegionInfo().getRegionName(),
102        EnvironmentEdgeManager.currentTime());
103    put.addColumn(HConstants.CATALOG_FAMILY, HConstants.STATE_QUALIFIER,
104        Bytes.toBytes("BAD_STATE"));
105
106    try (Table table = UTIL.getConnection().getTable(TableName.META_TABLE_NAME)) {
107      table.put(put);
108    }
109
110    final AtomicBoolean visitorCalled = new AtomicBoolean(false);
111    regionStateStore.visitMetaForRegion(encodedName, new RegionStateStore.RegionStateVisitor() {
112      @Override
113      public void visitRegionState(Result result, RegionInfo regionInfo,
114                                   RegionState.State state, ServerName regionLocation,
115                                   ServerName lastHost, long openSeqNum) {
116        assertEquals(encodedName, regionInfo.getEncodedName());
117        assertNull(state);
118        visitorCalled.set(true);
119      }
120    });
121    assertTrue("Visitor has not been called.", visitorCalled.get());
122  }
123
124  @Test
125  public void testVisitMetaForRegionNonExistingRegion() throws Exception {
126    final String encodedName = "fakeencodedregionname";
127    final RegionStateStore regionStateStore = UTIL.getHBaseCluster().getMaster().
128      getAssignmentManager().getRegionStateStore();
129    final AtomicBoolean visitorCalled = new AtomicBoolean(false);
130    regionStateStore.visitMetaForRegion(encodedName, new RegionStateStore.RegionStateVisitor() {
131      @Override
132      public void visitRegionState(Result result, RegionInfo regionInfo, RegionState.State state,
133        ServerName regionLocation, ServerName lastHost, long openSeqNum) {
134        visitorCalled.set(true);
135      }
136    });
137    assertFalse("Visitor has been called, but it shouldn't.", visitorCalled.get());
138  }
139}