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.HRegion;
27  import org.apache.hadoop.hbase.regionserver.InternalScanner;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.apache.omid.committable.CommitTable;
30  import org.apache.omid.committable.CommitTable.Client;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  import org.testng.annotations.DataProvider;
34  import org.testng.annotations.Test;
35  
36  import java.util.Queue;
37  
38  import static org.mockito.Mockito.mock;
39  import static org.mockito.Mockito.spy;
40  import static org.mockito.Mockito.when;
41  import static org.testng.Assert.assertEquals;
42  import static org.testng.Assert.assertFalse;
43  
44  public class TestCompactorScanner {
45  
46      private static final Logger LOG = LoggerFactory.getLogger(TestCompactorScanner.class);
47  
48      private static final long TEST_TS = 1L;
49  
50      @DataProvider(name = "cell-retain-options")
51      public Object[][] createCellRetainOptions() {
52          return new Object[][]{
53                  {1, true}, {2, false},
54          };
55      }
56  
57      @Test(dataProvider = "cell-retain-options", timeOut = 60_000)
58      public void testShouldRetainNonTransactionallyDeletedCellMethod(int optionIdx, boolean retainOption)
59              throws Exception {
60  
61          // Create required mocks
62          @SuppressWarnings("unchecked")
63          ObserverContext<RegionCoprocessorEnvironment> ctx = mock(ObserverContext.class);
64          InternalScanner internalScanner = mock(InternalScanner.class);
65          CommitTable.Client ctClient = mock(CommitTable.Client.class);
66          @SuppressWarnings("unchecked")
67          Queue<Client> queue = mock(Queue.class);
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                  queue,
85                  false,
86                  retainOption))) {
87  
88              // Different cell types to test
89              KeyValue regularKV = new KeyValue(Bytes.toBytes("test-row"), TEST_TS, Type.Put);
90              KeyValue deleteKV = new KeyValue(Bytes.toBytes("test-row"), TEST_TS, Type.Delete);
91              KeyValue deleteColumnKV = new KeyValue(Bytes.toBytes("test-row"), TEST_TS, Type.DeleteColumn);
92              KeyValue deleteFamilyKV = new KeyValue(Bytes.toBytes("test-row"), TEST_TS, Type.DeleteFamily);
93              KeyValue deleteFamilyVersionKV = new KeyValue(Bytes.toBytes("test-row"), TEST_TS, Type.DeleteFamilyVersion);
94  
95              assertFalse(scanner.shouldRetainNonTransactionallyDeletedCell(regularKV));
96              assertEquals(scanner.shouldRetainNonTransactionallyDeletedCell(deleteKV), retainOption);
97              assertEquals(scanner.shouldRetainNonTransactionallyDeletedCell(deleteColumnKV), retainOption);
98              assertEquals(scanner.shouldRetainNonTransactionallyDeletedCell(deleteFamilyKV), retainOption);
99              assertEquals(scanner.shouldRetainNonTransactionallyDeletedCell(deleteFamilyVersionKV), retainOption);
100 
101         }
102 
103     }
104 
105 }