Skip to content

Commit 75b8d03

Browse files
committed
fix: taint master node in leave host test
Signed-off-by: BruceAko <chongzhi@hust.edu.cn>
1 parent f96fb35 commit 75b8d03

File tree

17 files changed

+134
-81
lines changed

17 files changed

+134
-81
lines changed

client/daemon/daemon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func New(opt *config.DaemonOption, d dfpath.Dfpath) (Daemon, error) {
128128
}
129129

130130
host := &schedulerv1.PeerHost{
131-
Id: idgen.HostIDV2(opt.Host.AdvertiseIP.String(), opt.Host.Hostname),
131+
Id: idgen.HostIDV2(opt.Host.AdvertiseIP.String(), opt.Host.Hostname, opt.Scheduler.Manager.SeedPeer.Enable),
132132
Ip: opt.Host.AdvertiseIP.String(),
133133
RpcPort: int32(opt.Download.PeerGRPC.TCPListen.PortRange.Start),
134134
DownPort: 0,

manager/job/sync_peers.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"d7y.io/dragonfly/v2/manager/config"
3434
"d7y.io/dragonfly/v2/manager/models"
3535
"d7y.io/dragonfly/v2/pkg/idgen"
36+
"d7y.io/dragonfly/v2/pkg/types"
3637
resource "d7y.io/dragonfly/v2/scheduler/resource/standard"
3738
)
3839

@@ -196,7 +197,11 @@ func (s *syncPeers) mergePeers(ctx context.Context, scheduler models.Scheduler,
196197

197198
// If the peer exists in the sync peer results, update the peer data in the database with
198199
// the sync peer results and delete the sync peer from the sync peers map.
199-
id := idgen.HostIDV2(peer.IP, peer.Hostname)
200+
isSeedPeer := false
201+
if types.ParseHostType(peer.Type) != types.HostTypeNormal {
202+
isSeedPeer = true
203+
}
204+
id := idgen.HostIDV2(peer.IP, peer.Hostname, isSeedPeer)
200205
if syncPeer, ok := syncPeers[id]; ok {
201206
if err := s.db.WithContext(ctx).First(&models.Peer{}, peer.ID).Updates(models.Peer{
202207
Type: syncPeer.Type.Name(),

pkg/idgen/host_id.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ package idgen
1818

1919
import (
2020
"fmt"
21-
22-
"d7y.io/dragonfly/v2/pkg/digest"
2321
)
2422

2523
// HostIDV1 generates v1 version of host id.
@@ -28,6 +26,9 @@ func HostIDV1(hostname string, port int32) string {
2826
}
2927

3028
// HostIDV2 generates v2 version of host id.
31-
func HostIDV2(ip, hostname string) string {
32-
return digest.SHA256FromStrings(ip, hostname)
29+
func HostIDV2(ip, hostname string, isSeedPeer bool) string {
30+
if isSeedPeer {
31+
return fmt.Sprintf("%s-%s-seed", ip, hostname)
32+
}
33+
return fmt.Sprintf("%s-%s", ip, hostname)
3334
}

pkg/idgen/host_id_test.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,43 +67,57 @@ func TestHostIDV1(t *testing.T) {
6767

6868
func TestHostIDV2(t *testing.T) {
6969
tests := []struct {
70-
name string
71-
ip string
72-
hostname string
73-
expect func(t *testing.T, d string)
70+
name string
71+
ip string
72+
hostname string
73+
isSeedPeer bool
74+
expect func(t *testing.T, d string)
7475
}{
7576
{
76-
name: "generate HostID",
77-
ip: "127.0.0.1",
78-
hostname: "foo",
77+
name: "generate HostID for peer",
78+
ip: "127.0.0.1",
79+
hostname: "foo",
80+
isSeedPeer: false,
7981
expect: func(t *testing.T, d string) {
8082
assert := assert.New(t)
81-
assert.Equal(d, "52727e8408e0ee1f999086f241ec43d5b3dbda666f1a06ef1fcbe75b4e90fa17")
83+
assert.Equal(d, "127.0.0.1-foo")
8284
},
8385
},
8486
{
85-
name: "generate HostID with empty ip",
86-
ip: "",
87-
hostname: "foo",
87+
name: "generate HostID for seed peer",
88+
ip: "127.0.0.1",
89+
hostname: "foo",
90+
isSeedPeer: true,
8891
expect: func(t *testing.T, d string) {
8992
assert := assert.New(t)
90-
assert.Equal(d, "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae")
93+
assert.Equal(d, "127.0.0.1-foo-seed")
9194
},
9295
},
9396
{
94-
name: "generate HostID with empty host",
95-
ip: "127.0.0.1",
96-
hostname: "",
97+
name: "generate HostID with empty ip for seed peer",
98+
ip: "",
99+
hostname: "foo",
100+
isSeedPeer: true,
101+
expect: func(t *testing.T, d string) {
102+
assert := assert.New(t)
103+
assert.Equal(d, "-foo-seed")
104+
},
105+
},
106+
{
107+
name: "generate HostID with empty host for seed peer",
108+
ip: "127.0.0.1",
109+
hostname: "",
110+
isSeedPeer: true,
97111
expect: func(t *testing.T, d string) {
98112
assert := assert.New(t)
99-
assert.Equal(d, "12ca17b49af2289436f303e0166030a21e525d266e209267433801a8fd4071a0")
113+
assert.Equal(d, "127.0.0.1--seed")
100114
},
101115
},
102116
}
103117

104118
for _, tc := range tests {
105119
t.Run(tc.name, func(t *testing.T) {
106-
tc.expect(t, HostIDV2(tc.ip, tc.hostname))
120+
tc.expect(t, HostIDV2(tc.ip, tc.hostname, tc.isSeedPeer))
107121
})
108122
}
109123
}

scheduler/resource/standard/host.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,7 @@ func (h *Host) LeavePeers() {
451451
func (h *Host) FreeUploadCount() int32 {
452452
return h.ConcurrentUploadLimit.Load() - h.ConcurrentUploadCount.Load()
453453
}
454+
455+
func (h *Host) IsSeedPeer() bool {
456+
return h.Type == types.HostTypeSuperSeed || h.Type == types.HostTypeStrongSeed || h.Type == types.HostTypeWeakSeed
457+
}

scheduler/resource/standard/host_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ var (
132132

133133
mockAnnounceInterval = 5 * time.Minute
134134

135-
mockHostID = idgen.HostIDV2("127.0.0.1", "foo")
136-
mockSeedHostID = idgen.HostIDV2("127.0.0.1", "bar")
135+
mockHostID = idgen.HostIDV2("127.0.0.1", "foo", false)
136+
mockSeedHostID = idgen.HostIDV2("127.0.0.1", "bar", true)
137137
mockHostLocation = "baz"
138138
mockHostIDC = "bas"
139139
)

scheduler/resource/standard/seed_peer_client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ package standard
2121
import (
2222
"context"
2323
"fmt"
24-
reflect "reflect"
24+
"reflect"
2525

2626
"github.com/hashicorp/go-multierror"
2727
"google.golang.org/grpc"
@@ -156,7 +156,7 @@ func (sc *seedPeerClient) updateSeedPeersForHostManager(seedPeers []*managerv2.S
156156
concurrentUploadLimit = int32(config.LoadLimit)
157157
}
158158

159-
id := idgen.HostIDV2(seedPeer.Ip, seedPeer.Hostname)
159+
id := idgen.HostIDV2(seedPeer.Ip, seedPeer.Hostname, true)
160160
seedPeerHost, loaded := sc.hostManager.Load(id)
161161
if !loaded {
162162
options := []HostOption{WithNetwork(Network{

scheduler/scheduling/evaluator/evaluator_base_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ var (
141141
mockTaskFilteredQueryParams = []string{"bar"}
142142
mockTaskHeader = map[string]string{"content-length": "100"}
143143
mockTaskPieceLength int32 = 2048
144-
mockHostID = idgen.HostIDV2("127.0.0.1", "foo")
145-
mockSeedHostID = idgen.HostIDV2("127.0.0.1", "bar")
144+
mockHostID = idgen.HostIDV2("127.0.0.1", "foo", false)
145+
mockSeedHostID = idgen.HostIDV2("127.0.0.1", "bar", true)
146146
mockHostLocation = "bas"
147147
mockHostIDC = "baz"
148148
mockPeerID = idgen.PeerIDV2()

scheduler/scheduling/scheduling_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ var (
170170
mockTaskFilteredQueryParams = []string{"bar"}
171171
mockTaskHeader = map[string]string{"content-length": "100"}
172172
mockTaskPieceLength int32 = 2048
173-
mockHostID = idgen.HostIDV2("127.0.0.1", "foo")
174-
mockSeedHostID = idgen.HostIDV2("127.0.0.1", "bar")
173+
mockHostID = idgen.HostIDV2("127.0.0.1", "foo", false)
174+
mockSeedHostID = idgen.HostIDV2("127.0.0.1", "bar", true)
175175
mockHostLocation = "baz"
176176
mockHostIDC = "bas"
177177
mockPeerID = idgen.PeerIDV2()
@@ -1040,7 +1040,7 @@ func TestScheduling_FindCandidateParents(t *testing.T) {
10401040
var mockPeers []*resource.Peer
10411041
for i := 0; i < 11; i++ {
10421042
mockHost := resource.NewHost(
1043-
idgen.HostIDV2("127.0.0.1", uuid.New().String()), mockRawHost.IP, mockRawHost.Hostname,
1043+
idgen.HostIDV2("127.0.0.1", uuid.New().String(), false), mockRawHost.IP, mockRawHost.Hostname,
10441044
mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type)
10451045
peer := resource.NewPeer(idgen.PeerIDV1(fmt.Sprintf("127.0.0.%d", i)), mockTask, mockHost)
10461046
mockPeers = append(mockPeers, peer)
@@ -1357,7 +1357,7 @@ func TestScheduling_FindParentAndCandidateParents(t *testing.T) {
13571357
var mockPeers []*resource.Peer
13581358
for i := 0; i < 11; i++ {
13591359
mockHost := resource.NewHost(
1360-
idgen.HostIDV2("127.0.0.1", uuid.New().String()), mockRawHost.IP, mockRawHost.Hostname,
1360+
idgen.HostIDV2("127.0.0.1", uuid.New().String(), false), mockRawHost.IP, mockRawHost.Hostname,
13611361
mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type)
13621362
peer := resource.NewPeer(idgen.PeerIDV1(fmt.Sprintf("127.0.0.%d", i)), mockTask, mockHost)
13631363
mockPeers = append(mockPeers, peer)
@@ -1618,7 +1618,7 @@ func TestScheduling_FindSuccessParent(t *testing.T) {
16181618
var mockPeers []*resource.Peer
16191619
for i := 0; i < 11; i++ {
16201620
mockHost := resource.NewHost(
1621-
idgen.HostIDV2("127.0.0.1", uuid.New().String()), mockRawHost.IP, mockRawHost.Hostname,
1621+
idgen.HostIDV2("127.0.0.1", uuid.New().String(), false), mockRawHost.IP, mockRawHost.Hostname,
16221622
mockRawHost.Port, mockRawHost.DownloadPort, mockRawHost.Type)
16231623
peer := resource.NewPeer(idgen.PeerIDV1(fmt.Sprintf("127.0.0.%d", i)), mockTask, mockHost)
16241624
mockPeers = append(mockPeers, peer)

scheduler/service/service_v1_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ var (
192192
mockTaskFilteredQueryParams = []string{"bar"}
193193
mockTaskHeader = map[string]string{"Content-Length": "100", "Range": "bytes=0-99"}
194194
mockTaskPieceLength int32 = 2048
195-
mockHostID = idgen.HostIDV2("127.0.0.1", "foo")
196-
mockSeedHostID = idgen.HostIDV2("127.0.0.1", "bar")
195+
mockHostID = idgen.HostIDV2("127.0.0.1", "foo", false)
196+
mockSeedHostID = idgen.HostIDV2("127.0.0.1", "bar", true)
197197
mockHostLocation = "bas"
198198
mockHostIDC = "baz"
199199
mockPeerID = idgen.PeerIDV2()
@@ -2559,7 +2559,7 @@ func TestServiceV1_LeaveHost(t *testing.T) {
25592559

25602560
tc.mock(host, mockPeer, hostManager, scheduling.EXPECT(), res.EXPECT(), hostManager.EXPECT())
25612561
tc.expect(t, mockPeer, svc.LeaveHost(context.Background(), &schedulerv1.LeaveHostRequest{
2562-
Id: idgen.HostIDV2(host.IP, host.Hostname),
2562+
Id: idgen.HostIDV2(host.IP, host.Hostname, true),
25632563
}))
25642564
})
25652565
}

0 commit comments

Comments
 (0)