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