forked from openark/orchestrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot_data.go
189 lines (165 loc) · 5.76 KB
/
snapshot_data.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
/*
Copyright 2017 Shlomi Noach, GitHub Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package logic
import (
"bytes"
"compress/gzip"
"encoding/json"
"io"
"github.com/github/orchestrator/go/db"
"github.com/github/orchestrator/go/inst"
"github.com/openark/golib/log"
"github.com/openark/golib/sqlutils"
)
type SnapshotData struct {
Keys []inst.InstanceKey
RecoveryDisabled bool
ClusterAlias,
ClusterAliasOverride,
ClusterDomainName,
HostAttributes,
AccessToken,
PoolInstances,
HostnameResolves,
HostnameUnresolves,
DowntimedInstances,
Candidates,
Detections,
KVStore,
Recovery,
RecoverySteps sqlutils.NamedResultData
}
func NewSnapshotData() *SnapshotData {
return &SnapshotData{}
}
func readTableData(tableName string, data *sqlutils.NamedResultData) error {
orcdb, err := db.OpenOrchestrator()
if err != nil {
return log.Errore(err)
}
*data, err = sqlutils.ScanTable(orcdb, tableName)
return log.Errore(err)
}
func writeTableData(tableName string, data *sqlutils.NamedResultData) error {
orcdb, err := db.OpenOrchestrator()
if err != nil {
return log.Errore(err)
}
err = sqlutils.WriteTable(orcdb, tableName, *data)
return log.Errore(err)
}
func CreateSnapshotData() *SnapshotData {
snapshotData := NewSnapshotData()
// keys
snapshotData.Keys, _ = inst.ReadAllInstanceKeys()
snapshotData.RecoveryDisabled, _ = IsRecoveryDisabled()
readTableData("cluster_alias", &snapshotData.ClusterAlias)
readTableData("cluster_alias_override", &snapshotData.ClusterAliasOverride)
readTableData("cluster_domain_name", &snapshotData.ClusterDomainName)
readTableData("access_token", &snapshotData.AccessToken)
readTableData("host_attributes", &snapshotData.HostAttributes)
readTableData("database_instance_pool", &snapshotData.PoolInstances)
readTableData("hostname_resolve", &snapshotData.HostnameResolves)
readTableData("hostname_unresolve", &snapshotData.HostnameUnresolves)
readTableData("database_instance_downtime", &snapshotData.DowntimedInstances)
readTableData("candidate_database_instance", &snapshotData.Candidates)
readTableData("topology_failure_detection", &snapshotData.Detections)
readTableData("kv_store", &snapshotData.KVStore)
readTableData("topology_recovery", &snapshotData.Recovery)
readTableData("topology_recovery_steps", &snapshotData.RecoverySteps)
log.Debugf("raft snapshot data created")
return snapshotData
}
type SnapshotDataCreatorApplier struct {
}
func NewSnapshotDataCreatorApplier() *SnapshotDataCreatorApplier {
generator := &SnapshotDataCreatorApplier{}
return generator
}
func (this *SnapshotDataCreatorApplier) GetData() (data []byte, err error) {
snapshotData := CreateSnapshotData()
b, err := json.Marshal(snapshotData)
if err != nil {
return b, err
}
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
if _, err := zw.Write(b); err != nil {
return b, err
}
if err := zw.Close(); err != nil {
return b, err
}
return buf.Bytes(), nil
}
func (this *SnapshotDataCreatorApplier) Restore(rc io.ReadCloser) error {
snapshotData := NewSnapshotData()
zr, err := gzip.NewReader(rc)
if err != nil {
return err
}
if err := json.NewDecoder(zr).Decode(&snapshotData); err != nil {
return err
}
// keys
{
snapshotInstanceKeyMap := inst.NewInstanceKeyMap()
snapshotInstanceKeyMap.AddKeys(snapshotData.Keys)
discardedKeys := 0
// Forget instances that were not in snapshot
existingKeys, _ := inst.ReadAllInstanceKeys()
for _, existingKey := range existingKeys {
if !snapshotInstanceKeyMap.HasKey(existingKey) {
inst.ForgetInstance(&existingKey)
discardedKeys++
}
}
log.Debugf("raft snapshot restore: discarded %+v keys", discardedKeys)
existingKeysMap := inst.NewInstanceKeyMap()
existingKeysMap.AddKeys(existingKeys)
// Discover instances that are in snapshot and not in our own database.
// Instances that _are_ in our own database will self-discover. No need
// to explicitly discover them.
discoveredKeys := 0
for _, snapshotKey := range snapshotData.Keys {
if !existingKeysMap.HasKey(snapshotKey) {
snapshotKey := snapshotKey
go func() {
snapshotDiscoveryKeys <- snapshotKey
}()
discoveredKeys++
}
}
log.Debugf("raft snapshot restore: discovered %+v keys", discoveredKeys)
}
writeTableData("cluster_alias", &snapshotData.ClusterAlias)
writeTableData("cluster_alias_override", &snapshotData.ClusterAliasOverride)
writeTableData("cluster_domain_name", &snapshotData.ClusterDomainName)
writeTableData("access_token", &snapshotData.AccessToken)
writeTableData("host_attributes", &snapshotData.HostAttributes)
writeTableData("database_instance_pool", &snapshotData.PoolInstances)
writeTableData("hostname_resolve", &snapshotData.HostnameResolves)
writeTableData("hostname_unresolve", &snapshotData.HostnameUnresolves)
writeTableData("database_instance_downtime", &snapshotData.DowntimedInstances)
writeTableData("candidate_database_instance", &snapshotData.Candidates)
writeTableData("kv_store", &snapshotData.KVStore)
writeTableData("topology_recovery", &snapshotData.Recovery)
writeTableData("topology_failure_detection", &snapshotData.Detections)
writeTableData("topology_recovery_steps", &snapshotData.RecoverySteps)
// recovery disable
{
SetRecoveryDisabled(snapshotData.RecoveryDisabled)
}
log.Debugf("raft snapshot restore applied")
return nil
}