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

Add builder for EngineConfig #5618

Merged
merged 4 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -81,30 +81,29 @@ EngineConfig engineConfigWithLargerIndexingMemory(EngineConfig config) {
.put("indices.memory.index_buffer_size", "10mb")
.build();
IndexSettings indexSettings = new IndexSettings(config.getIndexSettings().getIndexMetadata(), settings);
return new EngineConfig(
config.getShardId(),
config.getThreadPool(),
indexSettings,
config.getWarmer(),
config.getStore(),
config.getMergePolicy(),
config.getAnalyzer(),
config.getSimilarity(),
new CodecService(null, LogManager.getLogger(IndexingMemoryControllerIT.class)),
config.getEventListener(),
config.getQueryCache(),
config.getQueryCachingPolicy(),
config.getTranslogConfig(),
config.getFlushMergesAfter(),
config.getExternalRefreshListener(),
config.getInternalRefreshListener(),
config.getIndexSort(),
config.getCircuitBreakerService(),
config.getGlobalCheckpointSupplier(),
config.retentionLeasesSupplier(),
config.getPrimaryTermSupplier(),
config.getTombstoneDocSupplier()
);
return new EngineConfig.Builder().setShardId(config.getShardId())
.setThreadPool(config.getThreadPool())
.setIndexSettings(indexSettings)
.setWarmer(config.getWarmer())
.setStore(config.getStore())
.setMergePolicy(config.getMergePolicy())
.setAnalyzer(config.getAnalyzer())
.setSimilarity(config.getSimilarity())
.setCodecService(new CodecService(null, LogManager.getLogger(IndexingMemoryControllerIT.class)))
.setEventListener(config.getEventListener())
.setQueryCache(config.getQueryCache())
.setQueryCachingPolicy(config.getQueryCachingPolicy())
.setTranslogConfig(config.getTranslogConfig())
.setFlushMergesAfter(config.getFlushMergesAfter())
.setExternalRefreshListener(config.getExternalRefreshListener())
.setInternalRefreshListener(config.getInternalRefreshListener())
.setIndexSort(config.getIndexSort())
.setCircuitBreakerService(config.getCircuitBreakerService())
.setGlobalCheckpointSupplier(config.getGlobalCheckpointSupplier())
.setRetentionLeasesSupplier(config.retentionLeasesSupplier())
.setPrimaryTermSupplier(config.getPrimaryTermSupplier())
.setTombstoneDocSupplier(config.getTombstoneDocSupplier())
.createEngineConfig();
}

@Override
Expand Down
296 changes: 188 additions & 108 deletions server/src/main/java/org/opensearch/index/engine/EngineConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,114 +154,6 @@ public Supplier<RetentionLeases> retentionLeasesSupplier() {

private final TranslogFactory translogFactory;

public EngineConfig(
ShardId shardId,
ThreadPool threadPool,
IndexSettings indexSettings,
Engine.Warmer warmer,
Store store,
MergePolicy mergePolicy,
Analyzer analyzer,
Similarity similarity,
CodecService codecService,
Engine.EventListener eventListener,
QueryCache queryCache,
QueryCachingPolicy queryCachingPolicy,
TranslogConfig translogConfig,
TimeValue flushMergesAfter,
List<ReferenceManager.RefreshListener> externalRefreshListener,
List<ReferenceManager.RefreshListener> internalRefreshListener,
Sort indexSort,
CircuitBreakerService circuitBreakerService,
LongSupplier globalCheckpointSupplier,
Supplier<RetentionLeases> retentionLeasesSupplier,
LongSupplier primaryTermSupplier,
TombstoneDocSupplier tombstoneDocSupplier
) {
this(
shardId,
threadPool,
indexSettings,
warmer,
store,
mergePolicy,
analyzer,
similarity,
codecService,
eventListener,
queryCache,
queryCachingPolicy,
translogConfig,
null,
flushMergesAfter,
externalRefreshListener,
internalRefreshListener,
indexSort,
circuitBreakerService,
globalCheckpointSupplier,
retentionLeasesSupplier,
primaryTermSupplier,
tombstoneDocSupplier
);
}

/**
* Creates a new {@link org.opensearch.index.engine.EngineConfig}
*/
EngineConfig(
ShardId shardId,
ThreadPool threadPool,
IndexSettings indexSettings,
Engine.Warmer warmer,
Store store,
MergePolicy mergePolicy,
Analyzer analyzer,
Similarity similarity,
CodecService codecService,
Engine.EventListener eventListener,
QueryCache queryCache,
QueryCachingPolicy queryCachingPolicy,
TranslogConfig translogConfig,
TranslogDeletionPolicyFactory translogDeletionPolicyFactory,
TimeValue flushMergesAfter,
List<ReferenceManager.RefreshListener> externalRefreshListener,
List<ReferenceManager.RefreshListener> internalRefreshListener,
Sort indexSort,
CircuitBreakerService circuitBreakerService,
LongSupplier globalCheckpointSupplier,
Supplier<RetentionLeases> retentionLeasesSupplier,
LongSupplier primaryTermSupplier,
TombstoneDocSupplier tombstoneDocSupplier
) {
this(
shardId,
threadPool,
indexSettings,
warmer,
store,
mergePolicy,
analyzer,
similarity,
codecService,
eventListener,
queryCache,
queryCachingPolicy,
translogConfig,
translogDeletionPolicyFactory,
flushMergesAfter,
externalRefreshListener,
internalRefreshListener,
indexSort,
circuitBreakerService,
globalCheckpointSupplier,
retentionLeasesSupplier,
primaryTermSupplier,
tombstoneDocSupplier,
false,
new InternalTranslogFactory()
);
}

/**
* Creates a new {@link org.opensearch.index.engine.EngineConfig}
*/
Expand Down Expand Up @@ -573,4 +465,192 @@ public TombstoneDocSupplier getTombstoneDocSupplier() {
public TranslogDeletionPolicyFactory getCustomTranslogDeletionPolicyFactory() {
return translogDeletionPolicyFactory;
}

/**
* Builder for EngineConfig class
*
* @opensearch.internal
*/
public static class Builder {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this Builder make certain ctor parameters optional? How do we know we don't miss out on all mandatory ones

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The build method still calls the constructor (which is private now) so all the validations on parameters remain same.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree on the validation but the notion of what are mandatory parameters and what are optional is diluted. Previously everything was mandatory, but is there something we could do to ease that and make this of the type EngineConfig#builder(mandatory_params).optionalParam(param).build(this)

private ShardId shardId;
private ThreadPool threadPool;
private IndexSettings indexSettings;
private Engine.Warmer warmer;
private Store store;
private MergePolicy mergePolicy;
private Analyzer analyzer;
private Similarity similarity;
private CodecService codecService;
private Engine.EventListener eventListener;
private QueryCache queryCache;
private QueryCachingPolicy queryCachingPolicy;
private TranslogConfig translogConfig;
private TimeValue flushMergesAfter;
private List<ReferenceManager.RefreshListener> externalRefreshListener;
private List<ReferenceManager.RefreshListener> internalRefreshListener;
private Sort indexSort;
private CircuitBreakerService circuitBreakerService;
private LongSupplier globalCheckpointSupplier;
private Supplier<RetentionLeases> retentionLeasesSupplier;
private LongSupplier primaryTermSupplier;
private TombstoneDocSupplier tombstoneDocSupplier;
private TranslogDeletionPolicyFactory translogDeletionPolicyFactory = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the others fields are implicitly assigned to null. Can probably remove this for consistency.

private boolean isReadOnlyReplica = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isReadOnlyReplica will have false as default, so it can be removed as well.

private TranslogFactory translogFactory = new InternalTranslogFactory();

public Builder setShardId(ShardId shardId) {
this.shardId = shardId;
return this;
}

public Builder setThreadPool(ThreadPool threadPool) {
this.threadPool = threadPool;
return this;
}

public Builder setIndexSettings(IndexSettings indexSettings) {
this.indexSettings = indexSettings;
return this;
}

public Builder setWarmer(Engine.Warmer warmer) {
this.warmer = warmer;
return this;
}

public Builder setStore(Store store) {
this.store = store;
return this;
}

public Builder setMergePolicy(MergePolicy mergePolicy) {
this.mergePolicy = mergePolicy;
return this;
}

public Builder setAnalyzer(Analyzer analyzer) {
this.analyzer = analyzer;
return this;
}

public Builder setSimilarity(Similarity similarity) {
this.similarity = similarity;
return this;
}

public Builder setCodecService(CodecService codecService) {
this.codecService = codecService;
return this;
}

public Builder setEventListener(Engine.EventListener eventListener) {
this.eventListener = eventListener;
return this;
}

public Builder setQueryCache(QueryCache queryCache) {
this.queryCache = queryCache;
return this;
}

public Builder setQueryCachingPolicy(QueryCachingPolicy queryCachingPolicy) {
this.queryCachingPolicy = queryCachingPolicy;
return this;
}

public Builder setTranslogConfig(TranslogConfig translogConfig) {
this.translogConfig = translogConfig;
return this;
}

public Builder setFlushMergesAfter(TimeValue flushMergesAfter) {
this.flushMergesAfter = flushMergesAfter;
return this;
}

public Builder setExternalRefreshListener(List<ReferenceManager.RefreshListener> externalRefreshListener) {
this.externalRefreshListener = externalRefreshListener;
return this;
}

public Builder setInternalRefreshListener(List<ReferenceManager.RefreshListener> internalRefreshListener) {
this.internalRefreshListener = internalRefreshListener;
return this;
}

public Builder setIndexSort(Sort indexSort) {
this.indexSort = indexSort;
return this;
}

public Builder setCircuitBreakerService(CircuitBreakerService circuitBreakerService) {
this.circuitBreakerService = circuitBreakerService;
return this;
}

public Builder setGlobalCheckpointSupplier(LongSupplier globalCheckpointSupplier) {
this.globalCheckpointSupplier = globalCheckpointSupplier;
return this;
}

public Builder setRetentionLeasesSupplier(Supplier<RetentionLeases> retentionLeasesSupplier) {
this.retentionLeasesSupplier = retentionLeasesSupplier;
return this;
}

public Builder setPrimaryTermSupplier(LongSupplier primaryTermSupplier) {
this.primaryTermSupplier = primaryTermSupplier;
return this;
}

public Builder setTombstoneDocSupplier(TombstoneDocSupplier tombstoneDocSupplier) {
this.tombstoneDocSupplier = tombstoneDocSupplier;
return this;
}

public Builder setTranslogDeletionPolicyFactory(TranslogDeletionPolicyFactory translogDeletionPolicyFactory) {
this.translogDeletionPolicyFactory = translogDeletionPolicyFactory;
return this;
}

public Builder setIsReadOnlyReplica(boolean isReadOnlyReplica) {
this.isReadOnlyReplica = isReadOnlyReplica;
return this;
}

public Builder setTranslogFactory(TranslogFactory translogFactory) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick:

For the setters in a builder we usually drop the set prefix in favour of a fluent design

i.e. public Builder translogFactory(TranslogFactory translogFactory) over public Builder setTranslogFactory(TranslogFactory translogFactory)

this.translogFactory = translogFactory;
return this;
}

public EngineConfig createEngineConfig() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, but create would be fine here since "engine config" is in the class name

return new EngineConfig(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another nitpick...assuming this constructor can be made private, then you can pass the this instance of EngineConfig.Builder and let the constructor read the fields. It's a bit more concise and makes adding a new field require fewer changes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this. Will change in next commit.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for the review comment from @andrross. Without a private constructor here, the builder for EngineConfig can be bypassed.

shardId,
threadPool,
indexSettings,
warmer,
store,
mergePolicy,
analyzer,
similarity,
codecService,
eventListener,
queryCache,
queryCachingPolicy,
translogConfig,
translogDeletionPolicyFactory,
flushMergesAfter,
externalRefreshListener,
internalRefreshListener,
indexSort,
circuitBreakerService,
globalCheckpointSupplier,
retentionLeasesSupplier,
primaryTermSupplier,
tombstoneDocSupplier,
isReadOnlyReplica,
translogFactory
);
}
}
}
Loading