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

WIP: create feature to dynamically set readpreference on a call-by-ca… #770

Draft
wants to merge 3 commits into
base: 8.2.x
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ trait MongoEntity<D> implements GormEntity<D>, DynamicAttributes {
currentMongoStaticApi().useDatabase(databaseName)
}

static <T> T withReadPreference(String readPreferenceString, Closure<T> callable) {
currentMongoStaticApi().withReadPreference(readPreferenceString, callable)
}

/**
* Counts the number of hits
* @param query The query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ interface MongoStaticOperations<D> extends GormStaticOperations<D> {
*/
public <T> T withCollection(String collectionName, Closure<T> callable)

/**
* Use the given read preference for the scope of the closure call
* @param readPreferenceString The read preference as string (primary, secondaryPreferred, etc)
* @param callable The callable
* @return The result of the closure
*/
public <T> T withReadPreference(String readPreferenceString, Closure<T> callable)

/**
* Use the given collection for this entity for the scope of the session
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ class MongoStaticApi<D> extends GormStaticApi<D> implements MongoAllOperations<D
}
}

@Override
def <T> T withReadPreference(String readPreferenceString, Closure<T> callable) {
withSession { AbstractMongoSession session ->
final previous = session.getReadPreference()
try {
session.setReadPreference(ReadPreference.valueOf(readPreferenceString))
return callable.call()
} finally {
session.setReadPreference(previous)
}
}
}

@Override
String useCollection(String collectionName) {
withSession { AbstractMongoSession session ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.grails.datastore.mapping.mongo;

import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import com.mongodb.client.MongoClient;
import org.bson.Document;
Expand Down Expand Up @@ -45,6 +46,7 @@ public abstract class AbstractMongoSession extends AbstractSession<MongoClient>
protected final String defaultDatabase;
protected MongoDatastore mongoDatastore;
protected WriteConcern writeConcern = null;
protected ReadPreference readPreference = null;
protected boolean errorOccured = false;
protected Map<PersistentEntity, String> mongoCollections = new ConcurrentHashMap<PersistentEntity, String>();
protected Map<PersistentEntity, String> mongoDatabases = new ConcurrentHashMap<PersistentEntity, String>();
Expand Down Expand Up @@ -135,6 +137,14 @@ private WriteConcern getDeclaredWriteConcern(WriteConcern defaultConcern, Persis
return writeConcern;
}

public void setReadPreference(ReadPreference readPreference) {
this.readPreference = readPreference;
}

public ReadPreference getReadPreference() {
return readPreference;
}

public MongoClient getNativeInterface() {
return ((MongoDatastore)getDatastore()).getMongoClient();
}
Expand Down Expand Up @@ -182,10 +192,16 @@ public com.mongodb.client.MongoCollection<Document> getCollection(PersistentEnti
if(entity.isRoot()) {
final String database = getDatabase(entity);
final String collectionName = getCollectionName(entity);
return getNativeInterface()
com.mongodb.client.MongoCollection<Document> collection = getNativeInterface()
.getDatabase(database)
.getCollection(collectionName)
.withCodecRegistry(getDatastore().getCodecRegistry());
final ReadPreference readPreference = getReadPreference();
if (readPreference != null) {
return collection.withReadPreference(readPreference);
} else {
return collection;
}
}
else {
final PersistentEntity root = entity.getRootEntity();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.grails.datastore.gorm.mongo

import grails.gorm.annotation.Entity
import grails.mongodb.MongoEntity
import com.mongodb.ReadPreference
import grails.gorm.tests.GormDatastoreSpec
import org.junit.platform.commons.logging.LoggerFactory

class ReadPreferenceSpec extends GormDatastoreSpec {
List getDomainClasses() {
[PotatoFarm]
}

void "Gets read preference from collection correctly"() {
when: "Read read concern by default"
def rp = PotatoFarm.getCollection().getReadPreference()

then: "It is primary"
rp == ReadPreference.primary()

when: "Ask for secondary"
def rp2 = PotatoFarm.withReadPreference("secondary") {
PotatoFarm.getCollection().getReadPreference()
}

then: "It is secondary"
rp2 == ReadPreference.secondary()

when: "After asking for secondary"
def rp3 = PotatoFarm.getCollection().getReadPreference()

then: "It returns to primary"
rp3 == ReadPreference.primary()
}
}

@Entity
class PotatoFarm implements MongoEntity<PotatoFarm> {
String id
String name
Integer potatoCount
}
Loading