From 6a9049a840fc2da4816e2a6cf1837bd31218ae97 Mon Sep 17 00:00:00 2001 From: Andrew Prudhomme Date: Wed, 30 Oct 2024 12:55:12 -0700 Subject: [PATCH] Expose more server endpoint through cli commands (#773) * Expose more server endpoint through cli commands * Address review feedback --- .../server/grpc/NrtsearchClient.java | 8 +++ .../nrtsearch/tools/cli/CustomCommand.java | 56 +++++++++++++++++++ .../tools/cli/DeleteByQueryCommand.java | 56 +++++++++++++++++++ .../tools/cli/GlobalStateCommand.java | 47 ++++++++++++++++ .../nrtsearch/tools/cli/MetricsCommand.java | 47 ++++++++++++++++ .../nrtsearch/tools/cli/NodeInfoCommand.java | 47 ++++++++++++++++ .../tools/cli/NrtsearchClientCommand.java | 5 ++ 7 files changed, 266 insertions(+) create mode 100644 src/main/java/com/yelp/nrtsearch/tools/cli/CustomCommand.java create mode 100644 src/main/java/com/yelp/nrtsearch/tools/cli/DeleteByQueryCommand.java create mode 100644 src/main/java/com/yelp/nrtsearch/tools/cli/GlobalStateCommand.java create mode 100644 src/main/java/com/yelp/nrtsearch/tools/cli/MetricsCommand.java create mode 100644 src/main/java/com/yelp/nrtsearch/tools/cli/NodeInfoCommand.java diff --git a/src/main/java/com/yelp/nrtsearch/server/grpc/NrtsearchClient.java b/src/main/java/com/yelp/nrtsearch/server/grpc/NrtsearchClient.java index 8f6e7ba5a..c9583d03c 100644 --- a/src/main/java/com/yelp/nrtsearch/server/grpc/NrtsearchClient.java +++ b/src/main/java/com/yelp/nrtsearch/server/grpc/NrtsearchClient.java @@ -361,6 +361,14 @@ public void deleteAllDocuments(String indexName) { logger.info("Server returned genId : " + response.getGenId()); } + public void deleteByQuery(String indexName, Query query) { + AddDocumentResponse response = + blockingStub.deleteByQuery( + DeleteByQueryRequest.newBuilder().setIndexName(indexName).addQuery(query).build()); + logger.info( + "Server returned primaryId: {}, genId : {}", response.getPrimaryId(), response.getGenId()); + } + public void stopIndex(String indexName) { blockingStub.stopIndex(StopIndexRequest.newBuilder().setIndexName(indexName).build()); } diff --git a/src/main/java/com/yelp/nrtsearch/tools/cli/CustomCommand.java b/src/main/java/com/yelp/nrtsearch/tools/cli/CustomCommand.java new file mode 100644 index 000000000..165f5c6af --- /dev/null +++ b/src/main/java/com/yelp/nrtsearch/tools/cli/CustomCommand.java @@ -0,0 +1,56 @@ +/* + * Copyright 2024 Yelp Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.yelp.nrtsearch.tools.cli; + +import com.yelp.nrtsearch.server.grpc.CustomRequest; +import com.yelp.nrtsearch.server.grpc.CustomResponse; +import com.yelp.nrtsearch.server.grpc.NrtsearchClient; +import java.util.concurrent.Callable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import picocli.CommandLine; + +@CommandLine.Command( + name = CustomCommand.CUSTOM_COMMAND, + description = "Sends a custom command to endpoint registered by a plugin") +public class CustomCommand implements Callable { + public static final String CUSTOM_COMMAND = "custom"; + private static final Logger logger = LoggerFactory.getLogger(CustomCommand.class); + + @CommandLine.ParentCommand private NrtsearchClientCommand baseCmd; + + @CommandLine.Option( + names = {"-r", "--request"}, + description = + "The custom request to send, CustomRequest protobuf message as json or @file/path", + required = true) + private String request; + + @Override + public Integer call() throws Exception { + CustomRequest.Builder requestBuilder = CustomRequest.newBuilder(); + CliUtils.mergeBuilderFromParam(request, requestBuilder); + + NrtsearchClient client = baseCmd.getClient(); + try { + CustomResponse response = client.getBlockingStub().custom(requestBuilder.build()); + logger.info("Server returned : {}", response); + } finally { + client.shutdown(); + } + return 0; + } +} diff --git a/src/main/java/com/yelp/nrtsearch/tools/cli/DeleteByQueryCommand.java b/src/main/java/com/yelp/nrtsearch/tools/cli/DeleteByQueryCommand.java new file mode 100644 index 000000000..ae33b06dd --- /dev/null +++ b/src/main/java/com/yelp/nrtsearch/tools/cli/DeleteByQueryCommand.java @@ -0,0 +1,56 @@ +/* + * Copyright 2024 Yelp Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.yelp.nrtsearch.tools.cli; + +import com.yelp.nrtsearch.server.grpc.NrtsearchClient; +import com.yelp.nrtsearch.server.grpc.Query; +import java.util.concurrent.Callable; +import picocli.CommandLine; + +@CommandLine.Command( + name = DeleteByQueryCommand.DELETE_BY_QUERY, + description = "Delete all docs that match a query") +public class DeleteByQueryCommand implements Callable { + public static final String DELETE_BY_QUERY = "deleteByQuery"; + + @CommandLine.ParentCommand private NrtsearchClientCommand baseCmd; + + @CommandLine.Option( + names = {"-i", "--indexName"}, + description = "Name of the index whose docs are to be deleted", + required = true) + private String indexName; + + @CommandLine.Option( + names = {"-q", "--query"}, + description = "Deletion query, Query protobuf message as json or @file/path", + required = true) + private String query; + + @Override + public Integer call() throws Exception { + Query.Builder queryBuilder = Query.newBuilder(); + CliUtils.mergeBuilderFromParam(query, queryBuilder); + + NrtsearchClient client = baseCmd.getClient(); + try { + client.deleteByQuery(indexName, queryBuilder.build()); + } finally { + client.shutdown(); + } + return 0; + } +} diff --git a/src/main/java/com/yelp/nrtsearch/tools/cli/GlobalStateCommand.java b/src/main/java/com/yelp/nrtsearch/tools/cli/GlobalStateCommand.java new file mode 100644 index 000000000..36ba8a523 --- /dev/null +++ b/src/main/java/com/yelp/nrtsearch/tools/cli/GlobalStateCommand.java @@ -0,0 +1,47 @@ +/* + * Copyright 2024 Yelp Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.yelp.nrtsearch.tools.cli; + +import com.yelp.nrtsearch.server.grpc.GlobalStateRequest; +import com.yelp.nrtsearch.server.grpc.GlobalStateResponse; +import com.yelp.nrtsearch.server.grpc.NrtsearchClient; +import java.util.concurrent.Callable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import picocli.CommandLine; + +@CommandLine.Command( + name = GlobalStateCommand.GLOBAL_STATE, + description = "Get server global state") +public class GlobalStateCommand implements Callable { + public static final String GLOBAL_STATE = "globalState"; + private static final Logger logger = LoggerFactory.getLogger(GlobalStateCommand.class); + + @CommandLine.ParentCommand private NrtsearchClientCommand baseCmd; + + @Override + public Integer call() throws Exception { + NrtsearchClient client = baseCmd.getClient(); + try { + GlobalStateResponse response = + client.getBlockingStub().globalState(GlobalStateRequest.newBuilder().build()); + logger.info("Server returned global state: {}", response); + } finally { + client.shutdown(); + } + return 0; + } +} diff --git a/src/main/java/com/yelp/nrtsearch/tools/cli/MetricsCommand.java b/src/main/java/com/yelp/nrtsearch/tools/cli/MetricsCommand.java new file mode 100644 index 000000000..c7c79bf61 --- /dev/null +++ b/src/main/java/com/yelp/nrtsearch/tools/cli/MetricsCommand.java @@ -0,0 +1,47 @@ +/* + * Copyright 2024 Yelp Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.yelp.nrtsearch.tools.cli; + +import com.google.api.HttpBody; +import com.google.protobuf.Empty; +import com.yelp.nrtsearch.server.grpc.NrtsearchClient; +import java.util.concurrent.Callable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import picocli.CommandLine; + +@CommandLine.Command( + name = MetricsCommand.METRICS, + description = "Get prometheus metrics for the server") +public class MetricsCommand implements Callable { + public static final String METRICS = "metrics"; + private static final Logger logger = LoggerFactory.getLogger(MetricsCommand.class); + + @CommandLine.ParentCommand private NrtsearchClientCommand baseCmd; + + @Override + public Integer call() throws Exception { + NrtsearchClient client = baseCmd.getClient(); + try { + HttpBody response = client.getBlockingStub().metrics(Empty.newBuilder().build()); + String metrics = new String(response.getData().toByteArray()); + logger.info("Server returned metrics:\n{}", metrics); + } finally { + client.shutdown(); + } + return 0; + } +} diff --git a/src/main/java/com/yelp/nrtsearch/tools/cli/NodeInfoCommand.java b/src/main/java/com/yelp/nrtsearch/tools/cli/NodeInfoCommand.java new file mode 100644 index 000000000..5fa4cba5a --- /dev/null +++ b/src/main/java/com/yelp/nrtsearch/tools/cli/NodeInfoCommand.java @@ -0,0 +1,47 @@ +/* + * Copyright 2024 Yelp Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.yelp.nrtsearch.tools.cli; + +import com.yelp.nrtsearch.server.grpc.NodeInfoRequest; +import com.yelp.nrtsearch.server.grpc.NodeInfoResponse; +import com.yelp.nrtsearch.server.grpc.NrtsearchClient; +import java.util.concurrent.Callable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import picocli.CommandLine; + +@CommandLine.Command( + name = NodeInfoCommand.NODE_INFO, + description = "Get node information for the server") +public class NodeInfoCommand implements Callable { + public static final String NODE_INFO = "nodeInfo"; + private static final Logger logger = LoggerFactory.getLogger(NodeInfoCommand.class); + + @CommandLine.ParentCommand private NrtsearchClientCommand baseCmd; + + @Override + public Integer call() throws Exception { + NrtsearchClient client = baseCmd.getClient(); + try { + NodeInfoResponse response = + client.getBlockingStub().nodeInfo(NodeInfoRequest.newBuilder().build()); + logger.info("Server returned node info: {}", response); + } finally { + client.shutdown(); + } + return 0; + } +} diff --git a/src/main/java/com/yelp/nrtsearch/tools/cli/NrtsearchClientCommand.java b/src/main/java/com/yelp/nrtsearch/tools/cli/NrtsearchClientCommand.java index 553e778e6..40590aa0c 100644 --- a/src/main/java/com/yelp/nrtsearch/tools/cli/NrtsearchClientCommand.java +++ b/src/main/java/com/yelp/nrtsearch/tools/cli/NrtsearchClientCommand.java @@ -31,14 +31,19 @@ CommitCommand.class, CreateIndexCommand.class, GetCurrentSearcherVersion.class, + CustomCommand.class, + DeleteByQueryCommand.class, DeleteDocumentsCommand.class, DeleteAllDocumentsCommand.class, DeleteIndexCommand.class, ForceMergeCommand.class, ForceMergeDeletesCommand.class, + GlobalStateCommand.class, IndexStateCommand.class, IndicesCommand.class, LiveSettingsV2Command.class, + MetricsCommand.class, + NodeInfoCommand.class, ReadyCommand.class, RefreshCommand.class, RegisterFieldsCommand.class,