-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.go
102 lines (86 loc) · 2.55 KB
/
main.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
// Copyright 2017 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net"
"os"
"path/filepath"
"time"
"github.com/juju/mgo/v2"
"github.com/juju/txn/v2"
)
var dbName = flag.String("db", "", "mongo database name (required)")
var txnsName = flag.String("txns", "txns", "mgo txns collection name")
var url = flag.String("url", "localhost:27017", "mongo URL")
var insecureTLS = flag.Bool("insecuretls", false, "connect with insecure TLS handshake")
var dialTimeout = flag.Int("dialtimeout", 10, "dial timeout")
var syncTimeout = flag.Int("synctimeout", 7, "session sync timeout")
var socketTimeout = flag.Int("sockettimeout", 60, "session socket timeout")
func main() {
flag.Usage = wrapUsage(flag.Usage)
flag.Parse()
if *dbName == "" {
flag.PrintDefaults()
os.Exit(1)
}
var (
session *mgo.Session
err error
)
if *insecureTLS {
info, err := mgo.ParseURL(*url)
if err != nil {
log.Fatalf("failed to parse url: %v", err)
}
info.DialServer = dialInsecureTLS
session, err = mgo.DialWithInfo(info)
if err != nil {
log.Fatalf("failed to connect to mongo: %v", err)
}
} else {
session, err = mgo.DialWithTimeout(*url, time.Second*time.Duration(*dialTimeout))
if err != nil {
log.Fatalf("failed to connect to mongo: %v", err)
}
}
session.SetSyncTimeout(time.Second * time.Duration(*syncTimeout))
session.SetSocketTimeout(time.Second * time.Duration(*socketTimeout))
db := session.DB(*dbName)
txnsC := db.C(*txnsName)
startTime := time.Now()
stats, err := txn.CleanAndPrune(txn.CleanAndPruneArgs{Txns: txnsC})
if err != nil {
log.Fatalf("failed to clean and prune txns: %v", err)
}
log.Println("clean and prune complete after", time.Since(startTime))
log.Println(stats.DocsCleaned, "docs cleaned,", stats.TransactionsRemoved, "txns removed,",
stats.StashDocumentsRemoved, "txns.stash docs removed")
}
func dialInsecureTLS(addr *mgo.ServerAddr) (net.Conn, error) {
c, err := net.Dial("tcp", addr.String())
if err != nil {
return nil, err
}
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
cc := tls.Client(c, tlsConfig)
if err := cc.Handshake(); err != nil {
return nil, err
}
return cc, nil
}
func wrapUsage(f func()) func() {
return func() {
fmt.Fprintf(os.Stderr, `%s - clean and purge mgo/txn collections and affected documents
Do not use this utility unless you understand mgo/txn internals and
know what you are doing. Data loss may result from inappropriate or
incorrect usage. Good luck!
`, filepath.Base(os.Args[0]))
f()
}
}