Browse Source

Simplify `command`

jianghua 4 years ago
parent
commit
97eb43b4a5

+ 6 - 9
src/main/java/io/milvus/client/MilvusClient.java

@@ -290,26 +290,23 @@ public interface MilvusClient {
   /**
   /**
    * Gets server status
    * Gets server status
    *
    *
-   * @return <code>Response</code>
-   * @see Response
+   * @return a server status string
    */
    */
-  Response getServerStatus();
+  String getServerStatus();
 
 
   /**
   /**
    * Gets server version
    * Gets server version
    *
    *
-   * @return <code>Response</code>
-   * @see Response
+   * @return a server version string.
    */
    */
-  Response getServerVersion();
+  String getServerVersion();
 
 
   /**
   /**
    * Sends a command to server
    * Sends a command to server
    *
    *
-   * @return <code>Response</code> command's response will be return in <code>message</code>
-   * @see Response
+   * @return a message string
    */
    */
-  Response command(String command);
+  String command(String command);
 
 
   /**
   /**
    * Pre-loads collection to memory
    * Pre-loads collection to memory

+ 12 - 37
src/main/java/io/milvus/client/MilvusGrpcClient.java

@@ -32,7 +32,6 @@ import io.grpc.ManagedChannelBuilder;
 import io.grpc.MethodDescriptor;
 import io.grpc.MethodDescriptor;
 import io.grpc.StatusRuntimeException;
 import io.grpc.StatusRuntimeException;
 import io.milvus.client.exception.ClientSideMilvusException;
 import io.milvus.client.exception.ClientSideMilvusException;
-import io.milvus.client.exception.InitializationException;
 import io.milvus.client.exception.MilvusException;
 import io.milvus.client.exception.MilvusException;
 import io.milvus.client.exception.ServerSideMilvusException;
 import io.milvus.client.exception.ServerSideMilvusException;
 import io.milvus.client.exception.UnsupportedServerVersion;
 import io.milvus.client.exception.UnsupportedServerVersion;
@@ -80,14 +79,9 @@ public class MilvusGrpcClient extends AbstractMilvusGrpcClient {
     blockingStub = MilvusServiceGrpc.newBlockingStub(channel);
     blockingStub = MilvusServiceGrpc.newBlockingStub(channel);
     futureStub = MilvusServiceGrpc.newFutureStub(channel);
     futureStub = MilvusServiceGrpc.newFutureStub(channel);
     try {
     try {
-      Response response = getServerVersion();
-      if (response.ok()) {
-        String serverVersion = response.getMessage();
-        if (!serverVersion.matches("^" + SUPPORTED_SERVER_VERSION + "(\\..*)?$")) {
-          throw new UnsupportedServerVersion(connectParam.getTarget(), SUPPORTED_SERVER_VERSION, serverVersion);
-        }
-      } else {
-        throw new InitializationException(connectParam.getTarget(), response.getMessage());
+      String serverVersion = getServerVersion();
+      if (!serverVersion.matches("^" + SUPPORTED_SERVER_VERSION + "(\\..*)?$")) {
+        throw new UnsupportedServerVersion(connectParam.getTarget(), SUPPORTED_SERVER_VERSION, serverVersion);
       }
       }
     } catch (Throwable t) {
     } catch (Throwable t) {
       channel.shutdownNow();
       channel.shutdownNow();
@@ -387,41 +381,22 @@ abstract class AbstractMilvusGrpcClient implements MilvusClient {
   }
   }
 
 
   @Override
   @Override
-  public Response getServerStatus() {
+  public String getServerStatus() {
     return command("status");
     return command("status");
   }
   }
 
 
   @Override
   @Override
-  public Response getServerVersion() {
+  public String getServerVersion() {
     return command("version");
     return command("version");
   }
   }
 
 
-  public Response command(@Nonnull String command) {
-
-    if (!maybeAvailable()) {
-      logWarning("You are not connected to Milvus server");
-      return new Response(Response.Status.CLIENT_NOT_CONNECTED);
-    }
-
-    Command request = Command.newBuilder().setCmd(command).build();
-    StringReply response;
-
-    try {
-      response = blockingStub().cmd(request);
-
-      if (response.getStatus().getErrorCode() == ErrorCode.SUCCESS) {
-        logInfo("Command `{}`: {}", command, response.getStringReply());
-        return new Response(Response.Status.SUCCESS, response.getStringReply());
-      } else {
-        logError("Command `{}` failed:\n{}", command, response.toString());
-        return new Response(
-            Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
-            response.getStatus().getReason());
-      }
-    } catch (StatusRuntimeException e) {
-      logError("Command RPC failed:\n{}", e.getStatus().toString());
-      return new Response(Response.Status.RPC_ERROR, e.toString());
-    }
+  public String command(@Nonnull String command) {
+    return translateExceptions(() -> {
+      Command request = Command.newBuilder().setCmd(command).build();
+      StringReply response = blockingStub().cmd(request);
+      checkResponseStatus(response.getStatus());
+      return response.getStringReply();
+    });
   }
   }
 
 
   @Override
   @Override

+ 0 - 7
src/main/java/io/milvus/client/exception/InitializationException.java

@@ -1,7 +0,0 @@
-package io.milvus.client.exception;
-
-public class InitializationException extends ClientSideMilvusException {
-  public InitializationException(String target, String message) {
-    super(target, message);
-  }
-}

+ 6 - 7
src/test/java/io/milvus/client/MilvusGrpcClientTest.java

@@ -20,12 +20,12 @@
 package io.milvus.client;
 package io.milvus.client;
 
 
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
 import io.grpc.NameResolverProvider;
 import io.grpc.NameResolverProvider;
 import io.grpc.NameResolverRegistry;
 import io.grpc.NameResolverRegistry;
 import io.grpc.Status;
 import io.grpc.Status;
 import io.grpc.StatusRuntimeException;
 import io.grpc.StatusRuntimeException;
 import io.milvus.client.exception.ClientSideMilvusException;
 import io.milvus.client.exception.ClientSideMilvusException;
-import io.milvus.client.exception.InitializationException;
 import io.milvus.client.exception.ServerSideMilvusException;
 import io.milvus.client.exception.ServerSideMilvusException;
 import io.milvus.client.exception.UnsupportedServerVersion;
 import io.milvus.client.exception.UnsupportedServerVersion;
 import io.milvus.grpc.ErrorCode;
 import io.milvus.grpc.ErrorCode;
@@ -92,7 +92,7 @@ class ContainerMilvusClientTest extends MilvusClientTest {
 
 
     MilvusClient loadBalancingClient = new MilvusGrpcClient(connectParam);
     MilvusClient loadBalancingClient = new MilvusGrpcClient(connectParam);
     assertEquals(50, IntStream.range(0, 100)
     assertEquals(50, IntStream.range(0, 100)
-            .filter(i -> loadBalancingClient.hasCollection(randomCollectionName).hasCollection())
+            .filter(i -> loadBalancingClient.hasCollection(randomCollectionName))
             .count());
             .count());
   }
   }
 }
 }
@@ -280,7 +280,7 @@ class MilvusClientTest {
   @org.junit.jupiter.api.Test
   @org.junit.jupiter.api.Test
   void connectUnreachableHost() {
   void connectUnreachableHost() {
     ConnectParam connectParam = connectParamBuilder("250.250.250.250", 19530).build();
     ConnectParam connectParam = connectParamBuilder("250.250.250.250", 19530).build();
-    assertThrows(InitializationException.class, () -> new MilvusGrpcClient(connectParam));
+    assertThrows(ClientSideMilvusException.class, () -> new MilvusGrpcClient(connectParam));
   }
   }
 
 
   @org.junit.jupiter.api.Test
   @org.junit.jupiter.api.Test
@@ -626,14 +626,13 @@ class MilvusClientTest {
 
 
   @org.junit.jupiter.api.Test
   @org.junit.jupiter.api.Test
   void serverStatus() {
   void serverStatus() {
-    Response serverStatusResponse = client.getServerStatus();
-    assertTrue(serverStatusResponse.ok());
+    JSONObject serverStatus = new JSONObject(client.getServerStatus());
+    assertEquals(ImmutableSet.of("indexing", "require_restart", "server_time", "uptime"), serverStatus.keySet());
   }
   }
 
 
   @org.junit.jupiter.api.Test
   @org.junit.jupiter.api.Test
   void serverVersion() {
   void serverVersion() {
-    Response serverVersionResponse = client.getServerVersion();
-    assertTrue(serverVersionResponse.ok());
+    assertEquals("0.11.0", client.getServerVersion());
   }
   }
 
 
   @org.junit.jupiter.api.Test
   @org.junit.jupiter.api.Test