forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
214 lines (183 loc) · 6.01 KB
/
store.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
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package raftlease
import (
"context"
"fmt"
"time"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/prometheus/client_golang/prometheus"
"github.com/DavinZhang/juju/core/lease"
)
var logger = loggo.GetLogger("juju.core.raftlease")
func aborted(command *Command) error {
switch command.Operation {
case OperationSetTime:
return errors.Annotatef(lease.ErrAborted, "setTime")
case OperationPin, OperationUnpin:
leaseId := fmt.Sprintf("%.6s:%s", command.ModelUUID, command.Lease)
return errors.Annotatef(lease.ErrAborted, "%q on %q",
command.Operation, leaseId)
default:
leaseId := fmt.Sprintf("%.6s:%s", command.ModelUUID, command.Lease)
return errors.Annotatef(lease.ErrAborted, "%q on %q for %q",
command.Operation, leaseId, command.Holder)
}
}
// NotifyTarget defines methods needed to keep an external database
// updated with who holds leases. (In non-test code the notify target
// will generally be the state DB.)
type NotifyTarget interface {
// Claimed will be called when a new lease has been claimed.
Claimed(lease.Key, string) error
// Expired will be called when an existing lease has expired.
Expired(lease.Key) error
}
// TrapdoorFunc returns a trapdoor to be attached to lease details for
// use by clients. This is intended to hold assertions that can be
// added to state transactions to ensure the lease is still held when
// the transaction is applied.
type TrapdoorFunc func(lease.Key, string) lease.Trapdoor
// ReadOnlyClock describes a clock from which global time can be read.
type ReadOnlyClock interface {
GlobalTime() time.Time
}
// ReadonlyFSM defines the methods of the lease FSM the store can use
// - any writes must go through the hub.
type ReadonlyFSM interface {
ReadOnlyClock
// Leases and LeaseGroup receive a func for retrieving time,
// because it needs to be determined after potential lock-waiting
// to be accurate.
Leases(func() time.Time, ...lease.Key) map[lease.Key]lease.Info
LeaseGroup(func() time.Time, string, string) map[lease.Key]lease.Info
Pinned() map[lease.Key][]string
}
// StoreConfig holds resources and settings needed to run the Store.
type StoreConfig struct {
FSM ReadonlyFSM
Client Client
Trapdoor TrapdoorFunc
Clock clock.Clock
MetricsCollector MetricsCollector
}
// Store manages a raft FSM and forwards writes through a pubsub hub.
type Store struct {
fsm ReadonlyFSM
config StoreConfig
metrics MetricsCollector
client Client
}
// NewStore returns a core/lease.Store that manages leases in Raft.
func NewStore(config StoreConfig) *Store {
return &Store{
fsm: config.FSM,
config: config,
client: config.Client,
metrics: config.MetricsCollector,
}
}
// ClaimLease is part of lease.Store.
func (s *Store) ClaimLease(key lease.Key, req lease.Request, stop <-chan struct{}) error {
return errors.Trace(s.runOnLeader(&Command{
Version: CommandVersion,
Operation: OperationClaim,
Namespace: key.Namespace,
ModelUUID: key.ModelUUID,
Lease: key.Lease,
Holder: req.Holder,
Duration: req.Duration,
}, stop))
}
// ExtendLease is part of lease.Store.
func (s *Store) ExtendLease(key lease.Key, req lease.Request, stop <-chan struct{}) error {
return errors.Trace(s.runOnLeader(&Command{
Version: CommandVersion,
Operation: OperationExtend,
Namespace: key.Namespace,
ModelUUID: key.ModelUUID,
Lease: key.Lease,
Holder: req.Holder,
Duration: req.Duration,
}, stop))
}
// RevokeLease is part of lease.Store.
func (s *Store) RevokeLease(key lease.Key, holder string, stop <-chan struct{}) error {
return errors.Trace(s.runOnLeader(&Command{
Version: CommandVersion,
Operation: OperationRevoke,
Namespace: key.Namespace,
ModelUUID: key.ModelUUID,
Lease: key.Lease,
Holder: holder,
}, stop))
}
// Leases is part of lease.Store.
func (s *Store) Leases(keys ...lease.Key) map[lease.Key]lease.Info {
leaseMap := s.fsm.Leases(s.config.Clock.Now, keys...)
s.addTrapdoors(leaseMap)
return leaseMap
}
// LeaseGroup is part of Lease.Store.
func (s *Store) LeaseGroup(namespace, modelUUID string) map[lease.Key]lease.Info {
leaseMap := s.fsm.LeaseGroup(s.config.Clock.Now, namespace, modelUUID)
s.addTrapdoors(leaseMap)
return leaseMap
}
func (s *Store) addTrapdoors(leaseMap map[lease.Key]lease.Info) {
for k, v := range leaseMap {
v.Trapdoor = s.config.Trapdoor(k, v.Holder)
leaseMap[k] = v
}
}
// PinLease is part of lease.Store.
func (s *Store) PinLease(key lease.Key, entity string, stop <-chan struct{}) error {
return errors.Trace(s.pinOp(OperationPin, key, entity, stop))
}
// UnpinLease is part of lease.Store.
func (s *Store) UnpinLease(key lease.Key, entity string, stop <-chan struct{}) error {
return errors.Trace(s.pinOp(OperationUnpin, key, entity, stop))
}
// Pinned is part of the Store interface.
func (s *Store) Pinned() map[lease.Key][]string {
return s.fsm.Pinned()
}
func (s *Store) pinOp(operation string, key lease.Key, entity string, stop <-chan struct{}) error {
return errors.Trace(s.runOnLeader(&Command{
Version: CommandVersion,
Operation: operation,
Namespace: key.Namespace,
ModelUUID: key.ModelUUID,
Lease: key.Lease,
PinEntity: entity,
}, stop))
}
func (s *Store) runOnLeader(command *Command, stop <-chan struct{}) error {
start := s.config.Clock.Now()
defer func() {
elapsed := s.config.Clock.Now().Sub(start)
logger.Tracef("runOnLeader %v, elapsed from publish: %v", command.Operation, elapsed.Round(time.Millisecond))
}()
ch := make(chan struct{})
defer close(ch)
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
go func() {
select {
case <-stop:
cancel()
case <-ch:
}
}()
return s.client.Request(ctx, command)
}
// Describe is part of prometheus.Collector.
func (s *Store) Describe(ch chan<- *prometheus.Desc) {
s.metrics.Describe(ch)
}
// Collect is part of prometheus.Collector.
func (s *Store) Collect(ch chan<- prometheus.Metric) {
s.metrics.Collect(ch)
}