Skip to content
Snippets Groups Projects
Commit e4676517 authored by akwizgran's avatar akwizgran
Browse files

Merge branch '524-check-that-acra-is-catching-all-uncaught-exceptions' into 'master'

Introduce a @Scheduler annotation

and make sure work is offloaded to an executor, so exceptions can be caught.

Closes #524

See merge request !422
parents dbbeb374 20708bc1
No related branches found
No related tags found
No related merge requests found
package org.briarproject.api.system;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Annotation for injecting a scheduled executor service
* that can be used to schedule the execution of tasks.
* <p>
* The service should <b>only</b> be used for running tasks on other executors
* at scheduled times.
* No significant work should be run by the service itself!
*/
@Qualifier
@Target({FIELD, METHOD, PARAMETER})
@Retention(RUNTIME)
public @interface Scheduler {
}
\ No newline at end of file
...@@ -29,6 +29,7 @@ import org.briarproject.api.feed.FeedManager; ...@@ -29,6 +29,7 @@ import org.briarproject.api.feed.FeedManager;
import org.briarproject.api.identity.IdentityManager; import org.briarproject.api.identity.IdentityManager;
import org.briarproject.api.identity.LocalAuthor; import org.briarproject.api.identity.LocalAuthor;
import org.briarproject.api.lifecycle.IoExecutor; import org.briarproject.api.lifecycle.IoExecutor;
import org.briarproject.api.system.Scheduler;
import org.briarproject.api.plugins.TorConstants; import org.briarproject.api.plugins.TorConstants;
import org.briarproject.api.sync.Group; import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId; import org.briarproject.api.sync.GroupId;
...@@ -74,7 +75,7 @@ class FeedManagerImpl implements FeedManager, Client, EventListener { ...@@ -74,7 +75,7 @@ class FeedManagerImpl implements FeedManager, Client, EventListener {
private static final int CONNECT_TIMEOUT = 60 * 1000; // Milliseconds private static final int CONNECT_TIMEOUT = 60 * 1000; // Milliseconds
private final ScheduledExecutorService feedExecutor; private final ScheduledExecutorService scheduler;
private final Executor ioExecutor; private final Executor ioExecutor;
private final DatabaseComponent db; private final DatabaseComponent db;
private final ContactGroupFactory contactGroupFactory; private final ContactGroupFactory contactGroupFactory;
...@@ -92,13 +93,13 @@ class FeedManagerImpl implements FeedManager, Client, EventListener { ...@@ -92,13 +93,13 @@ class FeedManagerImpl implements FeedManager, Client, EventListener {
Clock clock; Clock clock;
@Inject @Inject
FeedManagerImpl(ScheduledExecutorService feedExecutor, FeedManagerImpl(@Scheduler ScheduledExecutorService scheduler,
@IoExecutor Executor ioExecutor, DatabaseComponent db, @IoExecutor Executor ioExecutor, DatabaseComponent db,
ContactGroupFactory contactGroupFactory, ClientHelper clientHelper, ContactGroupFactory contactGroupFactory, ClientHelper clientHelper,
IdentityManager identityManager, BlogManager blogManager, IdentityManager identityManager, BlogManager blogManager,
SocketFactory torSocketFactory) { SocketFactory torSocketFactory) {
this.feedExecutor = feedExecutor; this.scheduler = scheduler;
this.ioExecutor = ioExecutor; this.ioExecutor = ioExecutor;
this.db = db; this.db = db;
this.contactGroupFactory = contactGroupFactory; this.contactGroupFactory = contactGroupFactory;
...@@ -132,7 +133,7 @@ class FeedManagerImpl implements FeedManager, Client, EventListener { ...@@ -132,7 +133,7 @@ class FeedManagerImpl implements FeedManager, Client, EventListener {
}); });
} }
}; };
feedExecutor.scheduleWithFixedDelay(fetcher, FETCH_DELAY_INITIAL, scheduler.scheduleWithFixedDelay(fetcher, FETCH_DELAY_INITIAL,
FETCH_INTERVAL, FETCH_UNIT); FETCH_INTERVAL, FETCH_UNIT);
} }
......
...@@ -8,6 +8,7 @@ import org.briarproject.api.plugins.ConnectionManager; ...@@ -8,6 +8,7 @@ import org.briarproject.api.plugins.ConnectionManager;
import org.briarproject.api.plugins.ConnectionRegistry; import org.briarproject.api.plugins.ConnectionRegistry;
import org.briarproject.api.plugins.PluginManager; import org.briarproject.api.plugins.PluginManager;
import org.briarproject.api.system.Clock; import org.briarproject.api.system.Clock;
import org.briarproject.api.system.Scheduler;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
...@@ -37,7 +38,7 @@ public class PluginsModule { ...@@ -37,7 +38,7 @@ public class PluginsModule {
@Provides @Provides
@Singleton @Singleton
Poller providePoller(@IoExecutor Executor ioExecutor, Poller providePoller(@IoExecutor Executor ioExecutor,
ScheduledExecutorService scheduler, @Scheduler ScheduledExecutorService scheduler,
ConnectionManager connectionManager, ConnectionManager connectionManager,
ConnectionRegistry connectionRegistry, PluginManager pluginManager, ConnectionRegistry connectionRegistry, PluginManager pluginManager,
SecureRandom random, Clock clock, EventBus eventBus) { SecureRandom random, Clock clock, EventBus eventBus) {
......
...@@ -18,6 +18,7 @@ import org.briarproject.api.plugins.duplex.DuplexPlugin; ...@@ -18,6 +18,7 @@ import org.briarproject.api.plugins.duplex.DuplexPlugin;
import org.briarproject.api.plugins.duplex.DuplexTransportConnection; import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
import org.briarproject.api.plugins.simplex.SimplexPlugin; import org.briarproject.api.plugins.simplex.SimplexPlugin;
import org.briarproject.api.system.Clock; import org.briarproject.api.system.Clock;
import org.briarproject.api.system.Scheduler;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.HashMap; import java.util.HashMap;
...@@ -48,7 +49,8 @@ class Poller implements EventListener { ...@@ -48,7 +49,8 @@ class Poller implements EventListener {
private final Map<TransportId, PollTask> tasks; // Locking: lock private final Map<TransportId, PollTask> tasks; // Locking: lock
@Inject @Inject
Poller(@IoExecutor Executor ioExecutor, ScheduledExecutorService scheduler, Poller(@IoExecutor Executor ioExecutor,
@Scheduler ScheduledExecutorService scheduler,
ConnectionManager connectionManager, ConnectionManager connectionManager,
ConnectionRegistry connectionRegistry, PluginManager pluginManager, ConnectionRegistry connectionRegistry, PluginManager pluginManager,
SecureRandom random, Clock clock) { SecureRandom random, Clock clock) {
...@@ -135,13 +137,14 @@ class Poller implements EventListener { ...@@ -135,13 +137,14 @@ class Poller implements EventListener {
private void reschedule(TransportId t) { private void reschedule(TransportId t) {
Plugin p = pluginManager.getPlugin(t); Plugin p = pluginManager.getPlugin(t);
if (p.shouldPoll()) schedule(p, p.getPollingInterval(), false); if (p != null && p.shouldPoll())
schedule(p, p.getPollingInterval(), false);
} }
private void pollNow(TransportId t) { private void pollNow(TransportId t) {
Plugin p = pluginManager.getPlugin(t); Plugin p = pluginManager.getPlugin(t);
// Randomise next polling interval // Randomise next polling interval
if (p.shouldPoll()) schedule(p, 0, true); if (p != null && p.shouldPoll()) schedule(p, 0, true);
} }
private void schedule(Plugin p, int delay, boolean randomiseNext) { private void schedule(Plugin p, int delay, boolean randomiseNext) {
...@@ -152,24 +155,25 @@ class Poller implements EventListener { ...@@ -152,24 +155,25 @@ class Poller implements EventListener {
try { try {
PollTask scheduled = tasks.get(t); PollTask scheduled = tasks.get(t);
if (scheduled == null || due < scheduled.due) { if (scheduled == null || due < scheduled.due) {
PollTask task = new PollTask(p, due, randomiseNext); final PollTask task = new PollTask(p, due, randomiseNext);
tasks.put(t, task); tasks.put(t, task);
scheduler.schedule(task, delay, MILLISECONDS); scheduler.schedule(new Runnable() {
@Override
public void run() {
ioExecutor.execute(task);
}
}, delay, MILLISECONDS);
} }
} finally { } finally {
lock.unlock(); lock.unlock();
} }
} }
@IoExecutor
private void poll(final Plugin p) { private void poll(final Plugin p) {
ioExecutor.execute(new Runnable() { TransportId t = p.getId();
@Override if (LOG.isLoggable(INFO)) LOG.info("Polling plugin " + t);
public void run() { p.poll(connectionRegistry.getConnectedContacts(t));
TransportId t = p.getId();
if (LOG.isLoggable(INFO)) LOG.info("Polling plugin " + t);
p.poll(connectionRegistry.getConnectedContacts(t));
}
});
} }
private class PollTask implements Runnable { private class PollTask implements Runnable {
...@@ -185,6 +189,7 @@ class Poller implements EventListener { ...@@ -185,6 +189,7 @@ class Poller implements EventListener {
} }
@Override @Override
@IoExecutor
public void run() { public void run() {
lock.lock(); lock.lock();
try { try {
......
...@@ -2,6 +2,7 @@ package org.briarproject.system; ...@@ -2,6 +2,7 @@ package org.briarproject.system;
import org.briarproject.api.lifecycle.LifecycleManager; import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.system.Clock; import org.briarproject.api.system.Clock;
import org.briarproject.api.system.Scheduler;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
...@@ -17,6 +18,7 @@ public class SystemModule { ...@@ -17,6 +18,7 @@ public class SystemModule {
public static class EagerSingletons { public static class EagerSingletons {
@Inject @Inject
@Scheduler
ScheduledExecutorService scheduledExecutorService; ScheduledExecutorService scheduledExecutorService;
} }
...@@ -33,6 +35,7 @@ public class SystemModule { ...@@ -33,6 +35,7 @@ public class SystemModule {
@Provides @Provides
@Singleton @Singleton
@Scheduler
ScheduledExecutorService provideScheduledExecutorService( ScheduledExecutorService provideScheduledExecutorService(
LifecycleManager lifecycleManager) { LifecycleManager lifecycleManager) {
lifecycleManager.registerForShutdown(scheduler); lifecycleManager.registerForShutdown(scheduler);
......
...@@ -5,6 +5,7 @@ import org.briarproject.api.crypto.CryptoComponent; ...@@ -5,6 +5,7 @@ import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.db.DatabaseComponent; import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DatabaseExecutor; import org.briarproject.api.db.DatabaseExecutor;
import org.briarproject.api.system.Clock; import org.briarproject.api.system.Clock;
import org.briarproject.api.system.Scheduler;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
...@@ -23,7 +24,7 @@ class TransportKeyManagerFactoryImpl implements ...@@ -23,7 +24,7 @@ class TransportKeyManagerFactoryImpl implements
@Inject @Inject
TransportKeyManagerFactoryImpl(DatabaseComponent db, CryptoComponent crypto, TransportKeyManagerFactoryImpl(DatabaseComponent db, CryptoComponent crypto,
@DatabaseExecutor Executor dbExecutor, @DatabaseExecutor Executor dbExecutor,
ScheduledExecutorService scheduler, Clock clock) { @Scheduler ScheduledExecutorService scheduler, Clock clock) {
this.db = db; this.db = db;
this.crypto = crypto; this.crypto = crypto;
this.dbExecutor = dbExecutor; this.dbExecutor = dbExecutor;
......
...@@ -9,6 +9,7 @@ import org.briarproject.api.db.DatabaseComponent; ...@@ -9,6 +9,7 @@ import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException; import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Transaction; import org.briarproject.api.db.Transaction;
import org.briarproject.api.system.Clock; import org.briarproject.api.system.Clock;
import org.briarproject.api.system.Scheduler;
import org.briarproject.api.transport.StreamContext; import org.briarproject.api.transport.StreamContext;
import org.briarproject.api.transport.TransportKeys; import org.briarproject.api.transport.TransportKeys;
import org.briarproject.transport.ReorderingWindow.Change; import org.briarproject.transport.ReorderingWindow.Change;
...@@ -48,7 +49,7 @@ class TransportKeyManagerImpl implements TransportKeyManager { ...@@ -48,7 +49,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
private final Map<ContactId, MutableTransportKeys> keys; private final Map<ContactId, MutableTransportKeys> keys;
TransportKeyManagerImpl(DatabaseComponent db, CryptoComponent crypto, TransportKeyManagerImpl(DatabaseComponent db, CryptoComponent crypto,
Executor dbExecutor, ScheduledExecutorService scheduler, Executor dbExecutor, @Scheduler ScheduledExecutorService scheduler,
Clock clock, TransportId transportId, long maxLatency) { Clock clock, TransportId transportId, long maxLatency) {
this.db = db; this.db = db;
this.crypto = crypto; this.crypto = crypto;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment