Skip to content
This repository was archived by the owner on May 28, 2021. It is now read-only.

Commit 399291e

Browse files
Workaround the improper behavior of MySQL shell with respect to
auto_increment_offset and auto_increment_increment values override. Signed-off-by: Gianluca Borello <[email protected]>
1 parent 2a251f7 commit 399291e

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

pkg/controllers/cluster/manager/cluster_manager.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,14 @@ func (m *ClusterManager) handleInstanceMissing(ctx context.Context, primaryAddr
261261
glog.Errorf("Failed to rejoin cluster: %v", err)
262262
return false
263263
}
264+
265+
if !m.Instance.MultiMaster {
266+
err = m.localMySh.SetSinglePrimaryAutoIncrementParams(ctx)
267+
if err != nil {
268+
glog.Errorf("Failed setting auto increment parameters: %v", err)
269+
return false
270+
}
271+
}
264272
} else {
265273
glog.V(4).Infof("Removing instance from cluster")
266274
if err := primarySh.RemoveInstanceFromCluster(ctx, m.Instance.GetShellURI(), mysqlsh.Options{"force": "True"}); err != nil {
@@ -290,6 +298,15 @@ func (m *ClusterManager) handleInstanceNotFound(ctx context.Context, primaryAddr
290298
glog.Errorf("Failed to add to cluster: %v", err)
291299
return false
292300
}
301+
302+
if !m.Instance.MultiMaster {
303+
err = m.localMySh.SetSinglePrimaryAutoIncrementParams(ctx)
304+
if err != nil {
305+
glog.Errorf("Failed setting auto increment parameters: %v", err)
306+
return false
307+
}
308+
}
309+
293310
return true
294311
}
295312

@@ -327,6 +344,14 @@ func (m *ClusterManager) createCluster(ctx context.Context) (*innodb.ClusterStat
327344
if err != nil {
328345
return nil, errors.Wrap(err, "failed to create new cluster")
329346
}
347+
348+
if !m.Instance.MultiMaster {
349+
err = m.localMySh.SetSinglePrimaryAutoIncrementParams(ctx)
350+
if err != nil {
351+
return nil, errors.Wrap(err, "failed setting auto increment parameters")
352+
}
353+
}
354+
330355
return status, nil
331356
}
332357

@@ -342,6 +367,14 @@ func (m *ClusterManager) rebootFromOutage(ctx context.Context) (*innodb.ClusterS
342367
if err != nil {
343368
return nil, errors.Wrap(err, "getting cluster status")
344369
}
370+
371+
if !m.Instance.MultiMaster {
372+
err = m.localMySh.SetSinglePrimaryAutoIncrementParams(ctx)
373+
if err != nil {
374+
return nil, errors.Wrap(err, "failed setting auto increment parameters")
375+
}
376+
}
377+
345378
return status, nil
346379
}
347380

pkg/util/mysqlsh/mysqlsh.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ type Interface interface {
5555
// RebootClusterFromCompleteOutage recovers a cluster when all of its members
5656
// have failed.
5757
RebootClusterFromCompleteOutage(ctx context.Context) error
58+
// SetSinglePrimaryAutoIncrementParams sets auto increment parameters to 1 and
59+
// it should be called in single primary mode.
60+
SetSinglePrimaryAutoIncrementParams(ctx context.Context) error
5861
}
5962

6063
// errorRegex is used to parse Python tracebacks generated by mysql-shell.
@@ -226,6 +229,31 @@ func (r *runner) RebootClusterFromCompleteOutage(ctx context.Context) error {
226229
return err
227230
}
228231

232+
func (r *runner) SetSinglePrimaryAutoIncrementParams(ctx context.Context) error {
233+
// Restore auto_increment_offset and auto_increment_increment
234+
// to a safe 1 value, since we are in single primary mode.
235+
// There seems to be a bug in MySQL Shell, and it initializes
236+
// them to 7 and 1+id%7, which is not ideal. Also, from MySQL 8,
237+
// MySQL will not overwrite these values according to
238+
// group_replication_auto_increment_increment and its default of 7,
239+
// so there's a wrong MySQL Shell assumption
240+
// (https://github.com/mysql/mysql-server/commit/846ced27f8315a4697405b2b9a7bdeadb44cf070)
241+
242+
python := fmt.Sprintf("session.query('SET PERSIST auto_increment_offset = 1')")
243+
_, err := r.run(ctx, python)
244+
if err != nil {
245+
return err
246+
}
247+
248+
python = fmt.Sprintf("session.query('SET PERSIST auto_increment_increment = 1')")
249+
_, err = r.run(ctx, python)
250+
if err != nil {
251+
return err
252+
}
253+
254+
return nil
255+
}
256+
229257
// Error holds errors from mysql-shell commands.
230258
type Error struct {
231259
error

0 commit comments

Comments
 (0)