Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(iot): release rule capacity when worker evaluation ends (#6272)

Andre Kurait433ab5aefcab
diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java
index fa7a965f73..c5cbb0360b 100644
--- a/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerService.java
@@ -757,19 +757,33 @@ public class IotMqttBrokerService implements Resettable {
}
return;
}
- ruleWorker.executeBlocking(() -> {
- if (ruleState.get() == admitted) {
- rules.run();
- }
- return null;
- }, false).onComplete(result -> {
- if (pendingRuleEvaluations.decrementAndGet() == 0) {
- ruleEvaluationsThrottled.set(false);
- }
- if (result.failed()) {
- LOG.warnv(result.cause(), "IoT rule evaluation for topic {0} failed", topic);
- }
- });
+ AtomicBoolean released = new AtomicBoolean();
+ try {
+ ruleWorker.executeBlocking(() -> {
+ try {
+ if (ruleState.get() == admitted) {
+ rules.run();
+ }
+ return null;
+ } finally {
+ releaseRuleEvaluation(released);
+ }
+ }, false).onComplete(result -> {
+ releaseRuleEvaluation(released);
+ if (result.failed()) {
+ LOG.warnv(result.cause(), "IoT rule evaluation for topic {0} failed", topic);
+ }
+ });
+ } catch (RuntimeException dispatchFailure) {
+ releaseRuleEvaluation(released);
+ throw dispatchFailure;
+ }
+ }
+
+ private void releaseRuleEvaluation(AtomicBoolean released) {
+ if (released.compareAndSet(false, true) && pendingRuleEvaluations.decrementAndGet() == 0) {
+ ruleEvaluationsThrottled.set(false);
+ }
}
private void fanOut(String topic, byte[] payload, boolean retained) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java
index c8552d4f4b..b5b96dc15a 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttBrokerServiceTest.java
@@ -32,6 +32,7 @@ import io.quarkus.tls.runtime.config.TlsConfig;
import io.vertx.core.Context;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
+import io.vertx.core.WorkerExecutor;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.KeyCertOptions;
import io.vertx.core.net.PemKeyCertOptions;
@@ -57,6 +58,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.AdditionalAnswers;
import org.mockito.ArgumentCaptor;
+import org.mockito.ArgumentMatchers;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
@@ -87,6 +89,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
@@ -108,6 +111,7 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
@@ -1476,6 +1480,80 @@ class IotMqttBrokerServiceTest {
assertFalse(beyondBoundRan.get(), "the evaluation beyond the bound never ran");
}
+ @Test
+ void drainedRulesReleaseCapacityWhileTheirCallerContextIsBlocked() throws Exception {
+ String topic = "devices/d1/metrics";
+ Context context = vertx.getOrCreateContext();
+ CountDownLatch submitted = new CountDownLatch(1);
+ CountDownLatch releaseCaller = new CountDownLatch(1);
+ CountDownLatch callerReturned = new CountDownLatch(1);
+ CountDownLatch rulesRan = new CountDownLatch(IotMqttBrokerService.MAX_PENDING_RULE_EVALUATIONS);
+ CountDownLatch afterDrainRan = new CountDownLatch(1);
+ CountDownLatch workerStarted = new CountDownLatch(1);
+ CountDownLatch releaseWorker = new CountDownLatch(1);
+ AtomicReference<Throwable> callerFailure = new AtomicReference<>();
+ boolean callerQueued = false;
+ try {
+ broker.ruleWorker.executeBlocking(() -> {
+ workerStarted.countDown();
+ releaseWorker.await();
+ return null;
+ }, false);
+ context.runOnContext(ignored -> {
+ try {
+ for (int i = 0; i < IotMqttBrokerService.MAX_PENDING_RULE_EVALUATIONS; i++) {
+ broker.evaluateRulesOnWorker(topic, rulesRan::countDown);
+ }
+ submitted.countDown();
+ releaseCaller.await();
+ } catch (InterruptedException interrupted) {
+ Thread.currentThread().interrupt();
+ callerFailure.set(interrupted);
+ } catch (RuntimeException failure) {
+ callerFailure.set(failure);
+ } finally {
+ submitted.countDown();
+ callerReturned.countDown();
+ }
+ });
+ callerQueued = true;
+ assertTrue(workerStarted.await(10, TimeUnit.SECONDS), "the worker held the queued rules");
+ assertTrue(submitted.await(10, TimeUnit.SECONDS), "the caller submitted the bounded backlog");
+ assertNull(callerFailure.get());
+ releaseWorker.countDown();
+ assertTrue(rulesRan.await(10, TimeUnit.SECONDS), "the rules drained while their caller remained blocked");
+ CountDownLatch workerDrained = new CountDownLatch(1);
+ broker.ruleWorker.executeBlocking(() -> {
+ workerDrained.countDown();
+ return null;
+ }, false);
+ assertTrue(workerDrained.await(10, TimeUnit.SECONDS), "the worker passed the last rule's cleanup");
+ broker.evaluateRulesOnWorker(topic, afterDrainRan::countDown);
+ assertTrue(afterDrainRan.await(10, TimeUnit.SECONDS), "drained worker capacity is immediately reusable");
+ } finally {
+ releaseWorker.countDown();
+ releaseCaller.countDown();
+ if (callerQueued) {
+ assertTrue(callerReturned.await(10, TimeUnit.SECONDS), "the caller context was released");
+ }
+ }
+ }
+
+ @Test
+ void rejectedRuleDispatchDoesNotConsumePendingCapacity() {
+ Vertx rejectedVertx = mock(Vertx.class);
+ WorkerExecutor rejectedWorker = mock(WorkerExecutor.class);
+ when(rejectedVertx.createSharedWorkerExecutor("floci-iot-mqtt-rules", 1)).thenReturn(rejectedWorker);
+ when(rejectedWorker.executeBlocking(ArgumentMatchers.<Callable<Void>>any(), eq(false)))
+ .thenThrow(new IllegalStateException("dispatch unavailable"));
+ IotMqttBrokerService rejectedBroker = new IotMqttBrokerService(
+ config, rejectedVertx, iotService, customAuthorizer, registry, nanoTime::get);
+ for (int i = 0; i <= IotMqttBrokerService.MAX_PENDING_RULE_EVALUATIONS; i++) {
+ assertThrows(IllegalStateException.class,
+ () -> rejectedBroker.evaluateRulesOnWorker("devices/d1/metrics", () -> {}));
+ }
+ }
+
@Test
void rulesAreSkippedFromAResetUntilItEnds() throws Exception {
assertSkipsWaitingAndLaterRules(broker::beforeReset, broker::afterReset);