Browse Source

add Command

zhiru 5 years ago
parent
commit
5206540c51

+ 5 - 1
pom.xml

@@ -8,9 +8,13 @@
     <artifactId>milvus-sdk-java</artifactId>
     <version>1.0-SNAPSHOT</version>
 
+    <name>milvus-sdk-java</name>
+    <description>Java SDK for Milvus distributed high-performance vector search engine. </description>
+    <url>https://github.com/milvus-io/milvus-sdk-java</url>
+
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-        <grpc.version>1.23.0</grpc.version><!-- CURRENT_GRPC_VERSION -->
+        <grpc.version>1.24.0</grpc.version><!-- CURRENT_GRPC_VERSION -->
         <protobuf.version>3.9.0</protobuf.version>
         <protoc.version>3.9.0</protoc.version>
         <maven.compiler.source>1.8</maven.compiler.source>

+ 1 - 1
src/main/java/io/milvus/client/ConnectParam.java

@@ -41,7 +41,7 @@ public class ConnectParam {
 
     @Override
     public String toString() {
-        return "ConnectParam{" +
+        return "ConnectParam {" +
                 "host='" + host + '\'' +
                 ", port='" + port + '\'' +
                 '}';

+ 5 - 1
src/main/java/io/milvus/client/MilvusClient.java

@@ -33,7 +33,11 @@ public interface MilvusClient {
 
     GetTableRowCountResponse getTableRowCount(TableParam tableParam);
 
-    //Cmd(Command) not implemented
+    Response serverStatus();
+
+    Response serverVersion();
+
+//    Response command(CommandParam commandParam);
 
     Response deleteByRange(DeleteByRangeParam deleteByRangeParam);
 

+ 49 - 1
src/main/java/io/milvus/client/MilvusGrpcClient.java

@@ -1,6 +1,7 @@
 package io.milvus.client;
 
 import io.grpc.*;
+import io.milvus.client.grpc.Command;
 
 import javax.annotation.Nonnull;
 import java.text.SimpleDateFormat;
@@ -40,7 +41,11 @@ public class MilvusGrpcClient implements MilvusClient {
                     .usePlaintext()
                     .build();
 
+            ConnectivityState connectivityState = channel.getState(true);
+            connectivityState = channel.getState(false);
+
             blockingStub = io.milvus.client.grpc.MilvusServiceGrpc.newBlockingStub(channel);
+
         } catch (Exception e) {
             logSevere("Connect failed!", e.toString());
             return new Response(Response.Status.CONNECT_FAILED);
@@ -472,7 +477,50 @@ public class MilvusGrpcClient implements MilvusClient {
         }
     }
 
-    //Cmd(Command) not implemented
+    @Override
+    public Response serverStatus() {
+        CommandParam commandParam = new CommandParam.Builder("OK").build();
+        return command(commandParam);
+    }
+
+    @Override
+    public Response serverVersion() {
+        CommandParam commandParam = new CommandParam.Builder("version").build();
+        return command(commandParam);
+    }
+
+    private Response command(@Nonnull CommandParam commandParam) {
+
+        if (!connected()) {
+            logWarning("You are not connected to Milvus server");
+            return new Response(Response.Status.CLIENT_NOT_CONNECTED);
+        }
+
+        String command = commandParam.getCommand();
+        io.milvus.client.grpc.Command request = io.milvus.client.grpc.Command
+                                                .newBuilder()
+                                                .setCmd(command)
+                                                .build();
+        io.milvus.client.grpc.StringReply response;
+
+        try {
+            response = blockingStub
+                       .withDeadlineAfter(commandParam.getTimeout(), TimeUnit.SECONDS)
+                       .cmd(request);
+
+            if (response.getStatus().getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
+                logInfo("Command `{0}`: {1}", command, response.getStringReply());
+                return new Response(Response.Status.SUCCESS, response.getStringReply());
+            } else {
+                logSevere("Command `{0}` failed:\n{1}", command, response.toString());
+                return new Response(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
+                                    response.getStatus().getReason());
+            }
+        } catch (StatusRuntimeException e) {
+            logSevere("Command RPC failed:\n{0}", e.getStatus().toString());
+            return new Response(Response.Status.RPC_ERROR, e.toString());
+        }
+    }
 
     public Response deleteByRange(@Nonnull DeleteByRangeParam deleteByRangeParam) {
 

+ 13 - 1
src/test/java/io/milvus/client/MilvusGrpcClientTest.java

@@ -41,7 +41,7 @@ class MilvusGrpcClientTest {
 
         client = new MilvusGrpcClient();
         ConnectParam connectParam = new ConnectParam.Builder()
-                                        .withHost("localhost")
+                                        .withHost("192.168.1.149")
                                         .withPort("19530")
                                         .build();
         client.connect(connectParam);
@@ -197,6 +197,18 @@ class MilvusGrpcClientTest {
         assertTrue(showTablesResponse.getResponse().ok());
     }
 
+    @org.junit.jupiter.api.Test
+    void serverStatus() {
+        Response serverStatusResponse = client.serverStatus();
+        assertTrue(serverStatusResponse.ok());
+    }
+
+    @org.junit.jupiter.api.Test
+    void serverVersion() {
+        Response serverVersionResponse = client.serverVersion();
+        assertTrue(serverVersionResponse.ok());
+    }
+
     @org.junit.jupiter.api.Test
     void getTableRowCount() throws InterruptedException {
         insert();