This repository has been archived by the owner on May 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 235
/
cluster_manager.go
367 lines (317 loc) · 12.2 KB
/
cluster_manager.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright 2018 Oracle and/or its affiliates. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package manager
import (
"context"
"fmt"
"strings"
"time"
"github.com/golang/glog"
"github.com/pkg/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
wait "k8s.io/apimachinery/pkg/util/wait"
kubeinformers "k8s.io/client-go/informers"
kubernetes "k8s.io/client-go/kubernetes"
utilexec "k8s.io/utils/exec"
"github.com/oracle/mysql-operator/pkg/cluster"
"github.com/oracle/mysql-operator/pkg/cluster/innodb"
"github.com/oracle/mysql-operator/pkg/controllers/cluster/labeler"
"github.com/oracle/mysql-operator/pkg/util/metrics"
"github.com/oracle/mysql-operator/pkg/util/mysqlsh"
)
const pollingIntervalSeconds = 15
// ClusterManager manages the local MySQL instance's membership of an InnoDB cluster.
type ClusterManager struct {
kubeClient kubernetes.Interface
// kubeInformerFactory is a kubernetes core informer factory.
kubeInformerFactory kubeinformers.SharedInformerFactory
// mysqlshFactory creates new mysqlsh.Interfaces. Implemented as a factory
// for testing purposes.
mysqlshFactory func(uri string) mysqlsh.Interface
// localMySh is a mysqlsh.Interface configured for the local instance of MySQL.
localMySh mysqlsh.Interface
// Instance is the local instance of MySQL under management.
Instance *cluster.Instance
// primaryCancelFunc cancels the execution of the primary-only controllers.
primaryCancelFunc context.CancelFunc
podLabelerController *labeler.ClusterLabelerController
}
// NewClusterManager creates a InnoDB cluster ClusterManager.
func NewClusterManager(
kubeClient kubernetes.Interface,
kubeInformerFactory kubeinformers.SharedInformerFactory,
mysqlshFactory func(string) mysqlsh.Interface,
instance *cluster.Instance,
) *ClusterManager {
manager := &ClusterManager{
kubeClient: kubeClient,
kubeInformerFactory: kubeInformerFactory,
mysqlshFactory: mysqlshFactory,
Instance: instance,
localMySh: mysqlshFactory(instance.GetShellURI()),
}
return manager
}
// NewLocalClusterManger creates a new cluster.ClusterManager for the local MySQL instance.
func NewLocalClusterManger(kubeclient kubernetes.Interface, kubeInformerFactory kubeinformers.SharedInformerFactory) (*ClusterManager, error) {
// Create a new instance representing the local MySQL instance.
instance, err := cluster.NewLocalInstance()
if err != nil {
return nil, errors.Wrap(err, "failed to get local MySQL instance")
}
return NewClusterManager(
kubeclient,
kubeInformerFactory,
func(uri string) mysqlsh.Interface { return mysqlsh.New(utilexec.New(), uri) },
instance,
), nil
}
func (m *ClusterManager) getClusterStatus(ctx context.Context) (*innodb.ClusterStatus, error) {
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()
clusterStatus, localMSHErr := m.localMySh.GetClusterStatus(ctx)
if localMSHErr != nil {
var err error
clusterStatus, err = getClusterStatusFromGroupSeeds(ctx, m.kubeClient, m.Instance)
if err != nil {
// NOTE: We return the localMSHErr rather than the error here so that we
// can dispatch on it.
return nil, errors.Wrap(localMSHErr, "getting cluster status from group seeds")
}
}
return clusterStatus, nil
}
// Sync ensures that the MySQL database instance managed by this instance of the
// agent is part of the InnoDB cluster and is online.
func (m *ClusterManager) Sync(ctx context.Context) bool {
if !isDatabaseRunning(ctx) {
glog.V(2).Infof("Database not yet running. Waiting...")
return false
}
// First try the local instance so we reuse the mysqlsh.Instance in the
// most common case.
clusterStatus, err := m.getClusterStatus(ctx)
if err != nil {
myshErr, ok := errors.Cause(err).(*mysqlsh.Error)
if !ok {
glog.Errorf("Failed to get the cluster status: %+v", err)
return false
}
// We can't find a cluster. Bootstrap if we're the first member of the
// StatefulSet.
if m.Instance.Ordinal == 0 {
clusterStatus, err = m.bootstrap(ctx, myshErr)
if err != nil {
glog.Errorf("Error bootstrapping cluster: %v", err)
metrics.IncEventCounter(clusterCreateErrorCount)
return false
}
metrics.IncEventCounter(clusterCreateCount)
} else {
glog.V(2).Info("Cluster not yet present. Waiting...")
return false
}
}
// Set the cluster status so that the in-cluster healthcheck gets the
// most up to date information.
cluster.SetStatus(clusterStatus)
if clusterStatus.DefaultReplicaSet.Status == innodb.ReplicaSetStatusNoQuorum {
glog.V(4).Info("Cluster as seen from this instance is in NO_QUORUM state")
metrics.IncEventCounter(clusterNoQuorumCount)
}
online := false
instanceStatus := clusterStatus.GetInstanceStatus(m.Instance.Name())
switch instanceStatus {
case innodb.InstanceStatusOnline:
metrics.IncStatusCounter(instanceStatusCount, innodb.InstanceStatusOnline)
glog.V(4).Info("MySQL instance is online")
online = true
case innodb.InstanceStatusRecovering:
metrics.IncStatusCounter(instanceStatusCount, innodb.InstanceStatusRecovering)
glog.V(4).Info("MySQL instance is recovering")
case innodb.InstanceStatusMissing:
metrics.IncStatusCounter(instanceStatusCount, innodb.InstanceStatusMissing)
primaryAddr, err := clusterStatus.GetPrimaryAddr()
if err != nil {
glog.Errorf("%v", err)
return false
}
online = m.handleInstanceMissing(ctx, primaryAddr)
if online {
metrics.IncEventCounter(instanceRejoinCount)
} else {
metrics.IncEventCounter(instanceRejoinErrorCount)
}
case innodb.InstanceStatusNotFound:
metrics.IncStatusCounter(instanceStatusCount, innodb.InstanceStatusNotFound)
primaryAddr, err := clusterStatus.GetPrimaryAddr()
if err != nil {
glog.Errorf("%v", err)
return false
}
online = m.handleInstanceNotFound(ctx, primaryAddr)
if online {
metrics.IncEventCounter(instanceAddCount)
} else {
metrics.IncEventCounter(instanceAddErrorCount)
}
case innodb.InstanceStatusUnreachable:
metrics.IncStatusCounter(instanceStatusCount, innodb.InstanceStatusUnreachable)
default:
metrics.IncStatusCounter(instanceStatusCount, innodb.InstanceStatusUnknown)
glog.Errorf("Received unrecognised cluster membership status: %q", instanceStatus)
}
if online && !m.Instance.MultiMaster {
m.ensurePrimaryControllerState(ctx, clusterStatus)
}
return online
}
// ensurePrimaryControllerState ensures that the primary-only controllers are
// running if the local MySQL instance is the primary.
func (m *ClusterManager) ensurePrimaryControllerState(ctx context.Context, status *innodb.ClusterStatus) {
// Are we the primary?
primaryAddr, err := status.GetPrimaryAddr()
if err != nil {
glog.Errorf("%v", err)
return
}
if !strings.HasPrefix(primaryAddr, m.Instance.Name()) {
if m.primaryCancelFunc != nil {
glog.V(4).Info("Calling primaryCancelFunc()")
m.primaryCancelFunc()
m.primaryCancelFunc = nil
}
return
}
// We are the Primary. Is/are the primary controller(s) running?
if m.primaryCancelFunc == nil {
// Run the primary controller(s).
m.podLabelerController = labeler.NewClusterLabelerController(m.Instance, m.kubeClient, m.kubeInformerFactory.Core().V1().Pods())
ctx, m.primaryCancelFunc = context.WithCancel(ctx)
go m.podLabelerController.Run(ctx)
// We must call Start() on the shared informer factory here to register
// the new informer in the case of failover (where the shared informer
// factory will have been started previously with no reference to the
// Pod informer required by the labeler).
go m.kubeInformerFactory.Start(ctx.Done())
}
if err := m.podLabelerController.EnqueueClusterStatus(status.DeepCopy()); err != nil {
utilruntime.HandleError(errors.Wrap(err, "enqueuing ClusterStatus"))
}
}
func (m *ClusterManager) handleInstanceMissing(ctx context.Context, primaryAddr string) bool {
primaryURI := fmt.Sprintf("%s:%s@%s", m.Instance.GetUser(), m.Instance.GetPassword(), primaryAddr)
primarySh := m.mysqlshFactory(primaryURI)
// TODO: just call RejoinInstanceToCluster and handle the error.
instanceState, err := primarySh.CheckInstanceState(ctx, m.Instance.GetShellURI())
if err != nil {
glog.Errorf("Failed to determine if we can rejoin the cluster: %v", err)
return false
}
glog.V(4).Infof("Checking if instance can rejoin cluster")
if instanceState.CanRejoinCluster() {
whitelistCIDR, err := m.Instance.WhitelistCIDR()
if err != nil {
glog.Errorf("Getting CIDR to whitelist for GR: %v", err)
return false
}
glog.V(4).Infof("Attempting to rejoin instance to cluster")
if err := primarySh.RejoinInstanceToCluster(ctx, m.Instance.GetShellURI(), mysqlsh.Options{
"ipWhitelist": whitelistCIDR,
"memberSslMode": "REQUIRED",
}); err != nil {
glog.Errorf("Failed to rejoin cluster: %v", err)
return false
}
} else {
glog.V(4).Infof("Removing instance from cluster")
if err := primarySh.RemoveInstanceFromCluster(ctx, m.Instance.GetShellURI(), mysqlsh.Options{"force": "True"}); err != nil {
glog.Errorf("Failed to remove from cluster: %v", err)
return false
}
}
return true
}
func (m *ClusterManager) handleInstanceNotFound(ctx context.Context, primaryAddr string) bool {
glog.V(4).Infof("Adding secondary instance to the cluster")
primaryURI := fmt.Sprintf("%s:%s@%s", m.Instance.GetUser(), m.Instance.GetPassword(), primaryAddr)
psh := m.mysqlshFactory(primaryURI)
whitelistCIDR, err := m.Instance.WhitelistCIDR()
if err != nil {
glog.Errorf("Getting CIDR to whitelist for GR: %v", err)
return false
}
if err := psh.AddInstanceToCluster(ctx, m.Instance.GetShellURI(), mysqlsh.Options{
"memberSslMode": "REQUIRED",
"ipWhitelist": whitelistCIDR,
}); err != nil {
glog.Errorf("Failed to add to cluster: %v", err)
return false
}
return true
}
// bootstrap bootstraps the cluster. Called on the first Pod in the StatefulSet.
func (m *ClusterManager) bootstrap(ctx context.Context, mshErr *mysqlsh.Error) (*innodb.ClusterStatus, error) {
if strings.Contains(mshErr.Message, "Cannot perform operation while group replication is starting up") {
return nil, mshErr
}
if strings.Contains(mshErr.Message, "(metadata exists, but GR is not active)") {
return m.rebootFromOutage(ctx)
}
return m.createCluster(ctx)
}
func (m *ClusterManager) createCluster(ctx context.Context) (*innodb.ClusterStatus, error) {
glog.Infof("Creating InnoDB cluster")
msh := m.mysqlshFactory(m.Instance.GetShellURI())
whitelistCIDR, err := m.Instance.WhitelistCIDR()
if err != nil {
return nil, errors.Wrap(err, "getting CIDR to whitelist for GR")
}
opts := mysqlsh.Options{
"memberSslMode": "REQUIRED",
"ipWhitelist": whitelistCIDR,
}
if m.Instance.MultiMaster {
opts["force"] = "True"
opts["multiMaster"] = "True"
}
status, err := msh.CreateCluster(ctx, opts)
if err != nil {
return nil, errors.Wrap(err, "failed to create new cluster")
}
return status, nil
}
func (m *ClusterManager) rebootFromOutage(ctx context.Context) (*innodb.ClusterStatus, error) {
glog.Info("Found existing InnoDB cluster (metadata exists, but GR is not active)")
msh := m.mysqlshFactory(m.Instance.GetShellURI())
if err := msh.RebootClusterFromCompleteOutage(ctx); err != nil {
return nil, errors.Wrap(err, "rebooting cluster from complete outage")
}
status, err := msh.GetClusterStatus(ctx)
if err != nil {
return nil, errors.Wrap(err, "getting cluster status")
}
return status, nil
}
// Run runs the ClusterManager controller.
// NOTE: ctx is not currently used for cancellation by caller (the stopCh is).
func (m *ClusterManager) Run(ctx context.Context) {
wait.Until(func() { m.Sync(ctx) }, time.Second*pollingIntervalSeconds, ctx.Done())
<-ctx.Done()
// Stop the primary-only controllers if they're running
if m.primaryCancelFunc != nil {
m.primaryCancelFunc()
m.primaryCancelFunc = nil
}
}