View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.omid.transaction;
19  
20  import com.google.common.util.concurrent.SettableFuture;
21  import org.apache.hadoop.hbase.HRegionInfo;
22  import org.apache.hadoop.hbase.KeyValue;
23  import org.apache.hadoop.hbase.KeyValue.Type;
24  import org.apache.hadoop.hbase.coprocessor.ObserverContext;
25  import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
26  import org.apache.hadoop.hbase.regionserver.CompactorScanner;
27  import org.apache.hadoop.hbase.regionserver.HRegion;
28  import org.apache.hadoop.hbase.regionserver.InternalScanner;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.apache.omid.committable.CommitTable;
31  import org.apache.omid.committable.CommitTable.Client;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  import org.testng.annotations.DataProvider;
35  import org.testng.annotations.Test;
36  
37  import java.util.Queue;
38  
39  import static org.mockito.Mockito.mock;
40  import static org.mockito.Mockito.spy;
41  import static org.mockito.Mockito.when;
42  import static org.testng.Assert.assertEquals;
43  import static org.testng.Assert.assertFalse;
44  
45  public class TestCompactorScanner {
46  
47      private static final Logger LOG = LoggerFactory.getLogger(TestCompactorScanner.class);
48  
49      private static final long TEST_TS = 1L;
50  
51      @DataProvider(name = "cell-retain-options")
52      public Object[][] createCellRetainOptions() {
53          return new Object[][]{
54                  {1, true}, {2, false},
55          };
56      }
57  
58      @Test(dataProvider = "cell-retain-options")
59      public void testShouldRetainNonTransactionallyDeletedCellMethod(int optionIdx, boolean retainOption)
60              throws Exception {
61  
62          // Create required mocks
63          @SuppressWarnings("unchecked")
64          ObserverContext<RegionCoprocessorEnvironment> ctx = mock(ObserverContext.class);
65          InternalScanner internalScanner = mock(InternalScanner.class);
66          CommitTable.Client ctClient = mock(CommitTable.Client.class);
67          @SuppressWarnings("unchecked")
68          Queue<Client> queue = mock(Queue.class);
69          RegionCoprocessorEnvironment rce = mock(RegionCoprocessorEnvironment.class);
70          HRegion hRegion = mock(HRegion.class);
71          HRegionInfo hRegionInfo = mock(HRegionInfo.class);
72          SettableFuture<Long> f = SettableFuture.create();
73  
74          // Wire required mock internals
75          f.set(TEST_TS);
76          when(ctClient.readLowWatermark()).thenReturn(f);
77          when(ctx.getEnvironment()).thenReturn(rce);
78          when(rce.getRegion()).thenReturn(hRegion);
79          when(hRegion.getRegionInfo()).thenReturn(hRegionInfo);
80  
81          LOG.info("Testing when retain is {}", retainOption);
82          try (CompactorScanner scanner = spy(new CompactorScanner(ctx,
83                  internalScanner,
84                  ctClient,
85                  queue,
86                  false,
87                  retainOption))) {
88  
89              // Different cell types to test
90              KeyValue regularKV = new KeyValue(Bytes.toBytes("test-row"), TEST_TS, Type.Put);
91              KeyValue deleteKV = new KeyValue(Bytes.toBytes("test-row"), TEST_TS, Type.Delete);
92              KeyValue deleteColumnKV = new KeyValue(Bytes.toBytes("test-row"), TEST_TS, Type.DeleteColumn);
93              KeyValue deleteFamilyKV = new KeyValue(Bytes.toBytes("test-row"), TEST_TS, Type.DeleteFamily);
94              KeyValue deleteFamilyVersionKV = new KeyValue(Bytes.toBytes("test-row"), TEST_TS, Type.DeleteFamilyVersion);
95  
96              assertFalse(scanner.shouldRetainNonTransactionallyDeletedCell(regularKV));
97              assertEquals(scanner.shouldRetainNonTransactionallyDeletedCell(deleteKV), retainOption);
98              assertEquals(scanner.shouldRetainNonTransactionallyDeletedCell(deleteColumnKV), retainOption);
99              assertEquals(scanner.shouldRetainNonTransactionallyDeletedCell(deleteFamilyKV), retainOption);
100             assertEquals(scanner.shouldRetainNonTransactionallyDeletedCell(deleteFamilyVersionKV), retainOption);
101 
102         }
103 
104     }
105 
106 }