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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.hadoop
.hbase
;
20 import static org
.junit
.Assert
.assertEquals
;
22 import java
.io
.IOException
;
23 import java
.util
.List
;
24 import java
.util
.concurrent
.ExecutionException
;
25 import java
.util
.concurrent
.TimeUnit
;
26 import java
.util
.concurrent
.TimeoutException
;
27 import org
.apache
.hadoop
.fs
.Path
;
28 import org
.apache
.hadoop
.hbase
.client
.Admin
;
29 import org
.apache
.hadoop
.hbase
.client
.Put
;
30 import org
.apache
.hadoop
.hbase
.client
.RegionInfo
;
31 import org
.apache
.hadoop
.hbase
.client
.RegionLocator
;
32 import org
.apache
.hadoop
.hbase
.client
.Table
;
33 import org
.apache
.hadoop
.hbase
.regionserver
.HRegion
;
34 import org
.apache
.hadoop
.hbase
.regionserver
.HRegionServer
;
35 import org
.apache
.hadoop
.hbase
.regionserver
.wal
.AbstractFSWAL
;
36 import org
.apache
.hadoop
.hbase
.testclassification
.LargeTests
;
37 import org
.apache
.hadoop
.hbase
.testclassification
.RegionServerTests
;
38 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
39 import org
.apache
.hadoop
.hbase
.wal
.WAL
;
40 import org
.apache
.hadoop
.hbase
.wal
.WALFactory
;
41 import org
.junit
.After
;
42 import org
.junit
.AfterClass
;
43 import org
.junit
.BeforeClass
;
44 import org
.junit
.ClassRule
;
45 import org
.junit
.Test
;
46 import org
.junit
.experimental
.categories
.Category
;
49 * Testcase for HBASE-20066
51 @Category({ RegionServerTests
.class, LargeTests
.class })
52 public class TestSequenceIdMonotonicallyIncreasing
{
55 public static final HBaseClassTestRule CLASS_RULE
=
56 HBaseClassTestRule
.forClass(TestSequenceIdMonotonicallyIncreasing
.class);
58 private static final HBaseTestingUtility UTIL
= new HBaseTestingUtility();
60 private static final TableName NAME
= TableName
.valueOf("test");
62 private static final byte[] CF
= Bytes
.toBytes("cf");
64 private static final byte[] CQ
= Bytes
.toBytes("cq");
67 public static void setUpBeforeClass() throws Exception
{
68 UTIL
.startMiniCluster(2);
72 public static void tearDownAfterClass() throws Exception
{
73 UTIL
.shutdownMiniCluster();
77 public void tearDown() throws IOException
{
78 Admin admin
= UTIL
.getAdmin();
79 if (admin
.tableExists(NAME
)) {
80 admin
.disableTable(NAME
);
81 admin
.deleteTable(NAME
);
85 private Table
createTable(boolean multiRegions
) throws IOException
{
87 return UTIL
.createTable(NAME
, CF
, new byte[][] { Bytes
.toBytes(1) });
89 return UTIL
.createTable(NAME
, CF
);
93 private long getMaxSeqId(HRegionServer rs
, RegionInfo region
) throws IOException
{
94 Path walFile
= ((AbstractFSWAL
<?
>) rs
.getWAL(null)).getCurrentFileName();
96 try (WAL
.Reader reader
=
97 WALFactory
.createReader(UTIL
.getTestFileSystem(), walFile
, UTIL
.getConfiguration())) {
99 WAL
.Entry entry
= reader
.next();
103 if (Bytes
.equals(region
.getEncodedNameAsBytes(), entry
.getKey().getEncodedRegionName())) {
104 maxSeqId
= Math
.max(maxSeqId
, entry
.getKey().getSequenceId());
112 public void testSplit()
113 throws IOException
, InterruptedException
, ExecutionException
, TimeoutException
{
114 try (Table table
= createTable(false)) {
115 table
.put(new Put(Bytes
.toBytes(0)).addColumn(CF
, CQ
, Bytes
.toBytes(0)));
116 table
.put(new Put(Bytes
.toBytes(1)).addColumn(CF
, CQ
, Bytes
.toBytes(0)));
119 HRegionServer rs
= UTIL
.getRSForFirstRegionInTable(NAME
);
120 RegionInfo region
= UTIL
.getMiniHBaseCluster().getRegions(NAME
).get(0).getRegionInfo();
121 UTIL
.getAdmin().splitRegionAsync(region
.getRegionName(), Bytes
.toBytes(1)).get(1,
123 long maxSeqId
= getMaxSeqId(rs
, region
);
124 RegionLocator locator
= UTIL
.getConnection().getRegionLocator(NAME
);
125 HRegionLocation locA
= locator
.getRegionLocation(Bytes
.toBytes(0), true);
126 HRegionLocation locB
= locator
.getRegionLocation(Bytes
.toBytes(1), true);
127 assertEquals(maxSeqId
+ 1, locA
.getSeqNum());
128 assertEquals(maxSeqId
+ 1, locB
.getSeqNum());
132 public void testMerge()
133 throws IOException
, InterruptedException
, ExecutionException
, TimeoutException
{
134 try (Table table
= createTable(true)) {
135 table
.put(new Put(Bytes
.toBytes(0)).addColumn(CF
, CQ
, Bytes
.toBytes(0)));
136 table
.put(new Put(Bytes
.toBytes(1)).addColumn(CF
, CQ
, Bytes
.toBytes(0)));
137 table
.put(new Put(Bytes
.toBytes(2)).addColumn(CF
, CQ
, Bytes
.toBytes(0)));
140 MiniHBaseCluster cluster
= UTIL
.getMiniHBaseCluster();
141 List
<HRegion
> regions
= cluster
.getRegions(NAME
);
142 HRegion regionA
= regions
.get(0);
143 HRegion regionB
= regions
.get(1);
145 cluster
.getRegionServer(cluster
.getServerWith(regionA
.getRegionInfo().getRegionName()));
147 cluster
.getRegionServer(cluster
.getServerWith(regionB
.getRegionInfo().getRegionName()));
148 UTIL
.getAdmin().mergeRegionsAsync(regionA
.getRegionInfo().getRegionName(),
149 regionB
.getRegionInfo().getRegionName(), false).get(1, TimeUnit
.MINUTES
);
150 long maxSeqIdA
= getMaxSeqId(rsA
, regionA
.getRegionInfo());
151 long maxSeqIdB
= getMaxSeqId(rsB
, regionB
.getRegionInfo());
152 HRegionLocation loc
=
153 UTIL
.getConnection().getRegionLocator(NAME
).getRegionLocation(Bytes
.toBytes(0), true);
154 assertEquals(Math
.max(maxSeqIdA
, maxSeqIdB
) + 1, loc
.getSeqNum());