forked from BTrDB/btrdb-server
/
main.go
196 lines (180 loc) · 5.16 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
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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"runtime"
"time"
"github.com/SoftwareDefinedBuildings/btrdb"
"github.com/SoftwareDefinedBuildings/btrdb/grpcinterface"
"github.com/SoftwareDefinedBuildings/btrdb/internal/bstore"
"github.com/SoftwareDefinedBuildings/btrdb/internal/configprovider"
"github.com/SoftwareDefinedBuildings/btrdb/version"
"github.com/immesys/sysdigtracer"
"github.com/op/go-logging"
opentracing "github.com/opentracing/opentracing-go"
)
var log *logging.Logger
func init() {
logging.SetBackend(logging.NewLogBackend(os.Stderr, "", 0))
logging.SetFormatter(logging.MustStringFormatter("[%{level}]%{shortfile} > %{message}"))
log = logging.MustGetLogger("log")
}
var createDB = flag.Bool("makedb", false, "create a new database")
var printVersion = flag.Bool("version", false, "print version and exit")
func main() {
flag.Parse()
if *printVersion {
fmt.Println(version.VersionString)
os.Exit(0)
}
log.Infof("Starting BTrDB version %s %s", version.VersionString, version.BuildDate)
dotracer := os.Getenv("BTRDB_ENABLE_OVERWATCH")
if dotracer != "" {
// // create collector.
// collector, err := zipkin.NewHTTPCollector("http://zipkin:9411/api/v1/spans")
// if err != nil {
// fmt.Printf("unable to create Zipkin HTTP collector: %+v", err)
// os.Exit(-1)
// }
//
// // create recorder.
// recorder := zipkin.NewRecorder(collector, false, "0.0.0.0:4410", "btrdbd")
//
// // create tracer.
// tracer, err := zipkin.NewTracer(
// recorder,
// zipkin.ClientServerSameSpan(true),
// zipkin.TraceID128Bit(true),
// )
// if err != nil {
// fmt.Printf("unable to create Zipkin tracer: %+v", err)
// os.Exit(-1)
// }
tracer := sysdigtracer.New()
//Cheers love! The cavalry's here!
opentracing.SetGlobalTracer(tracer)
fmt.Printf("TRACING ENABLED\n")
} else {
fmt.Printf("TRACING IS _NOT_ ENABLED\n")
}
cfg, err1 := configprovider.LoadFileConfig("./btrdb.conf")
if cfg == nil {
var err2 error
cfg, err2 = configprovider.LoadFileConfig("/etc/btrdb/btrdb.conf")
if cfg == nil {
fmt.Println("Could not locate configuration")
fmt.Printf("Tried ./btrdb.conf : %s\n", err1)
fmt.Printf("Tried /etc/btrdb/btrdb.conf : %s\n", err2)
fmt.Printf("Unashamedly giving up\n")
os.Exit(1)
}
}
if cfg.ClusterEnabled() {
hostname, err := os.Hostname()
if err != nil {
fmt.Println("Could not obtain hostname")
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
cfg, err = configprovider.LoadEtcdConfig(cfg, hostname)
if err != nil {
fmt.Println("Could not load cluster configuration")
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
}
fmt.Println("CONFIG OKAY!")
if *createDB {
fmt.Printf("Creating a new database\n")
bstore.CreateDatabase(cfg)
fmt.Printf("Done\n")
os.Exit(0)
}
q, err := btrdb.NewQuasar(cfg)
if err != nil {
log.Panicf("error: %v", err)
}
fmt.Println("QUASAR OKAY!")
go func() {
for {
time.Sleep(10 * time.Second)
log.Infof("Num goroutines: %d", runtime.NumGoroutine())
}
}()
//if cfg.HttpEnabled() {
// go httpinterface.QuasarServeHTTP(q, cfg.HttpAddress()+":"+strconv.FormatInt(int64(cfg.HttpPort()), 10))
//}
// if cfg.CapnpEnabled() {
// go cpinterface.ServeCPNP(q, "tcp", cfg.CapnpAddress()+":"+strconv.FormatInt(int64(cfg.CapnpPort()), 10))
// }
grpcHandle := grpcinterface.ServeGRPC(q, "0.0.0.0:4410")
//go httpinterface.Run()
// if Configuration.Debug.Heapprofile {
// go func() {
// idx := 0
// for {
// f, err := os.Create(fmt.Sprintf("profile.heap.%05d", idx))
// if err != nil {
// log.Panicf("Could not create memory profile %v", err)
// }
// idx = idx + 1
// pprof.WriteHeapProfile(f)
// f.Close()
// time.Sleep(30 * time.Second)
// }
// }()
// }
//So the correct shutdown procedure is:
// - out your node in the cluster
// - all write requests must finish (grpc must watch out notify too)
// - all caches must flush
// - wait graceful shutdown of grpc (for read)
// - exit
sigchan := make(chan os.Signal, 3)
signal.Notify(sigchan, os.Interrupt)
for {
select {
case _ = <-sigchan:
log.Warning("Received SIGINT, removing node from cluster")
log.Warning("send SIGINT again to quit immediately")
grpc := grpcHandle.InitiateShutdown()
select {
case _ = <-grpc:
log.Warning("GRPC shutdown complete")
case _ = <-sigchan:
log.Warning("SIGINT RECEIVED, SKIPPING SAFE SHUTDOWN")
return
}
/*http := httpinterface.InitiateShutdown()
select {
case _ = <-http:
log.Warning("HTTP shutdown complete")
case _ = <-sigchan:
log.Warning("SIGINT RECEIVED, SKIPPING SAFE SHUTDOWN")
return
}*/
qdone := q.InitiateShutdown()
select {
case _ = <-qdone:
log.Warning("Core shutdown complete")
case _ = <-sigchan:
log.Warning("SIGINT RECEIVED, SKIPPING SAFE SHUTDOWN")
return
}
log.Warning("Safe shutdown complete")
// if Configuration.Debug.Heapprofile {
// log.Warning("writing heap profile")
// f, err := os.Create("profile.heap.FIN")
// if err != nil {
// log.Panicf("Could not create memory profile %v", err)
// }
// pprof.WriteHeapProfile(f)
// f.Close()
//
// }
return //end the program
}
}
}