forked from asonawalla/gazette
-
Notifications
You must be signed in to change notification settings - Fork 3
/
shard.go
153 lines (131 loc) · 3.61 KB
/
shard.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
package consumer
import (
"path/filepath"
etcd "github.com/coreos/etcd/client"
log "github.com/sirupsen/logrus"
"github.com/LiveRamp/gazette/pkg/topic"
)
type shardState string
const (
shardStateInit shardState = "init"
shardStateReplica shardState = "replica"
shardStateMaster shardState = "master"
shardStateCancelled shardState = "cancelled"
)
// Models the state-machine of how a shard transitions from replica, to master,
// to cancelled. Delegates out the interesting bits to `replica` and `master`.
type shard struct {
id ShardID
partition topic.Partition
localDir string
state shardState
replica *replica
master *master
// A "zombie" shard is a prior shard instance of the same |id| which has been
// cancelled, but is still tearing down. We must allow it to fully exit
// before taking an action which might clobber it.
zombie *shard
// cancelCh is plumbed through replica & master, and acts as a single
// channel by which to signal cancellation of all shard processing.
cancelCh chan struct{}
}
func newShard(id ShardID, partition topic.Partition, runner *Runner, zombie *shard) *shard {
return &shard{
cancelCh: make(chan struct{}),
id: id,
partition: partition,
localDir: filepath.Join(runner.LocalDir, id.String()),
state: shardStateInit,
zombie: zombie,
}
}
// Called from Allocate() goroutine. Cannot block.
func (s *shard) transitionReplica(runner *Runner, tree *etcd.Node) {
switch s.state {
case shardStateInit:
s.state = shardStateReplica
// Fall-through.
case shardStateReplica:
return // No-op.
default:
log.WithFields(log.Fields{"shard": s.id, "state": s.state}).
Panic("invalid replica transaction")
}
var err error
if s.replica, err = newReplica(s, runner, tree); err != nil {
log.WithFields(log.Fields{"shard": s.id, "err": err}).Error("failed to init replica")
go abort(runner, s.id)
return
}
go func() {
if s.zombie != nil {
// Wait for prior shard instance to tear down before replicating.
s.zombie.blockUntilHalted()
s.zombie = nil
}
s.replica.serve(runner)
}()
}
// Called from Allocate() goroutine. Cannot block.
func (s *shard) transitionMaster(runner *Runner, tree *etcd.Node) {
switch s.state {
case shardStateInit:
s.transitionReplica(runner, tree)
s.transitionMaster(runner, tree)
return
case shardStateReplica:
s.state = shardStateMaster
case shardStateMaster:
return // No-op.
default:
log.WithFields(log.Fields{"shard": s.id, "state": s.state}).
Panic("invalid master transition")
}
var err error
if s.master, err = newMaster(s, tree); err != nil {
log.WithFields(log.Fields{"shard": s.id, "err": err}).Error("failed to init master")
go abort(runner, s.id)
return
}
go s.master.serve(runner, s.replica)
}
// Called from Allocate() goroutine. Cannot block.
func (s *shard) transitionCancel() {
switch s.state {
case shardStateInit, shardStateReplica, shardStateMaster:
s.state = shardStateCancelled
case shardStateCancelled:
return // No-op.
default:
log.WithFields(log.Fields{"shard": s.id, "state": s.state}).
Panic("invalid replica transaction")
}
close(s.cancelCh)
}
// Returns only after replica & master have completed.
func (s *shard) blockUntilHalted() {
if s.replica != nil {
<-s.replica.servingCh
}
if s.master != nil {
<-s.master.servingCh
}
}
// Returns whether replica & master have halted, without blocking.
func (s *shard) hasHalted() bool {
if s.master != nil {
select {
case <-s.master.servingCh:
default:
return false
}
}
if s.replica != nil {
select {
case <-s.replica.servingCh:
default:
return false
}
}
return true
}