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

Commit c75b6a2

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 d9b3b4f commit c75b6a2

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

pkg/controllers/cluster/manager/cluster_manager.go

+33
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,14 @@ func (m *ClusterManager) handleInstanceMissing(ctx context.Context, primaryAddr
260260
glog.Errorf("Failed to rejoin cluster: %v", err)
261261
return false
262262
}
263+
264+
if !m.Instance.MultiMaster {
265+
err = m.localMySh.SetSinglePrimaryAutoIncrementParams(ctx)
266+
if err != nil {
267+
glog.Errorf("Failed setting auto increment parameters: %v", err)
268+
return false
269+
}
270+
}
263271
} else {
264272
glog.V(4).Infof("Removing instance from cluster")
265273
if err := primarySh.RemoveInstanceFromCluster(ctx, m.Instance.GetShellURI(), mysqlsh.Options{"force": "True"}); err != nil {
@@ -289,6 +297,15 @@ func (m *ClusterManager) handleInstanceNotFound(ctx context.Context, primaryAddr
289297
glog.Errorf("Failed to add to cluster: %v", err)
290298
return false
291299
}
300+
301+
if !m.Instance.MultiMaster {
302+
err = m.localMySh.SetSinglePrimaryAutoIncrementParams(ctx)
303+
if err != nil {
304+
glog.Errorf("Failed setting auto increment parameters: %v", err)
305+
return false
306+
}
307+
}
308+
292309
return true
293310
}
294311

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

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

pkg/util/mysqlsh/mysqlsh.go

+28
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.
@@ -202,6 +205,31 @@ func (r *runner) RebootClusterFromCompleteOutage(ctx context.Context) error {
202205
return err
203206
}
204207

208+
func (r *runner) SetSinglePrimaryAutoIncrementParams(ctx context.Context) error {
209+
// Restore auto_increment_offset and auto_increment_increment
210+
// to a safe 1 value, since we are in single primary mode.
211+
// There seems to be a bug in MySQL Shell, and it initializes
212+
// them to 7 and 1+id%7, which is not ideal. Also, from MySQL 8,
213+
// MySQL will not overwrite these values according to
214+
// group_replication_auto_increment_increment and its default of 7,
215+
// so there's a wrong MySQL Shell assumption
216+
// (https://github.com/mysql/mysql-server/commit/846ced27f8315a4697405b2b9a7bdeadb44cf070)
217+
218+
python := fmt.Sprintf("session.query('SET PERSIST auto_increment_offset = 1')")
219+
_, err := r.run(ctx, python)
220+
if err != nil {
221+
return err
222+
}
223+
224+
python = fmt.Sprintf("session.query('SET PERSIST auto_increment_increment = 1')")
225+
_, err = r.run(ctx, python)
226+
if err != nil {
227+
return err
228+
}
229+
230+
return nil
231+
}
232+
205233
// Error holds errors from mysql-shell commands.
206234
type Error struct {
207235
error

0 commit comments

Comments
 (0)