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.jupiter.api.Assertions.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.NoEOFWALStreamReader;
040import org.apache.hadoop.hbase.wal.WAL;
041import org.apache.hadoop.hbase.wal.WALStreamReader;
042import org.junit.jupiter.api.AfterAll;
043import org.junit.jupiter.api.AfterEach;
044import org.junit.jupiter.api.BeforeAll;
045import org.junit.jupiter.api.Tag;
046import org.junit.jupiter.api.Test;
047
048/**
049 * Testcase for HBASE-20066
050 */
051@Tag(RegionServerTests.TAG)
052@Tag(LargeTests.TAG)
053public class TestSequenceIdMonotonicallyIncreasing {
054
055  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
056
057  private static final TableName NAME = TableName.valueOf("test");
058
059  private static final byte[] CF = Bytes.toBytes("cf");
060
061  private static final byte[] CQ = Bytes.toBytes("cq");
062
063  @BeforeAll
064  public static void setUpBeforeClass() throws Exception {
065    UTIL.startMiniCluster(2);
066  }
067
068  @AfterAll
069  public static void tearDownAfterClass() throws Exception {
070    UTIL.shutdownMiniCluster();
071  }
072
073  @AfterEach
074  public void tearDown() throws IOException {
075    Admin admin = UTIL.getAdmin();
076    if (admin.tableExists(NAME)) {
077      admin.disableTable(NAME);
078      admin.deleteTable(NAME);
079    }
080  }
081
082  private Table createTable(boolean multiRegions) throws IOException {
083    if (multiRegions) {
084      return UTIL.createTable(NAME, CF, new byte[][] { Bytes.toBytes(1) });
085    } else {
086      return UTIL.createTable(NAME, CF);
087    }
088  }
089
090  private long getMaxSeqId(HRegionServer rs, RegionInfo region) throws IOException {
091    Path walFile = ((AbstractFSWAL<?>) rs.getWAL(null)).getCurrentFileName();
092    long maxSeqId = -1L;
093    try (WALStreamReader reader =
094      NoEOFWALStreamReader.create(UTIL.getTestFileSystem(), walFile, UTIL.getConfiguration())) {
095      for (;;) {
096        WAL.Entry entry = reader.next();
097        if (entry == null) {
098          break;
099        }
100        if (Bytes.equals(region.getEncodedNameAsBytes(), entry.getKey().getEncodedRegionName())) {
101          maxSeqId = Math.max(maxSeqId, entry.getKey().getSequenceId());
102        }
103      }
104    }
105    return maxSeqId;
106  }
107
108  @Test
109  public void testSplit()
110    throws IOException, InterruptedException, ExecutionException, TimeoutException {
111    try (Table table = createTable(false)) {
112      table.put(new Put(Bytes.toBytes(0)).addColumn(CF, CQ, Bytes.toBytes(0)));
113      table.put(new Put(Bytes.toBytes(1)).addColumn(CF, CQ, Bytes.toBytes(0)));
114    }
115    UTIL.flush(NAME);
116    HRegionServer rs = UTIL.getRSForFirstRegionInTable(NAME);
117    RegionInfo region = UTIL.getMiniHBaseCluster().getRegions(NAME).get(0).getRegionInfo();
118    UTIL.getAdmin().splitRegionAsync(region.getRegionName(), Bytes.toBytes(1)).get(1,
119      TimeUnit.MINUTES);
120    long maxSeqId = getMaxSeqId(rs, region);
121    RegionLocator locator = UTIL.getConnection().getRegionLocator(NAME);
122    HRegionLocation locA = locator.getRegionLocation(Bytes.toBytes(0), true);
123    HRegionLocation locB = locator.getRegionLocation(Bytes.toBytes(1), true);
124    assertEquals(maxSeqId + 1, locA.getSeqNum());
125    assertEquals(maxSeqId + 1, locB.getSeqNum());
126  }
127
128  @Test
129  public void testMerge()
130    throws IOException, InterruptedException, ExecutionException, TimeoutException {
131    try (Table table = createTable(true)) {
132      table.put(new Put(Bytes.toBytes(0)).addColumn(CF, CQ, Bytes.toBytes(0)));
133      table.put(new Put(Bytes.toBytes(1)).addColumn(CF, CQ, Bytes.toBytes(0)));
134      table.put(new Put(Bytes.toBytes(2)).addColumn(CF, CQ, Bytes.toBytes(0)));
135    }
136    UTIL.flush(NAME);
137    SingleProcessHBaseCluster cluster = UTIL.getMiniHBaseCluster();
138    List<HRegion> regions = cluster.getRegions(NAME);
139    HRegion regionA = regions.get(0);
140    HRegion regionB = regions.get(1);
141    HRegionServer rsA =
142      cluster.getRegionServer(cluster.getServerWith(regionA.getRegionInfo().getRegionName()));
143    HRegionServer rsB =
144      cluster.getRegionServer(cluster.getServerWith(regionB.getRegionInfo().getRegionName()));
145    UTIL.getAdmin().mergeRegionsAsync(regionA.getRegionInfo().getRegionName(),
146      regionB.getRegionInfo().getRegionName(), false).get(1, TimeUnit.MINUTES);
147    long maxSeqIdA = getMaxSeqId(rsA, regionA.getRegionInfo());
148    long maxSeqIdB = getMaxSeqId(rsB, regionB.getRegionInfo());
149    HRegionLocation loc =
150      UTIL.getConnection().getRegionLocator(NAME).getRegionLocation(Bytes.toBytes(0), true);
151    assertEquals(Math.max(maxSeqIdA, maxSeqIdB) + 1, loc.getSeqNum());
152  }
153}