Ver código fonte

Refine code (#1089)

Signed-off-by: yhmo <yihua.mo@zilliz.com>
groot 7 meses atrás
pai
commit
46829c07a9

+ 14 - 0
examples/main/resources/log4j.properties

@@ -0,0 +1,14 @@
+# Set root logger level to DEBUG and its only appender to A1.
+log4j.rootLogger=INFO, A1
+
+# A1 is set to be a ConsoleAppender.
+log4j.appender.A1=org.apache.log4j.ConsoleAppender
+
+# A1 uses PatternLayout.
+log4j.appender.A1.layout=org.apache.log4j.PatternLayout
+log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
+
+# If the log level is DEBUG, print only messages of level WARN for some noisy libs.
+log4j.logger.io.grpc.netty.shaded=WARN
+log4j.logger.org.apache.parquet=WARN
+log4j.logger.org.apache.hadoop=WARN

+ 6 - 0
examples/pom.xml

@@ -77,6 +77,12 @@
             <version>1.18.22</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-log4j12</artifactId>
+            <version>1.7.36</version>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
 
 </project>

+ 3 - 3
src/main/java/io/milvus/client/MilvusServiceClient.java

@@ -329,14 +329,14 @@ public class MilvusServiceClient extends AbstractMilvusGrpcClient {
 
                     if (timeoutChecker.call() == Boolean.TRUE) {
                         String msg = String.format("Retry timeout: %dms, maxRetry:%d, retries: %d, reason: %s",
-                                this.timeoutMs, maxRetryTimes, k, e.getMessage());
+                                this.timeoutMs, maxRetryTimes, k, e);
                         throw new MilvusException(msg, code.value());
                     }
                 } else if (e instanceof ServerException) {
                     ServerException serverException = (ServerException)e;
                     if (timeoutChecker.call() == Boolean.TRUE) {
                         String msg = String.format("Retry timeout: %dms, maxRetry:%d, retries: %d, reason: %s",
-                                this.timeoutMs, maxRetryTimes, k, e.getMessage());
+                                this.timeoutMs, maxRetryTimes, k, e);
                         throw new MilvusException(msg, serverException.getStatus());
                     }
 
@@ -362,7 +362,7 @@ public class MilvusServiceClient extends AbstractMilvusGrpcClient {
                     // print log, follow the pymilvus logic
                     if (k > 3) {
                         logWarning(String.format("Retry(%d) with interval %dms. Reason: %s",
-                                k, retryIntervalMs, e.getMessage()));
+                                k, retryIntervalMs, e));
                     }
                     TimeUnit.MILLISECONDS.sleep(retryIntervalMs);
                 }

+ 4 - 4
src/main/java/io/milvus/v2/client/MilvusClientV2.java

@@ -161,11 +161,11 @@ public class MilvusClientV2 {
             try {
                 return callable.call();
             } catch (StatusRuntimeException e) {
-                throw new MilvusClientException(ErrorCode.RPC_ERROR, e.getMessage()); // rpc error
+                throw new MilvusClientException(ErrorCode.RPC_ERROR, e); // rpc error
             } catch (MilvusClientException e) {
                 throw e; // server error or client error
             } catch (Exception e) {
-                throw new MilvusClientException(ErrorCode.CLIENT_ERROR, e.getMessage()); // others error treated as client error
+                throw new MilvusClientException(ErrorCode.CLIENT_ERROR, e); // others error treated as client error
             }
         }
 
@@ -195,7 +195,7 @@ public class MilvusClientV2 {
                         || code == Status.ALREADY_EXISTS.getCode()
                         || code == Status.RESOURCE_EXHAUSTED.getCode()
                         || code == Status.UNIMPLEMENTED.getCode()) {
-                    String msg = String.format("Encounter rpc error that cannot be retried, reason: %s", e.getMessage());
+                    String msg = String.format("Encounter rpc error that cannot be retried, reason: %s", e);
                     logger.error(msg);
                     throw new MilvusClientException(ErrorCode.RPC_ERROR, msg); // throw rpc error
                 }
@@ -230,7 +230,7 @@ public class MilvusClientV2 {
                     throw e; // exit retry, throw the error
                 }
             } catch (Exception e) {
-                throw new MilvusClientException(ErrorCode.CLIENT_ERROR, e.getMessage()); // others error treated as client error
+                throw new MilvusClientException(ErrorCode.CLIENT_ERROR, e); // others error treated as client error
             }
 
             try {

+ 5 - 0
src/main/java/io/milvus/v2/exception/MilvusClientException.java

@@ -34,6 +34,11 @@ public class MilvusClientException extends RuntimeException {
         this.errorCode = errorCode;
     }
 
+    public MilvusClientException(ErrorCode errorCode, Throwable e) {
+        super(e);
+        this.errorCode = errorCode;
+    }
+
     public MilvusClientException(ErrorCode errorCode, String message, int serverErrCode, int legacyServerCode) {
         super(message);
         this.errorCode = errorCode;

+ 1 - 1
src/main/java/io/milvus/v2/service/collection/CollectionService.java

@@ -103,7 +103,7 @@ public class CollectionService extends BaseService {
             //TimeUnit.MILLISECONDS.sleep(1000);
             loadCollection(blockingStub, LoadCollectionReq.builder().collectionName(request.getCollectionName()).build());
         } catch (Exception e) {
-            throw new MilvusClientException(ErrorCode.SERVER_ERROR, "Load collection failed" + e.getMessage());
+            throw new MilvusClientException(ErrorCode.SERVER_ERROR, "Load collection failed: " + e);
         }
         return null;
     }