Use an unbounded executor for DB tasks, which may depend on each other.

This commit is contained in:
akwizgran
2013-03-15 16:34:00 +00:00
parent f076a65e0a
commit 3fd23830c9

View File

@@ -1,7 +1,12 @@
package net.sf.briar.db;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.sql.Connection;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import net.sf.briar.api.clock.Clock;
import net.sf.briar.api.clock.SystemClock;
@@ -9,7 +14,6 @@ import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DatabaseConfig;
import net.sf.briar.api.db.DatabaseExecutor;
import net.sf.briar.api.lifecycle.ShutdownManager;
import net.sf.briar.util.BoundedExecutor;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
@@ -17,27 +21,23 @@ import com.google.inject.Singleton;
public class DatabaseModule extends AbstractModule {
// FIXME: Determine suitable values for these constants empirically
/**
* The maximum number of database tasks that can be queued for execution
* before submitting another task will block.
*/
private static final int MAX_QUEUED_DB_TASKS = 1000;
/** The minimum number of database threads to keep in the pool. */
private static final int MIN_DB_THREADS = 1;
/** The maximum number of database threads. */
private static final int MAX_DB_THREADS = 10;
/** The time in milliseconds to keep unused database threads alive. */
private static final int DB_KEEPALIVE = 60 * 1000;
@Override
protected void configure() {
bind(DatabaseCleaner.class).to(DatabaseCleanerImpl.class);
// The executor is bounded, so tasks must be independent and short-lived
// Database tasks may depend on each other, so use an unbounded queue
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
bind(Executor.class).annotatedWith(DatabaseExecutor.class).toInstance(
new BoundedExecutor(MAX_QUEUED_DB_TASKS, MIN_DB_THREADS,
MAX_DB_THREADS));
new ThreadPoolExecutor(MIN_DB_THREADS, MAX_DB_THREADS,
DB_KEEPALIVE, MILLISECONDS, queue));
}
@Provides