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.assertNotEquals;
022import static org.junit.jupiter.api.Assertions.assertNotNull;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024
025import java.io.IOException;
026import java.util.List;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.NamespaceDescriptor;
030import org.apache.hadoop.hbase.SingleProcessHBaseCluster;
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.HRegion;
035import org.apache.hadoop.hbase.regionserver.HRegionServer;
036import org.apache.hadoop.hbase.regionserver.Region;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.apache.hadoop.hbase.util.JVMClusterUtil;
040import org.junit.jupiter.api.AfterEach;
041import org.junit.jupiter.api.BeforeEach;
042import org.junit.jupiter.api.Tag;
043import org.junit.jupiter.api.Test;
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@Tag(MediumTests.TAG)
052public class TestGetLastFlushedSequenceId {
053
054  private final HBaseTestingUtil testUtil = new HBaseTestingUtil();
055
056  private final TableName tableName = TableName.valueOf(getClass().getSimpleName(), "test");
057
058  private final byte[] family = Bytes.toBytes("f1");
059
060  private final byte[][] families = new byte[][] { family };
061
062  @BeforeEach
063  public void setUp() throws Exception {
064    testUtil.getConfiguration().setInt("hbase.regionserver.msginterval", 1000);
065    testUtil.startMiniCluster();
066  }
067
068  @AfterEach
069  public void tearDown() throws Exception {
070    testUtil.shutdownMiniCluster();
071  }
072
073  @Test
074  public void test() throws IOException, InterruptedException {
075    testUtil.getAdmin()
076      .createNamespace(NamespaceDescriptor.create(tableName.getNamespaceAsString()).build());
077    Table table = testUtil.createTable(tableName, families);
078    table
079      .put(new Put(Bytes.toBytes("k")).addColumn(family, Bytes.toBytes("q"), Bytes.toBytes("v")));
080    SingleProcessHBaseCluster cluster = testUtil.getMiniHBaseCluster();
081    List<JVMClusterUtil.RegionServerThread> rsts = cluster.getRegionServerThreads();
082    Region region = null;
083    for (int i = 0; i < cluster.getRegionServerThreads().size(); i++) {
084      HRegionServer hrs = rsts.get(i).getRegionServer();
085      for (Region r : hrs.getRegions(tableName)) {
086        region = r;
087        break;
088      }
089    }
090    assertNotNull(region);
091    Thread.sleep(2000);
092    RegionStoreSequenceIds ids = testUtil.getHBaseCluster().getMaster().getServerManager()
093      .getLastFlushedSequenceId(region.getRegionInfo().getEncodedNameAsBytes());
094    // This will be the sequenceid just before that of the earliest edit in memstore.
095    long storeSequenceId = ids.getStoreSequenceId(0).getSequenceId();
096    assertTrue(storeSequenceId > 0);
097    // HBASE-30335: openSeqNum is now seeded on region OPEN, so lastFlushedSequenceId is no
098    // longer NO_SEQNUM before the first flush.
099    assertNotEquals(HConstants.NO_SEQNUM, ids.getLastFlushedSequenceId());
100    testUtil.getAdmin().flush(tableName);
101    Thread.sleep(2000);
102    ids = testUtil.getHBaseCluster().getMaster().getServerManager()
103      .getLastFlushedSequenceId(region.getRegionInfo().getEncodedNameAsBytes());
104    assertTrue(ids.getLastFlushedSequenceId() > storeSequenceId,
105      ids.getLastFlushedSequenceId() + " > " + storeSequenceId);
106    assertEquals(ids.getLastFlushedSequenceId(), ids.getStoreSequenceId(0).getSequenceId());
107    table.close();
108  }
109
110  /**
111   * HBASE-30335: after a region is opened - and before any user write or flush - the master's
112   * flushedSequenceIdByRegion must already contain the region's openSeqNum. Otherwise a subsequent
113   * WAL split (e.g. the hosting RS crashes before its first flush heartbeat) would treat
114   * already-durable edits as unflushed and produce orphaned recovered.edits.
115   */
116  @Test
117  public void testFlushedSequenceIdSeededOnRegionOpen() throws IOException, InterruptedException {
118    TableName freshTable = TableName.valueOf(getClass().getSimpleName(), "openseed");
119    testUtil.getAdmin()
120      .createNamespace(NamespaceDescriptor.create(freshTable.getNamespaceAsString()).build());
121    Table table = testUtil.createTable(freshTable, families);
122    try {
123      SingleProcessHBaseCluster cluster = testUtil.getMiniHBaseCluster();
124      HRegion region = null;
125      for (JVMClusterUtil.RegionServerThread rst : cluster.getRegionServerThreads()) {
126        for (HRegion r : rst.getRegionServer().getRegions(freshTable)) {
127          region = r;
128          break;
129        }
130        if (region != null) {
131          break;
132        }
133      }
134      assertNotNull(region);
135      long openSeqNum = region.getOpenSeqNum();
136      RegionStoreSequenceIds ids = testUtil.getHBaseCluster().getMaster().getServerManager()
137        .getLastFlushedSequenceId(region.getRegionInfo().getEncodedNameAsBytes());
138      assertNotEquals(HConstants.NO_SEQNUM, ids.getLastFlushedSequenceId(),
139        "flushedSequenceIdByRegion should be seeded on region OPEN (HBASE-30335)");
140      assertEquals(openSeqNum, ids.getLastFlushedSequenceId(),
141        "seeded value must equal the region's openSeqNum");
142    } finally {
143      table.close();
144    }
145  }
146}