-
Notifications
You must be signed in to change notification settings - Fork 144
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: Naveen Tatikonda <navtat@amazon.com>
- Loading branch information
1 parent
0a7fc4e
commit 24f2309
Showing
9 changed files
with
437 additions
and
2 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
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
100 changes: 100 additions & 0 deletions
100
src/main/java/org/opensearch/knn/plugin/rest/RestClearCacheHandler.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,100 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.plugin.rest; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import lombok.AllArgsConstructor; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.cluster.metadata.IndexNameExpressionResolver; | ||
import org.opensearch.cluster.service.ClusterService; | ||
import org.opensearch.common.Strings; | ||
import org.opensearch.index.Index; | ||
import org.opensearch.knn.common.exception.KNNInvalidIndicesException; | ||
import org.opensearch.knn.plugin.KNNPlugin; | ||
import org.opensearch.knn.plugin.transport.ClearCacheAction; | ||
import org.opensearch.knn.plugin.transport.ClearCacheRequest; | ||
import org.opensearch.rest.BaseRestHandler; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.rest.action.RestToXContentListener; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.opensearch.action.support.IndicesOptions.strictExpandOpen; | ||
import static org.opensearch.knn.common.KNNConstants.CLEAR_CACHE; | ||
import static org.opensearch.knn.index.KNNSettings.KNN_INDEX; | ||
|
||
/** | ||
* RestHandler for k-NN Clear Cache API. API provides the ability for a user to evict those indices from Cache. | ||
*/ | ||
@AllArgsConstructor | ||
public class RestClearCacheHandler extends BaseRestHandler { | ||
private static final Logger logger = LogManager.getLogger(RestClearCacheHandler.class); | ||
|
||
private static final String INDEX = "index"; | ||
public static String NAME = "knn_clear_cache_action"; | ||
private final ClusterService clusterService; | ||
private final IndexNameExpressionResolver indexNameExpressionResolver; | ||
|
||
/** | ||
* @return name of Clear Cache API action | ||
*/ | ||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
/** | ||
* @return Immutable List of Clear Cache API endpoint | ||
*/ | ||
@Override | ||
public List<Route> routes() { | ||
return ImmutableList.of( | ||
new Route(RestRequest.Method.POST, String.format(Locale.ROOT, "%s/%s/{%s}", KNNPlugin.KNN_BASE_URI, CLEAR_CACHE, INDEX)) | ||
); | ||
} | ||
|
||
/** | ||
* @param request RestRequest | ||
* @param client NodeClient | ||
* @return RestChannelConsumer | ||
*/ | ||
@Override | ||
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) { | ||
ClearCacheRequest clearCacheRequest = createClearCacheRequest(request); | ||
logger.info("[KNN] ClearCache started for the following indices: [{}]", String.join(",", clearCacheRequest.indices())); | ||
return channel -> client.execute(ClearCacheAction.INSTANCE, clearCacheRequest, new RestToXContentListener<>(channel)); | ||
} | ||
|
||
// Create a clear cache request by processing the rest request and validating the indices | ||
private ClearCacheRequest createClearCacheRequest(RestRequest request) { | ||
String[] indexNames = Strings.splitStringByCommaToArray(request.param("index")); | ||
Index[] indices = indexNameExpressionResolver.concreteIndices(clusterService.state(), strictExpandOpen(), indexNames); | ||
validateIndices(indices); | ||
|
||
return new ClearCacheRequest(indexNames); | ||
} | ||
|
||
// Validate if the given indices are k-NN indices or not. If there are any invalid indices, | ||
// the request is rejected and an exception is thrown. | ||
private void validateIndices(Index[] indices) { | ||
List<String> invalidIndexNames = Arrays.stream(indices) | ||
.filter(index -> !"true".equals(clusterService.state().metadata().getIndexSafe(index).getSettings().get(KNN_INDEX))) | ||
.map(Index::getName) | ||
.collect(Collectors.toList()); | ||
|
||
if (!invalidIndexNames.isEmpty()) { | ||
throw new KNNInvalidIndicesException( | ||
invalidIndexNames, | ||
"ClearCache request rejected. One or more indices have 'index.knn' set to false." | ||
); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/org/opensearch/knn/plugin/transport/ClearCacheAction.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,27 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.plugin.transport; | ||
|
||
import org.opensearch.action.ActionType; | ||
import org.opensearch.common.io.stream.Writeable; | ||
|
||
/** | ||
* Action associated with ClearCache | ||
*/ | ||
public class ClearCacheAction extends ActionType<ClearCacheResponse> { | ||
|
||
public static final ClearCacheAction INSTANCE = new ClearCacheAction(); | ||
public static final String NAME = "cluster:admin/clear_cache_action"; | ||
|
||
private ClearCacheAction() { | ||
super(NAME, ClearCacheResponse::new); | ||
} | ||
|
||
@Override | ||
public Writeable.Reader<ClearCacheResponse> getResponseReader() { | ||
return ClearCacheResponse::new; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/org/opensearch/knn/plugin/transport/ClearCacheRequest.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,36 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.plugin.transport; | ||
|
||
import org.opensearch.action.support.broadcast.BroadcastRequest; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Clear Cache Request. This request contains a list of indices which needs to be evicted from Cache. | ||
*/ | ||
public class ClearCacheRequest extends BroadcastRequest<ClearCacheRequest> { | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param in input stream | ||
* @throws IOException if read from stream fails | ||
*/ | ||
public ClearCacheRequest(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param indices list of indices which needs to be evicted from cache | ||
*/ | ||
public ClearCacheRequest(String... indices) { | ||
super(indices); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/org/opensearch/knn/plugin/transport/ClearCacheResponse.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,49 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.plugin.transport; | ||
|
||
import org.opensearch.action.support.DefaultShardOperationFailedException; | ||
import org.opensearch.action.support.broadcast.BroadcastResponse; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.core.xcontent.ToXContentObject; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
* {@link ClearCacheResponse} represents Response returned by {@link ClearCacheRequest}. | ||
* Returns total number of shards on which ClearCache was performed on, as well as | ||
* the number of shards that succeeded and the number of shards that failed. | ||
*/ | ||
public class ClearCacheResponse extends BroadcastResponse implements ToXContentObject { | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param in input stream | ||
* @throws IOException if read from stream fails | ||
*/ | ||
public ClearCacheResponse(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param totalShards total number of shards on which ClearCache was performed | ||
* @param successfulShards number of shards that succeeded | ||
* @param failedShards number of shards that failed | ||
* @param shardFailures list of shard failure exceptions | ||
*/ | ||
public ClearCacheResponse( | ||
int totalShards, | ||
int successfulShards, | ||
int failedShards, | ||
List<DefaultShardOperationFailedException> shardFailures | ||
) { | ||
super(totalShards, successfulShards, failedShards, shardFailures); | ||
} | ||
} |
Oops, something went wrong.