-
Notifications
You must be signed in to change notification settings - Fork 176
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
Elasticsearch client implementation with pit and no context search #2910
Merged
graytaylor0
merged 3 commits into
opensearch-project:main
from
graytaylor0:ElasticsearchClient
Jun 21, 2023
Merged
Changes from 2 commits
Commits
Show all changes
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,26 @@ | |
*/ | ||
package org.opensearch.dataprepper.plugins.source.opensearch.worker.client; | ||
|
||
import co.elastic.clients.elasticsearch.ElasticsearchClient; | ||
import co.elastic.clients.elasticsearch._types.ElasticsearchException; | ||
import co.elastic.clients.elasticsearch._types.ScoreSort; | ||
import co.elastic.clients.elasticsearch._types.SortOptions; | ||
import co.elastic.clients.elasticsearch._types.SortOrder; | ||
import co.elastic.clients.elasticsearch._types.Time; | ||
import co.elastic.clients.elasticsearch._types.query_dsl.MatchAllQuery; | ||
import co.elastic.clients.elasticsearch._types.query_dsl.Query; | ||
import co.elastic.clients.elasticsearch.core.ClosePointInTimeRequest; | ||
import co.elastic.clients.elasticsearch.core.ClosePointInTimeResponse; | ||
import co.elastic.clients.elasticsearch.core.OpenPointInTimeRequest; | ||
import co.elastic.clients.elasticsearch.core.OpenPointInTimeResponse; | ||
import co.elastic.clients.elasticsearch.core.SearchRequest; | ||
import co.elastic.clients.elasticsearch.core.SearchResponse; | ||
import co.elastic.clients.elasticsearch.core.search.PointInTimeReference; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.event.EventType; | ||
import org.opensearch.dataprepper.model.event.JacksonEvent; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.worker.client.exceptions.SearchContextLimitException; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.worker.client.model.CreatePointInTimeRequest; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.worker.client.model.CreatePointInTimeResponse; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.worker.client.model.CreateScrollRequest; | ||
|
@@ -13,32 +33,100 @@ | |
import org.opensearch.dataprepper.plugins.source.opensearch.worker.client.model.NoSearchContextSearchRequest; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.worker.client.model.SearchContextType; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.worker.client.model.SearchPointInTimeRequest; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.worker.client.model.SearchWithSearchAfterResults; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.worker.client.model.SearchScrollRequest; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.worker.client.model.SearchScrollResponse; | ||
import org.opensearch.dataprepper.plugins.source.opensearch.worker.client.model.SearchWithSearchAfterResults; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.opensearch.dataprepper.plugins.source.opensearch.worker.client.model.MetadataKeyAttributes.DOCUMENT_ID_METADATA_ATTRIBUTE_NAME; | ||
import static org.opensearch.dataprepper.plugins.source.opensearch.worker.client.model.MetadataKeyAttributes.INDEX_METADATA_ATTRIBUTE_NAME; | ||
|
||
public class ElasticsearchAccessor implements SearchAccessor, ClusterClientFactory { | ||
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. I think there's room to consolidate common logic between this class and OpenSearchAccessor. We can handle that down the line though |
||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ElasticsearchAccessor.class); | ||
|
||
static final String PIT_RESOURCE_LIMIT_ERROR_TYPE = "rejected_execution_exception"; | ||
|
||
private final ElasticsearchClient elasticsearchClient; | ||
private final SearchContextType searchContextType; | ||
|
||
public ElasticsearchAccessor(final ElasticsearchClient elasticsearchClient, final SearchContextType searchContextType) { | ||
this.elasticsearchClient = elasticsearchClient; | ||
this.searchContextType = searchContextType; | ||
} | ||
|
||
@Override | ||
public SearchContextType getSearchContextType() { | ||
// todo: implement | ||
return null; | ||
return searchContextType; | ||
} | ||
|
||
@Override | ||
public CreatePointInTimeResponse createPit(final CreatePointInTimeRequest createPointInTimeRequest) { | ||
//todo: implement | ||
return null; | ||
|
||
OpenPointInTimeResponse openPointInTimeResponse; | ||
try { | ||
openPointInTimeResponse = elasticsearchClient.openPointInTime(OpenPointInTimeRequest.of(request -> request | ||
.keepAlive(Time.of(time -> time.time(createPointInTimeRequest.getKeepAlive()))) | ||
.index(createPointInTimeRequest.getIndex()))); | ||
} catch (final ElasticsearchException e) { | ||
if (isDueToPitLimitExceeded(e)) { | ||
throw new SearchContextLimitException(String.format("There was an error creating a new point in time for index '%s': %s", createPointInTimeRequest.getIndex(), | ||
e.error().causedBy().reason())); | ||
} | ||
LOG.error("There was an error creating a point in time for Elasticsearch: ", e); | ||
throw e; | ||
} catch (final IOException e) { | ||
LOG.error("There was an error creating a point in time for Elasticsearch: ", e); | ||
throw new RuntimeException(e); | ||
} | ||
|
||
return CreatePointInTimeResponse.builder() | ||
.withPitId(openPointInTimeResponse.id()) | ||
.withCreationTime(Instant.now().toEpochMilli()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public SearchWithSearchAfterResults searchWithPit(SearchPointInTimeRequest searchPointInTimeRequest) { | ||
//todo: implement | ||
return null; | ||
public SearchWithSearchAfterResults searchWithPit(final SearchPointInTimeRequest searchPointInTimeRequest) { | ||
final SearchRequest searchRequest = SearchRequest.of(builder -> { builder | ||
.pit(PointInTimeReference.of(pit -> pit | ||
.id(searchPointInTimeRequest.getPitId()) | ||
.keepAlive(Time.of(time -> time.time(searchPointInTimeRequest.getKeepAlive()))))) | ||
.size(searchPointInTimeRequest.getPaginationSize()) | ||
.sort(SortOptions.of(sortOptionsBuilder -> sortOptionsBuilder.doc(ScoreSort.of(scoreSort -> scoreSort.order(SortOrder.Asc))))) | ||
.query(Query.of(query -> query.matchAll(MatchAllQuery.of(matchAllQuery -> matchAllQuery)))); | ||
|
||
if (Objects.nonNull(searchPointInTimeRequest.getSearchAfter())) { | ||
builder.searchAfter(searchPointInTimeRequest.getSearchAfter()); | ||
} | ||
return builder; | ||
}); | ||
|
||
|
||
return searchWithSearchAfter(searchRequest); | ||
} | ||
|
||
@Override | ||
public void deletePit(final DeletePointInTimeRequest deletePointInTimeRequest) { | ||
//todo: implement | ||
try { | ||
final ClosePointInTimeResponse closePointInTimeResponse = elasticsearchClient.closePointInTime(ClosePointInTimeRequest.of(request -> request | ||
.id(deletePointInTimeRequest.getPitId()))); | ||
if (closePointInTimeResponse.succeeded()) { | ||
LOG.debug("Successfully deleted point in time id {}", deletePointInTimeRequest.getPitId()); | ||
} else { | ||
LOG.warn("Point in time id {} was not deleted successfully. It will expire from keep-alive", deletePointInTimeRequest.getPitId()); | ||
} | ||
} catch (final IOException | RuntimeException e) { | ||
LOG.error("There was an error deleting the point in time with id {} for Elasticsearch. It will expire from keep-alive: ", deletePointInTimeRequest.getPitId(), e); | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -59,12 +147,56 @@ public void deleteScroll(DeleteScrollRequest deleteScrollRequest) { | |
} | ||
|
||
@Override | ||
public SearchWithSearchAfterResults searchWithoutSearchContext(NoSearchContextSearchRequest noSearchContextSearchRequest) { | ||
return null; | ||
public SearchWithSearchAfterResults searchWithoutSearchContext(final NoSearchContextSearchRequest noSearchContextSearchRequest) { | ||
final SearchRequest searchRequest = SearchRequest.of(builder -> { | ||
builder | ||
.index(noSearchContextSearchRequest.getIndex()) | ||
.size(noSearchContextSearchRequest.getPaginationSize()) | ||
.sort(SortOptions.of(sortOptionsBuilder -> sortOptionsBuilder.doc(ScoreSort.of(scoreSort -> scoreSort.order(SortOrder.Asc))))) | ||
.query(Query.of(query -> query.matchAll(MatchAllQuery.of(matchAllQuery -> matchAllQuery)))); | ||
|
||
if (Objects.nonNull(noSearchContextSearchRequest.getSearchAfter())) { | ||
builder.searchAfter(noSearchContextSearchRequest.getSearchAfter()); | ||
} | ||
|
||
return builder; | ||
}); | ||
|
||
return searchWithSearchAfter(searchRequest); | ||
} | ||
|
||
@Override | ||
public Object getClient() { | ||
return null; | ||
return elasticsearchClient; | ||
} | ||
|
||
private SearchWithSearchAfterResults searchWithSearchAfter(final SearchRequest searchRequest) { | ||
|
||
try { | ||
final SearchResponse<ObjectNode> searchResponse = elasticsearchClient.search(searchRequest, ObjectNode.class); | ||
|
||
final List<Event> documents = searchResponse.hits().hits().stream() | ||
.map(hit -> JacksonEvent.builder() | ||
.withData(hit.source()) | ||
.withEventMetadataAttributes(Map.of(DOCUMENT_ID_METADATA_ATTRIBUTE_NAME, hit.id(), INDEX_METADATA_ATTRIBUTE_NAME, hit.index())) | ||
.withEventType(EventType.DOCUMENT.toString()).build()) | ||
.collect(Collectors.toList()); | ||
|
||
final List<String> nextSearchAfter = Objects.nonNull(searchResponse.hits().hits()) && !searchResponse.hits().hits().isEmpty() ? | ||
searchResponse.hits().hits().get(searchResponse.hits().hits().size() - 1).sort() : | ||
null; | ||
|
||
return SearchWithSearchAfterResults.builder() | ||
.withDocuments(documents) | ||
.withNextSearchAfter(nextSearchAfter) | ||
.build(); | ||
} catch (final IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private boolean isDueToPitLimitExceeded(final ElasticsearchException e) { | ||
return Objects.nonNull(e.error()) && Objects.nonNull(e.error().causedBy()) && Objects.nonNull(e.error().causedBy().type()) | ||
&& PIT_RESOURCE_LIMIT_ERROR_TYPE.equals(e.error().causedBy().type()); | ||
} | ||
} |
Oops, something went wrong.
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.
We can just take in the index name here to simplify the logic.
The calls above to this method would then be
filter(record -> shouldIndexBeProcessed(record.index()))