Skip to content

Commit

Permalink
feat(discovery): dual HTTP+JMX registration (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores authored Sep 21, 2023
1 parent b8371aa commit fd141de
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 36 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ and how it advertises itself to a Cryostat server instance. Required properties
- [ ] `cryostat.agent.webserver.port` [`int`]: the internal port number for the embedded webserver to bind to. Default `9977`.
- [ ] `cryostat.agent.app.name` [`String`]: a human-friendly name for this application. Default `cryostat-agent`.
- [ ] `cryostat.agent.app.jmx.port` [`int`]: the JMX RMI port that the application is listening on. The default is to attempt to determine this from the `com.sun.management.jmxremote.port` system property.
- [ ] `cryostat.agent.registration.prefer-jmx` [`boolean`]: control whether the Agent prefers to publish itself as reachable via JMX or HTTP. If JMX is not enabled on the application JVM then the Agent will always publish itself using HTTP. Default `false`.
- [ ] `cryostat.agent.registration.retry-ms` [`long`]: the duration in milliseconds between attempts to register with the Cryostat server. Default `5000`.
- [ ] `cryostat.agent.exit.signals` [`[String]`]: a comma-separated list of signals that the agent should handle. When any of these signals is caught the agent initiates an orderly shutdown, deregistering from the Cryostat server and potentially uploading the latest harvested JFR data. Default `INT,TERM`.
- [ ] `cryostat.agent.exit.deregistration.timeout-ms` [`long`]: the duration in milliseconds to wait for a response from the Cryostat server when attempting to deregister at shutdown time . Default `3s`.
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/io/cryostat/agent/ConfigModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ public abstract class ConfigModule {
public static final String CRYOSTAT_AGENT_APP_NAME = "cryostat.agent.app.name";
public static final String CRYOSTAT_AGENT_HOSTNAME = "cryostat.agent.hostname";
public static final String CRYOSTAT_AGENT_APP_JMX_PORT = "cryostat.agent.app.jmx.port";
public static final String CRYOSTAT_AGENT_REGISTRATION_PREFER_JMX =
"cryostat.agent.registration.prefer-jmx";
public static final String CRYOSTAT_AGENT_REGISTRATION_RETRY_MS =
"cryostat.agent.registration.retry-ms";
public static final String CRYOSTAT_AGENT_REGISTRATION_CHECK_MS =
Expand Down Expand Up @@ -206,14 +204,6 @@ public static int provideCryostatAgentAppJmxPort(SmallRyeConfig config) {
System.getProperty("com.sun.management.jmxremote.port", "-1")));
}

@Provides
@Singleton
@Named(CRYOSTAT_AGENT_REGISTRATION_PREFER_JMX)
public static boolean provideCryostatAgentRegistrationPreferJmx(SmallRyeConfig config) {
return config.getOptionalValue(CRYOSTAT_AGENT_REGISTRATION_PREFER_JMX, boolean.class)
.orElse(false);
}

@Provides
@Singleton
@Named(CRYOSTAT_AGENT_REGISTRATION_RETRY_MS)
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/cryostat/agent/CryostatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -335,7 +335,8 @@ public CompletableFuture<Void> deregister(PluginInfo pluginInfo) {
.thenApply(res -> null);
}

public CompletableFuture<Void> update(PluginInfo pluginInfo, Set<DiscoveryNode> subtree) {
public CompletableFuture<Void> update(
PluginInfo pluginInfo, Collection<DiscoveryNode> subtree) {
try {
HttpPost req =
new HttpPost(
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/io/cryostat/agent/MainModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ public static Registration provideRegistration(
@Named(ConfigModule.CRYOSTAT_AGENT_APP_NAME) String appName,
@Named(ConfigModule.CRYOSTAT_AGENT_REALM) String realm,
@Named(ConfigModule.CRYOSTAT_AGENT_HOSTNAME) String hostname,
@Named(ConfigModule.CRYOSTAT_AGENT_REGISTRATION_PREFER_JMX) boolean preferJmx,
@Named(ConfigModule.CRYOSTAT_AGENT_APP_JMX_PORT) int jmxPort,
@Named(ConfigModule.CRYOSTAT_AGENT_REGISTRATION_RETRY_MS) int registrationRetryMs,
@Named(ConfigModule.CRYOSTAT_AGENT_REGISTRATION_CHECK_MS) int registrationCheckMs) {
Expand Down Expand Up @@ -229,7 +228,6 @@ public static Registration provideRegistration(
appName,
realm,
hostname,
preferJmx,
jmxPort,
registrationRetryMs,
registrationCheckMs);
Expand Down
60 changes: 40 additions & 20 deletions src/main/java/io/cryostat/agent/Registration.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.time.Instant;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
Expand All @@ -31,6 +32,7 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import io.cryostat.agent.model.DiscoveryNode;
import io.cryostat.agent.model.PluginInfo;
Expand All @@ -54,7 +56,6 @@ class Registration {
private final String appName;
private final String realm;
private final String hostname;
private final boolean preferJmx;
private final int jmxPort;
private final int registrationRetryMs;
private final int registrationCheckMs;
Expand All @@ -74,7 +75,6 @@ class Registration {
String appName,
String realm,
String hostname,
boolean preferJmx,
int jmxPort,
int registrationRetryMs,
int registrationCheckMs) {
Expand All @@ -87,7 +87,6 @@ class Registration {
this.appName = appName;
this.realm = realm;
this.hostname = hostname;
this.preferJmx = preferJmx;
this.jmxPort = jmxPort;
this.registrationRetryMs = registrationRetryMs;
this.registrationCheckMs = registrationCheckMs;
Expand Down Expand Up @@ -238,16 +237,20 @@ private void tryUpdate() {
log.warn("update attempted before initialized");
return;
}
DiscoveryNode selfNode;
Collection<DiscoveryNode> selfNodes;
try {
selfNode = defineSelf();
selfNodes = defineSelf();
} catch (UnknownHostException | URISyntaxException e) {
log.error("Unable to define self", e);
return;
}
log.info("publishing self as {}", selfNode.getTarget().getConnectUrl());
log.info(
"publishing self as {}",
selfNodes.stream()
.map(n -> n.getTarget().getConnectUrl())
.collect(Collectors.toList()));
Future<Void> f =
cryostat.update(pluginInfo, Set.of(selfNode))
cryostat.update(pluginInfo, selfNodes)
.handle(
(n, t) -> {
if (t != null) {
Expand All @@ -266,7 +269,9 @@ private void tryUpdate() {
}
}

private DiscoveryNode defineSelf() throws UnknownHostException, URISyntaxException {
private Set<DiscoveryNode> defineSelf() throws UnknownHostException, URISyntaxException {
Set<DiscoveryNode> discoveryNodes = new HashSet<>();

long pid = ProcessHandle.current().pid();
String javaMain = System.getProperty("sun.java.command", System.getenv("JAVA_MAIN_CLASS"));
if (StringUtils.isBlank(javaMain)) {
Expand All @@ -281,15 +286,7 @@ private DiscoveryNode defineSelf() throws UnknownHostException, URISyntaxExcepti
.getEpochSecond();
URI uri = callback;
int port = uri.getPort();
if (preferJmx && jmxPort > 0) {
uri =
URI.create(
String.format(
"service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi",
hostname, jmxPort));
port = jmxPort;
}
DiscoveryNode.Target target =
DiscoveryNode.Target httpSelf =
new DiscoveryNode.Target(
realm,
uri,
Expand All @@ -301,10 +298,33 @@ private DiscoveryNode defineSelf() throws UnknownHostException, URISyntaxExcepti
port,
javaMain,
startTime);
discoveryNodes.add(
new DiscoveryNode(appName + "-agent-" + pluginInfo.getId(), NODE_TYPE, httpSelf));

if (jmxPort > 0) {
uri =
URI.create(
String.format(
"service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi",
hostname, jmxPort));
port = jmxPort;
DiscoveryNode.Target jmxSelf =
new DiscoveryNode.Target(
realm,
uri,
appName,
instanceId,
jvmId,
pid,
hostname,
port,
javaMain,
startTime);
discoveryNodes.add(
new DiscoveryNode(appName + "-jmx-" + pluginInfo.getId(), NODE_TYPE, jmxSelf));
}

DiscoveryNode selfNode =
new DiscoveryNode(appName + "-" + pluginInfo.getId(), NODE_TYPE, target);
return selfNode;
return discoveryNodes;
}

void stop() {}
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/META-INF/microprofile-config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ cryostat.agent.realm=
cryostat.agent.exit.signals=INT,TERM
cryostat.agent.registration.retry-ms=5000
cryostat.agent.registration.check-ms=60000
cryostat.agent.registration.prefer-jmx=false
cryostat.agent.exit.deregistration.timeout-ms=3000

cryostat.agent.harvester.period-ms=-1
Expand Down

0 comments on commit fd141de

Please sign in to comment.