View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  import java.util.List;
26  
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.MiniHBaseCluster;
30  import org.apache.hadoop.hbase.NamespaceDescriptor;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.client.HTable;
33  import org.apache.hadoop.hbase.client.Put;
34  import org.apache.hadoop.hbase.protobuf.generated.ClusterStatusProtos.RegionStoreSequenceIds;
35  import org.apache.hadoop.hbase.regionserver.HRegionServer;
36  import org.apache.hadoop.hbase.regionserver.Region;
37  import org.apache.hadoop.hbase.testclassification.MediumTests;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.apache.hadoop.hbase.util.JVMClusterUtil;
40  import org.junit.After;
41  import org.junit.Before;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  
45  /**
46   * Trivial test to confirm that we can get last flushed sequence id by encodedRegionName. See
47   * HBASE-12715.
48   */
49  @Category(MediumTests.class)
50  public class TestGetLastFlushedSequenceId {
51  
52    private final HBaseTestingUtility testUtil = new HBaseTestingUtility();
53  
54    private final TableName tableName = TableName.valueOf(getClass().getSimpleName(), "test");
55  
56    private final byte[] family = Bytes.toBytes("f1");
57  
58    private final byte[][] families = new byte[][] { family };
59  
60    @Before
61    public void setUp() throws Exception {
62      testUtil.getConfiguration().setInt("hbase.regionserver.msginterval", 1000);
63      testUtil.startMiniCluster(1, 1);
64    }
65  
66    @After
67    public void tearDown() throws Exception {
68      testUtil.shutdownMiniCluster();
69    }
70  
71    @Test
72    public void test() throws IOException, InterruptedException {
73      testUtil.getHBaseAdmin().createNamespace(
74        NamespaceDescriptor.create(tableName.getNamespaceAsString()).build());
75      HTable table = testUtil.createTable(tableName, families);
76      table.put(new Put(Bytes.toBytes("k")).add(family, Bytes.toBytes("q"), Bytes.toBytes("v")));
77      table.flushCommits();
78      MiniHBaseCluster cluster = testUtil.getMiniHBaseCluster();
79      List<JVMClusterUtil.RegionServerThread> rsts = cluster.getRegionServerThreads();
80      Region region = null;
81      for (int i = 0; i < cluster.getRegionServerThreads().size(); i++) {
82        HRegionServer hrs = rsts.get(i).getRegionServer();
83        for (Region r : hrs.getOnlineRegions(tableName)) {
84          region = r;
85          break;
86        }
87      }
88      assertNotNull(region);
89      Thread.sleep(2000);
90      RegionStoreSequenceIds ids =
91          testUtil.getHBaseCluster().getMaster()
92              .getLastSequenceId(region.getRegionInfo().getEncodedNameAsBytes());
93      assertEquals(HConstants.NO_SEQNUM, ids.getLastFlushedSequenceId());
94      // This will be the sequenceid just before that of the earliest edit in memstore.
95      long storeSequenceId = ids.getStoreSequenceId(0).getSequenceId();
96      assertTrue(storeSequenceId > 0);
97      testUtil.getHBaseAdmin().flush(tableName);
98      Thread.sleep(2000);
99      ids =
100         testUtil.getHBaseCluster().getMaster()
101             .getLastSequenceId(region.getRegionInfo().getEncodedNameAsBytes());
102     assertTrue(ids.getLastFlushedSequenceId() + " > " + storeSequenceId,
103       ids.getLastFlushedSequenceId() > storeSequenceId);
104     assertEquals(ids.getLastFlushedSequenceId(), ids.getStoreSequenceId(0).getSequenceId());
105     table.close();
106   }
107 }