forked from rwynn/monstache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monstache.go
918 lines (862 loc) · 29 KB
/
monstache.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
package main
import (
"bytes"
"compress/gzip"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/BurntSushi/toml"
elastigo "github.com/mattbaird/elastigo/lib"
"github.com/robertkrimen/otto"
_ "github.com/robertkrimen/otto/underscore"
"github.com/rwynn/gtm"
"github.com/rwynn/gtm/consistent"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/signal"
"regexp"
"strconv"
"strings"
"syscall"
"time"
)
var gridByteBuffer bytes.Buffer
var infoLog *log.Logger = log.New(os.Stdout, "INFO ", log.Flags())
var mapEnvs map[string]*executionEnv
var mapIndexTypes map[string]*indexTypeMapping
var fileNamespaces map[string]bool
var chunksRegex = regexp.MustCompile("\\.chunks$")
var systemsRegex = regexp.MustCompile("system\\..+$")
const Version = "2.8.1"
const mongoUrlDefault string = "localhost"
const resumeNameDefault string = "default"
const elasticMaxConnsDefault int = 10
const gtmChannelSizeDefault int = 100
type executionEnv struct {
Vm *otto.Otto
Script string
}
type javascript struct {
Namespace string
Script string
}
type indexTypeMapping struct {
Namespace string
Index string
Type string
}
type configOptions struct {
MongoUrl string `toml:"mongo-url"`
MongoPemFile string `toml:"mongo-pem-file"`
MongoOpLogDatabaseName string `toml:"mongo-oplog-database-name"`
MongoOpLogCollectionName string `toml:"mongo-oplog-collection-name"`
MongoCursorTimeout string `toml:"mongo-cursor-timeout"`
ElasticUrl string `toml:"elasticsearch-url"`
ElasticPemFile string `toml:"elasticsearch-pem-file"`
ResumeName string `toml:"resume-name"`
NsRegex string `toml:"namespace-regex"`
NsExcludeRegex string `toml:"namespace-exclude-regex"`
Version bool
Gzip bool
Verbose bool
Resume bool
ResumeWriteUnsafe bool `toml:"resume-write-unsafe"`
Replay bool
DroppedDatabases bool `toml:"dropped-databases"`
DroppedCollections bool `toml:"dropped-collections"`
IndexFiles bool `toml:"index-files"`
FileHighlighting bool `toml:"file-highlighting"`
ElasticMaxConns int `toml:"elasticsearch-max-conns"`
ElasticRetrySeconds int `toml:"elasticsearch-retry-seconds"`
ElasticMaxDocs int `toml:"elasticsearch-max-docs"`
ElasticMaxBytes int `toml:"elasticsearch-max-bytes"`
ElasticMaxSeconds int `toml:"elasticsearch-max-seconds"`
ElasticHosts []string `toml:"elasticsearch-hosts"`
ElasticMajorVersion int
ChannelSize int `toml:"gtm-channel-size"`
MaxFileSize int64 `toml:"max-file-size"`
ConfigFile string
Script []javascript
Mapping []indexTypeMapping
FileNamespaces []string `toml:"file-namespaces"`
Workers []string
Worker string
}
func TestElasticSearchConn(conn *elastigo.Conn, configuration *configOptions) (err error) {
var result map[string]interface{}
body, err := conn.DoCommand("GET", "/", nil, nil)
if err != nil {
return
}
err = json.Unmarshal(body, &result)
if err == nil {
version := result["version"].(map[string]interface{})
if version == nil {
err = errors.New("Unable to determine elasticsearch version")
} else {
number := version["number"].(string)
if number == "" {
err = errors.New("Unable to determine elasticsearch version")
} else if configuration.Verbose {
infoLog.Printf("Successfully connected to elasticsearch version %s", number)
}
versionParts := strings.Split(number, ".")
if len(versionParts) > 0 {
version, err := strconv.Atoi(versionParts[0])
if err == nil {
configuration.ElasticMajorVersion = version
}
} else {
err = errors.New("Unable to parse elasticsearch version")
}
}
}
return
}
func NormalizeIndexName(name string) (normal string) {
normal = strings.ToLower(strings.TrimPrefix(name, "_"))
return
}
func NormalizeTypeName(name string) (normal string) {
normal = strings.TrimPrefix(name, "_")
return
}
func NormalizeEsId(id string) (normal string) {
normal = strings.TrimPrefix(id, "_")
return
}
func DeleteIndexes(conn *elastigo.Conn, db string, configuration *configOptions) (err error) {
for ns, m := range mapIndexTypes {
parts := strings.SplitN(ns, ".", 2)
if parts[0] == db {
if _, err = conn.DeleteIndex(m.Index + "*"); err != nil {
return
}
}
}
_, err = conn.DeleteIndex(NormalizeIndexName(db) + "*")
return
}
func DeleteIndex(conn *elastigo.Conn, namespace string, configuration *configOptions) (err error) {
esIndex := NormalizeIndexName(namespace)
if m := mapIndexTypes[namespace]; m != nil {
esIndex = m.Index
}
_, err = conn.DeleteIndex(esIndex)
return err
}
func IngestAttachment(conn *elastigo.Conn, esIndex string, esType string, esId string, data map[string]interface{}) (err error) {
var body []byte
args := map[string]interface{}{
"pipeline": "attachment",
}
body, err = json.Marshal(data)
if err == nil {
_, err = conn.DoCommand("PUT", fmt.Sprintf("/%s/%s/%s", esIndex, esType, esId), args, string(body))
}
return err
}
func EnsureFileMapping(conn *elastigo.Conn, namespace string, configuration *configOptions) (err error) {
if configuration.ElasticMajorVersion < 5 {
return EnsureFileMappingMapperAttachment(conn, namespace, configuration)
} else {
return EnsureFileMappingIngestAttachment(conn, namespace, configuration)
}
}
func EnsureFileMappingIngestAttachment(conn *elastigo.Conn, namespace string, configuration *configOptions) (err error) {
var body []byte
pipeline := map[string]interface{}{
"description": "Extract file information",
"processors": [1]map[string]interface{}{
map[string]interface{}{
"attachment": map[string]interface{}{
"field": "file",
},
},
},
}
body, err = json.Marshal(pipeline)
if err == nil {
_, err = conn.DoCommand("PUT", "/_ingest/pipeline/attachment", nil, string(body))
}
return err
}
func EnsureFileMappingMapperAttachment(conn *elastigo.Conn, namespace string, configuration *configOptions) (err error) {
var body []byte
parts := strings.SplitN(namespace, ".", 2)
esIndex, esType := NormalizeIndexName(namespace), NormalizeTypeName(parts[1])
if m := mapIndexTypes[namespace]; m != nil {
esIndex, esType = m.Index, m.Type
}
props := map[string]interface{}{
"properties": map[string]interface{}{
"file": map[string]interface{}{
"type": "attachment",
},
},
}
file := props["properties"].(map[string]interface{})["file"].(map[string]interface{})
types := map[string]interface{}{
esType: props,
}
mappings := map[string]interface{}{
"mappings": types,
}
if configuration.FileHighlighting {
file["fields"] = map[string]interface{}{
"content": map[string]interface{}{
"type": "string",
"term_vector": "with_positions_offsets",
"store": true,
},
}
}
if exists, _ := conn.ExistsIndex(esIndex, "", nil); exists {
body, err = json.Marshal(types)
if err != nil {
return err
}
_, err = conn.DoCommand("PUT", fmt.Sprintf("/%s/%s/_mapping", esIndex, esType), nil, string(body))
} else {
body, err = json.Marshal(mappings)
if err != nil {
return err
}
_, err = conn.DoCommand("PUT", fmt.Sprintf("/%s", esIndex), nil, string(body))
}
return err
}
func DefaultIndexTypeMapping(op *gtm.Op) *indexTypeMapping {
return &indexTypeMapping{
Namespace: op.Namespace,
Index: NormalizeIndexName(op.Namespace),
Type: NormalizeTypeName(op.GetCollection()),
}
}
func IndexTypeMapping(op *gtm.Op) *indexTypeMapping {
mapping := DefaultIndexTypeMapping(op)
if mapIndexTypes != nil {
if m := mapIndexTypes[op.Namespace]; m != nil {
mapping = m
}
}
return mapping
}
func OpIdToString(op *gtm.Op) string {
var opIdStr string
switch op.Id.(type) {
case bson.ObjectId:
opIdStr = op.Id.(bson.ObjectId).Hex()
default:
opIdStr = NormalizeEsId(fmt.Sprintf("%v", op.Id))
}
return opIdStr
}
func MapData(op *gtm.Op) error {
if mapEnvs == nil {
return nil
}
if env := mapEnvs[op.Namespace]; env != nil {
val, err := env.Vm.Call("module.exports", op.Data, op.Data)
if err != nil {
return err
}
if strings.ToLower(val.Class()) == "object" {
data, err := val.Export()
if err != nil {
return err
} else if data == val {
return errors.New("exported function must return an object")
} else {
op.Data = data.(map[string]interface{})
}
} else {
indexed, err := val.ToBoolean()
if err != nil {
return err
} else if !indexed {
op.Data = nil
}
}
}
return nil
}
func PrepareDataForIndexing(data map[string]interface{}) {
delete(data, "_id")
delete(data, "_type")
delete(data, "_index")
delete(data, "_score")
delete(data, "_source")
}
func AddFileContent(session *mgo.Session, op *gtm.Op, configuration *configOptions) (err error) {
op.Data["file"] = ""
gridByteBuffer.Reset()
db, bucket :=
session.DB(op.GetDatabase()),
strings.SplitN(op.GetCollection(), ".", 2)[0]
encoder := base64.NewEncoder(base64.StdEncoding, &gridByteBuffer)
file, err := db.GridFS(bucket).OpenId(op.Id)
if err != nil {
return
}
defer file.Close()
if configuration.MaxFileSize > 0 {
if file.Size() > configuration.MaxFileSize {
infoLog.Printf("file %s md5(%s) exceeds max file size. file content omitted.",
file.Name(), file.MD5())
return
}
}
if _, err = io.Copy(encoder, file); err != nil {
return
}
if err = encoder.Close(); err != nil {
return
}
op.Data["file"] = string(gridByteBuffer.Bytes())
return
}
func NotMonstache(op *gtm.Op) bool {
return op.GetDatabase() != "monstache"
}
func NotChunks(op *gtm.Op) bool {
return !chunksRegex.MatchString(op.GetCollection())
}
func NotSystem(op *gtm.Op) bool {
return !systemsRegex.MatchString(op.GetCollection())
}
func FilterWithRegex(regex string) gtm.OpFilter {
var validNameSpace = regexp.MustCompile(regex)
return func(op *gtm.Op) bool {
return validNameSpace.MatchString(op.Namespace)
}
}
func FilterInverseWithRegex(regex string) gtm.OpFilter {
var invalidNameSpace = regexp.MustCompile(regex)
return func(op *gtm.Op) bool {
return !invalidNameSpace.MatchString(op.Namespace)
}
}
func SaveTimestamp(session *mgo.Session, op *gtm.Op, resumeName string) error {
col := session.DB("monstache").C("monstache")
doc := make(map[string]interface{})
doc["ts"] = op.Timestamp
_, err := col.UpsertId(resumeName, bson.M{"$set": doc})
return err
}
func (configuration *configOptions) ParseCommandLineFlags() *configOptions {
flag.StringVar(&configuration.MongoUrl, "mongo-url", "", "MongoDB connection URL")
flag.StringVar(&configuration.MongoPemFile, "mongo-pem-file", "", "Path to a PEM file for secure connections to MongoDB")
flag.StringVar(&configuration.MongoOpLogDatabaseName, "mongo-oplog-database-name", "", "Override the database name which contains the mongodb oplog")
flag.StringVar(&configuration.MongoOpLogCollectionName, "mongo-oplog-collection-name", "", "Override the collection name which contains the mongodb oplog")
flag.StringVar(&configuration.MongoCursorTimeout, "mongo-cursor-timeout", "", "Override the duration before a cursor timeout occurs when tailing the oplog")
flag.StringVar(&configuration.ElasticUrl, "elasticsearch-url", "", "ElasticSearch connection URL")
flag.StringVar(&configuration.ElasticPemFile, "elasticsearch-pem-file", "", "Path to a PEM file for secure connections to elasticsearch")
flag.IntVar(&configuration.ElasticMaxConns, "elasticsearch-max-conns", 0, "ElasticSearch max connections")
flag.IntVar(&configuration.ElasticRetrySeconds, "elasticsearch-retry-seconds", 0, "Number of seconds before retrying ElasticSearch requests")
flag.IntVar(&configuration.ElasticMaxDocs, "elasticsearch-max-docs", 0, "Number of docs to hold before flushing to ElasticSearch")
flag.IntVar(&configuration.ElasticMaxBytes, "elasticsearch-max-bytes", 0, "Number of bytes to hold before flushing to ElasticSearch")
flag.IntVar(&configuration.ElasticMaxSeconds, "elasticsearch-max-seconds", 0, "Number of seconds before flushing to ElasticSearch")
flag.IntVar(&configuration.ChannelSize, "gtm-channel-size", 0, "Size of gtm channels")
flag.Int64Var(&configuration.MaxFileSize, "max-file-size", 0, "GridFs file content exceeding this limit in bytes will not be indexed in ElasticSearch")
flag.StringVar(&configuration.ConfigFile, "f", "", "Location of configuration file")
flag.BoolVar(&configuration.DroppedDatabases, "dropped-databases", true, "True to delete indexes from dropped databases")
flag.BoolVar(&configuration.DroppedCollections, "dropped-collections", true, "True to delete indexes from dropped collections")
flag.BoolVar(&configuration.Version, "v", false, "True to print the version number")
flag.BoolVar(&configuration.Gzip, "gzip", false, "True to use gzip for requests to elasticsearch")
flag.BoolVar(&configuration.Verbose, "verbose", false, "True to output verbose messages")
flag.BoolVar(&configuration.Resume, "resume", false, "True to capture the last timestamp of this run and resume on a subsequent run")
flag.BoolVar(&configuration.ResumeWriteUnsafe, "resume-write-unsafe", false, "True to speedup writes of the last timestamp synched for resuming at the cost of error checking")
flag.BoolVar(&configuration.Replay, "replay", false, "True to replay all events from the oplog and index them in elasticsearch")
flag.BoolVar(&configuration.IndexFiles, "index-files", false, "True to index gridfs files into elasticsearch. Requires the elasticsearch mapper-attachments (deprecated) or ingest-attachment plugin")
flag.BoolVar(&configuration.FileHighlighting, "file-highlighting", false, "True to enable the ability to highlight search times for a file query")
flag.StringVar(&configuration.ResumeName, "resume-name", "", "Name under which to load/store the resume state. Defaults to 'default'")
flag.StringVar(&configuration.Worker, "worker", "", "The name of this worker in a multi-worker configuration")
flag.StringVar(&configuration.NsRegex, "namespace-regex", "", "A regex which is matched against an operation's namespace (<database>.<collection>). Only operations which match are synched to elasticsearch")
flag.StringVar(&configuration.NsRegex, "namespace-exclude-regex", "", "A regex which is matched against an operation's namespace (<database>.<collection>). Only operations which do not match are synched to elasticsearch")
flag.Parse()
return configuration
}
func (configuration *configOptions) LoadIndexTypes() {
if configuration.Mapping != nil {
mapIndexTypes = make(map[string]*indexTypeMapping)
for _, m := range configuration.Mapping {
if m.Namespace != "" && m.Index != "" && m.Type != "" {
mapIndexTypes[m.Namespace] = &indexTypeMapping{
Namespace: m.Namespace,
Index: NormalizeIndexName(m.Index),
Type: NormalizeTypeName(m.Type),
}
} else {
panic("mappings must specify namespace, index, and type attributes")
}
}
}
}
func (configuration *configOptions) LoadScripts() {
if configuration.Script != nil {
mapEnvs = make(map[string]*executionEnv)
for _, s := range configuration.Script {
if s.Namespace != "" && s.Script != "" {
env := &executionEnv{
Vm: otto.New(),
Script: s.Script,
}
if err := env.Vm.Set("module", make(map[string]interface{})); err != nil {
panic(err)
}
if _, err := env.Vm.Run(env.Script); err != nil {
panic(err)
}
val, err := env.Vm.Run("module.exports")
if err != nil {
panic(err)
} else if !val.IsFunction() {
panic("module.exports must be a function")
}
mapEnvs[s.Namespace] = env
} else {
panic("scripts must specify namespace and script attributes")
}
}
}
}
func (configuration *configOptions) LoadConfigFile() *configOptions {
if configuration.ConfigFile != "" {
var tomlConfig configOptions = configOptions{
DroppedDatabases: true,
DroppedCollections: true,
}
if _, err := toml.DecodeFile(configuration.ConfigFile, &tomlConfig); err != nil {
panic(err)
}
if configuration.MongoUrl == "" {
configuration.MongoUrl = tomlConfig.MongoUrl
}
if configuration.MongoPemFile == "" {
configuration.MongoPemFile = tomlConfig.MongoPemFile
}
if configuration.MongoOpLogDatabaseName == "" {
configuration.MongoOpLogDatabaseName = tomlConfig.MongoOpLogDatabaseName
}
if configuration.MongoOpLogCollectionName == "" {
configuration.MongoOpLogCollectionName = tomlConfig.MongoOpLogCollectionName
}
if configuration.MongoCursorTimeout == "" {
configuration.MongoCursorTimeout = tomlConfig.MongoCursorTimeout
}
if configuration.ElasticPemFile == "" {
configuration.ElasticPemFile = tomlConfig.ElasticPemFile
}
if configuration.ElasticUrl == "" {
configuration.ElasticUrl = tomlConfig.ElasticUrl
}
if configuration.ElasticMaxConns == 0 {
configuration.ElasticMaxConns = tomlConfig.ElasticMaxConns
}
if configuration.ElasticRetrySeconds == 0 {
configuration.ElasticRetrySeconds = tomlConfig.ElasticRetrySeconds
}
if configuration.ElasticMaxDocs == 0 {
configuration.ElasticMaxDocs = tomlConfig.ElasticMaxDocs
}
if configuration.ElasticMaxBytes == 0 {
configuration.ElasticMaxBytes = tomlConfig.ElasticMaxBytes
}
if configuration.ElasticMaxSeconds == 0 {
configuration.ElasticMaxSeconds = tomlConfig.ElasticMaxSeconds
}
if configuration.ChannelSize == 0 {
configuration.ChannelSize = tomlConfig.ChannelSize
}
if configuration.MaxFileSize == 0 {
configuration.MaxFileSize = tomlConfig.MaxFileSize
}
if configuration.DroppedDatabases && !tomlConfig.DroppedDatabases {
configuration.DroppedDatabases = false
}
if configuration.DroppedCollections && !tomlConfig.DroppedCollections {
configuration.DroppedCollections = false
}
if !configuration.Gzip && tomlConfig.Gzip {
configuration.Gzip = true
}
if !configuration.Verbose && tomlConfig.Verbose {
configuration.Verbose = true
}
if !configuration.IndexFiles && tomlConfig.IndexFiles {
configuration.IndexFiles = true
}
if !configuration.FileHighlighting && tomlConfig.FileHighlighting {
configuration.FileHighlighting = true
}
if !configuration.Replay && tomlConfig.Replay {
configuration.Replay = true
}
if !configuration.Resume && tomlConfig.Resume {
configuration.Resume = true
}
if !configuration.ResumeWriteUnsafe && tomlConfig.ResumeWriteUnsafe {
configuration.ResumeWriteUnsafe = true
}
if configuration.Resume && configuration.ResumeName == "" {
configuration.ResumeName = tomlConfig.ResumeName
}
if configuration.NsRegex == "" {
configuration.NsRegex = tomlConfig.NsRegex
}
if configuration.NsExcludeRegex == "" {
configuration.NsExcludeRegex = tomlConfig.NsExcludeRegex
}
if configuration.IndexFiles {
configuration.FileNamespaces = tomlConfig.FileNamespaces
tomlConfig.LoadGridFsConfig()
}
if configuration.Worker == "" {
configuration.Worker = tomlConfig.Worker
}
configuration.Workers = tomlConfig.Workers
configuration.ElasticHosts = tomlConfig.ElasticHosts
tomlConfig.LoadScripts()
tomlConfig.LoadIndexTypes()
}
return configuration
}
func (configuration *configOptions) LoadGridFsConfig() *configOptions {
fileNamespaces = make(map[string]bool)
for _, namespace := range configuration.FileNamespaces {
fileNamespaces[namespace] = true
}
return configuration
}
func (configuration *configOptions) SetDefaults() *configOptions {
if configuration.MongoUrl == "" {
configuration.MongoUrl = mongoUrlDefault
}
if configuration.ResumeName == "" {
if configuration.Worker != "" {
configuration.ResumeName = configuration.Worker
} else {
configuration.ResumeName = resumeNameDefault
}
}
if configuration.ElasticMaxConns == 0 {
configuration.ElasticMaxConns = elasticMaxConnsDefault
}
if configuration.ChannelSize == 0 {
configuration.ChannelSize = gtmChannelSizeDefault
}
return configuration
}
func (configuration *configOptions) DialMongo() (*mgo.Session, error) {
if configuration.MongoPemFile != "" {
certs := x509.NewCertPool()
if ca, err := ioutil.ReadFile(configuration.MongoPemFile); err == nil {
certs.AppendCertsFromPEM(ca)
} else {
return nil, err
}
tlsConfig := &tls.Config{RootCAs: certs}
dialInfo, err := mgo.ParseURL(configuration.MongoUrl)
if err != nil {
return nil, err
} else {
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
return tls.Dial("tcp", addr.String(), tlsConfig)
}
return mgo.DialWithInfo(dialInfo)
}
} else {
return mgo.Dial(configuration.MongoUrl)
}
}
func (configuration *configOptions) ConfigHttpTransport() error {
if configuration.ElasticPemFile != "" {
certs := x509.NewCertPool()
if ca, err := ioutil.ReadFile(configuration.ElasticPemFile); err == nil {
certs.AppendCertsFromPEM(ca)
} else {
return err
}
tlsConfig := &tls.Config{RootCAs: certs}
http.DefaultTransport.(*http.Transport).TLSClientConfig = tlsConfig
}
return nil
}
func TraceRequest(method, url, body string) {
infoLog.Printf("%s request sent to %s", method, url)
if body != "" {
ba := []byte(body)
if len(ba) > 1 && ba[0] == 0x1f && ba[1] == 0x8b {
buff := bytes.NewBuffer(ba)
reader, err := gzip.NewReader(buff)
if err != nil {
return
}
defer reader.Close()
if unzipped, err := ioutil.ReadAll(reader); err == nil {
infoLog.Printf("request body: %s", unzipped)
} else {
log.Printf("unable to unzip response: %s", err)
}
} else {
infoLog.Printf("request body: %s", body)
}
}
}
func DoDrop(elastic *elastigo.Conn, op *gtm.Op, configuration *configOptions) (indexed bool, err error) {
if db, drop := op.IsDropDatabase(); drop {
if configuration.DroppedDatabases {
if err = DeleteIndexes(elastic, db, configuration); err == nil {
indexed = true
}
} else {
indexed = true
}
} else if col, drop := op.IsDropCollection(); drop {
if configuration.DroppedCollections {
if err = DeleteIndex(elastic, op.GetDatabase()+"."+col, configuration); err == nil {
indexed = true
}
} else {
indexed = true
}
}
return
}
func DoFileContent(mongo *mgo.Session, op *gtm.Op, configuration *configOptions) (ingestAttachment bool, err error) {
if !configuration.IndexFiles {
return
}
if fileNamespaces[op.Namespace] {
err = AddFileContent(mongo, op, configuration)
if configuration.ElasticMajorVersion >= 5 {
if op.Data["file"] != "" {
ingestAttachment = true
}
}
}
return
}
func DoResume(mongo *mgo.Session, op *gtm.Op, configuration *configOptions) (err error) {
if configuration.Resume {
err = SaveTimestamp(mongo, op, configuration.ResumeName)
}
return
}
func DoIndexing(indexer *elastigo.BulkIndexer, elastic *elastigo.Conn, op *gtm.Op, ingestAttachment bool) (indexed bool, err error) {
PrepareDataForIndexing(op.Data)
objectId, indexType := OpIdToString(op), IndexTypeMapping(op)
if ingestAttachment {
if err = IngestAttachment(elastic, indexType.Index, indexType.Type, objectId, op.Data); err == nil {
indexed = true
}
} else {
if err = indexer.Index(indexType.Index, indexType.Type, objectId, "", "", nil, op.Data); err == nil {
indexed = true
}
}
return
}
func DoIndex(indexer *elastigo.BulkIndexer, elastic *elastigo.Conn, op *gtm.Op, ingestAttachment bool) (indexed bool, err error) {
if err = MapData(op); err == nil {
if op.Data != nil {
indexed, err = DoIndexing(indexer, elastic, op, ingestAttachment)
} else if op.IsUpdate() {
objectId, indexType := OpIdToString(op), IndexTypeMapping(op)
indexer.Delete(indexType.Index, indexType.Type, objectId)
indexed = true
} else {
indexed = true
}
}
return
}
func DoDelete(indexer *elastigo.BulkIndexer, op *gtm.Op) (indexed bool) {
objectId, indexType := OpIdToString(op), IndexTypeMapping(op)
indexer.Delete(indexType.Index, indexType.Type, objectId)
indexed = true
return
}
func main() {
log.SetPrefix("ERROR ")
configuration := &configOptions{}
configuration.ParseCommandLineFlags()
if configuration.Version {
fmt.Println(Version)
os.Exit(0)
}
configuration.LoadConfigFile().SetDefaults()
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
if err := configuration.ConfigHttpTransport(); err != nil {
log.Panicf("Unable to configure HTTP transport: %s", err)
}
mongo, err := configuration.DialMongo()
if err != nil {
log.Panicf("Unable to connect to mongodb using URL %s: %s", configuration.MongoUrl, err)
}
defer mongo.Close()
mongo.SetMode(mgo.Monotonic, true)
if configuration.Resume && configuration.ResumeWriteUnsafe {
mongo.SetSafe(nil)
}
elastic := elastigo.NewConn()
if configuration.ElasticUrl != "" {
elastic.SetFromUrl(configuration.ElasticUrl)
}
if configuration.ElasticHosts != nil {
elastic.SetHosts(configuration.ElasticHosts)
}
if configuration.Verbose {
elastic.RequestTracer = TraceRequest
}
if configuration.Gzip {
elastic.Gzip = true
}
if err := TestElasticSearchConn(elastic, configuration); err != nil {
host := elastic.Domain
if len(configuration.ElasticHosts) > 0 {
host = configuration.ElasticHosts[0]
}
log.Panicf("Unable to validate connection to elasticsearch using %s://%s:%s: %s",
elastic.Protocol, host, elastic.Port, err)
}
indexer := elastic.NewBulkIndexerErrors(configuration.ElasticMaxConns, configuration.ElasticRetrySeconds)
if configuration.ElasticMaxDocs != 0 {
indexer.BulkMaxDocs = configuration.ElasticMaxDocs
}
if configuration.ElasticMaxBytes != 0 {
indexer.BulkMaxBuffer = configuration.ElasticMaxBytes
}
if configuration.ElasticMaxSeconds != 0 {
indexer.BufferDelayMax = time.Duration(configuration.ElasticMaxSeconds) * time.Second
}
indexer.Start()
defer indexer.Stop()
go func(mongo *mgo.Session, indexer *elastigo.BulkIndexer) {
<-sigs
mongo.Close()
indexer.Flush()
indexer.Stop()
done <- true
}(mongo, indexer)
var after gtm.TimestampGenerator = nil
if configuration.Resume {
after = func(session *mgo.Session, options *gtm.Options) bson.MongoTimestamp {
ts := gtm.LastOpTimestamp(session, options)
if configuration.Replay {
ts = 0
} else {
collection := session.DB("monstache").C("monstache")
doc := make(map[string]interface{})
collection.FindId(configuration.ResumeName).One(doc)
if doc["ts"] != nil {
ts = doc["ts"].(bson.MongoTimestamp)
}
}
return ts
}
} else if configuration.Replay {
after = func(session *mgo.Session, options *gtm.Options) bson.MongoTimestamp {
return 0
}
}
if configuration.IndexFiles {
if len(configuration.FileNamespaces) == 0 {
log.Fatalln("File indexing is ON but no file namespaces are configured")
}
for _, namespace := range configuration.FileNamespaces {
if err := EnsureFileMapping(elastic, namespace, configuration); err != nil {
panic(err)
}
if configuration.ElasticMajorVersion >= 5 {
break
}
}
}
var filter gtm.OpFilter = nil
filterChain := []gtm.OpFilter{NotMonstache, NotSystem, NotChunks}
if configuration.NsRegex != "" {
filterChain = append(filterChain, FilterWithRegex(configuration.NsRegex))
}
if configuration.NsExcludeRegex != "" {
filterChain = append(filterChain, FilterInverseWithRegex(configuration.NsExcludeRegex))
}
if configuration.Worker != "" {
workerFilter, err := consistent.ConsistentHashFilter(configuration.Worker, configuration.Workers)
if err != nil {
panic(err)
}
filterChain = append(filterChain, workerFilter)
} else if configuration.Workers != nil {
panic("workers configured but this worker is undefined. worker must be set to one of the workers.")
}
filter = gtm.ChainOpFilters(filterChain...)
var oplogDatabaseName, oplogCollectionName, cursorTimeout *string
if configuration.MongoOpLogDatabaseName != "" {
oplogDatabaseName = &configuration.MongoOpLogDatabaseName
}
if configuration.MongoOpLogCollectionName != "" {
oplogCollectionName = &configuration.MongoOpLogCollectionName
}
if configuration.MongoCursorTimeout != "" {
cursorTimeout = &configuration.MongoCursorTimeout
}
ops, errs := gtm.Tail(mongo, >m.Options{
After: after,
Filter: filter,
OpLogDatabaseName: oplogDatabaseName,
OpLogCollectionName: oplogCollectionName,
CursorTimeout: cursorTimeout,
ChannelSize: configuration.ChannelSize,
})
exitStatus := 0
for {
select {
case <-done:
os.Exit(exitStatus)
case err = <-errs:
exitStatus = 1
log.Println(err)
case indexErr := <-indexer.ErrorChannel:
if indexErr.Buf != nil {
errs <- fmt.Errorf("%s. Failed Request Body : %s", indexErr.Err, indexErr.Buf)
} else {
errs <- indexErr.Err
}
case op := <-ops:
ingestAttachment, indexed := false, false
if op.IsDrop() {
if indexed, err = DoDrop(elastic, op, configuration); err != nil {
errs <- err
}
} else if op.IsDelete() {
indexed = DoDelete(indexer, op)
} else if op.Data != nil {
if ingestAttachment, err = DoFileContent(mongo, op, configuration); err != nil {
errs <- err
}
if indexed, err = DoIndex(indexer, elastic, op, ingestAttachment); err != nil {
errs <- err
}
}
if indexed {
if err = DoResume(mongo, op, configuration); err != nil {
errs <- err
}
}
}
}
}