1
|
|
|
package main |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"flag" |
5
|
|
|
cache "github.com/arazmj/gerdu/cache" |
6
|
|
|
"github.com/arazmj/gerdu/grpcserver" |
7
|
|
|
"github.com/arazmj/gerdu/httpserver" |
8
|
|
|
"github.com/arazmj/gerdu/lfucache" |
9
|
|
|
"github.com/arazmj/gerdu/lrucache" |
10
|
|
|
"github.com/arazmj/gerdu/memcached" |
11
|
|
|
"github.com/arazmj/gerdu/raftproxy" |
12
|
|
|
"github.com/arazmj/gerdu/redis" |
13
|
|
|
"github.com/arazmj/gerdu/weakcache" |
14
|
|
|
"github.com/inhies/go-bytesize" |
15
|
|
|
log "github.com/sirupsen/logrus" |
16
|
|
|
"os" |
17
|
|
|
"os/signal" |
18
|
|
|
"strconv" |
19
|
|
|
"strings" |
20
|
|
|
) |
21
|
|
|
|
22
|
|
|
var gerdu raftproxy.RaftCache |
23
|
|
|
|
24
|
|
|
var ( |
25
|
|
|
loglevel = flag.String("log", "info", |
26
|
|
|
"log level can be any of values of 'panic', 'fatal', 'error', 'warn', 'info', 'debug', 'trace'") |
27
|
|
|
capacityStr = flag.String("capacity", "64MB", |
28
|
|
|
"The size of cache, once cache reached this capacity old values will evicted.\n"+ |
29
|
|
|
"Specify a numerical value followed by one of the following units (not case sensitive)"+ |
30
|
|
|
"\nK or KB: Kilobytes"+ |
31
|
|
|
"\nM or MB: Megabytes"+ |
32
|
|
|
"\nG or GB: Gigabytes"+ |
33
|
|
|
"\nT or TB: Terabytes") |
34
|
|
|
httpPort = flag.Int("httpport", 8080, "http server port number") |
35
|
|
|
grpcPort = flag.Int("grpcport", 8081, "grpc server port number") |
36
|
|
|
mcdPort = flag.Int("mcdport", 11211, "memcached server port number") |
37
|
|
|
redisPort = flag.Int("redisport", 6379, "redis server port number") |
38
|
|
|
kind = flag.String("type", "lru", "type of cache, lru or lfu, weak") |
39
|
|
|
protocols = flag.String("protocols", "", |
40
|
|
|
"protocol 'grpc', 'redis' or 'mcd' (memcached), multiple comma-separated values, http is not optional") |
41
|
|
|
tlsKey = flag.String("key", "", "SSL certificate private key") |
42
|
|
|
tlsCert = flag.String("cert", "", "SSL certificate public key") |
43
|
|
|
host = flag.String("host", "127.0.0.1", "The host that server listens") |
44
|
|
|
raftAddr = flag.String("raft", "127.0.0.1:12000", "Set raft bind address") |
45
|
|
|
joinAddr = flag.String("join", "", "Set join address, if any") |
46
|
|
|
nodeID = flag.String("id", "master", "Node ID") |
47
|
|
|
storage = flag.String("storage", "", "Path to store log files and snapshot, will store in memory if not set") |
48
|
|
|
|
49
|
|
|
secure = len(*tlsCert) > 0 && len(*tlsKey) > 0 |
50
|
|
|
) |
51
|
|
|
|
52
|
|
|
func main() { |
53
|
|
|
flag.Parse() |
54
|
|
|
setLogLevel() |
55
|
|
|
setCache() |
56
|
|
|
serve() |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
func serve() { |
60
|
|
|
*protocols = strings.ToLower(*protocols) |
61
|
|
|
|
62
|
|
|
go func() { |
63
|
|
|
httpHost := *host + ":" + strconv.Itoa(*httpPort) |
64
|
|
|
if secure { |
65
|
|
|
httpserver.HTTPServeTLS(httpHost, *tlsCert, *tlsKey, gerdu) |
66
|
|
|
} else { |
67
|
|
|
httpserver.HTTPServe(httpHost, gerdu) |
68
|
|
|
} |
69
|
|
|
}() |
70
|
|
|
|
71
|
|
|
if strings.Contains(*protocols, "grpc") { |
72
|
|
|
go func() { |
73
|
|
|
grpcHost := *host + ":" + strconv.Itoa(*grpcPort) |
74
|
|
|
if secure { |
75
|
|
|
grpcserver.GrpcServeTLS(grpcHost, *tlsCert, *tlsKey, gerdu) |
76
|
|
|
} else { |
77
|
|
|
grpcserver.GrpcServe(grpcHost, gerdu) |
78
|
|
|
} |
79
|
|
|
}() |
80
|
|
|
} |
81
|
|
|
if strings.Contains(*protocols, "mcd") { |
82
|
|
|
go func() { |
83
|
|
|
mcdHost := *host + ":" + strconv.Itoa(*mcdPort) |
84
|
|
|
if secure { |
85
|
|
|
log.Fatalln("Memcached protocol does not support TLS") |
86
|
|
|
os.Exit(1) |
87
|
|
|
} |
88
|
|
|
memcached.Serve(mcdHost, gerdu) |
89
|
|
|
}() |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if strings.Contains(*protocols, "redis") { |
93
|
|
|
go func() { |
94
|
|
|
redisHost := *host + ":" + strconv.Itoa(*redisPort) |
95
|
|
|
if secure { |
96
|
|
|
redis.ServeTLS(redisHost, *tlsCert, *tlsKey, gerdu) |
97
|
|
|
} else { |
98
|
|
|
redis.Serve(redisHost, gerdu) |
99
|
|
|
} |
100
|
|
|
}() |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
terminate := make(chan os.Signal, 1) |
104
|
|
|
signal.Notify(terminate, os.Interrupt) |
105
|
|
|
<-terminate |
106
|
|
|
err := gerdu.(*raftproxy.RaftProxy).Leave(*nodeID) |
107
|
|
|
if err != nil { |
108
|
|
|
log.Errorf("Cannot leave the cluster gracefully %v", err) |
109
|
|
|
} else { |
110
|
|
|
log.Println("Gerdu exiting") |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
func setCache() { |
115
|
|
|
capacity, err := bytesize.Parse(*capacityStr) |
116
|
|
|
if err != nil { |
117
|
|
|
log.Fatal("Invalid value for capacity", err.Error()) |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
var c cache.UnImplementedCache |
121
|
|
|
if strings.ToLower(*kind) == "lru" { |
122
|
|
|
c = lrucache.NewCache(capacity) |
123
|
|
|
} else if strings.ToLower(*kind) == "lfu" { |
124
|
|
|
c = lfucache.NewCache(capacity) |
125
|
|
|
} else if strings.ToLower(*kind) == "weak" { |
126
|
|
|
c = weakcache.NewWeakCache() |
127
|
|
|
} else { |
128
|
|
|
log.Fatalf("Invalid value for type") |
129
|
|
|
os.Exit(1) |
130
|
|
|
} |
131
|
|
|
gerdu = raftproxy.NewRaftProxy(c, *raftAddr, *joinAddr, *nodeID) |
132
|
|
|
err = gerdu.OpenRaft(*storage) |
133
|
|
|
if err != nil { |
134
|
|
|
log.Fatalf("Cannot open raft peer connection: %s", err) |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
func setLogLevel() { |
140
|
|
|
switch *loglevel { |
141
|
|
|
case "panic": |
142
|
|
|
log.SetLevel(log.PanicLevel) |
143
|
|
|
case "fatal": |
144
|
|
|
log.SetLevel(log.FatalLevel) |
145
|
|
|
case "error": |
146
|
|
|
log.SetLevel(log.ErrorLevel) |
147
|
|
|
case "warn": |
148
|
|
|
log.SetLevel(log.WarnLevel) |
149
|
|
|
case "info": |
150
|
|
|
log.SetLevel(log.InfoLevel) |
151
|
|
|
case "debug": |
152
|
|
|
log.SetLevel(log.DebugLevel) |
153
|
|
|
case "trace": |
154
|
|
|
log.SetReportCaller(true) |
155
|
|
|
log.SetLevel(log.DebugLevel) |
156
|
|
|
default: |
157
|
|
|
log.Fatalf("Invalid log level value %s\n", *loglevel) |
158
|
|
|
os.Exit(1) |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|