Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STITCH-3344 Remove the local MongoDB service from the server SDK #172

Merged
merged 1 commit into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,6 @@ def serverPackages = ["com.mongodb.stitch.server.core",
"com.mongodb.stitch.server.services.aws.ses",
"com.mongodb.stitch.server.services.fcm",
"com.mongodb.stitch.server.services.http",
"com.mongodb.stitch.server.services.mongodb.local",
"com.mongodb.stitch.server.services.mongodb.remote",
"com.mongodb.stitch.server.services.twilio"]

Expand Down
1 change: 0 additions & 1 deletion core/services/mongodb-remote/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ dependencies {

testImplementation project(':core:stitch-core-admin-client')
testImplementation project(':core:stitch-core-testutils')
testImplementation project(':server:server-services:stitch-server-services-mongodb-local')
testImplementation "org.mongodb:mongodb-driver-embedded:3.9.0-SNAPSHOT"
testImplementation 'junit:junit:4.12'
testImplementation "org.mockito:mockito-core:2.18.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,80 +26,94 @@
import com.mongodb.stitch.core.services.mongodb.remote.sync.internal.SyncMongoClientFactory;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class CoreRemoteMongoClientImpl implements CoreRemoteMongoClient, StitchServiceBinder {

private final CoreStitchServiceClient service;
private final StitchAppClientInfo appInfo;
private final EmbeddedMongoClientFactory clientFactory;

private DataSynchronizer dataSynchronizer;
// Sync related fields
private boolean hasSync;
@Nullable private final EmbeddedMongoClientFactory clientFactory;
@Nullable private DataSynchronizer dataSynchronizer;
@Nonnull private String lastActiveUserId;

public CoreRemoteMongoClientImpl(final CoreStitchServiceClient service,
final String instanceKey,
final StitchAppClientInfo appInfo,
final EmbeddedMongoClientFactory clientFactory) {
final @Nullable EmbeddedMongoClientFactory clientFactory) {
this.service = service;
this.appInfo = appInfo;
this.clientFactory = clientFactory;

if (clientFactory != null) {
this.hasSync = true;
this.dataSynchronizer = new DataSynchronizer(
instanceKey,
service,
SyncMongoClientFactory.getClient(
appInfo,
service.getName(),
clientFactory
),
this,
appInfo.getNetworkMonitor(),
appInfo.getAuthMonitor(),
appInfo.getEventDispatcher()
);
// set this to an empty string to avoid cumbersome null
// checks when comparing to the next activeUser
this.lastActiveUserId = appInfo.getAuthMonitor().getActiveUserId() != null
? appInfo.getAuthMonitor().getActiveUserId() : "";
} else {
this.hasSync = false;
this.lastActiveUserId = "";
}

this.service.bind(this);
// set this to an empty string to avoid cumbersome null
// checks when comparing to the next activeUser
this.lastActiveUserId = appInfo.getAuthMonitor().getActiveUserId() != null
? appInfo.getAuthMonitor().getActiveUserId() : "";

this.dataSynchronizer = new DataSynchronizer(
instanceKey,
service,
SyncMongoClientFactory.getClient(
appInfo,
service.getName(),
clientFactory
),
this,
appInfo.getNetworkMonitor(),
appInfo.getAuthMonitor(),
appInfo.getEventDispatcher()
);
}

private void onAuthEvent(final AuthEvent authEvent) {
switch (authEvent.getAuthEventType()) {
case USER_REMOVED:
final String userId = ((AuthEvent.UserRemoved)authEvent).getRemovedUser().getId();
if (!SyncMongoClientFactory.deleteDatabase(
appInfo,
service.getName(),
clientFactory,
userId
)) {
System.err.println("Could not delete database for user id " + userId);
}
break;
case ACTIVE_USER_CHANGED:
if (!lastActiveUserId.equals(appInfo.getAuthMonitor().getActiveUserId())) {
this.lastActiveUserId = appInfo.getAuthMonitor().getActiveUserId() != null
? appInfo.getAuthMonitor().getActiveUserId() : "";
if (authEvent instanceof AuthEvent.ActiveUserChanged
&& ((AuthEvent.ActiveUserChanged)authEvent).getCurrentActiveUser() != null) {
// reinitialize the DataSynchronizer entirely.
// any auth event will trigger this.
this.dataSynchronizer.reinitialize(
SyncMongoClientFactory.getClient(
appInfo,
service.getName(),
clientFactory
)
);
} else {
this.dataSynchronizer.stop();
// Only perform user rebinding if this client has mobile sync functionality.
if (hasSync) {
switch (authEvent.getAuthEventType()) {
case USER_REMOVED:
final String userId = ((AuthEvent.UserRemoved)authEvent).getRemovedUser().getId();
if (!SyncMongoClientFactory.deleteDatabase(
appInfo,
service.getName(),
clientFactory,
userId
)) {
System.err.println("Could not delete database for user id " + userId);
}
}
break;
default:
// no-op
break;
break;
case ACTIVE_USER_CHANGED:
if (!lastActiveUserId.equals(appInfo.getAuthMonitor().getActiveUserId())) {
this.lastActiveUserId = appInfo.getAuthMonitor().getActiveUserId() != null
? appInfo.getAuthMonitor().getActiveUserId() : "";
if (authEvent instanceof AuthEvent.ActiveUserChanged
&& ((AuthEvent.ActiveUserChanged)authEvent).getCurrentActiveUser() != null) {
// reinitialize the DataSynchronizer entirely.
// any auth event will trigger this.
this.dataSynchronizer.reinitialize(
SyncMongoClientFactory.getClient(
appInfo,
service.getName(),
clientFactory
)
);
} else {
this.dataSynchronizer.stop();
}
}
break;
default:
// no-op
break;
}
}
}

Expand All @@ -124,7 +138,7 @@ public CoreRemoteMongoDatabaseImpl getDatabase(final String databaseName) {
return new CoreRemoteMongoDatabaseImpl(
databaseName,
service,
dataSynchronizer,
(hasSync) ? dataSynchronizer : null,
appInfo.getNetworkMonitor()
);
}
Expand All @@ -135,7 +149,9 @@ public DataSynchronizer getDataSynchronizer() {

@Override
public void close() {
this.dataSynchronizer.stop();
this.dataSynchronizer.close();
if (hasSync) {
this.dataSynchronizer.stop();
this.dataSynchronizer.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
import java.io.IOException;
import java.util.List;

import javax.annotation.Nullable;

import org.bson.BsonDocument;
import org.bson.BsonObjectId;
import org.bson.BsonValue;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;


public interface CoreRemoteMongoCollection<DocumentT> {

/**
Expand Down Expand Up @@ -521,5 +524,5 @@ Stream<CompactChangeEvent<DocumentT>> watchCompact(final BsonValue... ids)
*
* @return set of sync operations for this collection
*/
CoreSync<DocumentT> sync();
@Nullable CoreSync<DocumentT> sync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,50 +42,59 @@
import java.util.HashSet;
import java.util.List;

import javax.annotation.Nullable;

import org.bson.BsonDocument;
import org.bson.BsonObjectId;
import org.bson.BsonValue;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;


public class CoreRemoteMongoCollectionImpl<DocumentT>
implements CoreRemoteMongoCollection<DocumentT> {

private final MongoNamespace namespace;
private final Class<DocumentT> documentClass;
private final CoreStitchServiceClient service;
private final Operations<DocumentT> operations;

// Sync related fields
private final CoreSync<DocumentT> coreSync;
private final DataSynchronizer dataSynchronizer;
private final NetworkMonitor networkMonitor;
@Nullable private final CoreSync<DocumentT> coreSync;
@Nullable private final DataSynchronizer dataSynchronizer;
@Nullable private final NetworkMonitor networkMonitor;

CoreRemoteMongoCollectionImpl(final MongoNamespace namespace,
final Class<DocumentT> documentClass,
final CoreStitchServiceClient service,
final DataSynchronizer dataSynchronizer,
final NetworkMonitor networkMonitor) {
@Nullable final DataSynchronizer dataSynchronizer,
@Nullable final NetworkMonitor networkMonitor) {
notNull("namespace", namespace);
notNull("documentClass", documentClass);
this.namespace = namespace;
this.documentClass = documentClass;
this.service = service;
this.operations = new Operations<>(namespace, documentClass, service.getCodecRegistry());

this.dataSynchronizer = dataSynchronizer;
this.networkMonitor = networkMonitor;

this.coreSync = new CoreSyncImpl<>(
getNamespace(),
getDocumentClass(),
dataSynchronizer,
service,
new SyncOperations<>(
getNamespace(),
getDocumentClass(),
dataSynchronizer,
getCodecRegistry())
);
if (dataSynchronizer != null && this.networkMonitor != null) {
this.coreSync = new CoreSyncImpl<>(
getNamespace(),
getDocumentClass(),
dataSynchronizer,
service,
new SyncOperations<>(
getNamespace(),
getDocumentClass(),
dataSynchronizer,
getCodecRegistry())
);
} else {
this.coreSync = null;
}
}

/**
Expand Down Expand Up @@ -788,7 +797,7 @@ public Stream<CompactChangeEvent<DocumentT>> watchCompact(final BsonValue... ids
}

@Override
public CoreSync<DocumentT> sync() {
public @Nullable CoreSync<DocumentT> sync() {
return this.coreSync;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,25 @@
import com.mongodb.stitch.core.services.internal.CoreStitchServiceClient;
import com.mongodb.stitch.core.services.mongodb.remote.sync.internal.DataSynchronizer;

import javax.annotation.Nullable;

import org.bson.Document;


public class CoreRemoteMongoDatabaseImpl implements CoreRemoteMongoDatabase {

private final String name;
private final CoreStitchServiceClient service;

// Sync related fields
private final DataSynchronizer dataSynchronizer;
private final NetworkMonitor networkMonitor;
@Nullable private final DataSynchronizer dataSynchronizer;
@Nullable private final NetworkMonitor networkMonitor;

CoreRemoteMongoDatabaseImpl(
final String name,
final CoreStitchServiceClient service,
final DataSynchronizer dataSynchronizer,
final NetworkMonitor networkMonitor
@Nullable final DataSynchronizer dataSynchronizer,
@Nullable final NetworkMonitor networkMonitor
) {
notNull("name", name);
checkDatabaseNameValidity(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Nullable;

public abstract class CoreRemoteClientFactory {

private static final Map<String, CoreRemoteMongoClient> syncInstances = new HashMap<>();

public static synchronized CoreRemoteMongoClient getClient(
final CoreStitchServiceClient service,
final StitchAppClientInfo appInfo,
final EmbeddedMongoClientFactory clientFactory
final @Nullable EmbeddedMongoClientFactory clientFactory
) {
final String instanceKey = String.format("%s-%s", appInfo.getClientAppId(), service.getName());
if (syncInstances.containsKey(instanceKey)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.junit.Assert.assertEquals;

import com.mongodb.stitch.core.services.mongodb.remote.sync.internal.CoreRemoteClientFactory;
import com.mongodb.stitch.server.services.mongodb.local.internal.ServerEmbeddedMongoClientFactory;

import org.junit.After;
import org.junit.Test;
Expand All @@ -30,7 +29,6 @@ public class CoreRemoteMongoClientUnitTests {
@After
public void teardown() {
CoreRemoteClientFactory.close();
ServerEmbeddedMongoClientFactory.getInstance().close();
}

@Test
Expand Down
Loading