Skip to content

Commit

Permalink
*: fix the change master bug
Browse files Browse the repository at this point in the history
  • Loading branch information
acekingke committed Sep 18, 2023
1 parent fba7cb7 commit 7fdd762
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
12 changes: 9 additions & 3 deletions mysqlcluster/syncer/readonly_statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,16 @@ func putMySQLReadOnly(s *StatefulSetSyncer, host string) error {
s.log.V(1).Info("slave status has gotten error", "error", err)
}
if isReplicating == corev1.ConditionFalse {
// chang master
changeSql := fmt.Sprintf(`stop slave;CHANGE MASTER TO MASTER_HOST='%s', MASTER_PORT=%d, MASTER_USER='%s', MASTER_PASSWORD='%s',
// No.1 start slave
if errStart := sqlRunner.QueryExec(internal.NewQuery("start slave;")); errStart != nil {
s.log.V(1).Info("start slave gotten error", "error", errStart)
// No2. change master and start
changeSql := fmt.Sprintf(`stop slave;CHANGE MASTER TO MASTER_HOST='%s', MASTER_PORT=%d, MASTER_USER='%s', MASTER_PASSWORD='%s',
MASTER_AUTO_POSITION=1; start slave;`, buildMasterName(s), 3306, "root", cfg.Password)
sqlRunner.QueryExec(internal.NewQuery(changeSql))
if err2 := sqlRunner.QueryExec(internal.NewQuery(changeSql)); err2 != nil {
s.log.V(1).Info("change master and start slave gotten error", "error", err2)
}
}
}
}
return errOut
Expand Down
17 changes: 15 additions & 2 deletions mysqlcluster/syncer/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,6 @@ func (s *StatusSyncer) RoCheckStatus(ctx context.Context, cli client.Client, pod
host := fmt.Sprintf("%s.%s.%s", podName, s.GetNameForResource(utils.ReadOnlyHeadlessSVC), s.Namespace)
index := s.getRoStatusIndex(host)
node := &s.Status.Nodes[index]
node.Message = ""

isInitial, isReadonly, isCloseSemi, isReplicating := corev1.ConditionUnknown, corev1.ConditionUnknown, corev1.ConditionUnknown, corev1.ConditionUnknown
isSupperReadOnly := corev1.ConditionUnknown
Expand Down Expand Up @@ -661,7 +660,21 @@ func (s *StatusSyncer) RoCheckStatus(ctx context.Context, cli client.Client, pod
}
// 3. change master
if _, isReplicating, err = internal.CheckSlaveStatus(sqlRunner, s.Spec.ReplicaLag); err != nil {
node.Message = err.Error()
parts := strings.Split(node.Message, " ")
now := time.Now().Unix()
var last int64
var err2 error
if len(parts[0]) == 0 {
node.Message = fmt.Sprintf("%d unixtime error:", now) + err.Error()
} else if last, err2 = strconv.ParseInt(parts[0], 10, 64); err2 != nil {
s.log.Info("get times from message", "times", last)
}
if now-last > 10 {
node.Message = fmt.Sprintf("%d unixseconds error:", now) + err.Error()
}

} else {
node.Message = ""
}
}
//update node Rostatus
Expand Down

0 comments on commit 7fdd762

Please sign in to comment.