/
catalog.go
551 lines (497 loc) · 15.1 KB
/
catalog.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
package core
// transfer2go implementation of Trivial File Catalog (TFC)
// Author: Valentin Kuznetsov <vkuznet@gmail.com>
import (
"database/sql"
"errors"
"fmt"
"os/exec"
"reflect"
"strconv"
"strings"
"time"
logs "github.com/sirupsen/logrus"
"github.com/vkuznet/transfer2go/utils"
// loads sqlite3 database layer
_ "github.com/mattn/go-sqlite3"
)
// Record represent main DB record we work with
type Record map[string]interface{}
// DB is global pointer to sql database object, it is initialized once when server starts
var DB *sql.DB
// DBTYPE holds database type, e.g. sqlite3
var DBTYPE string
// DBSQL represent common record we get from DB SQL statement
var DBSQL Record
// TFC stands for Trivial File Catalog
var TFC Catalog
// CatalogEntry represents an entry in TFC
type CatalogEntry struct {
Lfn string `json:"lfn"` // lfn stands for Logical File Name
Pfn string `json:"pfn"` // pfn stands for Physical File Name
Dataset string `json:"dataset"` // dataset represents collection of blocks
Block string `json:"block"` // block idetify single block within a dataset
Bytes int64 `json:"bytes"` // size of the files in bytes
Hash string `json:"hash"` // hash represents checksum of the pfn
TransferTime int64 `json:"transferTime"` // transfer time
Timestamp int64 `json:"timestamp"` // time stamp
}
// TransferData helps to structure the rows of transfers table
type TransferData struct {
Timestamp int64 `json:"timestamp"` // Helps to get data historically
CpuUsage float64 `json:"cpu"` // percentage of cpu used
MemUsage float64 `json:"ram"` // ram used in MB
Throughput float64 `json:"throughput"` // network throughput during transfer in MB
}
// Catalog represents Trivial File Catalog (TFC) of the model
type Catalog struct {
Type string `json:"type"` // catalog type, e.g. sqlite3, etc.
Uri string `json:"uri"` // catalog uri, e.g. file.db
Login string `json:"login"` // database login
Password string `json:"password"` // database password
Owner string `json:"owner"` // used by ORACLE DB, defines owner of the database
}
func check(msg string, err error) {
if err != nil {
logs.WithFields(logs.Fields{
"Message": msg,
"Err": err,
}).Fatal("Stop process")
}
}
// LoadSQL is a helper function to load DBS SQL statements
func LoadSQL(dbtype, owner string) Record {
dbsql := make(Record)
// query statement
tmplData := make(Record)
tmplData["Owner"] = owner
sdir := fmt.Sprintf("%s/sql/%s", utils.STATICDIR, dbtype)
for _, f := range utils.ListFiles(sdir) {
path := strings.Split(f, "/")
k := strings.Split(path[len(path)-1], ".")[0]
dbsql[k] = utils.ParseTmpl(f, tmplData)
}
return dbsql
}
// helper function to get SQL statement from DBSQL dict for a given key
func getSQL(key string) string {
// use generic query API to fetch the results from DB
stm, ok := DBSQL[key]
if !ok {
msg := fmt.Sprintf("Unable to load %s SQL", key)
logs.Fatal(msg)
}
return stm.(string)
}
// helper function to assign placeholder for SQL WHERE clause, it depends on database type
func placeholder(pholder string) string {
if DBTYPE == "ora" || DBTYPE == "oci8" {
return fmt.Sprintf(":%s", pholder)
} else if DBTYPE == "PostgreSQL" {
return fmt.Sprintf("$%s", pholder)
} else {
return "?"
}
}
// String provides string representation of CatalogEntry
func (c *CatalogEntry) String() string {
return fmt.Sprintf("<CatalogEntry: dataset=%s block=%s lfn=%s pfn=%s bytes=%d hash=%s transferTime=%d timestamp=%d>", c.Dataset, c.Block, c.Lfn, c.Pfn, c.Bytes, c.Hash, c.TransferTime, c.Timestamp)
}
// Dump method returns TFC dump in CSV format
func (c *Catalog) Dump() []byte {
if c.Type == "sqlite3" {
// cmd := fmt.Sprintf("sqlite3 %s .dump", c.Uri)
out, err := exec.Command("sqlite3", c.Uri, ".dump").Output()
if err != nil {
logs.WithFields(logs.Fields{
"Err": err,
}).Error("c.Dump")
}
return out
}
logs.WithFields(logs.Fields{
"Type": c.Type,
}).Println("Catalog Dump method is not implemented yet for")
return nil
}
// Add method adds entry to a catalog
func (c *Catalog) Add(entry CatalogEntry) error {
// add entry to the catalog
tx, e := DB.Begin()
check("Unable to setup transaction", e)
var stm string
var did, bid int
// insert dataset into dataset tables
stm = getSQL("insert_datasets")
_, e = DB.Exec(stm, entry.Dataset)
if e != nil {
if !strings.Contains(e.Error(), "UNIQUE") {
check("Unable to insert into datasets table", e)
}
}
// get dataset id
stm = getSQL("id_datasets")
rows, err := DB.Query(stm, entry.Dataset)
check("Unable to perform DB.Query over datasets table", err)
defer rows.Close()
for rows.Next() {
err = rows.Scan(&did)
check("Unable to scan rows for datasetid", err)
}
// insert block into block table
stm = getSQL("insert_blocks")
_, e = DB.Exec(stm, entry.Block, did)
if e != nil {
if !strings.Contains(e.Error(), "UNIQUE") {
check("Unable to insert into blocks table", e)
}
}
// get block id
stm = getSQL("id_blocks")
rows, err = DB.Query(stm, entry.Block)
check("Unable to DB.Query over blocks table", err)
for rows.Next() {
err = rows.Scan(&bid)
check("Unable to scan rows for datasetid", err)
}
// insert entry into files table
stm = getSQL("insert_files")
_, e = DB.Exec(stm, entry.Lfn, entry.Pfn, bid, did, entry.Bytes, entry.Hash, entry.TransferTime, entry.Timestamp)
if e != nil {
if !strings.Contains(e.Error(), "UNIQUE") {
check(fmt.Sprintf("Unable to DB.Exec(%s)", stm), e)
}
}
tx.Commit()
if utils.VERBOSE > 0 {
logs.WithFields(logs.Fields{
"Entry": entry.String(),
"Dataset Id": did,
"Block Id": bid,
}).Println("Committed to Catalog")
}
return nil
}
// Files returns list of files for specified conditions
func (c *Catalog) Files(dataset, block, lfn string) []string {
var files []string
req := TransferRequest{Dataset: dataset, Block: block, Lfn: lfn}
for _, rec := range c.Records(req) {
files = append(files, rec.Lfn)
}
return files
}
// PfnFiles returns list of files for specified conditions
func (c *Catalog) PfnFiles(dataset, block, lfn string) []string {
var files []string
req := TransferRequest{Dataset: dataset, Block: block, Lfn: lfn}
for _, rec := range c.Records(req) {
files = append(files, rec.Pfn)
}
return files
}
// helper function to convert interface into string representation
func asString(src interface{}) string {
switch v := src.(type) {
case string:
return v
case []byte:
return string(v)
}
rv := reflect.ValueOf(src)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(rv.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(rv.Uint(), 10)
case reflect.Float64:
return strconv.FormatFloat(rv.Float(), 'g', -1, 64)
case reflect.Float32:
return strconv.FormatFloat(rv.Float(), 'g', -1, 32)
case reflect.Bool:
return strconv.FormatBool(rv.Bool())
}
return fmt.Sprintf("%v", src)
}
// Snapshot returns a snapshot of the TFC catalog and return it as a map
// which holds table names and list of rows where each row is represented
// as a comma separated values
func (c *Catalog) Snapshot() map[string][]string {
maps := make(map[string][]string)
snapshots := []string{"files", "blocks", "datasets", "requests", "transfers"}
for _, name := range snapshots {
stm := getSQL(fmt.Sprintf("snapshot_%s", name))
// fetch data from DB
rows, err := DB.Query(stm)
if err != nil {
logs.WithFields(logs.Fields{
"Query": stm,
"Error": err,
}).Error("DB.Query")
return maps
}
defer rows.Close()
cols, err := rows.Columns() // Remember to check err afterwards
if err != nil {
logs.WithFields(logs.Fields{
"Error": err,
}).Error("Unable to get column names")
return maps
}
values := make([]interface{}, len(cols))
args := make([]interface{}, len(values))
for i := range values {
args[i] = &values[i]
}
var out []string
for rows.Next() {
err := rows.Scan(args...)
if err != nil {
logs.WithFields(logs.Fields{
"Err": err,
}).Error("rows.Scan")
}
var rowValues []string
for i := range cols {
rowValues = append(rowValues, asString(values[i]))
}
out = append(out, strings.Join(rowValues, ","))
}
maps[name] = out
}
return maps
}
// Records returns catalog records for a given transfer request
func (c *Catalog) Records(req TransferRequest) []CatalogEntry {
stm := getSQL("files_blocks_datasets")
var cond []string
var vals []interface{}
if req.Lfn != "" {
cond = append(cond, fmt.Sprintf("F.LFN=%s", placeholder("lfn")))
vals = append(vals, req.Lfn)
}
if req.Block != "" {
cond = append(cond, fmt.Sprintf("B.BLOCK=%s", placeholder("block")))
vals = append(vals, req.Block)
}
if req.Dataset != "" {
cond = append(cond, fmt.Sprintf("D.DATASET=%s", placeholder("dataset")))
vals = append(vals, req.Dataset)
}
if len(cond) > 0 {
stm += fmt.Sprintf(" WHERE %s", strings.Join(cond, " AND "))
}
if utils.VERBOSE > 0 {
logs.WithFields(logs.Fields{
"Query": stm,
"Value": vals,
}).Println("Records query")
}
// fetch data from DB
rows, err := DB.Query(stm, vals...)
if err != nil {
logs.WithFields(logs.Fields{
"Query": stm,
"Error": err,
}).Error("DB.Query")
return []CatalogEntry{}
}
defer rows.Close()
var out []CatalogEntry
for rows.Next() {
rec := CatalogEntry{}
err := rows.Scan(&rec.Dataset, &rec.Block, &rec.Lfn, &rec.Pfn, &rec.Bytes, &rec.Hash)
if err != nil {
logs.WithFields(logs.Fields{
"Err": err,
}).Error("rows.Scan")
}
out = append(out, rec)
}
return out
}
// Transfers method returns transfers of the agent in given time interval
func (c *Catalog) Transfers(time0, time1 string) []CatalogEntry {
stm := getSQL("transfers")
var vals []interface{}
stm += fmt.Sprintf(" WHERE TIMESTAMP>=%s AND TIMESTAMP<=%s", time0, time1)
vals = append(vals, time0)
vals = append(vals, time1)
if utils.VERBOSE > 0 {
logs.WithFields(logs.Fields{
"Query": stm,
"Value": vals,
}).Println("Records query", stm, vals)
}
// fetch data from DB
rows, err := DB.Query(stm, vals...)
if err != nil {
logs.WithFields(logs.Fields{
"Query": stm,
"Err": err,
}).Error("DB.Query")
return []CatalogEntry{}
}
defer rows.Close()
var out []CatalogEntry
for rows.Next() {
rec := CatalogEntry{}
err := rows.Scan(&rec.Bytes, &rec.TransferTime)
if err != nil {
logs.WithFields(logs.Fields{
"Err": err,
}).Error("rows.Scan")
}
out = append(out, rec)
}
return out
}
// GetTransfers provide details about transfers in given time interval
func (c *Catalog) GetTransfers(time0, time1 string) ([]TransferData, error) {
stm := getSQL("get_transfers")
// fetch data from DB
rows, err := DB.Query(stm, time0, time1)
if err != nil {
logs.WithFields(logs.Fields{
"Query": stm,
"Err": err,
}).Error("DB.Query")
return []TransferData{}, err
}
defer rows.Close()
var out []TransferData
for rows.Next() {
rec := TransferData{}
err := rows.Scan(&rec.Timestamp, &rec.CpuUsage, &rec.MemUsage, &rec.Throughput)
if err != nil {
logs.WithFields(logs.Fields{
"Err": err,
}).Error("Exec: rows.Scan")
}
out = append(out, rec)
}
return out, nil
}
// InsertRequest inserts new request
func (c *Catalog) InsertRequest(r TransferRequest) error {
stm := getSQL("insert_request")
_, e := DB.Exec(stm, r.Id, r.Lfn, r.Block, r.Dataset, r.SrcUrl, r.SrcAlias, r.DstUrl, r.DstAlias, r.RegUrl, r.RegAlias, "pending", r.Priority)
logs.WithFields(logs.Fields{
"Request": r,
}).Info("Catalog: InsertRequest")
if e != nil {
if !strings.Contains(e.Error(), "UNIQUE") {
check("Unable to insert into REQUESTS table", e)
}
}
return e
}
// Exec method update db upto three times
func (c *Catalog) Exec(stm, status, rid string) error {
count := 0
_, err := DB.Exec(stm, status, rid)
for err != nil && count < 3 {
_, err = DB.Exec(stm, status, rid)
count += 1
time.Sleep(time.Second * 1)
}
return err
}
// UpdateRequest updates the status of request
func (c *Catalog) UpdateRequest(rid string, status string) error {
stm := getSQL("update_request")
err := c.Exec(stm, status, rid)
return err
}
// RetrieveRequest gets the request details based on request id
func (c *Catalog) RetrieveRequest(r *TransferRequest) error {
stm := getSQL("request_by_id")
rows, err := DB.Query(stm, r.Id)
if err != nil {
r.Status = err.Error()
return err
}
defer rows.Close()
for rows.Next() {
if err := rows.Scan(&r.Lfn, &r.Block, &r.Dataset, &r.SrcUrl, &r.SrcAlias, &r.DstUrl, &r.DstAlias, &r.RegUrl, &r.RegAlias, &r.Priority); err != nil {
r.Status = err.Error()
return err
}
}
return nil
}
// GetStatus gets the status of request
func (c *Catalog) GetStatus(rid string) (string, error) {
var status string
stm := getSQL("get_status")
rows, err := DB.Query(stm, rid)
if err != nil {
return "", err
}
for rows.Next() {
if err := rows.Scan(&status); err != nil {
return "", err
}
}
defer rows.Close()
return status, err
}
// ListRequest gets specific type of transfer requests according to status
func (c *Catalog) ListRequest(query string) ([]TransferRequest, error) {
var (
err error
rows *sql.Rows
)
switch query {
case "pending":
stm := getSQL("request_by_status") // Request is waiting for the approval
rows, err = DB.Query(stm, query)
case "finished":
stm := getSQL("request_by_status") // Request is successfully transferred
rows, err = DB.Query(stm, query)
case "all":
stm := getSQL("all_request") // Get all the requests
rows, err = DB.Query(stm)
case "deleted":
stm := getSQL("request_by_status") // Request is deleted without transfer
rows, err = DB.Query(stm, query)
case "error":
stm := getSQL("request_by_status") // Error occurred while transferring data
rows, err = DB.Query(stm, query)
case "processing":
stm := getSQL("request_by_status") // Error occurred while transferring data
rows, err = DB.Query(stm, query)
default:
return nil, errors.New("Requested request type could not find")
}
if err != nil {
return nil, err
}
cols, err := rows.Columns()
defer rows.Close()
if err != nil {
return nil, err
}
pointers := make([]interface{}, len(cols))
con := make([]string, len(cols)) // A pointer to Columns of db
var requests []TransferRequest
for i := range pointers {
pointers[i] = &con[i]
}
// Sqlite columns => 0:id 1:rid 2:file 3:block 4:dataset 5:srcurl 6:srcalias 7:dsturl 8:dstalias 9:regurl 10:regalias 11:status 12:priority
for rows.Next() {
rows.Scan(pointers...)
priority, err := strconv.Atoi(con[12])
if err != nil {
return nil, err
}
r := TransferRequest{SrcUrl: con[5], SrcAlias: con[6], DstUrl: con[7], DstAlias: con[8], RegUrl: con[9], RegAlias: con[10], Lfn: con[2], Block: con[3], Dataset: con[4], Id: con[1], Priority: priority, Status: con[11]}
requests = append(requests, r)
}
return requests, err
}
// InsertTransfers inserts new row to TRANSFERS table
func (c *Catalog) InsertTransfers(time int64, cpuUsage float64, memUsage float64, throughput float64) {
stm := getSQL("insert_transfers")
DB.Exec(stm, time, cpuUsage, memUsage, throughput)
}