-
Notifications
You must be signed in to change notification settings - Fork 39
/
uq.go
218 lines (199 loc) · 4.98 KB
/
uq.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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"path"
"runtime"
"strings"
"sync"
"syscall"
"github.com/buaazp/uq/admin"
"github.com/buaazp/uq/entry"
"github.com/buaazp/uq/queue"
"github.com/buaazp/uq/store"
)
var (
ip string
host string
port int
adminPort int
protocol string
db string
dir string
logFile string
etcd string
cluster string
)
func init() {
flag.StringVar(&ip, "ip", "127.0.0.1", "self ip/host address")
flag.StringVar(&host, "host", "0.0.0.0", "listen ip")
flag.IntVar(&port, "port", 8808, "listen port")
flag.IntVar(&adminPort, "admin-port", 8809, "admin listen port")
flag.StringVar(&protocol, "protocol", "redis", "frontend interface type [redis/mc/http]")
flag.StringVar(&db, "db", "goleveldb", "backend storage type [goleveldb/memdb]")
flag.StringVar(&dir, "dir", "./data", "backend storage path")
flag.StringVar(&logFile, "log", "", "uq log path")
flag.StringVar(&etcd, "etcd", "", "etcd service location")
flag.StringVar(&cluster, "cluster", "uq", "cluster name in etcd")
}
func belong(single string, team []string) bool {
for _, one := range team {
if single == one {
return true
}
}
return false
}
func checkArgs() bool {
if !belong(db, []string{"goleveldb", "memdb"}) {
fmt.Printf("db mode %s is not supported!\n", db)
return false
}
if !belong(protocol, []string{"redis", "mc", "http"}) {
fmt.Printf("protocol %s is not supported!\n", protocol)
return false
}
return true
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
defer func() {
fmt.Printf("byebye! uq see u later! 😄\n")
}()
flag.Parse()
if !checkArgs() {
return
}
err := os.MkdirAll(dir, 0755)
if err != nil {
fmt.Printf("mkdir %s error: %s\n", dir, err)
return
}
if logFile != "" {
logFile = path.Join(dir, "uq.log")
logf, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
fmt.Printf("log open error: %s\n", err)
return
}
defer logf.Close()
log.SetOutput(logf)
}
log.SetFlags(log.Lshortfile | log.LstdFlags | log.Lmicroseconds)
log.SetPrefix("[uq] ")
fmt.Printf("uq started! 😄\n")
var storage store.Storage
// if db == "rocksdb" {
// dbpath := path.Clean(path.Join(dir, "uq.db"))
// log.Printf("dbpath: %s", dbpath)
// storage, err = store.NewRockStore(dbpath)
// } else if db == "goleveldb" {
if db == "goleveldb" {
dbpath := path.Clean(path.Join(dir, "uq.db"))
log.Printf("dbpath: %s", dbpath)
storage, err = store.NewLevelStore(dbpath)
} else if db == "memdb" {
storage, err = store.NewMemStore()
} else {
fmt.Printf("store %s is not supported!\n", db)
return
}
if err != nil {
fmt.Printf("store init error: %s\n", err)
return
}
var etcdServers []string
if etcd != "" {
etcdServers = strings.Split(etcd, ",")
for i, etcdServer := range etcdServers {
if !strings.HasPrefix(etcdServer, "http://") {
etcdServers[i] = "http://" + etcdServer
}
}
}
var messageQueue queue.MessageQueue
// messageQueue, err = queue.NewFakeQueue(storage, ip, port, etcdServers, cluster)
// if err != nil {
// fmt.Printf("queue init error: %s\n", err)
// storage.Close()
// return
// }
messageQueue, err = queue.NewUnitedQueue(storage, ip, port, etcdServers, cluster)
if err != nil {
fmt.Printf("queue init error: %s\n", err)
storage.Close()
return
}
var entrance entry.Entrance
if protocol == "http" {
entrance, err = entry.NewHTTPEntry(host, port, messageQueue)
} else if protocol == "mc" {
entrance, err = entry.NewMcEntry(host, port, messageQueue)
} else if protocol == "redis" {
entrance, err = entry.NewRedisEntry(host, port, messageQueue)
} else {
fmt.Printf("protocol %s is not supported!\n", protocol)
return
}
if err != nil {
fmt.Printf("entry init error: %s\n", err)
messageQueue.Close()
return
}
stop := make(chan os.Signal)
entryFailed := make(chan bool)
adminFailed := make(chan bool)
signal.Notify(stop,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
var wg sync.WaitGroup
// start entrance server
go func(c chan bool) {
wg.Add(1)
defer wg.Done()
err := entrance.ListenAndServe()
if err != nil {
if !strings.Contains(err.Error(), "stopped") {
fmt.Printf("entry listen error: %s\n", err)
}
close(c)
}
}(entryFailed)
var adminServer admin.Administrator
adminServer, err = admin.NewUnitedAdmin(host, adminPort, messageQueue)
if err != nil {
fmt.Printf("admin init error: %s\n", err)
entrance.Stop()
return
}
// start admin server
go func(c chan bool) {
wg.Add(1)
defer wg.Done()
err := adminServer.ListenAndServe()
if err != nil {
if !strings.Contains(err.Error(), "stopped") {
fmt.Printf("entry listen error: %s\n", err)
}
close(c)
}
}(adminFailed)
select {
case <-stop:
// log.Printf("got signal: %v", signal)
adminServer.Stop()
log.Printf("admin server stoped.")
entrance.Stop()
log.Printf("entrance stoped.")
case <-entryFailed:
messageQueue.Close()
case <-adminFailed:
entrance.Stop()
}
wg.Wait()
}