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;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.procedure2.Procedure;
026import org.apache.hadoop.hbase.procedure2.store.ProcedureStoreTracker.DeleteState;
027import org.apache.hadoop.hbase.testclassification.MasterTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033@Category({ MasterTests.class, SmallTests.class })
034public class TestBitSetNode {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038    HBaseClassTestRule.forClass(TestBitSetNode.class);
039
040  @Test
041  public void testGetActiveMaxMinProcId() {
042    BitSetNode node = new BitSetNode(5L, false);
043    assertEquals(5L, node.getActiveMinProcId());
044    assertEquals(5L, node.getActiveMaxProcId());
045    node.insertOrUpdate(10L);
046    assertEquals(5L, node.getActiveMinProcId());
047    assertEquals(10L, node.getActiveMaxProcId());
048    node.insertOrUpdate(1L);
049    assertEquals(1L, node.getActiveMinProcId());
050    assertEquals(10L, node.getActiveMaxProcId());
051
052    node.delete(10L);
053    assertEquals(1L, node.getActiveMinProcId());
054    assertEquals(5L, node.getActiveMaxProcId());
055    node.delete(1L);
056    assertEquals(5L, node.getActiveMinProcId());
057    assertEquals(5L, node.getActiveMaxProcId());
058    node.delete(5L);
059    assertEquals(Procedure.NO_PROC_ID, node.getActiveMinProcId());
060    assertEquals(Procedure.NO_PROC_ID, node.getActiveMaxProcId());
061  }
062
063  @Test
064  public void testGrow() {
065    BitSetNode node = new BitSetNode(1000, false);
066    // contains, do not need to grow but should not fail
067    assertTrue(node.canGrow(1024));
068    assertTrue(node.canGrow(900));
069    assertTrue(node.canGrow(1100));
070    assertFalse(node.canGrow(100));
071    assertFalse(node.canGrow(10000));
072
073    // grow to right
074    node.grow(1100);
075    assertTrue(node.contains(1100));
076    assertTrue(node.isModified(1000));
077    // grow to left
078    node.grow(900);
079    assertTrue(node.contains(900));
080    assertTrue(node.isModified(1000));
081    for (long i = node.getStart(); i <= node.getEnd(); i++) {
082      if (i != 1000) {
083        assertEquals(DeleteState.YES, node.isDeleted(i));
084      } else {
085        assertEquals(DeleteState.NO, node.isDeleted(i));
086      }
087    }
088  }
089
090  @Test
091  public void testMerge() {
092    BitSetNode node = new BitSetNode(1000, false);
093    assertTrue(node.canMerge(new BitSetNode(1200, false)));
094    assertFalse(node.canMerge(new BitSetNode(10000, false)));
095    BitSetNode rightNode = new BitSetNode(1200, false);
096    node.merge(rightNode);
097    assertTrue(node.isModified(1000));
098    assertTrue(node.isModified(1200));
099    for (long i = node.getStart(); i <= node.getEnd(); i++) {
100      if (i != 1000 && i != 1200) {
101        assertEquals(DeleteState.YES, node.isDeleted(i));
102      } else {
103        assertEquals(DeleteState.NO, node.isDeleted(i));
104      }
105    }
106  }
107}