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.regionserver;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.List;
023import java.util.concurrent.atomic.AtomicLong;
024import org.apache.hadoop.fs.FileSystem;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.MultithreadedTestUtil.RepeatingTestThread;
029import org.apache.hadoop.hbase.MultithreadedTestUtil.TestContext;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.ClientServiceCallable;
032import org.apache.hadoop.hbase.client.ClusterConnection;
033import org.apache.hadoop.hbase.client.RpcRetryingCaller;
034import org.apache.hadoop.hbase.client.RpcRetryingCallerFactory;
035import org.apache.hadoop.hbase.client.Table;
036import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
037import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
038import org.apache.hadoop.hbase.testclassification.LargeTests;
039import org.apache.hadoop.hbase.testclassification.RegionServerTests;
040import org.apache.hadoop.hbase.util.Bytes;
041import org.apache.hadoop.hbase.util.Pair;
042import org.junit.BeforeClass;
043import org.junit.ClassRule;
044import org.junit.Ignore;
045import org.junit.experimental.categories.Category;
046import org.junit.runner.RunWith;
047import org.junit.runners.Parameterized;
048import org.slf4j.Logger;
049import org.slf4j.LoggerFactory;
050
051import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
052
053import org.apache.hadoop.hbase.shaded.protobuf.RequestConverter;
054import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos;
055import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.CompactRegionRequest;
056
057/**
058 * Tests bulk loading of HFiles with old secure Endpoint client for backward compatibility. Will be
059 * removed when old non-secure client for backward compatibility is not supported.
060 */
061@RunWith(Parameterized.class)
062@Category({RegionServerTests.class, LargeTests.class})
063@Ignore // BROKEN. FIX OR REMOVE.
064public class TestHRegionServerBulkLoadWithOldSecureEndpoint extends TestHRegionServerBulkLoad {
065
066  @ClassRule
067  public static final HBaseClassTestRule CLASS_RULE =
068      HBaseClassTestRule.forClass(TestHRegionServerBulkLoadWithOldSecureEndpoint.class);
069
070  public TestHRegionServerBulkLoadWithOldSecureEndpoint(int duration) {
071    super(duration);
072  }
073
074  private static final Logger LOG =
075      LoggerFactory.getLogger(TestHRegionServerBulkLoadWithOldSecureEndpoint.class);
076
077  @BeforeClass
078  public static void setUpBeforeClass() throws IOException {
079    conf.setInt("hbase.rpc.timeout", 10 * 1000);
080    conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
081      "org.apache.hadoop.hbase.security.access.SecureBulkLoadEndpoint");
082  }
083
084  public static class AtomicHFileLoader extends RepeatingTestThread {
085    final AtomicLong numBulkLoads = new AtomicLong();
086    final AtomicLong numCompactions = new AtomicLong();
087    private TableName tableName;
088
089    public AtomicHFileLoader(TableName tableName, TestContext ctx,
090        byte targetFamilies[][]) throws IOException {
091      super(ctx);
092      this.tableName = tableName;
093    }
094
095    public void doAnAction() throws Exception {
096      long iteration = numBulkLoads.getAndIncrement();
097      Path dir =  UTIL.getDataTestDirOnTestFS(String.format("bulkLoad_%08d",
098          iteration));
099
100      // create HFiles for different column families
101      FileSystem fs = UTIL.getTestFileSystem();
102      byte[] val = Bytes.toBytes(String.format("%010d", iteration));
103      final List<Pair<byte[], String>> famPaths = new ArrayList<>(NUM_CFS);
104      for (int i = 0; i < NUM_CFS; i++) {
105        Path hfile = new Path(dir, family(i));
106        byte[] fam = Bytes.toBytes(family(i));
107        createHFile(fs, hfile, fam, QUAL, val, 1000);
108        famPaths.add(new Pair<>(fam, hfile.toString()));
109      }
110
111      // bulk load HFiles
112      final ClusterConnection conn = (ClusterConnection) UTIL.getAdmin().getConnection();
113      Table table = conn.getTable(tableName);
114      final String bulkToken = new SecureBulkLoadEndpointClient(table).prepareBulkLoad(tableName);
115      RpcControllerFactory rpcControllerFactory = new RpcControllerFactory(UTIL.getConfiguration());
116      ClientServiceCallable<Void> callable =
117          new ClientServiceCallable<Void>(conn, tableName, Bytes.toBytes("aaa"),
118              rpcControllerFactory.newController(), HConstants.PRIORITY_UNSET) {
119            @Override
120            protected Void rpcCall() throws Exception {
121              LOG.debug("Going to connect to server " + getLocation() + " for row " +
122                  Bytes.toStringBinary(getRow()));
123              try (Table table = conn.getTable(getTableName())) {
124                boolean loaded = new SecureBulkLoadEndpointClient(table).bulkLoadHFiles(famPaths,
125                    null, bulkToken, getLocation().getRegionInfo().getStartKey());
126              }
127              return null;
128            }
129          };
130      RpcRetryingCallerFactory factory = new RpcRetryingCallerFactory(conf);
131      RpcRetryingCaller<Void> caller = factory.<Void> newCaller();
132      caller.callWithRetries(callable, Integer.MAX_VALUE);
133
134      // Periodically do compaction to reduce the number of open file handles.
135      if (numBulkLoads.get() % 5 == 0) {
136        // 5 * 50 = 250 open file handles!
137        callable = new ClientServiceCallable<Void>(conn, tableName, Bytes.toBytes("aaa"),
138            rpcControllerFactory.newController(), HConstants.PRIORITY_UNSET) {
139          @Override
140          protected Void rpcCall() throws Exception {
141            LOG.debug("compacting " + getLocation() + " for row "
142                + Bytes.toStringBinary(getRow()));
143            AdminProtos.AdminService.BlockingInterface server =
144              conn.getAdmin(getLocation().getServerName());
145            CompactRegionRequest request =
146              RequestConverter.buildCompactRegionRequest(
147                getLocation().getRegionInfo().getRegionName(), true, null);
148            server.compactRegion(null, request);
149            numCompactions.incrementAndGet();
150            return null;
151          }
152        };
153        caller.callWithRetries(callable, Integer.MAX_VALUE);
154      }
155    }
156  }
157
158  void runAtomicBulkloadTest(TableName tableName, int millisToRun, int numScanners)
159      throws Exception {
160    setupTable(tableName, 10);
161
162    TestContext ctx = new TestContext(UTIL.getConfiguration());
163
164    AtomicHFileLoader loader = new AtomicHFileLoader(tableName, ctx, null);
165    ctx.addThread(loader);
166
167    List<AtomicScanReader> scanners = Lists.newArrayList();
168    for (int i = 0; i < numScanners; i++) {
169      AtomicScanReader scanner = new AtomicScanReader(tableName, ctx, families);
170      scanners.add(scanner);
171      ctx.addThread(scanner);
172    }
173
174    ctx.startThreads();
175    ctx.waitFor(millisToRun);
176    ctx.stop();
177
178    LOG.info("Loaders:");
179    LOG.info("  loaded " + loader.numBulkLoads.get());
180    LOG.info("  compations " + loader.numCompactions.get());
181
182    LOG.info("Scanners:");
183    for (AtomicScanReader scanner : scanners) {
184      LOG.info("  scanned " + scanner.numScans.get());
185      LOG.info("  verified " + scanner.numRowsScanned.get() + " rows");
186    }
187  }
188}