From 2818d7e7c262a2307607238300e79c9033a3d027 Mon Sep 17 00:00:00 2001 From: Gautam Dey Date: Thu, 21 Jan 2016 21:41:28 -0500 Subject: [PATCH 1/3] This fixes issue #34. Using the wrong hostname. When registering as a slave we are using the wrong hostname. It should be our hostname, not the hostname name of the server we are connecting to. Fixed issue with using the wrong variable. --- replication/binlogsyncer.go | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/replication/binlogsyncer.go b/replication/binlogsyncer.go index 1b68e6638..1aabe170e 100644 --- a/replication/binlogsyncer.go +++ b/replication/binlogsyncer.go @@ -3,6 +3,7 @@ package replication import ( "encoding/binary" "fmt" + "os" "sync" "time" @@ -25,10 +26,12 @@ type BinlogSyncer struct { c *client.Conn serverID uint32 - host string - port uint16 - user string - password string + // LocalHost is the name of you want to present to the MySQL master. If it is not set it will default to os.Hostname() + LocalHost string + host string + port uint16 + user string + password string masterID uint32 @@ -62,6 +65,15 @@ func NewBinlogSyncer(serverID uint32, flavor string) *BinlogSyncer { return b } +func (b *BinlogSyncer) LocalHostname() string { + + if b.LocalHost == "" { + h, _ := os.Hostname() + return h + } + return b.LocalHost +} + func (b *BinlogSyncer) Close() { b.m.Lock() defer b.m.Unlock() @@ -222,11 +234,9 @@ func (b *BinlogSyncer) EnableSemiSync() error { } _, err := b.c.Execute(`SET @rpl_semi_sync_slave = 1;`) - if err != nil { b.semiSyncEnabled = true } - return errors.Trace(err) } @@ -402,7 +412,10 @@ func (b *BinlogSyncer) writeBinlogDumpMariadbGTIDCommand(gset GTIDSet) error { func (b *BinlogSyncer) writeRegisterSlaveCommand() error { b.c.ResetSequence() - data := make([]byte, 4+1+4+1+len(b.host)+1+len(b.user)+1+len(b.password)+2+4+4) + hostname := b.LocalHostname() + + // This should be the name of slave host not the host we are connecting to. + data := make([]byte, 4+1+4+1+len(hostname)+1+len(b.user)+1+len(b.password)+2+4+4) pos := 4 data[pos] = COM_REGISTER_SLAVE @@ -411,9 +424,10 @@ func (b *BinlogSyncer) writeRegisterSlaveCommand() error { binary.LittleEndian.PutUint32(data[pos:], b.serverID) pos += 4 - data[pos] = uint8(len(b.host)) + // This should be the name of slave hostname not the host we are connecting to. + data[pos] = uint8(len(hostname)) pos++ - n := copy(data[pos:], b.host) + n := copy(data[pos:], hostname) pos += n data[pos] = uint8(len(b.user)) From ec6c17b026a21be2b307f23f8074725eae284961 Mon Sep 17 00:00:00 2001 From: Gautam Dey Date: Fri, 22 Jan 2016 06:49:45 -0500 Subject: [PATCH 2/3] Renamed LocalHostname to localhostName. --- replication/binlogsyncer.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/replication/binlogsyncer.go b/replication/binlogsyncer.go index 1aabe170e..5650e482e 100644 --- a/replication/binlogsyncer.go +++ b/replication/binlogsyncer.go @@ -65,7 +65,7 @@ func NewBinlogSyncer(serverID uint32, flavor string) *BinlogSyncer { return b } -func (b *BinlogSyncer) LocalHostname() string { +func (b *BinlogSyncer) localhostName() string { if b.LocalHost == "" { h, _ := os.Hostname() @@ -412,7 +412,7 @@ func (b *BinlogSyncer) writeBinlogDumpMariadbGTIDCommand(gset GTIDSet) error { func (b *BinlogSyncer) writeRegisterSlaveCommand() error { b.c.ResetSequence() - hostname := b.LocalHostname() + hostname := b.localhostName() // This should be the name of slave host not the host we are connecting to. data := make([]byte, 4+1+4+1+len(hostname)+1+len(b.user)+1+len(b.password)+2+4+4) From fbc3c98393ab5c83f962198fa98c71034d887126 Mon Sep 17 00:00:00 2001 From: Gautam Dey Date: Mon, 25 Jan 2016 13:51:39 -0500 Subject: [PATCH 3/3] Change hostname function name and hid variable. Made the LocalHost variable private and changed it's name to localhost. Added additional funciton SetLocalHostname to set the hostname to register as. Change getter methods name to match setter. Change it to LocalHostname(). --- replication/binlogsyncer.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/replication/binlogsyncer.go b/replication/binlogsyncer.go index 5650e482e..fcbbf327a 100644 --- a/replication/binlogsyncer.go +++ b/replication/binlogsyncer.go @@ -26,8 +26,7 @@ type BinlogSyncer struct { c *client.Conn serverID uint32 - // LocalHost is the name of you want to present to the MySQL master. If it is not set it will default to os.Hostname() - LocalHost string + localhost string host string port uint16 user string @@ -65,13 +64,19 @@ func NewBinlogSyncer(serverID uint32, flavor string) *BinlogSyncer { return b } -func (b *BinlogSyncer) localhostName() string { +// LocalHostname returns the hostname that register slave would register as. +func (b *BinlogSyncer) LocalHostname() string { - if b.LocalHost == "" { + if b.localhost == "" { h, _ := os.Hostname() return h } - return b.LocalHost + return b.localhost +} + +// SetLocalHostname set's the hostname that register salve would register as. +func (b *NewBinlogSyncer) SetLocalHostname(name string) { + b.localhost = name } func (b *BinlogSyncer) Close() { @@ -412,7 +417,7 @@ func (b *BinlogSyncer) writeBinlogDumpMariadbGTIDCommand(gset GTIDSet) error { func (b *BinlogSyncer) writeRegisterSlaveCommand() error { b.c.ResetSequence() - hostname := b.localhostName() + hostname := b.LocalHostname() // This should be the name of slave host not the host we are connecting to. data := make([]byte, 4+1+4+1+len(hostname)+1+len(b.user)+1+len(b.password)+2+4+4)