-
Notifications
You must be signed in to change notification settings - Fork 1
/
prophet.go
260 lines (222 loc) · 6.61 KB
/
prophet.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
package prophet
import (
"context"
"errors"
"math/rand"
"sync"
"time"
"github.com/deepfabric/prophet/cluster"
"github.com/deepfabric/prophet/config"
"github.com/deepfabric/prophet/core"
"github.com/deepfabric/prophet/election"
"github.com/deepfabric/prophet/join"
"github.com/deepfabric/prophet/member"
"github.com/deepfabric/prophet/option"
"github.com/deepfabric/prophet/pb/metapb"
"github.com/deepfabric/prophet/schedule/hbstream"
"github.com/deepfabric/prophet/storage"
"github.com/deepfabric/prophet/util"
"github.com/deepfabric/prophet/util/typeutil"
"github.com/fagongzi/goetty"
"github.com/fagongzi/util/task"
"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/embed"
)
var (
rootPath = "/prophet"
clusterIDPath = "/prophet/cluster_id"
)
// Prophet is the distributed scheduler and coordinator
type Prophet interface {
// Start start the prophet instance, this will start the lead election, heartbeat loop and listen requests
Start()
// Stop stop the prophet instance
Stop()
// GetStorage returns the storage
GetStorage() storage.Storage
// GetClient returns the prophet client
GetClient() Client
// GetLeader returns leader
GetLeader() *metapb.Member
// GetMember returns self
GetMember() *member.Member
// GetConfig returns cfg
GetConfig() *config.Config
// GetClusterID return cluster id
GetClusterID() uint64
}
type defaultProphet struct {
sync.Mutex
ctx context.Context
cancel context.CancelFunc
cfg *config.Config
persistOptions *config.PersistOptions
runner *task.Runner
wn *watcherNotifier
// about leader election
etcd *embed.Etcd
elector election.Elector
member *member.Member
notifyOnce sync.Once
completeC chan struct{}
// cluster
clusterID uint64
storage storage.Storage
basicCluster *core.BasicCluster
cluster *cluster.RaftCluster
// rpc
hbStreams *hbstream.HeartbeatStreams
trans goetty.NetApplication
client Client
}
// NewProphet returns a prophet instance
func NewProphet(cfg *config.Config) Prophet {
var elector election.Elector
var etcdClient *clientv3.Client
var etcd *embed.Etcd
var err error
ctx, cancel := context.WithCancel(context.Background())
if cfg.StorageNode {
join.PrepareJoinCluster(cfg)
etcdClient, etcd, err = startEmbedEtcd(ctx, cfg)
if err != nil {
util.GetLogger().Fatalf("start embed etcd failed with %+v", err)
}
} else {
etcdClient, err = clientv3.New(clientv3.Config{
Endpoints: cfg.ExternalEtcd,
AutoSyncInterval: time.Second * 30,
DialTimeout: etcdTimeout,
})
if err != nil {
util.GetLogger().Fatalf("create external etcd client failed with %+v", err)
}
}
elector, err = election.NewElector(etcdClient,
election.WithLeaderLeaseSeconds(cfg.LeaderLease),
election.WithEmbedEtcd(etcd))
if err != nil {
util.GetLogger().Fatalf("create elector failed with %+v", err)
}
p := &defaultProphet{}
p.cfg = cfg
p.persistOptions = config.NewPersistOptions(cfg)
p.ctx = ctx
p.cancel = cancel
p.elector = elector
p.etcd = etcd
p.member = member.NewMember(etcdClient, etcd, elector, cfg.StorageNode, p.enableLeader, p.disableLeader)
p.runner = task.NewRunner()
p.completeC = make(chan struct{})
return p
}
func (p *defaultProphet) Start() {
if err := p.initClusterID(); err != nil {
util.GetLogger().Fatalf("init cluster failed with %+v", err)
}
p.member.MemberInfo(p.cfg.Name, p.cfg.RPCAddr)
p.storage = storage.NewStorage(rootPath,
storage.NewEtcdKV(rootPath, p.elector.Client(), p.member.GetLeadership()),
p.cfg.Adapter)
p.basicCluster = core.NewBasicCluster(p.cfg.Adapter.NewResource)
p.cluster = cluster.NewRaftCluster(p.ctx, rootPath, p.clusterID, p.elector.Client(), p.cfg.Adapter)
p.hbStreams = hbstream.NewHeartbeatStreams(p.ctx, p.clusterID, p.cluster)
p.startSystemMonitor(context.Background())
p.startListen()
p.startLeaderLoop()
}
func (p *defaultProphet) Stop() {
p.trans.Stop()
p.runner.Stop()
p.member.Stop()
p.cancel()
p.elector.Client().Close()
if p.etcd != nil {
p.etcd.Close()
}
}
func (p *defaultProphet) GetStorage() storage.Storage {
return p.storage
}
func (p *defaultProphet) GetClient() Client {
return p.client
}
func (p *defaultProphet) GetLeader() *metapb.Member {
return p.member.GetLeader()
}
func (p *defaultProphet) GetMember() *member.Member {
return p.member
}
func (p *defaultProphet) GetClusterID() uint64 {
return p.clusterID
}
func (p *defaultProphet) initClusterID() error {
// Get any cluster key to parse the cluster ID.
resp, err := util.GetEtcdResp(p.elector.Client(), clusterIDPath)
if err != nil {
return err
}
// If no key exist, generate a random cluster ID.
if len(resp.Kvs) == 0 {
p.clusterID, err = initOrGetClusterID(p.elector.Client(), clusterIDPath)
return err
}
p.clusterID, err = typeutil.BytesToUint64(resp.Kvs[0].Value)
return err
}
func initOrGetClusterID(c *clientv3.Client, key string) (uint64, error) {
ctx, cancel := context.WithTimeout(c.Ctx(), option.DefaultRequestTimeout)
defer cancel()
// Generate a random cluster ID.
ts := uint64(time.Now().Unix())
clusterID := (ts << 32) + uint64(rand.Uint32())
value := typeutil.Uint64ToBytes(clusterID)
// Multiple Prophets may try to init the cluster ID at the same time.
// Only one Prophet can commit this transaction, then other Prophets can get
// the committed cluster ID.
resp, err := c.Txn(ctx).
If(clientv3.Compare(clientv3.CreateRevision(key), "=", 0)).
Then(clientv3.OpPut(key, string(value))).
Else(clientv3.OpGet(key)).
Commit()
if err != nil {
return 0, err
}
// Txn commits ok, return the generated cluster ID.
if resp.Succeeded {
return clusterID, nil
}
// Otherwise, parse the committed cluster ID.
if len(resp.Responses) == 0 {
return 0, errors.New("etcd Txn failed")
}
response := resp.Responses[0].GetResponseRange()
if response == nil || len(response.Kvs) != 1 {
return 0, errors.New("etcd Txn failed")
}
return typeutil.BytesToUint64(response.Kvs[0].Value)
}
// imple raft cluster server interface methods
func (p *defaultProphet) GetConfig() *config.Config {
return p.cfg
}
func (p *defaultProphet) GetPersistOptions() *config.PersistOptions {
return p.persistOptions
}
func (p *defaultProphet) GetHBStreams() *hbstream.HeartbeatStreams {
return p.hbStreams
}
func (p *defaultProphet) GetRaftCluster() *cluster.RaftCluster {
if p.cluster == nil || !p.cluster.IsRunning() {
return nil
}
return p.cluster
}
func (p *defaultProphet) GetBasicCluster() *core.BasicCluster {
return p.basicCluster
}
func (p *defaultProphet) startSystemMonitor(ctx context.Context) {
go StartMonitor(ctx, time.Now, func() {
util.GetLogger().Error("system time jumps backward")
})
}