diff options
author | Arne Juul <arnej@verizonmedia.com> | 2020-03-18 15:12:24 +0000 |
---|---|---|
committer | Arne Juul <arnej@verizonmedia.com> | 2020-03-18 17:09:45 +0000 |
commit | 208353d6d80768dda4418bcc8563c95fab55565d (patch) | |
tree | e3ba1d26280ecb55fee8da714e39bb36fb4d46c2 /metrics-proxy | |
parent | b28776878ebe89270131fd8b151f68eaa37577b8 (diff) |
use RPC directly instead of calling external process
Diffstat (limited to 'metrics-proxy')
3 files changed, 32 insertions, 54 deletions
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/ConfigSentinelClient.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/ConfigSentinelClient.java index 09fb66bffab..9fceafc8849 100644 --- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/ConfigSentinelClient.java +++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/ConfigSentinelClient.java @@ -4,9 +4,15 @@ package ai.vespa.metricsproxy.service; import com.google.inject.Inject; import com.yahoo.log.LogLevel; +import com.yahoo.jrt.ErrorCode; +import com.yahoo.jrt.Request; +import com.yahoo.jrt.Spec; +import com.yahoo.jrt.Supervisor; +import com.yahoo.jrt.Target; +import com.yahoo.jrt.Transport; + import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.StringReader; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; @@ -17,11 +23,8 @@ import java.util.logging.Logger; public class ConfigSentinelClient { private final static Logger log = Logger.getLogger(ConfigSentinelClient.class.getName()); - private final CmdClient client; - @Inject public ConfigSentinelClient() { - this.client = new CmdClient(); } /** @@ -61,11 +64,8 @@ public class ConfigSentinelClient { * @throws Exception if something went wrong */ protected synchronized void setStatus(List<VespaService> services) throws Exception { - InputStream in; - client.connect(); - - in = client.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + String in = sentinelLs(); + BufferedReader reader = new BufferedReader(new StringReader(in)); String line; List<VespaService> updatedServices = new ArrayList<>(); while ((line = reader.readLine()) != null) { @@ -90,7 +90,6 @@ public class ConfigSentinelClient { //Close streams reader.close(); - client.disconnect(); } static VespaService parseServiceString(String line, List<VespaService> services) { @@ -144,28 +143,26 @@ public class ConfigSentinelClient { return service; } - static class CmdClient { - Process proc; - // NOTE: hostname/port not used yet - void connect() { - String[] args = new String[]{"vespa-sentinel-cmd", "list"}; - try { - proc = Runtime.getRuntime().exec(args); - } catch (Exception e) { - log.log(LogLevel.WARNING, "could not run vespa-sentinel-cmd: "+e); - proc = null; - } - } - void disconnect() { - if (proc.isAlive()) { - proc.destroy(); + String sentinelLs() { + String servicelist = ""; + Supervisor supervisor = new Supervisor(new Transport()); + int rpcPort = 19097; + Spec spec = new Spec("localhost", rpcPort); + Target connection = supervisor.connect(spec); + if (connection.isValid()) { + Request req = new Request("sentinel.ls"); + connection.invokeSync(req, 5.0); + if (req.errorCode() == ErrorCode.NONE && + req.checkReturnTypes("s")) + { + servicelist = req.returnValues().get(0).asString(); + } else { + log.log(LogLevel.WARNING, "Bad answer to RPC request: " + req.errorMessage()); } - proc = null; - } - InputStream getInputStream() { - return (proc != null) - ? proc.getInputStream() - : new java.io.ByteArrayInputStream(new byte[0]); + } else { + log.log(LogLevel.WARNING, "Could not connect to sentinel at: "+spec); } + supervisor.transport().shutdown().join(); + return servicelist; } } diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/ConfigSentinelClientTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/ConfigSentinelClientTest.java index 8c6c5ffc3f8..c28e16a8eaa 100644 --- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/ConfigSentinelClientTest.java +++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/ConfigSentinelClientTest.java @@ -98,4 +98,5 @@ public class ConfigSentinelClientTest { assertThat(containerClusterController.getPid(),is(25020)); assertThat(containerClusterController.getState(),is("RUNNING")); } + } diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/MockConfigSentinelClient.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/MockConfigSentinelClient.java index 6b439093682..4c660711c5d 100644 --- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/MockConfigSentinelClient.java +++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/MockConfigSentinelClient.java @@ -22,27 +22,7 @@ public class MockConfigSentinelClient extends ConfigSentinelClient { } @Override - protected synchronized void setStatus(List<VespaService> services) throws Exception { - List<VespaService> updatedServices = new ArrayList<>(); - String[] lines = configSentinel.getServiceList().split("\n"); - for (String line : lines) { - if (line.equals("")) { - break; - } - - VespaService s = parseServiceString(line, services); - if (s != null) { - updatedServices.add(s); - } - } - - //Check if there are services that were not found in - //from the sentinel - for (VespaService s : services) { - if (!updatedServices.contains(s)) { - log.log(LogLevel.DEBUG, "Service " + s + " is no longer found with sentinel - setting alive = false"); - s.setAlive(false); - } - } + String sentinelLs() { + return configSentinel.getServiceList(); } } |