forked from OpenAtomFoundation/pikiwidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentinel.go
More file actions
127 lines (106 loc) · 2.98 KB
/
sentinel.go
File metadata and controls
127 lines (106 loc) · 2.98 KB
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
// Copyright 2016 CodisLabs. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package redis
import (
"encoding/json"
"net"
"strconv"
"time"
"pika/codis/v2/pkg/models"
)
type SentinelMaster struct {
Addr string
Info map[string]string
Epoch int64
}
type MonitorConfig struct {
Quorum int
ParallelSyncs int
DownAfter time.Duration
FailoverTimeout time.Duration
NotificationScript string
ClientReconfigScript string
}
type SentinelGroup struct {
Master map[string]string `json:"master"`
Slaves []map[string]string `json:"slaves,omitempty"`
}
type InfoSlave struct {
IP string `json:"ip"`
Port string `json:"port"`
State string `json:"state"`
Offset int `json:"offset"`
Lag int `json:"lag"`
}
func (i *InfoSlave) UnmarshalJSON(b []byte) error {
var kvmap map[string]string
if err := json.Unmarshal(b, &kvmap); err != nil {
return err
}
i.IP = kvmap["ip"]
i.Port = kvmap["port"]
i.State = kvmap["state"]
if val, ok := kvmap["offset"]; ok {
if intval, err := strconv.Atoi(val); err == nil {
i.Offset = intval
}
}
if val, ok := kvmap["lag"]; ok {
if intval, err := strconv.Atoi(val); err == nil {
i.Lag = intval
}
}
return nil
}
type InfoReplication struct {
Role string `json:"role"`
ConnectedSlaves int `json:"connected_slaves"`
MasterHost string `json:"master_host"`
MasterPort string `json:"master_port"`
MasterLinkStatus string `json:"master_link_status"` // down; up
DbBinlogFileNum uint64 `json:"binlog_file_num"` // db0
DbBinlogOffset uint64 `json:"binlog_offset"` // db0
IsEligibleForMasterElection bool `json:"is_eligible_for_master_election"`
Slaves []InfoSlave `json:"-"`
}
type ReplicationState struct {
GroupID int
Index int
Addr string
Server *models.GroupServer
Replication *InfoReplication
Err error
}
func (i *InfoReplication) GetMasterAddr() string {
if len(i.MasterHost) == 0 {
return ""
}
return net.JoinHostPort(i.MasterHost, i.MasterPort)
}
func (i *InfoReplication) UnmarshalJSON(b []byte) error {
var kvmap map[string]string
if err := json.Unmarshal(b, &kvmap); err != nil {
return err
}
if val, ok := kvmap["connected_slaves"]; ok {
if intval, err := strconv.Atoi(val); err == nil {
i.ConnectedSlaves = intval
}
}
i.Role = kvmap["role"]
i.MasterPort = kvmap["master_port"]
i.MasterHost = kvmap["master_host"]
i.MasterLinkStatus = kvmap["master_link_status"]
i.IsEligibleForMasterElection = kvmap["is_eligible_for_master_election"] == "true"
if val, ok := kvmap["binlog_file_num"]; ok {
if intval, err := strconv.ParseUint(val, 10, 64); err == nil {
i.DbBinlogFileNum = intval
}
}
if val, ok := kvmap["binlog_offset"]; ok {
if intval, err := strconv.ParseUint(val, 10, 64); err == nil {
i.DbBinlogOffset = intval
}
}
return nil
}