handler.go 6.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Licensed to the LF AI & Data foundation 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.

17 18 19 20 21 22 23 24 25
package datacoord

import (
	"context"

	"github.com/milvus-io/milvus/internal/log"
	"github.com/milvus-io/milvus/internal/proto/commonpb"
	"github.com/milvus-io/milvus/internal/proto/datapb"
	"github.com/milvus-io/milvus/internal/proto/internalpb"
26
	"github.com/milvus-io/milvus/internal/util/funcutil"
G
godchen 已提交
27
	"go.uber.org/zap"
28 29
)

30
// Handler handles some channel method for ChannelManager
31
type Handler interface {
32
	// GetVChanPositions gets the information recovery needed of a channel
33 34
	GetVChanPositions(channel string, collectionID UniqueID, partitionID UniqueID) *datapb.VchannelInfo
	CheckShouldDropChannel(channel string) bool
35
	FinishDropChannel(channel string)
36 37
}

38
// ServerHandler is a helper of Server
39 40 41 42
type ServerHandler struct {
	s *Server
}

43
// newServerHandler creates a new ServerHandler
44 45 46 47
func newServerHandler(s *Server) *ServerHandler {
	return &ServerHandler{s: s}
}

48
// GetVChanPositions gets vchannel latest postitions with provided dml channel names
49
func (h *ServerHandler) GetVChanPositions(channel string, collectionID UniqueID, partitionID UniqueID) *datapb.VchannelInfo {
50 51 52 53
	// cannot use GetSegmentsByChannel since dropped segments are needed here
	segments := h.s.meta.SelectSegments(func(s *SegmentInfo) bool {
		return s.InsertChannel == channel
	})
X
Xiaofan 已提交
54
	log.Info("GetSegmentsByChannel",
55 56 57 58
		zap.Any("collectionID", collectionID),
		zap.Any("channel", channel),
		zap.Any("numOfSegments", len(segments)),
	)
59 60 61
	var flushedIds []int64
	var unflushedIds []int64
	var droppedIds []int64
62 63 64 65 66 67 68
	var seekPosition *internalpb.MsgPosition
	for _, s := range segments {
		if (partitionID > allPartitionID && s.PartitionID != partitionID) ||
			(s.GetStartPosition() == nil && s.GetDmlPosition() == nil) {
			continue
		}

69
		if s.GetState() == commonpb.SegmentState_Dropped {
70
			droppedIds = append(droppedIds, trimSegmentInfo(s.SegmentInfo).GetID())
71 72 73
			continue
		}

74
		if s.GetState() == commonpb.SegmentState_Flushing || s.GetState() == commonpb.SegmentState_Flushed {
75
			flushedIds = append(flushedIds, trimSegmentInfo(s.SegmentInfo).GetID())
76
		} else {
77
			unflushedIds = append(unflushedIds, s.SegmentInfo.GetID())
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		}

		var segmentPosition *internalpb.MsgPosition
		if s.GetDmlPosition() != nil {
			segmentPosition = s.GetDmlPosition()
		} else {
			segmentPosition = s.GetStartPosition()
		}

		if seekPosition == nil || segmentPosition.Timestamp < seekPosition.Timestamp {
			seekPosition = segmentPosition
		}
	}
	// use collection start position when segment position is not found
	if seekPosition == nil {
		collection := h.GetCollection(h.s.ctx, collectionID)
		if collection != nil {
			seekPosition = getCollectionStartPosition(channel, collection)
		}
	}

	return &datapb.VchannelInfo{
100 101 102 103 104 105
		CollectionID:        collectionID,
		ChannelName:         channel,
		SeekPosition:        seekPosition,
		FlushedSegmentIds:   flushedIds,
		UnflushedSegmentIds: unflushedIds,
		DroppedSegmentIds:   droppedIds,
106 107 108 109 110
	}
}

func getCollectionStartPosition(channel string, collectionInfo *datapb.CollectionInfo) *internalpb.MsgPosition {
	for _, sp := range collectionInfo.GetStartPositions() {
111
		if sp.GetKey() != funcutil.ToPhysicalChannel(channel) {
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
			continue
		}
		return &internalpb.MsgPosition{
			ChannelName: channel,
			MsgID:       sp.GetData(),
		}
	}
	return nil
}

// trimSegmentInfo returns a shallow copy of datapb.SegmentInfo and sets ALL binlog info to nil
func trimSegmentInfo(info *datapb.SegmentInfo) *datapb.SegmentInfo {
	return &datapb.SegmentInfo{
		ID:             info.ID,
		CollectionID:   info.CollectionID,
		PartitionID:    info.PartitionID,
		InsertChannel:  info.InsertChannel,
		NumOfRows:      info.NumOfRows,
		State:          info.State,
		MaxRowNum:      info.MaxRowNum,
		LastExpireTime: info.LastExpireTime,
		StartPosition:  info.StartPosition,
		DmlPosition:    info.DmlPosition,
	}
}

138
// GetCollection returns collection info with specified collection id
139 140 141 142 143 144 145 146 147 148 149 150 151
func (h *ServerHandler) GetCollection(ctx context.Context, collectionID UniqueID) *datapb.CollectionInfo {
	coll := h.s.meta.GetCollection(collectionID)
	if coll != nil {
		return coll
	}
	err := h.s.loadCollectionFromRootCoord(ctx, collectionID)
	if err != nil {
		log.Warn("failed to load collection from rootcoord", zap.Int64("collectionID", collectionID), zap.Error(err))
	}

	return h.s.meta.GetCollection(collectionID)
}

152
// CheckShouldDropChannel returns whether specified channel is marked to be removed
153
func (h *ServerHandler) CheckShouldDropChannel(channel string) bool {
154 155 156 157 158 159 160 161 162 163 164 165 166
	/*
		segments := h.s.meta.GetSegmentsByChannel(channel)
		for _, segment := range segments {
			if segment.GetStartPosition() != nil && // filter empty segment
				// FIXME: we filter compaction generated segments
				// because datanode may not know the segment due to the network lag or
				// datacoord crash when handling CompleteCompaction.
				// FIXME: cancel this limitation for #12265
				// need to change a unified DropAndFlush to solve the root problem
				//len(segment.CompactionFrom) == 0 &&
				segment.GetState() != commonpb.SegmentState_Dropped {
				return false
			}
167
		}
168
		return false*/
169
	return h.s.meta.catalog.IsChannelDropped(h.s.ctx, channel)
170 171
}

172 173
// FinishDropChannel cleans up the remove flag for channels
// this function is a wrapper of server.meta.FinishDropChannel
174
func (h *ServerHandler) FinishDropChannel(channel string) {
175
	h.s.meta.catalog.DropChannel(h.s.ctx, channel)
176
}