2021-12-28 15:43:51 +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.
|
|
|
|
|
2021-11-05 22:25:00 +08:00
|
|
|
package datacoord
|
|
|
|
|
|
|
|
import (
|
2022-08-31 12:10:57 +08:00
|
|
|
"context"
|
2021-11-05 22:25:00 +08:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-02-26 11:31:49 +08:00
|
|
|
"github.com/cockroachdb/errors"
|
2023-12-05 18:44:37 +08:00
|
|
|
"github.com/samber/lo"
|
2023-01-06 14:33:36 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-12-05 18:44:37 +08:00
|
|
|
"github.com/stretchr/testify/suite"
|
2023-09-26 09:57:25 +08:00
|
|
|
"google.golang.org/grpc"
|
2022-11-18 15:35:09 +08:00
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
2023-09-21 09:45:27 +08:00
|
|
|
mockkv "github.com/milvus-io/milvus/internal/kv/mocks"
|
2022-08-20 10:24:51 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/metastore/kv/datacoord"
|
2022-08-31 12:10:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/mocks"
|
2021-11-05 22:25:00 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/metautil"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
2023-09-21 09:45:27 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/tsoutil"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
2021-11-05 22:25:00 +08:00
|
|
|
)
|
|
|
|
|
2023-12-05 18:44:37 +08:00
|
|
|
func TestCompactionPlanHandlerSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(CompactionPlanHandlerSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type CompactionPlanHandlerSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
mockMeta *MockCompactionMeta
|
|
|
|
mockAlloc *NMockAllocator
|
|
|
|
mockSch *MockScheduler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CompactionPlanHandlerSuite) SetupTest() {
|
|
|
|
s.mockMeta = NewMockCompactionMeta(s.T())
|
|
|
|
s.mockAlloc = NewNMockAllocator(s.T())
|
|
|
|
s.mockSch = NewMockScheduler(s.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CompactionPlanHandlerSuite) TestRemoveTasksByChannel() {
|
|
|
|
s.mockSch.EXPECT().Finish(mock.Anything, mock.Anything).Return().Once()
|
|
|
|
handler := newCompactionPlanHandler(nil, nil, nil, nil)
|
|
|
|
handler.scheduler = s.mockSch
|
|
|
|
|
|
|
|
var ch string = "ch1"
|
|
|
|
handler.mu.Lock()
|
|
|
|
handler.plans[1] = &compactionTask{
|
|
|
|
plan: &datapb.CompactionPlan{PlanID: 19530},
|
|
|
|
dataNodeID: 1,
|
|
|
|
triggerInfo: &compactionSignal{channel: ch},
|
|
|
|
}
|
|
|
|
handler.mu.Unlock()
|
|
|
|
|
|
|
|
handler.removeTasksByChannel(ch)
|
|
|
|
|
|
|
|
handler.mu.Lock()
|
|
|
|
s.Equal(0, len(handler.plans))
|
|
|
|
handler.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CompactionPlanHandlerSuite) TestCheckResult() {
|
2023-12-11 17:52:37 +08:00
|
|
|
session := &SessionManagerImpl{
|
2023-12-05 18:44:37 +08:00
|
|
|
sessions: struct {
|
|
|
|
sync.RWMutex
|
|
|
|
data map[int64]*Session
|
|
|
|
}{
|
|
|
|
data: map[int64]*Session{
|
|
|
|
2: {client: &mockDataNodeClient{
|
|
|
|
compactionStateResp: &datapb.CompactionStateResponse{
|
|
|
|
Results: []*datapb.CompactionPlanResult{
|
|
|
|
{PlanID: 1, State: commonpb.CompactionState_Executing},
|
|
|
|
{PlanID: 3, State: commonpb.CompactionState_Completed, Segments: []*datapb.CompactionSegment{{PlanID: 3}}},
|
|
|
|
{PlanID: 4, State: commonpb.CompactionState_Executing},
|
|
|
|
{PlanID: 6, State: commonpb.CompactionState_Executing},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-12-22 12:00:43 +08:00
|
|
|
{
|
|
|
|
s.mockAlloc.EXPECT().allocTimestamp(mock.Anything).Return(0, errors.New("mock")).Once()
|
|
|
|
handler := newCompactionPlanHandler(session, nil, nil, s.mockAlloc)
|
|
|
|
handler.checkResult()
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
s.mockAlloc.EXPECT().allocTimestamp(mock.Anything).Return(19530, nil).Once()
|
|
|
|
handler := newCompactionPlanHandler(session, nil, nil, s.mockAlloc)
|
|
|
|
handler.checkResult()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CompactionPlanHandlerSuite) TestClean() {
|
|
|
|
startTime := tsoutil.ComposeTSByTime(time.Now(), 0)
|
|
|
|
cleanTime := tsoutil.ComposeTSByTime(time.Now().Add(-2*time.Hour), 0)
|
|
|
|
c := &compactionPlanHandler{
|
|
|
|
allocator: s.mockAlloc,
|
|
|
|
plans: map[int64]*compactionTask{
|
|
|
|
1: {
|
|
|
|
state: executing,
|
|
|
|
},
|
|
|
|
2: {
|
|
|
|
state: pipelining,
|
|
|
|
},
|
|
|
|
3: {
|
|
|
|
state: completed,
|
|
|
|
plan: &datapb.CompactionPlan{
|
|
|
|
StartTime: startTime,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
4: {
|
|
|
|
state: completed,
|
|
|
|
plan: &datapb.CompactionPlan{
|
|
|
|
StartTime: cleanTime,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Clean()
|
|
|
|
s.Len(c.plans, 3)
|
2023-12-05 18:44:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CompactionPlanHandlerSuite) TestHandleL0CompactionResults() {
|
|
|
|
channel := "Ch-1"
|
|
|
|
s.mockMeta.EXPECT().UpdateSegmentsInfo(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
|
|
|
|
Run(func(operators ...UpdateOperator) {
|
|
|
|
s.Equal(5, len(operators))
|
|
|
|
}).Return(nil).Once()
|
|
|
|
|
|
|
|
deltalogs := []*datapb.FieldBinlog{getFieldBinlogPaths(101, getDeltaLogPath("log3", 1))}
|
|
|
|
// 2 l0 segments, 3 sealed segments
|
|
|
|
plan := &datapb.CompactionPlan{
|
|
|
|
PlanID: 1,
|
|
|
|
SegmentBinlogs: []*datapb.CompactionSegmentBinlogs{
|
|
|
|
{
|
|
|
|
SegmentID: 100,
|
|
|
|
Deltalogs: deltalogs,
|
|
|
|
Level: datapb.SegmentLevel_L0,
|
|
|
|
InsertChannel: channel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SegmentID: 101,
|
|
|
|
Deltalogs: deltalogs,
|
|
|
|
Level: datapb.SegmentLevel_L0,
|
|
|
|
InsertChannel: channel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SegmentID: 200,
|
|
|
|
Level: datapb.SegmentLevel_L1,
|
|
|
|
InsertChannel: channel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SegmentID: 201,
|
|
|
|
Level: datapb.SegmentLevel_L1,
|
|
|
|
InsertChannel: channel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SegmentID: 202,
|
|
|
|
Level: datapb.SegmentLevel_L1,
|
|
|
|
InsertChannel: channel,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Type: datapb.CompactionType_Level0DeleteCompaction,
|
|
|
|
}
|
|
|
|
|
|
|
|
result := &datapb.CompactionPlanResult{
|
|
|
|
PlanID: plan.GetPlanID(),
|
|
|
|
State: commonpb.CompactionState_Completed,
|
|
|
|
Channel: channel,
|
|
|
|
Segments: []*datapb.CompactionSegment{
|
|
|
|
{
|
|
|
|
SegmentID: 200,
|
|
|
|
Deltalogs: deltalogs,
|
|
|
|
Channel: channel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SegmentID: 201,
|
|
|
|
Deltalogs: deltalogs,
|
|
|
|
Channel: channel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SegmentID: 202,
|
|
|
|
Deltalogs: deltalogs,
|
|
|
|
Channel: channel,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := newCompactionPlanHandler(nil, nil, s.mockMeta, s.mockAlloc)
|
|
|
|
err := handler.handleL0CompactionResult(plan, result)
|
|
|
|
s.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CompactionPlanHandlerSuite) TestRefreshL0Plan() {
|
|
|
|
channel := "Ch-1"
|
|
|
|
s.mockMeta.EXPECT().SelectSegments(mock.Anything).Return(
|
|
|
|
[]*SegmentInfo{
|
|
|
|
{SegmentInfo: &datapb.SegmentInfo{
|
|
|
|
ID: 200,
|
|
|
|
Level: datapb.SegmentLevel_L1,
|
|
|
|
InsertChannel: channel,
|
|
|
|
}},
|
|
|
|
{SegmentInfo: &datapb.SegmentInfo{
|
|
|
|
ID: 201,
|
|
|
|
Level: datapb.SegmentLevel_L1,
|
|
|
|
InsertChannel: channel,
|
|
|
|
}},
|
|
|
|
{SegmentInfo: &datapb.SegmentInfo{
|
|
|
|
ID: 202,
|
|
|
|
Level: datapb.SegmentLevel_L1,
|
|
|
|
InsertChannel: channel,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
deltalogs := []*datapb.FieldBinlog{getFieldBinlogPaths(101, getDeltaLogPath("log3", 1))}
|
|
|
|
// 2 l0 segments
|
|
|
|
plan := &datapb.CompactionPlan{
|
|
|
|
PlanID: 1,
|
|
|
|
SegmentBinlogs: []*datapb.CompactionSegmentBinlogs{
|
|
|
|
{
|
|
|
|
SegmentID: 100,
|
|
|
|
Deltalogs: deltalogs,
|
|
|
|
Level: datapb.SegmentLevel_L0,
|
|
|
|
InsertChannel: channel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SegmentID: 101,
|
|
|
|
Deltalogs: deltalogs,
|
|
|
|
Level: datapb.SegmentLevel_L0,
|
|
|
|
InsertChannel: channel,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Type: datapb.CompactionType_Level0DeleteCompaction,
|
|
|
|
}
|
|
|
|
|
|
|
|
task := &compactionTask{
|
|
|
|
triggerInfo: &compactionSignal{id: 19530, collectionID: 1, partitionID: 10},
|
|
|
|
state: executing,
|
|
|
|
plan: plan,
|
|
|
|
dataNodeID: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := newCompactionPlanHandler(nil, nil, s.mockMeta, s.mockAlloc)
|
|
|
|
handler.RefreshPlan(task)
|
|
|
|
|
|
|
|
s.Equal(5, len(task.plan.GetSegmentBinlogs()))
|
|
|
|
segIDs := lo.Map(task.plan.GetSegmentBinlogs(), func(b *datapb.CompactionSegmentBinlogs, _ int) int64 {
|
|
|
|
return b.GetSegmentID()
|
|
|
|
})
|
|
|
|
|
|
|
|
s.ElementsMatch([]int64{200, 201, 202, 100, 101}, segIDs)
|
|
|
|
}
|
|
|
|
|
2021-11-05 22:25:00 +08:00
|
|
|
func Test_compactionPlanHandler_execCompactionPlan(t *testing.T) {
|
|
|
|
type fields struct {
|
2023-02-07 19:02:31 +08:00
|
|
|
plans map[int64]*compactionTask
|
2023-12-11 17:52:37 +08:00
|
|
|
sessions SessionManager
|
2023-12-25 19:24:46 +08:00
|
|
|
chManager *ChannelManagerImpl
|
2023-02-07 19:02:31 +08:00
|
|
|
allocatorFactory func() allocator
|
2021-11-05 22:25:00 +08:00
|
|
|
}
|
|
|
|
type args struct {
|
2021-11-11 15:54:42 +08:00
|
|
|
signal *compactionSignal
|
|
|
|
plan *datapb.CompactionPlan
|
2021-11-05 22:25:00 +08:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"test exec compaction",
|
|
|
|
fields{
|
|
|
|
plans: map[int64]*compactionTask{},
|
2023-12-11 17:52:37 +08:00
|
|
|
sessions: &SessionManagerImpl{
|
2021-11-05 22:25:00 +08:00
|
|
|
sessions: struct {
|
|
|
|
sync.RWMutex
|
|
|
|
data map[int64]*Session
|
|
|
|
}{
|
|
|
|
data: map[int64]*Session{
|
2023-02-15 16:00:33 +08:00
|
|
|
1: {client: &mockDataNodeClient{ch: make(chan interface{}, 1)}},
|
2021-11-05 22:25:00 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-12-25 19:24:46 +08:00
|
|
|
chManager: &ChannelManagerImpl{
|
2021-11-05 22:25:00 +08:00
|
|
|
store: &ChannelStore{
|
|
|
|
channelsInfo: map[int64]*NodeChannelInfo{
|
2023-11-13 11:16:18 +08:00
|
|
|
1: {NodeID: 1, Channels: []RWChannel{&channelMeta{Name: "ch1"}}},
|
2021-11-05 22:25:00 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-02-07 19:02:31 +08:00
|
|
|
allocatorFactory: func() allocator { return newMockAllocator() },
|
2021-11-05 22:25:00 +08:00
|
|
|
},
|
|
|
|
args{
|
2021-11-11 15:54:42 +08:00
|
|
|
signal: &compactionSignal{id: 100},
|
|
|
|
plan: &datapb.CompactionPlan{PlanID: 1, Channel: "ch1", Type: datapb.CompactionType_MergeCompaction},
|
2021-11-05 22:25:00 +08:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
nil,
|
|
|
|
},
|
2022-08-26 14:22:55 +08:00
|
|
|
{
|
|
|
|
"test exec compaction failed",
|
|
|
|
fields{
|
|
|
|
plans: map[int64]*compactionTask{},
|
2023-12-25 19:24:46 +08:00
|
|
|
chManager: &ChannelManagerImpl{
|
2022-08-26 14:22:55 +08:00
|
|
|
store: &ChannelStore{
|
|
|
|
channelsInfo: map[int64]*NodeChannelInfo{
|
2023-11-13 11:16:18 +08:00
|
|
|
1: {NodeID: 1, Channels: []RWChannel{}},
|
|
|
|
bufferID: {NodeID: bufferID, Channels: []RWChannel{}},
|
2022-08-26 14:22:55 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-02-07 19:02:31 +08:00
|
|
|
allocatorFactory: func() allocator { return newMockAllocator() },
|
|
|
|
},
|
|
|
|
args{
|
|
|
|
signal: &compactionSignal{id: 100},
|
|
|
|
plan: &datapb.CompactionPlan{PlanID: 1, Channel: "ch1", Type: datapb.CompactionType_MergeCompaction},
|
|
|
|
},
|
|
|
|
true,
|
2023-11-07 03:18:18 +08:00
|
|
|
errChannelNotWatched,
|
2022-08-26 14:22:55 +08:00
|
|
|
},
|
2021-11-05 22:25:00 +08:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-12-05 18:44:37 +08:00
|
|
|
scheduler := NewCompactionScheduler()
|
2021-11-05 22:25:00 +08:00
|
|
|
c := &compactionPlanHandler{
|
2023-11-07 03:18:18 +08:00
|
|
|
plans: tt.fields.plans,
|
|
|
|
sessions: tt.fields.sessions,
|
|
|
|
chManager: tt.fields.chManager,
|
|
|
|
allocator: tt.fields.allocatorFactory(),
|
2023-12-05 18:44:37 +08:00
|
|
|
scheduler: scheduler,
|
2021-11-05 22:25:00 +08:00
|
|
|
}
|
2022-12-07 18:01:19 +08:00
|
|
|
Params.Save(Params.DataCoordCfg.CompactionCheckIntervalInSeconds.Key, "1")
|
2022-12-06 20:09:18 +08:00
|
|
|
c.start()
|
2021-11-11 15:54:42 +08:00
|
|
|
err := c.execCompactionPlan(tt.args.signal, tt.args.plan)
|
2023-11-13 11:16:18 +08:00
|
|
|
require.ErrorIs(t, tt.err, err)
|
2023-11-07 03:18:18 +08:00
|
|
|
|
|
|
|
task := c.getCompaction(tt.args.plan.PlanID)
|
|
|
|
if !tt.wantErr {
|
|
|
|
assert.Equal(t, tt.args.plan, task.plan)
|
|
|
|
assert.Equal(t, tt.args.signal, task.triggerInfo)
|
2023-12-05 18:44:37 +08:00
|
|
|
assert.Equal(t, 1, c.scheduler.GetTaskCount())
|
2023-11-07 03:18:18 +08:00
|
|
|
} else {
|
|
|
|
assert.Eventually(t,
|
|
|
|
func() bool {
|
2023-12-05 18:44:37 +08:00
|
|
|
scheduler.mu.RLock()
|
|
|
|
defer scheduler.mu.RUnlock()
|
|
|
|
return c.scheduler.GetTaskCount() == 0 && len(scheduler.parallelTasks[1]) == 0
|
2023-11-07 03:18:18 +08:00
|
|
|
},
|
|
|
|
5*time.Second, 100*time.Millisecond)
|
2021-11-05 22:25:00 +08:00
|
|
|
}
|
2022-12-06 20:09:18 +08:00
|
|
|
c.stop()
|
2021-11-05 22:25:00 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 15:50:52 +08:00
|
|
|
func Test_compactionPlanHandler_execWithParallels(t *testing.T) {
|
2023-09-26 09:57:25 +08:00
|
|
|
mockDataNode := &mocks.MockDataNodeClient{}
|
2023-11-07 03:18:18 +08:00
|
|
|
paramtable.Get().Save(Params.DataCoordCfg.CompactionCheckIntervalInSeconds.Key, "0.001")
|
2022-12-07 18:01:19 +08:00
|
|
|
defer paramtable.Get().Reset(Params.DataCoordCfg.CompactionCheckIntervalInSeconds.Key)
|
2022-08-23 15:50:52 +08:00
|
|
|
c := &compactionPlanHandler{
|
|
|
|
plans: map[int64]*compactionTask{},
|
2023-12-11 17:52:37 +08:00
|
|
|
sessions: &SessionManagerImpl{
|
2022-08-23 15:50:52 +08:00
|
|
|
sessions: struct {
|
|
|
|
sync.RWMutex
|
|
|
|
data map[int64]*Session
|
|
|
|
}{
|
|
|
|
data: map[int64]*Session{
|
2022-08-31 12:10:57 +08:00
|
|
|
1: {client: mockDataNode},
|
2022-08-23 15:50:52 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-12-25 19:24:46 +08:00
|
|
|
chManager: &ChannelManagerImpl{
|
2022-08-23 15:50:52 +08:00
|
|
|
store: &ChannelStore{
|
|
|
|
channelsInfo: map[int64]*NodeChannelInfo{
|
2023-11-13 11:16:18 +08:00
|
|
|
1: {NodeID: 1, Channels: []RWChannel{&channelMeta{Name: "ch1"}}},
|
2022-08-23 15:50:52 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-11-07 03:18:18 +08:00
|
|
|
allocator: newMockAllocator(),
|
2023-11-23 17:30:25 +08:00
|
|
|
scheduler: NewCompactionScheduler(),
|
2022-08-23 15:50:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
signal := &compactionSignal{id: 100}
|
|
|
|
plan1 := &datapb.CompactionPlan{PlanID: 1, Channel: "ch1", Type: datapb.CompactionType_MergeCompaction}
|
|
|
|
plan2 := &datapb.CompactionPlan{PlanID: 2, Channel: "ch1", Type: datapb.CompactionType_MergeCompaction}
|
|
|
|
plan3 := &datapb.CompactionPlan{PlanID: 3, Channel: "ch1", Type: datapb.CompactionType_MergeCompaction}
|
|
|
|
|
2022-08-31 12:10:57 +08:00
|
|
|
var mut sync.RWMutex
|
|
|
|
called := 0
|
|
|
|
|
2023-09-26 09:57:25 +08:00
|
|
|
mockDataNode.EXPECT().Compaction(mock.Anything, mock.Anything, mock.Anything).
|
|
|
|
Run(func(ctx context.Context, req *datapb.CompactionPlan, opts ...grpc.CallOption) {
|
|
|
|
mut.Lock()
|
|
|
|
defer mut.Unlock()
|
|
|
|
called++
|
2023-11-07 03:18:18 +08:00
|
|
|
}).Return(&commonpb.Status{ErrorCode: commonpb.ErrorCode_Success}, nil).Times(2)
|
|
|
|
|
|
|
|
err := c.execCompactionPlan(signal, plan1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = c.execCompactionPlan(signal, plan2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = c.execCompactionPlan(signal, plan3)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-05 18:44:37 +08:00
|
|
|
assert.Equal(t, 3, c.scheduler.GetTaskCount())
|
2023-11-07 03:18:18 +08:00
|
|
|
|
|
|
|
// parallel for the same node are 2
|
2023-12-05 18:44:37 +08:00
|
|
|
c.schedule()
|
|
|
|
c.schedule()
|
2022-08-23 15:50:52 +08:00
|
|
|
|
2022-08-31 12:10:57 +08:00
|
|
|
// wait for compaction called
|
|
|
|
assert.Eventually(t, func() bool {
|
|
|
|
mut.RLock()
|
|
|
|
defer mut.RUnlock()
|
2023-11-07 03:18:18 +08:00
|
|
|
return called == 2
|
|
|
|
}, 3*time.Second, time.Millisecond*100)
|
2022-08-23 15:50:52 +08:00
|
|
|
|
2023-12-05 18:44:37 +08:00
|
|
|
tasks := c.scheduler.Schedule()
|
2023-11-07 03:18:18 +08:00
|
|
|
assert.Equal(t, 0, len(tasks))
|
2022-08-23 15:50:52 +08:00
|
|
|
}
|
|
|
|
|
2022-11-18 15:35:09 +08:00
|
|
|
func getInsertLogPath(rootPath string, segmentID typeutil.UniqueID) string {
|
|
|
|
return metautil.BuildInsertLogPath(rootPath, 10, 100, segmentID, 1000, 10000)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStatsLogPath(rootPath string, segmentID typeutil.UniqueID) string {
|
|
|
|
return metautil.BuildStatsLogPath(rootPath, 10, 100, segmentID, 1000, 10000)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDeltaLogPath(rootPath string, segmentID typeutil.UniqueID) string {
|
|
|
|
return metautil.BuildDeltaLogPath(rootPath, 10, 100, segmentID, 10000)
|
|
|
|
}
|
|
|
|
|
2022-09-27 16:02:53 +08:00
|
|
|
func TestCompactionPlanHandler_handleMergeCompactionResult(t *testing.T) {
|
2023-09-26 09:57:25 +08:00
|
|
|
mockDataNode := &mocks.MockDataNodeClient{}
|
|
|
|
call := mockDataNode.EXPECT().SyncSegments(mock.Anything, mock.Anything, mock.Anything).
|
|
|
|
Run(func(ctx context.Context, req *datapb.SyncSegmentsRequest, opts ...grpc.CallOption) {}).
|
|
|
|
Return(&commonpb.Status{ErrorCode: commonpb.ErrorCode_Success}, nil)
|
2022-09-27 16:02:53 +08:00
|
|
|
|
|
|
|
dataNodeID := UniqueID(111)
|
|
|
|
|
|
|
|
seg1 := &datapb.SegmentInfo{
|
|
|
|
ID: 1,
|
2022-11-18 15:35:09 +08:00
|
|
|
Binlogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getInsertLogPath("log1", 1))},
|
|
|
|
Statslogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getStatsLogPath("log2", 1))},
|
|
|
|
Deltalogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getDeltaLogPath("log3", 1))},
|
2021-11-05 22:25:00 +08:00
|
|
|
}
|
2022-09-27 16:02:53 +08:00
|
|
|
|
|
|
|
seg2 := &datapb.SegmentInfo{
|
|
|
|
ID: 2,
|
2022-11-18 15:35:09 +08:00
|
|
|
Binlogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getInsertLogPath("log4", 2))},
|
|
|
|
Statslogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getStatsLogPath("log5", 2))},
|
|
|
|
Deltalogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getDeltaLogPath("log6", 2))},
|
2021-11-05 22:25:00 +08:00
|
|
|
}
|
2022-09-27 16:02:53 +08:00
|
|
|
|
|
|
|
plan := &datapb.CompactionPlan{
|
|
|
|
PlanID: 1,
|
|
|
|
SegmentBinlogs: []*datapb.CompactionSegmentBinlogs{
|
|
|
|
{
|
|
|
|
SegmentID: seg1.ID,
|
|
|
|
FieldBinlogs: seg1.GetBinlogs(),
|
|
|
|
Field2StatslogPaths: seg1.GetStatslogs(),
|
|
|
|
Deltalogs: seg1.GetDeltalogs(),
|
2021-11-05 22:25:00 +08:00
|
|
|
},
|
2022-09-27 16:02:53 +08:00
|
|
|
{
|
|
|
|
SegmentID: seg2.ID,
|
|
|
|
FieldBinlogs: seg2.GetBinlogs(),
|
|
|
|
Field2StatslogPaths: seg2.GetStatslogs(),
|
|
|
|
Deltalogs: seg2.GetDeltalogs(),
|
2021-11-05 22:25:00 +08:00
|
|
|
},
|
|
|
|
},
|
2022-09-27 16:02:53 +08:00
|
|
|
Type: datapb.CompactionType_MergeCompaction,
|
|
|
|
}
|
|
|
|
|
2023-12-11 17:52:37 +08:00
|
|
|
sessions := &SessionManagerImpl{
|
2022-09-27 16:02:53 +08:00
|
|
|
sessions: struct {
|
|
|
|
sync.RWMutex
|
|
|
|
data map[int64]*Session
|
|
|
|
}{
|
|
|
|
data: map[int64]*Session{
|
2023-09-21 09:45:27 +08:00
|
|
|
dataNodeID: {client: mockDataNode},
|
|
|
|
},
|
2021-11-05 22:25:00 +08:00
|
|
|
},
|
2022-09-27 16:02:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
task := &compactionTask{
|
|
|
|
triggerInfo: &compactionSignal{id: 1},
|
|
|
|
state: executing,
|
|
|
|
plan: plan,
|
|
|
|
dataNodeID: dataNodeID,
|
|
|
|
}
|
|
|
|
|
|
|
|
plans := map[int64]*compactionTask{1: task}
|
|
|
|
|
2023-07-07 19:38:26 +08:00
|
|
|
metakv := mockkv.NewMetaKv(t)
|
|
|
|
metakv.EXPECT().Save(mock.Anything, mock.Anything).Return(errors.New("failed")).Maybe()
|
|
|
|
metakv.EXPECT().MultiSave(mock.Anything).Return(errors.New("failed")).Maybe()
|
2023-07-11 19:38:28 +08:00
|
|
|
metakv.EXPECT().HasPrefix(mock.Anything).Return(false, nil).Maybe()
|
2022-10-26 16:49:31 +08:00
|
|
|
errMeta := &meta{
|
2023-07-07 19:38:26 +08:00
|
|
|
catalog: &datacoord.Catalog{MetaKv: metakv},
|
2022-10-26 16:49:31 +08:00
|
|
|
segments: &SegmentsInfo{
|
|
|
|
map[int64]*SegmentInfo{
|
|
|
|
seg1.ID: {SegmentInfo: seg1},
|
|
|
|
seg2.ID: {SegmentInfo: seg2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-09-27 16:02:53 +08:00
|
|
|
meta := &meta{
|
2023-01-06 14:33:36 +08:00
|
|
|
catalog: &datacoord.Catalog{MetaKv: NewMetaMemoryKV()},
|
2022-09-27 16:02:53 +08:00
|
|
|
segments: &SegmentsInfo{
|
|
|
|
map[int64]*SegmentInfo{
|
|
|
|
seg1.ID: {SegmentInfo: seg1},
|
|
|
|
seg2.ID: {SegmentInfo: seg2},
|
2021-11-11 15:54:42 +08:00
|
|
|
},
|
2021-11-05 22:25:00 +08:00
|
|
|
},
|
|
|
|
}
|
2022-06-20 21:56:12 +08:00
|
|
|
|
2022-09-27 16:02:53 +08:00
|
|
|
c := &compactionPlanHandler{
|
|
|
|
plans: plans,
|
|
|
|
sessions: sessions,
|
|
|
|
meta: meta,
|
2022-06-20 21:56:12 +08:00
|
|
|
}
|
2022-09-27 16:02:53 +08:00
|
|
|
|
2022-10-26 16:49:31 +08:00
|
|
|
c2 := &compactionPlanHandler{
|
|
|
|
plans: plans,
|
|
|
|
sessions: sessions,
|
|
|
|
meta: errMeta,
|
|
|
|
}
|
|
|
|
|
2023-11-14 15:56:19 +08:00
|
|
|
compactionResult := &datapb.CompactionPlanResult{
|
|
|
|
PlanID: 1,
|
|
|
|
Segments: []*datapb.CompactionSegment{
|
|
|
|
{
|
|
|
|
SegmentID: 3,
|
|
|
|
NumOfRows: 15,
|
|
|
|
InsertLogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getInsertLogPath("log301", 3))},
|
|
|
|
Field2StatslogPaths: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getStatsLogPath("log302", 3))},
|
|
|
|
Deltalogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getDeltaLogPath("log303", 3))},
|
|
|
|
},
|
|
|
|
},
|
2022-06-20 21:56:12 +08:00
|
|
|
}
|
2022-09-27 16:02:53 +08:00
|
|
|
|
2023-11-14 15:56:19 +08:00
|
|
|
compactionResult2 := &datapb.CompactionPlanResult{
|
|
|
|
PlanID: 1,
|
|
|
|
Segments: []*datapb.CompactionSegment{
|
|
|
|
{
|
|
|
|
SegmentID: 3,
|
|
|
|
NumOfRows: 0,
|
|
|
|
InsertLogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getInsertLogPath("log301", 3))},
|
|
|
|
Field2StatslogPaths: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getStatsLogPath("log302", 3))},
|
|
|
|
Deltalogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getDeltaLogPath("log303", 3))},
|
|
|
|
},
|
|
|
|
},
|
2022-10-26 16:49:31 +08:00
|
|
|
}
|
|
|
|
|
2023-12-05 18:44:37 +08:00
|
|
|
has, err := meta.HasSegments([]UniqueID{1, 2})
|
2022-09-27 16:02:53 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, has)
|
|
|
|
|
2023-12-05 18:44:37 +08:00
|
|
|
has, err = meta.HasSegments([]UniqueID{3})
|
2022-09-27 16:02:53 +08:00
|
|
|
require.Error(t, err)
|
|
|
|
require.False(t, has)
|
|
|
|
|
|
|
|
err = c.handleMergeCompactionResult(plan, compactionResult)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-10-26 16:49:31 +08:00
|
|
|
err = c.handleMergeCompactionResult(plan, compactionResult2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = c2.handleMergeCompactionResult(plan, compactionResult2)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2023-12-05 18:44:37 +08:00
|
|
|
has, err = meta.HasSegments([]UniqueID{1, 2, 3})
|
2022-09-27 16:02:53 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, has)
|
2023-03-17 17:27:56 +08:00
|
|
|
|
|
|
|
call.Unset()
|
2023-09-26 09:57:25 +08:00
|
|
|
mockDataNode.EXPECT().SyncSegments(mock.Anything, mock.Anything, mock.Anything).
|
|
|
|
Return(&commonpb.Status{ErrorCode: commonpb.ErrorCode_UnexpectedError}, nil)
|
2023-03-17 17:27:56 +08:00
|
|
|
err = c.handleMergeCompactionResult(plan, compactionResult2)
|
|
|
|
assert.Error(t, err)
|
2022-09-27 16:02:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompactionPlanHandler_completeCompaction(t *testing.T) {
|
|
|
|
t.Run("test not exists compaction task", func(t *testing.T) {
|
|
|
|
c := &compactionPlanHandler{
|
|
|
|
plans: map[int64]*compactionTask{1: {}},
|
|
|
|
}
|
2023-11-14 15:56:19 +08:00
|
|
|
err := c.completeCompaction(&datapb.CompactionPlanResult{PlanID: 2})
|
2022-09-27 16:02:53 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("test completed compaction task", func(t *testing.T) {
|
|
|
|
c := &compactionPlanHandler{
|
|
|
|
plans: map[int64]*compactionTask{1: {state: completed}},
|
|
|
|
}
|
2023-11-14 15:56:19 +08:00
|
|
|
err := c.completeCompaction(&datapb.CompactionPlanResult{PlanID: 1})
|
2022-09-27 16:02:53 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test complete merge compaction task", func(t *testing.T) {
|
2023-09-26 09:57:25 +08:00
|
|
|
mockDataNode := &mocks.MockDataNodeClient{}
|
|
|
|
mockDataNode.EXPECT().SyncSegments(mock.Anything, mock.Anything, mock.Anything).
|
|
|
|
Run(func(ctx context.Context, req *datapb.SyncSegmentsRequest, opts ...grpc.CallOption) {}).
|
|
|
|
Return(&commonpb.Status{ErrorCode: commonpb.ErrorCode_Success}, nil)
|
2022-09-27 16:02:53 +08:00
|
|
|
|
|
|
|
dataNodeID := UniqueID(111)
|
|
|
|
|
|
|
|
seg1 := &datapb.SegmentInfo{
|
|
|
|
ID: 1,
|
2022-11-18 15:35:09 +08:00
|
|
|
Binlogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getInsertLogPath("log1", 1))},
|
|
|
|
Statslogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getStatsLogPath("log2", 1))},
|
|
|
|
Deltalogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getDeltaLogPath("log3", 1))},
|
2022-09-27 16:02:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
seg2 := &datapb.SegmentInfo{
|
|
|
|
ID: 2,
|
2022-11-18 15:35:09 +08:00
|
|
|
Binlogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getInsertLogPath("log4", 2))},
|
|
|
|
Statslogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getStatsLogPath("log5", 2))},
|
|
|
|
Deltalogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getDeltaLogPath("log6", 2))},
|
2022-09-27 16:02:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
plan := &datapb.CompactionPlan{
|
|
|
|
PlanID: 1,
|
|
|
|
SegmentBinlogs: []*datapb.CompactionSegmentBinlogs{
|
|
|
|
{
|
|
|
|
SegmentID: seg1.ID,
|
|
|
|
FieldBinlogs: seg1.GetBinlogs(),
|
|
|
|
Field2StatslogPaths: seg1.GetStatslogs(),
|
|
|
|
Deltalogs: seg1.GetDeltalogs(),
|
2022-06-20 21:56:12 +08:00
|
|
|
},
|
2022-09-27 16:02:53 +08:00
|
|
|
{
|
|
|
|
SegmentID: seg2.ID,
|
|
|
|
FieldBinlogs: seg2.GetBinlogs(),
|
|
|
|
Field2StatslogPaths: seg2.GetStatslogs(),
|
|
|
|
Deltalogs: seg2.GetDeltalogs(),
|
2022-06-20 21:56:12 +08:00
|
|
|
},
|
|
|
|
},
|
2022-09-27 16:02:53 +08:00
|
|
|
Type: datapb.CompactionType_MergeCompaction,
|
|
|
|
}
|
|
|
|
|
2023-12-11 17:52:37 +08:00
|
|
|
sessions := &SessionManagerImpl{
|
2022-09-27 16:02:53 +08:00
|
|
|
sessions: struct {
|
|
|
|
sync.RWMutex
|
|
|
|
data map[int64]*Session
|
|
|
|
}{
|
|
|
|
data: map[int64]*Session{
|
2023-09-21 09:45:27 +08:00
|
|
|
dataNodeID: {client: mockDataNode},
|
|
|
|
},
|
2022-06-20 21:56:12 +08:00
|
|
|
},
|
2022-09-27 16:02:53 +08:00
|
|
|
}
|
2022-06-20 21:56:12 +08:00
|
|
|
|
2022-09-27 16:02:53 +08:00
|
|
|
task := &compactionTask{
|
|
|
|
triggerInfo: &compactionSignal{id: 1},
|
|
|
|
state: executing,
|
|
|
|
plan: plan,
|
|
|
|
dataNodeID: dataNodeID,
|
|
|
|
}
|
|
|
|
|
|
|
|
plans := map[int64]*compactionTask{1: task}
|
|
|
|
|
|
|
|
meta := &meta{
|
2023-01-06 14:33:36 +08:00
|
|
|
catalog: &datacoord.Catalog{MetaKv: NewMetaMemoryKV()},
|
2022-09-27 16:02:53 +08:00
|
|
|
segments: &SegmentsInfo{
|
|
|
|
map[int64]*SegmentInfo{
|
|
|
|
seg1.ID: {SegmentInfo: seg1},
|
|
|
|
seg2.ID: {SegmentInfo: seg2},
|
2022-06-20 21:56:12 +08:00
|
|
|
},
|
2022-09-27 16:02:53 +08:00
|
|
|
},
|
|
|
|
}
|
2023-11-14 15:56:19 +08:00
|
|
|
compactionResult := datapb.CompactionPlanResult{
|
|
|
|
PlanID: 1,
|
|
|
|
Segments: []*datapb.CompactionSegment{
|
|
|
|
{
|
|
|
|
SegmentID: 3,
|
|
|
|
NumOfRows: 15,
|
|
|
|
InsertLogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getInsertLogPath("log301", 3))},
|
|
|
|
Field2StatslogPaths: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getStatsLogPath("log302", 3))},
|
|
|
|
Deltalogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getDeltaLogPath("log303", 3))},
|
|
|
|
},
|
|
|
|
},
|
2022-09-27 16:02:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &compactionPlanHandler{
|
2023-11-07 03:18:18 +08:00
|
|
|
plans: plans,
|
|
|
|
sessions: sessions,
|
|
|
|
meta: meta,
|
2023-11-23 17:30:25 +08:00
|
|
|
scheduler: NewCompactionScheduler(),
|
2022-09-27 16:02:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err := c.completeCompaction(&compactionResult)
|
|
|
|
assert.NoError(t, err)
|
2023-12-22 12:00:43 +08:00
|
|
|
assert.Nil(t, compactionResult.GetSegments()[0].GetInsertLogs())
|
|
|
|
assert.Nil(t, compactionResult.GetSegments()[0].GetField2StatslogPaths())
|
|
|
|
assert.Nil(t, compactionResult.GetSegments()[0].GetDeltalogs())
|
2022-09-27 16:02:53 +08:00
|
|
|
})
|
2022-10-27 17:15:32 +08:00
|
|
|
|
|
|
|
t.Run("test empty result merge compaction task", func(t *testing.T) {
|
2023-09-26 09:57:25 +08:00
|
|
|
mockDataNode := &mocks.MockDataNodeClient{}
|
|
|
|
mockDataNode.EXPECT().SyncSegments(mock.Anything, mock.Anything, mock.Anything).
|
|
|
|
Run(func(ctx context.Context, req *datapb.SyncSegmentsRequest, opts ...grpc.CallOption) {}).
|
|
|
|
Return(&commonpb.Status{ErrorCode: commonpb.ErrorCode_Success}, nil)
|
2022-10-27 17:15:32 +08:00
|
|
|
|
|
|
|
dataNodeID := UniqueID(111)
|
|
|
|
|
|
|
|
seg1 := &datapb.SegmentInfo{
|
|
|
|
ID: 1,
|
2022-11-18 15:35:09 +08:00
|
|
|
Binlogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getInsertLogPath("log1", 1))},
|
|
|
|
Statslogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getStatsLogPath("log2", 1))},
|
|
|
|
Deltalogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getDeltaLogPath("log3", 1))},
|
2022-10-27 17:15:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
seg2 := &datapb.SegmentInfo{
|
|
|
|
ID: 2,
|
2022-11-18 15:35:09 +08:00
|
|
|
Binlogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getInsertLogPath("log4", 2))},
|
|
|
|
Statslogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getStatsLogPath("log5", 2))},
|
|
|
|
Deltalogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getDeltaLogPath("log6", 2))},
|
2022-10-27 17:15:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
plan := &datapb.CompactionPlan{
|
|
|
|
PlanID: 1,
|
|
|
|
SegmentBinlogs: []*datapb.CompactionSegmentBinlogs{
|
|
|
|
{
|
|
|
|
SegmentID: seg1.ID,
|
|
|
|
FieldBinlogs: seg1.GetBinlogs(),
|
|
|
|
Field2StatslogPaths: seg1.GetStatslogs(),
|
|
|
|
Deltalogs: seg1.GetDeltalogs(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SegmentID: seg2.ID,
|
|
|
|
FieldBinlogs: seg2.GetBinlogs(),
|
|
|
|
Field2StatslogPaths: seg2.GetStatslogs(),
|
|
|
|
Deltalogs: seg2.GetDeltalogs(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Type: datapb.CompactionType_MergeCompaction,
|
|
|
|
}
|
|
|
|
|
2023-12-11 17:52:37 +08:00
|
|
|
sessions := &SessionManagerImpl{
|
2022-10-27 17:15:32 +08:00
|
|
|
sessions: struct {
|
|
|
|
sync.RWMutex
|
|
|
|
data map[int64]*Session
|
|
|
|
}{
|
|
|
|
data: map[int64]*Session{
|
2023-09-21 09:45:27 +08:00
|
|
|
dataNodeID: {client: mockDataNode},
|
|
|
|
},
|
2022-10-27 17:15:32 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
task := &compactionTask{
|
|
|
|
triggerInfo: &compactionSignal{id: 1},
|
|
|
|
state: executing,
|
|
|
|
plan: plan,
|
|
|
|
dataNodeID: dataNodeID,
|
|
|
|
}
|
|
|
|
|
|
|
|
plans := map[int64]*compactionTask{1: task}
|
|
|
|
|
|
|
|
meta := &meta{
|
2023-01-06 14:33:36 +08:00
|
|
|
catalog: &datacoord.Catalog{MetaKv: NewMetaMemoryKV()},
|
2022-10-27 17:15:32 +08:00
|
|
|
segments: &SegmentsInfo{
|
|
|
|
map[int64]*SegmentInfo{
|
|
|
|
seg1.ID: {SegmentInfo: seg1},
|
|
|
|
seg2.ID: {SegmentInfo: seg2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-09-18 09:53:22 +08:00
|
|
|
meta.AddSegment(context.TODO(), NewSegmentInfo(seg1))
|
|
|
|
meta.AddSegment(context.TODO(), NewSegmentInfo(seg2))
|
2022-10-27 17:15:32 +08:00
|
|
|
|
|
|
|
segments := meta.GetAllSegmentsUnsafe()
|
|
|
|
assert.Equal(t, len(segments), 2)
|
2023-11-14 15:56:19 +08:00
|
|
|
compactionResult := datapb.CompactionPlanResult{
|
|
|
|
PlanID: 1,
|
|
|
|
Segments: []*datapb.CompactionSegment{
|
|
|
|
{
|
|
|
|
SegmentID: 3,
|
|
|
|
NumOfRows: 0,
|
|
|
|
InsertLogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getInsertLogPath("log301", 3))},
|
|
|
|
Field2StatslogPaths: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getStatsLogPath("log302", 3))},
|
|
|
|
Deltalogs: []*datapb.FieldBinlog{getFieldBinlogPaths(101, getDeltaLogPath("log303", 3))},
|
|
|
|
},
|
|
|
|
},
|
2022-10-27 17:15:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &compactionPlanHandler{
|
2023-11-07 03:18:18 +08:00
|
|
|
plans: plans,
|
|
|
|
sessions: sessions,
|
|
|
|
meta: meta,
|
2023-11-23 17:30:25 +08:00
|
|
|
scheduler: NewCompactionScheduler(),
|
2022-10-27 17:15:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err := c.completeCompaction(&compactionResult)
|
2023-07-21 14:21:05 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-27 17:15:32 +08:00
|
|
|
|
|
|
|
segments = meta.GetAllSegmentsUnsafe()
|
2023-03-03 14:13:49 +08:00
|
|
|
assert.Equal(t, len(segments), 3)
|
2022-10-27 17:15:32 +08:00
|
|
|
|
|
|
|
for _, segment := range segments {
|
|
|
|
assert.True(t, segment.State == commonpb.SegmentState_Dropped)
|
|
|
|
}
|
|
|
|
})
|
2021-11-05 22:25:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_compactionPlanHandler_getCompaction(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
plans map[int64]*compactionTask
|
2023-12-11 17:52:37 +08:00
|
|
|
sessions SessionManager
|
2021-11-05 22:25:00 +08:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
planID int64
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want *compactionTask
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"test get non existed task",
|
|
|
|
fields{plans: map[int64]*compactionTask{}},
|
|
|
|
args{planID: 1},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test get existed task",
|
|
|
|
fields{
|
|
|
|
plans: map[int64]*compactionTask{1: {
|
|
|
|
state: executing,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
args{planID: 1},
|
|
|
|
&compactionTask{
|
|
|
|
state: executing,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &compactionPlanHandler{
|
|
|
|
plans: tt.fields.plans,
|
|
|
|
sessions: tt.fields.sessions,
|
|
|
|
}
|
|
|
|
got := c.getCompaction(tt.args.planID)
|
|
|
|
assert.EqualValues(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 15:50:52 +08:00
|
|
|
func Test_compactionPlanHandler_updateCompaction(t *testing.T) {
|
2021-11-05 22:25:00 +08:00
|
|
|
type fields struct {
|
|
|
|
plans map[int64]*compactionTask
|
2023-12-11 17:52:37 +08:00
|
|
|
sessions SessionManager
|
2021-11-05 22:25:00 +08:00
|
|
|
meta *meta
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ts Timestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := time.Now()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr bool
|
2022-08-26 14:22:55 +08:00
|
|
|
timeout []int64
|
|
|
|
failed []int64
|
2021-11-05 22:25:00 +08:00
|
|
|
unexpired []int64
|
|
|
|
}{
|
|
|
|
{
|
2022-08-23 15:50:52 +08:00
|
|
|
"test update compaction task",
|
2021-11-05 22:25:00 +08:00
|
|
|
fields{
|
|
|
|
plans: map[int64]*compactionTask{
|
|
|
|
1: {
|
2022-08-23 15:50:52 +08:00
|
|
|
state: executing,
|
|
|
|
dataNodeID: 1,
|
2021-11-05 22:25:00 +08:00
|
|
|
plan: &datapb.CompactionPlan{
|
|
|
|
PlanID: 1,
|
|
|
|
StartTime: tsoutil.ComposeTS(ts.UnixNano()/int64(time.Millisecond), 0),
|
|
|
|
TimeoutInSeconds: 10,
|
|
|
|
SegmentBinlogs: []*datapb.CompactionSegmentBinlogs{
|
|
|
|
{SegmentID: 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
2: {
|
2022-08-23 15:50:52 +08:00
|
|
|
state: executing,
|
|
|
|
dataNodeID: 2,
|
2021-11-05 22:25:00 +08:00
|
|
|
plan: &datapb.CompactionPlan{
|
|
|
|
PlanID: 2,
|
|
|
|
StartTime: tsoutil.ComposeTS(ts.UnixNano()/int64(time.Millisecond), 0),
|
|
|
|
TimeoutInSeconds: 1,
|
|
|
|
},
|
|
|
|
},
|
2022-08-23 15:50:52 +08:00
|
|
|
3: {
|
2022-08-26 14:22:55 +08:00
|
|
|
state: executing,
|
2022-08-23 15:50:52 +08:00
|
|
|
dataNodeID: 2,
|
|
|
|
plan: &datapb.CompactionPlan{
|
|
|
|
PlanID: 3,
|
|
|
|
StartTime: tsoutil.ComposeTS(ts.UnixNano()/int64(time.Millisecond), 0),
|
|
|
|
TimeoutInSeconds: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
4: {
|
|
|
|
state: executing,
|
|
|
|
dataNodeID: 2,
|
|
|
|
plan: &datapb.CompactionPlan{
|
|
|
|
PlanID: 4,
|
|
|
|
StartTime: tsoutil.ComposeTS(ts.UnixNano()/int64(time.Millisecond), 0) - 200*1000,
|
|
|
|
TimeoutInSeconds: 1,
|
|
|
|
},
|
|
|
|
},
|
2023-08-30 11:12:27 +08:00
|
|
|
5: { // timeout and failed
|
|
|
|
state: timeout,
|
|
|
|
dataNodeID: 2,
|
|
|
|
plan: &datapb.CompactionPlan{
|
|
|
|
PlanID: 5,
|
|
|
|
StartTime: tsoutil.ComposeTS(ts.UnixNano()/int64(time.Millisecond), 0) - 200*1000,
|
|
|
|
TimeoutInSeconds: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
6: { // timeout and executing
|
|
|
|
state: timeout,
|
|
|
|
dataNodeID: 2,
|
|
|
|
plan: &datapb.CompactionPlan{
|
|
|
|
PlanID: 6,
|
|
|
|
StartTime: tsoutil.ComposeTS(ts.UnixNano()/int64(time.Millisecond), 0) - 200*1000,
|
|
|
|
TimeoutInSeconds: 1,
|
|
|
|
},
|
|
|
|
},
|
2021-11-05 22:25:00 +08:00
|
|
|
},
|
|
|
|
meta: &meta{
|
|
|
|
segments: &SegmentsInfo{
|
|
|
|
map[int64]*SegmentInfo{
|
|
|
|
1: {SegmentInfo: &datapb.SegmentInfo{ID: 1}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-12-11 17:52:37 +08:00
|
|
|
sessions: &SessionManagerImpl{
|
2022-08-23 15:50:52 +08:00
|
|
|
sessions: struct {
|
|
|
|
sync.RWMutex
|
|
|
|
data map[int64]*Session
|
|
|
|
}{
|
|
|
|
data: map[int64]*Session{
|
2023-08-30 11:12:27 +08:00
|
|
|
2: {client: &mockDataNodeClient{
|
2022-08-23 15:50:52 +08:00
|
|
|
compactionStateResp: &datapb.CompactionStateResponse{
|
2023-11-14 15:56:19 +08:00
|
|
|
Results: []*datapb.CompactionPlanResult{
|
2022-08-23 15:50:52 +08:00
|
|
|
{PlanID: 1, State: commonpb.CompactionState_Executing},
|
2023-11-14 15:56:19 +08:00
|
|
|
{PlanID: 3, State: commonpb.CompactionState_Completed, Segments: []*datapb.CompactionSegment{{PlanID: 3}}},
|
2022-08-26 14:22:55 +08:00
|
|
|
{PlanID: 4, State: commonpb.CompactionState_Executing},
|
2023-08-30 11:12:27 +08:00
|
|
|
{PlanID: 6, State: commonpb.CompactionState_Executing},
|
2022-08-23 15:50:52 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-11-05 22:25:00 +08:00
|
|
|
},
|
|
|
|
args{ts: tsoutil.ComposeTS(ts.Add(5*time.Second).UnixNano()/int64(time.Millisecond), 0)},
|
|
|
|
false,
|
2023-08-30 11:12:27 +08:00
|
|
|
[]int64{4, 6},
|
|
|
|
[]int64{2, 5},
|
2022-08-23 15:50:52 +08:00
|
|
|
[]int64{1, 3},
|
2021-11-05 22:25:00 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-12-05 18:44:37 +08:00
|
|
|
scheduler := NewCompactionScheduler()
|
2021-11-05 22:25:00 +08:00
|
|
|
c := &compactionPlanHandler{
|
2023-11-07 03:18:18 +08:00
|
|
|
plans: tt.fields.plans,
|
|
|
|
sessions: tt.fields.sessions,
|
|
|
|
meta: tt.fields.meta,
|
2023-12-05 18:44:37 +08:00
|
|
|
scheduler: scheduler,
|
2021-11-05 22:25:00 +08:00
|
|
|
}
|
|
|
|
|
2022-08-23 15:50:52 +08:00
|
|
|
err := c.updateCompaction(tt.args.ts)
|
2021-11-05 22:25:00 +08:00
|
|
|
assert.Equal(t, tt.wantErr, err != nil)
|
|
|
|
|
2022-08-26 14:22:55 +08:00
|
|
|
for _, id := range tt.timeout {
|
|
|
|
task := c.getCompaction(id)
|
|
|
|
assert.Equal(t, timeout, task.state)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range tt.failed {
|
2021-11-05 22:25:00 +08:00
|
|
|
task := c.getCompaction(id)
|
2022-08-23 15:50:52 +08:00
|
|
|
assert.Equal(t, failed, task.state)
|
2021-11-05 22:25:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range tt.unexpired {
|
|
|
|
task := c.getCompaction(id)
|
2022-08-23 15:50:52 +08:00
|
|
|
assert.NotEqual(t, failed, task.state)
|
2021-11-05 22:25:00 +08:00
|
|
|
}
|
2023-08-30 11:12:27 +08:00
|
|
|
|
2023-12-05 18:44:37 +08:00
|
|
|
scheduler.mu.Lock()
|
|
|
|
assert.Equal(t, 0, len(scheduler.parallelTasks[2]))
|
|
|
|
scheduler.mu.Unlock()
|
2021-11-05 22:25:00 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_newCompactionPlanHandler(t *testing.T) {
|
|
|
|
type args struct {
|
2023-12-11 17:52:37 +08:00
|
|
|
sessions SessionManager
|
2023-12-25 19:24:46 +08:00
|
|
|
cm *ChannelManagerImpl
|
2021-11-05 22:25:00 +08:00
|
|
|
meta *meta
|
|
|
|
allocator allocator
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *compactionPlanHandler
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"test new handler",
|
|
|
|
args{
|
2023-12-11 17:52:37 +08:00
|
|
|
&SessionManagerImpl{},
|
2023-12-25 19:24:46 +08:00
|
|
|
&ChannelManagerImpl{},
|
2021-11-05 22:25:00 +08:00
|
|
|
&meta{},
|
|
|
|
newMockAllocator(),
|
|
|
|
},
|
|
|
|
&compactionPlanHandler{
|
2023-11-07 03:18:18 +08:00
|
|
|
plans: map[int64]*compactionTask{},
|
2023-12-11 17:52:37 +08:00
|
|
|
sessions: &SessionManagerImpl{},
|
2023-12-25 19:24:46 +08:00
|
|
|
chManager: &ChannelManagerImpl{},
|
2023-11-07 03:18:18 +08:00
|
|
|
meta: &meta{},
|
|
|
|
allocator: newMockAllocator(),
|
2023-11-23 17:30:25 +08:00
|
|
|
scheduler: NewCompactionScheduler(),
|
2021-11-05 22:25:00 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-11-14 15:56:19 +08:00
|
|
|
got := newCompactionPlanHandler(tt.args.sessions, tt.args.cm, tt.args.meta, tt.args.allocator)
|
2021-11-05 22:25:00 +08:00
|
|
|
assert.EqualValues(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-11-09 14:47:02 +08:00
|
|
|
|
|
|
|
func Test_getCompactionTasksBySignalID(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
plans map[int64]*compactionTask
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
signalID int64
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want []*compactionTask
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"test get compaction tasks",
|
|
|
|
fields{
|
|
|
|
plans: map[int64]*compactionTask{
|
|
|
|
1: {
|
|
|
|
triggerInfo: &compactionSignal{id: 1},
|
|
|
|
state: executing,
|
|
|
|
},
|
|
|
|
2: {
|
|
|
|
triggerInfo: &compactionSignal{id: 1},
|
|
|
|
state: completed,
|
|
|
|
},
|
|
|
|
3: {
|
|
|
|
triggerInfo: &compactionSignal{id: 1},
|
2022-08-23 15:50:52 +08:00
|
|
|
state: failed,
|
2021-11-09 14:47:02 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args{1},
|
|
|
|
[]*compactionTask{
|
|
|
|
{
|
|
|
|
triggerInfo: &compactionSignal{id: 1},
|
|
|
|
state: executing,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
triggerInfo: &compactionSignal{id: 1},
|
|
|
|
state: completed,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
triggerInfo: &compactionSignal{id: 1},
|
2022-08-23 15:50:52 +08:00
|
|
|
state: failed,
|
2021-11-09 14:47:02 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
h := &compactionPlanHandler{
|
|
|
|
plans: tt.fields.plans,
|
|
|
|
}
|
|
|
|
got := h.getCompactionTasksBySignalID(tt.args.signalID)
|
|
|
|
assert.ElementsMatch(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-06-15 23:14:10 +08:00
|
|
|
|
|
|
|
func getFieldBinlogPaths(id int64, paths ...string) *datapb.FieldBinlog {
|
|
|
|
l := &datapb.FieldBinlog{
|
|
|
|
FieldID: id,
|
|
|
|
Binlogs: make([]*datapb.Binlog, 0, len(paths)),
|
|
|
|
}
|
|
|
|
for _, path := range paths {
|
|
|
|
l.Binlogs = append(l.Binlogs, &datapb.Binlog{LogPath: path})
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
2023-02-27 10:41:46 +08:00
|
|
|
|
|
|
|
func getFieldBinlogPathsWithEntry(id int64, entry int64, paths ...string) *datapb.FieldBinlog {
|
|
|
|
l := &datapb.FieldBinlog{
|
|
|
|
FieldID: id,
|
|
|
|
Binlogs: make([]*datapb.Binlog, 0, len(paths)),
|
|
|
|
}
|
|
|
|
for _, path := range paths {
|
|
|
|
l.Binlogs = append(l.Binlogs, &datapb.Binlog{LogPath: path, EntriesNum: entry})
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|