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;
019
020import static org.junit.Assert.assertEquals;
021
022import java.io.IOException;
023import java.util.List;
024import java.util.concurrent.ExecutionException;
025import java.util.concurrent.TimeUnit;
026import java.util.concurrent.TimeoutException;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.client.Admin;
029import org.apache.hadoop.hbase.client.Put;
030import org.apache.hadoop.hbase.client.RegionInfo;
031import org.apache.hadoop.hbase.client.RegionLocator;
032import org.apache.hadoop.hbase.client.Table;
033import org.apache.hadoop.hbase.regionserver.HRegion;
034import org.apache.hadoop.hbase.regionserver.HRegionServer;
035import org.apache.hadoop.hbase.regionserver.wal.AbstractFSWAL;
036import org.apache.hadoop.hbase.testclassification.LargeTests;
037import org.apache.hadoop.hbase.testclassification.RegionServerTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.apache.hadoop.hbase.wal.WAL;
040import org.apache.hadoop.hbase.wal.WALFactory;
041import org.junit.After;
042import org.junit.AfterClass;
043import org.junit.BeforeClass;
044import org.junit.ClassRule;
045import org.junit.Test;
046import org.junit.experimental.categories.Category;
047
048/**
049 * Testcase for HBASE-20066
050 */
051@Category({ RegionServerTests.class, LargeTests.class })
052public class TestSequenceIdMonotonicallyIncreasing {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056    HBaseClassTestRule.forClass(TestSequenceIdMonotonicallyIncreasing.class);
057
058  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
059
060  private static final TableName NAME = TableName.valueOf("test");
061
062  private static final byte[] CF = Bytes.toBytes("cf");
063
064  private static final byte[] CQ = Bytes.toBytes("cq");
065
066  @BeforeClass
067  public static void setUpBeforeClass() throws Exception {
068    UTIL.startMiniCluster(2);
069  }
070
071  @AfterClass
072  public static void tearDownAfterClass() throws Exception {
073    UTIL.shutdownMiniCluster();
074  }
075
076  @After
077  public void tearDown() throws IOException {
078    Admin admin = UTIL.getAdmin();
079    if (admin.tableExists(NAME)) {
080      admin.disableTable(NAME);
081      admin.deleteTable(NAME);
082    }
083  }
084
085  private Table createTable(boolean multiRegions) throws IOException {
086    if (multiRegions) {
087      return UTIL.createTable(NAME, CF, new byte[][] { Bytes.toBytes(1) });
088    } else {
089      return UTIL.createTable(NAME, CF);
090    }
091  }
092
093  private long getMaxSeqId(HRegionServer rs, RegionInfo region) throws IOException {
094    Path walFile = ((AbstractFSWAL<?>) rs.getWAL(null)).getCurrentFileName();
095    long maxSeqId = -1L;
096    try (WAL.Reader reader =
097      WALFactory.createReader(UTIL.getTestFileSystem(), walFile, UTIL.getConfiguration())) {
098      for (;;) {
099        WAL.Entry entry = reader.next();
100        if (entry == null) {
101          break;
102        }
103        if (Bytes.equals(region.getEncodedNameAsBytes(), entry.getKey().getEncodedRegionName())) {
104          maxSeqId = Math.max(maxSeqId, entry.getKey().getSequenceId());
105        }
106      }
107    }
108    return maxSeqId;
109  }
110
111  @Test
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)));
117    }
118    UTIL.flush(NAME);
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,
122      TimeUnit.MINUTES);
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());
129  }
130
131  @Test
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)));
138    }
139    UTIL.flush(NAME);
140    MiniHBaseCluster cluster = UTIL.getMiniHBaseCluster();
141    List<HRegion> regions = cluster.getRegions(NAME);
142    HRegion regionA = regions.get(0);
143    HRegion regionB = regions.get(1);
144    HRegionServer rsA =
145      cluster.getRegionServer(cluster.getServerWith(regionA.getRegionInfo().getRegionName()));
146    HRegionServer rsB =
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());
155  }
156}