-
Notifications
You must be signed in to change notification settings - Fork 0
/
nexus.go
277 lines (248 loc) · 7.39 KB
/
nexus.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package nexus
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/samuelduann/goshine"
"github.com/samuelduann/log4g"
"io"
"net/http"
"runtime/debug"
"sync"
"time"
)
type GoshineAgent struct {
cluster string
id int
gs *goshine.Goshine
lock sync.Mutex
}
type Nexus struct {
clusters map[string][]*GoshineAgent
l1cache *L1Cache
config *Config
}
type NxRequest struct {
Hql string
ClusterName string
}
type NxResponse struct {
Code int
Message string
Body interface{}
}
var (
Response_503 = NxResponse{Code: 503, Message: "System Is Down", Body: ""}
Response_500 = NxResponse{Code: 500, Message: "Internal Server Error", Body: ""}
Response_400 = NxResponse{Code: 400, Message: "Bad Request", Body: ""}
Response_200 = NxResponse{Code: 200, Message: "Ok", Body: ""}
Response_123 = NxResponse{Code: 123, Message: "It doesn't works!", Body: ""}
)
var logger *log4g.Logger
func NewNexus(confFilename string) *Nexus {
n := &Nexus{}
n.config = GetConfig()
n.config.Load(confFilename)
n.l1cache = NewL1Cache()
logger = log4g.NewLogger(n.config.LogPrefix, log4g.FilenameSuffixInHour)
return n
}
func (nx *Nexus) getCacheKey(req *NxRequest) string {
m := md5.New()
io.WriteString(m, req.Hql)
return hex.EncodeToString(m.Sum(nil))
}
func (nx *Nexus) updateCache(req *NxRequest, res *goshine.GsResultSet) {
nx.l1cache.Set(nx.getCacheKey(req), res, nx.config.CacheTimeout)
}
func (nx *Nexus) queryCache(req *NxRequest) *goshine.GsResultSet {
ret, _ := nx.l1cache.Get(nx.getCacheKey(req))
if ret != nil {
return ret.(*goshine.GsResultSet)
}
return nil
}
func (nx *Nexus) makeResponse(w http.ResponseWriter, r *http.Request, response *NxResponse, timing *Timing) {
data, _ := json.Marshal(response)
w.Write(data)
if response.Code == 200 {
logger.Infof("%s %s %s %d %s [%s]", r.RemoteAddr, r.Method, r.URL, response.Code, response.Message, timing)
} else {
logger.Warnf("%s %s %s %d %s [%s]", r.RemoteAddr, r.Method, r.URL, response.Code, response.Message, timing)
}
}
func (nx *Nexus) parseRequest(r *http.Request) *NxRequest {
r.ParseForm()
hql, ok := r.Form["hql"]
if !ok || len(hql[0]) == 0 {
return nil
}
cluster, ok := r.Form["cluster"]
if !ok || len(cluster[0]) == 0 {
return nil
}
return &NxRequest{Hql: hql[0], ClusterName: cluster[0]}
}
func (nx *Nexus) QueryHandler(w http.ResponseWriter, r *http.Request) {
timing := NewTiming()
request := nx.parseRequest(r)
if request == nil {
nx.makeResponse(w, r, &Response_400, timing)
return
}
// try fetch from cache
timing.TickStart("ReadCache1")
if result := nx.queryCache(request); result != nil {
nx.makeResponse(w, r, &NxResponse{Code: 200, Message: "Ok", Body: result}, timing)
return
}
timing.TickStop("ReadCache1")
// get an agent
timing.TickStart("AssignAgent")
g, err := nx.assignAgent(request)
if err != nil {
logger.Infof("assign agent failed")
nx.makeResponse(w, r, &NxResponse{Code: 504, Message: fmt.Sprintf("%s", err)}, timing)
return
}
defer func() {
if err := recover(); err != nil {
nx.freeAgent(g)
nx.makeResponse(w, r, &Response_500, timing)
if nx.config.DebugMode == true {
debug.PrintStack()
}
}
}()
timing.TickStop("AssignAgent")
timing.TickStart("ReadCache2")
// try fetch from cache again, in a very special case
if result := nx.queryCache(request); result != nil {
nx.freeAgent(g)
nx.makeResponse(w, r, &NxResponse{Code: 200, Message: "Ok", Body: result}, timing)
return
}
timing.TickStop("ReadCache2")
timing.TickStart("Spark")
result, err := g.gs.FetchAll(request.Hql)
timing.TickStop("Spark")
// put agent back
nx.freeAgent(g)
if err != nil {
nx.makeResponse(w, r, &NxResponse{Code: 500, Message: fmt.Sprintf("%s", err)}, timing)
return
}
// put cache
timing.TickStart("UpdateCache")
nx.updateCache(request, result)
timing.TickStop("UpdateCache")
// return result
nx.makeResponse(w, r, &NxResponse{Code: 200, Message: "Ok", Body: result}, timing)
}
func (nx *Nexus) ExecuteHandler(w http.ResponseWriter, r *http.Request) {
timing := NewTiming()
request := nx.parseRequest(r)
if request == nil {
nx.makeResponse(w, r, &Response_400, timing)
return
}
// get an agent and execute
timing.TickStart("AssignAgent")
g, err := nx.assignAgent(request)
if err != nil {
nx.makeResponse(w, r, &NxResponse{Code: 504, Message: fmt.Sprintf("%s", err)}, timing)
return
}
timing.TickStop("AssignAgent")
timing.TickStart("Spark")
if err := g.gs.Execute(request.Hql); err != nil {
nx.makeResponse(w, r, &NxResponse{Code: 500, Message: fmt.Sprintf("%s", err)}, timing)
} else {
nx.makeResponse(w, r, &Response_200, timing)
}
nx.freeAgent(g)
}
func (nx *Nexus) initClusters() error {
nx.clusters = make(map[string][]*GoshineAgent)
for cluster_name, cluster_config := range nx.config.GoshineClusters {
nx.clusters[cluster_name] = make([]*GoshineAgent, len(cluster_config), len(cluster_config))
for i, gconf := range cluster_config {
g := goshine.NewGoshine(gconf.Host, gconf.Port, "user0", "passwd0", nx.config.Database, nx.config.UDFLoaders)
if err := g.Connect(); err != nil {
logger.Warnf("init goshine agent %s:%d %s:%d failed, err: %s", cluster_name, i, gconf.Host, gconf.Port, err)
return errors.New("Init Goshine Failed")
}
logger.Infof("goshine agent %s:%d %s:%d activated", cluster_name, i, gconf.Host, gconf.Port)
nx.clusters[cluster_name][i] = &GoshineAgent{cluster: cluster_name, id: i, gs: g}
}
}
go nx.agentMaintenance()
return nil
}
func (nx *Nexus) getAgentId(req *NxRequest, cluster []*GoshineAgent) int {
checksum := 0
for _, chr := range req.Hql {
checksum = (checksum + int(chr)) % len(cluster)
}
return checksum
}
func (nx *Nexus) assignAgent(request *NxRequest) (*GoshineAgent, error) {
// check if the cluster exists
cluster, ok := nx.clusters[request.ClusterName]
if !ok {
logger.Warnf("cluster %s does not exists", request.ClusterName)
return nil, errors.New("No Such Cluster")
}
//map request to an available agent id
i := nx.getAgentId(request, cluster)
id := -1
for skip := 0; skip < len(cluster); skip++ {
id = (i + skip) % len(cluster)
if cluster[id].gs.GetStatus() == goshine.GS_STATUS_CONNECTED {
break
}
logger.Warnf("agent %s:%d is down, trying next..", request.ClusterName, i)
}
if id == -1 {
return nil, errors.New("System Is Down")
}
// try get control of that agent
cluster[id].lock.Lock()
return cluster[id], nil
}
func (nx *Nexus) freeAgent(ga *GoshineAgent) {
ga.lock.Unlock()
}
func (nx *Nexus) agentMaintenance() {
// check out agents which is at error state, and try reconnect
ticker := time.Tick(10 * time.Second)
for {
<-ticker
for cluster_name, gas := range nx.clusters {
for i, ga := range gas {
if ga.gs.GetStatus() == goshine.GS_STATUS_ERROR {
logger.Warnf("agent %s:%d is in error status, trying to recover...", cluster_name, i)
if err := ga.gs.Connect(); err != nil {
logger.Warnf("agent %s:%d recovering failed err: %s", cluster_name, i, err)
} else {
logger.Warnf("agent %s:%d recovered", cluster_name, i)
}
}
}
}
}
}
func (nx *Nexus) Start() bool {
if nx.initClusters() != nil {
logger.Warnf("initClusters failed, abort")
return false
}
http.HandleFunc("/query", nx.QueryHandler)
http.HandleFunc("/execute", nx.ExecuteHandler)
addr := fmt.Sprintf("%s:%d", nx.config.Host, nx.config.Port)
logger.Infof("staring nexus at %s", addr)
http.ListenAndServe(addr, nil)
return true
}