Skip to content

Commit 94512f5

Browse files
committed
add zone and rack info; return hostname in urls
1 parent 262cd52 commit 94512f5

File tree

7 files changed

+90
-54
lines changed

7 files changed

+90
-54
lines changed

client_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ func TestClient(t *testing.T) {
4141
cleanDB(t, tr.db)
4242
go tr.Run()
4343
defer tr.Shutdown()
44-
_, err = tr.db.Exec("insert into host(hostid, hostname, status, hostip) values(1, 'foo', 'alive', '127.0.0.1')")
44+
_, err = tr.db.Exec("insert into zone(zoneid, name) values(1, 'zone1')")
45+
if err != nil {
46+
t.Fatal(err)
47+
}
48+
_, err = tr.db.Exec("insert into rack(rackid, zoneid, subnet) values(1, 1, '0.0.0.0/0')")
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
_, err = tr.db.Exec("insert into host(hostid, hostname, status, hostip, rackid) values(1, '127.0.0.1', 'alive', '127.0.0.1', 1)")
4553
if err != nil {
4654
t.Fatal(err)
4755
}

config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func init() {
1717

1818
func cleanDB(t *testing.T, db *sql.DB) {
1919
t.Helper()
20-
tables := []string{"file_on", "tempfile", "file", "device", "host"}
20+
tables := []string{"file_on", "tempfile", "file", "device", "host", "rack", "zone"}
2121
for _, table := range tables {
2222
_, err := db.Exec("delete from " + table)
2323
if err != nil {

drain_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ func TestDrain(t *testing.T) {
2121
cleanDB(t, tr.db)
2222
go tr.Run()
2323
defer tr.Shutdown()
24-
_, err = tr.db.Exec("insert into host(hostid, hostname, status, hostip) values(1, 'foo', 'alive', '127.0.0.1')")
24+
_, err = tr.db.Exec("insert into zone(zoneid, name) values(1, 'zone1')")
25+
if err != nil {
26+
t.Fatal(err)
27+
}
28+
_, err = tr.db.Exec("insert into rack(rackid, zoneid, subnet) values(1, 1, '0.0.0.0/0')")
29+
if err != nil {
30+
t.Fatal(err)
31+
}
32+
_, err = tr.db.Exec("insert into host(hostid, hostname, status, hostip, rackid) values(1, '127.0.0.1', 'alive', '127.0.0.1', 1)")
2533
if err != nil {
2634
t.Fatal(err)
2735
}

schema.sql

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
SET default_storage_engine=INNODB;
22

3+
CREATE TABLE `zone` (
4+
`zoneid` mediumint(8) unsigned NOT NULL,
5+
`name` varchar(40) NOT NULL,
6+
PRIMARY KEY (`zoneid`)
7+
);
8+
9+
CREATE TABLE `rack` (
10+
`rackid` mediumint(8) unsigned NOT NULL,
11+
`zoneid` mediumint(8) unsigned NOT NULL,
12+
`subnet` varchar(18) NOT NULL,
13+
PRIMARY KEY (`rackid`),
14+
FOREIGN KEY (`zoneid`) REFERENCES `zone` (`zoneid`)
15+
);
16+
317
CREATE TABLE `host` (
418
`hostid` mediumint(8) unsigned NOT NULL,
519
`status` enum('alive','dead','down') NOT NULL DEFAULT 'alive',
620
`hostname` varchar(40) NOT NULL,
721
`hostip` varchar(40) NOT NULL,
8-
PRIMARY KEY (`hostid`)
22+
`rackid` mediumint(8) unsigned NOT NULL,
23+
PRIMARY KEY (`hostid`),
24+
FOREIGN KEY (`rackid`) REFERENCES `rack` (`rackid`)
925
);
1026

1127
CREATE TABLE `device` (

server_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ func setupServer(t *testing.T, ttl time.Duration) (s *Server, closeFunc func())
3434

3535
cleanDB(t, s.db)
3636

37-
_, err = s.db.Exec("insert into host(hostid, hostname, status, hostip) values(1, 'foo', 'alive', '127.0.0.1')")
37+
_, err = s.db.Exec("insert into zone(zoneid, name) values(1, 'zone1')")
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
_, err = s.db.Exec("insert into rack(rackid, zoneid, subnet) values(1, 1, '0.0.0.0/0')")
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
_, err = s.db.Exec("insert into host(hostid, hostname, status, hostip, rackid) values(1, 'foo', 'alive', '127.0.0.1', 1)")
3846
if err != nil {
3947
t.Fatal(err)
4048
}

tracker.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,20 @@ func (t *Tracker) internalServerError(message string, err error, r *http.Request
139139
func (t *Tracker) getPath(w http.ResponseWriter, r *http.Request) {
140140
var response GetPath
141141
key := r.FormValue("key")
142-
row := t.db.QueryRow("select h.hostip, d.read_port, d.devid, f.fid, f.created_at "+
142+
row := t.db.QueryRow("select h.hostname, d.read_port, d.devid, f.fid, f.created_at "+
143143
"from file f "+
144144
"join file_on fo on f.fid=fo.fid "+
145145
"join device d on d.devid=fo.devid "+
146146
"join host h on h.hostid=d.hostid "+
147147
"where h.status='alive' "+
148148
"and d.status in ('alive', 'drain') "+
149149
"and f.dkey=?", key)
150-
var hostip string
150+
var hostname string
151151
var httpPort int64
152152
var devid int64
153153
var fid int64
154154
var createdAt mysql.NullTime
155-
err := row.Scan(&hostip, &httpPort, &devid, &fid, &createdAt)
155+
err := row.Scan(&hostname, &httpPort, &devid, &fid, &createdAt)
156156
if err == sql.ErrNoRows {
157157
http.Error(w, "file not found", http.StatusNotFound)
158158
return
@@ -161,7 +161,7 @@ func (t *Tracker) getPath(w http.ResponseWriter, r *http.Request) {
161161
t.internalServerError("cannot scan rows", err, r, w)
162162
return
163163
}
164-
response.Path = fmt.Sprintf("http://%s:%d/dev%d/%s", hostip, httpPort, devid, vivify(fid))
164+
response.Path = fmt.Sprintf("http://%s:%d/dev%d/%s", hostname, httpPort, devid, vivify(fid))
165165
response.CreatedAt = createdAt.Time.Format(time.RFC3339)
166166
encoder := json.NewEncoder(w)
167167
encoder.Encode(response) // nolint: errcheck
@@ -172,7 +172,7 @@ func (t *Tracker) getPaths(w http.ResponseWriter, r *http.Request) {
172172
Paths: make([]GetPath, 0),
173173
}
174174
key := r.FormValue("key")
175-
rows, err := t.db.Query("select h.hostip, d.read_port, d.devid, f.fid, f.created_at "+
175+
rows, err := t.db.Query("select h.hostname, d.read_port, d.devid, f.fid, f.created_at "+
176176
"from file f "+
177177
"join file_on fo on f.fid=fo.fid "+
178178
"join device d on d.devid=fo.devid "+
@@ -186,12 +186,12 @@ func (t *Tracker) getPaths(w http.ResponseWriter, r *http.Request) {
186186
}
187187
defer rows.Close() // nolint: errcheck
188188
for rows.Next() {
189-
var hostip string
189+
var hostname string
190190
var httpPort int64
191191
var devid int64
192192
var fid int64
193193
var createdAt mysql.NullTime
194-
err = rows.Scan(&hostip, &httpPort, &devid, &fid, &createdAt)
194+
err = rows.Scan(&hostname, &httpPort, &devid, &fid, &createdAt)
195195
if err == sql.ErrNoRows {
196196
http.Error(w, "file not found", http.StatusNotFound)
197197
return
@@ -201,7 +201,7 @@ func (t *Tracker) getPaths(w http.ResponseWriter, r *http.Request) {
201201
return
202202
}
203203
path := GetPath{
204-
Path: fmt.Sprintf("http://%s:%d/dev%d/%s", hostip, httpPort, devid, vivify(fid)),
204+
Path: fmt.Sprintf("http://%s:%d/dev%d/%s", hostname, httpPort, devid, vivify(fid)),
205205
CreatedAt: createdAt.Time.Format(time.RFC3339),
206206
}
207207
response.Paths = append(response.Paths, path)
@@ -262,13 +262,13 @@ func (t *Tracker) createOpen(w http.ResponseWriter, r *http.Request) {
262262
var errNoDeviceAvailable = errors.New("no device available")
263263

264264
type aliveDevice struct {
265-
hostip string
265+
hostname string
266266
httpPort int64
267267
devid int64
268268
}
269269

270270
func (d *aliveDevice) PatchURL(fid int64) string {
271-
return fmt.Sprintf("http://%s:%d/dev%d/%s", d.hostip, d.httpPort, d.devid, vivify(fid))
271+
return fmt.Sprintf("http://%s:%d/dev%d/%s", d.hostname, d.httpPort, d.devid, vivify(fid))
272272
}
273273

274274
func findAliveDevice(db *sql.DB, size int64, devids []int64) (*aliveDevice, error) {
@@ -282,7 +282,7 @@ func findAliveDevice(db *sql.DB, size int64, devids []int64) (*aliveDevice, erro
282282
} else {
283283
devidsSQL = "and d.status='alive' "
284284
}
285-
rows, err := db.Query("select h.hostip, d.write_port, d.devid "+
285+
rows, err := db.Query("select h.hostname, d.write_port, d.devid "+
286286
"from device d "+
287287
"join host h on d.hostid=h.hostid "+
288288
"where h.status='alive' "+
@@ -297,7 +297,7 @@ func findAliveDevice(db *sql.DB, size int64, devids []int64) (*aliveDevice, erro
297297
defer rows.Close() // nolint: errcheck
298298
for rows.Next() {
299299
var d aliveDevice
300-
err = rows.Scan(&d.hostip, &d.httpPort, &d.devid)
300+
err = rows.Scan(&d.hostname, &d.httpPort, &d.devid)
301301
if err != nil {
302302
return nil, err
303303
}
@@ -385,12 +385,12 @@ func (t *Tracker) createClose(w http.ResponseWriter, r *http.Request) {
385385
t.internalServerError("cannot insert file_on record", err, r, w)
386386
return
387387
}
388-
row = tx.QueryRow("select h.hostip, d.read_port "+
388+
row = tx.QueryRow("select h.hostname, d.read_port "+
389389
"from device d join host h on h.hostid=d.hostid "+
390390
"where d.devid=?", devid)
391-
var hostip string
391+
var hostname string
392392
var httpPort int64
393-
err = row.Scan(&hostip, &httpPort)
393+
err = row.Scan(&hostname, &httpPort)
394394
if err != nil {
395395
t.internalServerError("cannot select host ip", err, r, w)
396396
return
@@ -404,7 +404,7 @@ func (t *Tracker) createClose(w http.ResponseWriter, r *http.Request) {
404404
go t.publishDeleteTask(olddevids, oldfid)
405405
}
406406
var response CreateClose
407-
response.Path = fmt.Sprintf("http://%s:%d/dev%d/%s", hostip, httpPort, devid, vivify(fid))
407+
response.Path = fmt.Sprintf("http://%s:%d/dev%d/%s", hostname, httpPort, devid, vivify(fid))
408408
encoder := json.NewEncoder(w)
409409
encoder.Encode(response) // nolint: errcheck
410410
}

tracker_test.go

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,30 @@ func TestPing(t *testing.T) {
3030
}
3131
}
3232

33-
func TestGetPath(t *testing.T) {
34-
tr, err := NewTracker(testConfig)
33+
func insertHost(t *testing.T, tr *Tracker) {
34+
t.Helper()
35+
36+
_, err := tr.db.Exec("insert into zone(zoneid, name) values(1, 'zone1')")
3537
if err != nil {
3638
t.Fatal(err)
3739
}
38-
cleanDB(t, tr.db)
39-
_, err = tr.db.Exec("insert into host(hostid, hostname, status, hostip) values(1, 'foo', 'alive', '1.2.3.4')")
40+
_, err = tr.db.Exec("insert into rack(rackid, zoneid, subnet) values(1, 1, '0.0.0.0/0')")
4041
if err != nil {
4142
t.Fatal(err)
4243
}
44+
_, err = tr.db.Exec("insert into host(hostid, hostname, status, hostip, rackid) values(1, 'foo', 'alive', '1.2.3.4', 1)")
45+
if err != nil {
46+
t.Fatal(err)
47+
}
48+
}
49+
50+
func TestGetPath(t *testing.T) {
51+
tr, err := NewTracker(testConfig)
52+
if err != nil {
53+
t.Fatal(err)
54+
}
55+
cleanDB(t, tr.db)
56+
insertHost(t, tr)
4357
_, err = tr.db.Exec("insert into device(devid, status, hostid, read_port) values(2, 'alive', 1, 1234)")
4458
if err != nil {
4559
t.Fatal(err)
@@ -68,7 +82,7 @@ func TestGetPath(t *testing.T) {
6882
if err != nil {
6983
t.Fatal(err)
7084
}
71-
expected := "http://1.2.3.4:1234/dev2/0/000/000/0000000042.fid"
85+
expected := "http://foo:1234/dev2/0/000/000/0000000042.fid"
7286
if resp.Path != expected {
7387
t.Errorf("handler returned unexpected path: got %v want %v",
7488
resp.Path, expected)
@@ -81,10 +95,7 @@ func TestCreateOpen(t *testing.T) {
8195
t.Fatal(err)
8296
}
8397
cleanDB(t, tr.db)
84-
_, err = tr.db.Exec("insert into host(hostid, hostname, status, hostip) values(1, 'foo', 'alive', '1.2.3.4')")
85-
if err != nil {
86-
t.Fatal(err)
87-
}
98+
insertHost(t, tr)
8899
_, err = tr.db.Exec("insert into device(devid, status, hostid, bytes_total, bytes_used, bytes_free, write_port) values(2, 'alive', 1, 1000, 500, 500, 1234)")
89100
if err != nil {
90101
t.Fatal(err)
@@ -104,7 +115,7 @@ func TestCreateOpen(t *testing.T) {
104115
t.Errorf("handler returned wrong status code: got %v want %v",
105116
status, http.StatusOK)
106117
}
107-
expected := "{\"path\":\"http://1.2.3.4:1234/dev2/0/000/000/0000000005.fid\",\"fid\":5}\n"
118+
expected := "{\"path\":\"http://foo:1234/dev2/0/000/000/0000000005.fid\",\"fid\":5}\n"
108119
if rr.Body.String() != expected {
109120
t.Errorf("handler returned unexpected body: got %v want %v",
110121
rr.Body.String(), expected)
@@ -117,10 +128,7 @@ func TestCreateClose(t *testing.T) {
117128
t.Fatal(err)
118129
}
119130
cleanDB(t, tr.db)
120-
_, err = tr.db.Exec("insert into host(hostid, hostname, status, hostip) values(1, 'foo', 'alive', '1.2.3.4')")
121-
if err != nil {
122-
t.Fatal(err)
123-
}
131+
insertHost(t, tr)
124132
_, err = tr.db.Exec("insert into device(devid, status, hostid, read_port) values(2, 'alive', 1, 5678)")
125133
if err != nil {
126134
t.Fatal(err)
@@ -145,7 +153,7 @@ func TestCreateClose(t *testing.T) {
145153
if err != nil {
146154
t.Fatal(err)
147155
}
148-
expected := "http://1.2.3.4:5678/dev2/0/000/000/0000000009.fid"
156+
expected := "http://foo:5678/dev2/0/000/000/0000000009.fid"
149157
if resp.Path != expected {
150158
t.Errorf("handler returned unexpected path: got %v want %v",
151159
resp.Path, expected)
@@ -158,10 +166,7 @@ func TestCreateCloseOverwrite(t *testing.T) {
158166
t.Fatal(err)
159167
}
160168
cleanDB(t, tr.db)
161-
_, err = tr.db.Exec("insert into host(hostid, hostname, status, hostip) values(1, 'foo', 'alive', '1.2.3.4')")
162-
if err != nil {
163-
t.Fatal(err)
164-
}
169+
insertHost(t, tr)
165170
_, err = tr.db.Exec("insert into device(devid, status, hostid) values(2, 'alive', 1)")
166171
if err != nil {
167172
t.Fatal(err)
@@ -199,7 +204,7 @@ func TestCreateCloseOverwrite(t *testing.T) {
199204
if err != nil {
200205
t.Fatal(err)
201206
}
202-
expected := "http://1.2.3.4:5678/dev3/0/000/000/0000000009.fid"
207+
expected := "http://foo:5678/dev3/0/000/000/0000000009.fid"
203208
if resp.Path != expected {
204209
t.Errorf("handler returned unexpected path: got %v want %v",
205210
resp.Path, expected)
@@ -212,10 +217,7 @@ func TestDelete(t *testing.T) {
212217
t.Fatal(err)
213218
}
214219
cleanDB(t, tr.db)
215-
_, err = tr.db.Exec("insert into host(hostid, hostname, status, hostip) values(1, 'foo', 'alive', '1.2.3.4')")
216-
if err != nil {
217-
t.Fatal(err)
218-
}
220+
insertHost(t, tr)
219221
_, err = tr.db.Exec("insert into device(devid, hostid) values(2, 1)")
220222
if err != nil {
221223
t.Fatal(err)
@@ -257,10 +259,7 @@ func TestGetDevices(t *testing.T) {
257259
t.Fatal(err)
258260
}
259261
cleanDB(t, tr.db)
260-
_, err = tr.db.Exec("insert into host(hostid, hostname, hostip, status) values(1, 'foo', '127.0.0.1', 'alive')")
261-
if err != nil {
262-
t.Fatal(err)
263-
}
262+
insertHost(t, tr)
264263
_, err = tr.db.Exec("insert into device(devid, status, hostid, bytes_total, bytes_used, bytes_free, updated_at) values(2, 'alive', 1, 1000, 500, 500, from_unixtime(1510216046))")
265264
if err != nil {
266265
t.Fatal(err)
@@ -289,10 +288,7 @@ func TestGetHosts(t *testing.T) {
289288
t.Fatal(err)
290289
}
291290
cleanDB(t, tr.db)
292-
_, err = tr.db.Exec("insert into host(hostid, hostname, hostip, status) values(1, 'foo', '127.0.0.1', 'alive')")
293-
if err != nil {
294-
t.Fatal(err)
295-
}
291+
insertHost(t, tr)
296292
req, err := http.NewRequest("GET", "/get-hosts", nil)
297293
if err != nil {
298294
t.Fatal(err)
@@ -304,7 +300,7 @@ func TestGetHosts(t *testing.T) {
304300
t.Errorf("handler returned wrong status code: got %v want %v",
305301
status, http.StatusOK)
306302
}
307-
expected := "{\"hosts\":[{\"hostid\":1,\"status\":\"alive\",\"hostname\":\"foo\",\"hostip\":\"127.0.0.1\"}]}\n"
303+
expected := "{\"hosts\":[{\"hostid\":1,\"status\":\"alive\",\"hostname\":\"foo\",\"hostip\":\"1.2.3.4\"}]}\n"
308304

309305
if rr.Body.String() != expected {
310306
t.Errorf("handler returned unexpected body: got %v want %v",

0 commit comments

Comments
 (0)