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.concurrent.ThreadLocalRandom;
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    for (int i = 0; i < NRUNS; ++i) {
157      assertTrue(tracker.isEmpty());
158
159      int count = 0;
160      while (count < NPROCEDURES) {
161        long procId = ThreadLocalRandom.current().nextLong();
162        if (procId < 1) {
163          continue;
164        }
165
166        tracker.setDeleted(procId, i % 2 == 0);
167        count++;
168      }
169
170      tracker.reset();
171    }
172  }
173
174  @Test
175  public void testLoad() {
176    final int MAX_PROCS = 1000;
177    final ProcedureStoreTracker tracker = new ProcedureStoreTracker();
178    for (int numProcs = 1; numProcs < MAX_PROCS; ++numProcs) {
179      for (int start = 1; start <= numProcs; ++start) {
180        assertTrue(tracker.isEmpty());
181
182        LOG.debug("loading " + numProcs + " procs from start=" + start);
183        for (int i = start; i <= numProcs; ++i) {
184          tracker.setDeleted(i, false);
185        }
186        for (int i = 1; i < start; ++i) {
187          tracker.setDeleted(i, false);
188        }
189
190        tracker.reset();
191      }
192    }
193  }
194
195  @Test
196  public void testDelete() {
197    final ProcedureStoreTracker tracker = new ProcedureStoreTracker();
198
199    long[] procIds = new long[] { 65, 1, 193 };
200    for (int i = 0; i < procIds.length; ++i) {
201      tracker.insert(procIds[i]);
202      tracker.dump();
203    }
204
205    for (int i = 0; i < (64 * 4); ++i) {
206      boolean hasProc = false;
207      for (int j = 0; j < procIds.length; ++j) {
208        if (procIds[j] == i) {
209          hasProc = true;
210          break;
211        }
212      }
213      if (hasProc) {
214        assertEquals(ProcedureStoreTracker.DeleteState.NO, tracker.isDeleted(i));
215      } else {
216        assertEquals("procId=" + i, ProcedureStoreTracker.DeleteState.YES, tracker.isDeleted(i));
217      }
218    }
219  }
220
221  @Test
222  public void testSetDeletedIfModified() {
223    final ProcedureStoreTracker tracker = new ProcedureStoreTracker();
224    final long[] procIds = new long[] { 1, 3, 7, 152, 512, 1024, 1025 };
225
226    // test single proc
227    for (int i = 0; i < procIds.length; ++i) {
228      tracker.insert(procIds[i]);
229    }
230    assertEquals(false, tracker.isEmpty());
231
232    for (int i = 0; i < procIds.length; ++i) {
233      tracker.setDeletedIfModified(procIds[i] - 1);
234      tracker.setDeletedIfModified(procIds[i]);
235      tracker.setDeletedIfModified(procIds[i] + 1);
236    }
237    assertEquals(true, tracker.isEmpty());
238
239    // test batch
240    tracker.reset();
241    for (int i = 0; i < procIds.length; ++i) {
242      tracker.insert(procIds[i]);
243    }
244    assertEquals(false, tracker.isEmpty());
245
246    tracker.setDeletedIfModified(procIds);
247    assertEquals(true, tracker.isEmpty());
248  }
249
250  @Test
251  public void testGetActiveProcIds() {
252    ProcedureStoreTracker tracker = new ProcedureStoreTracker();
253    for (int i = 0; i < 10000; i++) {
254      tracker.insert(i * 10);
255    }
256    for (int i = 0; i < 10000; i += 2) {
257      tracker.delete(i * 10);
258    }
259    long[] activeProcIds = tracker.getAllActiveProcIds();
260    assertEquals(5000, activeProcIds.length);
261    for (int i = 0; i < 5000; i++) {
262      assertEquals((2 * i + 1) * 10, activeProcIds[i]);
263    }
264  }
265
266  @Test
267  public void testGetActiveMinProcId() {
268    ProcedureStoreTracker tracker = new ProcedureStoreTracker();
269    assertEquals(Procedure.NO_PROC_ID, tracker.getActiveMinProcId());
270    for (int i = 100; i < 1000; i = 2 * i + 1) {
271      tracker.insert(i);
272    }
273    for (int i = 100; i < 1000; i = 2 * i + 1) {
274      assertEquals(i, tracker.getActiveMinProcId());
275      tracker.delete(i);
276    }
277    assertEquals(Procedure.NO_PROC_ID, tracker.getActiveMinProcId());
278  }
279}