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.jupiter.api.Assertions.assertEquals;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.List;
026import java.util.Optional;
027import org.apache.hadoop.conf.Configurable;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.fs.FSDataOutputStream;
030import org.apache.hadoop.fs.FileSystem;
031import org.apache.hadoop.fs.Path;
032import org.apache.hadoop.hbase.HBaseTestingUtil;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.client.Durability;
035import org.apache.hadoop.hbase.client.Put;
036import org.apache.hadoop.hbase.coprocessor.ObserverContext;
037import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
038import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
039import org.apache.hadoop.hbase.coprocessor.RegionObserver;
040import org.apache.hadoop.hbase.regionserver.Region;
041import org.apache.hadoop.hbase.testclassification.LargeTests;
042import org.apache.hadoop.hbase.testclassification.MapReduceTests;
043import org.apache.hadoop.hbase.util.Bytes;
044import org.apache.hadoop.hbase.wal.WALEdit;
045import org.apache.hadoop.util.Tool;
046import org.apache.hadoop.util.ToolRunner;
047import org.junit.jupiter.api.AfterAll;
048import org.junit.jupiter.api.BeforeAll;
049import org.junit.jupiter.api.Tag;
050import org.junit.jupiter.api.Test;
051import org.junit.jupiter.api.TestInfo;
052import org.slf4j.Logger;
053import org.slf4j.LoggerFactory;
054
055@Tag(MapReduceTests.TAG)
056@Tag(LargeTests.TAG)
057public class TestImportTSVWithTTLs implements Configurable {
058
059  protected static final Logger LOG = LoggerFactory.getLogger(TestImportTSVWithTTLs.class);
060  protected static final String NAME = TestImportTsv.class.getSimpleName();
061  protected static HBaseTestingUtil util = new HBaseTestingUtil();
062
063  /**
064   * Delete the tmp directory after running doMROnTableTest. Boolean. Default is false.
065   */
066  protected static final String DELETE_AFTER_LOAD_CONF = NAME + ".deleteAfterLoad";
067
068  /**
069   * Force use of combiner in doMROnTableTest. Boolean. Default is true.
070   */
071  protected static final String FORCE_COMBINER_CONF = NAME + ".forceCombiner";
072
073  private final String FAMILY = "FAM";
074  private static Configuration conf;
075
076  @Override
077  public Configuration getConf() {
078    return util.getConfiguration();
079  }
080
081  @Override
082  public void setConf(Configuration conf) {
083    throw new IllegalArgumentException("setConf not supported");
084  }
085
086  @BeforeAll
087  public static void provisionCluster() throws Exception {
088    conf = util.getConfiguration();
089    // We don't check persistence in HFiles in this test, but if we ever do we will
090    // need this where the default hfile version is not 3 (i.e. 0.98)
091    conf.setInt("hfile.format.version", 3);
092    conf.set("hbase.coprocessor.region.classes", TTLCheckingObserver.class.getName());
093    util.startMiniCluster();
094  }
095
096  @AfterAll
097  public static void releaseCluster() throws Exception {
098    util.shutdownMiniCluster();
099  }
100
101  @Test
102  public void testMROnTable(TestInfo testInfo) throws Exception {
103    final TableName tableName =
104      TableName.valueOf(testInfo.getTestMethod().get().getName() + util.getRandomUUID());
105
106    // Prepare the arguments required for the test.
107    String[] args = new String[] {
108      "-D" + ImportTsv.MAPPER_CONF_KEY + "=org.apache.hadoop.hbase.mapreduce.TsvImporterMapper",
109      "-D" + ImportTsv.COLUMNS_CONF_KEY + "=HBASE_ROW_KEY,FAM:A,FAM:B,HBASE_CELL_TTL",
110      "-D" + ImportTsv.SEPARATOR_CONF_KEY + "=\u001b", tableName.getNameAsString() };
111    String data = "KEY\u001bVALUE1\u001bVALUE2\u001b1000000\n";
112    util.createTable(tableName, FAMILY);
113    doMROnTableTest(util, FAMILY, data, args, 1);
114    util.deleteTable(tableName);
115  }
116
117  protected static Tool doMROnTableTest(HBaseTestingUtil util, String family, String data,
118    String[] args, int valueMultiplier) throws Exception {
119    TableName table = TableName.valueOf(args[args.length - 1]);
120    Configuration conf = new Configuration(util.getConfiguration());
121
122    // populate input file
123    FileSystem fs = FileSystem.get(conf);
124    Path inputPath =
125      fs.makeQualified(new Path(util.getDataTestDirOnTestFS(table.getNameAsString()), "input.dat"));
126    FSDataOutputStream op = fs.create(inputPath, true);
127    op.write(Bytes.toBytes(data));
128    op.close();
129    LOG.debug(String.format("Wrote test data to file: %s", inputPath));
130
131    if (conf.getBoolean(FORCE_COMBINER_CONF, true)) {
132      LOG.debug("Forcing combiner.");
133      conf.setInt("mapreduce.map.combine.minspills", 1);
134    }
135
136    // run the import
137    List<String> argv = new ArrayList<>(Arrays.asList(args));
138    argv.add(inputPath.toString());
139    Tool tool = new ImportTsv();
140    LOG.debug("Running ImportTsv with arguments: " + argv);
141    try {
142      // Job will fail if observer rejects entries without TTL
143      assertEquals(0, ToolRunner.run(conf, tool, argv.toArray(args)));
144    } finally {
145      // Clean up
146      if (conf.getBoolean(DELETE_AFTER_LOAD_CONF, true)) {
147        LOG.debug("Deleting test subdirectory");
148        util.cleanupDataTestDirOnTestFS(table.getNameAsString());
149      }
150    }
151
152    return tool;
153  }
154
155  public static class TTLCheckingObserver implements RegionCoprocessor, RegionObserver {
156
157    @Override
158    public Optional<RegionObserver> getRegionObserver() {
159      return Optional.of(this);
160    }
161
162    @Override
163    public void prePut(ObserverContext<? extends RegionCoprocessorEnvironment> e, Put put,
164      WALEdit edit, Durability durability) throws IOException {
165      Region region = e.getEnvironment().getRegion();
166      if (
167        !region.getRegionInfo().isMetaRegion() && !region.getRegionInfo().getTable().isSystemTable()
168      ) {
169        // The put carries the TTL attribute
170        if (put.getTTL() != Long.MAX_VALUE) {
171          return;
172        }
173        throw new IOException("Operation does not have TTL set");
174      }
175    }
176  }
177}