Merge branch 'aggressive-polling' into 'master'

Try harder to connect to contacts

* When an outgoing connection is lost, try to reconnect to the contact straight away
* Use periodic polling for Tor, regardless of whether our hidden service descriptor has been published
* Reduce polling intervals for all plugins (this can be reverted if we solve the connectivity issues)

Closes #262, #314. Hopefully helps with #361.

See merge request !177
This commit is contained in:
akwizgran
2016-05-10 14:47:04 +00:00
18 changed files with 230 additions and 127 deletions

View File

@@ -144,7 +144,7 @@ class ConnectionManagerImpl implements ConnectionManager {
return;
}
ContactId contactId = ctx.getContactId();
connectionRegistry.registerConnection(contactId, transportId);
connectionRegistry.registerConnection(contactId, transportId, true);
try {
// Create and run the incoming session
createIncomingSession(ctx, reader).run();
@@ -153,7 +153,8 @@ class ConnectionManagerImpl implements ConnectionManager {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
disposeReader(true, true);
} finally {
connectionRegistry.unregisterConnection(contactId, transportId);
connectionRegistry.unregisterConnection(contactId, transportId,
true);
}
}
@@ -194,7 +195,8 @@ class ConnectionManagerImpl implements ConnectionManager {
disposeWriter(true);
return;
}
connectionRegistry.registerConnection(contactId, transportId);
connectionRegistry.registerConnection(contactId, transportId,
false);
try {
// Create and run the outgoing session
createSimplexOutgoingSession(ctx, writer).run();
@@ -203,7 +205,8 @@ class ConnectionManagerImpl implements ConnectionManager {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
disposeWriter(true);
} finally {
connectionRegistry.unregisterConnection(contactId, transportId);
connectionRegistry.unregisterConnection(contactId, transportId,
false);
}
}
@@ -254,7 +257,7 @@ class ConnectionManagerImpl implements ConnectionManager {
return;
}
contactId = ctx.getContactId();
connectionRegistry.registerConnection(contactId, transportId);
connectionRegistry.registerConnection(contactId, transportId, true);
// Start the outgoing session on another thread
ioExecutor.execute(new Runnable() {
public void run() {
@@ -270,7 +273,8 @@ class ConnectionManagerImpl implements ConnectionManager {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
disposeReader(true, true);
} finally {
connectionRegistry.unregisterConnection(contactId, transportId);
connectionRegistry.unregisterConnection(contactId, transportId,
true);
}
}
@@ -398,7 +402,8 @@ class ConnectionManagerImpl implements ConnectionManager {
disposeReader(true, true);
return;
}
connectionRegistry.registerConnection(contactId, transportId);
connectionRegistry.registerConnection(contactId, transportId,
false);
try {
// Create and run the incoming session
incomingSession = createIncomingSession(ctx, reader);
@@ -408,7 +413,8 @@ class ConnectionManagerImpl implements ConnectionManager {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
disposeReader(true, true);
} finally {
connectionRegistry.unregisterConnection(contactId, transportId);
connectionRegistry.unregisterConnection(contactId, transportId,
false);
}
}

View File

@@ -2,6 +2,8 @@ package org.briarproject.plugins;
import org.briarproject.api.TransportId;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.event.ConnectionClosedEvent;
import org.briarproject.api.event.ConnectionOpenedEvent;
import org.briarproject.api.event.ContactConnectedEvent;
import org.briarproject.api.event.ContactDisconnectedEvent;
import org.briarproject.api.event.EventBus;
@@ -40,8 +42,12 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
contactCounts = new HashMap<ContactId, Integer>();
}
public void registerConnection(ContactId c, TransportId t) {
if (LOG.isLoggable(INFO)) LOG.info("Connection registered: " + t);
public void registerConnection(ContactId c, TransportId t,
boolean incoming) {
if (LOG.isLoggable(INFO)) {
if (incoming) LOG.info("Incoming connection registered: " + t);
else LOG.info("Outgoing connection registered: " + t);
}
boolean firstConnection = false;
lock.lock();
try {
@@ -63,14 +69,19 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
} finally {
lock.unlock();
}
eventBus.broadcast(new ConnectionOpenedEvent(c, t, incoming));
if (firstConnection) {
LOG.info("Contact connected");
eventBus.broadcast(new ContactConnectedEvent(c));
}
}
public void unregisterConnection(ContactId c, TransportId t) {
if (LOG.isLoggable(INFO)) LOG.info("Connection unregistered: " + t);
public void unregisterConnection(ContactId c, TransportId t,
boolean incoming) {
if (LOG.isLoggable(INFO)) {
if (incoming) LOG.info("Incoming connection unregistered: " + t);
else LOG.info("Outgoing connection unregistered: " + t);
}
boolean lastConnection = false;
lock.lock();
try {
@@ -94,6 +105,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
} finally {
lock.unlock();
}
eventBus.broadcast(new ConnectionClosedEvent(c, t, incoming));
if (lastConnection) {
LOG.info("Contact disconnected");
eventBus.broadcast(new ContactDisconnectedEvent(c));

View File

@@ -3,6 +3,7 @@ package org.briarproject.plugins;
import org.briarproject.api.TransportId;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.db.DbException;
import org.briarproject.api.event.ConnectionClosedEvent;
import org.briarproject.api.event.ContactStatusChangedEvent;
import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventBus;
@@ -141,10 +142,12 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
}
}
@Override
public Plugin getPlugin(TransportId t) {
return plugins.get(t);
}
@Override
public Collection<DuplexPlugin> getInvitationPlugins() {
List<DuplexPlugin> supported = new ArrayList<DuplexPlugin>();
for (DuplexPlugin d : duplexPlugins)
@@ -152,6 +155,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
return Collections.unmodifiableList(supported);
}
@Override
public Collection<DuplexPlugin> getKeyAgreementPlugins() {
List<DuplexPlugin> supported = new ArrayList<DuplexPlugin>();
for (DuplexPlugin d : duplexPlugins)
@@ -163,7 +167,16 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
public void eventOccurred(Event e) {
if (e instanceof ContactStatusChangedEvent) {
ContactStatusChangedEvent c = (ContactStatusChangedEvent) e;
if (c.isActive()) connectToContact(c.getContactId());
if (c.isActive()) {
// Connect to the newly activated contact
connectToContact(c.getContactId());
}
} else if (e instanceof ConnectionClosedEvent) {
ConnectionClosedEvent c = (ConnectionClosedEvent) e;
if (!c.isIncoming()) {
// Connect to the disconnected contact
connectToContact(c.getContactId(), c.getTransportId());
}
}
}
@@ -174,8 +187,17 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
if (d.shouldPoll()) connectToContact(c, d);
}
private void connectToContact(ContactId c, TransportId t) {
Plugin p = plugins.get(t);
if (p instanceof SimplexPlugin && p.shouldPoll())
connectToContact(c, (SimplexPlugin) p);
else if (p instanceof DuplexPlugin && p.shouldPoll())
connectToContact(c, (DuplexPlugin) p);
}
private void connectToContact(final ContactId c, final SimplexPlugin p) {
ioExecutor.execute(new Runnable() {
@Override
public void run() {
TransportId t = p.getId();
if (!connectionRegistry.isConnected(c, t)) {
@@ -189,6 +211,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
private void connectToContact(final ContactId c, final DuplexPlugin p) {
ioExecutor.execute(new Runnable() {
@Override
public void run() {
TransportId t = p.getId();
if (!connectionRegistry.isConnected(c, t)) {
@@ -211,6 +234,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
this.latch = latch;
}
@Override
public void run() {
try {
TransportId id = factory.getId();
@@ -230,7 +254,6 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
if (started) {
plugins.put(id, plugin);
simplexPlugins.add(plugin);
if (plugin.shouldPoll()) poller.addPlugin(plugin);
if (LOG.isLoggable(INFO)) {
String name = plugin.getClass().getSimpleName();
LOG.info("Starting " + name + " took " +
@@ -263,6 +286,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
this.latch = latch;
}
@Override
public void run() {
try {
TransportId id = factory.getId();
@@ -282,7 +306,6 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
if (started) {
plugins.put(id, plugin);
duplexPlugins.add(plugin);
if (plugin.shouldPoll()) poller.addPlugin(plugin);
if (LOG.isLoggable(INFO)) {
String name = plugin.getClass().getSimpleName();
LOG.info("Starting " + name + " took " +
@@ -314,6 +337,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
this.latch = latch;
}
@Override
public void run() {
try {
long start = System.currentTimeMillis();
@@ -339,6 +363,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
this.id = id;
}
@Override
public Settings getSettings() {
try {
return settingsManager.getSettings(id.getString());
@@ -348,6 +373,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
}
}
@Override
public TransportProperties getLocalProperties() {
try {
TransportProperties p =
@@ -359,6 +385,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
}
}
@Override
public Map<ContactId, TransportProperties> getRemoteProperties() {
try {
return transportPropertyManager.getRemoteProperties(id);
@@ -368,6 +395,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
}
}
@Override
public void mergeSettings(Settings s) {
try {
settingsManager.mergeSettings(s, id.getString());
@@ -376,6 +404,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
}
}
@Override
public void mergeLocalProperties(TransportProperties p) {
try {
transportPropertyManager.mergeLocalProperties(id, p);
@@ -384,24 +413,29 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
}
}
@Override
public int showChoice(String[] options, String... message) {
return uiCallback.showChoice(options, message);
}
@Override
public boolean showConfirmationMessage(String... message) {
return uiCallback.showConfirmationMessage(message);
}
@Override
public void showMessage(String... message) {
uiCallback.showMessage(message);
}
@Override
public void transportEnabled() {
eventBus.broadcast(new TransportEnabledEvent(id));
Plugin p = plugins.get(id);
if (p != null) poller.pollNow(p);
}
@Override
public void transportDisabled() {
eventBus.broadcast(new TransportDisabledEvent(id));
}
@@ -414,10 +448,12 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
super(id);
}
@Override
public void readerCreated(TransportConnectionReader r) {
connectionManager.manageIncomingConnection(id, r);
}
@Override
public void writerCreated(ContactId c, TransportConnectionWriter w) {
connectionManager.manageOutgoingConnection(c, id, w);
}
@@ -430,10 +466,12 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
super(id);
}
@Override
public void incomingConnectionCreated(DuplexTransportConnection d) {
connectionManager.manageIncomingConnection(id, d);
}
@Override
public void outgoingConnectionCreated(ContactId c,
DuplexTransportConnection d) {
connectionManager.manageOutgoingConnection(c, id, d);

View File

@@ -4,9 +4,6 @@ import org.briarproject.api.plugins.Plugin;
interface Poller {
/** Adds the given plugin to the collection of plugins to be polled. */
void addPlugin(Plugin p);
/** Tells the poller to poll the given plugin immediately. */
void pollNow(Plugin p);

View File

@@ -39,25 +39,17 @@ class PollerImpl implements Poller {
tasks = new ConcurrentHashMap<TransportId, PollTask>();
}
@Override
public void stop() {
timer.cancel();
}
public void addPlugin(Plugin p) {
// Randomise first polling interval
if (p.shouldPoll())
schedule(p, randomise(p.getPollingInterval()), false);
}
@Override
public void pollNow(Plugin p) {
// Randomise next polling interval
if (p.shouldPoll()) schedule(p, 0, true);
}
private int randomise(int interval) {
return (int) (interval * random.nextDouble());
}
private void schedule(Plugin p, int interval, boolean randomiseNext) {
// Replace any previously scheduled task for this plugin
PollTask task = new PollTask(p, randomiseNext);
@@ -68,6 +60,7 @@ class PollerImpl implements Poller {
private void poll(final Plugin p) {
ioExecutor.execute(new Runnable() {
@Override
public void run() {
if (LOG.isLoggable(INFO))
LOG.info("Polling " + p.getClass().getSimpleName());
@@ -90,7 +83,8 @@ class PollerImpl implements Poller {
public void run() {
tasks.remove(plugin.getId());
int interval = plugin.getPollingInterval();
if (randomiseNext) interval = randomise(interval);
if (randomiseNext)
interval = (int) (interval * random.nextDouble());
schedule(plugin, interval, false);
poll(plugin);
}

View File

@@ -13,8 +13,8 @@ public class LanTcpPluginFactory implements DuplexPluginFactory {
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
private static final int MIN_POLLING_INTERVAL = 2 * 60 * 1000; // 2 minutes
private static final int MAX_POLLING_INTERVAL = 60 * 60 * 1000; // 1 hour
private static final int MIN_POLLING_INTERVAL = 60 * 1000; // 1 minute
private static final int MAX_POLLING_INTERVAL = 10 * 60 * 1000; // 10 mins
private static final double BACKOFF_BASE = 1.2;
private final Executor ioExecutor;
@@ -26,14 +26,17 @@ public class LanTcpPluginFactory implements DuplexPluginFactory {
this.backoffFactory = backoffFactory;
}
@Override
public TransportId getId() {
return LanTcpPlugin.ID;
}
@Override
public int getMaxLatency() {
return MAX_LATENCY;
}
@Override
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
MAX_POLLING_INTERVAL, BACKOFF_BASE);

View File

@@ -14,8 +14,8 @@ public class WanTcpPluginFactory implements DuplexPluginFactory {
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
private static final int MIN_POLLING_INTERVAL = 2 * 60 * 1000; // 2 minutes
private static final int MAX_POLLING_INTERVAL = 60 * 60 * 1000; // 1 hour
private static final int MIN_POLLING_INTERVAL = 60 * 1000; // 1 minute
private static final int MAX_POLLING_INTERVAL = 10 * 60 * 1000; // 10 mins
private static final double BACKOFF_BASE = 1.2;
private final Executor ioExecutor;
@@ -29,14 +29,17 @@ public class WanTcpPluginFactory implements DuplexPluginFactory {
this.shutdownManager = shutdownManager;
}
@Override
public TransportId getId() {
return WanTcpPlugin.ID;
}
@Override
public int getMaxLatency() {
return MAX_LATENCY;
}
@Override
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
MAX_POLLING_INTERVAL, BACKOFF_BASE);