liuml07 commented on a change in pull request #1583: URL: https://github.com/apache/hbase/pull/1583#discussion_r414972976
########## 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"; Review comment: Replace `hbase.it.shellexeccoproc.async.delay` with `hbase.it.shellexeccoproc.async.delay.ms` so we know this is for milliseconds? ########## File path: hbase-it/src/test/java/org/apache/hadoop/hbase/TestShellExecCoproc.java ########## @@ -0,0 +1,130 @@ +/* + * 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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Optional; +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.client.AsyncAdmin; +import org.apache.hadoop.hbase.client.AsyncConnection; +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.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecService; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +/** + * Test for the {@link ShellExecCoproc}. + */ +@Category(MediumTests.class) +public class TestShellExecCoproc { + + @ClassRule + public static final HBaseClassTestRule testRule = + HBaseClassTestRule.forClass(TestShellExecCoproc.class); + + @ClassRule + public static final MiniClusterRule miniClusterRule = MiniClusterRule.newBuilder() + .setConfiguration(createConfiguration()) + .build(); + + @Rule + public final ConnectionRule connectionRule = + new ConnectionRule(miniClusterRule::createConnection); + + @Test + public void testShellExec() { + final AsyncConnection conn = connectionRule.getConnection(); + final AsyncAdmin admin = conn.getAdmin(); + + final String command = "echo -n \"hello world\""; + final ShellExecRequest req = ShellExecRequest.newBuilder() + .setCommand(command) + .build(); + final ShellExecResponse resp = admin + .<ShellExecService.Stub, ShellExecResponse>coprocessorService( + ShellExecService::newStub, + (stub, controller, callback) -> stub.shellExec(controller, req, callback)) + .join(); + assertEquals(0, resp.getExitCode()); + assertEquals("hello world", resp.getStdout()); + } + + @Test + public void testShellExecAsync() throws IOException { + final AsyncConnection conn = connectionRule.getConnection(); + final AsyncAdmin admin = conn.getAdmin(); + + final File testDataDir = ensureTestDataDirExists(miniClusterRule.getTestingUtility()); + final File testFile = new File(testDataDir, "shell_exec_async.txt"); + assertTrue(testFile.createNewFile()); + assertEquals(0, testFile.length()); + + final String command = "echo \"hello world\" >> " + testFile.getAbsolutePath(); + final ShellExecRequest req = ShellExecRequest.newBuilder() + .setCommand(command) + .build(); + admin.<ShellExecService.Stub, ShellExecResponse>coprocessorService( + ShellExecService::newStub, + (stub, controller, callback) -> stub.shellExecAsync(controller, req, callback)) + .join(); + + Waiter.waitFor(conn.getConfiguration(), 5_000, () -> testFile.length() > 0); Review comment: This seems a good idea. Just concerned both `testFile` and `cat` can be empty, so... ########## File path: hbase-it/src/test/java/org/apache/hadoop/hbase/CoprocClusterManager.java ########## @@ -0,0 +1,118 @@ +/* + * 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 java.io.IOException; +import java.util.Objects; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.hbase.client.AsyncAdmin; +import org.apache.hadoop.hbase.client.AsyncConnection; +import org.apache.hadoop.hbase.client.ConnectionFactory; +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.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecService; +import org.apache.hadoop.hbase.util.Pair; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Overrides commands to make use of Coproc where possible. + */ [email protected] +public class CoprocClusterManager extends HBaseClusterManager { + private static final Logger LOG = LoggerFactory.getLogger(CoprocClusterManager.class); + + @Override + protected Pair<Integer, String> exec(String hostname, ServiceType service, String... cmd) + throws IOException { + // we don't have ssh to log in as the service user, so wrap the command in sudo. + // assumes user running the hbase process has sudo privileges to target service. + final String serviceUser = getServiceUser(service); + final String[] commandPrefix = + !Objects.equals(serviceUser, getServiceUser(ServiceType.HBASE_MASTER)) + ? new String[] { "sudo", "-u", serviceUser } + : new String[0]; + final String command = StringUtils.join(ArrayUtils.addAll(commandPrefix, cmd), " "); + LOG.info("Executing remote command: {}, hostname:{}", command, hostname); + + try (final AsyncConnection conn = ConnectionFactory.createAsyncConnection(getConf()).join()) { + final AsyncAdmin admin = conn.getAdmin(); + final ShellExecRequest req = ShellExecRequest.newBuilder() + .setCommand(command) + .build(); + + final ShellExecResponse resp; + switch(service) { + case HBASE_MASTER: + // TODO: what happens if the intended action was killing a backup master? + resp = masterExec(admin, req); + break; + case HBASE_REGIONSERVER: + final ServerName targetHost = resolveRegionServerName(admin, hostname); + resp = regionServerExec(admin, req, targetHost); + break; + default: + throw unsupportedServiceType(service); + } + + LOG.info("Executed remote command, exit code:{} , output:{}", resp.getExitCode(), Review comment: Do we need the command again? Otherwise the output and exit code may not be related to command easily. ``` LOG.info("Executed remote command: '{}', exit code: {}, output: {}", command, ...) ``` ########## File path: hbase-endpoint/src/main/protobuf/ShellExecEndpoint.proto ########## @@ -0,0 +1,42 @@ +/** Review comment: nit: replace `/**` with `/*` ########## File path: hbase-it/src/test/java/org/apache/hadoop/hbase/TestShellExecCoproc.java ########## @@ -0,0 +1,130 @@ +/* + * 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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Optional; +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.client.AsyncAdmin; +import org.apache.hadoop.hbase.client.AsyncConnection; +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.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecService; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +/** + * Test for the {@link ShellExecCoproc}. + */ +@Category(MediumTests.class) +public class TestShellExecCoproc { + + @ClassRule + public static final HBaseClassTestRule testRule = + HBaseClassTestRule.forClass(TestShellExecCoproc.class); + + @ClassRule + public static final MiniClusterRule miniClusterRule = MiniClusterRule.newBuilder() + .setConfiguration(createConfiguration()) + .build(); + + @Rule + public final ConnectionRule connectionRule = + new ConnectionRule(miniClusterRule::createConnection); + + @Test + public void testShellExec() { + final AsyncConnection conn = connectionRule.getConnection(); + final AsyncAdmin admin = conn.getAdmin(); + + final String command = "echo -n \"hello world\""; + final ShellExecRequest req = ShellExecRequest.newBuilder() + .setCommand(command) + .build(); + final ShellExecResponse resp = admin + .<ShellExecService.Stub, ShellExecResponse>coprocessorService( + ShellExecService::newStub, + (stub, controller, callback) -> stub.shellExec(controller, req, callback)) + .join(); + assertEquals(0, resp.getExitCode()); + assertEquals("hello world", resp.getStdout()); + } + + @Test + public void testShellExecAsync() throws IOException { + final AsyncConnection conn = connectionRule.getConnection(); + final AsyncAdmin admin = conn.getAdmin(); + + final File testDataDir = ensureTestDataDirExists(miniClusterRule.getTestingUtility()); + final File testFile = new File(testDataDir, "shell_exec_async.txt"); + assertTrue(testFile.createNewFile()); + assertEquals(0, testFile.length()); + + final String command = "echo \"hello world\" >> " + testFile.getAbsolutePath(); + final ShellExecRequest req = ShellExecRequest.newBuilder() + .setCommand(command) + .build(); + admin.<ShellExecService.Stub, ShellExecResponse>coprocessorService( + ShellExecService::newStub, + (stub, controller, callback) -> stub.shellExecAsync(controller, req, callback)) + .join(); + + Waiter.waitFor(conn.getConfiguration(), 5_000, () -> testFile.length() > 0); + final String content = Optional.of(testFile.toPath()) Review comment: By ``` final String content = Optional.of(testFile.toPath()) .map(val -> { try { return Files.readAllLines(val); } catch (IOException e) { return null; } }) .map(val -> StringUtils.join(val, "")) .map(String::trim) .orElseThrow(() -> new RuntimeException("Unable to parse file content")); assertEquals("hello world", content); ``` Do you mean: ``` final String content = new String(Files.readAllBytes(testFile.toPath())); assertEquals("hello world\n", content); ``` ########## File path: hbase-it/src/test/java/org/apache/hadoop/hbase/CoprocClusterManager.java ########## @@ -0,0 +1,118 @@ +/* + * 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 java.io.IOException; +import java.util.Objects; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.hbase.client.AsyncAdmin; +import org.apache.hadoop.hbase.client.AsyncConnection; +import org.apache.hadoop.hbase.client.ConnectionFactory; +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.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecService; +import org.apache.hadoop.hbase.util.Pair; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Overrides commands to make use of Coproc where possible. + */ [email protected] +public class CoprocClusterManager extends HBaseClusterManager { + private static final Logger LOG = LoggerFactory.getLogger(CoprocClusterManager.class); + + @Override + protected Pair<Integer, String> exec(String hostname, ServiceType service, String... cmd) + throws IOException { + // we don't have ssh to log in as the service user, so wrap the command in sudo. + // assumes user running the hbase process has sudo privileges to target service. + final String serviceUser = getServiceUser(service); + final String[] commandPrefix = + !Objects.equals(serviceUser, getServiceUser(ServiceType.HBASE_MASTER)) Review comment: It's possible because `HBASE_REGIONSERVER` results in `new String[0]`? ---------------------------------------------------------------- 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]
