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.procedure2.store.wal; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertTrue; 023 024import java.util.Random; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.procedure2.Procedure; 027import org.apache.hadoop.hbase.testclassification.MasterTests; 028import org.apache.hadoop.hbase.testclassification.MediumTests; 029import org.junit.ClassRule; 030import org.junit.Test; 031import org.junit.experimental.categories.Category; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034 035@Category({MasterTests.class, MediumTests.class}) 036public class TestProcedureStoreTracker { 037 @ClassRule 038 public static final HBaseClassTestRule CLASS_RULE = 039 HBaseClassTestRule.forClass(TestProcedureStoreTracker.class); 040 041 private static final Logger LOG = LoggerFactory.getLogger(TestProcedureStoreTracker.class); 042 043 @Test 044 public void testSeqInsertAndDelete() { 045 ProcedureStoreTracker tracker = new ProcedureStoreTracker(); 046 assertTrue(tracker.isEmpty()); 047 048 final int MIN_PROC = 1; 049 final int MAX_PROC = 1 << 10; 050 051 // sequential insert 052 for (int i = MIN_PROC; i < MAX_PROC; ++i) { 053 tracker.insert(i); 054 055 // All the proc that we inserted should not be deleted 056 for (int j = MIN_PROC; j <= i; ++j) { 057 assertEquals(ProcedureStoreTracker.DeleteState.NO, tracker.isDeleted(j)); 058 } 059 // All the proc that are not yet inserted should be result as deleted 060 for (int j = i + 1; j < MAX_PROC; ++j) { 061 assertTrue(tracker.isDeleted(j) != ProcedureStoreTracker.DeleteState.NO); 062 } 063 } 064 065 // sequential delete 066 for (int i = MIN_PROC; i < MAX_PROC; ++i) { 067 tracker.delete(i); 068 069 // All the proc that we deleted should be deleted 070 for (int j = MIN_PROC; j <= i; ++j) { 071 assertEquals(ProcedureStoreTracker.DeleteState.YES, tracker.isDeleted(j)); 072 } 073 // All the proc that are not yet deleted should be result as not deleted 074 for (int j = i + 1; j < MAX_PROC; ++j) { 075 assertEquals(ProcedureStoreTracker.DeleteState.NO, tracker.isDeleted(j)); 076 } 077 } 078 assertTrue(tracker.isEmpty()); 079 } 080 081 @Test 082 public void testPartialTracker() { 083 ProcedureStoreTracker tracker = new ProcedureStoreTracker(); 084 tracker.setPartialFlag(true); 085 086 // nothing in the tracker, the state is unknown 087 assertTrue(tracker.isEmpty()); 088 assertEquals(ProcedureStoreTracker.DeleteState.MAYBE, tracker.isDeleted(1)); 089 assertEquals(ProcedureStoreTracker.DeleteState.MAYBE, tracker.isDeleted(579)); 090 091 // Mark 1 as deleted, now that is a known state 092 tracker.setDeleted(1, true); 093 tracker.dump(); 094 assertEquals(ProcedureStoreTracker.DeleteState.YES, tracker.isDeleted(1)); 095 assertEquals(ProcedureStoreTracker.DeleteState.MAYBE, tracker.isDeleted(2)); 096 assertEquals(ProcedureStoreTracker.DeleteState.MAYBE, tracker.isDeleted(579)); 097 098 // Mark 579 as non-deleted, now that is a known state 099 tracker.setDeleted(579, false); 100 assertEquals(ProcedureStoreTracker.DeleteState.YES, tracker.isDeleted(1)); 101 assertEquals(ProcedureStoreTracker.DeleteState.MAYBE, tracker.isDeleted(2)); 102 assertEquals(ProcedureStoreTracker.DeleteState.NO, tracker.isDeleted(579)); 103 assertEquals(ProcedureStoreTracker.DeleteState.MAYBE, tracker.isDeleted(577)); 104 assertEquals(ProcedureStoreTracker.DeleteState.MAYBE, tracker.isDeleted(580)); 105 106 tracker.setDeleted(579, true); 107 tracker.setPartialFlag(false); 108 assertTrue(tracker.isEmpty()); 109 } 110 111 @Test 112 public void testBasicCRUD() { 113 ProcedureStoreTracker tracker = new ProcedureStoreTracker(); 114 assertTrue(tracker.isEmpty()); 115 116 long[] procs = new long[] { 1, 2, 3, 4, 5, 6 }; 117 118 tracker.insert(procs[0]); 119 tracker.insert(procs[1], new long[] { procs[2], procs[3], procs[4] }); 120 assertFalse(tracker.isEmpty()); 121 assertTrue(tracker.isAllModified()); 122 123 tracker.resetModified(); 124 assertFalse(tracker.isAllModified()); 125 126 for (int i = 0; i < 4; ++i) { 127 tracker.update(procs[i]); 128 assertFalse(tracker.isEmpty()); 129 assertFalse(tracker.isAllModified()); 130 } 131 132 tracker.update(procs[4]); 133 assertFalse(tracker.isEmpty()); 134 assertTrue(tracker.isAllModified()); 135 136 tracker.update(procs[5]); 137 assertFalse(tracker.isEmpty()); 138 assertTrue(tracker.isAllModified()); 139 140 for (int i = 0; i < 5; ++i) { 141 tracker.delete(procs[i]); 142 assertFalse(tracker.isEmpty()); 143 assertTrue(tracker.isAllModified()); 144 } 145 tracker.delete(procs[5]); 146 assertTrue(tracker.isEmpty()); 147 } 148 149 @Test 150 public void testRandLoad() { 151 final int NPROCEDURES = 2500; 152 final int NRUNS = 5000; 153 154 final ProcedureStoreTracker tracker = new ProcedureStoreTracker(); 155 156 Random rand = new Random(1); 157 for (int i = 0; i < NRUNS; ++i) { 158 assertTrue(tracker.isEmpty()); 159 160 int count = 0; 161 while (count < NPROCEDURES) { 162 long procId = rand.nextLong(); 163 if (procId < 1) { 164 continue; 165 } 166 167 tracker.setDeleted(procId, i % 2 == 0); 168 count++; 169 } 170 171 tracker.reset(); 172 } 173 } 174 175 @Test 176 public void testLoad() { 177 final int MAX_PROCS = 1000; 178 final ProcedureStoreTracker tracker = new ProcedureStoreTracker(); 179 for (int numProcs = 1; numProcs < MAX_PROCS; ++numProcs) { 180 for (int start = 1; start <= numProcs; ++start) { 181 assertTrue(tracker.isEmpty()); 182 183 LOG.debug("loading " + numProcs + " procs from start=" + start); 184 for (int i = start; i <= numProcs; ++i) { 185 tracker.setDeleted(i, false); 186 } 187 for (int i = 1; i < start; ++i) { 188 tracker.setDeleted(i, false); 189 } 190 191 tracker.reset(); 192 } 193 } 194 } 195 196 @Test 197 public void testDelete() { 198 final ProcedureStoreTracker tracker = new ProcedureStoreTracker(); 199 200 long[] procIds = new long[] { 65, 1, 193 }; 201 for (int i = 0; i < procIds.length; ++i) { 202 tracker.insert(procIds[i]); 203 tracker.dump(); 204 } 205 206 for (int i = 0; i < (64 * 4); ++i) { 207 boolean hasProc = false; 208 for (int j = 0; j < procIds.length; ++j) { 209 if (procIds[j] == i) { 210 hasProc = true; 211 break; 212 } 213 } 214 if (hasProc) { 215 assertEquals(ProcedureStoreTracker.DeleteState.NO, tracker.isDeleted(i)); 216 } else { 217 assertEquals("procId=" + i, ProcedureStoreTracker.DeleteState.YES, tracker.isDeleted(i)); 218 } 219 } 220 } 221 222 @Test 223 public void testSetDeletedIfModified() { 224 final ProcedureStoreTracker tracker = new ProcedureStoreTracker(); 225 final long[] procIds = new long[] { 1, 3, 7, 152, 512, 1024, 1025 }; 226 227 // test single proc 228 for (int i = 0; i < procIds.length; ++i) { 229 tracker.insert(procIds[i]); 230 } 231 assertEquals(false, tracker.isEmpty()); 232 233 for (int i = 0; i < procIds.length; ++i) { 234 tracker.setDeletedIfModified(procIds[i] - 1); 235 tracker.setDeletedIfModified(procIds[i]); 236 tracker.setDeletedIfModified(procIds[i] + 1); 237 } 238 assertEquals(true, tracker.isEmpty()); 239 240 // test batch 241 tracker.reset(); 242 for (int i = 0; i < procIds.length; ++i) { 243 tracker.insert(procIds[i]); 244 } 245 assertEquals(false, tracker.isEmpty()); 246 247 tracker.setDeletedIfModified(procIds); 248 assertEquals(true, tracker.isEmpty()); 249 } 250 251 @Test 252 public void testGetActiveProcIds() { 253 ProcedureStoreTracker tracker = new ProcedureStoreTracker(); 254 for (int i = 0; i < 10000; i++) { 255 tracker.insert(i * 10); 256 } 257 for (int i = 0; i < 10000; i += 2) { 258 tracker.delete(i * 10); 259 } 260 long[] activeProcIds = tracker.getAllActiveProcIds(); 261 assertEquals(5000, activeProcIds.length); 262 for (int i = 0; i < 5000; i++) { 263 assertEquals((2 * i + 1) * 10, activeProcIds[i]); 264 } 265 } 266 267 @Test 268 public void testGetActiveMinProcId() { 269 ProcedureStoreTracker tracker = new ProcedureStoreTracker(); 270 assertEquals(Procedure.NO_PROC_ID, tracker.getActiveMinProcId()); 271 for (int i = 100; i < 1000; i = 2 * i + 1) { 272 tracker.insert(i); 273 } 274 for (int i = 100; i < 1000; i = 2 * i + 1) { 275 assertEquals(i, tracker.getActiveMinProcId()); 276 tracker.delete(i); 277 } 278 assertEquals(Procedure.NO_PROC_ID, tracker.getActiveMinProcId()); 279 } 280}