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.janitor;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025import java.util.ArrayList;
026import java.util.List;
027import java.util.concurrent.ExecutionException;
028import org.apache.hadoop.hbase.CatalogFamilyFormat;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseTestingUtil;
031import org.apache.hadoop.hbase.HRegionLocation;
032import org.apache.hadoop.hbase.MetaMockingUtil;
033import org.apache.hadoop.hbase.MetaTableAccessor;
034import org.apache.hadoop.hbase.TableName;
035import org.apache.hadoop.hbase.TableNameTestRule;
036import org.apache.hadoop.hbase.Waiter;
037import org.apache.hadoop.hbase.client.Admin;
038import org.apache.hadoop.hbase.client.Connection;
039import org.apache.hadoop.hbase.client.ConnectionFactory;
040import org.apache.hadoop.hbase.client.Get;
041import org.apache.hadoop.hbase.client.RegionInfo;
042import org.apache.hadoop.hbase.client.RegionLocator;
043import org.apache.hadoop.hbase.client.Result;
044import org.apache.hadoop.hbase.client.Table;
045import org.apache.hadoop.hbase.master.HMaster;
046import org.apache.hadoop.hbase.master.ServerManager;
047import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
048import org.apache.hadoop.hbase.master.assignment.GCRegionProcedure;
049import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
050import org.apache.hadoop.hbase.procedure2.Procedure;
051import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
052import org.apache.hadoop.hbase.testclassification.MasterTests;
053import org.apache.hadoop.hbase.testclassification.MediumTests;
054import org.apache.hadoop.hbase.util.Bytes;
055import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
056import org.apache.hadoop.hbase.util.PairOfSameType;
057import org.apache.hadoop.hbase.util.Threads;
058import org.junit.AfterClass;
059import org.junit.BeforeClass;
060import org.junit.ClassRule;
061import org.junit.Rule;
062import org.junit.Test;
063import org.junit.experimental.categories.Category;
064import org.slf4j.Logger;
065import org.slf4j.LoggerFactory;
066
067@Category({ MasterTests.class, MediumTests.class })
068public class TestCatalogJanitorInMemoryStates {
069
070  @ClassRule
071  public static final HBaseClassTestRule CLASS_RULE =
072    HBaseClassTestRule.forClass(TestCatalogJanitorInMemoryStates.class);
073
074  private static final Logger LOG = LoggerFactory.getLogger(TestCatalogJanitorInMemoryStates.class);
075
076  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
077
078  private static byte[] FAMILY = Bytes.toBytes("testFamily");
079
080  @Rule
081  public final TableNameTestRule name = new TableNameTestRule();
082
083  @BeforeClass
084  public static void setUpBeforeClass() throws Exception {
085    TEST_UTIL.startMiniCluster(1);
086  }
087
088  @AfterClass
089  public static void tearDownAfterClass() throws Exception {
090    TEST_UTIL.shutdownMiniCluster();
091  }
092
093  /**
094   * Test clearing a split parent from memory.
095   */
096  @Test
097  public void testInMemoryParentCleanup()
098    throws IOException, InterruptedException, ExecutionException {
099    HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
100    final AssignmentManager am = master.getAssignmentManager();
101    final ServerManager sm = master.getServerManager();
102
103    Admin admin = TEST_UTIL.getAdmin();
104    admin.catalogJanitorSwitch(false);
105
106    final TableName tableName = name.getTableName();
107    Table t = TEST_UTIL.createTable(tableName, FAMILY);
108    TEST_UTIL.loadTable(t, FAMILY, false);
109
110    RegionLocator locator = TEST_UTIL.getConnection().getRegionLocator(tableName);
111    List<HRegionLocation> allRegionLocations = locator.getAllRegionLocations();
112
113    // We need to create a valid split with daughter regions
114    HRegionLocation parent = allRegionLocations.get(0);
115    List<HRegionLocation> daughters = splitRegion(parent.getRegion());
116    LOG.info("Parent region: " + parent);
117    LOG.info("Daughter regions: " + daughters);
118    assertNotNull("Should have found daughter regions for " + parent, daughters);
119
120    assertTrue("Parent region should exist in RegionStates",
121      am.getRegionStates().isRegionInRegionStates(parent.getRegion()));
122    assertTrue("Parent region should exist in ServerManager",
123      sm.isRegionInServerManagerStates(parent.getRegion()));
124
125    // clean the parent
126    Result r = MetaMockingUtil.getMetaTableRowResult(parent.getRegion(), null,
127      daughters.get(0).getRegion(), daughters.get(1).getRegion());
128    CatalogJanitor.cleanParent(master, parent.getRegion(), r);
129
130    // wait for procedures to complete
131    Waiter.waitFor(TEST_UTIL.getConfiguration(), 10 * 1000, new Waiter.Predicate<Exception>() {
132      @Override
133      public boolean evaluate() throws Exception {
134        ProcedureExecutor<MasterProcedureEnv> pe = master.getMasterProcedureExecutor();
135        for (Procedure<MasterProcedureEnv> proc : pe.getProcedures()) {
136          if (proc.getClass().isAssignableFrom(GCRegionProcedure.class) && proc.isFinished()) {
137            return true;
138          }
139        }
140        return false;
141      }
142    });
143
144    assertFalse("Parent region should have been removed from RegionStates",
145      am.getRegionStates().isRegionInRegionStates(parent.getRegion()));
146    assertFalse("Parent region should have been removed from ServerManager",
147      sm.isRegionInServerManagerStates(parent.getRegion()));
148
149  }
150
151  /**
152   * Splits a region
153   * @param r Region to split.
154   * @return List of region locations
155   */
156  private List<HRegionLocation> splitRegion(final RegionInfo r)
157    throws IOException, InterruptedException, ExecutionException {
158    List<HRegionLocation> locations = new ArrayList<>();
159    // Split this table in two.
160    Admin admin = TEST_UTIL.getAdmin();
161    Connection connection = TEST_UTIL.getConnection();
162    admin.splitRegionAsync(r.getEncodedNameAsBytes()).get();
163    admin.close();
164    PairOfSameType<RegionInfo> regions = waitOnDaughters(r);
165    if (regions != null) {
166      try (RegionLocator rl = connection.getRegionLocator(r.getTable())) {
167        locations.add(rl.getRegionLocation(regions.getFirst().getEncodedNameAsBytes()));
168        locations.add(rl.getRegionLocation(regions.getSecond().getEncodedNameAsBytes()));
169      }
170      return locations;
171    }
172    return locations;
173  }
174
175  /*
176   * Wait on region split. May return because we waited long enough on the split and it didn't
177   * happen. Caller should check.
178   * @return Daughter regions; caller needs to check table actually split.
179   */
180  private PairOfSameType<RegionInfo> waitOnDaughters(final RegionInfo r) throws IOException {
181    long start = EnvironmentEdgeManager.currentTime();
182    PairOfSameType<RegionInfo> pair = null;
183    try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration());
184      Table metaTable = conn.getTable(TableName.META_TABLE_NAME)) {
185      Result result = null;
186      RegionInfo region = null;
187      while ((EnvironmentEdgeManager.currentTime() - start) < 60000) {
188        result = metaTable.get(new Get(r.getRegionName()));
189        if (result == null) {
190          break;
191        }
192        region = CatalogFamilyFormat.getRegionInfo(result);
193        if (region.isSplitParent()) {
194          LOG.debug(region.toString() + " IS a parent!");
195          pair = MetaTableAccessor.getDaughterRegions(result);
196          break;
197        }
198        Threads.sleep(100);
199      }
200
201      if (pair.getFirst() == null || pair.getSecond() == null) {
202        throw new IOException("Failed to get daughters, for parent region: " + r);
203      }
204      return pair;
205    }
206  }
207}