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;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025import java.util.List;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.MiniHBaseCluster;
030import org.apache.hadoop.hbase.NamespaceDescriptor;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.Put;
033import org.apache.hadoop.hbase.client.Table;
034import org.apache.hadoop.hbase.regionserver.HRegionServer;
035import org.apache.hadoop.hbase.regionserver.Region;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.apache.hadoop.hbase.util.JVMClusterUtil;
039import org.junit.After;
040import org.junit.Before;
041import org.junit.ClassRule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044
045import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos.RegionStoreSequenceIds;
046
047/**
048 * Trivial test to confirm that we can get last flushed sequence id by encodedRegionName. See
049 * HBASE-12715.
050 */
051@Category(MediumTests.class)
052public class TestGetLastFlushedSequenceId {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056      HBaseClassTestRule.forClass(TestGetLastFlushedSequenceId.class);
057
058  private final HBaseTestingUtility testUtil = new HBaseTestingUtility();
059
060  private final TableName tableName = TableName.valueOf(getClass().getSimpleName(), "test");
061
062  private final byte[] family = Bytes.toBytes("f1");
063
064  private final byte[][] families = new byte[][] { family };
065
066  @Before
067  public void setUp() throws Exception {
068    testUtil.getConfiguration().setInt("hbase.regionserver.msginterval", 1000);
069    testUtil.startMiniCluster(1, 1);
070  }
071
072  @After
073  public void tearDown() throws Exception {
074    testUtil.shutdownMiniCluster();
075  }
076
077  @Test
078  public void test() throws IOException, InterruptedException {
079    testUtil.getAdmin().createNamespace(
080      NamespaceDescriptor.create(tableName.getNamespaceAsString()).build());
081    Table table = testUtil.createTable(tableName, families);
082    table.put(new Put(Bytes.toBytes("k"))
083            .addColumn(family, Bytes.toBytes("q"), Bytes.toBytes("v")));
084    MiniHBaseCluster cluster = testUtil.getMiniHBaseCluster();
085    List<JVMClusterUtil.RegionServerThread> rsts = cluster.getRegionServerThreads();
086    Region region = null;
087    for (int i = 0; i < cluster.getRegionServerThreads().size(); i++) {
088      HRegionServer hrs = rsts.get(i).getRegionServer();
089      for (Region r : hrs.getRegions(tableName)) {
090        region = r;
091        break;
092      }
093    }
094    assertNotNull(region);
095    Thread.sleep(2000);
096    RegionStoreSequenceIds ids =
097        testUtil.getHBaseCluster().getMaster()
098            .getLastSequenceId(region.getRegionInfo().getEncodedNameAsBytes());
099    assertEquals(HConstants.NO_SEQNUM, ids.getLastFlushedSequenceId());
100    // This will be the sequenceid just before that of the earliest edit in memstore.
101    long storeSequenceId = ids.getStoreSequenceId(0).getSequenceId();
102    assertTrue(storeSequenceId > 0);
103    testUtil.getAdmin().flush(tableName);
104    Thread.sleep(2000);
105    ids =
106        testUtil.getHBaseCluster().getMaster()
107            .getLastSequenceId(region.getRegionInfo().getEncodedNameAsBytes());
108    assertTrue(ids.getLastFlushedSequenceId() + " > " + storeSequenceId,
109      ids.getLastFlushedSequenceId() > storeSequenceId);
110    assertEquals(ids.getLastFlushedSequenceId(), ids.getStoreSequenceId(0).getSequenceId());
111    table.close();
112  }
113}