-
Notifications
You must be signed in to change notification settings - Fork 10
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
Introduced data freshness with the MongoDB implementation at connection-level and at query-level #209
Introduced data freshness with the MongoDB implementation at connection-level and at query-level #209
Changes from all commits
8077db8
b3ab9a0
8d1037f
a97c11a
21b8783
78503c2
38d3b6c
a16f4d5
fd2b3f4
4e0b29c
e85b786
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,7 +71,7 @@ public class DocStoreTest { | |
public static void init() { | ||
datastoreMap = Maps.newHashMap(); | ||
mongo = | ||
new GenericContainer<>(DockerImageName.parse("mongo:4.4.0")) | ||
new GenericContainer<>(DockerImageName.parse("mongo:7.0.14")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the MongoDB version 7.0.14? Is this the same version that has been deployed? |
||
.withExposedPorts(27017) | ||
.waitingFor(Wait.forListeningPort()); | ||
mongo.start(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import java.util.Set; | ||
import org.hypertrace.core.documentstore.expression.impl.KeyExpression; | ||
import org.hypertrace.core.documentstore.model.exception.DuplicateDocumentException; | ||
import org.hypertrace.core.documentstore.model.options.QueryOptions; | ||
import org.hypertrace.core.documentstore.model.options.UpdateOptions; | ||
import org.hypertrace.core.documentstore.model.subdoc.SubDocumentUpdate; | ||
|
||
|
@@ -127,6 +128,16 @@ public interface Collection { | |
*/ | ||
CloseableIterator<Document> aggregate(final org.hypertrace.core.documentstore.query.Query query); | ||
|
||
/** | ||
* Query the documents conforming to the query specification. | ||
* | ||
* @param query The query specification | ||
* @param queryOptions The query options | ||
* @return {@link CloseableIterator} of matching documents | ||
*/ | ||
CloseableIterator<Document> query( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: the existing query api deprecated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think that's required. If clients want to query with the default configurations, the older method can be used. |
||
final org.hypertrace.core.documentstore.query.Query query, final QueryOptions queryOptions); | ||
|
||
/** | ||
* Delete the document with the given key. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.hypertrace.core.documentstore.model.options; | ||
|
||
@SuppressWarnings("UnnecessarySemicolon") | ||
public enum DataFreshness { | ||
SYSTEM_DEFAULT, | ||
REALTIME_FRESHNESS, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: What does the freshness mean? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Freshness means the liveliness of the data. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Basically "SECONDARY" is preferred. In case "SECONDARY" is not available, it'll fetch from "PRIMARY". |
||
NEAR_REALTIME_FRESHNESS, | ||
; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.hypertrace.core.documentstore.model.options; | ||
|
||
import lombok.Builder; | ||
import lombok.Builder.Default; | ||
import lombok.Value; | ||
import lombok.experimental.Accessors; | ||
|
||
@Value | ||
@Builder | ||
@Accessors(fluent = true, chain = true) | ||
public class QueryOptions { | ||
public static final QueryOptions DEFAULT_QUERY_OPTIONS = QueryOptions.builder().build(); | ||
|
||
@Default DataFreshness dataFreshness = DataFreshness.SYSTEM_DEFAULT; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.hypertrace.core.documentstore.mongo.collection; | ||
|
||
import static org.hypertrace.core.documentstore.model.options.DataFreshness.SYSTEM_DEFAULT; | ||
import static org.hypertrace.core.documentstore.mongo.collection.MongoReadPreferenceConverter.convert; | ||
|
||
import com.mongodb.BasicDBObject; | ||
import com.mongodb.ReadPreference; | ||
import com.mongodb.client.MongoCollection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import lombok.Builder; | ||
import lombok.Value; | ||
import lombok.experimental.Accessors; | ||
import org.hypertrace.core.documentstore.model.config.ConnectionConfig; | ||
import org.hypertrace.core.documentstore.model.options.QueryOptions; | ||
|
||
public class MongoCollectionOptionsApplier { | ||
private final Map<CacheKey, MongoCollection<BasicDBObject>> collectionCache; | ||
|
||
public MongoCollectionOptionsApplier() { | ||
this.collectionCache = new HashMap<>(); | ||
} | ||
|
||
public MongoCollection<BasicDBObject> applyOptions( | ||
final ConnectionConfig connectionConfig, | ||
final QueryOptions queryOptions, | ||
final MongoCollection<BasicDBObject> collection) { | ||
final CacheKey cacheKey = | ||
CacheKey.builder().readPreference(readPreference(connectionConfig, queryOptions)).build(); | ||
return collectionCache.computeIfAbsent( | ||
cacheKey, key -> collection.withReadPreference(key.readPreference())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is the CacheKey associated with the collection? Since ReadPreference can be either primary or secondary, does this mean that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes. |
||
} | ||
|
||
private ReadPreference readPreference( | ||
final ConnectionConfig connectionConfig, final QueryOptions queryOptions) { | ||
return SYSTEM_DEFAULT.equals(queryOptions.dataFreshness()) | ||
? convert(connectionConfig.dataFreshness()) | ||
: convert(queryOptions.dataFreshness()); | ||
} | ||
|
||
@Value | ||
@Builder | ||
@Accessors(fluent = true, chain = true) | ||
private static class CacheKey { | ||
ReadPreference readPreference; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.hypertrace.core.documentstore.mongo.collection; | ||
|
||
import static com.mongodb.ReadPreference.primary; | ||
import static com.mongodb.ReadPreference.secondaryPreferred; | ||
import static java.util.Map.entry; | ||
import static org.hypertrace.core.documentstore.model.options.DataFreshness.NEAR_REALTIME_FRESHNESS; | ||
import static org.hypertrace.core.documentstore.model.options.DataFreshness.REALTIME_FRESHNESS; | ||
import static org.hypertrace.core.documentstore.model.options.DataFreshness.SYSTEM_DEFAULT; | ||
|
||
import com.mongodb.ReadPreference; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.hypertrace.core.documentstore.model.options.DataFreshness; | ||
|
||
public class MongoReadPreferenceConverter { | ||
Check warning on line 15 in document-store/src/main/java/org/hypertrace/core/documentstore/mongo/collection/MongoReadPreferenceConverter.java Codecov / codecov/patchdocument-store/src/main/java/org/hypertrace/core/documentstore/mongo/collection/MongoReadPreferenceConverter.java#L15
|
||
private static final Map<DataFreshness, ReadPreference> DATA_FRESHNESS_TO_READ_PREFERENCE = | ||
Map.ofEntries( | ||
entry(SYSTEM_DEFAULT, primary()), | ||
entry(REALTIME_FRESHNESS, primary()), | ||
entry(NEAR_REALTIME_FRESHNESS, secondaryPreferred())); | ||
|
||
public static ReadPreference convert(final DataFreshness dataFreshness) { | ||
if (dataFreshness == null) { | ||
return primary(); | ||
} | ||
|
||
return Optional.ofNullable(DATA_FRESHNESS_TO_READ_PREFERENCE.get(dataFreshness)) | ||
.orElseThrow( | ||
() -> | ||
new UnsupportedOperationException("Unsupported data freshness: " + dataFreshness)); | ||
Check warning on line 30 in document-store/src/main/java/org/hypertrace/core/documentstore/mongo/collection/MongoReadPreferenceConverter.java Codecov / codecov/patchdocument-store/src/main/java/org/hypertrace/core/documentstore/mongo/collection/MongoReadPreferenceConverter.java#L30
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for not moving to latest version -
10.0.4
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that version. It's failing (mostly due to Java version incompatibility).