-
Notifications
You must be signed in to change notification settings - Fork 34
/
filemaintainer.go
144 lines (118 loc) · 4.4 KB
/
filemaintainer.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
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
// 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 filemaintainer
import (
"context"
"io"
"time"
"github.com/sirupsen/logrus"
"github.com/PaddlePaddle/PaddleDTX/xdb/blockchain"
"github.com/PaddlePaddle/PaddleDTX/xdb/config"
ctype "github.com/PaddlePaddle/PaddleDTX/xdb/engine/challenger/merkle/types"
"github.com/PaddlePaddle/PaddleDTX/xdb/engine/common"
"github.com/PaddlePaddle/PaddleDTX/xdb/engine/copier"
"github.com/PaddlePaddle/PaddleDTX/xdb/engine/encryptor"
"github.com/PaddlePaddle/PaddleDTX/xdb/engine/types"
"github.com/PaddlePaddle/PaddleDTX/xdb/peer"
)
const (
// Defines the default interval for files migration
defaultFileMigrateInterval = time.Hour * 1
)
var (
logger = logrus.WithField("monitor", "filemaintainer")
)
type Copier interface {
Push(ctx context.Context, id, sourceID string, r io.Reader, node *blockchain.Node) (string, error)
Pull(ctx context.Context, id, storIndex, fileID string, node *blockchain.Node) (io.ReadCloser, error)
ReplicaExpansion(ctx context.Context, opt *copier.ReplicaExpOptions, enc common.CommonEncryptor,
challengeAlgorithm, sourceID, fileID string) ([]blockchain.PublicSliceMeta, []encryptor.EncryptedSlice, error)
}
type Encryptor interface {
Encrypt(r io.Reader, opt *encryptor.EncryptOptions) (encryptor.EncryptedSlice, error)
Recover(r io.Reader, opt *encryptor.RecoverOptions) ([]byte, error)
}
type Blockchain interface {
PublishFile(file *blockchain.PublishFileOptions) error
ListFiles(opt *blockchain.ListFileOptions) ([]blockchain.File, error)
GetFileByID(id string) (blockchain.File, error)
ListFileNs(opt *blockchain.ListNsOptions) ([]blockchain.Namespace, error)
UpdateFilePublicSliceMeta(opt *blockchain.UpdateFilePSMOptions) error
SliceMigrateRecord(opt *blockchain.SliceMigrateOptions) error
ListNodes() (blockchain.Nodes, error)
GetNode(id []byte) (blockchain.Node, error)
GetNodeHealth(id []byte) (string, error)
GetHeartbeatNum(id []byte, timestamp int64) (int, error)
}
type Challenger interface {
// merkle Challenge
Setup(sliceData []byte, rangeAmount int) ([]ctype.RangeHash, error)
Save(cms []ctype.Material) error
Take(fileID string, sliceID string, nodeID []byte) (ctype.RangeHash, error)
GetChallengeConf() (string, types.PairingChallengeConf)
Close()
}
type NewFileMaintainerOptions struct {
LocalNode peer.Local
Blockchain Blockchain
Copier Copier
Encryptor Encryptor
Challenger Challenger
}
// FileMaintainer runs if local node is dataOwner-node, and its main work is to check storage-nodes health conditions
// and migrate slices from bad nodes to healthy nodes.
type FileMaintainer struct {
localNode peer.Local
blockchain Blockchain
copier Copier
encryptor Encryptor
challenger Challenger
challengerInterval int64
fileMigrateInterval time.Duration
doneMigrateC chan struct{} //doneMigrateC will be closed when loop breaks
}
func New(conf *config.MonitorConf, opt *NewFileMaintainerOptions, interval int64) (*FileMaintainer, error) {
fileMigrateInterval := time.Duration(conf.FilemigrateInterval) * time.Hour
if fileMigrateInterval == 0 {
fileMigrateInterval = defaultFileMigrateInterval
}
logger.WithFields(logrus.Fields{
"filemigrate-interval": fileMigrateInterval,
}).Info("monitor initialize...")
return &FileMaintainer{
localNode: opt.LocalNode,
blockchain: opt.Blockchain,
copier: opt.Copier,
encryptor: opt.Encryptor,
challenger: opt.Challenger,
challengerInterval: interval,
fileMigrateInterval: fileMigrateInterval,
}, nil
}
// Migrate starts file migration
func (m *FileMaintainer) Migrate(ctx context.Context) {
go m.migrate(ctx)
}
// StopMigrate stops file migration
func (m *FileMaintainer) StopMigrate() {
if m.doneMigrateC == nil {
return
}
logger.Info("stops file migration ...")
select {
case <-m.doneMigrateC:
return
default:
}
<-m.doneMigrateC
}