Browse Source

Implement AlterIndex interface (#785)

Signed-off-by: yhmo <yihua.mo@zilliz.com>
groot 1 year ago
parent
commit
4561ed4703

+ 2 - 2
docker-compose.yml

@@ -32,7 +32,7 @@ services:
 
 
   standalone:
   standalone:
     container_name: milvus-javasdk-test-standalone
     container_name: milvus-javasdk-test-standalone
-    image: milvusdb/milvus:v2.3.10
+    image: milvusdb/milvus:master-20240229-50a78b68
     command: ["milvus", "run", "standalone"]
     command: ["milvus", "run", "standalone"]
     environment:
     environment:
       ETCD_ENDPOINTS: etcd:2379
       ETCD_ENDPOINTS: etcd:2379
@@ -77,7 +77,7 @@ services:
 
 
   standaloneslave:
   standaloneslave:
     container_name: milvus-javasdk-test-slave-standalone
     container_name: milvus-javasdk-test-slave-standalone
-    image: milvusdb/milvus:v2.3.10
+    image: milvusdb/milvus:master-20240229-50a78b68
     command: ["milvus", "run", "standalone"]
     command: ["milvus", "run", "standalone"]
     environment:
     environment:
       ETCD_ENDPOINTS: etcdslave:2379
       ETCD_ENDPOINTS: etcdslave:2379

+ 49 - 11
src/main/java/io/milvus/client/AbstractMilvusGrpcClient.java

@@ -559,10 +559,7 @@ public abstract class AbstractMilvusGrpcClient implements MilvusClient {
                 builder.setDbName(requestParam.getDatabaseName());
                 builder.setDbName(requestParam.getDatabaseName());
             }
             }
 
 
-            LoadCollectionRequest loadCollectionRequest = builder
-                    .build();
-
-            Status response = blockingStub().loadCollection(loadCollectionRequest);
+            Status response = blockingStub().loadCollection(builder.build());
             handleResponse(title, response);
             handleResponse(title, response);
 
 
             // sync load, wait until collection finish loading
             // sync load, wait until collection finish loading
@@ -591,11 +588,13 @@ public abstract class AbstractMilvusGrpcClient implements MilvusClient {
         String title = String.format("ReleaseCollectionRequest collectionName:%s", requestParam.getCollectionName());
         String title = String.format("ReleaseCollectionRequest collectionName:%s", requestParam.getCollectionName());
 
 
         try {
         try {
-            ReleaseCollectionRequest releaseCollectionRequest = ReleaseCollectionRequest.newBuilder()
-                    .setCollectionName(requestParam.getCollectionName())
-                    .build();
+            ReleaseCollectionRequest.Builder builder = ReleaseCollectionRequest.newBuilder()
+                    .setCollectionName(requestParam.getCollectionName());
+            if (StringUtils.isNotEmpty(requestParam.getDatabaseName())) {
+                builder.setDbName(requestParam.getDatabaseName());
+            }
 
 
-            Status response = blockingStub().releaseCollection(releaseCollectionRequest);
+            Status response = blockingStub().releaseCollection(builder.build());
             handleResponse(title, response);
             handleResponse(title, response);
             return R.success(new RpcStatus(RpcStatus.SUCCESS_MSG));
             return R.success(new RpcStatus(RpcStatus.SUCCESS_MSG));
         } catch (StatusRuntimeException e) {
         } catch (StatusRuntimeException e) {
@@ -745,13 +744,16 @@ public abstract class AbstractMilvusGrpcClient implements MilvusClient {
         String title = String.format("AlterCollectionRequest collectionName:%s", requestParam.getCollectionName());
         String title = String.format("AlterCollectionRequest collectionName:%s", requestParam.getCollectionName());
 
 
         try {
         try {
-            AlterCollectionRequest.Builder alterCollRequestBuilder = AlterCollectionRequest.newBuilder();
+            AlterCollectionRequest.Builder builder = AlterCollectionRequest.newBuilder();
             List<KeyValuePair> propertiesList = ParamUtils.AssembleKvPair(requestParam.getProperties());
             List<KeyValuePair> propertiesList = ParamUtils.AssembleKvPair(requestParam.getProperties());
             if (CollectionUtils.isNotEmpty(propertiesList)) {
             if (CollectionUtils.isNotEmpty(propertiesList)) {
-                propertiesList.forEach(alterCollRequestBuilder::addProperties);
+                propertiesList.forEach(builder::addProperties);
+            }
+            if (StringUtils.isNotEmpty(requestParam.getDatabaseName())) {
+                builder.setDbName(requestParam.getDatabaseName());
             }
             }
 
 
-            AlterCollectionRequest alterCollectionRequest = alterCollRequestBuilder
+            AlterCollectionRequest alterCollectionRequest = builder
                     .setCollectionName(requestParam.getCollectionName())
                     .setCollectionName(requestParam.getCollectionName())
                     .build();
                     .build();
 
 
@@ -1309,6 +1311,42 @@ public abstract class AbstractMilvusGrpcClient implements MilvusClient {
         }
         }
     }
     }
 
 
+    @Override
+    public R<RpcStatus> alterIndex(AlterIndexParam requestParam) {
+        if (!clientIsReady()) {
+            return R.failed(new ClientNotConnectedException("Client rpc channel is not ready"));
+        }
+
+        logDebug(requestParam.toString());
+        String title = String.format("AlterIndexRequest indexName:%s", requestParam.getIndexName());
+
+        try {
+            AlterIndexRequest.Builder builder = AlterIndexRequest.newBuilder();
+            List<KeyValuePair> propertiesList = ParamUtils.AssembleKvPair(requestParam.getProperties());
+            if (CollectionUtils.isNotEmpty(propertiesList)) {
+                propertiesList.forEach(builder::addExtraParams);
+            }
+            if (StringUtils.isNotEmpty(requestParam.getDatabaseName())) {
+                builder.setDbName(requestParam.getDatabaseName());
+            }
+
+            AlterIndexRequest alterIndexRequest = builder
+                    .setCollectionName(requestParam.getCollectionName())
+                    .setIndexName(requestParam.getIndexName())
+                    .build();
+
+            Status response = blockingStub().alterIndex(alterIndexRequest);
+            handleResponse(title, response);
+            return R.success(new RpcStatus(RpcStatus.SUCCESS_MSG));
+        } catch (StatusRuntimeException e) {
+            logError("{} RPC failed! Exception:{}", title, e);
+            return R.failed(e);
+        } catch (Exception e) {
+            logError("{} failed! Exception:{}", title, e);
+            return R.failed(e);
+        }
+    }
+
     @Override
     @Override
     public R<MutationResult> delete(@NonNull DeleteParam requestParam) {
     public R<MutationResult> delete(@NonNull DeleteParam requestParam) {
         if (!clientIsReady()) {
         if (!clientIsReady()) {

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

@@ -201,8 +201,7 @@ public interface MilvusClient {
     R<ShowCollectionsResponse> showCollections(ShowCollectionsParam requestParam);
     R<ShowCollectionsResponse> showCollections(ShowCollectionsParam requestParam);
 
 
     /**
     /**
-     * Alter collection.
-     * Currently, only support setting collection TTL with key `collection.ttl.seconds`
+     * Alter collection with key-value properties.
      *
      *
      * @param requestParam {@link AlterCollectionParam}
      * @param requestParam {@link AlterCollectionParam}
      * @return {status:result code, data:RpcStatus{msg: result message}}
      * @return {status:result code, data:RpcStatus{msg: result message}}
@@ -351,6 +350,14 @@ public interface MilvusClient {
      */
      */
     R<GetIndexBuildProgressResponse> getIndexBuildProgress(GetIndexBuildProgressParam requestParam);
     R<GetIndexBuildProgressResponse> getIndexBuildProgress(GetIndexBuildProgressParam requestParam);
 
 
+    /**
+     * Alter index with key value properties.
+     *
+     * @param requestParam {@link AlterIndexParam}
+     * @return {status:result code, data:RpcStatus{msg: result message}}
+     */
+    R<RpcStatus> alterIndex(AlterIndexParam requestParam);
+
     /**
     /**
      * Inserts entities into a specified collection . Note that you don't need to
      * Inserts entities into a specified collection . Note that you don't need to
      * input primary key field if auto_id is enabled.
      * input primary key field if auto_id is enabled.

+ 5 - 0
src/main/java/io/milvus/client/MilvusMultiServiceClient.java

@@ -339,6 +339,11 @@ public class MilvusMultiServiceClient implements MilvusClient {
         return this.clusterFactory.getMaster().getClient().getIndexBuildProgress(requestParam);
         return this.clusterFactory.getMaster().getClient().getIndexBuildProgress(requestParam);
     }
     }
 
 
+    @Override
+    public R<RpcStatus> alterIndex(AlterIndexParam requestParam) {
+        return this.clusterFactory.getMaster().getClient().alterIndex(requestParam);
+    }
+
     @Override
     @Override
     public R<MutationResult> insert(InsertParam requestParam) {
     public R<MutationResult> insert(InsertParam requestParam) {
         List<R<MutationResult>> response = this.clusterFactory.getAvailableServerSettings().parallelStream()
         List<R<MutationResult>> response = this.clusterFactory.getAvailableServerSettings().parallelStream()

+ 1 - 0
src/main/java/io/milvus/param/Constant.java

@@ -49,6 +49,7 @@ public class Constant {
 
 
     // constant values for general
     // constant values for general
     public static final String TTL_SECONDS = "collection.ttl.seconds";
     public static final String TTL_SECONDS = "collection.ttl.seconds";
+    public static final String MMAP_ENABLED = "mmap.enabled";
 
 
     // max value for waiting loading collection/partition interval, unit: millisecond
     // max value for waiting loading collection/partition interval, unit: millisecond
     public static final Long MAX_WAITING_LOADING_INTERVAL = 2000L;
     public static final Long MAX_WAITING_LOADING_INTERVAL = 2000L;

+ 41 - 12
src/main/java/io/milvus/param/collection/AlterCollectionParam.java

@@ -21,16 +21,12 @@ package io.milvus.param.collection;
 
 
 import io.milvus.exception.ParamException;
 import io.milvus.exception.ParamException;
 import io.milvus.param.Constant;
 import io.milvus.param.Constant;
-import io.milvus.param.IndexType;
-import io.milvus.param.MetricType;
 import io.milvus.param.ParamUtils;
 import io.milvus.param.ParamUtils;
 import lombok.Getter;
 import lombok.Getter;
 import lombok.NonNull;
 import lombok.NonNull;
-import org.apache.commons.lang3.StringUtils;
 
 
 import java.util.HashMap;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map;
-import java.util.Objects;
 
 
 /**
 /**
  * Parameters for <code>alterCollection</code> interface.
  * Parameters for <code>alterCollection</code> interface.
@@ -38,13 +34,13 @@ import java.util.Objects;
 @Getter
 @Getter
 public class AlterCollectionParam {
 public class AlterCollectionParam {
     private final String collectionName;
     private final String collectionName;
+    private final String databaseName;
     private final Map<String, String> properties = new HashMap<>();
     private final Map<String, String> properties = new HashMap<>();
 
 
     private AlterCollectionParam(@NonNull Builder builder) {
     private AlterCollectionParam(@NonNull Builder builder) {
         this.collectionName = builder.collectionName;
         this.collectionName = builder.collectionName;
-        if (builder.ttlSeconds >= 0) {
-            this.properties.put(Constant.TTL_SECONDS, Integer.toString(builder.ttlSeconds));
-        }
+        this.databaseName = builder.databaseName;
+        this.properties.putAll(builder.properties);
     }
     }
 
 
     public static Builder newBuilder() {
     public static Builder newBuilder() {
@@ -56,11 +52,9 @@ public class AlterCollectionParam {
      */
      */
     public static final class Builder {
     public static final class Builder {
         private String collectionName;
         private String collectionName;
+        private String databaseName;
 
 
-        // The ttlSeconds value should be 0 or greater.
-        // In server side, the default value is 0, which means TTL is disabled.
-        // Here we set ttlSeconds = -1 to avoid being overwritten with unconscious
-        private Integer ttlSeconds = -1;
+        private final Map<String, String> properties = new HashMap<>();
 
 
 
 
         private Builder() {
         private Builder() {
@@ -77,9 +71,21 @@ public class AlterCollectionParam {
             return this;
             return this;
         }
         }
 
 
+        /**
+         * Sets the database name. database name can be nil.
+         *
+         * @param databaseName database name
+         * @return <code>Builder</code>
+         */
+        public Builder withDatabaseName(String databaseName) {
+            this.databaseName = databaseName;
+            return this;
+        }
+
         /**
         /**
          * Collection time to live (TTL) is the expiration time of data in a collection.
          * Collection time to live (TTL) is the expiration time of data in a collection.
          * Expired data in the collection will be cleaned up and will not be involved in searches or queries.
          * Expired data in the collection will be cleaned up and will not be involved in searches or queries.
+         * In server side, the default value is 0, which means TTL is disabled.
          * Specify TTL in the unit of seconds.
          * Specify TTL in the unit of seconds.
          *
          *
          * @param ttlSeconds TTL seconds, value should be 0 or greater
          * @param ttlSeconds TTL seconds, value should be 0 or greater
@@ -89,7 +95,29 @@ public class AlterCollectionParam {
             if (ttlSeconds < 0) {
             if (ttlSeconds < 0) {
                 throw new ParamException("The ttlSeconds value should be 0 or greater");
                 throw new ParamException("The ttlSeconds value should be 0 or greater");
             }
             }
-            this.ttlSeconds = ttlSeconds;
+
+            return this.withProperty(Constant.TTL_SECONDS, Integer.toString(ttlSeconds));
+        }
+
+        /**
+         * Enable MMap or not for index data files.
+         *
+         * @param enabledMMap enabled or not
+         * @return <code>Builder</code>
+         */
+        public Builder withMMapEnabled(boolean enabledMMap) {
+            return this.withProperty(Constant.MMAP_ENABLED, Boolean.toString(enabledMMap));
+        }
+
+        /**
+         * Basic method to set a key-value property.
+         *
+         * @param key the key
+         * @param value the value
+         * @return <code>Builder</code>
+         */
+        public Builder withProperty(@NonNull String key, @NonNull String value) {
+            this.properties.put(key, value);
             return this;
             return this;
         }
         }
 
 
@@ -114,6 +142,7 @@ public class AlterCollectionParam {
     public String toString() {
     public String toString() {
         return "AlterCollectionParam{" +
         return "AlterCollectionParam{" +
                 "collectionName='" + collectionName + '\'' +
                 "collectionName='" + collectionName + '\'' +
+                "dbName='" + databaseName + '\'' +
                 ", properties='" + properties.toString() + '\'' +
                 ", properties='" + properties.toString() + '\'' +
                 '}';
                 '}';
     }
     }

+ 15 - 0
src/main/java/io/milvus/param/collection/ReleaseCollectionParam.java

@@ -30,9 +30,11 @@ import lombok.NonNull;
  */
  */
 @Getter
 @Getter
 public class ReleaseCollectionParam {
 public class ReleaseCollectionParam {
+    private final String databaseName;
     private final String collectionName;
     private final String collectionName;
 
 
     private ReleaseCollectionParam(@NonNull Builder builder) {
     private ReleaseCollectionParam(@NonNull Builder builder) {
+        this.databaseName = builder.databaseName;
         this.collectionName = builder.collectionName;
         this.collectionName = builder.collectionName;
     }
     }
 
 
@@ -44,11 +46,23 @@ public class ReleaseCollectionParam {
      * Builder for {@link ReleaseCollectionParam} class.
      * Builder for {@link ReleaseCollectionParam} class.
      */
      */
     public static final class Builder {
     public static final class Builder {
+        private String databaseName;
         private String collectionName;
         private String collectionName;
 
 
         private Builder() {
         private Builder() {
         }
         }
 
 
+        /**
+         * Sets the database name. database name can be nil.
+         *
+         * @param databaseName database name
+         * @return <code>Builder</code>
+         */
+        public Builder withDatabaseName(String databaseName) {
+            this.databaseName = databaseName;
+            return this;
+        }
+
         /**
         /**
          * Sets the collection name. Collection name cannot be empty or null.
          * Sets the collection name. Collection name cannot be empty or null.
          *
          *
@@ -80,6 +94,7 @@ public class ReleaseCollectionParam {
     @Override
     @Override
     public String toString() {
     public String toString() {
         return "ReleaseCollectionParam{" +
         return "ReleaseCollectionParam{" +
+                "databaseName='" + databaseName + '\'' +
                 "collectionName='" + collectionName + '\'' + '}';
                 "collectionName='" + collectionName + '\'' + '}';
     }
     }
 }
 }

+ 148 - 0
src/main/java/io/milvus/param/index/AlterIndexParam.java

@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 io.milvus.param.index;
+
+import io.milvus.exception.ParamException;
+import io.milvus.param.Constant;
+import io.milvus.param.ParamUtils;
+import lombok.Getter;
+import lombok.NonNull;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Parameters for <code>alterCollection</code> interface.
+ */
+@Getter
+public class AlterIndexParam {
+    private final String collectionName;
+    private final String databaseName;
+    private final String indexName;
+    private final Map<String, String> properties = new HashMap<>();
+
+    private AlterIndexParam(@NonNull Builder builder) {
+        this.collectionName = builder.collectionName;
+        this.databaseName = builder.databaseName;
+        this.indexName = builder.indexName;
+        this.properties.putAll(builder.properties);
+    }
+
+    public static Builder newBuilder() {
+        return new Builder();
+    }
+
+    /**
+     * Builder for {@link AlterIndexParam} class.
+     */
+    public static final class Builder {
+        private String collectionName;
+        private String databaseName;
+        private String indexName;
+
+        private final Map<String, String> properties = new HashMap<>();
+
+
+        private Builder() {
+        }
+
+        /**
+         * Set the collection name. Collection name cannot be empty or null.
+         *
+         * @param collectionName collection name
+         * @return <code>Builder</code>
+         */
+        public Builder withCollectionName(@NonNull String collectionName) {
+            this.collectionName = collectionName;
+            return this;
+        }
+
+        /**
+         * Sets the database name. database name can be nil.
+         *
+         * @param databaseName database name
+         * @return <code>Builder</code>
+         */
+        public Builder withDatabaseName(String databaseName) {
+            this.databaseName = databaseName;
+            return this;
+        }
+
+        /**
+         * Set the index name. Index name cannot be empty or null.
+         *
+         * @param indexName index name
+         * @return <code>Builder</code>
+         */
+        public Builder withIndexName(@NonNull String indexName) {
+            this.indexName = indexName;
+            return this;
+        }
+
+        /**
+         * Enable MMap or not for index data files.
+         *
+         * @param enabledMMap enabled or not
+         * @return <code>Builder</code>
+         */
+        public Builder withMMapEnabled(boolean enabledMMap) {
+            return this.withProperty(Constant.MMAP_ENABLED, Boolean.toString(enabledMMap));
+        }
+
+        /**
+         * Basic method to set a key-value property.
+         *
+         * @param key the key
+         * @param value the value
+         * @return <code>Builder</code>
+         */
+        public Builder withProperty(@NonNull String key, @NonNull String value) {
+            this.properties.put(key, value);
+            return this;
+        }
+
+        /**
+         * Verifies parameters and creates a new {@link AlterIndexParam} instance.
+         *
+         * @return {@link AlterIndexParam}
+         */
+        public AlterIndexParam build() throws ParamException {
+            ParamUtils.CheckNullEmptyString(collectionName, "Collection name");
+            ParamUtils.CheckNullEmptyString(indexName, "Index name");
+
+            return new AlterIndexParam(this);
+        }
+    }
+
+    /**
+     * Constructs a <code>String</code> by {@link AlterIndexParam} instance.
+     *
+     * @return <code>String</code>
+     */
+    @Override
+    public String toString() {
+        return "AlterIndexParam{" +
+                "collectionName='" + collectionName + '\'' +
+                "dbName='" + databaseName + '\'' +
+                "indexName='" + indexName + '\'' +
+                ", properties='" + properties.toString() + '\'' +
+                '}';
+    }
+}