-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrain.go
147 lines (138 loc) · 3.02 KB
/
drain.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
package main
import (
"database/sql"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/cenkalti/log"
)
type Drainer struct {
Dest []int64
config *Config
devid int64
db *sql.DB
client *Client
log log.Logger
shutdown chan struct{}
stopped chan struct{}
stopOnError bool
}
func NewDrainer(c *Config) (*Drainer, 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
}
clt, err := NewClient(c)
if err != nil {
return nil, err
}
clt.drainer = true
logger := log.NewLogger("drain")
d := &Drainer{
config: c,
devid: devid,
db: db,
client: clt,
log: logger,
shutdown: make(chan struct{}),
stopped: make(chan struct{}),
}
if d.config.Debug {
d.log.SetLevel(log.DEBUG)
}
return d, nil
}
func (d *Drainer) Run() error {
d.log.Noticeln("Setting device status to 'drain' on device:", d.devid)
_, err := d.db.Exec("update device set status='drain' where devid=?", d.devid)
if err != nil {
return err
}
var fids []int64
rows, err := d.db.Query("select fid from file_on where devid=?", d.devid)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var fid int64
err = rows.Scan(&fid)
if err != nil {
return err
}
fids = append(fids, fid)
}
if err = rows.Err(); err != nil {
return err
}
for i, fid := range fids {
select {
case <-d.shutdown:
close(d.stopped)
return nil
default:
}
d.log.Infof("moving fid=%v; %v of %v (%v%%)", fid, i+1, len(fids), ((i+1)*100)/len(fids))
if err = d.moveFile(fid); err != nil {
d.log.Error(err)
if d.stopOnError {
return err
}
}
}
return nil
}
func (d *Drainer) moveFile(fid int64) error {
fidpath := filepath.Join(d.config.Server.DataDir, vivify(fid))
f, err := os.Open(fidpath)
if os.IsNotExist(err) {
d.log.Warningf("file (%s) does not exist on disk; removing fid (%d) from device (%d)", fidpath, fid, d.devid)
_, err = d.db.Exec("delete from file_on where fid=? and devid=?", fid, d.devid)
return err
}
if err != nil {
return err
}
defer logCloseFile(d.log, f)
fi, err := f.Stat()
if err != nil {
return err
}
ad, err := findAliveDevice(d.db, fi.Size(), d.Dest, "")
if err != nil {
return err
}
newPath := ad.PatchURL(fid)
_, err = d.client.sendFile(newPath, f, fi.Size())
if err != nil {
return err
}
_, err = d.db.Exec("update file_on set devid=? where devid=? and fid=?", ad.devid, d.devid, fid)
if err != nil {
return err
}
return os.Remove(fidpath)
}
func (d *Drainer) Shutdown() error {
close(d.shutdown)
<-d.stopped
err := d.db.Close()
if err != nil {
d.log.Error("Error while closing database connection")
return err
}
return nil
}