Merge branch '1145-avoid-unnecessary-db-queries' into 'maintenance-0.16'

Backport: Avoid unnecessary DB queries when starting clients

See merge request akwizgran/briar!669
This commit is contained in:
akwizgran
2018-01-16 15:33:14 +00:00
11 changed files with 235 additions and 84 deletions

View File

@@ -2,12 +2,27 @@ package org.briarproject.bramble.test;
import org.briarproject.bramble.api.UniqueId;
import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.identity.Author;
import org.briarproject.bramble.api.identity.AuthorId;
import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.api.sync.ClientId;
import org.briarproject.bramble.api.sync.Group;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.util.IoUtils;
import java.io.File;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_GROUP_DESCRIPTOR_LENGTH;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH;
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
public class TestUtils {
private static final AtomicInteger nextTestDir =
@@ -38,4 +53,50 @@ public class TestUtils {
return new SecretKey(getRandomBytes(SecretKey.LENGTH));
}
public static LocalAuthor getLocalAuthor() {
return getLocalAuthor(1 + random.nextInt(MAX_AUTHOR_NAME_LENGTH));
}
public static LocalAuthor getLocalAuthor(int nameLength) {
AuthorId id = new AuthorId(getRandomId());
String name = getRandomString(nameLength);
byte[] publicKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
byte[] privateKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
long created = System.currentTimeMillis();
return new LocalAuthor(id, name, publicKey, privateKey, created);
}
public static Author getAuthor() {
return getAuthor(1 + random.nextInt(MAX_AUTHOR_NAME_LENGTH));
}
public static Author getAuthor(int nameLength) {
AuthorId id = new AuthorId(getRandomId());
String name = getRandomString(nameLength);
byte[] publicKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
return new Author(id, name, publicKey);
}
public static Group getGroup(ClientId clientId) {
int descriptorLength = 1 + random.nextInt(MAX_GROUP_DESCRIPTOR_LENGTH);
return getGroup(clientId, descriptorLength);
}
public static Group getGroup(ClientId clientId, int descriptorLength) {
GroupId groupId = new GroupId(getRandomId());
byte[] descriptor = getRandomBytes(descriptorLength);
return new Group(groupId, clientId, descriptor);
}
public static Message getMessage(GroupId groupId) {
int bodyLength = 1 + random.nextInt(MAX_MESSAGE_BODY_LENGTH);
return getMessage(groupId, MESSAGE_HEADER_LENGTH + bodyLength);
}
public static Message getMessage(GroupId groupId, int rawLength) {
MessageId id = new MessageId(getRandomId());
byte[] raw = getRandomBytes(rawLength);
long timestamp = System.currentTimeMillis();
return new Message(id, groupId, timestamp, raw);
}
}

View File

@@ -63,6 +63,7 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
@Override
public void createLocalState(Transaction txn) throws DbException {
if (db.containsGroup(txn, localGroup.getId())) return;
db.addGroup(txn, localGroup);
// Ensure we've set things up for any pre-existing contacts
for (Contact c : db.getContacts(txn)) addingContact(txn, c);

View File

@@ -94,6 +94,8 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
Group contactGroup1 = getGroup(), contactGroup2 = getGroup();
context.checking(new Expectations() {{
oneOf(db).containsGroup(txn, localGroup.getId());
will(returnValue(false));
oneOf(db).addGroup(txn, localGroup);
oneOf(db).getContacts(txn);
will(returnValue(contacts));
@@ -123,7 +125,21 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
}
@Test
public void testCreatesGroupWhenAddingContact() throws Exception {
public void testDoesNotCreateGroupsAtStartupIfAlreadyCreated()
throws Exception {
Transaction txn = new Transaction(null, false);
context.checking(new Expectations() {{
oneOf(db).containsGroup(txn, localGroup.getId());
will(returnValue(true));
}});
TransportPropertyManagerImpl t = createInstance();
t.createLocalState(txn);
}
@Test
public void testCreatesContactGroupWhenAddingContact() throws Exception {
Transaction txn = new Transaction(null, false);
Contact contact = getContact(true);
Group contactGroup = getGroup();

View File

@@ -106,7 +106,9 @@ class IntroductionManagerImpl extends ConversationClientImpl
@Override
public void createLocalState(Transaction txn) throws DbException {
db.addGroup(txn, introductionGroupFactory.createLocalGroup());
Group localGroup = introductionGroupFactory.createLocalGroup();
if (db.containsGroup(txn, localGroup.getId())) return;
db.addGroup(txn, localGroup);
// Ensure we've set things up for any pre-existing contacts
for (Contact c : db.getContacts(txn)) addingContact(txn, c);
}

View File

@@ -54,6 +54,10 @@ class MessagingManagerImpl extends ConversationClientImpl
@Override
public void createLocalState(Transaction txn) throws DbException {
// Create a local group to indicate that we've set this client up
Group localGroup = contactGroupFactory.createLocalGroup(CLIENT_ID);
if (db.containsGroup(txn, localGroup.getId())) return;
db.addGroup(txn, localGroup);
// Ensure we've set things up for any pre-existing contacts
for (Contact c : db.getContacts(txn)) addingContact(txn, c);
}

View File

@@ -96,6 +96,10 @@ class GroupInvitationManagerImpl extends ConversationClientImpl
@Override
public void createLocalState(Transaction txn) throws DbException {
// Create a local group to indicate that we've set this client up
Group localGroup = contactGroupFactory.createLocalGroup(CLIENT_ID);
if (db.containsGroup(txn, localGroup.getId())) return;
db.addGroup(txn, localGroup);
// Ensure we've set things up for any pre-existing contacts
for (Contact c : db.getContacts(txn)) addingContact(txn, c);
}

View File

@@ -56,6 +56,9 @@ class BlogSharingManagerImpl extends SharingManagerImpl<Blog>
*/
@Override
public void addingContact(Transaction txn, Contact c) throws DbException {
// Return if we've already set things up for this contact
if (db.containsGroup(txn, getContactGroup(c).getId())) return;
// creates a group to share with the contact
super.addingContact(txn, c);

View File

@@ -82,6 +82,10 @@ abstract class SharingManagerImpl<S extends Shareable>
@Override
public void createLocalState(Transaction txn) throws DbException {
// Create a local group to indicate that we've set this client up
Group localGroup = contactGroupFactory.createLocalGroup(getClientId());
if (db.containsGroup(txn, localGroup.getId())) return;
db.addGroup(txn, localGroup);
// Ensure we've set things up for any pre-existing contacts
for (Contact c : db.getContacts(txn)) addingContact(txn, c);
}

View File

@@ -8,7 +8,6 @@ import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.system.SystemModule;
import org.briarproject.bramble.test.TestUtils;
import org.briarproject.bramble.util.StringUtils;
import org.briarproject.briar.api.forum.ForumConstants;
import org.briarproject.briar.api.forum.ForumPost;
@@ -22,8 +21,8 @@ import javax.inject.Inject;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_RECORD_PAYLOAD_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.briar.api.forum.ForumConstants.MAX_FORUM_POST_BODY_LENGTH;
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH;
import static org.junit.Assert.assertTrue;

View File

@@ -46,6 +46,9 @@ import javax.annotation.Nullable;
import static junit.framework.TestCase.fail;
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getAuthor;
import static org.briarproject.bramble.test.TestUtils.getGroup;
import static org.briarproject.bramble.test.TestUtils.getMessage;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
@@ -92,16 +95,12 @@ public class GroupInvitationManagerImplTest extends BrambleMockTestCase {
private final Transaction txn = new Transaction(null, false);
private final ContactId contactId = new ContactId(0);
private final Author author =
new Author(new AuthorId(getRandomId()), getRandomString(5),
getRandomBytes(5));
private final Contact contact =
new Contact(contactId, author, new AuthorId(getRandomId()), true,
true);
private final Group contactGroup =
new Group(new GroupId(getRandomId()), CLIENT_ID, getRandomBytes(5));
private final Group privateGroup =
new Group(new GroupId(getRandomId()), CLIENT_ID, getRandomBytes(5));
private final Author author = getAuthor();
private final Contact contact = new Contact(contactId, author,
new AuthorId(getRandomId()), true, true);
private final Group localGroup = getGroup(CLIENT_ID);
private final Group contactGroup = getGroup(CLIENT_ID);
private final Group privateGroup = getGroup(CLIENT_ID);
private final BdfDictionary meta = BdfDictionary.of(new BdfEntry("m", "e"));
private final Message message =
new Message(new MessageId(getRandomId()), contactGroup.getId(),
@@ -109,9 +108,7 @@ public class GroupInvitationManagerImplTest extends BrambleMockTestCase {
private final BdfList body = BdfList.of("body");
private final SessionId sessionId =
new SessionId(privateGroup.getId().getBytes());
private final Message storageMessage =
new Message(new MessageId(getRandomId()), contactGroup.getId(),
0L, getRandomBytes(MESSAGE_HEADER_LENGTH + 1));
private final Message storageMessage = getMessage(contactGroup.getId());
private final BdfDictionary bdfSession =
BdfDictionary.of(new BdfEntry("f", "o"));
private final Map<MessageId, BdfDictionary> oneResult =
@@ -150,8 +147,13 @@ public class GroupInvitationManagerImplTest extends BrambleMockTestCase {
}
@Test
public void testCreateLocalState() throws Exception {
public void testCreateLocalStateFirstTime() throws Exception {
context.checking(new Expectations() {{
oneOf(contactGroupFactory).createLocalGroup(CLIENT_ID);
will(returnValue(localGroup));
oneOf(db).containsGroup(txn, localGroup.getId());
will(returnValue(false));
oneOf(db).addGroup(txn, localGroup);
oneOf(db).getContacts(txn);
will(returnValue(Collections.singletonList(contact)));
}});
@@ -159,6 +161,17 @@ public class GroupInvitationManagerImplTest extends BrambleMockTestCase {
groupInvitationManager.createLocalState(txn);
}
@Test
public void testCreateLocalStateSubsequentTime() throws Exception {
context.checking(new Expectations() {{
oneOf(contactGroupFactory).createLocalGroup(CLIENT_ID);
will(returnValue(localGroup));
oneOf(db).containsGroup(txn, localGroup.getId());
will(returnValue(true));
}});
groupInvitationManager.createLocalState(txn);
}
private void expectAddingContact(Contact c, boolean contactExists)
throws Exception {
context.checking(new Expectations() {{

View File

@@ -12,7 +12,6 @@ import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Metadata;
import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.identity.Author;
import org.briarproject.bramble.api.identity.AuthorId;
import org.briarproject.bramble.api.identity.IdentityManager;
import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.api.sync.Group;
@@ -34,8 +33,10 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
import static org.briarproject.bramble.test.TestUtils.getAuthor;
import static org.briarproject.bramble.test.TestUtils.getGroup;
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.briar.api.blog.BlogSharingManager.CLIENT_ID;
@@ -57,22 +58,19 @@ public class BlogSharingManagerImplTest extends BrambleMockTestCase {
context.mock(ContactGroupFactory.class);
private final BlogManager blogManager = context.mock(BlogManager.class);
private final AuthorId localAuthorId = new AuthorId(getRandomId());
private final LocalAuthor localAuthor = getLocalAuthor();
private final ContactId contactId = new ContactId(0);
private final AuthorId authorId = new AuthorId(getRandomId());
private final Author author = new Author(authorId, "Author",
getRandomBytes(MAX_PUBLIC_KEY_LENGTH));
private final Author author = getAuthor();
private final Contact contact =
new Contact(contactId, author, localAuthorId, true, true);
new Contact(contactId, author, localAuthor.getId(), true, true);
private final Collection<Contact> contacts =
Collections.singletonList(contact);
private final Group contactGroup =
new Group(new GroupId(getRandomId()), CLIENT_ID,
getRandomBytes(42));
private final Group blogGroup =
new Group(new GroupId(getRandomId()), BlogManager.CLIENT_ID,
getRandomBytes(42));
private final Group localGroup = getGroup(CLIENT_ID);
private final Group contactGroup = getGroup(CLIENT_ID);
private final Group blogGroup = getGroup(BlogManager.CLIENT_ID);
private final Blog blog = new Blog(blogGroup, author, false);
private final Group localBlogGroup = getGroup(BlogManager.CLIENT_ID);
private final Blog localBlog = new Blog(localBlogGroup, localAuthor, false);
@SuppressWarnings("unchecked")
private final ProtocolEngine<Blog> engine =
context.mock(ProtocolEngine.class);
@@ -93,24 +91,110 @@ public class BlogSharingManagerImplTest extends BrambleMockTestCase {
}
@Test
public void testAddingContactFreshState() throws Exception {
Map<MessageId, BdfDictionary> sessions = new HashMap<>(0);
testAddingContact(sessions);
public void testCreateLocalStateFirstTimeWithExistingContactNotSetUp()
throws Exception {
Transaction txn = new Transaction(null, false);
context.checking(new Expectations() {{
// The local group doesn't exist - we need to set things up
oneOf(contactGroupFactory).createLocalGroup(CLIENT_ID);
will(returnValue(localGroup));
oneOf(db).containsGroup(txn, localGroup.getId());
will(returnValue(false));
oneOf(db).addGroup(txn, localGroup);
// Get contacts
oneOf(db).getContacts(txn);
will(returnValue(contacts));
}});
// Set things up for the contact
expectAddingContact(txn);
blogSharingManager.createLocalState(txn);
}
private void expectAddingContact(Transaction txn) throws Exception {
BdfDictionary meta = BdfDictionary.of(
new BdfEntry(GROUP_KEY_CONTACT_ID, contactId.getInt()));
Map<MessageId, BdfDictionary> sessions = Collections.emptyMap();
context.checking(new Expectations() {{
// Check for contact group in BlogSharingManagerImpl
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
will(returnValue(contactGroup));
oneOf(db).containsGroup(txn, contactGroup.getId());
will(returnValue(false));
// Check for contact group again in SharingManagerImpl
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
will(returnValue(contactGroup));
oneOf(db).containsGroup(txn, contactGroup.getId());
will(returnValue(false));
// Create the contact group and share it with the contact
oneOf(db).addGroup(txn, contactGroup);
oneOf(db).setGroupVisibility(txn, contactId, contactGroup.getId(),
SHARED);
// Attach the contact ID to the group
oneOf(clientHelper)
.mergeGroupMetadata(txn, contactGroup.getId(), meta);
// Get our blog and the contact's blog
oneOf(identityManager).getLocalAuthor(txn);
will(returnValue(localAuthor));
oneOf(blogManager).getPersonalBlog(localAuthor);
will(returnValue(localBlog));
oneOf(blogManager).getPersonalBlog(author);
will(returnValue(blog));
}});
// Pre-share our blog with the contact and vice versa
expectPreShareShareable(txn, contact, localBlog, sessions);
expectPreShareShareable(txn, contact, blog, sessions);
}
@Test
public void testAddingContactExistingState() throws Exception {
Map<MessageId, BdfDictionary> sessions = new HashMap<>(1);
sessions.put(new MessageId(getRandomId()), new BdfDictionary());
testAddingContact(sessions);
public void testCreateLocalStateFirstTimeWithExistingContactAlreadySetUp()
throws Exception {
Transaction txn = new Transaction(null, false);
context.checking(new Expectations() {{
// The local group doesn't exist - we need to set things up
oneOf(contactGroupFactory).createLocalGroup(CLIENT_ID);
will(returnValue(localGroup));
oneOf(db).containsGroup(txn, localGroup.getId());
will(returnValue(false));
oneOf(db).addGroup(txn, localGroup);
// Get contacts
oneOf(db).getContacts(txn);
will(returnValue(contacts));
// The contact has already been set up
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
will(returnValue(contactGroup));
oneOf(db).containsGroup(txn, contactGroup.getId());
will(returnValue(true));
}});
blogSharingManager.createLocalState(txn);
}
@Test(expected = DbException.class)
public void testAddingContactMultipleSessions() throws Exception {
Map<MessageId, BdfDictionary> sessions = new HashMap<>(2);
sessions.put(new MessageId(getRandomId()), new BdfDictionary());
sessions.put(new MessageId(getRandomId()), new BdfDictionary());
testAddingContact(sessions);
@Test
public void testCreateLocalStateSubsequentTime() throws Exception {
Transaction txn = new Transaction(null, false);
context.checking(new Expectations() {{
// The local group exists - everything has been set up
oneOf(contactGroupFactory).createLocalGroup(CLIENT_ID);
will(returnValue(localGroup));
oneOf(db).containsGroup(txn, localGroup.getId());
will(returnValue(true));
}});
blogSharingManager.createLocalState(txn);
}
@Test
public void testAddingContact() throws Exception {
Transaction txn = new Transaction(null, false);
expectAddingContact(txn);
blogSharingManager.addingContact(txn, contact);
}
@Test
@@ -134,46 +218,6 @@ public class BlogSharingManagerImplTest extends BrambleMockTestCase {
testRemovingBlog(sessions);
}
private void testAddingContact(Map<MessageId, BdfDictionary> sessions)
throws Exception {
Transaction txn = new Transaction(null, false);
LocalAuthor localAuthor =
new LocalAuthor(localAuthorId, "Local Author",
getRandomBytes(MAX_PUBLIC_KEY_LENGTH),
getRandomBytes(MAX_PUBLIC_KEY_LENGTH),
System.currentTimeMillis());
BdfDictionary meta = BdfDictionary
.of(new BdfEntry(GROUP_KEY_CONTACT_ID, contactId.getInt()));
Group localBlogGroup =
new Group(new GroupId(getRandomId()), BlogManager.CLIENT_ID,
getRandomBytes(42));
Blog localBlog = new Blog(localBlogGroup, localAuthor, false);
context.checking(new Expectations() {{
oneOf(db).getContacts(txn);
will(returnValue(contacts));
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
will(returnValue(contactGroup));
oneOf(db).containsGroup(txn, contactGroup.getId());
will(returnValue(false));
oneOf(db).addGroup(txn, contactGroup);
oneOf(db).setGroupVisibility(txn, contactId, contactGroup.getId(),
SHARED);
oneOf(clientHelper)
.mergeGroupMetadata(txn, contactGroup.getId(), meta);
oneOf(identityManager).getLocalAuthor(txn);
will(returnValue(localAuthor));
oneOf(blogManager).getPersonalBlog(localAuthor);
will(returnValue(localBlog));
oneOf(blogManager).getPersonalBlog(author);
will(returnValue(blog));
}});
expectPreShareShareable(txn, contact, localBlog, sessions);
expectPreShareShareable(txn, contact, blog, sessions);
blogSharingManager.createLocalState(txn);
}
private void expectPreShareShareable(Transaction txn, Contact contact,
Blog blog, Map<MessageId, BdfDictionary> sessions)
throws Exception {