-
Notifications
You must be signed in to change notification settings - Fork 266
/
host_manager.go
165 lines (134 loc) · 3.88 KB
/
host_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright 2020 The Dragonfly Authors
*
* Licensed 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.
*/
//go:generate mockgen -destination host_manager_mock.go -source host_manager.go -package resource
package resource
import (
"sync"
"d7y.io/dragonfly/v2/pkg/container/set"
pkggc "d7y.io/dragonfly/v2/pkg/gc"
"d7y.io/dragonfly/v2/pkg/types"
"d7y.io/dragonfly/v2/scheduler/config"
)
const (
// GC host id.
GCHostID = "host"
)
// HostManager is the interface used for host manager.
type HostManager interface {
// Load returns host for a key.
Load(string) (*Host, bool)
// Store sets host.
Store(*Host)
// LoadOrStore returns host the key if present.
// Otherwise, it stores and returns the given host.
// The loaded result is true if the host was loaded, false if stored.
LoadOrStore(*Host) (*Host, bool)
// Delete deletes host for a key.
Delete(string)
// Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration.
Range(f func(any, any) bool)
// LoadRandomHosts loads host randomly through the Range of sync.Map.
LoadRandomHosts(int, set.SafeSet[string]) []*Host
// Try to reclaim host.
RunGC() error
}
// hostManager contains content for host manager.
type hostManager struct {
// Host sync map.
*sync.Map
}
// New host manager interface.
func newHostManager(cfg *config.GCConfig, gc pkggc.GC) (HostManager, error) {
h := &hostManager{
Map: &sync.Map{},
}
if err := gc.Add(pkggc.Task{
ID: GCHostID,
Interval: cfg.HostGCInterval,
Timeout: cfg.HostGCInterval,
Runner: h,
}); err != nil {
return nil, err
}
return h, nil
}
// Load returns host for a key.
func (h *hostManager) Load(key string) (*Host, bool) {
rawHost, loaded := h.Map.Load(key)
if !loaded {
return nil, false
}
return rawHost.(*Host), loaded
}
// Store sets host.
func (h *hostManager) Store(host *Host) {
h.Map.Store(host.ID, host)
}
// LoadOrStore returns host the key if present.
// Otherwise, it stores and returns the given host.
// The loaded result is true if the host was loaded, false if stored.
func (h *hostManager) LoadOrStore(host *Host) (*Host, bool) {
rawHost, loaded := h.Map.LoadOrStore(host.ID, host)
return rawHost.(*Host), loaded
}
// Delete deletes host for a key.
func (h *hostManager) Delete(key string) {
h.Map.Delete(key)
}
// Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration.
func (h *hostManager) Range(f func(key, value any) bool) {
h.Map.Range(f)
}
// LoadRandomHosts loads host randomly through the Range of sync.Map.
func (h *hostManager) LoadRandomHosts(n int, blocklist set.SafeSet[string]) []*Host {
hosts := make([]*Host, 0, n)
h.Map.Range(func(key, value any) bool {
if len(hosts) >= n {
return false
}
host, ok := value.(*Host)
if !ok {
host.Log.Error("invalid host")
return true
}
if blocklist.Contains(host.ID) {
return true
}
hosts = append(hosts, host)
return true
})
return hosts
}
// Try to reclaim host.
func (h *hostManager) RunGC() error {
h.Map.Range(func(_, value any) bool {
host, ok := value.(*Host)
if !ok {
host.Log.Error("invalid host")
return true
}
if host.PeerCount.Load() == 0 &&
host.ConcurrentUploadCount.Load() == 0 &&
host.Type == types.HostTypeNormal {
host.Log.Info("host has been reclaimed")
h.Delete(host.ID)
}
return true
})
return nil
}