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 org.apache.phoenix.thirdparty.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", timeOut = 60_000)
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  
68          RegionCoprocessorEnvironment rce = mock(RegionCoprocessorEnvironment.class);
69          HRegion hRegion = mock(HRegion.class);
70          HRegionInfo hRegionInfo = mock(HRegionInfo.class);
71          SettableFuture<Long> f = SettableFuture.create();
72  
73          // Wire required mock internals
74          f.set(TEST_TS);
75          when(ctClient.readLowWatermark()).thenReturn(f);
76          when(ctx.getEnvironment()).thenReturn(rce);
77          when(rce.getRegion()).thenReturn(hRegion);
78          when(hRegion.getRegionInfo()).thenReturn(hRegionInfo);
79  
80          LOG.info("Testing when retain is {}", retainOption);
81          try (CompactorScanner scanner = spy(new CompactorScanner(ctx,
82                  internalScanner,
83                  ctClient,
84                  false,
85                  retainOption))) {
86  
87              // Different cell types to test
88              KeyValue regularKV = new KeyValue(Bytes.toBytes("test-row"), TEST_TS, Type.Put);
89              KeyValue deleteKV = new KeyValue(Bytes.toBytes("test-row"), TEST_TS, Type.Delete);
90              KeyValue deleteColumnKV = new KeyValue(Bytes.toBytes("test-row"), TEST_TS, Type.DeleteColumn);
91              KeyValue deleteFamilyKV = new KeyValue(Bytes.toBytes("test-row"), TEST_TS, Type.DeleteFamily);
92              KeyValue deleteFamilyVersionKV = new KeyValue(Bytes.toBytes("test-row"), TEST_TS, Type.DeleteFamilyVersion);
93  
94              assertFalse(scanner.shouldRetainNonTransactionallyDeletedCell(regularKV));
95              assertEquals(scanner.shouldRetainNonTransactionallyDeletedCell(deleteKV), retainOption);
96              assertEquals(scanner.shouldRetainNonTransactionallyDeletedCell(deleteColumnKV), retainOption);
97              assertEquals(scanner.shouldRetainNonTransactionallyDeletedCell(deleteFamilyKV), retainOption);
98              assertEquals(scanner.shouldRetainNonTransactionallyDeletedCell(deleteFamilyVersionKV), retainOption);
99  
100         }
101 
102     }
103 
104 }