-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathserver.go
212 lines (176 loc) · 4.86 KB
/
server.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
package proxy
import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
u "github.com/araddon/gou"
"github.com/dataux/dataux/models"
"github.com/dataux/dataux/planner"
)
var asciiIntro = `
_ _
| | | |
__| | __ _| |_ __ _ _ ___ __
/ _* |/ _* | __/ _* | | | \ \/ /
| (_| | (_| | || (_| | |_| |> <
\__,_|\__,_|\__\__,_|\__,_/_/\_\
`
// Global Access to Config
var Conf *models.Config
// LoadConfig from @configFile (read from disk?)
// also available is a
func LoadConfig(configFile string) (*models.Config, error) {
// get config from file and exit if error
var err error
Conf, err = models.LoadConfigFromFile(configFile)
if err != nil {
return nil, err
}
return Conf, nil
}
// LoadConfigString from @config data confl format
func LoadConfigString(config string) (*models.Config, error) {
// get config from file and exit if error
var err error
Conf, err = models.LoadConfig(config)
if err != nil {
u.Errorf("Could not load config from config string: %v", err)
return nil, err
}
return Conf, nil
}
func banner() string {
return strings.Replace(asciiIntro, "*", "`", -1)
}
// Start a DataUX Service, can be either worker, listener, both
//
// @listener true/false do we run the listeneners (mysql)?
// if not then this is a worker only node
// @workerct over-ride # of workers
func RunDaemon(listener bool, workerCt int) {
svrCtx := models.NewServerCtx(Conf)
svrCtx.Init()
planner.GridConf.SchemaLoader = svrCtx.SchemaLoader
planner.GridConf.JobMaker = svrCtx.JobMaker
svr, err := NewServer(svrCtx)
if err != nil {
u.Errorf("Could not create new server err=%v", err)
return
}
quit := make(chan bool)
go func() {
sc := make(chan os.Signal, 1)
signal.Notify(sc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
sig := <-sc
close(quit) // This should signal worker nodes, master node to quit
u.Infof("Got signal [%d] to exit.", sig)
time.Sleep(time.Millisecond * 50)
svr.Shutdown(Reason{Reason: "signal", Message: fmt.Sprintf("%v", sig)})
}()
// Gratuitous Loading Banner
fmt.Println(banner())
// If distributed mode then we need to prepare the master planner
if Conf.DistributedMode() {
go func() {
// PlanGrid is the master that coordinates
// with etcd and submit tasks to worker nodes
svrCtx.PlanGrid.Run(quit)
}()
}
// If listener, run tcp listeners, optionally
// a daemon can be worker only mode without listeners
if listener {
// Blocking
svr.RunListeners()
}
}
// Server is the main DataUX server, the running process and responsible for:
// 1) starting *listeners* - network transports/protocols (mysql,mongo,redis)
// 2) routing requests through *Handlers*(plugins) which
// filter, transform, log, etc
// 3) connecting front-end requests to the execution/planning engine
type Server struct {
conf *models.Config
ctx *models.ServerCtx
// Frontend listener is a Listener Protocol handler
// to listen on specific port such as mysql
listeners []models.Listener
stop chan bool
}
// Reason info on internal events
type Reason struct {
Reason string
err error
Message string
}
func NewServer(ctx *models.ServerCtx) (*Server, error) {
svr := &Server{conf: ctx.Config, ctx: ctx, stop: make(chan bool)}
if err := svr.loadFrontends(); err != nil {
return nil, err
}
return svr, nil
}
// Run the listeners
func (m *Server) Run() {
m.RunListeners()
}
// RunListeners a blocking runner, that starts [mysql,?] tcp listeners
// and returns if connection to listeners cannot be established
func (m *Server) RunListeners() {
if len(m.listeners) == 0 {
u.Errorf("No frontends found ")
return
}
for _, listener := range m.listeners {
u.Infof("starting listener: %s", listener)
go func(l models.Listener) {
defer func() {
if r := recover(); r != nil {
u.Errorf("listener shutdown: %v", r)
}
}()
// Blocking runner
if err := l.Run(m.stop); err != nil {
u.Errorf("error on frontend? %#v %v", l, err)
m.Shutdown(Reason{"error", err, ""})
}
}(listener)
}
// block until shutdown signal
<-m.stop
// after shutdown, ensure they are all closed
for _, listener := range m.listeners {
if err := listener.Close(); err != nil {
u.Errorf("Error shuting down %T err=%v", listener, err)
}
}
}
func (m *Server) loadFrontends() error {
for name, listener := range models.Listeners() {
//u.Debugf("looking for frontend for %v", name)
for _, listenConf := range m.conf.Frontends {
if listenConf.Type == name {
//u.Debugf("found listener conf: %#v", listenConf)
err := listener.Init(listenConf, m.ctx)
if err != nil {
u.Errorf("Could not get frontend", err)
return err
}
m.listeners = append(m.listeners, listener)
//u.Infof("Loaded listener %s ", name)
}
}
}
return nil
}
// Shutdown listeners and close down
func (m *Server) Shutdown(reason Reason) {
close(m.stop)
}