-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.go
161 lines (135 loc) · 3.34 KB
/
cluster.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
package rqlite
import (
"encoding/json"
"net/http"
"strings"
"time"
)
type cluster struct {
peers []*peer // leader first
}
func (db *DB) peers() []*peer {
db.clusterMu.Lock()
pp := db.cluster.peers
db.clusterMu.Unlock()
return pp
}
func (db *DB) UpdateClusterInfo() error {
status, err := db.Status()
if err != nil {
return err
}
leaderRaftAddr := status.Store.Leader
var leader *peer
var otherPeers []*peer
for raftAddr, httpAddr := range status.Store.Meta.APIPeers {
hostPort := strings.Split(httpAddr, ":")
p := db.newPeer(hostPort[0], hostPort[1])
if leaderRaftAddr == raftAddr {
leader = p
} else {
otherPeers = append(otherPeers, p)
}
}
if leader == nil {
return ErrLeaderNotFound
}
pp := make([]*peer, 0, len(otherPeers)+1)
pp = append(pp, leader)
pp = append(pp, otherPeers...)
db.clusterMu.Lock()
db.cluster = cluster{peers: pp}
db.clusterMu.Unlock()
return nil
}
type ClusterStatus struct {
Runtime RuntimeStatus
HTTP HTTPStatus
Node NodeStatus
Store StoreStatus
LastBackup time.Time `json:"last_backup_time"`
Build BuildStatus
}
type RuntimeStatus struct {
GOARCH string
GOOS string
GOMAXPROCS int
NumCPU int
NumGoroutine int
Version string
}
type HTTPStatus struct {
Addr string
Auth string
Redirect string
ConnIdleTimeout string `json:"conn_idle_timeout"`
ConnTxTimeout string `json:"conn_tx_timeout"`
}
type BuildStatus struct {
Branch string
Commit string
Version string
BuildTime string `json:"build_time"` // TODO(br): Provide this as time.Time
}
type NodeStatus struct {
StartTime time.Time `json:"start_time"`
Uptime string // TODO(br): Provide this as time.Duration
}
type StoreStatus struct {
Addr string
ApplyTimeout string `json:"apply_timeout"`
DB DBStatus `json:"db_conf"`
Dir string
HeartBeatTimeout string `json:"heartbeat_timeout"`
Leader string
Meta MetaStatus
OpenTimeout string `json:"open_timeout"`
Peers []string
Raft RaftStatus
SnapshotThreshold int `json:"snapshot_threshold"`
Sqlite3 Sqlite3Status
}
type DBStatus struct {
DSN string
Memory bool
}
type MetaStatus struct {
APIPeers map[string]string
}
type RaftStatus struct {
AppliedIndex string `json:"applied_index"`
CommitIndex string `json:"commit_index"`
FsmPending string `json:"fsm_pending"`
LastContact string `json:"last_contact"`
LastLogIndex string `json:"last_log_index"`
LastLogTerm string `json:"last_log_term"`
LastSnapshotIndex string `json:"last_snapshot_index"`
LastSnapshotTerm string `json:"last_snapshot_term"`
NumPeers string `json:"num_peers"`
State string
Term string
}
type Sqlite3Status struct {
DNS string
FKConstraints string
Path string
Version string
}
func (db *DB) Status() (*ClusterStatus, error) {
pp := db.peers()
if len(pp) < 1 {
return nil, ErrNoPeers
}
for _, p := range pp {
resp, err := db.request(opSTATUS, false, http.MethodGet, p, nil)
if err != nil {
continue
}
clStatus := ClusterStatus{}
if err = json.Unmarshal(resp, &clStatus); err != nil {
return nil, err
}
return &clStatus, nil
}
return nil, ErrPeersUnavailable
}