-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathreplication.go
198 lines (171 loc) · 4.78 KB
/
replication.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
package aranGO
import (
"errors"
"time"
)
type CollectionParameters struct {
CollectionOptions
Id string `json:"cid"`
Version int `json:"version"`
Deleted bool `json:"deleted"`
}
type CollectionDump struct {
Parameters CollectionParameters `json:"parameters"`
Indexes []Index
}
type ReplicationState struct {
Running bool `json:"running"`
LastTick string `json:"lastLogTick"`
TotalEvents int64 `json:"totalEvents"`
Time time.Time `json:"time"`
}
type ReplicationInventory struct {
Collections []CollectionDump `json:"collections"`
State ReplicationState `json:"state"`
Tick string `json:"tick"`
}
// Inventory returns replication inventory
func (db *Database) Inventory() (*ReplicationInventory, error) {
var rinv ReplicationInventory
res, err := db.get("replication", "inventory", "GET", nil, &rinv, &rinv)
if err != nil {
return nil, err
}
switch res.Status() {
case 405, 500:
return nil, errors.New("Error when dumping replication info")
default:
return &rinv, nil
}
}
type ServerInfo struct {
Id string `json:"serverId"`
Version string `json:"version"`
}
type Logger struct {
State ReplicationState `json:"state"`
Server ServerInfo `json:"server"`
Client []string `json:"clients"`
}
func (db *Database) LoggerState() (*Logger, error) {
var log Logger
res, err := db.get("replication", "logger-state", "GET", nil, &log, &log)
if err != nil {
return nil, err
}
switch res.Status() {
case 405, 500:
return nil, errors.New("Logger state could not be determined")
default:
return &log, nil
}
}
type ApplierConf struct {
Endpoint string `json:"endpoint,omitempty"`
Database string `json:"database,omitempty"`
Username string `json:"username,omitempty"`
password string `json:"password,omitempty"`
Ssl int `json:"sslProtocol,omitempty"`
ReConnect int `json:"maxConnectRetries,omitempty"`
ConnectTimeout int `json:"connectTimeOut,omitempty"`
RequestTimeout int `json:"requestTimeOut,omitempty"`
Chunk int `json:"chunkSize,omitempty"`
AutoStart bool `json:"autoStart,omitempty"`
AdaptPolling bool `json:"adaptivePolling,omitempty"`
}
type ApplierProgress struct {
Time time.Time `json:"time"`
Message string `json:"message"`
Fails int `json:"failedConnects"`
}
type ApplierState struct {
Running bool `json:"running"`
Progress ApplierProgress `json:"progress"`
TotalRequests int `json:"totalRequests"`
FailConnects int `json:"totalFailedConnects"`
TotalEvents int `json:"totalEvents"`
Time time.Time `json:"time"`
}
type Applier struct {
State ApplierState `json:"state"`
Server ServerInfo `json:"server"`
Endpoint string `json:"endpoint"`
Database string `json:"database"`
}
func (db *Database) Applier() (*Applier, error) {
var appl Applier
res, err := db.get("replication", "applier-config", "GET", nil, &appl, &appl)
if err != nil {
return nil, err
}
switch res.Status() {
case 405, 500:
return nil, errors.New("Applier state could not be determined")
default:
return &appl, nil
}
}
func (db *Database) ApplierConf() (*ApplierConf, error) {
var appConf ApplierConf
res, err := db.get("replication", "applier-config", "GET", nil, &appConf, &appConf)
if err != nil {
return nil, err
}
switch res.Status() {
case 405, 500:
return nil, errors.New("Applier state could not be determined")
default:
return &appConf, nil
}
}
func (db *Database) SetApplierConf(appconf *ApplierConf) error {
if appconf == nil {
return errors.New("Invalid config")
}
res, err := db.send("replication", "applier-config", "PUT", appconf, nil, nil)
if err != nil {
return err
}
switch res.Status() {
case 400:
return errors.New("Configuration is incomplete or malformed or applier running")
case 405, 500:
return errors.New("Error occurred while assembling the response.")
default:
return nil
}
}
func (db *Database) StartReplication() error {
res, err := db.send("replication", "applier-start", "PUT", nil, nil, nil)
if err != nil {
return err
}
switch res.Status() {
case 400:
return errors.New("Invalid applier configuration")
case 405, 500:
return errors.New("Error starting replication")
default:
return nil
}
}
func (db *Database) StopReplication() error {
res, err := db.send("replication", "applier-stop", "PUT", nil, nil, nil)
if err != nil {
return err
}
switch res.Status() {
case 405, 500:
return errors.New("Error stoping replication")
default:
return nil
}
}
func (db *Database) ServerID() string {
server := map[string]string{}
_, err := db.get("replication", "server-id", "GET", nil, &server, &server)
if err != nil {
return ""
}
return server["serverId"]
}