Repository navigation
Expand file tree
/
Copy pathquorum.go
More file actions
154 lines (126 loc) · 3.33 KB
/
Copy pathquorum.go
File metadata and controls
154 lines (126 loc) · 3.33 KB
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
package group
import (
"errors"
"fmt"
"reflect"
"sync"
"time"
"github.com/docker/infrakit/pkg/spi/group"
"github.com/docker/infrakit/pkg/spi/instance"
)
// TODO(wfarner): Converge this implementation with scaler.go, they share a lot of behavior.
type quorum struct {
id group.ID
scaled Scaled
LogicalIDs []instance.LogicalID
pollInterval time.Duration
stop chan bool
}
// NewQuorum creates a supervisor for a group of instances operating in a quorum.
func NewQuorum(id group.ID, scaled Scaled, logicalIDs []instance.LogicalID, pollInterval time.Duration) Supervisor {
return &quorum{
id: id,
scaled: scaled,
LogicalIDs: logicalIDs,
pollInterval: pollInterval,
stop: make(chan bool),
}
}
func (q *quorum) PlanUpdate(scaled Scaled, settings groupSettings, newSettings groupSettings) (updatePlan, error) {
if !reflect.DeepEqual(settings.config.Allocation.LogicalIDs, newSettings.config.Allocation.LogicalIDs) {
return nil, errors.New("Logical ID changes to a quorum is not currently supported")
}
if settings.config.InstanceHash() == newSettings.config.InstanceHash() {
// This is a no-op update because the instance configuration is unchanged
return &noopUpdate{}, nil
}
return &rollingupdate{
desc: fmt.Sprintf(
"Performing a rolling update on %d instances",
len(settings.config.Allocation.LogicalIDs)),
scaled: scaled,
updatingTo: newSettings,
stop: make(chan bool),
}, nil
}
func (q *quorum) Stop() {
close(q.stop)
}
func (q *quorum) Run() {
ticker := time.NewTicker(q.pollInterval)
q.converge()
for {
select {
case <-ticker.C:
q.converge()
case <-q.stop:
ticker.Stop()
return
}
}
}
func (q *quorum) ID() group.ID {
return q.id
}
func (q *quorum) Size() uint {
return uint(len(q.LogicalIDs))
}
func (q *quorum) converge() {
descriptions, err := labelAndList(q.scaled)
if err != nil {
log.Error("Failed to check to group", "err", err)
return
}
log.Debug("Found existing instances", "groupID", q.ID(), "descriptions", descriptions, "V", debugV)
unknownIPs := []instance.Description{}
for _, description := range descriptions {
if description.LogicalID == nil {
log.Warn("No logical ID", "description", description, "id", description.ID)
continue
}
matched := false
for _, expectedID := range q.LogicalIDs {
if expectedID == *description.LogicalID {
matched = true
}
}
if !matched {
unknownIPs = append(unknownIPs, description)
}
}
grp := sync.WaitGroup{}
for _, ip := range unknownIPs {
unknownInstance := ip
log.Warn("Destroying instances with unknown IP address", "instance", unknownInstance)
grp.Add(1)
go func() {
defer grp.Done()
q.scaled.Destroy(unknownInstance, instance.Termination)
}()
}
missingIDs := []instance.LogicalID{}
for _, expectedID := range q.LogicalIDs {
matched := false
for _, description := range descriptions {
if description.LogicalID == nil {
continue
}
if expectedID == *description.LogicalID {
matched = true
}
}
if !matched {
missingIDs = append(missingIDs, expectedID)
}
}
for _, missingID := range missingIDs {
log.Info("Logical ID is missing, provisioning new instance", "instance", missingID)
id := missingID
grp.Add(1)
go func() {
defer grp.Done()
q.scaled.CreateOne(&id)
}()
}
grp.Wait()
}