|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.Set; |
| 25 | +import org.apache.commons.lang3.StringUtils; |
| 26 | +import org.apache.hadoop.hbase.client.AsyncAdmin; |
| 27 | +import org.apache.hadoop.hbase.client.AsyncConnection; |
| 28 | +import org.apache.hadoop.hbase.client.ConnectionFactory; |
| 29 | +import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecRequest; |
| 30 | +import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecResponse; |
| 31 | +import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecService; |
| 32 | +import org.apache.hadoop.hbase.util.Pair; |
| 33 | +import org.apache.yetus.audience.InterfaceAudience; |
| 34 | +import org.slf4j.Logger; |
| 35 | +import org.slf4j.LoggerFactory; |
| 36 | + |
| 37 | +/** |
| 38 | + * Overrides commands to make use of coprocessor where possible. Only supports actions taken |
| 39 | + * against Master and Region Server hosts. |
| 40 | + */ |
| 41 | +@InterfaceAudience.Private |
| 42 | +@SuppressWarnings("unused") // no way to test this without a distributed cluster. |
| 43 | +public class CoprocClusterManager extends HBaseClusterManager { |
| 44 | + private static final Logger LOG = LoggerFactory.getLogger(CoprocClusterManager.class); |
| 45 | + private static final Set<ServiceType> supportedServices = buildSupportedServicesSet(); |
| 46 | + |
| 47 | + @Override |
| 48 | + protected Pair<Integer, String> exec(String hostname, ServiceType service, String... cmd) |
| 49 | + throws IOException { |
| 50 | + if (!supportedServices.contains(service)) { |
| 51 | + throw unsupportedServiceType(service); |
| 52 | + } |
| 53 | + |
| 54 | + // We only support actions vs. Master or Region Server processes. We're issuing those actions |
| 55 | + // via the coprocessor that's running within those processes. Thus, there's no support for |
| 56 | + // honoring the configured service user. |
| 57 | + final String command = StringUtils.join(cmd, " "); |
| 58 | + LOG.info("Executing remote command: {}, hostname:{}", command, hostname); |
| 59 | + |
| 60 | + try (final AsyncConnection conn = ConnectionFactory.createAsyncConnection(getConf()).join()) { |
| 61 | + final AsyncAdmin admin = conn.getAdmin(); |
| 62 | + final ShellExecRequest req = ShellExecRequest.newBuilder() |
| 63 | + .setCommand(command) |
| 64 | + .setAwaitResponse(false) |
| 65 | + .build(); |
| 66 | + |
| 67 | + final ShellExecResponse resp; |
| 68 | + switch(service) { |
| 69 | + case HBASE_MASTER: |
| 70 | + // What happens if the intended action was killing a backup master? Right now we have |
| 71 | + // no `RestartBackupMasterAction` so it's probably fine. |
| 72 | + resp = masterExec(admin, req); |
| 73 | + break; |
| 74 | + case HBASE_REGIONSERVER: |
| 75 | + final ServerName targetHost = resolveRegionServerName(admin, hostname); |
| 76 | + resp = regionServerExec(admin, req, targetHost); |
| 77 | + break; |
| 78 | + default: |
| 79 | + throw new RuntimeException("should not happen"); |
| 80 | + } |
| 81 | + |
| 82 | + if (LOG.isDebugEnabled()) { |
| 83 | + LOG.debug("Executed remote command: {}, exit code:{} , output:{}", command, resp.getExitCode(), |
| 84 | + resp.getStdout()); |
| 85 | + } else { |
| 86 | + LOG.info("Executed remote command: {}, exit code:{}", command, resp.getExitCode()); |
| 87 | + } |
| 88 | + return new Pair<>(resp.getExitCode(), resp.getStdout()); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static Set<ServiceType> buildSupportedServicesSet() { |
| 93 | + final Set<ServiceType> set = new HashSet<>(); |
| 94 | + set.add(ServiceType.HBASE_MASTER); |
| 95 | + set.add(ServiceType.HBASE_REGIONSERVER); |
| 96 | + return Collections.unmodifiableSet(set); |
| 97 | + } |
| 98 | + |
| 99 | + private static ShellExecResponse masterExec(final AsyncAdmin admin, |
| 100 | + final ShellExecRequest req) { |
| 101 | + // TODO: Admin API provides no means of sending exec to a backup master. |
| 102 | + return admin.<ShellExecService.Stub, ShellExecResponse>coprocessorService( |
| 103 | + ShellExecService::newStub, |
| 104 | + (stub, controller, callback) -> stub.shellExec(controller, req, callback)) |
| 105 | + .join(); |
| 106 | + } |
| 107 | + |
| 108 | + private static ShellExecResponse regionServerExec(final AsyncAdmin admin, |
| 109 | + final ShellExecRequest req, final ServerName targetHost) { |
| 110 | + return admin.<ShellExecService.Stub, ShellExecResponse>coprocessorService( |
| 111 | + ShellExecService::newStub, |
| 112 | + (stub, controller, callback) -> stub.shellExec(controller, req, callback), |
| 113 | + targetHost) |
| 114 | + .join(); |
| 115 | + } |
| 116 | + |
| 117 | + private static ServerName resolveRegionServerName(final AsyncAdmin admin, |
| 118 | + final String hostname) { |
| 119 | + return admin.getRegionServers() |
| 120 | + .thenApply(names -> names.stream() |
| 121 | + .filter(sn -> Objects.equals(sn.getHostname(), hostname)) |
| 122 | + .findAny()) |
| 123 | + .join() |
| 124 | + .orElseThrow(() -> serverNotFound(hostname)); |
| 125 | + } |
| 126 | + |
| 127 | + private static RuntimeException serverNotFound(final String hostname) { |
| 128 | + return new RuntimeException( |
| 129 | + String.format("Did not find %s amongst the servers known to the client.", hostname)); |
| 130 | + } |
| 131 | + |
| 132 | + private static RuntimeException unsupportedServiceType(final ServiceType serviceType) { |
| 133 | + return new RuntimeException( |
| 134 | + String.format("Unable to service request for service=%s", serviceType)); |
| 135 | + } |
| 136 | +} |
0 commit comments