/
scheduer.go
170 lines (144 loc) · 4.63 KB
/
scheduer.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
package modules
import (
"context"
"github.com/Filecoin-Titan/titan/api"
"github.com/Filecoin-Titan/titan/api/types"
"github.com/Filecoin-Titan/titan/lib/etcdcli"
"github.com/Filecoin-Titan/titan/node/config"
"github.com/Filecoin-Titan/titan/node/modules/dtypes"
"github.com/Filecoin-Titan/titan/node/modules/helpers"
"github.com/Filecoin-Titan/titan/node/repo"
"github.com/Filecoin-Titan/titan/node/scheduler/assets"
"github.com/Filecoin-Titan/titan/node/scheduler/db"
"github.com/Filecoin-Titan/titan/node/scheduler/leadership"
"github.com/Filecoin-Titan/titan/node/scheduler/validation"
"github.com/Filecoin-Titan/titan/node/sqldb"
"github.com/filecoin-project/go-jsonrpc/auth"
"github.com/filecoin-project/pubsub"
"github.com/google/uuid"
logging "github.com/ipfs/go-log/v2"
"go.uber.org/fx"
"golang.org/x/xerrors"
"github.com/Filecoin-Titan/titan/node/common"
"github.com/Filecoin-Titan/titan/node/scheduler/node"
"github.com/jmoiron/sqlx"
)
var log = logging.Logger("modules")
// NewDB returns an *sqlx.DB instance
func NewDB(cfg *config.SchedulerCfg) (*sqlx.DB, error) {
return sqldb.NewDB(cfg.DatabaseAddress)
}
// GenerateTokenWithWebPermission create a new token based on the given permissions
func GenerateTokenWithWebPermission(ca *common.CommonAPI) (dtypes.PermissionWebToken, error) {
token, err := ca.AuthNew(context.Background(), &types.JWTPayload{Allow: []auth.Permission{api.RoleWeb}, ID: uuid.NewString()})
if err != nil {
return "", err
}
return dtypes.PermissionWebToken(token), nil
}
// StorageManagerParams Manager Params
type StorageManagerParams struct {
fx.In
Lifecycle fx.Lifecycle
MetricsCtx helpers.MetricsCtx
MetadataDS dtypes.MetadataDS
NodeManger *node.Manager
dtypes.GetSchedulerConfigFunc
*db.SQLDB
}
// NewStorageManager creates a new storage manager instance
func NewStorageManager(params StorageManagerParams) *assets.Manager {
var (
mctx = params.MetricsCtx
lc = params.Lifecycle
nodeMgr = params.NodeManger
ds = params.MetadataDS
cfgFunc = params.GetSchedulerConfigFunc
sdb = params.SQLDB
)
ctx := helpers.LifecycleCtx(mctx, lc)
m := assets.NewManager(nodeMgr, ds, cfgFunc, sdb)
lc.Append(fx.Hook{
OnStart: func(context.Context) error {
go m.Start(ctx)
return nil
},
OnStop: m.Terminate,
})
return m
}
// NewValidation creates a new validation manager instance
func NewValidation(mctx helpers.MetricsCtx, l fx.Lifecycle, nm *node.Manager, am *assets.Manager, configFunc dtypes.GetSchedulerConfigFunc, p *pubsub.PubSub, lmgr *leadership.Manager) *validation.Manager {
v := validation.NewManager(nm, am, configFunc, p, lmgr)
ctx := helpers.LifecycleCtx(mctx, l)
l.Append(fx.Hook{
OnStart: func(context.Context) error {
go v.Start(ctx)
return nil
},
OnStop: v.Stop,
})
return v
}
// NewSetSchedulerConfigFunc creates a function to set the scheduler config
func NewSetSchedulerConfigFunc(r repo.LockedRepo) func(config.SchedulerCfg) error {
return func(cfg config.SchedulerCfg) (err error) {
return r.SetConfig(func(raw interface{}) {
scfg, ok := raw.(*config.SchedulerCfg)
if !ok {
return
}
scfg.EnableValidation = cfg.EnableValidation
})
}
}
// NewGetSchedulerConfigFunc creates a function to get the scheduler config
func NewGetSchedulerConfigFunc(r repo.LockedRepo) func() (config.SchedulerCfg, error) {
return func() (out config.SchedulerCfg, err error) {
raw, err := r.Config()
if err != nil {
return
}
scfg, ok := raw.(*config.SchedulerCfg)
if !ok {
return
}
out = *scfg
return
}
}
// NewPubSub returns a new pubsub instance with a buffer of 50
func NewPubSub() *pubsub.PubSub {
return pubsub.New(50)
}
// RegisterToEtcd registers the server to etcd
func RegisterToEtcd(mctx helpers.MetricsCtx, lc fx.Lifecycle, configFunc dtypes.GetSchedulerConfigFunc, serverID dtypes.ServerID, token dtypes.PermissionWebToken) (*etcdcli.Client, error) {
cfg, err := configFunc()
if err != nil {
return nil, err
}
sCfg := &types.SchedulerCfg{
AreaID: cfg.AreaID,
SchedulerURL: cfg.ExternalURL,
AccessToken: string(token),
Weight: cfg.Weight,
}
value, err := etcdcli.SCMarshal(sCfg)
if err != nil {
return nil, xerrors.Errorf("cfg SCMarshal err:%s", err.Error())
}
eCli, err := etcdcli.New(cfg.EtcdAddresses)
if err != nil {
return nil, err
}
ctx := helpers.LifecycleCtx(mctx, lc)
lc.Append(fx.Hook{
OnStart: func(context.Context) error {
return eCli.ServerRegister(ctx, string(serverID), types.RunningNodeType.String(), string(value))
},
OnStop: func(context.Context) error {
return eCli.ServerUnRegister(ctx, string(serverID), types.RunningNodeType.String())
},
})
return eCli, nil
}