github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/client/maintainance.go (about)

     1  // Licensed to the LF AI & Data foundation under one
     2  // or more contributor license agreements. See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership. The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License. You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package client
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
    24  	"github.com/milvus-io/milvus-sdk-go/v2/entity"
    25  )
    26  
    27  // ManualCompaction triggers a compaction on provided collection
    28  func (c *GrpcClient) ManualCompaction(ctx context.Context, collName string, _ time.Duration) (int64, error) {
    29  	if c.Service == nil {
    30  		return 0, ErrClientNotReady
    31  	}
    32  
    33  	if err := c.checkCollectionExists(ctx, collName); err != nil {
    34  		return 0, err
    35  	}
    36  	coll, err := c.DescribeCollection(ctx, collName)
    37  	if err != nil {
    38  		return 0, err
    39  	}
    40  
    41  	req := &milvuspb.ManualCompactionRequest{
    42  		CollectionID: coll.ID,
    43  	}
    44  
    45  	resp, err := c.Service.ManualCompaction(ctx, req)
    46  	if err != nil {
    47  		return 0, err
    48  	}
    49  
    50  	err = handleRespStatus(resp.GetStatus())
    51  	if err != nil {
    52  		return 0, err
    53  	}
    54  
    55  	return resp.GetCompactionID(), nil
    56  }
    57  
    58  // GetCompactionState get compaction state of provided compaction id
    59  func (c *GrpcClient) GetCompactionState(ctx context.Context, id int64) (entity.CompactionState, error) {
    60  	if c.Service == nil {
    61  		return entity.CompcationStateUndefined, ErrClientNotReady
    62  	}
    63  
    64  	req := &milvuspb.GetCompactionStateRequest{CompactionID: id}
    65  	resp, err := c.Service.GetCompactionState(ctx, req)
    66  	if err != nil {
    67  		return entity.CompcationStateUndefined, err
    68  	}
    69  
    70  	err = handleRespStatus(resp.GetStatus())
    71  	if err != nil {
    72  		return entity.CompcationStateUndefined, err
    73  	}
    74  
    75  	// direct mapping values of CompactionState
    76  	return entity.CompactionState(resp.GetState()), nil
    77  }
    78  
    79  // GetCompactionStateWithPlans get compaction state with plans of provided compaction id
    80  func (c *GrpcClient) GetCompactionStateWithPlans(ctx context.Context, id int64) (entity.CompactionState, []entity.CompactionPlan, error) {
    81  	if c.Service == nil {
    82  		return entity.CompcationStateUndefined, nil, ErrClientNotReady
    83  	}
    84  
    85  	req := &milvuspb.GetCompactionPlansRequest{
    86  		CompactionID: id,
    87  	}
    88  	resp, err := c.Service.GetCompactionStateWithPlans(ctx, req)
    89  	if err != nil {
    90  		return entity.CompcationStateUndefined, nil, err
    91  	}
    92  
    93  	err = handleRespStatus(resp.GetStatus())
    94  	if err != nil {
    95  		return entity.CompcationStateUndefined, nil, err
    96  	}
    97  
    98  	plans := make([]entity.CompactionPlan, 0, len(resp.GetMergeInfos()))
    99  	for _, mergeInfo := range resp.GetMergeInfos() {
   100  		plans = append(plans, entity.CompactionPlan{
   101  			Source:   mergeInfo.GetSources(),
   102  			Target:   mergeInfo.GetTarget(),
   103  			PlanType: entity.CompactionPlanMergeSegments,
   104  		})
   105  	}
   106  
   107  	return entity.CompactionState(resp.GetState()), plans, nil
   108  }