added a cache to the IdentityManager, changed its signature, modified when and where the author is stored

made the author creation single-threaded again in the LifecycleManager, removed redundant code
This commit is contained in:
Ernir Erlingsson
2016-10-13 11:01:25 +02:00
parent 47d6fc526f
commit eaa393a7ed
45 changed files with 299 additions and 552 deletions

View File

@@ -1,11 +1,16 @@
package org.briarproject.lifecycle;
import org.briarproject.api.clients.Client;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.crypto.KeyPair;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.ShutdownEvent;
import org.briarproject.api.identity.AuthorFactory;
import org.briarproject.api.identity.IdentityManager;
import org.briarproject.api.identity.LocalAuthor;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.lifecycle.Service;
import org.briarproject.api.lifecycle.ServiceException;
@@ -17,6 +22,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Semaphore;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.inject.Inject;
import static java.util.logging.Level.INFO;
@@ -36,15 +42,23 @@ class LifecycleManagerImpl implements LifecycleManager {
private final List<Service> services;
private final List<Client> clients;
private final List<ExecutorService> executors;
private final CryptoComponent crypto;
private final AuthorFactory authorFactory;
private final IdentityManager identityManager;
private final Semaphore startStopSemaphore = new Semaphore(1);
private final CountDownLatch dbLatch = new CountDownLatch(1);
private final CountDownLatch startupLatch = new CountDownLatch(1);
private final CountDownLatch shutdownLatch = new CountDownLatch(1);
@Inject
LifecycleManagerImpl(DatabaseComponent db, EventBus eventBus) {
LifecycleManagerImpl(DatabaseComponent db, EventBus eventBus,
CryptoComponent crypto, AuthorFactory authorFactory,
IdentityManager identityManager) {
this.db = db;
this.eventBus = eventBus;
this.crypto = crypto;
this.authorFactory = authorFactory;
this.identityManager = identityManager;
services = new CopyOnWriteArrayList<Service>();
clients = new CopyOnWriteArrayList<Client>();
executors = new CopyOnWriteArrayList<ExecutorService>();
@@ -70,8 +84,30 @@ class LifecycleManagerImpl implements LifecycleManager {
executors.add(e);
}
private LocalAuthor createLocalAuthor(final String nickname) {
long now = System.currentTimeMillis();
KeyPair keyPair = crypto.generateSignatureKeyPair();
byte[] publicKey = keyPair.getPublic().getEncoded();
byte[] privateKey = keyPair.getPrivate().getEncoded();
LocalAuthor localAuthor = authorFactory
.createLocalAuthor(nickname, publicKey, privateKey);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Identity creation took " + duration + " ms");
return localAuthor;
}
private void registerLocalAuthor(LocalAuthor author) throws DbException {
long now = System.currentTimeMillis();
identityManager.registerLocalAuthor(author);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Author registration took " + duration +
" ms");
}
@Override
public StartResult startServices() {
public StartResult startServices(@Nullable String authorNick) {
if (!startStopSemaphore.tryAcquire()) {
LOG.info("Already starting or stopping");
return ALREADY_RUNNING;
@@ -79,6 +115,7 @@ class LifecycleManagerImpl implements LifecycleManager {
try {
LOG.info("Starting services");
long start = System.currentTimeMillis();
boolean reopened = db.open();
long duration = System.currentTimeMillis() - start;
if (LOG.isLoggable(INFO)) {
@@ -86,6 +123,11 @@ class LifecycleManagerImpl implements LifecycleManager {
LOG.info("Reopening database took " + duration + " ms");
else LOG.info("Creating database took " + duration + " ms");
}
if (authorNick != null) {
registerLocalAuthor(createLocalAuthor(authorNick));
}
dbLatch.countDown();
Transaction txn = db.startTransaction(false);
try {
@@ -181,4 +223,5 @@ class LifecycleManagerImpl implements LifecycleManager {
public void waitForShutdown() throws InterruptedException {
shutdownLatch.await();
}
}

View File

@@ -1,7 +1,10 @@
package org.briarproject.lifecycle;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.identity.AuthorFactory;
import org.briarproject.api.identity.IdentityManager;
import org.briarproject.api.lifecycle.IoExecutor;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.lifecycle.ShutdownManager;
@@ -26,7 +29,8 @@ public class LifecycleModule {
public static class EagerSingletons {
@Inject
@IoExecutor Executor executor;
@IoExecutor
Executor executor;
}
private final ExecutorService ioExecutor;
@@ -51,8 +55,10 @@ public class LifecycleModule {
@Provides
@Singleton
LifecycleManager provideLifecycleManager(DatabaseComponent db,
EventBus eventBus) {
return new LifecycleManagerImpl(db, eventBus);
EventBus eventBus, CryptoComponent crypto,
AuthorFactory authorFactory, IdentityManager identityManager) {
return new LifecycleManagerImpl(db, eventBus, crypto, authorFactory,
identityManager);
}
@Provides