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.master; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertNotEquals; 022import static org.junit.jupiter.api.Assertions.assertNotNull; 023import static org.junit.jupiter.api.Assertions.assertTrue; 024 025import java.io.IOException; 026import java.util.List; 027import org.apache.hadoop.hbase.HBaseTestingUtil; 028import org.apache.hadoop.hbase.HConstants; 029import org.apache.hadoop.hbase.NamespaceDescriptor; 030import org.apache.hadoop.hbase.SingleProcessHBaseCluster; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.client.Put; 033import org.apache.hadoop.hbase.client.Table; 034import org.apache.hadoop.hbase.regionserver.HRegion; 035import org.apache.hadoop.hbase.regionserver.HRegionServer; 036import org.apache.hadoop.hbase.regionserver.Region; 037import org.apache.hadoop.hbase.testclassification.MediumTests; 038import org.apache.hadoop.hbase.util.Bytes; 039import org.apache.hadoop.hbase.util.JVMClusterUtil; 040import org.junit.jupiter.api.AfterEach; 041import org.junit.jupiter.api.BeforeEach; 042import org.junit.jupiter.api.Tag; 043import org.junit.jupiter.api.Test; 044 045import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos.RegionStoreSequenceIds; 046 047/** 048 * Trivial test to confirm that we can get last flushed sequence id by encodedRegionName. See 049 * HBASE-12715. 050 */ 051@Tag(MediumTests.TAG) 052public class TestGetLastFlushedSequenceId { 053 054 private final HBaseTestingUtil testUtil = new HBaseTestingUtil(); 055 056 private final TableName tableName = TableName.valueOf(getClass().getSimpleName(), "test"); 057 058 private final byte[] family = Bytes.toBytes("f1"); 059 060 private final byte[][] families = new byte[][] { family }; 061 062 @BeforeEach 063 public void setUp() throws Exception { 064 testUtil.getConfiguration().setInt("hbase.regionserver.msginterval", 1000); 065 testUtil.startMiniCluster(); 066 } 067 068 @AfterEach 069 public void tearDown() throws Exception { 070 testUtil.shutdownMiniCluster(); 071 } 072 073 @Test 074 public void test() throws IOException, InterruptedException { 075 testUtil.getAdmin() 076 .createNamespace(NamespaceDescriptor.create(tableName.getNamespaceAsString()).build()); 077 Table table = testUtil.createTable(tableName, families); 078 table 079 .put(new Put(Bytes.toBytes("k")).addColumn(family, Bytes.toBytes("q"), Bytes.toBytes("v"))); 080 SingleProcessHBaseCluster cluster = testUtil.getMiniHBaseCluster(); 081 List<JVMClusterUtil.RegionServerThread> rsts = cluster.getRegionServerThreads(); 082 Region region = null; 083 for (int i = 0; i < cluster.getRegionServerThreads().size(); i++) { 084 HRegionServer hrs = rsts.get(i).getRegionServer(); 085 for (Region r : hrs.getRegions(tableName)) { 086 region = r; 087 break; 088 } 089 } 090 assertNotNull(region); 091 Thread.sleep(2000); 092 RegionStoreSequenceIds ids = testUtil.getHBaseCluster().getMaster().getServerManager() 093 .getLastFlushedSequenceId(region.getRegionInfo().getEncodedNameAsBytes()); 094 // This will be the sequenceid just before that of the earliest edit in memstore. 095 long storeSequenceId = ids.getStoreSequenceId(0).getSequenceId(); 096 assertTrue(storeSequenceId > 0); 097 // HBASE-30335: openSeqNum is now seeded on region OPEN, so lastFlushedSequenceId is no 098 // longer NO_SEQNUM before the first flush. 099 assertNotEquals(HConstants.NO_SEQNUM, ids.getLastFlushedSequenceId()); 100 testUtil.getAdmin().flush(tableName); 101 Thread.sleep(2000); 102 ids = testUtil.getHBaseCluster().getMaster().getServerManager() 103 .getLastFlushedSequenceId(region.getRegionInfo().getEncodedNameAsBytes()); 104 assertTrue(ids.getLastFlushedSequenceId() > storeSequenceId, 105 ids.getLastFlushedSequenceId() + " > " + storeSequenceId); 106 assertEquals(ids.getLastFlushedSequenceId(), ids.getStoreSequenceId(0).getSequenceId()); 107 table.close(); 108 } 109 110 /** 111 * HBASE-30335: after a region is opened - and before any user write or flush - the master's 112 * flushedSequenceIdByRegion must already contain the region's openSeqNum. Otherwise a subsequent 113 * WAL split (e.g. the hosting RS crashes before its first flush heartbeat) would treat 114 * already-durable edits as unflushed and produce orphaned recovered.edits. 115 */ 116 @Test 117 public void testFlushedSequenceIdSeededOnRegionOpen() throws IOException, InterruptedException { 118 TableName freshTable = TableName.valueOf(getClass().getSimpleName(), "openseed"); 119 testUtil.getAdmin() 120 .createNamespace(NamespaceDescriptor.create(freshTable.getNamespaceAsString()).build()); 121 Table table = testUtil.createTable(freshTable, families); 122 try { 123 SingleProcessHBaseCluster cluster = testUtil.getMiniHBaseCluster(); 124 HRegion region = null; 125 for (JVMClusterUtil.RegionServerThread rst : cluster.getRegionServerThreads()) { 126 for (HRegion r : rst.getRegionServer().getRegions(freshTable)) { 127 region = r; 128 break; 129 } 130 if (region != null) { 131 break; 132 } 133 } 134 assertNotNull(region); 135 long openSeqNum = region.getOpenSeqNum(); 136 RegionStoreSequenceIds ids = testUtil.getHBaseCluster().getMaster().getServerManager() 137 .getLastFlushedSequenceId(region.getRegionInfo().getEncodedNameAsBytes()); 138 assertNotEquals(HConstants.NO_SEQNUM, ids.getLastFlushedSequenceId(), 139 "flushedSequenceIdByRegion should be seeded on region OPEN (HBASE-30335)"); 140 assertEquals(openSeqNum, ids.getLastFlushedSequenceId(), 141 "seeded value must equal the region's openSeqNum"); 142 } finally { 143 table.close(); 144 } 145 } 146 147 /** 148 * HBASE-30335 follow-up: on graceful region CLOSE the RS reports the region's final durable 149 * flushed seqid, and the master must lift its flushedSequenceIdByRegion watermark to that value. 150 * This complements the OPEN-time seed and covers the drain-move / disable window: if the source 151 * RS crashes after the close, a subsequent WAL split must see the already-durable edits as 152 * flushed and not resurrect them as orphaned recovered.edits. Leaves unflushed edits in the 153 * memstore so that the close itself flushes and advances the region's maxFlushedSeqId; a 154 * heartbeat sent before the close cannot carry that value, so only the CLOSE report can deliver 155 * it. Disables the table (a graceful close that keeps the region - unlike delete/split/merge, 156 * disable does not call ServerManager#removeRegion) and asserts the watermark equals the 157 * post-close flushed seqid. 158 */ 159 @Test 160 public void testFlushedSequenceIdSeededOnRegionClose() throws IOException, InterruptedException { 161 TableName freshTable = TableName.valueOf(getClass().getSimpleName(), "closeseed"); 162 testUtil.getAdmin() 163 .createNamespace(NamespaceDescriptor.create(freshTable.getNamespaceAsString()).build()); 164 Table table = testUtil.createTable(freshTable, families); 165 try { 166 SingleProcessHBaseCluster cluster = testUtil.getMiniHBaseCluster(); 167 HRegion region = null; 168 for (JVMClusterUtil.RegionServerThread rst : cluster.getRegionServerThreads()) { 169 for (HRegion r : rst.getRegionServer().getRegions(freshTable)) { 170 region = r; 171 break; 172 } 173 if (region != null) { 174 break; 175 } 176 } 177 assertNotNull(region); 178 // Leave the edits unflushed: heartbeats only report the region's maxFlushedSeqId, which 179 // stays put until the close flushes the memstore. 180 for (int i = 0; i < 3; i++) { 181 table.put(new Put(Bytes.toBytes("k" + i)).addColumn(family, Bytes.toBytes("q"), 182 Bytes.toBytes("v" + i))); 183 } 184 long preCloseFlushedSeqId = region.getMaxFlushedSeqId(); 185 byte[] encodedName = region.getRegionInfo().getEncodedNameAsBytes(); 186 // Gracefully close the region (disable keeps the region entry - it is not removed like a 187 // delete/split/merge would), driving a CLOSED transition that carries the flushed seqid. 188 testUtil.getAdmin().disableTable(freshTable); 189 long closeFlushedSeqId = region.getMaxFlushedSeqId(); 190 assertTrue(closeFlushedSeqId > preCloseFlushedSeqId, "test setup: close flush should advance" 191 + " maxFlushedSeqId past " + preCloseFlushedSeqId + ", got " + closeFlushedSeqId); 192 RegionStoreSequenceIds ids = testUtil.getHBaseCluster().getMaster().getServerManager() 193 .getLastFlushedSequenceId(encodedName); 194 assertEquals(closeFlushedSeqId, ids.getLastFlushedSequenceId(), 195 "flushedSequenceIdByRegion should be seeded with the region's flushed seqid on CLOSE"); 196 } finally { 197 table.close(); 198 } 199 } 200}