Skip to content

Commit

Permalink
enhance: enable AlterDatabase and DescribeDatabase API (#923)
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Liu <wei.liu@zilliz.com>
  • Loading branch information
weiliu1031 committed Jun 7, 2024
1 parent 9c84c0f commit cdd3757
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/main/java/io/milvus/client/AbstractMilvusGrpcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,59 @@ public R<RpcStatus> dropDatabase(DropDatabaseParam requestParam) {
}
}

@Override
public R<RpcStatus> alterDatabase(AlterDatabaseParam requestParam) {
if (!clientIsReady()) {
return R.failed(new ClientNotConnectedException("Client rpc channel is not ready"));
}

logDebug(requestParam.toString());
String title = String.format("AlterDatabaseRequest databaseName:%s", requestParam.getDatabaseName());

try {
List<KeyValuePair> propertiesList = ParamUtils.AssembleKvPair(requestParam.getProperties());
AlterDatabaseRequest alterDatabaseRequest = AlterDatabaseRequest.newBuilder()
.setDbName(requestParam.getDatabaseName())
.addAllProperties(propertiesList)
.build();

Status response = blockingStub().alterDatabase(alterDatabaseRequest);
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
public R<DescribeDatabaseResponse> describeDatabase(DescribeDatabaseParam requestParam) {
if (!clientIsReady()) {
return R.failed(new ClientNotConnectedException("Client rpc channel is not ready"));
}

logDebug(requestParam.toString());
String title = String.format("DescribeDatabaseRequest databaseName:%s", requestParam.getDatabaseName());
try {
DescribeDatabaseRequest describeDatabaseRequest = DescribeDatabaseRequest.newBuilder()
.setDbName(requestParam.getDatabaseName())
.build();

DescribeDatabaseResponse response = blockingStub().describeDatabase(describeDatabaseRequest);
handleResponse(title, response.getStatus());
return R.success(response);
} 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
public R<RpcStatus> createCollection(@NonNull CreateCollectionParam requestParam) {
if (!clientIsReady()) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/io/milvus/client/MilvusClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ default void close() {
*/
R<ListDatabasesResponse> listDatabases();

/**
* Alter database with key value pair
* @param requestParam {@link AlterDatabaseParam}
* @return {status:result code, data:RpcStatus{msg: result message}}
*/
R<RpcStatus> alterDatabase(AlterDatabaseParam requestParam);

/**
* show detail of database base, such as replica number and resource groups
* @param requestParam {@link DescribeDatabaseParam}
* @return {status:result code, data:DescribeDatabaseResponse{replica_number,resource_groups}}
*/
R<DescribeDatabaseResponse> describeDatabase(DescribeDatabaseParam requestParam);

/**
* Creates a collection in Milvus.
*
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/io/milvus/client/MilvusMultiServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ public R<ListDatabasesResponse> listDatabases() {
return handleResponse(response);
}

@Override
public R<RpcStatus> alterDatabase(AlterDatabaseParam requestParam) {
List<R<RpcStatus>> response = this.clusterFactory.getAvailableServerSettings().stream()
.map(serverSetting -> serverSetting.getClient().alterDatabase(requestParam))
.collect(Collectors.toList());
return handleResponse(response);
}

@Override
public R<DescribeDatabaseResponse> describeDatabase(DescribeDatabaseParam requestParam) {
List<R<DescribeDatabaseResponse>> response = this.clusterFactory.getAvailableServerSettings().stream()
.map(serverSetting -> serverSetting.getClient().describeDatabase(requestParam))
.collect(Collectors.toList());
return handleResponse(response);
}

@Override
public R<RpcStatus> createCollection(CreateCollectionParam requestParam) {
List<R<RpcStatus>> response = this.clusterFactory.getAvailableServerSettings().stream()
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/milvus/client/MilvusServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,16 @@ public R<ListDatabasesResponse> listDatabases() {
return retry(super::listDatabases);
}

@Override
public R<RpcStatus> alterDatabase(AlterDatabaseParam requestParam) {
return retry(()-> super.alterDatabase(requestParam));
}

@Override
public R<DescribeDatabaseResponse> describeDatabase(DescribeDatabaseParam requestParam) {
return retry(()-> super.describeDatabase(requestParam));
}

@Override
public R<RpcStatus> createCollection(CreateCollectionParam requestParam) {
return retry(()-> super.createCollection(requestParam));
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/milvus/param/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class Constant {
// constant values for general
public static final String TTL_SECONDS = "collection.ttl.seconds";
public static final String MMAP_ENABLED = "mmap.enabled";
public static final String DATABASE_REPLICA_NUMBER = "database.replica.number";
public static final String DATABASE_RESOURCE_GROUPS = "database.resource_groups";

// max value for waiting loading collection/partition interval, unit: millisecond
public static final Long MAX_WAITING_LOADING_INTERVAL = 2000L;
Expand Down
117 changes: 117 additions & 0 deletions src/main/java/io/milvus/param/collection/AlterDatabaseParam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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.collection;

import io.milvus.exception.ParamException;
import io.milvus.param.Constant;
import io.milvus.param.ParamUtils;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Parameters for <code>alterDatabase</code> interface.
*/
@Getter
@ToString
public class AlterDatabaseParam {
private final String databaseName;
private final Map<String, String> properties = new HashMap<>();

private AlterDatabaseParam(@NonNull Builder builder) {
this.databaseName = builder.databaseName;
this.properties.putAll(builder.properties);
}

public static Builder newBuilder() {
return new Builder();
}

/**
* Builder for {@link AlterDatabaseParam} class.
*/
public static final class Builder {
private String databaseName;

private final Map<String, String> properties = new HashMap<>();


private Builder() {
}

/**
* Sets the database name. database name can be nil.
*
* @param databaseName database name
* @return <code>Builder</code>
*/
public Builder withDatabaseName(@NonNull String databaseName) {
this.databaseName = databaseName;
return this;
}

/**
* Sets the replica number in database level, then if load collection doesn't have replica number, it will use this replica number.
* @param replicaNumber replica number
* @return <code>Builder</code>
*/
public Builder withReplicaNumber(int replicaNumber) {
return this.withProperty(Constant.DATABASE_REPLICA_NUMBER, Integer.toString(replicaNumber));
}

/**
* Sets the resource groups in database level, then if load collection doesn't have resource groups, it will use this resource groups.
* @param resourceGroups resource group names
* @return
*/
public Builder WithResourceGroups(@NonNull List<String> resourceGroups) {
return this.withProperty(Constant.DATABASE_RESOURCE_GROUPS, String.join(",", resourceGroups));

}

/**
* 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 AlterDatabaseParam} instance.
*
* @return {@link AlterDatabaseParam}
*/
public AlterDatabaseParam build() throws ParamException {
ParamUtils.CheckNullEmptyString(databaseName, "Database name");

return new AlterDatabaseParam(this);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.collection;

import io.milvus.exception.ParamException;
import io.milvus.param.ParamUtils;

import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;

/**
* Parameters for <code>describeDatabase</code> interface.
*/
@Getter
@ToString
public class DescribeDatabaseParam {
private final String databaseName;

private DescribeDatabaseParam(@NonNull Builder builder) {
this.databaseName = builder.databaseName;
}

public static Builder newBuilder() {
return new Builder();
}

/**
* Builder for {@link DescribeDatabaseParam} class.
*/
public static final class Builder {
private String databaseName;

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;
}

/**
* Verifies parameters and creates a new {@link DescribeDatabaseParam} instance.
*
* @return {@link DescribeDatabaseParam}
*/
public DescribeDatabaseParam build() throws ParamException {
ParamUtils.CheckNullEmptyString(databaseName, "Database name");

return new DescribeDatabaseParam(this);
}
}

}
Loading

0 comments on commit cdd3757

Please sign in to comment.