/
staged_placement_watcher.go
156 lines (135 loc) · 4.78 KB
/
staged_placement_watcher.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package placement
import (
"errors"
"sync"
"github.com/m3db/m3/src/cluster/generated/proto/placementpb"
"github.com/m3db/m3/src/cluster/kv"
"github.com/m3db/m3/src/cluster/kv/util/runtime"
"github.com/m3db/m3/src/x/clock"
)
var (
errNilValue = errors.New("nil value received")
errPlacementWatcherIsNotWatching = errors.New("placement watcher is not watching")
errPlacementWatcherIsWatching = errors.New("placement watcher is watching")
)
type placementWatcherState int
const (
placementWatcherNotWatching placementWatcherState = iota
placementWatcherWatching
)
type stagedPlacementWatcher struct {
sync.RWMutex
runtime.Value
nowFn clock.NowFn
placementOpts ActiveStagedPlacementOptions
doneFn DoneFn
state placementWatcherState
proto *placementpb.PlacementSnapshots
placement ActiveStagedPlacement
}
// NewStagedPlacementWatcher creates a new staged placement watcher.
func NewStagedPlacementWatcher(opts StagedPlacementWatcherOptions) StagedPlacementWatcher {
watcher := &stagedPlacementWatcher{
nowFn: opts.ClockOptions().NowFn(),
placementOpts: opts.ActiveStagedPlacementOptions(),
proto: &placementpb.PlacementSnapshots{},
}
watcher.doneFn = watcher.onActiveStagedPlacementDone
valueOpts := runtime.NewOptions().
SetInstrumentOptions(opts.InstrumentOptions()).
SetInitWatchTimeout(opts.InitWatchTimeout()).
SetKVStore(opts.StagedPlacementStore()).
SetUnmarshalFn(watcher.toStagedPlacement).
SetProcessFn(watcher.process)
watcher.Value = runtime.NewValue(opts.StagedPlacementKey(), valueOpts)
return watcher
}
func (t *stagedPlacementWatcher) Watch() error {
t.Lock()
if t.state != placementWatcherNotWatching {
t.Unlock()
return errPlacementWatcherIsWatching
}
t.state = placementWatcherWatching
t.Unlock()
// NB(xichen): we watch the placementWatcher updates outside the lock because
// otherwise the initial update will trigger the process() callback,
// which attempts to acquire the same lock, causing a deadlock.
return t.Value.Watch()
}
func (t *stagedPlacementWatcher) ActiveStagedPlacement() (ActiveStagedPlacement, DoneFn, error) {
t.RLock()
if t.state != placementWatcherWatching {
t.RUnlock()
return nil, nil, errPlacementWatcherIsNotWatching
}
return t.placement, t.doneFn, nil
}
func (t *stagedPlacementWatcher) Unwatch() error {
t.Lock()
if t.state != placementWatcherWatching {
t.Unlock()
return errPlacementWatcherIsNotWatching
}
t.state = placementWatcherNotWatching
if t.placement != nil {
t.placement.Close()
}
t.placement = nil
t.Unlock()
// NB(xichen): we unwatch the updates outside the lock to avoid deadlock
// due to placementWatcher contending for the runtime value lock and the
// runtime updating goroutine attempting to acquire placementWatcher lock.
t.Value.Unwatch()
return nil
}
func (t *stagedPlacementWatcher) onActiveStagedPlacementDone() { t.RUnlock() }
func (t *stagedPlacementWatcher) toStagedPlacement(value kv.Value) (interface{}, error) {
t.Lock()
defer t.Unlock()
if t.state != placementWatcherWatching {
return nil, errPlacementWatcherIsNotWatching
}
if value == nil {
return nil, errNilValue
}
t.proto.Reset()
if err := value.Unmarshal(t.proto); err != nil {
return nil, err
}
version := value.Version()
return NewStagedPlacementFromProto(version, t.proto, t.placementOpts)
}
func (t *stagedPlacementWatcher) process(value interface{}) error {
t.Lock()
defer t.Unlock()
if t.state != placementWatcherWatching {
return errPlacementWatcherIsNotWatching
}
ps := value.(StagedPlacement)
placement := ps.ActiveStagedPlacement(t.nowFn().UnixNano())
if t.placement != nil {
t.placement.Close()
}
t.placement = placement
return nil
}