-
Notifications
You must be signed in to change notification settings - Fork 0
/
journal.go
145 lines (113 loc) · 2.91 KB
/
journal.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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"time"
"github.com/apple/foundationdb/bindings/go/src/fdb"
"github.com/bitgn/layers/go/benchmark/bench"
)
var (
jsonKey = append([]byte{255, 255}, []byte("/status/json")...)
)
func loadClusterInfo(name string) (map[string]string, error) {
if _, err := os.Stat(name); err != nil {
return nil, nil
}
f, _ := os.Open(name)
defer f.Close()
m := make(map[string]string)
// Create a new Scanner for the file.
scanner := bufio.NewScanner(f)
// Loop over all lines in the file and print them.
for scanner.Scan() {
line := scanner.Text()
fields := strings.SplitN(line, ":", 2)
key := strings.TrimSpace(fields[0])
value := strings.TrimSpace(fields[1])
m[key] = value
}
return m, nil
}
var (
journal = flag.String("journal", "", "Journal file name")
)
func createJournal(db fdb.Database, hz int, d *bench.Description, name string) *os.File {
// experiment journal is a folder that contains following information
// metadata json
// tsv rolling log(s)
// any evidence from the cluster
// we could probably store that in LMDB, but human-readable is always good
t := time.Now().UTC()
folder := *journal
meta := make(map[string]interface{})
var (
info map[string]string
data []byte
err error
)
if info, err = loadClusterInfo("/etc/cluster"); info != nil {
meta["cluster"] = info
} else {
fmt.Println("Failed to get cluster info: ", err)
}
if len(folder) == 0 {
folder = t.Format("bench-2006-01-02-15-04-05")
folder = fmt.Sprintf("%s-%s-%dhz", folder, name, hz)
if info != nil {
folder = fmt.Sprintf("%s-%s-%s", folder, info["fdb_type"], info["fdb_count"])
}
}
fmt.Println("Using folder", folder)
os.MkdirAll(folder, 0755)
var status []byte
status, err = getStatus(db)
if err != nil {
log.Fatalln("Failed to get FDB status", err)
}
statusFile := path.Join(folder, "status.json")
err = ioutil.WriteFile(statusFile, status, 0644)
if err != nil {
log.Fatalln("Failed to dump status.json", err)
}
meta["status_file"] = "status.json"
meta["args"] = os.Args[1:]
meta["main_tsv"] = "main.tsv"
meta["time"] = t.Format(time.RFC3339)
meta["bench-hz"] = hz
meta["bench-name"] = d.Name
meta["bench-setup"] = d.Setup
meta["bench-text"] = d.Explanation
meta["build"] = buildVersion
data, err = json.Marshal(meta)
if err != nil {
log.Fatalln(err)
}
err = ioutil.WriteFile(path.Join(folder, "meta.json"), data, 0644)
if err != nil {
log.Fatalln(err)
}
var (
tsvFile *os.File
)
tsvFile, err = os.OpenFile(path.Join(folder, "main.tsv"), os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalln(err)
}
return tsvFile
}
func getStatus(db fdb.Database) ([]byte, error) {
raw, err := db.ReadTransact(func(tr fdb.ReadTransaction) (interface{}, error) {
return tr.Get(fdb.Key(jsonKey)).Get()
})
if err != nil {
return nil, err
}
return raw.([]byte), nil
}