Search in sources :

Example 1 with ShellExecRequest

use of org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecRequest 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());
    }
}
Also used : AsyncAdmin(org.apache.hadoop.hbase.client.AsyncAdmin) ShellExecResponse(org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecResponse) AsyncConnection(org.apache.hadoop.hbase.client.AsyncConnection) ShellExecRequest(org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecRequest) Pair(org.apache.hadoop.hbase.util.Pair)

Example 2 with ShellExecRequest

use of org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecRequest 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);
}
Also used : ShellExecService(org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecService) Files(java.nio.file.Files) MediumTests(org.apache.hadoop.hbase.testclassification.MediumTests) AsyncAdmin(org.apache.hadoop.hbase.client.AsyncAdmin) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) File(java.io.File) Consumer(java.util.function.Consumer) AsyncConnection(org.apache.hadoop.hbase.client.AsyncConnection) ShellExecResponse(org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecResponse) Rule(org.junit.Rule) Paths(java.nio.file.Paths) Assert.assertFalse(org.junit.Assert.assertFalse) Configuration(org.apache.hadoop.conf.Configuration) Optional(java.util.Optional) ClassRule(org.junit.ClassRule) Path(java.nio.file.Path) Assert.assertEquals(org.junit.Assert.assertEquals) ShellExecRequest(org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecRequest) AsyncAdmin(org.apache.hadoop.hbase.client.AsyncAdmin) ShellExecResponse(org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecResponse) AsyncConnection(org.apache.hadoop.hbase.client.AsyncConnection) ShellExecRequest(org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecRequest) ShellExecService(org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecService) File(java.io.File) Test(org.junit.Test)

Aggregations

AsyncAdmin (org.apache.hadoop.hbase.client.AsyncAdmin)2 AsyncConnection (org.apache.hadoop.hbase.client.AsyncConnection)2 ShellExecRequest (org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecRequest)2 ShellExecResponse (org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecResponse)2 File (java.io.File)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Optional (java.util.Optional)1 Consumer (java.util.function.Consumer)1 Configuration (org.apache.hadoop.conf.Configuration)1 ShellExecService (org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecService)1 MediumTests (org.apache.hadoop.hbase.testclassification.MediumTests)1 Pair (org.apache.hadoop.hbase.util.Pair)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 ClassRule (org.junit.ClassRule)1 Rule (org.junit.Rule)1