2021-10-18 10:38:35 +08:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
package datanode
|
|
|
|
|
|
|
|
import (
|
2021-11-15 17:19:10 +08:00
|
|
|
"context"
|
2021-10-19 11:04:34 +08:00
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
2021-10-18 10:38:35 +08:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/kv"
|
2021-10-19 11:04:34 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
2021-11-15 17:19:10 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2021-10-19 11:04:34 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/etcdpb"
|
2021-10-18 10:38:35 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
2021-10-19 11:04:34 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/storage"
|
2021-11-15 17:19:10 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/retry"
|
2021-12-01 10:11:39 +08:00
|
|
|
"go.uber.org/atomic"
|
2021-10-19 11:04:34 +08:00
|
|
|
"go.uber.org/zap"
|
2021-10-18 10:38:35 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// flushManager defines a flush manager signature
|
|
|
|
type flushManager interface {
|
|
|
|
// notify flush manager insert buffer data
|
2021-11-11 20:56:49 +08:00
|
|
|
flushBufferData(data *BufferData, segmentID UniqueID, flushed bool, dropped bool, pos *internalpb.MsgPosition) error
|
2021-10-18 10:38:35 +08:00
|
|
|
// notify flush manager del buffer data
|
2021-10-19 11:04:34 +08:00
|
|
|
flushDelData(data *DelDataBuf, segmentID UniqueID, pos *internalpb.MsgPosition) error
|
2021-10-28 19:16:31 +08:00
|
|
|
// injectFlush injects compaction or other blocking task before flush sync
|
2021-11-24 18:41:16 +08:00
|
|
|
injectFlush(injection *taskInjection, segments ...UniqueID)
|
2021-12-01 10:11:39 +08:00
|
|
|
// startDropping changes flush manager into dropping mode
|
|
|
|
startDropping()
|
|
|
|
// notifyAllFlushed tells flush manager there is not future incoming flush task for drop mode
|
|
|
|
notifyAllFlushed()
|
2021-11-17 10:39:15 +08:00
|
|
|
// close handles resource clean up
|
|
|
|
close()
|
2021-10-18 10:38:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// segmentFlushPack contains result to save into meta
|
|
|
|
type segmentFlushPack struct {
|
|
|
|
segmentID UniqueID
|
2021-12-19 20:00:42 +08:00
|
|
|
insertLogs map[UniqueID]*datapb.Binlog
|
|
|
|
statsLogs map[UniqueID]*datapb.Binlog
|
|
|
|
deltaLogs []*datapb.Binlog
|
2021-10-18 10:38:35 +08:00
|
|
|
pos *internalpb.MsgPosition
|
2021-10-19 11:04:34 +08:00
|
|
|
flushed bool
|
2021-11-11 20:56:49 +08:00
|
|
|
dropped bool
|
2021-11-15 17:19:10 +08:00
|
|
|
err error // task execution error, if not nil, notify func should stop datanode
|
2021-10-18 10:38:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// notifyMetaFunc notify meta to persistent flush result
|
2021-11-15 17:19:10 +08:00
|
|
|
type notifyMetaFunc func(*segmentFlushPack)
|
2021-10-18 10:38:35 +08:00
|
|
|
|
2021-12-01 10:11:39 +08:00
|
|
|
// flushAndDropFunc notifies meta to flush current state and drop virtual channel
|
|
|
|
type flushAndDropFunc func([]*segmentFlushPack)
|
|
|
|
|
2021-10-18 10:38:35 +08:00
|
|
|
// taskPostFunc clean up function after single flush task done
|
2021-10-25 20:17:34 +08:00
|
|
|
type taskPostFunc func(pack *segmentFlushPack, postInjection postInjectionFunc)
|
|
|
|
|
|
|
|
// postInjectionFunc post injection pack process logic
|
|
|
|
type postInjectionFunc func(pack *segmentFlushPack)
|
2021-10-18 10:38:35 +08:00
|
|
|
|
|
|
|
// make sure implementation
|
|
|
|
var _ flushManager = (*rendezvousFlushManager)(nil)
|
|
|
|
|
2021-12-01 10:11:39 +08:00
|
|
|
// orderFlushQueue keeps the order of task notifyFunc execution in order
|
2021-10-18 10:38:35 +08:00
|
|
|
type orderFlushQueue struct {
|
|
|
|
sync.Once
|
2021-10-19 11:04:34 +08:00
|
|
|
segmentID UniqueID
|
2021-11-24 18:41:16 +08:00
|
|
|
injectCh chan *taskInjection
|
2021-10-25 20:17:34 +08:00
|
|
|
|
2021-10-18 10:38:35 +08:00
|
|
|
// MsgID => flushTask
|
|
|
|
working sync.Map
|
|
|
|
notifyFunc notifyMetaFunc
|
|
|
|
|
|
|
|
tailMut sync.Mutex
|
|
|
|
tailCh chan struct{}
|
2021-10-25 20:17:34 +08:00
|
|
|
|
|
|
|
injectMut sync.Mutex
|
|
|
|
runningTasks int32
|
|
|
|
injectHandler *injectHandler
|
|
|
|
postInjection postInjectionFunc
|
2021-10-18 10:38:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// newOrderFlushQueue creates a orderFlushQueue
|
2021-10-19 11:04:34 +08:00
|
|
|
func newOrderFlushQueue(segID UniqueID, f notifyMetaFunc) *orderFlushQueue {
|
2021-10-25 20:17:34 +08:00
|
|
|
q := &orderFlushQueue{
|
2021-10-19 11:04:34 +08:00
|
|
|
segmentID: segID,
|
2021-10-18 10:38:35 +08:00
|
|
|
notifyFunc: f,
|
2021-11-24 18:41:16 +08:00
|
|
|
injectCh: make(chan *taskInjection, 100),
|
2021-10-18 10:38:35 +08:00
|
|
|
}
|
2021-10-25 20:17:34 +08:00
|
|
|
return q
|
2021-10-18 10:38:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// init orderFlushQueue use once protect init, init tailCh
|
|
|
|
func (q *orderFlushQueue) init() {
|
|
|
|
q.Once.Do(func() {
|
2021-11-17 10:39:15 +08:00
|
|
|
q.injectHandler = newInjectHandler(q)
|
2021-10-18 10:38:35 +08:00
|
|
|
// new queue acts like tailing task is done
|
|
|
|
q.tailCh = make(chan struct{})
|
|
|
|
close(q.tailCh)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *orderFlushQueue) getFlushTaskRunner(pos *internalpb.MsgPosition) *flushTaskRunner {
|
2021-10-25 20:17:34 +08:00
|
|
|
actual, loaded := q.working.LoadOrStore(string(pos.MsgID), newFlushTaskRunner(q.segmentID, q.injectCh))
|
2021-10-18 10:38:35 +08:00
|
|
|
t := actual.(*flushTaskRunner)
|
2021-12-01 10:11:39 +08:00
|
|
|
// not loaded means the task runner is new, do initializtion
|
2021-10-18 10:38:35 +08:00
|
|
|
if !loaded {
|
2021-12-01 10:11:39 +08:00
|
|
|
// take over injection if task queue is handling it
|
2021-10-25 20:17:34 +08:00
|
|
|
q.injectMut.Lock()
|
|
|
|
q.runningTasks++
|
|
|
|
if q.injectHandler != nil {
|
|
|
|
q.injectHandler.close()
|
|
|
|
q.injectHandler = nil
|
|
|
|
}
|
|
|
|
q.injectMut.Unlock()
|
2021-12-01 10:11:39 +08:00
|
|
|
// add task to tail
|
2021-10-18 10:38:35 +08:00
|
|
|
q.tailMut.Lock()
|
2021-10-25 20:17:34 +08:00
|
|
|
t.init(q.notifyFunc, q.postTask, q.tailCh)
|
2021-10-18 10:38:35 +08:00
|
|
|
q.tailCh = t.finishSignal
|
|
|
|
q.tailMut.Unlock()
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2021-12-01 10:11:39 +08:00
|
|
|
// postTask handles clean up work after a task is done
|
2021-10-25 20:17:34 +08:00
|
|
|
func (q *orderFlushQueue) postTask(pack *segmentFlushPack, postInjection postInjectionFunc) {
|
2021-12-01 10:11:39 +08:00
|
|
|
// delete task from working map
|
2021-10-25 20:17:34 +08:00
|
|
|
q.working.Delete(string(pack.pos.MsgID))
|
2021-12-01 10:11:39 +08:00
|
|
|
// after descreasing working count, check whether flush queue is empty
|
2021-10-25 20:17:34 +08:00
|
|
|
q.injectMut.Lock()
|
|
|
|
q.runningTasks--
|
2021-12-01 10:11:39 +08:00
|
|
|
// if flush queue is empty, let flush queue take over injection
|
2021-10-25 20:17:34 +08:00
|
|
|
if q.runningTasks == 0 {
|
|
|
|
q.injectHandler = newInjectHandler(q)
|
|
|
|
}
|
2021-12-01 10:11:39 +08:00
|
|
|
// set postInjection function if injection is handled in task
|
2021-10-25 20:17:34 +08:00
|
|
|
if postInjection != nil {
|
|
|
|
q.postInjection = postInjection
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.postInjection != nil {
|
|
|
|
q.postInjection(pack)
|
|
|
|
}
|
|
|
|
q.injectMut.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-10-18 10:38:35 +08:00
|
|
|
// enqueueInsertBuffer put insert buffer data into queue
|
2021-12-19 20:00:42 +08:00
|
|
|
func (q *orderFlushQueue) enqueueInsertFlush(task flushInsertTask, binlogs, statslogs map[UniqueID]*datapb.Binlog, flushed bool, dropped bool, pos *internalpb.MsgPosition) {
|
2021-11-11 20:56:49 +08:00
|
|
|
q.getFlushTaskRunner(pos).runFlushInsert(task, binlogs, statslogs, flushed, dropped, pos)
|
2021-10-18 10:38:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// enqueueDelBuffer put delete buffer data into queue
|
2021-10-19 11:04:34 +08:00
|
|
|
func (q *orderFlushQueue) enqueueDelFlush(task flushDeleteTask, deltaLogs *DelDataBuf, pos *internalpb.MsgPosition) {
|
|
|
|
q.getFlushTaskRunner(pos).runFlushDel(task, deltaLogs)
|
2021-10-18 10:38:35 +08:00
|
|
|
}
|
|
|
|
|
2021-10-25 20:17:34 +08:00
|
|
|
// inject performs injection for current task queue
|
|
|
|
// send into injectCh in there is running task
|
|
|
|
// or perform injection logic here if there is no injection
|
2021-11-24 18:41:16 +08:00
|
|
|
func (q *orderFlushQueue) inject(inject *taskInjection) {
|
2021-10-25 20:17:34 +08:00
|
|
|
q.injectCh <- inject
|
|
|
|
}
|
|
|
|
|
2021-12-01 10:11:39 +08:00
|
|
|
// injectionHandler handles injection for empty flush queue
|
2021-10-25 20:17:34 +08:00
|
|
|
type injectHandler struct {
|
|
|
|
once sync.Once
|
|
|
|
wg sync.WaitGroup
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
2021-12-01 10:11:39 +08:00
|
|
|
// newInjectHandler create injection handler for flush queue
|
2021-10-25 20:17:34 +08:00
|
|
|
func newInjectHandler(q *orderFlushQueue) *injectHandler {
|
|
|
|
h := &injectHandler{
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
h.wg.Add(1)
|
|
|
|
go h.handleInjection(q)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *injectHandler) handleInjection(q *orderFlushQueue) {
|
|
|
|
defer h.wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case inject := <-q.injectCh:
|
|
|
|
q.tailMut.Lock() //Maybe double check
|
|
|
|
injectDone := make(chan struct{})
|
|
|
|
q.tailCh = injectDone
|
|
|
|
q.tailMut.Unlock()
|
2021-11-24 18:41:16 +08:00
|
|
|
// notify one injection done
|
|
|
|
inject.injectOne()
|
|
|
|
ok := <-inject.injectOver
|
|
|
|
// apply injection
|
|
|
|
if ok {
|
|
|
|
q.postInjection = inject.postInjection
|
|
|
|
}
|
2021-10-25 20:17:34 +08:00
|
|
|
close(injectDone)
|
|
|
|
case <-h.done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *injectHandler) close() {
|
|
|
|
h.once.Do(func() {
|
|
|
|
close(h.done)
|
|
|
|
h.wg.Wait()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-01 10:11:39 +08:00
|
|
|
type dropHandler struct {
|
|
|
|
sync.Mutex
|
|
|
|
dropFlushWg sync.WaitGroup
|
|
|
|
flushAndDrop flushAndDropFunc
|
|
|
|
allFlushed chan struct{}
|
|
|
|
packs []*segmentFlushPack
|
|
|
|
}
|
|
|
|
|
2021-10-18 10:38:35 +08:00
|
|
|
// rendezvousFlushManager makes sure insert & del buf all flushed
|
|
|
|
type rendezvousFlushManager struct {
|
|
|
|
allocatorInterface
|
|
|
|
kv.BaseKV
|
2021-10-19 11:04:34 +08:00
|
|
|
Replica
|
2021-10-18 10:38:35 +08:00
|
|
|
|
|
|
|
// segment id => flush queue
|
|
|
|
dispatcher sync.Map
|
|
|
|
notifyFunc notifyMetaFunc
|
2021-12-01 10:11:39 +08:00
|
|
|
|
|
|
|
dropping atomic.Bool
|
|
|
|
dropHandler dropHandler
|
2021-10-18 10:38:35 +08:00
|
|
|
}
|
|
|
|
|
2021-12-01 10:11:39 +08:00
|
|
|
// getFlushQueue gets or creates a orderFlushQueue for segment id if not found
|
2021-10-18 10:38:35 +08:00
|
|
|
func (m *rendezvousFlushManager) getFlushQueue(segmentID UniqueID) *orderFlushQueue {
|
2021-11-17 10:39:15 +08:00
|
|
|
newQueue := newOrderFlushQueue(segmentID, m.notifyFunc)
|
|
|
|
actual, _ := m.dispatcher.LoadOrStore(segmentID, newQueue)
|
2021-10-18 10:38:35 +08:00
|
|
|
// all operation on dispatcher is private, assertion ok guaranteed
|
|
|
|
queue := actual.(*orderFlushQueue)
|
2021-11-17 10:39:15 +08:00
|
|
|
queue.init()
|
2021-10-18 10:38:35 +08:00
|
|
|
return queue
|
|
|
|
}
|
|
|
|
|
2021-12-19 20:00:42 +08:00
|
|
|
func (m *rendezvousFlushManager) handleInsertTask(segmentID UniqueID, task flushInsertTask, binlogs, statslogs map[UniqueID]*datapb.Binlog, flushed bool, dropped bool, pos *internalpb.MsgPosition) {
|
2021-12-01 10:11:39 +08:00
|
|
|
// in dropping mode
|
|
|
|
if m.dropping.Load() {
|
|
|
|
r := &flushTaskRunner{
|
|
|
|
WaitGroup: sync.WaitGroup{},
|
|
|
|
segmentID: segmentID,
|
|
|
|
}
|
|
|
|
r.WaitGroup.Add(1) // insert and delete are not bound in drop mode
|
|
|
|
r.runFlushInsert(task, binlogs, statslogs, flushed, dropped, pos)
|
|
|
|
r.WaitGroup.Wait()
|
|
|
|
|
|
|
|
m.dropHandler.Lock()
|
|
|
|
defer m.dropHandler.Unlock()
|
|
|
|
m.dropHandler.packs = append(m.dropHandler.packs, r.getFlushPack())
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// normal mode
|
|
|
|
m.getFlushQueue(segmentID).enqueueInsertFlush(task, binlogs, statslogs, flushed, dropped, pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *rendezvousFlushManager) handleDeleteTask(segmentID UniqueID, task flushDeleteTask, deltaLogs *DelDataBuf, pos *internalpb.MsgPosition) {
|
|
|
|
// in dropping mode
|
|
|
|
if m.dropping.Load() {
|
|
|
|
// preventing separate delete, check position exists in queue first
|
|
|
|
q := m.getFlushQueue(segmentID)
|
|
|
|
_, ok := q.working.Load(string(pos.MsgID))
|
|
|
|
// if ok, means position insert data already in queue, just handle task in normal mode
|
|
|
|
// if not ok, means the insert buf should be handle in drop mode
|
|
|
|
if !ok {
|
|
|
|
r := &flushTaskRunner{
|
|
|
|
WaitGroup: sync.WaitGroup{},
|
|
|
|
segmentID: segmentID,
|
|
|
|
}
|
|
|
|
r.WaitGroup.Add(1) // insert and delete are not bound in drop mode
|
|
|
|
r.runFlushDel(task, deltaLogs)
|
|
|
|
r.WaitGroup.Wait()
|
|
|
|
|
|
|
|
m.dropHandler.Lock()
|
|
|
|
defer m.dropHandler.Unlock()
|
|
|
|
m.dropHandler.packs = append(m.dropHandler.packs, r.getFlushPack())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// normal mode
|
|
|
|
m.getFlushQueue(segmentID).enqueueDelFlush(task, deltaLogs, pos)
|
|
|
|
}
|
|
|
|
|
2021-10-18 10:38:35 +08:00
|
|
|
// notify flush manager insert buffer data
|
2021-10-19 11:04:34 +08:00
|
|
|
func (m *rendezvousFlushManager) flushBufferData(data *BufferData, segmentID UniqueID, flushed bool,
|
2021-11-11 20:56:49 +08:00
|
|
|
dropped bool, pos *internalpb.MsgPosition) error {
|
2021-10-19 11:04:34 +08:00
|
|
|
|
|
|
|
// empty flush
|
|
|
|
if data == nil || data.buffer == nil {
|
2021-12-01 10:11:39 +08:00
|
|
|
//m.getFlushQueue(segmentID).enqueueInsertFlush(&flushBufferInsertTask{},
|
|
|
|
// map[UniqueID]string{}, map[UniqueID]string{}, flushed, dropped, pos)
|
2021-12-19 20:00:42 +08:00
|
|
|
m.handleInsertTask(segmentID, &flushBufferInsertTask{}, map[UniqueID]*datapb.Binlog{}, map[UniqueID]*datapb.Binlog{},
|
2021-12-01 10:11:39 +08:00
|
|
|
flushed, dropped, pos)
|
2021-10-19 11:04:34 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
collID, partID, meta, err := m.getSegmentMeta(segmentID, pos)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// encode data and convert output data
|
|
|
|
inCodec := storage.NewInsertCodec(meta)
|
|
|
|
|
|
|
|
binLogs, statsBinlogs, err := inCodec.Serialize(partID, segmentID, data.buffer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
start, _, err := m.allocIDBatch(uint32(len(binLogs)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-19 20:00:42 +08:00
|
|
|
field2Insert := make(map[UniqueID]*datapb.Binlog, len(binLogs))
|
2021-10-19 11:04:34 +08:00
|
|
|
kvs := make(map[string]string, len(binLogs))
|
|
|
|
field2Logidx := make(map[UniqueID]UniqueID, len(binLogs))
|
|
|
|
for idx, blob := range binLogs {
|
|
|
|
fieldID, err := strconv.ParseInt(blob.GetKey(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Flush failed ... cannot parse string to fieldID ..", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logidx := start + int64(idx)
|
|
|
|
|
|
|
|
// no error raise if alloc=false
|
2021-12-09 11:09:06 +08:00
|
|
|
k := JoinIDPath(collID, partID, segmentID, fieldID, logidx)
|
2021-10-19 11:04:34 +08:00
|
|
|
|
|
|
|
key := path.Join(Params.InsertBinlogRootPath, k)
|
|
|
|
kvs[key] = string(blob.Value[:])
|
2021-12-19 20:00:42 +08:00
|
|
|
field2Insert[fieldID] = &datapb.Binlog{
|
|
|
|
EntriesNum: data.size,
|
|
|
|
TimestampFrom: 0, //TODO
|
|
|
|
TimestampTo: 0, //TODO,
|
|
|
|
LogPath: key,
|
|
|
|
LogSize: int64(len(blob.Value)),
|
|
|
|
}
|
2021-10-19 11:04:34 +08:00
|
|
|
field2Logidx[fieldID] = logidx
|
|
|
|
}
|
|
|
|
|
2021-12-19 20:00:42 +08:00
|
|
|
field2Stats := make(map[UniqueID]*datapb.Binlog)
|
2021-10-19 11:04:34 +08:00
|
|
|
// write stats binlog
|
|
|
|
for _, blob := range statsBinlogs {
|
|
|
|
fieldID, err := strconv.ParseInt(blob.GetKey(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Flush failed ... cannot parse string to fieldID ..", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logidx := field2Logidx[fieldID]
|
|
|
|
|
|
|
|
// no error raise if alloc=false
|
2021-12-09 11:09:06 +08:00
|
|
|
k := JoinIDPath(collID, partID, segmentID, fieldID, logidx)
|
2021-10-19 11:04:34 +08:00
|
|
|
|
|
|
|
key := path.Join(Params.StatsBinlogRootPath, k)
|
2021-12-19 20:00:42 +08:00
|
|
|
kvs[key] = string(blob.Value)
|
|
|
|
field2Stats[fieldID] = &datapb.Binlog{
|
|
|
|
EntriesNum: 0,
|
|
|
|
TimestampFrom: 0, //TODO
|
|
|
|
TimestampTo: 0, //TODO,
|
|
|
|
LogPath: key,
|
|
|
|
LogSize: int64(len(blob.Value)),
|
|
|
|
}
|
2021-10-19 11:04:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m.updateSegmentCheckPoint(segmentID)
|
2021-12-01 10:11:39 +08:00
|
|
|
m.handleInsertTask(segmentID, &flushBufferInsertTask{
|
2021-10-19 11:04:34 +08:00
|
|
|
BaseKV: m.BaseKV,
|
|
|
|
data: kvs,
|
2021-11-11 20:56:49 +08:00
|
|
|
}, field2Insert, field2Stats, flushed, dropped, pos)
|
2021-10-19 11:04:34 +08:00
|
|
|
return nil
|
2021-10-18 10:38:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// notify flush manager del buffer data
|
|
|
|
func (m *rendezvousFlushManager) flushDelData(data *DelDataBuf, segmentID UniqueID,
|
2021-10-19 11:04:34 +08:00
|
|
|
pos *internalpb.MsgPosition) error {
|
|
|
|
|
|
|
|
// del signal with empty data
|
|
|
|
if data == nil || data.delData == nil {
|
2021-12-01 10:11:39 +08:00
|
|
|
m.handleDeleteTask(segmentID, &flushBufferDeleteTask{}, nil, pos)
|
2021-10-19 11:04:34 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-24 09:59:10 +08:00
|
|
|
collID, partID, err := m.getCollectionAndPartitionID(segmentID)
|
2021-10-19 11:04:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-24 09:59:10 +08:00
|
|
|
delCodec := storage.NewDeleteCodec()
|
2021-10-19 11:04:34 +08:00
|
|
|
|
2021-10-24 09:59:10 +08:00
|
|
|
blob, err := delCodec.Serialize(collID, partID, segmentID, data.delData)
|
2021-10-19 11:04:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logID, err := m.allocID()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to alloc ID", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-09 11:09:06 +08:00
|
|
|
blobKey := JoinIDPath(collID, partID, segmentID, logID)
|
2021-10-19 11:04:34 +08:00
|
|
|
blobPath := path.Join(Params.DeleteBinlogRootPath, blobKey)
|
|
|
|
kvs := map[string]string{blobPath: string(blob.Value[:])}
|
2021-12-19 20:00:42 +08:00
|
|
|
data.LogSize = int64(len(blob.Value))
|
|
|
|
data.LogPath = blobPath
|
2021-10-19 11:04:34 +08:00
|
|
|
log.Debug("delete blob path", zap.String("path", blobPath))
|
2021-12-01 10:11:39 +08:00
|
|
|
m.handleDeleteTask(segmentID, &flushBufferDeleteTask{
|
2021-10-19 11:04:34 +08:00
|
|
|
BaseKV: m.BaseKV,
|
|
|
|
data: kvs,
|
|
|
|
}, data, pos)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-25 20:17:34 +08:00
|
|
|
// injectFlush inject process before task finishes
|
2021-11-24 18:41:16 +08:00
|
|
|
func (m *rendezvousFlushManager) injectFlush(injection *taskInjection, segments ...UniqueID) {
|
|
|
|
go injection.waitForInjected()
|
2021-10-25 20:17:34 +08:00
|
|
|
for _, segmentID := range segments {
|
|
|
|
m.getFlushQueue(segmentID).inject(injection)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 11:04:34 +08:00
|
|
|
// fetch meta info for segment
|
|
|
|
func (m *rendezvousFlushManager) getSegmentMeta(segmentID UniqueID, pos *internalpb.MsgPosition) (UniqueID, UniqueID, *etcdpb.CollectionMeta, error) {
|
|
|
|
if !m.hasSegment(segmentID, true) {
|
2021-12-07 14:38:15 +08:00
|
|
|
return -1, -1, nil, fmt.Errorf("no such segment %d in the replica", segmentID)
|
2021-10-19 11:04:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetch meta information of segment
|
|
|
|
collID, partID, err := m.getCollectionAndPartitionID(segmentID)
|
|
|
|
if err != nil {
|
|
|
|
return -1, -1, nil, err
|
|
|
|
}
|
|
|
|
sch, err := m.getCollectionSchema(collID, pos.GetTimestamp())
|
|
|
|
if err != nil {
|
|
|
|
return -1, -1, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := &etcdpb.CollectionMeta{
|
|
|
|
ID: collID,
|
|
|
|
Schema: sch,
|
|
|
|
}
|
|
|
|
return collID, partID, meta, nil
|
2021-10-18 10:38:35 +08:00
|
|
|
}
|
|
|
|
|
2021-12-01 10:11:39 +08:00
|
|
|
// waitForAllTaskQueue waits for all flush queues in dispatcher become empty
|
|
|
|
func (m *rendezvousFlushManager) waitForAllFlushQueue() {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
m.dispatcher.Range(func(k, v interface{}) bool {
|
|
|
|
queue := v.(*orderFlushQueue)
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
<-queue.tailCh
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// startDropping changes flush manager into dropping mode
|
|
|
|
func (m *rendezvousFlushManager) startDropping() {
|
|
|
|
m.dropping.Store(true)
|
|
|
|
m.dropHandler.allFlushed = make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
<-m.dropHandler.allFlushed // all needed flush tasks are in flush manager now
|
|
|
|
m.waitForAllFlushQueue() // waits for all the normal flush queue done
|
|
|
|
m.dropHandler.dropFlushWg.Wait() // waits for all drop mode task done
|
|
|
|
m.dropHandler.Lock()
|
|
|
|
defer m.dropHandler.Unlock()
|
2021-12-02 16:39:33 +08:00
|
|
|
// apply injection if any
|
|
|
|
for _, pack := range m.dropHandler.packs {
|
|
|
|
q := m.getFlushQueue(pack.segmentID)
|
|
|
|
// queue will never be nil, sincde getFlushQueue will initialize one if not found
|
|
|
|
q.injectMut.Lock()
|
|
|
|
if q.postInjection != nil {
|
|
|
|
q.postInjection(pack)
|
|
|
|
}
|
|
|
|
q.injectMut.Unlock()
|
|
|
|
}
|
2021-12-01 10:11:39 +08:00
|
|
|
m.dropHandler.flushAndDrop(m.dropHandler.packs) // invoke drop & flush
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *rendezvousFlushManager) notifyAllFlushed() {
|
|
|
|
close(m.dropHandler.allFlushed)
|
|
|
|
}
|
|
|
|
|
2021-11-17 10:39:15 +08:00
|
|
|
// close cleans up all the left members
|
|
|
|
func (m *rendezvousFlushManager) close() {
|
|
|
|
m.dispatcher.Range(func(k, v interface{}) bool {
|
|
|
|
//assertion ok
|
|
|
|
queue := v.(*orderFlushQueue)
|
|
|
|
queue.injectMut.Lock()
|
|
|
|
if queue.injectHandler != nil {
|
|
|
|
queue.injectHandler.close()
|
|
|
|
}
|
|
|
|
queue.injectMut.Unlock()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-18 10:38:35 +08:00
|
|
|
type flushBufferInsertTask struct {
|
|
|
|
kv.BaseKV
|
2021-10-19 11:04:34 +08:00
|
|
|
data map[string]string
|
2021-10-18 10:38:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// flushInsertData implements flushInsertTask
|
|
|
|
func (t *flushBufferInsertTask) flushInsertData() error {
|
2021-10-19 11:04:34 +08:00
|
|
|
if t.BaseKV != nil && len(t.data) > 0 {
|
|
|
|
return t.MultiSave(t.data)
|
|
|
|
}
|
2021-10-18 10:38:35 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type flushBufferDeleteTask struct {
|
|
|
|
kv.BaseKV
|
2021-10-19 11:04:34 +08:00
|
|
|
data map[string]string
|
2021-10-18 10:38:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// flushDeleteData implements flushDeleteTask
|
|
|
|
func (t *flushBufferDeleteTask) flushDeleteData() error {
|
2021-10-19 11:04:34 +08:00
|
|
|
if len(t.data) > 0 && t.BaseKV != nil {
|
|
|
|
return t.MultiSave(t.data)
|
|
|
|
}
|
2021-10-18 10:38:35 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRendezvousFlushManager create rendezvousFlushManager with provided allocator and kv
|
2021-12-01 10:11:39 +08:00
|
|
|
func NewRendezvousFlushManager(allocator allocatorInterface, kv kv.BaseKV, replica Replica, f notifyMetaFunc, drop flushAndDropFunc) *rendezvousFlushManager {
|
|
|
|
fm := &rendezvousFlushManager{
|
2021-10-18 10:38:35 +08:00
|
|
|
allocatorInterface: allocator,
|
|
|
|
BaseKV: kv,
|
|
|
|
notifyFunc: f,
|
2021-10-19 11:04:34 +08:00
|
|
|
Replica: replica,
|
2021-12-01 10:11:39 +08:00
|
|
|
dropHandler: dropHandler{
|
|
|
|
flushAndDrop: drop,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// start with normal mode
|
|
|
|
fm.dropping.Store(false)
|
|
|
|
return fm
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFieldBinlogs(fieldID UniqueID, binlogs []*datapb.FieldBinlog) *datapb.FieldBinlog {
|
|
|
|
for _, binlog := range binlogs {
|
|
|
|
if fieldID == binlog.GetFieldID() {
|
|
|
|
return binlog
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dropVirtualChannelFunc(dsService *dataSyncService, opts ...retry.Option) flushAndDropFunc {
|
|
|
|
return func(packs []*segmentFlushPack) {
|
|
|
|
req := &datapb.DropVirtualChannelRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: 0, //TODO msg type
|
|
|
|
MsgID: 0, //TODO msg id
|
|
|
|
Timestamp: 0, //TODO time stamp
|
|
|
|
SourceID: Params.NodeID,
|
|
|
|
},
|
|
|
|
ChannelName: dsService.vchannelName,
|
|
|
|
}
|
|
|
|
|
|
|
|
segmentPack := make(map[UniqueID]*datapb.DropVirtualChannelSegment)
|
|
|
|
for _, pack := range packs {
|
|
|
|
segment, has := segmentPack[pack.segmentID]
|
|
|
|
if !has {
|
|
|
|
segment = &datapb.DropVirtualChannelSegment{
|
|
|
|
SegmentID: pack.segmentID,
|
|
|
|
CollectionID: dsService.collectionID,
|
|
|
|
}
|
|
|
|
|
|
|
|
segmentPack[pack.segmentID] = segment
|
|
|
|
}
|
|
|
|
for k, v := range pack.insertLogs {
|
|
|
|
fieldBinlogs := getFieldBinlogs(k, segment.Field2BinlogPaths)
|
|
|
|
if fieldBinlogs == nil {
|
|
|
|
segment.Field2BinlogPaths = append(segment.Field2BinlogPaths, &datapb.FieldBinlog{
|
|
|
|
FieldID: k,
|
2021-12-19 20:00:42 +08:00
|
|
|
Binlogs: []*datapb.Binlog{v},
|
2021-12-01 10:11:39 +08:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
fieldBinlogs.Binlogs = append(fieldBinlogs.Binlogs, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range pack.statsLogs {
|
|
|
|
fieldStatsLogs := getFieldBinlogs(k, segment.Field2StatslogPaths)
|
|
|
|
if fieldStatsLogs == nil {
|
|
|
|
segment.Field2StatslogPaths = append(segment.Field2StatslogPaths, &datapb.FieldBinlog{
|
|
|
|
FieldID: k,
|
2021-12-19 20:00:42 +08:00
|
|
|
Binlogs: []*datapb.Binlog{v},
|
2021-12-01 10:11:39 +08:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
fieldStatsLogs.Binlogs = append(fieldStatsLogs.Binlogs, v)
|
|
|
|
}
|
|
|
|
}
|
2021-12-19 20:00:42 +08:00
|
|
|
segment.Deltalogs = append(segment.Deltalogs, &datapb.FieldBinlog{
|
|
|
|
Binlogs: pack.deltaLogs,
|
|
|
|
})
|
2021-12-01 10:11:39 +08:00
|
|
|
updates, _ := dsService.replica.getSegmentStatisticsUpdates(pack.segmentID)
|
|
|
|
segment.NumOfRows = updates.GetNumRows()
|
|
|
|
if pack.pos != nil {
|
|
|
|
if segment.CheckPoint == nil || pack.pos.Timestamp > segment.CheckPoint.Timestamp {
|
|
|
|
segment.CheckPoint = pack.pos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start positions for all new segments
|
|
|
|
for _, pos := range dsService.replica.listNewSegmentsStartPositions() {
|
|
|
|
segment, has := segmentPack[pos.GetSegmentID()]
|
|
|
|
if !has {
|
|
|
|
segment = &datapb.DropVirtualChannelSegment{
|
|
|
|
SegmentID: pos.GetSegmentID(),
|
|
|
|
CollectionID: dsService.collectionID,
|
|
|
|
}
|
|
|
|
|
|
|
|
segmentPack[pos.GetSegmentID()] = segment
|
|
|
|
}
|
|
|
|
segment.StartPosition = pos.GetStartPosition()
|
|
|
|
}
|
|
|
|
|
|
|
|
err := retry.Do(context.Background(), func() error {
|
|
|
|
rsp, err := dsService.dataCoord.DropVirtualChannel(context.Background(), req)
|
|
|
|
// should be network issue, return error and retry
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO should retry only when datacoord status is unhealthy
|
|
|
|
if rsp.GetStatus().GetErrorCode() != commonpb.ErrorCode_Success {
|
|
|
|
return fmt.Errorf("data service DropVirtualChannel failed, reason = %s", rsp.GetStatus().GetReason())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, opts...)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("failed to DropVirtualChannel", zap.String("channel", dsService.vchannelName), zap.Error(err))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for segID := range segmentPack {
|
|
|
|
dsService.replica.segmentFlushed(segID)
|
|
|
|
dsService.flushingSegCache.Remove(segID)
|
|
|
|
}
|
2021-10-18 10:38:35 +08:00
|
|
|
}
|
|
|
|
}
|
2021-11-15 17:19:10 +08:00
|
|
|
|
|
|
|
func flushNotifyFunc(dsService *dataSyncService, opts ...retry.Option) notifyMetaFunc {
|
|
|
|
return func(pack *segmentFlushPack) {
|
|
|
|
if pack.err != nil {
|
2021-12-20 12:22:43 +08:00
|
|
|
log.Error("flush pack with error, DataNode quit now", zap.Error(pack.err))
|
2021-11-15 17:19:10 +08:00
|
|
|
// TODO silverxia change to graceful stop datanode
|
|
|
|
panic(pack.err)
|
|
|
|
}
|
|
|
|
fieldInsert := []*datapb.FieldBinlog{}
|
|
|
|
fieldStats := []*datapb.FieldBinlog{}
|
2021-12-19 20:00:42 +08:00
|
|
|
deltaInfos := []*datapb.FieldBinlog{}
|
2021-11-15 17:19:10 +08:00
|
|
|
checkPoints := []*datapb.CheckPoint{}
|
|
|
|
for k, v := range pack.insertLogs {
|
2021-12-19 20:00:42 +08:00
|
|
|
fieldInsert = append(fieldInsert, &datapb.FieldBinlog{FieldID: k, Binlogs: []*datapb.Binlog{v}})
|
2021-11-15 17:19:10 +08:00
|
|
|
}
|
|
|
|
for k, v := range pack.statsLogs {
|
2021-12-19 20:00:42 +08:00
|
|
|
fieldStats = append(fieldStats, &datapb.FieldBinlog{FieldID: k, Binlogs: []*datapb.Binlog{v}})
|
2021-11-15 17:19:10 +08:00
|
|
|
}
|
2021-12-19 20:00:42 +08:00
|
|
|
deltaInfos = append(deltaInfos, &datapb.FieldBinlog{Binlogs: pack.deltaLogs})
|
2021-11-15 17:19:10 +08:00
|
|
|
|
|
|
|
// only current segment checkpoint info,
|
|
|
|
updates, _ := dsService.replica.getSegmentStatisticsUpdates(pack.segmentID)
|
|
|
|
checkPoints = append(checkPoints, &datapb.CheckPoint{
|
|
|
|
SegmentID: pack.segmentID,
|
|
|
|
NumOfRows: updates.GetNumRows(),
|
|
|
|
Position: pack.pos,
|
|
|
|
})
|
|
|
|
|
2021-11-17 17:57:11 +08:00
|
|
|
startPos := dsService.replica.listNewSegmentsStartPositions()
|
|
|
|
|
2021-11-15 17:19:10 +08:00
|
|
|
log.Debug("SaveBinlogPath",
|
|
|
|
zap.Int64("SegmentID", pack.segmentID),
|
|
|
|
zap.Int64("CollectionID", dsService.collectionID),
|
2021-11-17 17:57:11 +08:00
|
|
|
zap.Any("startPos", startPos),
|
2021-11-15 17:19:10 +08:00
|
|
|
zap.Int("Length of Field2BinlogPaths", len(fieldInsert)),
|
|
|
|
zap.Int("Length of Field2Stats", len(fieldStats)),
|
|
|
|
zap.Int("Length of Field2Deltalogs", len(deltaInfos)),
|
2021-11-25 09:43:15 +08:00
|
|
|
zap.String("vChannelName", dsService.vchannelName),
|
2021-11-15 17:19:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
req := &datapb.SaveBinlogPathsRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: 0, //TODO msg type
|
|
|
|
MsgID: 0, //TODO msg id
|
|
|
|
Timestamp: 0, //TODO time stamp
|
|
|
|
SourceID: Params.NodeID,
|
|
|
|
},
|
|
|
|
SegmentID: pack.segmentID,
|
|
|
|
CollectionID: dsService.collectionID,
|
|
|
|
Field2BinlogPaths: fieldInsert,
|
|
|
|
Field2StatslogPaths: fieldStats,
|
|
|
|
Deltalogs: deltaInfos,
|
|
|
|
|
|
|
|
CheckPoints: checkPoints,
|
|
|
|
|
2021-11-17 17:57:11 +08:00
|
|
|
StartPositions: startPos,
|
2021-11-15 17:19:10 +08:00
|
|
|
Flushed: pack.flushed,
|
|
|
|
Dropped: pack.dropped,
|
|
|
|
}
|
|
|
|
err := retry.Do(context.Background(), func() error {
|
|
|
|
rsp, err := dsService.dataCoord.SaveBinlogPaths(context.Background(), req)
|
|
|
|
// should be network issue, return error and retry
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO should retry only when datacoord status is unhealthy
|
|
|
|
if rsp.ErrorCode != commonpb.ErrorCode_Success {
|
|
|
|
return fmt.Errorf("data service save bin log path failed, reason = %s", rsp.Reason)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, opts...)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("failed to SaveBinlogPaths", zap.Error(err))
|
|
|
|
// TODO change to graceful stop
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pack.flushed || pack.dropped {
|
|
|
|
dsService.replica.segmentFlushed(pack.segmentID)
|
|
|
|
}
|
|
|
|
dsService.flushingSegCache.Remove(req.GetSegmentID())
|
|
|
|
}
|
|
|
|
}
|