Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[METRICS] New thread for waiting list checking #1836

Merged
merged 6 commits into from
Aug 17, 2023
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -222,8 +221,8 @@ class MetricStoreImpl implements MetricStore {
private final Set<Integer> identities = new ConcurrentSkipListSet<>();

private volatile Map<MetricSensor, BiConsumer<Integer, Exception>> lastSensors = Map.of();
private final Map<CountDownLatch, Predicate<ClusterBean>> waitingList =
new ConcurrentHashMap<>();
// Monitor for detecting cluster bean changing.
private final Object beanUpdateMonitor = new Object();
// For mbean register. To distinguish mbeans of different metricStore.
private final String uid = Utils.randomString();
private final Sensor<Long> beanReceivedSensor =
Expand All @@ -236,6 +235,7 @@ private MetricStoreImpl(
this.receivers = receivers;
// receiver + cleaner
this.executor = Executors.newFixedThreadPool(2);

chinghongfang marked this conversation as resolved.
Show resolved Hide resolved
Runnable cleanerJob =
() -> {
while (!closed.get()) {
Expand Down Expand Up @@ -292,7 +292,10 @@ private MetricStoreImpl(
if (!allBeans.isEmpty()) {
// generate new cluster bean
updateClusterBean();
checkWaitingList(this.waitingList, clusterBean());
// Tell waiting threads that cluster bean has been changed
synchronized (beanUpdateMonitor) {
beanUpdateMonitor.notifyAll();
}
}
});
} catch (Exception e) {
Expand Down Expand Up @@ -349,23 +352,31 @@ public void close() {
receivers.forEach(Receiver::close);
}

/** User thread will "wait" until being awakened by the metric store or being timeout. */
/**
* User thread will wait until checker pass or timeout. When cluster bean has changed, the
* waiting threads will be notified.
*/
@Override
public void wait(Predicate<ClusterBean> checker, Duration timeout) {
var latch = new CountDownLatch(1);
try {
waitingList.put(latch, checker);
// Check the newly added checker immediately
checkWaitingList(Map.of(latch, checker), clusterBean());
// Wait until being awake or timeout
if (!latch.await(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
throw new IllegalStateException("Timeout waiting for the checker");
public void wait(Predicate<ClusterBean> checker, Duration duration) {
long timeout = System.currentTimeMillis() + duration.toMillis();
chinghongfang marked this conversation as resolved.
Show resolved Hide resolved
if (checker.test(clusterBean())) return;

chinghongfang marked this conversation as resolved.
Show resolved Hide resolved
while (System.currentTimeMillis() < timeout) {
try {
synchronized (beanUpdateMonitor) {
// Release the lock and wait for clusterBean being updated
this.beanUpdateMonitor.wait(timeout - System.currentTimeMillis());
// Tell other threads clusterBean has been updated
beanUpdateMonitor.notifyAll();
chinghongfang marked this conversation as resolved.
Show resolved Hide resolved
}
if (checker.test(clusterBean())) return;
} catch (NoSufficientMetricsException e) {
// Check failed. Try again next time.
} catch (InterruptedException ie) {
throw new IllegalStateException("Interrupted while waiting for the checker");
}
} catch (InterruptedException ie) {
throw new IllegalStateException("Interrupted while waiting for the checker");
} finally {
waitingList.remove(latch);
}
throw new IllegalStateException("Timeout waiting for the checker");
}

private void updateClusterBean() {
Expand All @@ -377,20 +388,5 @@ private void updateClusterBean() {
Collectors.toUnmodifiableMap(
Map.Entry::getKey, e -> List.copyOf(e.getValue()))));
}

/**
* Check the checkers in the waiting list. If the checker returns true, count down the latch.
*/
private static void checkWaitingList(
Map<CountDownLatch, Predicate<ClusterBean>> waitingList, ClusterBean clusterBean) {
waitingList.forEach(
(latch, checker) -> {
try {
if (checker.test(clusterBean)) latch.countDown();
} catch (NoSufficientMetricsException e) {
// Check failed. Try again next time.
}
});
}
}
}