github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/client/partition.go (about) 1 // Copyright (C) 2019-2021 Zilliz. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance 4 // with the License. You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software distributed under the License 9 // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 10 // or implied. See the License for the specific language governing permissions and limitations under the License. 11 12 package client 13 14 import ( 15 "context" 16 "fmt" 17 "time" 18 19 "github.com/cockroachdb/errors" 20 21 "github.com/milvus-io/milvus-proto/go-api/v2/commonpb" 22 "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb" 23 "github.com/milvus-io/milvus-sdk-go/v2/entity" 24 ) 25 26 // CreatePartition create partition for collection 27 func (c *GrpcClient) CreatePartition(ctx context.Context, collName string, partitionName string, opts ...CreatePartitionOption) error { 28 if c.Service == nil { 29 return ErrClientNotReady 30 } 31 if err := c.checkCollectionExists(ctx, collName); err != nil { 32 return err 33 } 34 has, err := c.HasPartition(ctx, collName, partitionName) 35 if err != nil { 36 return err 37 } 38 if has { 39 return fmt.Errorf("partition %s of collection %s already exists", partitionName, collName) 40 } 41 42 req := &milvuspb.CreatePartitionRequest{ 43 DbName: "", //reserved 44 CollectionName: collName, 45 PartitionName: partitionName, 46 } 47 for _, opt := range opts { 48 opt(req) 49 } 50 resp, err := c.Service.CreatePartition(ctx, req) 51 if err != nil { 52 return err 53 } 54 return handleRespStatus(resp) 55 } 56 57 func (c *GrpcClient) checkPartitionExists(ctx context.Context, collName string, partitionName string) error { 58 has, err := c.HasPartition(ctx, collName, partitionName) 59 if err != nil { 60 return err 61 } 62 if !has { 63 return partNotExistsErr(collName, partitionName) 64 } 65 return nil 66 } 67 68 // DropPartition drop partition from collection 69 func (c *GrpcClient) DropPartition(ctx context.Context, collName string, partitionName string, opts ...DropPartitionOption) error { 70 if c.Service == nil { 71 return ErrClientNotReady 72 } 73 if err := c.checkCollectionExists(ctx, collName); err != nil { 74 return err 75 } 76 if err := c.checkPartitionExists(ctx, collName, partitionName); err != nil { 77 return err 78 } 79 req := &milvuspb.DropPartitionRequest{ 80 DbName: "", 81 CollectionName: collName, 82 PartitionName: partitionName, 83 } 84 for _, opt := range opts { 85 opt(req) 86 } 87 resp, err := c.Service.DropPartition(ctx, req) 88 if err != nil { 89 return err 90 } 91 return handleRespStatus(resp) 92 } 93 94 // HasPartition check whether specified partition exists 95 func (c *GrpcClient) HasPartition(ctx context.Context, collName string, partitionName string) (bool, error) { 96 if c.Service == nil { 97 return false, ErrClientNotReady 98 } 99 req := &milvuspb.HasPartitionRequest{ 100 DbName: "", // reserved 101 CollectionName: collName, 102 PartitionName: partitionName, 103 } 104 resp, err := c.Service.HasPartition(ctx, req) 105 if err != nil { 106 return false, err 107 } 108 if resp.GetStatus().GetErrorCode() != commonpb.ErrorCode_Success { 109 return false, errors.New("request failed") 110 } 111 return resp.GetValue(), nil 112 } 113 114 // ShowPartitions list all partitions from collection 115 func (c *GrpcClient) ShowPartitions(ctx context.Context, collName string) ([]*entity.Partition, error) { 116 if c.Service == nil { 117 return []*entity.Partition{}, ErrClientNotReady 118 } 119 req := &milvuspb.ShowPartitionsRequest{ 120 DbName: "", // reserved 121 CollectionName: collName, 122 } 123 resp, err := c.Service.ShowPartitions(ctx, req) 124 if err != nil { 125 return []*entity.Partition{}, err 126 } 127 if err := handleRespStatus(resp.GetStatus()); err != nil { 128 return []*entity.Partition{}, err 129 } 130 partitions := make([]*entity.Partition, 0, len(resp.GetPartitionIDs())) 131 if len(resp.GetPartitionNames()) == 0 { 132 return []*entity.Partition{}, errors.New("length of PartitionNames") 133 } 134 for idx, partitionID := range resp.GetPartitionIDs() { 135 partition := &entity.Partition{ID: partitionID, Name: resp.GetPartitionNames()[idx]} 136 if len(resp.GetInMemoryPercentages()) > idx { 137 partition.Loaded = resp.GetInMemoryPercentages()[idx] == 100 138 } 139 140 partitions = append(partitions, partition) 141 } 142 return partitions, nil 143 } 144 145 // LoadPartitions load collection paritions into memory 146 func (c *GrpcClient) LoadPartitions(ctx context.Context, collName string, partitionNames []string, async bool, opts ...LoadPartitionsOption) error { 147 if c.Service == nil { 148 return ErrClientNotReady 149 } 150 if err := c.checkCollectionExists(ctx, collName); err != nil { 151 return err 152 } 153 for _, partitionName := range partitionNames { 154 if err := c.checkPartitionExists(ctx, collName, partitionName); err != nil { 155 return err 156 } 157 } 158 159 req := &milvuspb.LoadPartitionsRequest{ 160 DbName: "", // reserved 161 CollectionName: collName, 162 PartitionNames: partitionNames, 163 } 164 for _, opt := range opts { 165 opt(req) 166 } 167 resp, err := c.Service.LoadPartitions(ctx, req) 168 if err != nil { 169 return err 170 } 171 if err := handleRespStatus(resp); err != nil { 172 return err 173 } 174 175 if !async { 176 ticker := time.NewTicker(200 * time.Millisecond) 177 defer ticker.Stop() 178 179 for { 180 select { 181 case <-ctx.Done(): 182 return ctx.Err() 183 case <-ticker.C: 184 progress, err := c.getLoadingProgress(ctx, collName, partitionNames...) 185 if err != nil { 186 return err 187 } 188 if progress == 100 { 189 return nil 190 } 191 } 192 } 193 } 194 195 return nil 196 } 197 198 // ReleasePartitions release partitions 199 func (c *GrpcClient) ReleasePartitions(ctx context.Context, collName string, partitionNames []string, opts ...ReleasePartitionsOption) error { 200 if c.Service == nil { 201 return ErrClientNotReady 202 } 203 if err := c.checkCollectionExists(ctx, collName); err != nil { 204 return err 205 } 206 for _, partitionName := range partitionNames { 207 if err := c.checkPartitionExists(ctx, collName, partitionName); err != nil { 208 return err 209 } 210 } 211 req := &milvuspb.ReleasePartitionsRequest{ 212 DbName: "", // reserved 213 CollectionName: collName, 214 PartitionNames: partitionNames, 215 } 216 for _, opt := range opts { 217 opt(req) 218 } 219 resp, err := c.Service.ReleasePartitions(ctx, req) 220 if err != nil { 221 return err 222 } 223 224 return handleRespStatus(resp) 225 }