-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
executable file
·226 lines (184 loc) · 5.35 KB
/
monitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"errors"
"fmt"
"log"
"net"
"os"
"strconv"
"sync"
"time"
"github.com/bvisonl/redis-seer/redis"
)
var (
monitorConnections map[string]net.Conn = make(map[string]net.Conn, 0)
mutex = &sync.Mutex{}
)
func startMonitor() {
// Establish connection with all the servers
for key, server := range seerConfig.Servers {
if server.Enabled == false {
continue
}
redisConnection, err := net.Dial("tcp4", server.Host+":"+strconv.Itoa(server.Port))
monitorConnections[key] = redisConnection
if err != nil {
log.Printf("An error occurred connection to server: %s. Error: %s\r\n", key, err)
continue
}
if seerConfig.Master == "" {
err = getMasterRedis(key, redisConnection)
if err != nil {
log.Printf(err.Error())
os.Exit(1)
}
}
defer redisConnection.Close()
}
// Monitor all servers in the list
for name, redisConnection := range monitorConnections {
go monitor(name, redisConnection)
}
fmt.Println("Current master is " + seerConfig.Master)
}
func getMasterRedis(name string, redisConnection net.Conn) (err error) {
// Get the replication information
writer := redis.NewRESPWriter(redisConnection)
err = writer.WriteCommand("INFO", "replication")
if err != nil {
return err
}
// Wait for the answer
reader := redis.NewReader(redisConnection)
result, err := reader.ReadObject()
if err != nil {
return err
}
info, _ := redis.InfoToMap(result)
if info["role"] == "master" {
seerConfig.Master = name
return nil
} else {
for name, server := range seerConfig.Servers {
if server.Host == info["master_host"] && strconv.Itoa(server.Port) == info["master_port"] {
seerConfig.Master = name
return nil
}
}
}
return errors.New("Unable to find a suitable Master")
}
func monitor(name string, redisConnection net.Conn) {
Connect:
// Mark the server as reconnecting
mutex.Lock()
seerConfig.Servers[name].Status = SERVER_STATUS_INACTIVE
mutex.Unlock()
// Step 1 - Connect to the server
redisConnection, err := net.Dial("tcp4", seerConfig.Servers[name].Host+":"+strconv.Itoa(seerConfig.Servers[name].Port))
if err != nil {
if seerConfig.Master == name {
seerConfig.Master = "" // To avoid duplicate call of masterFailover
go masterFailover()
}
log.Printf("Error connecting to redis %s. Will attempt to reconnect in the next interval. Error: %s\r\n", name, err)
// Wait for the next interval to retry
time.Sleep(monitorInterval)
goto Connect
}
// Close the connection when done
defer redisConnection.Close()
// Update server status and connection
mutex.Lock()
monitorConnections[name] = redisConnection
seerConfig.Servers[name].Status = SERVER_STATUS_ACTIVE
mutex.Unlock()
log.Printf("Started monitoring %s.\r\n", name)
// Check the current master
checkMaster(name, redisConnection)
for {
// Send PING to server
writer := redis.NewRESPWriter(redisConnection)
err := writer.WriteCommand("PING")
if err != nil {
log.Printf("Error sending PING to redis %s. Will attempt to reconnect in the next interval. Error: %s\r\n", name, err)
time.Sleep(monitorInterval)
goto Connect
}
// Receive PONG
redisReader := redis.NewReader(redisConnection)
_, err = redisReader.ReadObject()
if err != nil {
fmt.Printf("Error reading data from Redis %s. Error: %s\r\n", name, err)
time.Sleep(monitorInterval)
goto Connect
}
// Wait for monitor interval
time.Sleep(monitorInterval)
}
}
func masterFailover() (err error) {
// Look for any active Slave
activeSlave := ""
for name, server := range seerConfig.Servers {
if server.Status == SERVER_STATUS_ACTIVE {
activeSlave = name
}
}
// Get the connection to that slave
redisConnection := monitorConnections[activeSlave]
if redisConnection == nil {
log.Printf("No active servers.")
return errors.New("No active servers found.")
}
// Get the info
writer := redis.NewRESPWriter(redisConnection)
err = writer.WriteCommand("SLAVEOF", "NO", "ONE")
if err != nil {
return err
}
seerConfig.Master = activeSlave
fmt.Printf("Promoted %s to master\r\n", seerConfig.Master)
for name, connection := range monitorConnections {
if name != activeSlave {
setMasterRedis(name, connection)
}
}
return nil
}
func checkMaster(name string, redisConnection net.Conn) (err error) {
if name == seerConfig.Master {
return nil
}
// Get the replication information
writer := redis.NewRESPWriter(redisConnection)
err = writer.WriteCommand("INFO", "replication")
if err != nil {
return err
}
// Wait for the answer
reader := redis.NewReader(redisConnection)
result, err := reader.ReadObject()
if err != nil {
return err
}
info, _ := redis.InfoToMap(result)
if info["role"] == "slave" && (seerConfig.Servers[seerConfig.Master].Host == info["master_host"] || strconv.Itoa(seerConfig.Servers[seerConfig.Master].Port) != info["master_port"]) {
// If the master is different than the one we have, change it
setMasterRedis(name, redisConnection)
}
return nil
}
func setMasterRedis(name string, redisConnection net.Conn) (err error) {
writer := redis.NewRESPWriter(redisConnection)
err = writer.WriteCommand("SLAVEOF", seerConfig.Servers[seerConfig.Master].Host, strconv.Itoa(seerConfig.Servers[seerConfig.Master].Port))
if err != nil {
return err
}
reader := redis.NewReader(redisConnection)
_, err = reader.ReadObject()
if err != nil {
return err
}
return nil
}