diff --git a/src/main/java/org/ice4j/ice/Agent.java b/src/main/java/org/ice4j/ice/Agent.java index 49a4a61..8b9d9e5 100644 --- a/src/main/java/org/ice4j/ice/Agent.java +++ b/src/main/java/org/ice4j/ice/Agent.java @@ -119,21 +119,9 @@ public class Agent /** * The ScheduledExecutorService to execute Agent's scheduled tasks */ - private static final ScheduledExecutorService agentTasksScheduler; - - static - { - CustomizableThreadFactory threadFactory - = new CustomizableThreadFactory("ice4j.Agent-", true); - - final ScheduledThreadPoolExecutor terminationExecutor - = new ScheduledThreadPoolExecutor(0, threadFactory); - terminationExecutor.setKeepAliveTime(10, TimeUnit.SECONDS); - terminationExecutor.setRemoveOnCancelPolicy(true); - agentTasksScheduler - = Executors.unconfigurableScheduledExecutorService( - terminationExecutor); - } + private static final ScheduledExecutorService agentTasksScheduler + = ExecutorFactory.createCPUBoundScheduledExecutor( + "ice4j.Agent-", 60, TimeUnit.SECONDS); /** * Termination task which will be scheduled with timeout diff --git a/src/main/java/org/ice4j/stack/StunClientTransaction.java b/src/main/java/org/ice4j/stack/StunClientTransaction.java index 68b23db..cac4d31 100644 --- a/src/main/java/org/ice4j/stack/StunClientTransaction.java +++ b/src/main/java/org/ice4j/stack/StunClientTransaction.java @@ -77,20 +77,9 @@ public class StunClientTransaction * The pool of Threads which retransmit * StunClientTransactions. */ - private static final ScheduledExecutorService retransmissionThreadPool; - - static { - CustomizableThreadFactory threadFactory - = new CustomizableThreadFactory("ice4j.StunClientTransaction-", true); - - final ScheduledThreadPoolExecutor terminationExecutor - = new ScheduledThreadPoolExecutor(0, threadFactory); - terminationExecutor.setKeepAliveTime(60, TimeUnit.SECONDS); - terminationExecutor.setRemoveOnCancelPolicy(true); - retransmissionThreadPool - = Executors.unconfigurableScheduledExecutorService( - terminationExecutor); - } + private static final ScheduledExecutorService retransmissionThreadPool + = ExecutorFactory.createCPUBoundScheduledExecutor( + "ice4j.StunClientTransaction-", 60, TimeUnit.SECONDS); /** * Maximum number of retransmissions. Once this number is reached and if no diff --git a/src/main/java/org/ice4j/stack/StunStack.java b/src/main/java/org/ice4j/stack/StunStack.java index 400f6eb..ebcda4a 100644 --- a/src/main/java/org/ice4j/stack/StunStack.java +++ b/src/main/java/org/ice4j/stack/StunStack.java @@ -21,6 +21,7 @@ import java.io.*; import java.net.*; import java.security.*; import java.util.*; +import java.util.concurrent.*; import java.util.logging.*; import javax.crypto.*; @@ -30,6 +31,7 @@ import org.ice4j.attribute.*; import org.ice4j.message.*; import org.ice4j.security.*; import org.ice4j.socket.*; +import org.ice4j.util.*; /** * The entry point to the Stun4J stack. The class is used to start, stop and @@ -52,8 +54,8 @@ public class StunStack * The Logger used by the StunStack class and its * instances for logging output. */ - private static final Logger logger - = Logger.getLogger(StunStack.class.getName()); + private static final java.util.logging.Logger logger + = java.util.logging.Logger.getLogger(StunStack.class.getName()); /** * The indicator which determines whether @@ -64,6 +66,14 @@ public class StunStack */ private static Mac mac; + /** + * The ScheduledExecutorService to execute StunStack scheduled tasks, + * in particular - expired server transactions collector. + */ + private static final ScheduledExecutorService tasksScheduler + = ExecutorFactory.createSingleThreadScheduledExecutor( + "ice4j.StunStack-", 60, TimeUnit.SECONDS); + /** * Our network gateway. */ @@ -84,11 +94,12 @@ public class StunStack = new Hashtable<>(); /** - * The Thread which expires the StunServerTransactions of - * this StunStack and removes them from - * {@link #serverTransactions}. + * The ExpiredServerTransactionsCollector which expires + * the StunServerTransactions of this StunStack and + * removes them from {@link #serverTransactions}. */ - private Thread serverTransactionExpireThread; + private ExpiredServerTransactionsCollector expiredTransactionsCollector + = new ExpiredServerTransactionsCollector(); /** * Currently open server transactions. The vector contains transaction ids @@ -408,7 +419,6 @@ public class StunStack new NetAccessManager(this, peerUdpMessageEventHandler, channelDataEventHandler); } - /** * Initializes a new StunStack instance. */ @@ -966,7 +976,7 @@ public class StunStack synchronized (serverTransactions) { serverTransactions.put(serverTid, sTran); - maybeStartServerTransactionExpireThread(); + expiredTransactionsCollector.schedule(); } } @@ -1106,6 +1116,8 @@ public class StunStack // serverTransactions Collection serverTransactionsToExpire; + expiredTransactionsCollector.cancel(); + synchronized (serverTransactions) { serverTransactionsToExpire @@ -1442,135 +1454,6 @@ public class StunStack return packetLogger != null && packetLogger.isEnabled(); } - /** - * Initializes and starts {@link #serverTransactionExpireThread} if - * necessary. - */ - private void maybeStartServerTransactionExpireThread() - { - synchronized (serverTransactions) - { - if (!serverTransactions.isEmpty() - && (serverTransactionExpireThread == null)) - { - Thread t - = new Thread() - { - @Override - public void run() - { - runInServerTransactionExpireThread(); - } - }; - - t.setDaemon(true); - t.setName( - getClass().getName() - + ".serverTransactionExpireThread"); - - boolean started = false; - - serverTransactionExpireThread = t; - try - { - t.start(); - started = true; - } - finally - { - if (!started && (serverTransactionExpireThread == t)) - serverTransactionExpireThread = null; - } - } - } - } - - /** - * Runs in {@link #serverTransactionExpireThread} and expires the - * StunServerTransactions of this StunStack and removes - * them from {@link #serverTransactions}. - */ - private void runInServerTransactionExpireThread() - { - try - { - long idleStartTime = -1; - - do - { - synchronized (serverTransactions) - { - try - { - serverTransactions.wait(StunServerTransaction.LIFETIME); - } - catch (InterruptedException ie) - { - } - - /* - * Is the current Thread still designated to expire the - * StunServerTransactions of this StunStack? - */ - if (Thread.currentThread() != serverTransactionExpireThread) - break; - - long now = System.currentTimeMillis(); - - /* - * Has the current Thread been idle long enough to merit - * disposing of it? - */ - if (serverTransactions.isEmpty()) - { - if (idleStartTime == -1) - idleStartTime = now; - else if (now - idleStartTime > 60 * 1000) - break; - } - else - { - // Expire the StunServerTransactions of this StunStack. - - idleStartTime = -1; - - for (Iterator i - = serverTransactions.values().iterator(); - i.hasNext();) - { - StunServerTransaction serverTransaction = i.next(); - - if (serverTransaction == null) - { - i.remove(); - } - else if (serverTransaction.isExpired(now)) - { - i.remove(); - serverTransaction.expire(); - } - } - } - } - } - while (true); - } - finally - { - synchronized (serverTransactions) - { - if (serverTransactionExpireThread == Thread.currentThread()) - serverTransactionExpireThread = null; - /* - * If serverTransactionExpireThread dies unexpectedly and yet it - * is still necessary, resurrect it. - */ - if (serverTransactionExpireThread == null) - maybeStartServerTransactionExpireThread(); - } - } - } - /** * Returns the Error Response object with specified errorCode and * reasonPhrase corresponding to input type. @@ -1639,4 +1522,117 @@ public class StunStack } } + /** + * Class which performs periodic collection of expired transactions. + * It's execution is controlled outside by {@link #schedule()} + * and {@link #cancel()} methods. Whenever expired transactions collector + * is scheduled it does self reschedule with fixed delay + * of StunServerTransaction.LIFETIME_MILLIS, + * until {@link #serverTransactions} is empty, in that case it self-cancel + * further execution and need to be scheduled again when new item added + * to {@link #serverTransactions} container. + */ + private final class ExpiredServerTransactionsCollector + { + /** + * Runnable which walks {@link #serverTransactions}, check if + * transaction is expired and if so - remove it + * from {@link #serverTransactions}. + * Self-cancels when {@link #serverTransactions} is empty + */ + private final Runnable collector = new Runnable() + { + @Override + public void run() + { + try + { + synchronized (serverTransactions) + { + final int transactionsBeforeCollection + = serverTransactions.size(); + + long now = System.currentTimeMillis(); + + for (Iterator i + = serverTransactions.values().iterator(); + i.hasNext();) + { + StunServerTransaction serverTransaction = i.next(); + + if (serverTransaction == null) + { + i.remove(); + } + else if (serverTransaction.isExpired(now)) + { + i.remove(); + serverTransaction.expire(); + } + } + + logger.fine("Non-expired server transactions " + + "count " + serverTransactions.size() + + ", transactions before collection " + + transactionsBeforeCollection); + + if (serverTransactions.isEmpty()) + { + cancel(); + logger.finest("Cancel expired collector " + + "due to no more server transactions"); + } + } + } + catch (Throwable t) + { + logger.log(Level.FINE, + "Failed to expire server transactions", t); + } + } + }; + + /** + * Scheduled execution of {@link #collector} runnable. + * Access synchronized via {@link #serverTransactions}. + */ + private ScheduledFuture scheduledCollectorFuture; + + /** + * Schedules repeated collector execution in background + * task executor. If collector is already scheduled - do nothing + */ + void schedule() + { + synchronized (serverTransactions) + { + if (scheduledCollectorFuture == null || + scheduledCollectorFuture.isDone()) + { + scheduledCollectorFuture + = tasksScheduler.scheduleWithFixedDelay( + collector, + StunServerTransaction.LIFETIME, + StunServerTransaction.LIFETIME, + TimeUnit.MILLISECONDS); + } + } + } + + /** + * Cancels execution of scheduled expired transactions collector if + * it is running + */ + void cancel() + { + synchronized (serverTransactions) + { + if (scheduledCollectorFuture != null) + { + scheduledCollectorFuture.cancel(false); + scheduledCollectorFuture = null; + } + } + } + } } diff --git a/src/main/java/org/ice4j/util/ExecutorFactory.java b/src/main/java/org/ice4j/util/ExecutorFactory.java new file mode 100644 index 0000000..b85adbe --- /dev/null +++ b/src/main/java/org/ice4j/util/ExecutorFactory.java @@ -0,0 +1,115 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * + * Copyright @ 2018 Jitsi.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.ice4j.util; + +import java.util.concurrent.*; + +/** + * Helper class which contains functions to create pre-configured executors + */ +public class ExecutorFactory +{ + /** + * Create {@link ScheduledExecutorService} with single executor thread + * @param threadNamePrefix - name prefix for threads created by pool + * @param threadKeepAliveTime - keep alive time before idle thread is freed + * @param timeUnit - time unit of threadKeepAliveTime + * @return pre-configured {@link ScheduledExecutorService} + */ + public static ScheduledExecutorService createSingleThreadScheduledExecutor( + String threadNamePrefix, + int threadKeepAliveTime, + TimeUnit timeUnit) + { + return createScheduledExecutor( + 1, + threadNamePrefix, + threadKeepAliveTime, + timeUnit); + } + + /** + * Create {@link ScheduledExecutorService} with number of threads up to + * number of CPU cores on machine + * @param threadNamePrefix - name prefix for threads created by pool + * @param threadKeepAliveTime - keep alive time before idle thread is freed + * @param timeUnit - time unit of threadKeepAliveTime + * @return pre-configured {@link ScheduledExecutorService} + */ + public static ScheduledExecutorService createCPUBoundScheduledExecutor( + String threadNamePrefix, + int threadKeepAliveTime, + TimeUnit timeUnit) + { + return createScheduledExecutor( + Runtime.getRuntime().availableProcessors(), + threadNamePrefix, + threadKeepAliveTime, + timeUnit); + } + /** + * Creates pre-configured {@link ScheduledExecutorService} instance with + * defaults suitable for ice4j + * @param threadNamePrefix - name prefix for threads created by pool + * @param poolSize - max number of threads to keep in pool. + * @param threadKeepAliveTime - keep alive time before idle thread is freed + * @param timeUnit - time unit of threadKeepAliveTime + * @return pre-configured {@link ScheduledExecutorService} + */ + private static ScheduledExecutorService createScheduledExecutor( + int poolSize, + String threadNamePrefix, + int threadKeepAliveTime, + TimeUnit timeUnit) + { + CustomizableThreadFactory threadFactory + = new CustomizableThreadFactory(threadNamePrefix, true); + + // Motivation: + // The desired behaviour from pool is that it creates limited number + // of threads based on current load, as well as releasing threads when + // there is no work to execute. + // Based on these requirements the following default configuration is + // chosen. + // corePoolSize for {@link ScheduledThreadPoolExecutor} is + // behaved both as corePoolSize and maxPoolSize, so + // it is actually fixed-size pool. + // Even so spec says that corePoolSize is number of + // threads to keep in pool they are actually created on demand, so if + // there is no load, then threads are not created. But until pool has + // less than corePoolSize threads, pool will create new thread, + // to execute scheduled task, even though already created threads are + // idle and have no tasks to execute. + // keepAliveTime is configurable and specifies timeout before + // idle core thread deleted from pool + // removeOnCancelPolicy is set to true, to immediately remove + // queued task from pool queue, because some task might be scheduled + // with very big delay causing having reference to creator from pool + // + // Having corePoolSize is set to 0 with unlimited pool size + // maximumPoolSize is observed to create only 1 thread in pool + // no matter how many task are queued and become eligible to execute. + final ScheduledThreadPoolExecutor executor + = new ScheduledThreadPoolExecutor(poolSize, threadFactory); + executor.setKeepAliveTime(threadKeepAliveTime, timeUnit); + executor.allowCoreThreadTimeOut(true); + executor.setRemoveOnCancelPolicy(true); + return Executors.unconfigurableScheduledExecutorService(executor); + } +}