/
main.go
137 lines (118 loc) · 2.51 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
package main
/*
cd /home/go/src/github.com/tumashov/goda
export PATH=$PATH:/usr/local/go/bin
export GOPATH=/home/go
go build
go build && ./goda
go get github.com/BurntSushi/toml
go get github.com/RoaringBitmap/roaring
go get github.com/valyala/fasthttp
go get github.com/kjk/smaz
*/
import (
"github.com/BurntSushi/toml"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
"os"
"runtime"
"runtime/debug"
"time"
)
var Config struct {
Storage struct {
Directory string
Compress bool
SegmentSize int
EffectiveMemo bool
Listen string
}
Replication struct {
Master bool
Nodes []string
Slave bool
AllowIP []string
}
Durability struct {
WAL bool
Directory string
}
Performance struct {
AppendWriterPeriod int
TruncateWriterPeriod int
StreamerWriterPeriod int
FreeMemoryOnLoading bool
}
Debug struct {
Log bool
}
}
func Goda() int {
configFile := "config"
if len(os.Args) == 2 {
configFile = os.Args[1]
}
b, err := ioutil.ReadFile(configFile + ".toml")
if err != nil {
log.Fatal(err)
}
if _, err := toml.Decode(string(b), &Config); err != nil {
log.Panicln("Config error: ", err)
}
go Server()
var stats runtime.MemStats
runtime.ReadMemStats(&stats)
start := time.Now()
alloc := stats
log.Println("\n")
go http.ListenAndServe(":6060", http.DefaultServeMux)
if Config.Performance.FreeMemoryOnLoading {
// GC DISABLE
debug.SetGCPercent(-1)
}
//os.RemoveAll(Config.Storage.Directory + "index")
CreateIndex("index", Config.Storage.SegmentSize)
index := SelectIndex("index")
if Config.Performance.FreeMemoryOnLoading {
//FreeMemory()
// GC ENABLE
debug.SetGCPercent(100)
}
go func() {
WriteBufferWaiting := time.Duration(Config.Performance.AppendWriterPeriod)
t := time.NewTicker(WriteBufferWaiting * time.Millisecond)
for range t.C {
Writer()
}
}()
go func() {
WriteBufferWaiting := time.Duration(Config.Performance.TruncateWriterPeriod)
t := time.NewTicker(WriteBufferWaiting * time.Millisecond)
for range t.C {
WriterTruncate()
}
}()
go func() {
WriteBufferWaiting := time.Duration(Config.Performance.StreamerWriterPeriod)
t := time.NewTicker(WriteBufferWaiting * time.Millisecond)
for range t.C {
StreamerWriter()
}
}()
runtime.ReadMemStats(&stats)
logStat("Loaded", int(index.IndexLastID), int(index.IndexLastID), start, 1, alloc.Alloc, stats.Alloc)
return 1
}
func main() {
Goda()
t := time.NewTicker(30 * time.Second)
for range t.C {
}
}
func Stop() {
Writer()
WriterTruncate()
os.Exit(1)
}