watcher.go 3.4 KB
Newer Older
S
sunby 已提交
1 2 3
package dataservice

import (
S
sunby 已提交
4
	"github.com/zilliztech/milvus-distributed/internal/log"
S
sunby 已提交
5 6
	"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
	"github.com/zilliztech/milvus-distributed/internal/proto/datapb"
紫晴 已提交
7
	"github.com/zilliztech/milvus-distributed/internal/util/trace"
S
sunby 已提交
8
	"go.uber.org/zap"
S
sunby 已提交
9

S
sunby 已提交
10 11 12 13 14
	"golang.org/x/net/context"

	"github.com/zilliztech/milvus-distributed/internal/msgstream"
)

N
neza2017 已提交
15 16 17 18 19 20 21 22 23 24
type proxyTimeTickWatcher struct {
	allocator segmentAllocatorInterface
	msgQueue  chan *msgstream.TimeTickMsg
}
type dataNodeTimeTickWatcher struct {
	meta      *meta
	cluster   *dataNodeCluster
	allocator segmentAllocatorInterface
	msgQueue  chan *msgstream.TimeTickMsg
}
S
sunby 已提交
25

26
func newProxyTimeTickWatcher(allocator segmentAllocatorInterface) *proxyTimeTickWatcher {
S
sunby 已提交
27
	return &proxyTimeTickWatcher{
S
sunby 已提交
28 29
		allocator: allocator,
		msgQueue:  make(chan *msgstream.TimeTickMsg, 1),
S
sunby 已提交
30 31 32
	}
}

S
sunby 已提交
33
func (watcher *proxyTimeTickWatcher) StartBackgroundLoop(ctx context.Context) {
S
sunby 已提交
34 35
	for {
		select {
S
sunby 已提交
36
		case <-ctx.Done():
S
sunby 已提交
37
			log.Debug("proxy time tick watcher closed")
S
sunby 已提交
38 39
			return
		case msg := <-watcher.msgQueue:
紫晴 已提交
40 41
			traceCtx := context.TODO()
			if err := watcher.allocator.ExpireAllocations(traceCtx, msg.Base.Timestamp); err != nil {
S
sunby 已提交
42
				log.Error("expire allocations error", zap.Error(err))
S
sunby 已提交
43 44 45 46 47
			}
		}
	}
}

S
sunby 已提交
48 49 50 51
func (watcher *proxyTimeTickWatcher) Watch(msg *msgstream.TimeTickMsg) {
	watcher.msgQueue <- msg
}

52
func newDataNodeTimeTickWatcher(meta *meta, allocator segmentAllocatorInterface, cluster *dataNodeCluster) *dataNodeTimeTickWatcher {
S
sunby 已提交
53
	return &dataNodeTimeTickWatcher{
S
sunby 已提交
54
		meta:      meta,
S
sunby 已提交
55
		allocator: allocator,
S
sunby 已提交
56
		cluster:   cluster,
S
sunby 已提交
57
		msgQueue:  make(chan *msgstream.TimeTickMsg, 1),
S
sunby 已提交
58 59 60 61 62 63 64
	}
}

func (watcher *dataNodeTimeTickWatcher) Watch(msg *msgstream.TimeTickMsg) {
	watcher.msgQueue <- msg
}

S
sunby 已提交
65
func (watcher *dataNodeTimeTickWatcher) StartBackgroundLoop(ctx context.Context) {
S
sunby 已提交
66 67
	for {
		select {
S
sunby 已提交
68
		case <-ctx.Done():
S
sunby 已提交
69
			log.Debug("data node time tick watcher closed")
S
sunby 已提交
70 71
			return
		case msg := <-watcher.msgQueue:
X
Xiangyu Wang 已提交
72
			if err := watcher.handleTimeTickMsg(msg); err != nil {
S
sunby 已提交
73
				log.Error("handle time tick error", zap.Error(err))
X
Xiangyu Wang 已提交
74 75 76 77 78 79 80
				continue
			}
		}
	}
}

func (watcher *dataNodeTimeTickWatcher) handleTimeTickMsg(msg *msgstream.TimeTickMsg) error {
紫晴 已提交
81 82 83 84
	ctx := context.TODO()
	sp, _ := trace.StartSpanFromContext(ctx)
	defer sp.Finish()
	segments, err := watcher.allocator.GetSealedSegments(ctx)
X
Xiangyu Wang 已提交
85 86 87 88
	if err != nil {
		return err
	}
	for _, id := range segments {
紫晴 已提交
89
		expired, err := watcher.allocator.IsAllocationsExpired(ctx, id, msg.Base.Timestamp)
X
Xiangyu Wang 已提交
90
		if err != nil {
S
sunby 已提交
91
			log.Error("check allocations expired error", zap.Int64("segmentID", id), zap.Error(err))
X
Xiangyu Wang 已提交
92 93 94 95
			continue
		}
		if expired {
			segmentInfo, err := watcher.meta.GetSegment(id)
S
sunby 已提交
96
			if err != nil {
S
sunby 已提交
97
				log.Error("get segment from meta error", zap.Int64("segmentID", id), zap.Error(err))
S
sunby 已提交
98 99
				continue
			}
T
ThreadDao 已提交
100
			if err = watcher.meta.SetSegmentState(id, commonpb.SegmentState_Sealed); err != nil {
S
sunby 已提交
101
				log.Error("set segment state error", zap.Int64("segmentID", id), zap.Error(err))
X
Xiangyu Wang 已提交
102
				continue
S
sunby 已提交
103
			}
G
godchen 已提交
104
			watcher.cluster.FlushSegment(&datapb.FlushSegmentsRequest{
X
Xiangyu Wang 已提交
105
				Base: &commonpb.MsgBase{
106
					MsgType:   commonpb.MsgType_Flush,
X
Xiangyu Wang 已提交
107 108 109 110 111 112 113
					MsgID:     -1, // todo add msg id
					Timestamp: 0,  // todo
					SourceID:  Params.NodeID,
				},
				CollectionID: segmentInfo.CollectionID,
				SegmentIDs:   []int64{segmentInfo.SegmentID},
			})
紫晴 已提交
114
			watcher.allocator.DropSegment(ctx, id)
S
sunby 已提交
115 116
		}
	}
X
Xiangyu Wang 已提交
117
	return nil
S
sunby 已提交
118
}