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.mapreduce;
019
020import static org.junit.Assert.assertEquals;
021import static org.mockito.ArgumentMatchers.any;
022import static org.mockito.ArgumentMatchers.eq;
023import static org.mockito.Mockito.doNothing;
024import static org.mockito.Mockito.verify;
025
026import java.io.IOException;
027import java.util.Collection;
028import java.util.List;
029import java.util.Map;
030import java.util.Objects;
031import org.apache.hadoop.conf.Configuration;
032import org.apache.hadoop.fs.Path;
033import org.apache.hadoop.hbase.HBaseClassTestRule;
034import org.apache.hadoop.hbase.client.Scan;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.apache.hadoop.hbase.util.CommonFSUtils;
038import org.junit.Before;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042import org.mockito.Mockito;
043
044import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
045import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableMap;
046import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
047import org.apache.hbase.thirdparty.com.google.common.collect.Maps;
048
049@Category({ SmallTests.class })
050public class TestMultiTableSnapshotInputFormatImpl {
051
052  @ClassRule
053  public static final HBaseClassTestRule CLASS_RULE =
054    HBaseClassTestRule.forClass(TestMultiTableSnapshotInputFormatImpl.class);
055
056  private MultiTableSnapshotInputFormatImpl subject;
057  private Map<String, Collection<Scan>> snapshotScans;
058  private Path restoreDir;
059  private Configuration conf;
060  private Path rootDir;
061
062  @Before
063  public void setUp() throws Exception {
064    this.subject = Mockito.spy(new MultiTableSnapshotInputFormatImpl());
065
066    // mock out restoreSnapshot
067    // TODO: this is kind of meh; it'd be much nicer to just inject the RestoreSnapshotHelper
068    // dependency into the
069    // input format. However, we need a new RestoreSnapshotHelper per snapshot in the current
070    // design, and it *also*
071    // feels weird to introduce a RestoreSnapshotHelperFactory and inject that, which would
072    // probably be the more "pure"
073    // way of doing things. This is the lesser of two evils, perhaps?
074    doNothing().when(this.subject).restoreSnapshot(any(), any(), any(), any(), any());
075
076    this.conf = new Configuration();
077    this.rootDir = new Path("file:///test-root-dir");
078    CommonFSUtils.setRootDir(conf, rootDir);
079    this.snapshotScans = ImmutableMap.<String, Collection<Scan>> of("snapshot1",
080      ImmutableList.of(new Scan().withStartRow(Bytes.toBytes("1")).withStopRow(Bytes.toBytes("2"))),
081      "snapshot2",
082      ImmutableList.of(new Scan().withStartRow(Bytes.toBytes("3")).withStopRow(Bytes.toBytes("4")),
083        new Scan().withStartRow(Bytes.toBytes("5")).withStopRow(Bytes.toBytes("6"))));
084
085    this.restoreDir = new Path(CommonFSUtils.getRootDir(conf), "restore-dir");
086
087  }
088
089  public void callSetInput() throws IOException {
090    subject.setInput(this.conf, snapshotScans, restoreDir);
091  }
092
093  public Map<String, Collection<ScanWithEquals>>
094    toScanWithEquals(Map<String, Collection<Scan>> snapshotScans) throws IOException {
095    Map<String, Collection<ScanWithEquals>> rtn = Maps.newHashMap();
096
097    for (Map.Entry<String, Collection<Scan>> entry : snapshotScans.entrySet()) {
098      List<ScanWithEquals> scans = Lists.newArrayList();
099
100      for (Scan scan : entry.getValue()) {
101        scans.add(new ScanWithEquals(scan));
102      }
103      rtn.put(entry.getKey(), scans);
104    }
105
106    return rtn;
107  }
108
109  public static class ScanWithEquals {
110
111    private final String startRow;
112    private final String stopRow;
113
114    /**
115     * Creates a new instance of this class while copying all values.
116     * @param scan The scan instance to copy from.
117     * @throws java.io.IOException When copying the values fails.
118     */
119    public ScanWithEquals(Scan scan) throws IOException {
120      this.startRow = Bytes.toStringBinary(scan.getStartRow());
121      this.stopRow = Bytes.toStringBinary(scan.getStopRow());
122    }
123
124    @Override
125    public boolean equals(Object obj) {
126      if (!(obj instanceof ScanWithEquals)) {
127        return false;
128      }
129      ScanWithEquals otherScan = (ScanWithEquals) obj;
130      return Objects.equals(this.startRow, otherScan.startRow)
131        && Objects.equals(this.stopRow, otherScan.stopRow);
132    }
133
134    @Override
135    public int hashCode() {
136      return Objects.hash(startRow, stopRow);
137    }
138
139    @Override
140    public String toString() {
141      return org.apache.hbase.thirdparty.com.google.common.base.MoreObjects.toStringHelper(this)
142        .add("startRow", startRow).add("stopRow", stopRow).toString();
143    }
144  }
145
146  @Test
147  public void testSetInputSetsSnapshotToScans() throws Exception {
148
149    callSetInput();
150
151    Map<String, Collection<Scan>> actual = subject.getSnapshotsToScans(conf);
152
153    // convert to scans we can use .equals on
154    Map<String, Collection<ScanWithEquals>> actualWithEquals = toScanWithEquals(actual);
155    Map<String, Collection<ScanWithEquals>> expectedWithEquals = toScanWithEquals(snapshotScans);
156
157    assertEquals(expectedWithEquals, actualWithEquals);
158  }
159
160  @Test
161  public void testSetInputPushesRestoreDirectories() throws Exception {
162    callSetInput();
163
164    Map<String, Path> restoreDirs = subject.getSnapshotDirs(conf);
165
166    assertEquals(this.snapshotScans.keySet(), restoreDirs.keySet());
167  }
168
169  @Test
170  public void testSetInputCreatesRestoreDirectoriesUnderRootRestoreDir() throws Exception {
171    callSetInput();
172
173    Map<String, Path> restoreDirs = subject.getSnapshotDirs(conf);
174
175    for (Path snapshotDir : restoreDirs.values()) {
176      assertEquals("Expected " + snapshotDir + " to be a child of " + restoreDir, restoreDir,
177        snapshotDir.getParent());
178    }
179  }
180
181  @Test
182  public void testSetInputRestoresSnapshots() throws Exception {
183    callSetInput();
184
185    Map<String, Path> snapshotDirs = subject.getSnapshotDirs(conf);
186
187    for (Map.Entry<String, Path> entry : snapshotDirs.entrySet()) {
188      verify(this.subject).restoreSnapshot(eq(this.conf), eq(entry.getKey()), eq(this.rootDir),
189        eq(entry.getValue()), any());
190    }
191  }
192}