This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 773
/
manager.go
261 lines (217 loc) · 7.99 KB
/
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Copyright 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.
*/
package dfgettask
import (
"context"
"fmt"
"strings"
"github.com/dragonflyoss/Dragonfly/apis/types"
"github.com/dragonflyoss/Dragonfly/pkg/errortypes"
"github.com/dragonflyoss/Dragonfly/pkg/metricsutils"
"github.com/dragonflyoss/Dragonfly/pkg/stringutils"
"github.com/dragonflyoss/Dragonfly/pkg/syncmap"
"github.com/dragonflyoss/Dragonfly/supernode/config"
"github.com/dragonflyoss/Dragonfly/supernode/daemon/mgr"
dutil "github.com/dragonflyoss/Dragonfly/supernode/daemon/util"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)
var _ mgr.DfgetTaskMgr = &Manager{}
const (
keyJoinChar = "@"
)
type metrics struct {
dfgetTasks *prometheus.GaugeVec
dfgetTasksRegisterCount *prometheus.CounterVec
dfgetTasksFailCount *prometheus.CounterVec
}
func newMetrics(register prometheus.Registerer) *metrics {
return &metrics{
dfgetTasks: metricsutils.NewGauge(config.SubsystemSupernode, "dfgettasks",
"Current status of dfgettasks", []string{"callsystem", "status"}, register),
dfgetTasksRegisterCount: metricsutils.NewCounter(config.SubsystemSupernode, "dfgettasks_registered_total",
"Total times of registering dfgettasks", []string{"callsystem"}, register),
dfgetTasksFailCount: metricsutils.NewCounter(config.SubsystemSupernode, "dfgettasks_failed_total",
"Total failure times of dfgettasks", []string{"callsystem"}, register),
}
}
// Manager is an implementation of the interface of DfgetTaskMgr.
type Manager struct {
cfg *config.Config
dfgetTaskStore *dutil.Store
ptoc *syncmap.SyncMap
metrics *metrics
}
// NewManager returns a new Manager.
func NewManager(cfg *config.Config, register prometheus.Registerer) (*Manager, error) {
return &Manager{
cfg: cfg,
dfgetTaskStore: dutil.NewStore(),
ptoc: syncmap.NewSyncMap(),
metrics: newMetrics(register),
}, nil
}
// Add a new dfgetTask, we use clientID and taskID to identify a dfgetTask uniquely.
// ClientID should be generated by dfget, supernode will use it directly.
// NOTE: We should create a new dfgetTask for each download process,
// even if the downloads initiated by the same machine.
func (dtm *Manager) Add(ctx context.Context, dfgetTask *types.DfGetTask) error {
if stringutils.IsEmptyStr(dfgetTask.Path) {
return errors.Wrapf(errortypes.ErrEmptyValue, "Path")
}
if stringutils.IsEmptyStr(dfgetTask.PeerID) {
return errors.Wrapf(errortypes.ErrEmptyValue, "PeerID")
}
key, err := generateKey(dfgetTask.CID, dfgetTask.TaskID)
if err != nil {
return err
}
// The default status of DfgetTask is WAITING.
if stringutils.IsEmptyStr(dfgetTask.Status) {
dfgetTask.Status = types.DfGetTaskStatusWAITING
}
// TODO: should we verify that the peerID is valid here.
dtm.ptoc.Add(generatePeerKey(dfgetTask.PeerID, dfgetTask.TaskID), dfgetTask.CID)
dtm.dfgetTaskStore.Put(key, dfgetTask)
// If dfget task is created by supernode cdn, don't update metrics.
if !dtm.cfg.IsSuperPID(dfgetTask.PeerID) || !dtm.cfg.IsSuperCID(dfgetTask.CID) {
dtm.metrics.dfgetTasks.WithLabelValues(dfgetTask.CallSystem, dfgetTask.Status).Inc()
dtm.metrics.dfgetTasksRegisterCount.WithLabelValues(dfgetTask.CallSystem).Inc()
}
return nil
}
// Get a dfgetTask info with specified clientID and taskID.
func (dtm *Manager) Get(ctx context.Context, clientID, taskID string) (dfgetTask *types.DfGetTask, err error) {
return dtm.getDfgetTask(clientID, taskID)
}
// GetCIDByPeerIDAndTaskID returns cid with specified peerID and taskID.
func (dtm *Manager) GetCIDByPeerIDAndTaskID(ctx context.Context, peerID, taskID string) (string, error) {
return dtm.ptoc.GetAsString(generatePeerKey(peerID, taskID))
}
// GetCIDsByTaskID returns cids as a string slice with specified taskID.
func (dtm *Manager) GetCIDsByTaskID(ctx context.Context, taskID string) ([]string, error) {
var result []string
suffixString := keyJoinChar + taskID
rangeFunc := func(k, v interface{}) bool {
key, ok := k.(string)
if !ok {
return true
}
if !strings.HasSuffix(key, suffixString) {
return true
}
cid, err := dtm.ptoc.GetAsString(key)
if err != nil {
logrus.Warnf("failed to get cid from ptoc with key(%s): %v", key, err)
return true
}
result = append(result, cid)
return true
}
dtm.ptoc.Range(rangeFunc)
return result, nil
}
// GetCIDAndTaskIDsByPeerID returns a cid<->taskID map by specified peerID.
func (dtm *Manager) GetCIDAndTaskIDsByPeerID(ctx context.Context, peerID string) (map[string]string, error) {
var result = make(map[string]string)
prefixStr := peerID + keyJoinChar
rangeFunc := func(k, v interface{}) bool {
key, ok := k.(string)
if !ok {
return true
}
if !strings.HasPrefix(key, prefixStr) {
return true
}
cid, err := dtm.ptoc.GetAsString(key)
if err != nil {
logrus.Warnf("failed to get cid from ptoc with key(%s): %v", key, err)
return true
}
// get TaskID from the key
splitResult := strings.Split(key, keyJoinChar)
result[cid] = splitResult[len(splitResult)-1]
return true
}
dtm.ptoc.Range(rangeFunc)
return result, nil
}
// List returns the list of dfgetTask.
func (dtm *Manager) List(ctx context.Context, filter map[string]string) (dfgetTaskList []*types.DfGetTask, err error) {
return nil, nil
}
// Delete deletes a dfgetTask with clientID and taskID.
func (dtm *Manager) Delete(ctx context.Context, clientID, taskID string) error {
key, err := generateKey(clientID, taskID)
if err != nil {
return err
}
dfgetTask, err := dtm.getDfgetTask(clientID, taskID)
if err != nil {
return err
}
dtm.ptoc.Delete(generatePeerKey(dfgetTask.PeerID, dfgetTask.TaskID))
if !dtm.cfg.IsSuperCID(clientID) {
dtm.metrics.dfgetTasks.WithLabelValues(dfgetTask.CallSystem, dfgetTask.Status).Dec()
}
return dtm.dfgetTaskStore.Delete(key)
}
// UpdateStatus updates the status of dfgetTask with specified clientID and taskID.
func (dtm *Manager) UpdateStatus(ctx context.Context, clientID, taskID, status string) error {
dfgetTask, err := dtm.getDfgetTask(clientID, taskID)
if err != nil {
return err
}
if dfgetTask.Status != types.DfGetTaskStatusSUCCESS {
dtm.metrics.dfgetTasks.WithLabelValues(dfgetTask.CallSystem, dfgetTask.Status).Dec()
dtm.metrics.dfgetTasks.WithLabelValues(dfgetTask.CallSystem, status).Inc()
dfgetTask.Status = status
}
// Add the total failed count.
if dfgetTask.Status == types.DfGetTaskStatusFAILED {
dtm.metrics.dfgetTasksFailCount.WithLabelValues(dfgetTask.CallSystem).Inc()
}
return nil
}
// getDfgetTask gets a DfGetTask from dfgetTaskStore with specified clientID and taskID.
func (dtm *Manager) getDfgetTask(clientID, taskID string) (*types.DfGetTask, error) {
key, err := generateKey(clientID, taskID)
if err != nil {
return nil, err
}
v, err := dtm.dfgetTaskStore.Get(key)
if err != nil {
return nil, err
}
if dfgetTask, ok := v.(*types.DfGetTask); ok {
return dfgetTask, nil
}
return nil, errors.Wrapf(errortypes.ErrConvertFailed, "clientID: %s, taskID: %s: %v", clientID, taskID, v)
}
// generateKey generates a key for a dfgetTask.
func generateKey(cID, taskID string) (string, error) {
if stringutils.IsEmptyStr(cID) {
return "", errors.Wrapf(errortypes.ErrEmptyValue, "cID")
}
if stringutils.IsEmptyStr(taskID) {
return "", errors.Wrapf(errortypes.ErrEmptyValue, "taskID")
}
return fmt.Sprintf("%s%s%s", cID, keyJoinChar, taskID), nil
}
func generatePeerKey(peerID, taskID string) string {
return fmt.Sprintf("%s%s%s", peerID, keyJoinChar, taskID)
}