-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
361 lines (284 loc) · 9.81 KB
/
utils.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
Package comment should be here. package description.
*/
package main
import (
as "github.com/aerospike/aerospike-client-go"
. "github.com/sud82/aerospike-data-sync/logger"
"encoding/hex"
"errors"
"fmt"
"os"
"path"
"strconv"
"strings"
"time"
// TLS related
"crypto/tls"
"crypto/x509"
"io/ioutil"
"github.com/BurntSushi/toml"
)
type FindError struct {
// Error returned by server while scaning records
ScanReqErr int
// Other error, Timeout, no connection pool etc
Err int
}
type SyncError struct {
// Generation error at (check and write) operation
GenErr int
// Other error
Err int
}
type TStats struct {
// Total checked objects
NObj int
// Num of Sampled objects
NSampleObj int
// Track total scanned records
NScanObj int
// Stats for records found not in sync
RecNotInSyncTotal int
RecNotInSyncUpdated int
RecNotInSyncInserted int
RecNotInSyncDeleted int
// Stats for records synced
RecSyncedTotal int
RecSyncedUpdated int
RecSyncedInserted int
RecSyncedDeleted int
FindSync FindError
DoSync SyncError
}
// Used in record sync log file. Each line will start with I,U,D based on
// operation by which that record went out of sync.
const (
VERSION_NUM = "1.0"
// Type of OPeration
INSERTED_OP = "I"
UPDATED_OP = "U"
DELETED_OP = "D"
FIELD_DEL = "#"
// HEADER keywords
// Version:1.0#Mod_after:#Mod_before:Aug 17, 2017 at 12:14am (PDT)#Namespace:test#Bins:
HEADER_KEYVAL_DEL = ":"
HEADER_VERSION = "Version"
HEADER_MOD_AFTER = "Mod_after"
HEADER_MOD_BEFORE = "Mod_before"
HEADER_NAMESPACE = "Namespace"
HEADER_BINS = "Bins"
HEADER_OFFSET_VERSION = 0
HEADER_OFFSET_MOD_AFTER = 1
HEADER_OFFSET_MOD_BEFORE = 2
HEADER_OFFSET_NAMESPACE = 3
HEADER_OFFSET_BINS = 4
// 2nd header line
HEADER_ACTION = "Action"
HEADER_DIGEST = "Digest"
HEADER_SET = "Set"
HEADER_GEN = "Gen"
// RECORD info line (OP:Digest:Set:Gen)
REC_LINE_ARGS = 4
REC_LINE_OFFSET_OP = 0
REC_LINE_OFFSET_DG = 1
REC_LINE_OFFSET_SET = 2
REC_LINE_OFFSET_GEN = 3
// ~50B per lines so ~250MB file
UNSYNC_REC_INFO_FILE_LINES_COUNT = 5000000
)
// Global used by all modules
var (
// Used by time parser
TimeLayout string = "Jan 2, 2006 at 3:04pm (MST)"
// AS client related
SrcClient *as.Client = nil
DstClient *as.Client = nil
// Global stats to track synced, unsynced records
GStat TStats
// Track stats for all sets within namespace
SetStats = map[string]*TStats{}
)
//----------------------------------------------------------------------------
// Other helper functions
//----------------------------------------------------------------------------
// Create a line containing op,digest,set,gen info.
func GetRecordLogInfoLine(op string, key *as.Key, gen uint32) string {
// op:digest:setname:dstRecordGen
if op == "" {
fmt.Println("Null op")
}
del := FIELD_DEL
dg := getKeyDigestString(key)
return op + del + dg + del + key.SetName() + del + strconv.FormatUint(uint64(gen), 10) + "\n"
}
// Get digest string from aerospike key
func getKeyDigestString(key *as.Key) string {
dg := key.Digest()
hlist := make([]byte, 2*len(dg))
for i := range dg {
hex := fmt.Sprintf("%02x ", dg[i])
idx := i * 2
copy(hlist[idx:], hex)
}
return string(hlist)
}
func GetLogFileHeader(version string, modAfter int64, modBefore int64, ns string, set string, binList []string) string {
// #ver:1#mod_after:1212334#mod_before:123233#ns:test#set:testset#bins:b1,b2,b3#
modAfterString := timestampToTimeString(modAfter)
modBeforeString := timestampToTimeString(modBefore)
binString := strings.Join(binList, ",")
hd := FIELD_DEL
d := HEADER_KEYVAL_DEL
return HEADER_VERSION + d + version + hd + HEADER_MOD_AFTER + d + modAfterString +
hd + HEADER_MOD_BEFORE + d + modBeforeString + hd + HEADER_NAMESPACE + d + ns +
hd + HEADER_BINS + d + binString + "\n" +
HEADER_ACTION + hd + HEADER_DIGEST + hd + HEADER_SET + hd + HEADER_GEN + "\n"
}
// Format time to given format type
func timestampToTimeString(timestamp int64) string {
if timestamp == 0 {
return ""
}
return time.Unix(0, timestamp).Format(TimeLayout)
}
func InitUnsyncRecInfoFile() {
filename := "Data" + "_" + strconv.Itoa(UnsyncRecInfoFileCount)
filepath := path.Join(UnsyncRecInfoDir, filename)
UnsyncRecInfoFileCount++
UnsyncRecInfoFile = filepath
// create and write header in file
Logger.Info("Create new Unsync Record info file: %s", filepath)
file, err := os.OpenFile(filepath, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
PanicOnError(err)
}
defer file.Close()
header := GetLogFileHeader(VERSION_NUM, ModAfter, ModBefore, Namespace, Set, BinList)
if _, err = file.WriteString(header); err != nil {
PanicOnError(err)
}
}
func PanicOnError(err error) {
if err != nil {
Logger.Error(err.Error())
panic(err)
}
}
//---------------------------------------------------------------------------
// Do Sync
//---------------------------------------------------------------------------
// Parse and validate header line of unsync_record_info file.
// TODO: validate second line too..not needed.
func ParseHeaderLine(headerLine string) {
headerOps := strings.Split(headerLine, FIELD_DEL)
headerVer := getValidateArg(headerOps[HEADER_OFFSET_VERSION], HEADER_VERSION)
if VERSION_NUM != headerVer {
PanicOnError(errors.New("Invalid version: " + headerVer + " expected: " + VERSION_NUM))
}
ModAfterString = getValidateArg(headerOps[HEADER_OFFSET_MOD_AFTER], HEADER_MOD_AFTER)
ModAfter = timeStringToTimestamp(ModAfterString)
ModBeforeString = getValidateArg(headerOps[HEADER_OFFSET_MOD_BEFORE], HEADER_MOD_BEFORE)
ModBefore = timeStringToTimestamp(ModBeforeString)
Namespace = getValidateArg(headerOps[HEADER_OFFSET_NAMESPACE], HEADER_NAMESPACE)
BinString = getValidateArg(headerOps[HEADER_OFFSET_BINS], HEADER_BINS)
if BinString != "" {
BinList = strings.Split(BinString, ",")
}
}
// Validate header arguments name
func getValidateArg(str string, validStr string) string {
args := strings.SplitN(str, HEADER_KEYVAL_DEL, 2)
if args[0] != validStr {
PanicOnError(errors.New("Invalid header string: " + str))
}
return args[1]
}
// Get UnixNano timestamp from time
func timeStringToTimestamp(timeString string) int64 {
if timeString == "" {
return 0
}
// Get timestamp from timestring
// TimeLayout, "Jul 5, 2017 at 11:55am (GMT)")
parsedTime, err := time.Parse(TimeLayout, timeString)
PanicOnError(err)
return parsedTime.In(time.UTC).UnixNano()
}
// Parse rec info line from unsync_record_info file and get as.Key
func GetKeyFromString(ns string, recInfoLine string) (*as.Key, error) {
recInfoList := strings.Split(recInfoLine, FIELD_DEL)
keyDigest := recInfoList[REC_LINE_OFFSET_DG]
byteDigest, err := hex.DecodeString(keyDigest)
PanicOnError(err)
return as.NewKeyWithDigest(ns, recInfoList[REC_LINE_OFFSET_SET], "", byteDigest)
}
// Calculate total recSynced
func CalcTotalRecSynced(setSts map[string]*TStats) int {
var totalSynced int = 0
for _, obj := range setSts {
if !FindOnly {
obj.RecSyncedTotal = obj.RecSyncedUpdated + obj.RecSyncedInserted +
obj.RecSyncedDeleted
totalSynced += obj.RecSyncedTotal
}
}
return totalSynced
}
//-------------------------------------------------------------------------------
// TLS related
//-------------------------------------------------------------------------------
type Config struct {
TLS struct {
ServerPool []string `toml:"server_cert_pool"`
ClientPool map[string]struct {
CertFile string `toml:"cert_file"`
KeyFile string `toml:"key_file"`
} `toml:"client_certs"`
EncryptOnly bool `toml:"encrypt_only"`
} `toml:"tls"`
serverPool *x509.CertPool
clientPool []tls.Certificate
}
var TLSConfig Config
func (c *Config) ServerPool() *x509.CertPool {
return c.serverPool
}
func (c *Config) ClientPool() []tls.Certificate {
return c.clientPool
}
func InitTLSConfig(configFile string) {
// to print everything out regarding reading the config in app init
// log.SetLevel(log.DebugLevel)
if _, err := toml.DecodeFile(configFile, &TLSConfig); err != nil {
fmt.Println(err)
return
}
// Try to load system CA certs, otherwise just make an empty pool
serverPool, err := x509.SystemCertPool()
if err != nil {
Logger.Error("FAILED: Adding system certificates to the pool failed: " + err.Error())
serverPool = x509.NewCertPool()
}
// Try to load system CA certs and add them to the system cert pool
for _, caFile := range TLSConfig.TLS.ServerPool {
caCert, err := ioutil.ReadFile(caFile)
if err != nil {
Logger.Error("FAILED: Adding server certificate " + caFile + " to the pool failed: " + err.Error())
continue
}
Logger.Debug("Adding server certificate to the pool: " + caFile)
serverPool.AppendCertsFromPEM(caCert)
}
TLSConfig.serverPool = serverPool
// Try to load system CA certs and add them to the system cert pool
for _, cFiles := range TLSConfig.TLS.ClientPool {
cert, err := tls.LoadX509KeyPair(cFiles.CertFile, cFiles.KeyFile)
if err != nil {
Logger.Error("FAILED: Adding client certificate " + cFiles.CertFile + " to the pool failed: " + err.Error())
continue
}
Logger.Debug("Adding client certificate to the pool: " + cFiles.CertFile)
TLSConfig.clientPool = append(TLSConfig.clientPool, cert)
}
}