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(Bytes.toBytes("1"), Bytes.toBytes("2"))), "snapshot2",
081      ImmutableList.of(new Scan(Bytes.toBytes("3"), Bytes.toBytes("4")),
082        new Scan(Bytes.toBytes("5"), Bytes.toBytes("6"))));
083
084    this.restoreDir = new Path(CommonFSUtils.getRootDir(conf), "restore-dir");
085
086  }
087
088  public void callSetInput() throws IOException {
089    subject.setInput(this.conf, snapshotScans, restoreDir);
090  }
091
092  public Map<String, Collection<ScanWithEquals>>
093    toScanWithEquals(Map<String, Collection<Scan>> snapshotScans) throws IOException {
094    Map<String, Collection<ScanWithEquals>> rtn = Maps.newHashMap();
095
096    for (Map.Entry<String, Collection<Scan>> entry : snapshotScans.entrySet()) {
097      List<ScanWithEquals> scans = Lists.newArrayList();
098
099      for (Scan scan : entry.getValue()) {
100        scans.add(new ScanWithEquals(scan));
101      }
102      rtn.put(entry.getKey(), scans);
103    }
104
105    return rtn;
106  }
107
108  public static class ScanWithEquals {
109
110    private final String startRow;
111    private final String stopRow;
112
113    /**
114     * Creates a new instance of this class while copying all values.
115     * @param scan The scan instance to copy from.
116     * @throws java.io.IOException When copying the values fails.
117     */
118    public ScanWithEquals(Scan scan) throws IOException {
119      this.startRow = Bytes.toStringBinary(scan.getStartRow());
120      this.stopRow = Bytes.toStringBinary(scan.getStopRow());
121    }
122
123    @Override
124    public boolean equals(Object obj) {
125      if (!(obj instanceof ScanWithEquals)) {
126        return false;
127      }
128      ScanWithEquals otherScan = (ScanWithEquals) obj;
129      return Objects.equals(this.startRow, otherScan.startRow)
130        && Objects.equals(this.stopRow, otherScan.stopRow);
131    }
132
133    @Override
134    public int hashCode() {
135      return Objects.hash(startRow, stopRow);
136    }
137
138    @Override
139    public String toString() {
140      return org.apache.hbase.thirdparty.com.google.common.base.MoreObjects.toStringHelper(this)
141        .add("startRow", startRow).add("stopRow", stopRow).toString();
142    }
143  }
144
145  @Test
146  public void testSetInputSetsSnapshotToScans() throws Exception {
147
148    callSetInput();
149
150    Map<String, Collection<Scan>> actual = subject.getSnapshotsToScans(conf);
151
152    // convert to scans we can use .equals on
153    Map<String, Collection<ScanWithEquals>> actualWithEquals = toScanWithEquals(actual);
154    Map<String, Collection<ScanWithEquals>> expectedWithEquals = toScanWithEquals(snapshotScans);
155
156    assertEquals(expectedWithEquals, actualWithEquals);
157  }
158
159  @Test
160  public void testSetInputPushesRestoreDirectories() throws Exception {
161    callSetInput();
162
163    Map<String, Path> restoreDirs = subject.getSnapshotDirs(conf);
164
165    assertEquals(this.snapshotScans.keySet(), restoreDirs.keySet());
166  }
167
168  @Test
169  public void testSetInputCreatesRestoreDirectoriesUnderRootRestoreDir() throws Exception {
170    callSetInput();
171
172    Map<String, Path> restoreDirs = subject.getSnapshotDirs(conf);
173
174    for (Path snapshotDir : restoreDirs.values()) {
175      assertEquals("Expected " + snapshotDir + " to be a child of " + restoreDir, restoreDir,
176        snapshotDir.getParent());
177    }
178  }
179
180  @Test
181  public void testSetInputRestoresSnapshots() throws Exception {
182    callSetInput();
183
184    Map<String, Path> snapshotDirs = subject.getSnapshotDirs(conf);
185
186    for (Map.Entry<String, Path> entry : snapshotDirs.entrySet()) {
187      verify(this.subject).restoreSnapshot(eq(this.conf), eq(entry.getKey()), eq(this.rootDir),
188        eq(entry.getValue()), any());
189    }
190  }
191}