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.regionserver.HRegion;
35  import org.apache.hadoop.hbase.regionserver.HRegionServer;
36  import org.apache.hadoop.hbase.testclassification.MediumTests;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.apache.hadoop.hbase.util.JVMClusterUtil;
39  import org.junit.After;
40  import org.junit.Before;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  /**
45   * Trivial test to confirm that we can get last flushed sequence id by encodedRegionName. See
46   * HBASE-12715.
47   */
48  @Category(MediumTests.class)
49  public class TestGetLastFlushedSequenceId {
50  
51    private final HBaseTestingUtility testUtil = new HBaseTestingUtility();
52  
53    private final TableName tableName = TableName.valueOf(getClass().getSimpleName(), "test");
54  
55    private final byte[] family = Bytes.toBytes("f1");
56  
57    private final byte[][] families = new byte[][] { family };
58  
59    @Before
60    public void setUp() throws Exception {
61      testUtil.getConfiguration().setInt("hbase.regionserver.msginterval", 1000);
62      testUtil.startMiniCluster(1, 1);
63    }
64  
65    @After
66    public void tearDown() throws Exception {
67      testUtil.shutdownMiniCluster();
68    }
69  
70    @Test
71    public void test() throws IOException, InterruptedException {
72      testUtil.getHBaseAdmin().createNamespace(
73        NamespaceDescriptor.create(tableName.getNamespaceAsString()).build());
74      HTable table = testUtil.createTable(tableName, families);
75      table.put(new Put(Bytes.toBytes("k")).add(family, Bytes.toBytes("q"), Bytes.toBytes("v")));
76      table.flushCommits();
77      MiniHBaseCluster cluster = testUtil.getMiniHBaseCluster();
78      List<JVMClusterUtil.RegionServerThread> rsts = cluster.getRegionServerThreads();
79      HRegion region = null;
80      for (int i = 0; i < cluster.getRegionServerThreads().size(); i++) {
81        HRegionServer hrs = rsts.get(i).getRegionServer();
82        for (HRegion r : hrs.getOnlineRegions(tableName)) {
83          region = r;
84          break;
85        }
86      }
87      assertNotNull(region);
88      Thread.sleep(2000);
89      assertEquals(HConstants.NO_SEQNUM, testUtil.getHBaseCluster().getMaster().getServerManager()
90          .getLastFlushedSequenceId(region.getRegionInfo().getEncodedNameAsBytes()));
91      testUtil.getHBaseAdmin().flush(tableName.getName());
92      Thread.sleep(2000);
93      assertTrue(testUtil.getHBaseCluster().getMaster().getServerManager()
94          .getLastFlushedSequenceId(region.getRegionInfo().getEncodedNameAsBytes()) > 0);
95      table.close();
96    }
97  }