forked from scylladb/scylla-jmx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scylla-jmx: Introduce a registration check object
Allows for shared code for synchronized and optionally partial update checks.
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/java/com/scylladb/jmx/metrics/RegistrationChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.scylladb.jmx.metrics; | ||
|
||
import static com.scylladb.jmx.metrics.RegistrationMode.Remove; | ||
import static com.scylladb.jmx.metrics.RegistrationMode.Wait; | ||
import static java.util.EnumSet.allOf; | ||
import static java.util.EnumSet.of; | ||
|
||
import java.net.UnknownHostException; | ||
import java.util.EnumSet; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
import javax.management.OperationsException; | ||
|
||
import com.scylladb.jmx.api.APIClient; | ||
import com.sun.jmx.mbeanserver.JmxMBeanServer; | ||
|
||
@SuppressWarnings("restriction") | ||
public abstract class RegistrationChecker { | ||
private final Lock lock = new ReentrantLock(); | ||
|
||
public static final EnumSet<RegistrationMode> REMOVE_NO_WAIT = of(Remove); | ||
public static final EnumSet<RegistrationMode> ADD_AND_REMOVE = allOf(RegistrationMode.class); | ||
|
||
public final void reap(APIClient client, JmxMBeanServer server) throws OperationsException, UnknownHostException { | ||
check(client, server, REMOVE_NO_WAIT); | ||
} | ||
|
||
public final void check(APIClient client, JmxMBeanServer server) throws OperationsException, UnknownHostException { | ||
check(client, server, ADD_AND_REMOVE); | ||
} | ||
|
||
public final void check(APIClient client, JmxMBeanServer server, EnumSet<RegistrationMode> mode) | ||
throws OperationsException, UnknownHostException { | ||
if (!lock.tryLock() && mode.contains(Wait)) { | ||
// someone is doing update. | ||
// since this is jmx, and sloppy, we'll just | ||
// assume that once he is done, things are | ||
// good enough. | ||
lock.lock(); | ||
lock.unlock(); | ||
return; | ||
} | ||
try { | ||
doCheck(client, server, mode); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
protected abstract void doCheck(APIClient client, JmxMBeanServer server, EnumSet<RegistrationMode> mode) | ||
throws OperationsException, UnknownHostException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.scylladb.jmx.metrics; | ||
|
||
public enum RegistrationMode { | ||
Wait, Add, Remove, | ||
} |