-
Notifications
You must be signed in to change notification settings - Fork 20
/
impl.go
431 lines (349 loc) · 12.5 KB
/
impl.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package scheduler
import (
"bytes"
"context"
"crypto"
"crypto/rsa"
"database/sql"
"encoding/gob"
"encoding/hex"
"fmt"
"io"
"net"
"time"
"github.com/Filecoin-Titan/titan/node/modules/dtypes"
"github.com/Filecoin-Titan/titan/node/scheduler/nat"
"github.com/Filecoin-Titan/titan/node/scheduler/validation"
"github.com/Filecoin-Titan/titan/node/scheduler/workload"
"github.com/docker/go-units"
"github.com/quic-go/quic-go"
"go.uber.org/fx"
"github.com/Filecoin-Titan/titan/node/config"
"github.com/Filecoin-Titan/titan/node/scheduler/assets"
"github.com/Filecoin-Titan/titan/api"
"github.com/Filecoin-Titan/titan/api/types"
"github.com/Filecoin-Titan/titan/node/common"
"github.com/Filecoin-Titan/titan/node/handler"
"github.com/Filecoin-Titan/titan/node/scheduler/node"
logging "github.com/ipfs/go-log/v2"
titanrsa "github.com/Filecoin-Titan/titan/node/rsa"
sSync "github.com/Filecoin-Titan/titan/node/scheduler/sync"
"golang.org/x/xerrors"
)
var log = logging.Logger("scheduler")
const (
cpuLimit = 140
memoryLimit = 2250 * units.GiB
diskSpaceLimit = 500 * units.TiB
bandwidthUpLimit = 200 * units.MiB
)
// Scheduler represents a scheduler node in a distributed system.
type Scheduler struct {
fx.In
*common.CommonAPI
*EdgeUpdateManager
dtypes.ServerID
NodeManager *node.Manager
ValidationMgr *validation.Manager
AssetManager *assets.Manager
NatManager *nat.Manager
DataSync *sSync.DataSync
SchedulerCfg *config.SchedulerCfg
SetSchedulerConfigFunc dtypes.SetSchedulerConfigFunc
GetSchedulerConfigFunc dtypes.GetSchedulerConfigFunc
WorkloadManager *workload.Manager
PrivateKey *rsa.PrivateKey
Transport *quic.Transport
}
var _ api.Scheduler = &Scheduler{}
// nodeConnect processes a node connect request with the given options and node type.
func (s *Scheduler) nodeConnect(ctx context.Context, opts *types.ConnectOptions, nodeType types.NodeType) error {
remoteAddr := handler.GetRemoteAddr(ctx)
nodeID := handler.GetNodeID(ctx)
alreadyConnect := true
cNode := s.NodeManager.GetNode(nodeID)
if cNode == nil {
if err := s.NodeManager.NodeExists(nodeID, nodeType); err != nil {
return xerrors.Errorf("node: %s, type: %d, error: %w", nodeID, nodeType, err)
}
cNode = node.New()
alreadyConnect = false
}
if cNode.ExternalIP != "" {
s.NodeManager.RemoveNodeIP(nodeID, cNode.ExternalIP)
}
externalIP, _, err := net.SplitHostPort(remoteAddr)
if err != nil {
return xerrors.Errorf("SplitHostPort err:%s", err.Error())
}
if !s.NodeManager.CheckNodeIP(nodeID, externalIP) {
return xerrors.Errorf("%s The number of IPs exceeds the limit", externalIP)
}
defer func() {
if err != nil {
s.NodeManager.RemoveNodeIP(nodeID, externalIP)
}
}()
cNode.SetToken(opts.Token)
cNode.RemoteAddr = remoteAddr
cNode.ExternalURL = opts.ExternalURL
cNode.TCPPort = opts.TcpServerPort
cNode.IsPrivateMinioOnly = opts.IsPrivateMinioOnly
log.Infof("node connected %s, address:%s , %v", nodeID, remoteAddr, alreadyConnect)
err = cNode.ConnectRPC(s.Transport, remoteAddr, nodeType)
if err != nil {
return xerrors.Errorf("nodeConnect ConnectRPC err:%s", err.Error())
}
// init node info
nodeInfo, err := cNode.API.GetNodeInfo(context.Background())
if err != nil {
log.Errorf("nodeConnect NodeInfo err:%s", err.Error())
return err
}
if nodeID != nodeInfo.NodeID {
return xerrors.Errorf("nodeID mismatch %s, %s", nodeID, nodeInfo.NodeID)
}
nodeInfo.Type = nodeType
nodeInfo.SchedulerID = s.ServerID
if nodeInfo.AvailableDiskSpace <= 0 {
nodeInfo.AvailableDiskSpace = 2 * units.GiB
}
if nodeInfo.AvailableDiskSpace > nodeInfo.DiskSpace {
nodeInfo.AvailableDiskSpace = nodeInfo.DiskSpace
}
if nodeType == types.NodeCandidate {
// isValidator, err := s.db.IsValidator(nodeID)
// if err != nil {
// return xerrors.Errorf("nodeConnect IsValidator err:%s", err.Error())
// }
// if isValidator {
// nodeType = types.NodeValidator
// }
nodeInfo.AvailableDiskSpace = nodeInfo.DiskSpace * 0.9
} else {
err = checkNodeParameters(&nodeInfo)
if err != nil {
return err
}
}
nodeInfo.ExternalIP = externalIP
nodeInfo.BandwidthUp = units.KiB
oldInfo, err := s.NodeManager.LoadNodeInfo(nodeID)
if err != nil && err != sql.ErrNoRows {
return xerrors.Errorf("load node online duration %s err : %s", nodeID, err.Error())
}
size, err := s.db.LoadReplicaSizeByNodeID(nodeID)
if err != nil {
return xerrors.Errorf("LoadReplicaSizeByNodeID %s err:%s", nodeID, err.Error())
}
nodeInfo.TitanDiskUsage = float64(size)
if nodeInfo.AvailableDiskSpace < float64(size) {
nodeInfo.AvailableDiskSpace = float64(roundUpToNextGB(size))
}
if oldInfo != nil {
// init node info
nodeInfo.PortMapping = oldInfo.PortMapping
nodeInfo.OnlineDuration = oldInfo.OnlineDuration
nodeInfo.BandwidthDown = oldInfo.BandwidthDown
nodeInfo.BandwidthUp = oldInfo.BandwidthUp
nodeInfo.DeactivateTime = oldInfo.DeactivateTime
if oldInfo.DeactivateTime > 0 && oldInfo.DeactivateTime < time.Now().Unix() {
return xerrors.Errorf("The node %s has been deactivate and cannot be logged in", nodeID)
}
}
cNode.OnlineDuration = nodeInfo.OnlineDuration
cNode.BandwidthDown = nodeInfo.BandwidthDown
cNode.BandwidthUp = nodeInfo.BandwidthUp
cNode.PortMapping = nodeInfo.PortMapping
cNode.DeactivateTime = nodeInfo.DeactivateTime
cNode.AvailableDiskSpace = nodeInfo.AvailableDiskSpace
cNode.NodeID = nodeInfo.NodeID
cNode.Type = nodeInfo.Type
cNode.ExternalIP = nodeInfo.ExternalIP
cNode.DiskSpace = nodeInfo.DiskSpace
cNode.TitanDiskUsage = nodeInfo.TitanDiskUsage
cNode.DiskUsage = nodeInfo.DiskUsage
cNode.IncomeIncr = (cNode.CalculateMCx(s.NodeManager.TotalNetworkEdges) * 360)
pCount, err := s.db.GetNodePullingCount(nodeID)
if err == nil {
cNode.PullAssetCount = int(pCount)
}
if !alreadyConnect {
pStr, err := s.NodeManager.LoadNodePublicKey(nodeID)
if err != nil && err != sql.ErrNoRows {
return xerrors.Errorf("load node port %s err : %s", nodeID, err.Error())
}
publicKey, err := titanrsa.Pem2PublicKey([]byte(pStr))
if err != nil {
return xerrors.Errorf("load node port %s err : %s", nodeID, err.Error())
}
cNode.PublicKey = publicKey
err = s.NodeManager.NodeOnline(cNode, &nodeInfo)
if err != nil {
log.Errorf("nodeConnect err:%s,nodeID:%s", err.Error(), nodeID)
return err
}
}
if nodeType == types.NodeEdge {
go s.NatManager.DetermineEdgeNATType(context.Background(), nodeID)
}
s.DataSync.AddNodeToList(nodeID)
return nil
}
func roundUpToNextGB(bytes int) int {
const GB = 1 << 30
if bytes%GB == 0 {
return bytes
}
return ((bytes / GB) + 1) * GB
}
func checkNodeParameters(nodeInfo *types.NodeInfo) error {
if nodeInfo.DiskSpace > diskSpaceLimit || nodeInfo.DiskSpace < 0 {
return xerrors.Errorf("checkNodeParameters [%s] DiskSpace [%.2f]", nodeInfo.NodeID, nodeInfo.DiskSpace)
}
// if nodeInfo.AvailableDiskSpace > nodeInfo.DiskSpace {
// return xerrors.Errorf("checkNodeParameters [%s] AvailableDiskSpace [%.2f] > DiskSpace [%.2f]", nodeInfo.NodeID, nodeInfo.AvailableDiskSpace, nodeInfo.DiskSpace)
// }
// if nodeInfo.AvailableDiskSpace < float64(tDiskUsage) {
// return xerrors.Errorf("checkNodeParameters [%s] AvailableDiskSpace [%.2f] < tDiskUsage [%d]", nodeInfo.NodeID, nodeInfo.AvailableDiskSpace, tDiskUsage)
// }
if nodeInfo.BandwidthDown < 0 {
return xerrors.Errorf("checkNodeParameters [%s] BandwidthDown [%d]", nodeInfo.NodeID, nodeInfo.BandwidthDown)
}
if nodeInfo.BandwidthUp > bandwidthUpLimit || nodeInfo.BandwidthUp < 0 {
return xerrors.Errorf("checkNodeParameters [%s] BandwidthUp [%d]", nodeInfo.NodeID, nodeInfo.BandwidthUp)
}
if nodeInfo.Memory > memoryLimit || nodeInfo.Memory < 0 {
return xerrors.Errorf("checkNodeParameters [%s] Memory [%.2f]", nodeInfo.NodeID, nodeInfo.Memory)
}
if nodeInfo.CPUCores > cpuLimit || nodeInfo.CPUCores < 0 {
return xerrors.Errorf("checkNodeParameters [%s] CPUCores [%d]", nodeInfo.NodeID, nodeInfo.CPUCores)
}
return nil
}
// NodeValidationResult processes the validation result for a node
func (s *Scheduler) NodeValidationResult(ctx context.Context, r io.Reader, sign string) error {
validator := handler.GetNodeID(ctx)
node := s.NodeManager.GetNode(validator)
if node == nil {
return fmt.Errorf("node %s not online", validator)
}
signBuf, err := hex.DecodeString(sign)
if err != nil {
return err
}
data, err := io.ReadAll(r)
if err != nil {
return err
}
rsa := titanrsa.New(crypto.SHA256, crypto.SHA256.New())
err = rsa.VerifySign(node.PublicKey, signBuf, data)
if err != nil {
return err
}
result := &api.ValidationResult{}
buffer := bytes.NewBuffer(data)
dec := gob.NewDecoder(buffer)
err = dec.Decode(result)
if err != nil {
return err
}
result.Validator = validator
s.ValidationMgr.PushResult(result)
return nil
}
// TriggerElection triggers a single election for validators.
func (s *Scheduler) TriggerElection(ctx context.Context) error {
s.ValidationMgr.StartElection()
return nil
}
// GetValidationResults retrieves a list of validation results.
func (s *Scheduler) GetValidationResults(ctx context.Context, nodeID string, limit, offset int) (*types.ListValidationResultRsp, error) {
svm, err := s.NodeManager.LoadValidationResultInfos(nodeID, limit, offset)
if err != nil {
return nil, err
}
return svm, nil
}
// GetSchedulerPublicKey get server publicKey
func (s *Scheduler) GetSchedulerPublicKey(ctx context.Context) (string, error) {
if s.PrivateKey == nil {
return "", fmt.Errorf("scheduler private key not exist")
}
publicKey := s.PrivateKey.PublicKey
pem := titanrsa.PublicKey2Pem(&publicKey)
return string(pem), nil
}
// GetNodePublicKey get node publicKey
func (s *Scheduler) GetNodePublicKey(ctx context.Context, nodeID string) (string, error) {
pem, err := s.NodeManager.LoadNodePublicKey(nodeID)
if err != nil {
return "", xerrors.Errorf("%s load node public key failed: %w", nodeID, err)
}
return string(pem), nil
}
// GetValidationInfo get information related to validation and election
func (s *Scheduler) GetValidationInfo(ctx context.Context) (*types.ValidationInfo, error) {
eTime := s.ValidationMgr.GetNextElectionTime()
return &types.ValidationInfo{
NextElectionTime: eTime,
}, nil
}
// SubmitUserWorkloadReport submits report of workload for User Asset Download
func (s *Scheduler) SubmitUserWorkloadReport(ctx context.Context, r io.Reader) error {
return nil
// nodeID := handler.GetNodeID(ctx)
// node := s.NodeManager.GetNode(nodeID)
// cipherText, err := io.ReadAll(r)
// if err != nil {
// return err
// }
// titanRsa := titanrsa.New(crypto.SHA256, crypto.SHA256.New())
// data, err := titanRsa.Decrypt(cipherText, s.PrivateKey)
// if err != nil {
// return xerrors.Errorf("decrypt error: %w", err)
// }
// return s.WorkloadManager.HandleUserWorkload(data, node)
}
// SubmitNodeWorkloadReport submits report of workload for node Asset Download
func (s *Scheduler) SubmitNodeWorkloadReport(ctx context.Context, r io.Reader) error {
nodeID := handler.GetNodeID(ctx)
node := s.NodeManager.GetNode(nodeID)
if node == nil {
return xerrors.Errorf("node %s not exists", nodeID)
}
report := &types.NodeWorkloadReport{}
dec := gob.NewDecoder(r)
err := dec.Decode(report)
if err != nil {
return xerrors.Errorf("decode data to NodeWorkloadReport error: %w", err)
}
titanRsa := titanrsa.New(crypto.SHA256, crypto.SHA256.New())
if err = titanRsa.VerifySign(node.PublicKey, report.Sign, report.CipherText); err != nil {
return xerrors.Errorf("verify sign error: %w", err)
}
data, err := titanRsa.Decrypt(report.CipherText, s.PrivateKey)
if err != nil {
return xerrors.Errorf("decrypt error: %w", err)
}
return s.WorkloadManager.PushResult(data, node)
}
// GetWorkloadRecords retrieves a list of workload results.
func (s *Scheduler) GetWorkloadRecords(ctx context.Context, nodeID string, limit, offset int) (*types.ListWorkloadRecordRsp, error) {
return s.NodeManager.LoadWorkloadRecords(nodeID, limit, offset)
}
// GetRetrieveEventRecords retrieves a list of retrieve events
func (s *Scheduler) GetRetrieveEventRecords(ctx context.Context, nodeID string, limit, offset int) (*types.ListRetrieveEventRsp, error) {
return s.NodeManager.LoadRetrieveEventRecords(nodeID, limit, offset)
}
// GetWorkloadRecord retrieves workload result.
func (s *Scheduler) GetWorkloadRecord(ctx context.Context, tokenID string) (*types.WorkloadRecord, error) {
return s.NodeManager.LoadWorkloadRecord(tokenID)
}
// ElectValidators elect validators
func (s *Scheduler) ElectValidators(ctx context.Context, nodeIDs []string) error {
if len(nodeIDs) == 0 {
return nil
}
return s.ValidationMgr.CompulsoryElection(nodeIDs)
}