2023-03-27 00:42:00 +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 delegator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DistributionSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
dist *distribution
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DistributionSuite) SetupTest() {
|
|
|
|
s.dist = NewDistribution()
|
2023-06-27 11:48:45 +08:00
|
|
|
s.Equal(initialTargetVersion, s.dist.getTargetVersion())
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DistributionSuite) TearDownTest() {
|
|
|
|
s.dist = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DistributionSuite) TestAddDistribution() {
|
|
|
|
type testCase struct {
|
2023-04-11 17:56:31 +08:00
|
|
|
tag string
|
|
|
|
input []SegmentEntry
|
|
|
|
growing []SegmentEntry
|
|
|
|
expected []SnapshotItem
|
|
|
|
expectedSignalClosed bool
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testCase{
|
|
|
|
{
|
2023-04-11 17:56:31 +08:00
|
|
|
tag: "one_node",
|
2023-03-27 00:42:00 +08:00
|
|
|
input: []SegmentEntry{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []SnapshotItem{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
Segments: []SegmentEntry{
|
|
|
|
{
|
2023-08-15 17:27:35 +08:00
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 1,
|
|
|
|
TargetVersion: unreadableTargetVersion,
|
2023-03-27 00:42:00 +08:00
|
|
|
},
|
|
|
|
{
|
2023-08-15 17:27:35 +08:00
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 2,
|
|
|
|
TargetVersion: unreadableTargetVersion,
|
2023-03-27 00:42:00 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-04-11 17:56:31 +08:00
|
|
|
expectedSignalClosed: true,
|
2023-03-27 00:42:00 +08:00
|
|
|
},
|
2023-08-11 11:21:32 +08:00
|
|
|
{
|
|
|
|
tag: "duplicate segment",
|
|
|
|
input: []SegmentEntry{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []SnapshotItem{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
Segments: []SegmentEntry{
|
|
|
|
{
|
2023-08-15 17:27:35 +08:00
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 1,
|
|
|
|
TargetVersion: unreadableTargetVersion,
|
2023-08-11 11:21:32 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedSignalClosed: true,
|
|
|
|
},
|
2023-03-27 00:42:00 +08:00
|
|
|
{
|
2023-04-11 17:56:31 +08:00
|
|
|
tag: "multiple_nodes",
|
2023-03-27 00:42:00 +08:00
|
|
|
input: []SegmentEntry{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 2,
|
|
|
|
SegmentID: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []SnapshotItem{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
Segments: []SegmentEntry{
|
|
|
|
{
|
2023-08-15 17:27:35 +08:00
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 1,
|
|
|
|
TargetVersion: unreadableTargetVersion,
|
2023-03-27 00:42:00 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2023-08-15 17:27:35 +08:00
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 3,
|
|
|
|
TargetVersion: unreadableTargetVersion,
|
2023-03-27 00:42:00 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 2,
|
|
|
|
Segments: []SegmentEntry{
|
|
|
|
{
|
2023-08-15 17:27:35 +08:00
|
|
|
NodeID: 2,
|
|
|
|
SegmentID: 2,
|
|
|
|
TargetVersion: unreadableTargetVersion,
|
2023-03-27 00:42:00 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-04-11 17:56:31 +08:00
|
|
|
expectedSignalClosed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "remove_growing",
|
|
|
|
growing: []SegmentEntry{
|
|
|
|
{NodeID: 1, SegmentID: 1},
|
|
|
|
},
|
|
|
|
input: []SegmentEntry{
|
|
|
|
{NodeID: 1, SegmentID: 1},
|
|
|
|
{NodeID: 1, SegmentID: 2},
|
|
|
|
},
|
|
|
|
expected: []SnapshotItem{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
Segments: []SegmentEntry{
|
2023-08-15 17:27:35 +08:00
|
|
|
{NodeID: 1, SegmentID: 1, TargetVersion: unreadableTargetVersion},
|
|
|
|
{NodeID: 1, SegmentID: 2, TargetVersion: unreadableTargetVersion},
|
2023-04-11 17:56:31 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedSignalClosed: false,
|
2023-03-27 00:42:00 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
s.Run(tc.tag, func() {
|
|
|
|
s.SetupTest()
|
|
|
|
defer s.TearDownTest()
|
2023-04-11 17:56:31 +08:00
|
|
|
s.dist.AddGrowing(tc.growing...)
|
2023-06-27 11:48:45 +08:00
|
|
|
_, _, version := s.dist.GetSegments(false)
|
|
|
|
s.dist.AddDistributions(tc.input...)
|
|
|
|
sealed, _ := s.dist.PeekSegments(false)
|
2023-03-27 00:42:00 +08:00
|
|
|
s.compareSnapshotItems(tc.expected, sealed)
|
2023-04-13 20:46:29 +08:00
|
|
|
s.dist.FinishUsage(version)
|
2023-03-27 00:42:00 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 17:56:31 +08:00
|
|
|
func (s *DistributionSuite) isClosedCh(ch chan struct{}) bool {
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-27 00:42:00 +08:00
|
|
|
func (s *DistributionSuite) compareSnapshotItems(target, value []SnapshotItem) {
|
|
|
|
if !s.Equal(len(target), len(value)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mapNodeItem := make(map[int64]SnapshotItem)
|
|
|
|
for _, valueItem := range value {
|
|
|
|
mapNodeItem[valueItem.NodeID] = valueItem
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, targetItem := range target {
|
|
|
|
valueItem, ok := mapNodeItem[targetItem.NodeID]
|
|
|
|
if !s.True(ok) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.ElementsMatch(targetItem.Segments, valueItem.Segments)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DistributionSuite) TestAddGrowing() {
|
|
|
|
type testCase struct {
|
|
|
|
tag string
|
|
|
|
input []SegmentEntry
|
|
|
|
expected []SegmentEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testCase{
|
|
|
|
{
|
|
|
|
tag: "nil input",
|
|
|
|
input: nil,
|
|
|
|
expected: []SegmentEntry{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "normal case",
|
|
|
|
input: []SegmentEntry{
|
|
|
|
{SegmentID: 1, PartitionID: 1},
|
|
|
|
{SegmentID: 2, PartitionID: 2},
|
|
|
|
},
|
|
|
|
expected: []SegmentEntry{
|
|
|
|
{SegmentID: 1, PartitionID: 1},
|
|
|
|
{SegmentID: 2, PartitionID: 2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
s.Run(tc.tag, func() {
|
|
|
|
s.SetupTest()
|
|
|
|
defer s.TearDownTest()
|
|
|
|
|
|
|
|
s.dist.AddGrowing(tc.input...)
|
2023-06-27 11:48:45 +08:00
|
|
|
_, growing, version := s.dist.GetSegments(false)
|
2023-03-27 00:42:00 +08:00
|
|
|
defer s.dist.FinishUsage(version)
|
|
|
|
|
|
|
|
s.ElementsMatch(tc.expected, growing)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DistributionSuite) TestRemoveDistribution() {
|
|
|
|
type testCase struct {
|
|
|
|
tag string
|
|
|
|
presetSealed []SegmentEntry
|
|
|
|
presetGrowing []SegmentEntry
|
|
|
|
removalSealed []SegmentEntry
|
|
|
|
removalGrowing []SegmentEntry
|
|
|
|
|
|
|
|
withMockRead bool
|
|
|
|
expectSealed []SnapshotItem
|
|
|
|
expectGrowing []SegmentEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testCase{
|
|
|
|
{
|
|
|
|
tag: "remove with no read",
|
|
|
|
presetSealed: []SegmentEntry{
|
|
|
|
{NodeID: 1, SegmentID: 1},
|
|
|
|
{NodeID: 2, SegmentID: 2},
|
|
|
|
{NodeID: 1, SegmentID: 3},
|
|
|
|
},
|
|
|
|
presetGrowing: []SegmentEntry{
|
|
|
|
{SegmentID: 4},
|
|
|
|
{SegmentID: 5},
|
|
|
|
},
|
|
|
|
|
|
|
|
removalSealed: []SegmentEntry{
|
|
|
|
{NodeID: 1, SegmentID: 1},
|
|
|
|
},
|
|
|
|
removalGrowing: []SegmentEntry{
|
|
|
|
{SegmentID: 5},
|
|
|
|
},
|
|
|
|
|
|
|
|
withMockRead: false,
|
|
|
|
|
|
|
|
expectSealed: []SnapshotItem{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
Segments: []SegmentEntry{
|
2023-08-15 17:27:35 +08:00
|
|
|
{NodeID: 1, SegmentID: 3, TargetVersion: unreadableTargetVersion},
|
2023-03-27 00:42:00 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 2,
|
|
|
|
Segments: []SegmentEntry{
|
2023-08-15 17:27:35 +08:00
|
|
|
{NodeID: 2, SegmentID: 2, TargetVersion: unreadableTargetVersion},
|
2023-03-27 00:42:00 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectGrowing: []SegmentEntry{{SegmentID: 4}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "remove with wrong nodeID",
|
|
|
|
presetSealed: []SegmentEntry{
|
|
|
|
{NodeID: 1, SegmentID: 1},
|
|
|
|
{NodeID: 2, SegmentID: 2},
|
|
|
|
{NodeID: 1, SegmentID: 3},
|
|
|
|
},
|
|
|
|
presetGrowing: []SegmentEntry{
|
|
|
|
{SegmentID: 4},
|
|
|
|
{SegmentID: 5},
|
|
|
|
},
|
|
|
|
|
|
|
|
removalSealed: []SegmentEntry{
|
|
|
|
{NodeID: 2, SegmentID: 1},
|
|
|
|
},
|
|
|
|
removalGrowing: nil,
|
|
|
|
|
|
|
|
withMockRead: false,
|
|
|
|
|
|
|
|
expectSealed: []SnapshotItem{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
Segments: []SegmentEntry{
|
2023-08-15 17:27:35 +08:00
|
|
|
{NodeID: 1, SegmentID: 1, TargetVersion: unreadableTargetVersion},
|
|
|
|
{NodeID: 1, SegmentID: 3, TargetVersion: unreadableTargetVersion},
|
2023-03-27 00:42:00 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 2,
|
|
|
|
Segments: []SegmentEntry{
|
2023-08-15 17:27:35 +08:00
|
|
|
{NodeID: 2, SegmentID: 2, TargetVersion: unreadableTargetVersion},
|
2023-03-27 00:42:00 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectGrowing: []SegmentEntry{{SegmentID: 4}, {SegmentID: 5}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "remove with wildcardNodeID",
|
|
|
|
presetSealed: []SegmentEntry{
|
|
|
|
{NodeID: 1, SegmentID: 1},
|
|
|
|
{NodeID: 2, SegmentID: 2},
|
|
|
|
{NodeID: 1, SegmentID: 3},
|
|
|
|
},
|
|
|
|
presetGrowing: []SegmentEntry{
|
|
|
|
{SegmentID: 4},
|
|
|
|
{SegmentID: 5},
|
|
|
|
},
|
|
|
|
|
|
|
|
removalSealed: []SegmentEntry{
|
|
|
|
{NodeID: wildcardNodeID, SegmentID: 1},
|
|
|
|
},
|
|
|
|
removalGrowing: nil,
|
|
|
|
|
|
|
|
withMockRead: false,
|
|
|
|
|
|
|
|
expectSealed: []SnapshotItem{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
Segments: []SegmentEntry{
|
2023-08-15 17:27:35 +08:00
|
|
|
{NodeID: 1, SegmentID: 3, TargetVersion: unreadableTargetVersion},
|
2023-03-27 00:42:00 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 2,
|
|
|
|
Segments: []SegmentEntry{
|
2023-08-15 17:27:35 +08:00
|
|
|
{NodeID: 2, SegmentID: 2, TargetVersion: unreadableTargetVersion},
|
2023-03-27 00:42:00 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectGrowing: []SegmentEntry{{SegmentID: 4}, {SegmentID: 5}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "remove with read",
|
|
|
|
presetSealed: []SegmentEntry{
|
|
|
|
{NodeID: 1, SegmentID: 1},
|
|
|
|
{NodeID: 2, SegmentID: 2},
|
|
|
|
{NodeID: 1, SegmentID: 3},
|
|
|
|
},
|
|
|
|
presetGrowing: []SegmentEntry{
|
|
|
|
{SegmentID: 4},
|
|
|
|
{SegmentID: 5},
|
|
|
|
},
|
|
|
|
|
|
|
|
removalSealed: []SegmentEntry{
|
|
|
|
{NodeID: 1, SegmentID: 1},
|
|
|
|
},
|
|
|
|
removalGrowing: []SegmentEntry{
|
|
|
|
{SegmentID: 5},
|
|
|
|
},
|
|
|
|
|
|
|
|
withMockRead: true,
|
|
|
|
|
|
|
|
expectSealed: []SnapshotItem{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
Segments: []SegmentEntry{
|
2023-08-15 17:27:35 +08:00
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 3,
|
|
|
|
TargetVersion: unreadableTargetVersion,
|
|
|
|
},
|
2023-03-27 00:42:00 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 2,
|
|
|
|
Segments: []SegmentEntry{
|
2023-08-15 17:27:35 +08:00
|
|
|
{
|
|
|
|
NodeID: 2,
|
|
|
|
SegmentID: 2,
|
|
|
|
TargetVersion: unreadableTargetVersion,
|
|
|
|
},
|
2023-03-27 00:42:00 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectGrowing: []SegmentEntry{{SegmentID: 4}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
s.Run(tc.tag, func() {
|
|
|
|
s.SetupTest()
|
|
|
|
defer s.TearDownTest()
|
|
|
|
|
|
|
|
s.dist.AddGrowing(tc.presetGrowing...)
|
|
|
|
s.dist.AddDistributions(tc.presetSealed...)
|
|
|
|
|
|
|
|
var version int64
|
|
|
|
if tc.withMockRead {
|
2023-06-27 11:48:45 +08:00
|
|
|
_, _, version = s.dist.GetSegments(false)
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ch := s.dist.RemoveDistributions(tc.removalSealed, tc.removalGrowing)
|
|
|
|
|
|
|
|
if tc.withMockRead {
|
|
|
|
// check ch not closed
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
s.Fail("ch closed with running read")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
s.dist.FinishUsage(version)
|
|
|
|
}
|
|
|
|
// check ch close very soon
|
|
|
|
timeout := time.NewTimer(time.Second)
|
|
|
|
defer timeout.Stop()
|
|
|
|
select {
|
|
|
|
case <-timeout.C:
|
|
|
|
s.Fail("ch not closed after 1 second")
|
|
|
|
case <-ch:
|
|
|
|
}
|
|
|
|
|
2023-06-27 11:48:45 +08:00
|
|
|
sealed, growing, version := s.dist.GetSegments(false)
|
2023-03-27 00:42:00 +08:00
|
|
|
defer s.dist.FinishUsage(version)
|
|
|
|
s.compareSnapshotItems(tc.expectSealed, sealed)
|
|
|
|
s.ElementsMatch(tc.expectGrowing, growing)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 16:10:24 +08:00
|
|
|
func (s *DistributionSuite) TestPeek() {
|
|
|
|
type testCase struct {
|
|
|
|
tag string
|
|
|
|
input []SegmentEntry
|
|
|
|
expected []SnapshotItem
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testCase{
|
|
|
|
{
|
|
|
|
tag: "one node",
|
|
|
|
input: []SegmentEntry{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []SnapshotItem{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
Segments: []SegmentEntry{
|
|
|
|
{
|
2023-08-15 17:27:35 +08:00
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 1,
|
|
|
|
TargetVersion: unreadableTargetVersion,
|
2023-04-03 16:10:24 +08:00
|
|
|
},
|
|
|
|
{
|
2023-08-15 17:27:35 +08:00
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 2,
|
|
|
|
TargetVersion: unreadableTargetVersion,
|
2023-04-03 16:10:24 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "multiple nodes",
|
|
|
|
input: []SegmentEntry{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 2,
|
|
|
|
SegmentID: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []SnapshotItem{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
Segments: []SegmentEntry{
|
|
|
|
{
|
2023-08-15 17:27:35 +08:00
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 1,
|
|
|
|
TargetVersion: unreadableTargetVersion,
|
2023-04-03 16:10:24 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2023-08-15 17:27:35 +08:00
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 3,
|
|
|
|
TargetVersion: unreadableTargetVersion,
|
2023-04-03 16:10:24 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 2,
|
|
|
|
Segments: []SegmentEntry{
|
|
|
|
{
|
2023-08-15 17:27:35 +08:00
|
|
|
NodeID: 2,
|
|
|
|
SegmentID: 2,
|
|
|
|
TargetVersion: unreadableTargetVersion,
|
2023-04-03 16:10:24 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
s.Run(tc.tag, func() {
|
|
|
|
s.SetupTest()
|
|
|
|
defer s.TearDownTest()
|
|
|
|
|
|
|
|
// peek during lock
|
|
|
|
s.dist.AddDistributions(tc.input...)
|
|
|
|
s.dist.mut.Lock()
|
2023-06-27 11:48:45 +08:00
|
|
|
sealed, _ := s.dist.PeekSegments(false)
|
2023-04-03 16:10:24 +08:00
|
|
|
s.compareSnapshotItems(tc.expected, sealed)
|
|
|
|
s.dist.mut.Unlock()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 14:42:30 +08:00
|
|
|
func (s *DistributionSuite) TestAddOfflines() {
|
|
|
|
type testCase struct {
|
|
|
|
tag string
|
|
|
|
input []SegmentEntry
|
|
|
|
offlines []int64
|
|
|
|
serviceable bool
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testCase{
|
|
|
|
{
|
|
|
|
tag: "offlineHits",
|
|
|
|
input: []SegmentEntry{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
offlines: []int64{2},
|
|
|
|
serviceable: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "offlineMissed",
|
|
|
|
input: []SegmentEntry{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 2,
|
|
|
|
SegmentID: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
serviceable: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
s.Run(tc.tag, func() {
|
|
|
|
s.SetupTest()
|
|
|
|
defer s.TearDownTest()
|
|
|
|
|
|
|
|
s.dist.AddDistributions(tc.input...)
|
|
|
|
s.dist.AddOfflines(tc.offlines...)
|
|
|
|
s.Equal(tc.serviceable, s.dist.Serviceable())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-27 11:48:45 +08:00
|
|
|
func (s *DistributionSuite) Test_SyncTargetVersion() {
|
|
|
|
growing := []SegmentEntry{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 1,
|
|
|
|
PartitionID: 1,
|
|
|
|
TargetVersion: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 2,
|
|
|
|
PartitionID: 1,
|
|
|
|
TargetVersion: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 3,
|
|
|
|
PartitionID: 1,
|
|
|
|
TargetVersion: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
sealed := []SegmentEntry{
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 4,
|
|
|
|
PartitionID: 1,
|
|
|
|
TargetVersion: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 5,
|
|
|
|
PartitionID: 1,
|
|
|
|
TargetVersion: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeID: 1,
|
|
|
|
SegmentID: 6,
|
|
|
|
PartitionID: 1,
|
|
|
|
TargetVersion: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.dist.AddGrowing(growing...)
|
|
|
|
s.dist.AddDistributions(sealed...)
|
2023-07-24 14:09:00 +08:00
|
|
|
s.dist.SyncTargetVersion(2, []int64{2, 3}, []int64{6}, []int64{})
|
2023-06-27 11:48:45 +08:00
|
|
|
|
|
|
|
s1, s2, _ := s.dist.GetSegments(true)
|
|
|
|
s.Len(s1[0].Segments, 1)
|
|
|
|
s.Len(s2, 2)
|
|
|
|
|
|
|
|
s1, s2, _ = s.dist.GetSegments(false)
|
|
|
|
s.Len(s1[0].Segments, 3)
|
|
|
|
s.Len(s2, 3)
|
2023-07-07 19:32:25 +08:00
|
|
|
|
|
|
|
s.dist.serviceable.Store(true)
|
2023-07-24 14:09:00 +08:00
|
|
|
s.dist.SyncTargetVersion(2, []int64{222}, []int64{}, []int64{})
|
2023-07-07 19:32:25 +08:00
|
|
|
s.True(s.dist.Serviceable())
|
|
|
|
|
2023-07-24 14:09:00 +08:00
|
|
|
s.dist.SyncTargetVersion(2, []int64{}, []int64{333}, []int64{})
|
2023-07-07 19:32:25 +08:00
|
|
|
s.False(s.dist.Serviceable())
|
2023-07-24 14:09:00 +08:00
|
|
|
|
|
|
|
s.dist.SyncTargetVersion(2, []int64{}, []int64{333}, []int64{1, 2, 3})
|
|
|
|
_, segments, _ := s.dist.GetSegments(true)
|
|
|
|
s.Len(segments, 0)
|
2023-06-27 11:48:45 +08:00
|
|
|
}
|
|
|
|
|
2023-03-27 00:42:00 +08:00
|
|
|
func TestDistributionSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(DistributionSuite))
|
|
|
|
}
|