use of org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecResponse in project hbase by apache.
the class CoprocClusterManager method exec.
@Override
protected Pair<Integer, String> exec(String hostname, ServiceType service, String... cmd) throws IOException {
if (!supportedServices.contains(service)) {
throw unsupportedServiceType(service);
}
// We only support actions vs. Master or Region Server processes. We're issuing those actions
// via the coprocessor that's running within those processes. Thus, there's no support for
// honoring the configured service user.
final String command = StringUtils.join(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).setAwaitResponse(false).build();
final ShellExecResponse resp;
switch(service) {
case HBASE_MASTER:
// What happens if the intended action was killing a backup master? Right now we have
// no `RestartBackupMasterAction` so it's probably fine.
resp = masterExec(admin, req);
break;
case HBASE_REGIONSERVER:
final ServerName targetHost = resolveRegionServerName(admin, hostname);
resp = regionServerExec(admin, req, targetHost);
break;
default:
throw new RuntimeException("should not happen");
}
if (LOG.isDebugEnabled()) {
LOG.debug("Executed remote command: {}, exit code:{} , output:{}", command, resp.getExitCode(), resp.getStdout());
} else {
LOG.info("Executed remote command: {}, exit code:{}", command, resp.getExitCode());
}
return new Pair<>(resp.getExitCode(), resp.getStdout());
}
}
use of org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecResponse in project hbase by apache.
the class TestShellExecEndpointCoprocessor method testShellExecForeground.
private void testShellExecForeground(final Consumer<ShellExecRequest.Builder> consumer) {
final AsyncConnection conn = connectionRule.getConnection();
final AsyncAdmin admin = conn.getAdmin();
final String command = "echo -n \"hello world\"";
final ShellExecRequest.Builder builder = ShellExecRequest.newBuilder().setCommand(command);
consumer.accept(builder);
final ShellExecResponse resp = admin.<ShellExecService.Stub, ShellExecResponse>coprocessorService(ShellExecService::newStub, (stub, controller, callback) -> stub.shellExec(controller, builder.build(), callback)).join();
assertEquals(0, resp.getExitCode());
assertEquals("hello world", resp.getStdout());
}
use of org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecResponse in project hbase by apache.
the class TestShellExecEndpointCoprocessor method testShellExecBackground.
@Test
public void testShellExecBackground() 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_background.txt");
assertTrue(testFile.createNewFile());
assertEquals(0, testFile.length());
final String command = "echo \"hello world\" >> " + testFile.getAbsolutePath();
final ShellExecRequest req = ShellExecRequest.newBuilder().setCommand(command).setAwaitResponse(false).build();
final ShellExecResponse resp = admin.<ShellExecService.Stub, ShellExecResponse>coprocessorService(ShellExecService::newStub, (stub, controller, callback) -> stub.shellExec(controller, req, callback)).join();
assertFalse("the response from a background task should have no exit code", resp.hasExitCode());
assertFalse("the response from a background task should have no stdout", resp.hasStdout());
assertFalse("the response from a background task should have no stderr", resp.hasStderr());
Waiter.waitFor(conn.getConfiguration(), 5_000, () -> testFile.length() > 0);
final String content = new String(Files.readAllBytes(testFile.toPath())).trim();
assertEquals("hello world", content);
}
Aggregations