ndimiduk commented on a change in pull request #1583: URL: https://github.com/apache/hbase/pull/1583#discussion_r417646021
########## File path: hbase-it/src/test/java/org/apache/hadoop/hbase/ShellExecCoproc.java ########## @@ -0,0 +1,149 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase; + +import com.google.protobuf.RpcCallback; +import com.google.protobuf.RpcController; +import com.google.protobuf.Service; +import java.io.IOException; +import java.util.Collections; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.coprocessor.MasterCoprocessor; +import org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessor; +import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint; +import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecRequest; +import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecResponse; +import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils; +import org.apache.hadoop.util.Shell; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder; + +/** + * Receives shell commands from the client and executes them blindly. + */ [email protected] +public class ShellExecCoproc + extends ShellExecEndpoint.ShellExecService + implements MasterCoprocessor, RegionServerCoprocessor { + private static final Logger LOG = LoggerFactory.getLogger(ShellExecCoproc.class); + + public static final String ASYNC_DELAY_KEY = "hbase.it.shellexeccoproc.async.delay"; + public static final long DEFAULT_ASYNC_DELAY = 1_000; + + private final ExecutorService workerPool; + private Configuration conf; + + public ShellExecCoproc() { + workerPool = Executors.newSingleThreadExecutor( + new ThreadFactoryBuilder() + .setNameFormat(ShellExecCoproc.class.getSimpleName() + "-{}") + .setDaemon(true) + .setUncaughtExceptionHandler((t, e) -> LOG.warn("Thread {} threw", t, e)) + .build()); + } + + @Override + public Iterable<Service> getServices() { + return Collections.singletonList(this); + } + + @Override + public void start(CoprocessorEnvironment env) { + conf = env.getConfiguration(); + } + + @Override + public void shellExec( + final RpcController controller, + final ShellExecRequest request, + final RpcCallback<ShellExecResponse> done + ) { + final Shell.ShellCommandExecutor shell = prepareShell(request, false); + + ShellExecResponse.Builder builder = ShellExecResponse.newBuilder(); + try { + builder = doExec(shell, builder); + } catch (IOException e) { + LOG.info("Failure launching process", e); + CoprocessorRpcUtils.setControllerException(controller, e); + } + + done.run(builder.build()); + } + + @Override + public void shellExecAsync( + final RpcController controller, + final ShellExecRequest request, + final RpcCallback<ShellExecResponse> done + ) { + final Shell.ShellCommandExecutor shell = prepareShell(request, true); + final long sleepDuration = conf.getLong(ASYNC_DELAY_KEY, DEFAULT_ASYNC_DELAY); + workerPool.submit(() -> { + try { + // sleep first so that the RPC can ACK. Review comment: I've adjusted names and added comments. Please let me know if the confusion persists. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
