-
Notifications
You must be signed in to change notification settings - Fork 207
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
Implement opensearch index partition creation supplier and PitWorker without processing indices #2821
Merged
graytaylor0
merged 2 commits into
opensearch-project:main
from
graytaylor0:PartitionSupplier
Jun 6, 2023
Merged
Implement opensearch index partition creation supplier and PitWorker without processing indices #2821
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
107 changes: 107 additions & 0 deletions
107
...ataprepper/plugins/source/opensearch/worker/OpenSearchIndexPartitionCreationSupplier.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,107 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.source.opensearch.worker; | ||
|
||
import org.opensearch.client.opensearch.OpenSearchClient; | ||
import org.opensearch.client.opensearch._types.OpenSearchException; | ||
import org.opensearch.client.opensearch.cat.IndicesResponse; | ||
import org.opensearch.client.opensearch.cat.indices.IndicesRecord; | ||
import org.opensearch.dataprepper.model.source.coordinator.PartitionIdentifier; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.OpenSearchSourceConfiguration; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.configuration.IndexParametersConfiguration; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.configuration.OpenSearchIndex; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.worker.client.ClusterClientFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
import java.util.regex.Matcher; | ||
import java.util.stream.Collectors; | ||
|
||
public class OpenSearchIndexPartitionCreationSupplier implements Function<Map<String, Object>, List<PartitionIdentifier>> { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(OpenSearchIndexPartitionCreationSupplier.class); | ||
|
||
private final OpenSearchSourceConfiguration openSearchSourceConfiguration; | ||
private final IndexParametersConfiguration indexParametersConfiguration; | ||
private final OpenSearchClient openSearchClient; | ||
|
||
public OpenSearchIndexPartitionCreationSupplier(final OpenSearchSourceConfiguration openSearchSourceConfiguration, | ||
final ClusterClientFactory clusterClientFactory) { | ||
this.openSearchSourceConfiguration = openSearchSourceConfiguration; | ||
this.indexParametersConfiguration = openSearchSourceConfiguration.getIndexParametersConfiguration(); | ||
|
||
final Object client = clusterClientFactory.getClient(); | ||
|
||
if (client instanceof OpenSearchClient) { | ||
this.openSearchClient = (OpenSearchClient) client; | ||
} else { | ||
throw new IllegalArgumentException(String.format("ClusterClientFactory provided an invalid client object to the index partition creation supplier. " + | ||
"The client must be of type OpenSearchClient. The client passed is of class %s", client.getClass())); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public List<PartitionIdentifier> apply(final Map<String, Object> globalStateMap) { | ||
|
||
if (Objects.nonNull(openSearchClient)) { | ||
return applyForOpenSearchClient(globalStateMap); | ||
} | ||
|
||
return Collections.emptyList(); | ||
} | ||
|
||
private List<PartitionIdentifier> applyForOpenSearchClient(final Map<String, Object> globalStateMap) { | ||
IndicesResponse indicesResponse; | ||
try { | ||
indicesResponse = openSearchClient.cat().indices(); | ||
} catch (IOException | OpenSearchException e) { | ||
LOG.error("There was an exception when calling /_cat/indices to create new index partitions", e); | ||
return Collections.emptyList(); | ||
} | ||
|
||
return indicesResponse.valueBody().stream() | ||
.filter(this::shouldIndexBeProcessed) | ||
.map(indexRecord -> PartitionIdentifier.builder().withPartitionKey(indexRecord.index()).build()) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private boolean shouldIndexBeProcessed(final IndicesRecord indicesRecord) { | ||
if (Objects.isNull(indicesRecord.index())) { | ||
return false; | ||
} | ||
|
||
if (Objects.isNull(indexParametersConfiguration)) { | ||
return true; | ||
} | ||
|
||
final List<OpenSearchIndex> includedIndices = indexParametersConfiguration.getIncludedIndices(); | ||
final List<OpenSearchIndex> excludedIndices = indexParametersConfiguration.getExcludedIndices(); | ||
|
||
final boolean matchesIncludedPattern = includedIndices.isEmpty() || doesIndexMatchPattern(includedIndices, indicesRecord); | ||
final boolean matchesExcludePattern = doesIndexMatchPattern(excludedIndices, indicesRecord); | ||
|
||
|
||
return matchesIncludedPattern && !matchesExcludePattern; | ||
} | ||
|
||
private boolean doesIndexMatchPattern(final List<OpenSearchIndex> indices, final IndicesRecord indicesRecord) { | ||
for (final OpenSearchIndex index : indices) { | ||
final Matcher matcher = index.getIndexNamePattern().matcher(indicesRecord.index()); | ||
|
||
if (matcher.matches()) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
.../opensearch/dataprepper/plugins/source/opensearch/worker/client/ClusterClientFactory.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,10 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.source.opensearch.worker.client; | ||
|
||
public interface ClusterClientFactory { | ||
Object getClient(); | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why do we implement
ClusterClientFactory
here if we are returning null?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.
The ElasticSearch accessor is not implemented yet