|
| 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 | + |
| 19 | +package org.apache.zookeeper.server.controller; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.net.InetSocketAddress; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import org.eclipse.jetty.client.HttpClient; |
| 26 | +import org.eclipse.jetty.client.api.ContentResponse; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +/** |
| 31 | + * A convenient helper to send controller command to ControllerService. |
| 32 | + */ |
| 33 | +public class CommandClient { |
| 34 | + private final int requestTimeoutInMs; |
| 35 | + private static final int DEFAULT_TIMEOUT = 10000; |
| 36 | + private static final Logger LOG = LoggerFactory.getLogger(CommandClient.class); |
| 37 | + private final int hostPort; |
| 38 | + private final String hostName; |
| 39 | + private HttpClient client; |
| 40 | + private boolean started = false; |
| 41 | + |
| 42 | + /** |
| 43 | + * Instantiate a client configured to send requests to localhost. |
| 44 | + * @param localHostPort Port that the localhost CommandListener is listening on. |
| 45 | + * @param requestTimeoutInMs Timeout in ms for synchronous requests to timeout. |
| 46 | + */ |
| 47 | + public CommandClient(int localHostPort, int requestTimeoutInMs) { |
| 48 | + this.client = new HttpClient(); |
| 49 | + this.requestTimeoutInMs = requestTimeoutInMs; |
| 50 | + this.hostName = "localhost"; |
| 51 | + this.hostPort = localHostPort; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Instantiate a client configured to send requests to the specified host address. |
| 56 | + * @param hostAddress The host address of the listening server. |
| 57 | + * @param requestTimeoutInMs Timeout in ms for synchronous requests to timeout. |
| 58 | + */ |
| 59 | + public CommandClient(InetSocketAddress hostAddress, int requestTimeoutInMs) { |
| 60 | + this.client = new HttpClient(); |
| 61 | + this.requestTimeoutInMs = requestTimeoutInMs; |
| 62 | + this.hostName = hostAddress.getHostName(); |
| 63 | + this.hostPort = hostAddress.getPort(); |
| 64 | + } |
| 65 | + |
| 66 | + public CommandClient(int localhostPort) { |
| 67 | + this(localhostPort, DEFAULT_TIMEOUT); |
| 68 | + } |
| 69 | + |
| 70 | + public synchronized void close() { |
| 71 | + try { |
| 72 | + if (client != null) { |
| 73 | + client.stop(); |
| 74 | + client = null; |
| 75 | + } |
| 76 | + } catch (Exception ex) { |
| 77 | + LOG.warn("Exception during shutdown", ex); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Send a command with no parameters to the server and wait for a response. |
| 83 | + * Returns true if we received a good (200) response and false otherwise. |
| 84 | + */ |
| 85 | + public boolean trySendCommand(ControlCommand.Action action) { |
| 86 | + return trySendCommand(action, null); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Send a command with an optional command parameter to the server and wait for a response. |
| 91 | + * @param action The command Action to send. |
| 92 | + * @param commandParameter The command parameter, in the form of command/action/parameter. |
| 93 | + * @return true if we received a good (200) response and false otherwise. |
| 94 | + */ |
| 95 | + public boolean trySendCommand(ControlCommand.Action action, String commandParameter) { |
| 96 | + try { |
| 97 | + if (!started) { |
| 98 | + client.start(); |
| 99 | + started = true; |
| 100 | + } |
| 101 | + ContentResponse response = sendCommand(action, commandParameter); |
| 102 | + LOG.info("Received {} response from the server", response); |
| 103 | + return (response.getStatus() == 200); |
| 104 | + } catch (InterruptedException | IOException ex) { |
| 105 | + LOG.warn("Failed to get response from server", ex); |
| 106 | + } catch (Exception ex) { |
| 107 | + LOG.error("Unknown exception when sending command", ex); |
| 108 | + } |
| 109 | + |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Send a command and optional command parameter to the server and block until receiving |
| 115 | + * a response. |
| 116 | + * |
| 117 | + * @param action The command Action to send. |
| 118 | + * @param commandParameter The command parameter, in the form of command/action/parameter. |
| 119 | + * @return The full response body from the CommandListener server. |
| 120 | + */ |
| 121 | + public ContentResponse sendCommand(ControlCommand.Action action, |
| 122 | + String commandParameter) throws Exception { |
| 123 | + String command = String.format("%s%s:%s/%s", "http://", |
| 124 | + this.hostName, this.hostPort, ControlCommand.createCommandUri(action, commandParameter)); |
| 125 | + ContentResponse response = this.client.newRequest(command).timeout(this.requestTimeoutInMs, |
| 126 | + TimeUnit.MILLISECONDS).send(); |
| 127 | + LOG.info("Sent command {}", command); |
| 128 | + LOG.info("Response body {}", new String(response.getContent(), StandardCharsets.UTF_8)); |
| 129 | + return response; |
| 130 | + } |
| 131 | +} |
0 commit comments