-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extension point for custom TranslogDeletionPolicy in EnginePlugin. (
#1404) This commit adds a method that can be used to provide a custom TranslogDeletionPolicy from within plugins that implement the EnginePlugin interface. This enables plugins to provide a custom deletion policy with the current limitation that only one plugin can override the policy. An exception will be thrown if more than one plugin overrides the policy.
- Loading branch information
Showing
7 changed files
with
326 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
server/src/main/java/org/opensearch/index/translog/TranslogDeletionPolicyFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.index.translog; | ||
|
||
import org.opensearch.index.IndexSettings; | ||
import org.opensearch.index.seqno.RetentionLeases; | ||
|
||
import java.util.function.Supplier; | ||
|
||
@FunctionalInterface | ||
public interface TranslogDeletionPolicyFactory { | ||
TranslogDeletionPolicy create(IndexSettings settings, Supplier<RetentionLeases> supplier); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
server/src/test/java/org/opensearch/index/engine/EngineConfigFactoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.index.engine; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.opensearch.Version; | ||
import org.opensearch.cluster.metadata.IndexMetadata; | ||
import org.opensearch.common.unit.TimeValue; | ||
import org.opensearch.index.IndexSettings; | ||
import org.opensearch.index.codec.CodecService; | ||
import org.opensearch.index.seqno.RetentionLeases; | ||
import org.opensearch.index.translog.TranslogDeletionPolicy; | ||
import org.opensearch.index.translog.TranslogDeletionPolicyFactory; | ||
import org.opensearch.plugins.EnginePlugin; | ||
import org.opensearch.plugins.Plugin; | ||
import org.opensearch.test.IndexSettingsModule; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
public class EngineConfigFactoryTests extends OpenSearchTestCase { | ||
public void testCreateEngineConfigFromFactory() { | ||
IndexMetadata meta = IndexMetadata.builder("test") | ||
.settings(settings(Version.CURRENT)) | ||
.numberOfShards(1) | ||
.numberOfReplicas(1) | ||
.build(); | ||
List<EnginePlugin> plugins = Collections.singletonList(new FooEnginePlugin()); | ||
IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("test", meta.getSettings()); | ||
EngineConfigFactory factory = new EngineConfigFactory(plugins, indexSettings); | ||
|
||
EngineConfig config = factory.newEngineConfig( | ||
null, | ||
null, | ||
indexSettings, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
TimeValue.timeValueMinutes(5), | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
() -> new RetentionLeases(0, 0, Collections.emptyList()), | ||
null, | ||
null | ||
); | ||
|
||
assertNotNull(config.getCodec()); | ||
assertNotNull(config.getCustomTranslogDeletionPolicyFactory()); | ||
assertTrue(config.getCustomTranslogDeletionPolicyFactory().create(indexSettings, null) instanceof CustomTranslogDeletionPolicy); | ||
} | ||
|
||
public void testCreateEngineConfigFromFactoryMultipleCodecServiceIllegalStateException() { | ||
IndexMetadata meta = IndexMetadata.builder("test") | ||
.settings(settings(Version.CURRENT)) | ||
.numberOfShards(1) | ||
.numberOfReplicas(1) | ||
.build(); | ||
List<EnginePlugin> plugins = Arrays.asList(new FooEnginePlugin(), new BarEnginePlugin()); | ||
IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("test", meta.getSettings()); | ||
|
||
expectThrows(IllegalStateException.class, () -> new EngineConfigFactory(plugins, indexSettings)); | ||
} | ||
|
||
public void testCreateEngineConfigFromFactoryMultipleCustomTranslogDeletionPolicyFactoryIllegalStateException() { | ||
IndexMetadata meta = IndexMetadata.builder("test") | ||
.settings(settings(Version.CURRENT)) | ||
.numberOfShards(1) | ||
.numberOfReplicas(1) | ||
.build(); | ||
List<EnginePlugin> plugins = Arrays.asList(new FooEnginePlugin(), new BazEnginePlugin()); | ||
IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("test", meta.getSettings()); | ||
|
||
expectThrows(IllegalStateException.class, () -> new EngineConfigFactory(plugins, indexSettings)); | ||
} | ||
|
||
private static class FooEnginePlugin extends Plugin implements EnginePlugin { | ||
@Override | ||
public Optional<EngineFactory> getEngineFactory(final IndexSettings indexSettings) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Optional<CodecService> getCustomCodecService(IndexSettings indexSettings) { | ||
return Optional.of(new CodecService(null, LogManager.getLogger(getClass()))); | ||
} | ||
|
||
@Override | ||
public Optional<TranslogDeletionPolicyFactory> getCustomTranslogDeletionPolicyFactory() { | ||
return Optional.of(CustomTranslogDeletionPolicy::new); | ||
} | ||
} | ||
|
||
private static class BarEnginePlugin extends Plugin implements EnginePlugin { | ||
@Override | ||
public Optional<EngineFactory> getEngineFactory(final IndexSettings indexSettings) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Optional<CodecService> getCustomCodecService(IndexSettings indexSettings) { | ||
return Optional.of(new CodecService(null, LogManager.getLogger(getClass()))); | ||
} | ||
} | ||
|
||
private static class BazEnginePlugin extends Plugin implements EnginePlugin { | ||
@Override | ||
public Optional<EngineFactory> getEngineFactory(final IndexSettings indexSettings) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Optional<TranslogDeletionPolicyFactory> getCustomTranslogDeletionPolicyFactory() { | ||
return Optional.of(CustomTranslogDeletionPolicy::new); | ||
} | ||
} | ||
|
||
private static class CustomTranslogDeletionPolicy extends TranslogDeletionPolicy { | ||
public CustomTranslogDeletionPolicy(IndexSettings indexSettings, Supplier<RetentionLeases> retentionLeasesSupplier) { | ||
super( | ||
indexSettings.getTranslogRetentionSize().getBytes(), | ||
indexSettings.getTranslogRetentionAge().getMillis(), | ||
indexSettings.getTranslogRetentionTotalFiles() | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.