Skip to content

Commit

Permalink
Expose more server endpoint through cli commands (#773)
Browse files Browse the repository at this point in the history
* Expose more server endpoint through cli commands

* Address review feedback
  • Loading branch information
aprudhomme authored Oct 30, 2024
1 parent b3fb57a commit 6a9049a
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/yelp/nrtsearch/tools/cli/CustomCommand.java
Original file line number Diff line number Diff line change
@@ -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<Integer> {
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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer> {
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;
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/yelp/nrtsearch/tools/cli/GlobalStateCommand.java
Original file line number Diff line number Diff line change
@@ -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<Integer> {
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;
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/yelp/nrtsearch/tools/cli/MetricsCommand.java
Original file line number Diff line number Diff line change
@@ -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<Integer> {
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;
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/yelp/nrtsearch/tools/cli/NodeInfoCommand.java
Original file line number Diff line number Diff line change
@@ -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<Integer> {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 6a9049a

Please sign in to comment.