Skip to content

Commit

Permalink
fix(registration): do not restart harvester on refresh (#29)
Browse files Browse the repository at this point in the history
* fix(registration): do not restart harvester on refresh

* ignore start request if already started

* log template and period on start

* fixup! fix(registration): do not restart harvester on refresh
  • Loading branch information
andrewazores authored Jan 5, 2023
1 parent 47abc1f commit 4a350ba
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
16 changes: 12 additions & 4 deletions src/main/java/io/cryostat/agent/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,18 @@ public static void main(String[] args) {
client.registration()
.addRegistrationListener(
evt -> {
if (evt.state) {
client.harvester().start();
} else {
client.harvester().stop();
switch (evt.state) {
case REGISTERED:
client.harvester().start();
break;
case UNREGISTERED:
client.harvester().stop();
break;
case REFRESHED:
break;
default:
log.error("Unknown registration state: {}", evt.state);
break;
}
});
client.registration().start();
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/cryostat/agent/Harvester.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class Harvester implements FlightRecorderListener {
}

public void start() {
if (running) {
return;
}
if (period <= 0) {
log.info("Harvester disabled, period {} < 0", period);
return;
Expand All @@ -99,7 +102,7 @@ public void start() {
try {
FlightRecorder.addListener(this);
this.flightRecorder = FlightRecorder.getFlightRecorder();
log.info("JFR Harvester started");
log.info("JFR Harvester started using template {} with period {}ms", template, period);
} catch (SecurityException | IllegalStateException e) {
log.error("Harvester could not start", e);
return;
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/io/cryostat/agent/Registration.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,18 @@ void tryRegister() {
.handleAsync(
(plugin, t) -> {
if (plugin != null) {
boolean previouslyRegistered =
this.pluginInfo.isInitialized();
this.pluginInfo.copyFrom(plugin);
log.info("Registered as {}", this.pluginInfo.getId());
notify(true);
notify(
previouslyRegistered
? RegistrationEvent.State.REFRESHED
: RegistrationEvent.State.REGISTERED);
tryUpdate();
} else if (t != null) {
notify(false);
this.pluginInfo.clear();
notify(RegistrationEvent.State.UNREGISTERED);
throw new RegistrationException(t);
}

Expand Down Expand Up @@ -214,32 +220,38 @@ CompletableFuture<Void> deregister() {
return cryostat.deregister(pluginInfo)
.handleAsync(
(n, t) -> {
this.pluginInfo.clear();
notify(RegistrationEvent.State.UNREGISTERED);
if (t != null) {
notify(false);
log.warn(
"Failed to deregister as Cryostat discovery plugin [{}]",
this.pluginInfo.getId());
} else {
notify(false);
log.info(
"Deregistered from Cryostat discovery plugin [{}]",
this.pluginInfo.getId());
}
this.pluginInfo.clear();
return null;
},
executor);
}

private void notify(boolean state) {
private void notify(RegistrationEvent.State state) {
RegistrationEvent evt = new RegistrationEvent(state);
this.listeners.forEach(listener -> listener.accept(evt));
}

static class RegistrationEvent {
boolean state;

RegistrationEvent(boolean state) {
enum State {
REGISTERED,
UNREGISTERED,
REFRESHED,
}

final State state;

RegistrationEvent(State state) {
this.state = state;
}
}
Expand Down

0 comments on commit 4a350ba

Please sign in to comment.