-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprophet_handler.go
121 lines (98 loc) · 2.57 KB
/
prophet_handler.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
package prophet
import (
"errors"
"fmt"
"time"
)
var (
errReq = errors.New("invalid req")
errStaleResource = errors.New("stale resource")
errTombstoneContainer = errors.New("container is tombstone")
)
func (p *defaultProphet) handleResourceHeartbeat(msg *ResourceHeartbeatReq) (*resourceHeartbeatRsp, error) {
if msg.LeaderPeer == nil && len(msg.Resource.Peers()) != 1 {
return nil, errReq
}
if msg.Resource.ID() == 0 {
return nil, errReq
}
if len(msg.Resource.Peers()) == 0 {
return nil, errReq
}
value := newResourceRuntime(msg.Resource, msg.LeaderPeer)
value.downPeers = msg.DownPeers
value.pendingPeers = msg.PendingPeers
p.Lock()
defer p.Unlock()
err := p.rt.handleResource(value)
if err != nil {
if err == errStaleResource {
return newChangePeerRsp(msg.Resource.ID(),
value.GetContainerPeer(msg.ContainerID),
RemovePeer), nil
}
return nil, err
}
return p.coordinator.dispatch(p.rt.Resource(value.meta.ID())), nil
}
func (p *defaultProphet) handleContainerHeartbeat(msg *ContainerHeartbeatReq) error {
meta := msg.Container
if meta != nil && meta.State() == Tombstone {
return errTombstoneContainer
}
p.Lock()
defer p.Unlock()
container := p.rt.Container(meta.ID())
if container == nil {
err := p.store.PutContainer(meta)
if err != nil {
return err
}
container = newContainerRuntime(meta)
}
container.blocked = msg.Block
container.busy = msg.Busy
container.leaderCount = msg.LeaderCount
container.replicaCount = msg.ReplicaCount
container.storageCapacity = msg.StorageCapacity
container.storageAvailable = msg.StorageAvailable
container.sendingSnapCount = msg.SendingSnapCount
container.receivingSnapCount = msg.ReceivingSnapCount
container.applyingSnapCount = msg.ApplyingSnapCount
container.lastHeartbeatTS = time.Now()
p.rt.handleContainer(container)
return nil
}
func (p *defaultProphet) handleAllocID(req *allocIDReq) *allocIDRsp {
id, err := p.store.AllocID()
return &allocIDRsp{
ID: id,
Err: err,
}
}
func (p *defaultProphet) handleAskSplit(req *askSplitReq) *askSplitRsp {
p.Lock()
defer p.Unlock()
rsp := &askSplitRsp{}
res := p.rt.Resource(req.Resource.ID())
if res == nil {
rsp.Err = fmt.Errorf("resource not found")
return rsp
}
newID, err := p.store.AllocID()
if err != nil {
rsp.Err = err
return rsp
}
cnt := len(req.Resource.Peers())
peerIDs := make([]uint64, cnt)
for index := 0; index < cnt; index++ {
if peerIDs[index], err = p.store.AllocID(); err != nil {
rsp.Err = err
return rsp
}
}
rsp.NewID = newID
rsp.NewPeerIDs = peerIDs
return rsp
}