-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chen Dai <[email protected]>
- Loading branch information
Showing
8 changed files
with
200 additions
and
123 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
flint-core/src/main/scala/org/opensearch/flint/core/metadata/log/FlintMetadataLog.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,18 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.core.metadata.log; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* Flint metadata log. | ||
*/ | ||
public interface FlintMetadataLog<T> { | ||
|
||
T add(T logEntry); | ||
|
||
Optional<T> getLatest(); | ||
} |
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
128 changes: 128 additions & 0 deletions
128
...ore/src/main/scala/org/opensearch/flint/core/metadata/log/FlintOpenSearchMetadataLog.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,128 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.core.metadata.log; | ||
|
||
import java.io.IOException; | ||
import java.util.Base64; | ||
import java.util.Optional; | ||
import java.util.logging.Logger; | ||
import org.opensearch.OpenSearchException; | ||
import org.opensearch.action.get.GetRequest; | ||
import org.opensearch.action.get.GetResponse; | ||
import org.opensearch.action.index.IndexRequest; | ||
import org.opensearch.action.index.IndexResponse; | ||
import org.opensearch.action.support.WriteRequest; | ||
import org.opensearch.action.update.UpdateRequest; | ||
import org.opensearch.action.update.UpdateResponse; | ||
import org.opensearch.client.RequestOptions; | ||
import org.opensearch.client.RestHighLevelClient; | ||
import org.opensearch.common.xcontent.XContentType; | ||
import org.opensearch.flint.core.FlintClient; | ||
|
||
/** | ||
* Flint metadata log in OpenSearch store. | ||
*/ | ||
public class FlintOpenSearchMetadataLog implements FlintMetadataLog<FlintMetadataLogEntry> { | ||
|
||
private static final Logger LOG = Logger.getLogger(FlintOpenSearchMetadataLog.class.getName()); | ||
|
||
/** | ||
* Flint client to create Rest OpenSearch client (This will be refactored later) | ||
*/ | ||
private final FlintClient flintClient; | ||
|
||
/** | ||
* Reuse query request index as Flint metadata log store | ||
*/ | ||
private final String metadataLogIndexName; | ||
|
||
/** | ||
* Doc id for latest log entry (Naming rule is static so no need to query Flint index metadata) | ||
*/ | ||
private final String latestId; | ||
|
||
public FlintOpenSearchMetadataLog(FlintClient flintClient, String flintIndexName, String metadataLogIndexName) { | ||
this.flintClient = flintClient; | ||
this.latestId = Base64.getEncoder().encodeToString(flintIndexName.getBytes()); | ||
this.metadataLogIndexName = metadataLogIndexName; | ||
} | ||
|
||
@Override | ||
public FlintMetadataLogEntry add(FlintMetadataLogEntry logEntry) { | ||
// TODO: use single doc for now. this will be always append in future. | ||
FlintMetadataLogEntry latest; | ||
if (logEntry.id().isEmpty()) { | ||
latest = createLogEntry(logEntry); | ||
} else { | ||
latest = updateLogEntry(logEntry); | ||
} | ||
return latest; | ||
} | ||
|
||
@Override | ||
public Optional<FlintMetadataLogEntry> getLatest() { | ||
try (RestHighLevelClient client = flintClient.createClient()) { | ||
GetResponse response = | ||
client.get(new GetRequest(metadataLogIndexName, latestId), RequestOptions.DEFAULT); | ||
if (response.isExists()) { | ||
return Optional.of( | ||
new FlintMetadataLogEntry( | ||
response.getId(), | ||
response.getSeqNo(), | ||
response.getPrimaryTerm(), | ||
response.getSourceAsMap())); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} catch (Exception e) { | ||
throw new IllegalStateException("Failed to fetch latest metadata log entry", e); | ||
} | ||
} | ||
|
||
private FlintMetadataLogEntry createLogEntry(FlintMetadataLogEntry logEntry) { | ||
LOG.info("Creating log entry " + logEntry); | ||
try (RestHighLevelClient client = flintClient.createClient()) { | ||
logEntry = logEntry.copy( | ||
latestId, | ||
logEntry.seqNo(), logEntry.primaryTerm(), logEntry.state(), logEntry.dataSource(), logEntry.error()); | ||
|
||
IndexResponse response = client.index( | ||
new IndexRequest() | ||
.index(metadataLogIndexName) | ||
.id(logEntry.id()) | ||
.source(logEntry.toJson(), XContentType.JSON), | ||
RequestOptions.DEFAULT); | ||
|
||
// Update seqNo and primaryTerm in log entry object | ||
return logEntry.copy( | ||
logEntry.id(), response.getSeqNo(), response.getPrimaryTerm(), | ||
logEntry.state(), logEntry.dataSource(), logEntry.error()); | ||
} catch (OpenSearchException | IOException e) { | ||
throw new IllegalStateException("Failed to create initial log entry", e); | ||
} | ||
} | ||
|
||
private FlintMetadataLogEntry updateLogEntry(FlintMetadataLogEntry logEntry) { | ||
LOG.info("Updating log entry " + logEntry); | ||
try (RestHighLevelClient client = flintClient.createClient()) { | ||
UpdateResponse response = | ||
client.update( | ||
new UpdateRequest(metadataLogIndexName, logEntry.id()) | ||
.doc(logEntry.toJson(), XContentType.JSON) | ||
.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL) | ||
.setIfSeqNo(logEntry.seqNo()) | ||
.setIfPrimaryTerm(logEntry.primaryTerm()), | ||
RequestOptions.DEFAULT); | ||
|
||
// Update seqNo and primaryTerm in log entry object | ||
return logEntry.copy( | ||
logEntry.id(), response.getSeqNo(), response.getPrimaryTerm(), | ||
logEntry.state(), logEntry.dataSource(), logEntry.error()); | ||
} catch (OpenSearchException | IOException e) { | ||
throw new IllegalStateException("Failed to update log entry: " + logEntry, e); | ||
} | ||
} | ||
} |
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.