Skip to content

Commit cae0d3f

Browse files
committed
fix masterinfo and relayinfo removing
1 parent 4e5c4b1 commit cae0d3f

4 files changed

Lines changed: 57 additions & 3 deletions

File tree

pkg/controller/controller.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"fmt"
2020
"math/rand"
2121
"net"
22-
"os"
2322
"sync"
2423
"time"
2524

@@ -503,10 +502,10 @@ func (c *Controller) syncReadOnlyVariable(readOnlyToBeTrue bool) error {
503502

504503
// startMariaDBService starts the mariadb service of systemd.
505504
func (c *Controller) startMariaDBService() error {
506-
if err := os.RemoveAll(mariadb.MasterInfoFilePath); err != nil {
505+
if err := c.mariaDBConnector.RemoveMasterInfo(); err != nil {
507506
return err
508507
}
509-
if err := os.RemoveAll(mariadb.RelayInfoFilePath); err != nil {
508+
if err := c.mariaDBConnector.RemoveRelayInfo(); err != nil {
510509
return err
511510
}
512511
if err := c.systemdConnector.StartService(mariadb.SystemdSerivceName); err != nil {

pkg/mariadb/connector.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package mariadb
1616

1717
import (
1818
"fmt"
19+
"os"
1920
"strings"
2021
"time"
2122

@@ -50,6 +51,10 @@ type Connector interface {
5051
CreateIDTable(dbName string, tableName string) error
5152
InsertIDRecord(dbName string, tableName string, id int) error
5253
DeleteRecords(dbName string, tableName string) error
54+
55+
// remove master info or relay info
56+
RemoveMasterInfo() error
57+
RemoveRelayInfo() error
5358
}
5459

5560
func NewDefaultConnector(logger *slog.Logger) Connector {
@@ -202,6 +207,30 @@ func (c *mySQLCommandConnector) runMysqlCommand(mysqlcmd string) ([]byte, error)
202207
return command.RunWithTimeout(mysqlCommandTimeout, name, args...)
203208
}
204209

210+
func (c *mySQLCommandConnector) RemoveMasterInfo() error {
211+
_, err := os.Stat(MasterInfoFilePath)
212+
213+
// do nothing if file is not found
214+
if err != nil {
215+
return nil
216+
}
217+
218+
// delete if file exists
219+
return os.Remove(MasterInfoFilePath)
220+
}
221+
222+
func (c *mySQLCommandConnector) RemoveRelayInfo() error {
223+
_, err := os.Stat(RelayInfoFilePath)
224+
225+
// do nothing if file is not found
226+
if err != nil {
227+
return nil
228+
}
229+
230+
// delete if file exists
231+
return os.Remove(RelayInfoFilePath)
232+
}
233+
205234
// parseShowReplicaStatusOutput parses the output of the "mysql -e 'show replica status \G'".
206235
func parseShowReplicaStatusOutput(out string) ReplicationStatus {
207236
m := ReplicationStatus{}

pkg/mariadb/fake_connector.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ func (c *FakeMariaDBConnector) InsertIDRecord(dbName string, tableName string, i
112112
return nil
113113
}
114114

115+
func (c *FakeMariaDBConnector) RemoveMasterInfo() error {
116+
return nil
117+
}
118+
119+
func (c *FakeMariaDBConnector) RemoveRelayInfo() error {
120+
return nil
121+
}
122+
115123
// FakeMariaDBFailWriteTestDataConnector is the mariadb connector that fails to write testdata.
116124
type FakeMariaDBFailWriteTestDataConnector struct {
117125
}
@@ -184,3 +192,11 @@ func (*FakeMariaDBFailWriteTestDataConnector) DeleteRecords(dbName string, table
184192
func (*FakeMariaDBFailWriteTestDataConnector) InsertIDRecord(dbName string, tableName string, id int) error {
185193
return nil
186194
}
195+
196+
func (c *FakeMariaDBFailWriteTestDataConnector) RemoveMasterInfo() error {
197+
return nil
198+
}
199+
200+
func (c *FakeMariaDBFailWriteTestDataConnector) RemoveRelayInfo() error {
201+
return nil
202+
}

pkg/mariadb/fake_mariadb_failed_replication_connector.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,13 @@ func (*FakeMariaDBFailedReplicationConnector) DeleteRecords(dbName string, table
8585
func (*FakeMariaDBFailedReplicationConnector) InsertIDRecord(dbName string, tableName string, id int) error {
8686
return nil
8787
}
88+
89+
// RemoveMasterInfo implements mariadb.Connector
90+
func (*FakeMariaDBFailedReplicationConnector) RemoveMasterInfo() error {
91+
return nil
92+
}
93+
94+
// RemoveRelayInfo implements mariadb.Connector
95+
func (*FakeMariaDBFailedReplicationConnector) RemoveRelayInfo() error {
96+
return nil
97+
}

0 commit comments

Comments
 (0)