From df28b55c35d8c2f96d4c5077671699d379cb01ff Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Tue, 21 Sep 2021 17:40:12 +0200 Subject: Cache the connection target and reuse to avoid ssl renegotiation. --- .../metricsproxy/service/ConfigSentinelClient.java | 41 ++++++++++++---------- 1 file changed, 22 insertions(+), 19 deletions(-) (limited to 'metrics-proxy/src') 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 d07a52f42bd..9236c68ec87 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 @@ -24,7 +24,9 @@ import java.util.logging.Logger; public class ConfigSentinelClient extends AbstractComponent { private final static Logger log = Logger.getLogger(ConfigSentinelClient.class.getName()); + private static final Spec SPEC = new Spec("localhost", 19097); private final Supervisor supervisor; + private Target connection = null; @Inject public ConfigSentinelClient() { @@ -33,6 +35,9 @@ public class ConfigSentinelClient extends AbstractComponent { @Override public void deconstruct() { + if (connection != null) { + connection.close(); + } supervisor.transport().shutdown().join(); super.deconstruct(); } @@ -126,7 +131,7 @@ public class ConfigSentinelClient extends AbstractComponent { } for (int i = 1; i < parts.length; i++) { - String keyValue[] = parts[i].split("="); + String [] keyValue = parts[i].split("="); String key = keyValue[0]; String value = keyValue[1]; @@ -155,26 +160,24 @@ public class ConfigSentinelClient extends AbstractComponent { String sentinelLs() { String servicelist = ""; - int rpcPort = 19097; - Spec spec = new Spec("localhost", rpcPort); - Target connection = supervisor.connect(spec); - try { - 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(Level.WARNING, "Bad answer to RPC request: " + req.errorMessage()); - } + synchronized (this) { + if (connection == null || ! connection.isValid()) { + 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(Level.WARNING, "Could not connect to sentinel at: "+spec); + log.log(Level.WARNING, "Bad answer to RPC request: " + req.errorMessage()); } - return servicelist; - } finally { - connection.close(); + } else { + log.log(Level.WARNING, "Could not connect to sentinel at: " + SPEC); } + return servicelist; } } -- cgit v1.2.3 From 62c76fb625ede6326c3e14c491218a114a551b92 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Tue, 21 Sep 2021 17:47:07 +0200 Subject: Add synchronization. --- .../java/ai/vespa/metricsproxy/service/ConfigSentinelClient.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'metrics-proxy/src') 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 9236c68ec87..3d834106ebc 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 @@ -35,8 +35,11 @@ public class ConfigSentinelClient extends AbstractComponent { @Override public void deconstruct() { - if (connection != null) { - connection.close(); + synchronized (this) { + if (connection != null) { + connection.close(); + connection = null; + } } supervisor.transport().shutdown().join(); super.deconstruct(); -- cgit v1.2.3