Skip to content

Commit

Permalink
Expose more server endpoint through cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
aprudhomme committed Oct 24, 2024
1 parent 015c1c5 commit 9c6585c
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/main/java/com/yelp/nrtsearch/server/grpc/NrtsearchClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import static com.yelp.nrtsearch.server.grpc.ReplicationServerClient.MAX_MESSAGE_BYTES_SIZE;

import com.google.api.HttpBody;
import com.google.protobuf.Empty;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import io.grpc.ManagedChannel;
Expand Down Expand Up @@ -361,6 +363,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 Expand Up @@ -400,6 +410,28 @@ public List<String> getIndices() {
.collect(Collectors.toList());
}

public void nodeInfo() {
NodeInfoResponse response = blockingStub.nodeInfo(NodeInfoRequest.newBuilder().build());
logger.info("Server returned node info: {}", response);
}

public void globalState() {
GlobalStateResponse response =
blockingStub.globalState(GlobalStateRequest.newBuilder().build());
logger.info("Server returned global state: {}", response);
}

public void metrics() {
HttpBody response = blockingStub.metrics(Empty.newBuilder().build());
String metrics = new String(response.getData().toByteArray());
logger.info("Server returned metrics:\n{}", metrics);
}

public void custom(CustomRequest request) {
CustomResponse response = blockingStub.custom(request);
logger.info("Server returned : {}", response);
}

private FieldDefRequest getFieldDefRequest(String jsonStr) {
logger.info(String.format("Converting fields %s to proto FieldDefRequest", jsonStr));
FieldDefRequest.Builder fieldDefRequestBuilder = FieldDefRequest.newBuilder();
Expand Down
52 changes: 52 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,52 @@
/*
* 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.NrtsearchClient;
import java.util.concurrent.Callable;
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";

@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 {
// Call the appropriate method to send the custom request
client.custom(requestBuilder.build());
} 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;
}
}
41 changes: 41 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,41 @@
/*
* 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 java.util.concurrent.Callable;
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";

@CommandLine.ParentCommand private NrtsearchClientCommand baseCmd;

@Override
public Integer call() throws Exception {
NrtsearchClient client = baseCmd.getClient();
try {
// Call the appropriate method to get global state
client.globalState();
} finally {
client.shutdown();
}
return 0;
}
}
41 changes: 41 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,41 @@
/*
* 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 java.util.concurrent.Callable;
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";

@CommandLine.ParentCommand private NrtsearchClientCommand baseCmd;

@Override
public Integer call() throws Exception {
NrtsearchClient client = baseCmd.getClient();
try {
// Call the appropriate method to get metrics
client.metrics();
} finally {
client.shutdown();
}
return 0;
}
}
41 changes: 41 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,41 @@
/*
* 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 java.util.concurrent.Callable;
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";

@CommandLine.ParentCommand private NrtsearchClientCommand baseCmd;

@Override
public Integer call() throws Exception {
NrtsearchClient client = baseCmd.getClient();
try {
// Call the appropriate method to get node information
client.nodeInfo();
} finally {
client.shutdown();
}
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@
CommitCommand.class,
CreateIndexCommand.class,
GetCurrentSearcherVersion.class,
CustomCommand.class,
DeleteByQueryCommand.class,
DeleteDocumentsCommand.class,
DeleteAllDocumentsCommand.class,
DeleteIndexCommand.class,
ForceMergeCommand.class,
ForceMergeDeletesCommand.class,
GlobalStateCommand.class,
IndicesCommand.class,
LiveSettingsV2Command.class,
MetricsCommand.class,
NodeInfoCommand.class,
ReadyCommand.class,
RefreshCommand.class,
RegisterFieldsCommand.class,
Expand Down

0 comments on commit 9c6585c

Please sign in to comment.