Skip to content

Commit

Permalink
Fix pending job counters.
Browse files Browse the repository at this point in the history
Run pending jobs on connection shutdown to align the counters.

Fixes issue #2065

Signed-off-by: Achim Kraus <achim.kraus@cloudcoap.net>
  • Loading branch information
boaks committed Sep 19, 2022
1 parent 92f7677 commit 726bac5
Show file tree
Hide file tree
Showing 12 changed files with 370 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,25 @@ public static void shutdownExecutorGracefully(long timeMaxToWaitInMs, ExecutorSe
}
}

/**
* Run all jobs.
*
* On {@link Throwable} write only warning and continue to run the other
* jobs.
*
* @param jobs list of jobs to run.
* @since 3.7
*/
public static void runAll(List<Runnable> jobs) {
for (Runnable job : jobs) {
try {
job.run();
} catch (Throwable e) {
LOGGER.warn("Ignoring error:", e);
}
}
}

/**
* Set remove on cancel policy of provided executor.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*******************************************************************************
* Copyright (c) 2022 Contributors to the Eclipse Foundation.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
******************************************************************************/
package org.eclipse.californium.elements.util;

import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Limited jobs.
*
* Limits pending jobs based on counters.
*
* Note: for proper operation, ensure the at the end of
* {@link Runnable#run()} the {@link LimitedRunnable#onDequeueing()} is
* called.
*
* <pre>
* public void run() {
* try {
* if (running.get() &amp;&amp; connection.isExecuting()) {
* // process
* }
* } finally {
* onDequeueing();
* }
* }
* </pre>
*
* @since 3.7
*/
public abstract class LimitedRunnable implements Runnable {

/**
* Down counter to limit pending jobs.
*/
private final AtomicInteger counter;
/**
* Indicator for overflows. {@code true}, if the counter indicates an
* overflow, {@code false}, otherwise.
*/
private volatile boolean overflow;

/**
* Create job limited by the provided counter
*
* @param counter counter in count-down mode
*/
public LimitedRunnable(AtomicInteger counter) {
this.counter = counter;
}

/**
* Queue job.
*
* @throws RejectedExecutionException if limit is exceeded
*/
public void onQueueing() {
if (counter.decrementAndGet() < 0) {
overflow = true;
throw new RejectedExecutionException("queue overflow!");
}
}

/**
* Dequeue job.
*/
public void onDequeueing() {
counter.incrementAndGet();
}

/**
* Checks, if queueing this job causes an counter overflow.
*
* @return {@code true}, if the counter indicates an overflow,
* {@code false}, otherwise.
*/
public boolean isOverflown() {
return overflow;
}

/**
* Execute this job.
*
* @param executor executor to execute jobs.
*/
public void execute(Executor executor) {
try {
onQueueing();
executor.execute(this);
} catch (RejectedExecutionException ex) {
onDequeueing();
throw ex;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ public class SerialExecutor extends AbstractExecutorService {
*
* @param executor target executor. If {@code null}, the executor is
* shutdown.
* @throws IllegalArgumentException if the executor is also a
* {@link SerialExecutor}
*/
public SerialExecutor(final Executor executor) {
if (executor == null) {
shutdown = true;
} else if (executor instanceof SerialExecutor) {
throw new IllegalArgumentException("Sequences of SerialExecutors are not supported!");
}
this.executor = executor;
}
Expand Down Expand Up @@ -211,7 +215,7 @@ public List<Runnable> shutdownNow() {
}

/**
* Shutdown this executor and add all pending task from {@link #tasks} to
* Shutdown this executor and add all pending jobs from {@link #tasks} to
* the provided collection.
*
* @param jobs collection to add pending jobs.
Expand Down Expand Up @@ -280,31 +284,31 @@ private final void scheduleNextJob() {
@Override
public void run() {
try {
setOwner();
ExecutionListener current = listener.get();
try {
setOwner();
ExecutionListener current = listener.get();
if (current != null) {
current.beforeExecution();
}
command.run();
} catch (Throwable t) {
LOGGER.error("unexpected error occurred:", t);
} finally {
try {
if (current != null) {
current.beforeExecution();
current.afterExecution();
}
command.run();
} catch (Throwable t) {
LOGGER.error("unexpected error occurred:", t);
} finally {
try {
if (current != null) {
current.afterExecution();
}
} catch (Throwable t) {
LOGGER.error("unexpected error occurred:", t);
}
clearOwner();
LOGGER.error("unexpected error occurred after execution:", t);
}
} finally {
clearOwner();
}
} finally {
try {
scheduleNextJob();
} catch (RejectedExecutionException ex) {
LOGGER.debug("shutdown?", ex);
}
} catch (RejectedExecutionException ex) {
LOGGER.debug("shutdown?", ex);
}
}
});
Expand Down Expand Up @@ -332,7 +336,8 @@ public ExecutionListener setExecutionListener(ExecutionListener listener) {
/**
* Execution listener.
*
* Called before and after executing a task.
* Called before and after executing a task. The calling thread is the same
* as the the one executing the job.
*
* @since 2.4
*/
Expand Down
25 changes: 25 additions & 0 deletions scandium-core/api-changes.json
Original file line number Diff line number Diff line change
Expand Up @@ -400,5 +400,30 @@
]
}
}
],
"3.7.0": [
{
"extension": "revapi.differences",
"configuration": {
"ignore": true,
"differences": [
{
"code": "java.method.removed",
"old": "method boolean org.eclipse.californium.scandium.DTLSConnector::executeHandshakeResult(java.util.concurrent.Executor, org.eclipse.californium.scandium.dtls.Connection, java.lang.Runnable)",
"justification": "Not part of the public API"
},
{
"code": "java.method.removed",
"old": "method boolean org.eclipse.californium.scandium.DTLSConnector::executeInbound(java.util.concurrent.Executor, java.net.InetSocketAddress, java.lang.Runnable)",
"justification": "Not part of the public API"
},
{
"code": "java.method.removed",
"old": "method boolean org.eclipse.californium.scandium.DTLSConnector::executeOutbound(java.util.concurrent.Executor, java.net.InetSocketAddress, java.lang.Runnable)",
"justification": "Not part of the public API"
}
]
}
}
]
}
Loading

0 comments on commit 726bac5

Please sign in to comment.