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 */
018
019package org.apache.hadoop.hbase.regionserver;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.apache.hadoop.hbase.client.Table;
030import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel;
031import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils;
032import org.apache.hadoop.hbase.ipc.ServerRpcController;
033import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
034import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
035import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.CleanupBulkLoadRequest;
036import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.CleanupBulkLoadResponse;
037import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.DelegationToken;
038import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.PrepareBulkLoadRequest;
039import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.PrepareBulkLoadResponse;
040import org.apache.hadoop.hbase.protobuf.generated.SecureBulkLoadProtos;
041import org.apache.hadoop.hbase.util.ByteStringer;
042import org.apache.hadoop.hbase.util.Pair;
043import org.apache.hadoop.security.token.Token;
044
045/**
046 * Client proxy for SecureBulkLoadProtocol used in conjunction with SecureBulkLoadEndpoint
047 * @deprecated Use for backward compatibility testing only. Will be removed when
048 *             SecureBulkLoadEndpoint is not supported.
049 */
050@InterfaceAudience.Private
051public class SecureBulkLoadEndpointClient {
052  private Table table;
053
054  public SecureBulkLoadEndpointClient(Table table) {
055    this.table = table;
056  }
057
058  public String prepareBulkLoad(final TableName tableName) throws IOException {
059    try {
060      CoprocessorRpcChannel channel = table.coprocessorService(HConstants.EMPTY_START_ROW);
061      SecureBulkLoadProtos.SecureBulkLoadService instance =
062          ProtobufUtil.newServiceStub(SecureBulkLoadProtos.SecureBulkLoadService.class, channel);
063
064      ServerRpcController controller = new ServerRpcController();
065
066      CoprocessorRpcUtils.BlockingRpcCallback<PrepareBulkLoadResponse> rpcCallback =
067          new CoprocessorRpcUtils.BlockingRpcCallback<>();
068
069      PrepareBulkLoadRequest request =
070          PrepareBulkLoadRequest.newBuilder()
071          .setTableName(ProtobufUtil.toProtoTableName(tableName)).build();
072
073      instance.prepareBulkLoad(controller, request, rpcCallback);
074
075      PrepareBulkLoadResponse response = rpcCallback.get();
076      if (controller.failedOnException()) {
077        throw controller.getFailedOn();
078      }
079
080      return response.getBulkToken();
081    } catch (Throwable throwable) {
082      throw new IOException(throwable);
083    }
084  }
085
086  public void cleanupBulkLoad(final String bulkToken) throws IOException {
087    try {
088      CoprocessorRpcChannel channel = table.coprocessorService(HConstants.EMPTY_START_ROW);
089      SecureBulkLoadProtos.SecureBulkLoadService instance =
090          ProtobufUtil.newServiceStub(SecureBulkLoadProtos.SecureBulkLoadService.class, channel);
091
092      ServerRpcController controller = new ServerRpcController();
093
094      CoprocessorRpcUtils.BlockingRpcCallback<CleanupBulkLoadResponse> rpcCallback =
095          new CoprocessorRpcUtils.BlockingRpcCallback<>();
096
097      CleanupBulkLoadRequest request =
098          CleanupBulkLoadRequest.newBuilder()
099              .setBulkToken(bulkToken).build();
100
101      instance.cleanupBulkLoad(controller,
102          request,
103          rpcCallback);
104
105      if (controller.failedOnException()) {
106        throw controller.getFailedOn();
107      }
108    } catch (Throwable throwable) {
109      throw new IOException(throwable);
110    }
111  }
112
113  public boolean bulkLoadHFiles(final List<Pair<byte[], String>> familyPaths,
114                         final Token<?> userToken,
115                         final String bulkToken,
116                         final byte[] startRow) throws IOException {
117    // we never want to send a batch of HFiles to all regions, thus cannot call
118    // HTable#coprocessorService methods that take start and end rowkeys; see HBASE-9639
119    try {
120      CoprocessorRpcChannel channel = table.coprocessorService(startRow);
121      SecureBulkLoadProtos.SecureBulkLoadService instance =
122          ProtobufUtil.newServiceStub(SecureBulkLoadProtos.SecureBulkLoadService.class, channel);
123
124      DelegationToken protoDT =
125          DelegationToken.newBuilder().build();
126      if(userToken != null) {
127        protoDT =
128            DelegationToken.newBuilder()
129              .setIdentifier(ByteStringer.wrap(userToken.getIdentifier()))
130              .setPassword(ByteStringer.wrap(userToken.getPassword()))
131              .setKind(userToken.getKind().toString())
132              .setService(userToken.getService().toString()).build();
133      }
134
135      List<ClientProtos.BulkLoadHFileRequest.FamilyPath> protoFamilyPaths =
136          new ArrayList<>(familyPaths.size());
137      for(Pair<byte[], String> el: familyPaths) {
138        protoFamilyPaths.add(ClientProtos.BulkLoadHFileRequest.FamilyPath.newBuilder()
139          .setFamily(ByteStringer.wrap(el.getFirst()))
140          .setPath(el.getSecond()).build());
141      }
142
143      SecureBulkLoadProtos.SecureBulkLoadHFilesRequest request =
144          SecureBulkLoadProtos.SecureBulkLoadHFilesRequest.newBuilder()
145            .setFsToken(protoDT)
146            .addAllFamilyPath(protoFamilyPaths)
147            .setBulkToken(bulkToken).build();
148
149      ServerRpcController controller = new ServerRpcController();
150      CoprocessorRpcUtils.BlockingRpcCallback<SecureBulkLoadProtos.SecureBulkLoadHFilesResponse>
151            rpcCallback = new CoprocessorRpcUtils.BlockingRpcCallback<>();
152      instance.secureBulkLoadHFiles(controller,
153        request,
154        rpcCallback);
155
156      SecureBulkLoadProtos.SecureBulkLoadHFilesResponse response = rpcCallback.get();
157      if (controller.failedOnException()) {
158        throw controller.getFailedOn();
159      }
160      return response.getLoaded();
161    } catch (Throwable throwable) {
162      throw new IOException(throwable);
163    }
164  }
165
166}