-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
352 lines (328 loc) · 8.42 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package main
import (
"context"
"database/sql"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/cenkalti/log"
"github.com/cenkalti/redialer/amqpredialer"
"github.com/getsentry/raven-go"
"github.com/shirou/gopsutil/disk"
"github.com/streadway/amqp"
)
// Server runs on storage servers.
type Server struct {
config *Config
db *sql.DB
log log.Logger
readServer http.Server
writeServer http.Server
amqp *amqpredialer.AMQPRedialer
onceDiskStatsUpdated sync.Once
devid int64
hostname string
shutdown chan struct{}
Ready chan struct{}
diskStatsUpdated chan struct{}
diskStatsStopped chan struct{}
diskCleanStopped chan struct{}
deviceCleanStopped chan struct{}
amqpRedialerStopped chan struct{}
}
// NewServer returns a new Server instance.
func NewServer(c *Config) (*Server, error) {
fi, err := os.Stat(c.Server.DataDir)
if err != nil {
return nil, err
}
if !fi.IsDir() {
return nil, fmt.Errorf("path must be a directory: %s", c.Server.DataDir)
}
devid, err := strconv.ParseInt(strings.TrimPrefix(filepath.Base(c.Server.DataDir), "dev"), 10, 32)
if err != nil {
return nil, fmt.Errorf("cannot determine device ID from dir: %s", c.Server.DataDir)
}
db, err := openDatabase(c.Database)
if err != nil {
return nil, err
}
logger := log.NewLogger("server")
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
s := &Server{
config: c,
devid: devid,
db: db,
log: logger,
hostname: hostname,
shutdown: make(chan struct{}),
Ready: make(chan struct{}),
diskStatsUpdated: make(chan struct{}),
diskStatsStopped: make(chan struct{}),
diskCleanStopped: make(chan struct{}),
deviceCleanStopped: make(chan struct{}),
amqpRedialerStopped: make(chan struct{}),
}
devicePrefix := "/" + filepath.Base(s.config.Server.DataDir)
s.writeServer.Handler = http.StripPrefix(devicePrefix, newFileReceiver(s.config.Server.DataDir, s.log, s.db))
s.writeServer.Handler = http.HandlerFunc(raven.RecoveryHandler(addVersion(s.writeServer.Handler)))
s.readServer.Handler = http.StripPrefix(devicePrefix, http.FileServer(http.Dir(s.config.Server.DataDir)))
if s.config.Debug {
s.log.SetLevel(log.DEBUG)
}
s.amqp, err = amqpredialer.New(c.AMQP.URL)
if err != nil {
return nil, err
}
return s, nil
}
// Run this server in a blocking manner. Running server can be stopped with Shutdown().
func (s *Server) Run() error {
writeListener, err := net.Listen("tcp", s.config.Server.ListenAddress)
if err != nil {
return err
}
readListener, err := net.Listen("tcp", s.config.Server.ListenAddressForRead)
if err != nil {
return err
}
go s.cleanDisk()
go s.cleanDevice()
go s.updateDiskStats()
go s.consumeDeleteQueue()
s.log.Notice("Server is started.")
errCh := make(chan error, 2)
go func() {
err2 := s.writeServer.Serve(writeListener)
if err2 == http.ErrServerClosed {
s.log.Notice("Write server is shutting down.")
}
errCh <- err2
}()
go func() {
err2 := s.readServer.Serve(readListener)
if err2 == http.ErrServerClosed {
s.log.Notice("Read server is shutting down.")
}
errCh <- err2
}()
go func() {
s.amqp.Run()
close(s.amqpRedialerStopped)
}()
go s.notifyReady()
err = <-errCh
if err != nil {
return err
}
err = <-errCh
return err
}
func (s *Server) notifyReady() {
select {
case <-s.shutdown:
return
case <-s.diskStatsUpdated:
close(s.Ready)
}
}
// Shutdown the server.
func (s *Server) Shutdown() error {
close(s.shutdown)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(s.config.Server.ShutdownTimeout))
_ = cancel
err := s.readServer.Shutdown(ctx)
if err != nil {
s.log.Error("Error while shutting down read HTTP server")
return err
}
err = s.writeServer.Shutdown(ctx)
if err != nil {
s.log.Error("Error while shutting down write HTTP server")
return err
}
<-s.diskStatsStopped
<-s.diskCleanStopped
err = s.db.Close()
if err != nil {
s.log.Error("Error while closing database connection")
return err
}
err = s.amqp.Close()
if err != nil {
return err
}
<-s.amqpRedialerStopped
return nil
}
func (s *Server) updateDiskStats() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
iostat, err := newIOStat(s.config.Server.DataDir, 10*time.Second)
if err != nil {
s.log.Warningln("Cannot get stats for dir:", s.config.Server.DataDir, "err:", err.Error())
}
for {
select {
case <-ticker.C:
total, used, free := s.getDiskUsage()
utilization := s.getDiskUtilization(iostat)
_, err = s.db.Exec("update device set io_utilization=?, bytes_total=?, bytes_used=?, bytes_free=?, updated_at=current_timestamp where devid=?", utilization, total, used, free, s.devid)
s.onceDiskStatsUpdated.Do(func() { close(s.diskStatsUpdated) })
if err != nil {
s.log.Errorln("Cannot update device stats:", err.Error())
continue
}
case <-s.shutdown:
close(s.diskStatsStopped)
return
}
}
}
func (s *Server) getDiskUsage() (total, used, free sql.NullInt64) {
usage, err := disk.Usage(s.config.Server.DataDir)
if err != nil {
s.log.Errorln("Cannot get disk usage:", err.Error())
return
}
total.Valid = true
total.Int64 = int64(usage.Total)
free.Valid = true
free.Int64 = int64(usage.Free)
used.Valid = true
used.Int64 = total.Int64 - free.Int64
return
}
func (s *Server) getDiskUtilization(iostat *IOStat) (utilization sql.NullInt64) {
if iostat == nil {
return
}
value, err := iostat.Utilization()
if err == errUtilizationNotAvailable {
return
} else if err != nil {
s.log.Errorln("Cannot get disk IO utilization:", err.Error())
return
}
utilization.Valid = true
utilization.Int64 = int64(value)
return
}
func declareDeleteQueue(ch *amqp.Channel, devid int64) (amqp.Queue, error) {
return ch.QueueDeclare(
deleteQueueName(devid),
true, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
}
func deleteQueueName(devid int64) string {
return "delete.dev" + strconv.FormatInt(devid, 10)
}
func (s *Server) consumeDeleteQueue() {
s.log.Debug("Starting delete queue consumer..")
for {
select {
case <-s.shutdown:
return
case conn, ok := <-s.amqp.Conn():
if !ok {
s.log.Error("Cannot consume delete queue. AMQP connection is not open.")
time.Sleep(time.Second)
continue
}
err := s.processDeleteTasks(conn)
if err != nil {
s.log.Error("Error while processing delete task", err)
raven.CaptureError(err, nil)
}
}
}
}
func (s *Server) processDeleteTasks(conn *amqp.Connection) error {
ch, err := conn.Channel()
if err != nil {
return err
}
q, err := declareDeleteQueue(ch, s.devid)
if err != nil {
return err
}
pid := os.Getpid()
consumerTag := "efes-delete-worker:" + strconv.Itoa(pid) + "@" + s.hostname + "/" + strconv.FormatInt(s.devid, 10)
messages, err := ch.Consume(
q.Name, // queue
consumerTag, // consumer
false, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
return err
}
for {
select {
case <-s.shutdown:
return nil
case msg, ok := <-messages:
if !ok {
return amqp.ErrClosed
}
msgBody := string(msg.Body)
fileID, err := strconv.ParseInt(msgBody, 10, 64)
if err != nil {
s.log.Error("Error while parsing int", err)
continue
}
err = s.deleteFidOnDisk(fileID)
if err != nil {
s.log.Errorf("Failed to delete fid %d, %s", fileID, err)
err2 := msg.Nack(false, false)
if err2 != nil {
s.log.Errorf("NACK error: %s", err2)
return err2
}
continue
}
err = msg.Ack(false)
if err != nil {
s.log.Errorf("ACK error: %s", err)
return err
}
}
}
}
func (s *Server) deleteFidOnDisk(fileID int64) error {
s.log.Debug("Deleting fid on disk ", fileID)
path := filepath.Join(s.config.Server.DataDir, vivify(fileID))
err := os.Remove(path)
if err != nil {
if os.IsNotExist(err) {
s.log.Debugf("Fid path does not exist %s. ", path)
return nil
}
return err
}
s.log.Debugf("Fid %d deleted. ", fileID)
return nil
}
func vivify(fid int64) string {
return vivifyExt(fid, "fid")
}
func vivifyExt(fid int64, ext string) string {
s := fmt.Sprintf("%010d", fid)
return fmt.Sprintf("%s/%s/%s/%s.%s", s[0:1], s[1:4], s[4:7], s, ext)
}