policy_test.go 13.6 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
package datacoord
S
sunby 已提交
18 19 20

import (
	"testing"
21
	"time"
S
sunby 已提交
22

23 24 25
	"github.com/golang/protobuf/proto"
	"github.com/milvus-io/milvus/internal/kv"
	memkv "github.com/milvus-io/milvus/internal/kv/mem"
S
sunby 已提交
26 27
	"github.com/milvus-io/milvus/internal/proto/datapb"
	"github.com/stretchr/testify/assert"
28
	"stathat.com/c/consistent"
S
sunby 已提交
29 30
)

31 32 33 34 35 36 37 38 39 40 41 42
func fillEmptyPosition(operations ChannelOpSet) {
	for _, op := range operations {
		if op.Type == Add {
			for range op.Channels {
				op.ChannelWatchInfos = append(op.ChannelWatchInfos, nil)
			}
		}
	}
}

func TestBufferChannelAssignPolicy(t *testing.T) {
	kv := memkv.NewMemoryKV()
43

44
	channels := []*channel{{Name: "chan1", CollectionID: 1}}
45 46 47
	store := &ChannelStore{
		store:        kv,
		channelsInfo: map[int64]*NodeChannelInfo{bufferID: {bufferID, channels}},
48
	}
49 50 51 52 53 54 55 56 57 58 59 60 61 62

	updates := BufferChannelAssignPolicy(store, 1)
	assert.NotNil(t, updates)
	assert.Equal(t, 2, len(updates))
	assert.EqualValues(t, &ChannelOp{Type: Delete, NodeID: bufferID, Channels: channels}, updates[0])
	assert.EqualValues(t, 1, updates[1].NodeID)
	assert.Equal(t, Add, updates[1].Type)
	assert.Equal(t, channels, updates[1].Channels)
}

func TestConsistentHashRegisterPolicy(t *testing.T) {
	t.Run("first register", func(t *testing.T) {
		kv := memkv.NewMemoryKV()
		channels := []*channel{
63 64
			{Name: "chan1", CollectionID: 1},
			{Name: "chan2", CollectionID: 2},
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
		}
		store := &ChannelStore{
			store:        kv,
			channelsInfo: map[int64]*NodeChannelInfo{bufferID: {bufferID, channels}},
		}

		hashring := consistent.New()
		policy := ConsistentHashRegisterPolicy(hashring)

		updates := policy(store, 1)
		assert.NotNil(t, updates)
		assert.Equal(t, 2, len(updates))
		assert.EqualValues(t, &ChannelOp{Type: Delete, NodeID: bufferID, Channels: channels}, updates[0])
		assert.EqualValues(t, &ChannelOp{Type: Add, NodeID: 1, Channels: channels}, updates[1])
	})

	t.Run("rebalance after register", func(t *testing.T) {
		kv := memkv.NewMemoryKV()

		channels := []*channel{
85 86
			{Name: "chan1", CollectionID: 1},
			{Name: "chan2", CollectionID: 2},
87 88 89 90 91 92 93 94 95 96 97 98 99 100
		}

		store := &ChannelStore{
			store:        kv,
			channelsInfo: map[int64]*NodeChannelInfo{1: {1, channels}, 2: {2, []*channel{}}},
		}

		hashring := consistent.New()
		hashring.Add(formatNodeID(1))
		policy := ConsistentHashRegisterPolicy(hashring)

		updates := policy(store, 2)

		assert.NotNil(t, updates)
101 102 103
		assert.Equal(t, 1, len(updates))
		// No Delete operation will be generated
		assert.EqualValues(t, &ChannelOp{Type: Add, NodeID: 1, Channels: []*channel{channels[0]}}, updates[0])
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	})
}

func TestAverageAssignPolicy(t *testing.T) {
	type args struct {
		store    ROChannelStore
		channels []*channel
	}
	tests := []struct {
		name string
		args args
		want ChannelOpSet
	}{
		{
			"test assign empty cluster",
			args{
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{},
				},
124
				[]*channel{{Name: "chan1", CollectionID: 1}},
125
			},
126
			[]*ChannelOp{{Add, bufferID, []*channel{{Name: "chan1", CollectionID: 1}}, nil}},
127 128 129 130 131 132 133
		},
		{
			"test watch same channel",
			args{
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{
134
						1: {1, []*channel{{Name: "chan1", CollectionID: 1}}},
135 136
					},
				},
137
				[]*channel{{Name: "chan1", CollectionID: 1}},
138 139 140 141 142 143 144 145 146
			},
			nil,
		},
		{
			"test normal assign",
			args{
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{
147 148
						1: {1, []*channel{{Name: "chan1", CollectionID: 1}, {Name: "chan2", CollectionID: 1}}},
						2: {2, []*channel{{Name: "chan3", CollectionID: 1}}},
149 150
					},
				},
151
				[]*channel{{Name: "chan4", CollectionID: 1}},
152
			},
153
			[]*ChannelOp{{Add, 2, []*channel{{Name: "chan4", CollectionID: 1}}, nil}},
154
		},
155
	}
156 157 158 159 160
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := AverageAssignPolicy(tt.args.store, tt.args.channels)
			assert.EqualValues(t, tt.want, got)
		})
161
	}
162
}
163

164 165 166 167 168
func TestConsistentHashChannelAssignPolicy(t *testing.T) {
	type args struct {
		hashring *consistent.Consistent
		store    ROChannelStore
		channels []*channel
S
sunby 已提交
169
	}
170 171 172 173 174 175 176 177 178 179 180 181 182
	tests := []struct {
		name string
		args args
		want ChannelOpSet
	}{
		{
			"test assign empty cluster",
			args{
				consistent.New(),
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{},
				},
183
				[]*channel{{Name: "chan1", CollectionID: 1}},
184
			},
185
			[]*ChannelOp{{Add, bufferID, []*channel{{Name: "chan1", CollectionID: 1}}, nil}},
186 187 188 189 190 191 192 193
		},
		{
			"test watch same channel",
			args{
				consistent.New(),
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{
194
						1: {1, []*channel{{Name: "chan1", CollectionID: 1}, {Name: "chan2", CollectionID: 1}}},
195 196
					},
				},
197
				[]*channel{{Name: "chan1", CollectionID: 1}},
198 199 200 201 202 203 204 205 206 207 208
			},
			nil,
		},
		{
			"test normal watch",
			args{
				consistent.New(),
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{1: {1, nil}, 2: {2, nil}, 3: {3, nil}},
				},
209
				[]*channel{{Name: "chan1", CollectionID: 1}, {Name: "chan2", CollectionID: 1}, {Name: "chan3", CollectionID: 1}},
210
			},
211
			[]*ChannelOp{{Add, 2, []*channel{{Name: "chan1", CollectionID: 1}}, nil}, {Add, 1, []*channel{{Name: "chan2", CollectionID: 1}}, nil}, {Add, 3, []*channel{{Name: "chan3", CollectionID: 1}}, nil}},
212
		},
S
sunby 已提交
213
	}
214 215 216 217 218 219 220
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			policy := ConsistentHashChannelAssignPolicy(tt.args.hashring)
			got := policy(tt.args.store, tt.args.channels)
			assert.Equal(t, len(tt.want), len(got))
			for _, op := range tt.want {
				assert.Contains(t, got, op)
221
			}
222
		})
223 224
	}
}
C
congqixia 已提交
225

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
func TestAvgAssignUnregisteredChannels(t *testing.T) {
	type args struct {
		store  ROChannelStore
		nodeID int64
	}
	tests := []struct {
		name string
		args args
		want ChannelOpSet
	}{
		{
			"test deregister the last node",
			args{
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{
242
						1: {1, []*channel{{Name: "chan1", CollectionID: 1}}},
243 244 245 246
					},
				},
				1,
			},
247
			[]*ChannelOp{{Delete, 1, []*channel{{Name: "chan1", CollectionID: 1}}, nil}, {Add, bufferID, []*channel{{Name: "chan1", CollectionID: 1}}, nil}},
248 249 250 251 252 253 254
		},
		{
			"test rebalance channels after deregister",
			args{
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{
255 256
						1: {1, []*channel{{Name: "chan1", CollectionID: 1}}},
						2: {2, []*channel{{Name: "chan2", CollectionID: 1}}},
257 258 259 260 261
						3: {3, []*channel{}},
					},
				},
				2,
			},
262
			[]*ChannelOp{{Delete, 2, []*channel{{Name: "chan2", CollectionID: 1}}, nil}, {Add, 3, []*channel{{Name: "chan2", CollectionID: 1}}, nil}},
263 264 265 266 267 268 269
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := AvgAssignUnregisteredChannels(tt.args.store, tt.args.nodeID)
			assert.EqualValues(t, tt.want, got)
		})
C
congqixia 已提交
270
	}
271
}
C
congqixia 已提交
272

273 274 275 276 277 278 279 280 281 282 283
func TestConsistentHashDeregisterPolicy(t *testing.T) {
	type args struct {
		hashring *consistent.Consistent
		store    ROChannelStore
		nodeID   int64
	}
	tests := []struct {
		name string
		args args
		want ChannelOpSet
	}{
C
congqixia 已提交
284
		{
285 286 287 288 289 290
			"test deregister the last node",
			args{
				consistent.New(),
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{
291
						1: {1, []*channel{{Name: "chan1", CollectionID: 1}}},
292 293 294
					},
				},
				1,
C
congqixia 已提交
295
			},
296
			[]*ChannelOp{{Delete, 1, []*channel{{Name: "chan1", CollectionID: 1}}, nil}, {Add, bufferID, []*channel{{Name: "chan1", CollectionID: 1}}, nil}},
C
congqixia 已提交
297 298
		},
		{
299 300 301 302 303 304
			"rebalance after deregister",
			args{
				consistent.New(),
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{
305 306 307
						1: {1, []*channel{{Name: "chan2", CollectionID: 1}}},
						2: {2, []*channel{{Name: "chan1", CollectionID: 1}}},
						3: {3, []*channel{{Name: "chan3", CollectionID: 1}}},
308 309 310 311
					},
				},
				2,
			},
312
			[]*ChannelOp{{Delete, 2, []*channel{{Name: "chan1", CollectionID: 1}}, nil}, {Add, 1, []*channel{{Name: "chan1", CollectionID: 1}}, nil}},
C
congqixia 已提交
313
		},
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			policy := ConsistentHashDeregisterPolicy(tt.args.hashring)
			got := policy(tt.args.store, tt.args.nodeID)
			assert.EqualValues(t, tt.want, got)
		})
	}
}

func TestAverageReassignPolicy(t *testing.T) {
	type args struct {
		store     ROChannelStore
		reassigns []*NodeChannelInfo
	}
	tests := []struct {
		name string
		args args
		want ChannelOpSet
	}{
C
congqixia 已提交
334
		{
335 336 337 338 339
			"test only one node",
			args{
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{
340
						1: {1, []*channel{{Name: "chan1", CollectionID: 1}}},
341 342
					},
				},
343
				[]*NodeChannelInfo{{1, []*channel{{Name: "chan1", CollectionID: 1}}}},
344 345
			},
			nil,
C
congqixia 已提交
346 347
		},
		{
348 349 350 351 352
			"test normal reassing",
			args{
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{
353
						1: {1, []*channel{{Name: "chan1", CollectionID: 1}, {Name: "chan2", CollectionID: 1}}},
354 355 356
						2: {2, []*channel{}},
					},
				},
357
				[]*NodeChannelInfo{{1, []*channel{{Name: "chan1", CollectionID: 1}, {Name: "chan2", CollectionID: 1}}}},
358
			},
359
			[]*ChannelOp{{Delete, 1, []*channel{{Name: "chan1", CollectionID: 1}, {Name: "chan2", CollectionID: 1}}, nil}, {Add, 2, []*channel{{Name: "chan1", CollectionID: 1}, {Name: "chan2", CollectionID: 1}}, nil}},
C
congqixia 已提交
360 361
		},
	}
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := AverageReassignPolicy(tt.args.store, tt.args.reassigns)
			assert.EqualValues(t, tt.want, got)
		})
	}
}

func TestBgCheckWithMaxWatchDuration(t *testing.T) {
	type watch struct {
		nodeID int64
		name   string
		info   *datapb.ChannelWatchInfo
	}
	getKv := func(watchInfos []*watch) kv.TxnKV {
		kv := memkv.NewMemoryKV()
		for _, info := range watchInfos {
379
			k := buildNodeChannelKey(info.nodeID, info.name)
380 381
			v, _ := proto.Marshal(info.info)
			kv.Save(k, string(v))
C
congqixia 已提交
382
		}
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
		return kv
	}

	type args struct {
		kv        kv.TxnKV
		channels  []*NodeChannelInfo
		timestamp time.Time
	}

	ts := time.Now()
	tests := []struct {
		name    string
		args    args
		want    []*NodeChannelInfo
		wantErr error
	}{
		{
			"test normal expiration",
			args{
				getKv([]*watch{{1, "chan1", &datapb.ChannelWatchInfo{StartTs: ts.Unix(), State: datapb.ChannelWatchState_Uncomplete}},
					{1, "chan2", &datapb.ChannelWatchInfo{StartTs: ts.Unix(), State: datapb.ChannelWatchState_Complete}}}),
404
				[]*NodeChannelInfo{{1, []*channel{{Name: "chan1", CollectionID: 1}, {Name: "chan2", CollectionID: 1}}}},
405 406
				ts.Add(maxWatchDuration),
			},
407
			[]*NodeChannelInfo{{1, []*channel{{Name: "chan1", CollectionID: 1}}}},
408 409 410 411 412 413
			nil,
		},
		{
			"test no expiration",
			args{
				getKv([]*watch{{1, "chan1", &datapb.ChannelWatchInfo{StartTs: ts.Unix(), State: datapb.ChannelWatchState_Uncomplete}}}),
414
				[]*NodeChannelInfo{{1, []*channel{{Name: "chan1", CollectionID: 1}}}},
415 416 417 418 419 420 421 422 423 424 425 426 427
				ts.Add(maxWatchDuration).Add(-time.Second),
			},
			[]*NodeChannelInfo{},
			nil,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			policy := BgCheckWithMaxWatchDuration(tt.args.kv)
			got, err := policy(tt.args.channels, tt.args.timestamp)
			assert.Equal(t, tt.wantErr, err)
			assert.EqualValues(t, tt.want, got)
		})
C
congqixia 已提交
428 429
	}
}
S
sunby 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457

func TestAvgAssignRegisterPolicy(t *testing.T) {
	type args struct {
		store  ROChannelStore
		nodeID int64
	}
	tests := []struct {
		name string
		args args
		want ChannelOpSet
	}{
		{
			"test empty",
			args{
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{},
				},
				1,
			},
			nil,
		},
		{
			"test with buffer channel",
			args{
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{
458
						bufferID: {bufferID, []*channel{{Name: "ch1", CollectionID: 1}}},
S
sunby 已提交
459 460 461 462 463 464 465 466
					},
				},
				1,
			},
			[]*ChannelOp{
				{
					Type:     Delete,
					NodeID:   bufferID,
467
					Channels: []*channel{{Name: "ch1", CollectionID: 1}},
S
sunby 已提交
468 469 470 471
				},
				{
					Type:     Add,
					NodeID:   1,
472
					Channels: []*channel{{Name: "ch1", CollectionID: 1}},
S
sunby 已提交
473 474 475 476 477 478 479 480 481
				},
			},
		},
		{
			"test with avg assign",
			args{
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{
482
						1: {1, []*channel{{Name: "ch1", CollectionID: 1}, {Name: "ch2", CollectionID: 1}}},
S
sunby 已提交
483 484 485 486 487 488 489
					},
				},
				3,
			},
			[]*ChannelOp{
				{
					Type:     Add,
490
					NodeID:   1,
491
					Channels: []*channel{{Name: "ch1", CollectionID: 1}},
S
sunby 已提交
492 493 494 495 496 497 498 499 500
				},
			},
		},
		{
			"test with avg equals to zero",
			args{
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{
501 502
						1: {1, []*channel{{Name: "ch1", CollectionID: 1}}},
						2: {2, []*channel{{Name: "ch3", CollectionID: 1}}},
S
sunby 已提交
503 504 505 506 507 508 509 510 511 512 513 514
					},
				},
				3,
			},
			nil,
		},
		{
			"test node with empty channel",
			args{
				&ChannelStore{
					memkv.NewMemoryKV(),
					map[int64]*NodeChannelInfo{
515
						1: {1, []*channel{{Name: "ch1", CollectionID: 1}, {Name: "ch2", CollectionID: 1}, {Name: "ch3", CollectionID: 1}}},
S
sunby 已提交
516 517 518 519 520 521 522 523
						2: {2, []*channel{}},
					},
				},
				3,
			},
			[]*ChannelOp{
				{
					Type:     Add,
524
					NodeID:   1,
525
					Channels: []*channel{{Name: "ch1", CollectionID: 1}},
S
sunby 已提交
526 527 528 529 530 531 532 533 534 535 536
				},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := AvgAssignRegisterPolicy(tt.args.store, tt.args.nodeID)
			assert.EqualValues(t, tt.want, got)
		})
	}
}