-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbs.go
337 lines (309 loc) · 9.07 KB
/
dbs.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
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"sync"
"sync/atomic"
"time"
)
const dbsUrl string = "https://cmsweb.cern.ch/dbs/prod/global/DBSReader"
// helper function to get DBS stats for total/valid number of files
func dbsStats(dataset string, verbose bool) (*DBSRecord, error) {
rec, err := dbsDatasetStats(dataset, 1, verbose)
if err != nil {
fmt.Printf("ERROR: unable to call dbsDatasetStats for %s, %v", dataset, err)
return rec, err
}
blocks, err := dbsBlocks(dataset, verbose)
if err != nil {
fmt.Printf("ERROR: unable to call dbsBlocks for %s, %v", dataset, err)
return rec, err
}
totalLumis, uniqueLumis, err := dbsBlocksLumis(blocks, verbose)
if err != nil {
fmt.Printf("ERROR: unable to call dbsBlocksLumis for %s, %v", dataset, err)
return rec, err
}
rec.TotalFileLumis = totalLumis
rec.UniqueFileLumis = uniqueLumis
totalLumis, err = dbsFilesummariesLumis(blocks, verbose)
if err != nil {
fmt.Printf("ERROR: unable to call dbsFilesummariesLumis for %s, %v", dataset, err)
return rec, err
}
rec.FilesummariesLumis = totalLumis
return rec, nil
}
// DBSRecord represents filesummaries record we need to parse
type DBSRecord struct {
NumLumis int64 `json:"num_lumi"` // output of filesummaries?dataset=xxx
NumFiles int64 `json:"num_file"` // output of filesummaries?dataset=xxx
NumEvents int64 `json:"num_event"` // output of filesummaries?dataset=xxx
NumBlocks int64 `json:"num_block"` // output of filesummaries?dataset=xxx
TotalFileLumis int64 `json:"num_file_lumis"` // output of filelumis?block_name=xxx
UniqueFileLumis int64 `json:"unique_file_lumis"` // output of filelumis?block_name=xxx
FilesummariesLumis int64 `json:"filesummaries_lumis"` // output of filesummaries?block_name=xxx
NumInvalidFiles int64 `json:"num_invalid_files"` // number of invalid files
}
// DBSBlocks represents blocks record we need to parse
type DBSBlock struct {
BlockName string `json:"block_name"`
}
// helper function to get list of blocks for a given dataset
func dbsBlocks(dataset string, verbose bool) ([]string, error) {
var blocks []string
rurl := fmt.Sprintf("%s/blocks?dataset=%s", dbsUrl, dataset)
if verbose {
log.Println("dbs call", rurl)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second*60))
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", rurl, nil)
if err != nil {
return blocks, err
}
req.Header.Add("Accept", "application/json")
client := HttpClient(verbose)
resp, err := client.Do(req)
atomic.AddUint64(&TotalURLCalls, 1)
if err != nil {
if verbose {
log.Println("dbsCall client.Do", err)
}
return blocks, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
var records []DBSBlock
err = json.Unmarshal(data, &records)
if err != nil {
if verbose {
log.Println("dbsCall json.Unmarshal", err)
}
return nil, err
}
for _, rec := range records {
if !InList(rec.BlockName, blocks) {
if rec.BlockName != "" {
blocks = append(blocks, rec.BlockName)
}
}
}
return blocks, nil
}
// RunLumi represents run-lumi object
type RunLumi struct {
Run int `json:"run_num"`
Lumi int `json:"lumi_section_num"`
}
// helper function to extract block ID from block name
func blockID(blk string) string {
arr := strings.Split(blk, "#")
if len(arr) != 2 {
log.Printf("### unable to extract block ID from '%s'", blk)
return blk
}
return arr[1]
}
// SyncMap represents map to keep track of go routines
// type SyncMap map[string]bool
type SyncMap struct {
sync.Map
}
// Len implements map size function
func (m *SyncMap) Len() int {
count := 0
m.Range(func(k, v any) bool {
count++
return true
})
return count
}
// Lumi represents part of filesummaries data structure
type Lumi struct {
NumLumi int64 `json:"num_lumi"`
}
// DbsListEntry identifies types used by list's generics function
type DbsListEntry interface {
RunLumi | Lumi
}
func dbsApiCall[T DbsListEntry](rurl, bid string, verbose bool, out *[]T) {
time0 := time.Now()
defer func() {
if verbose {
log.Printf("finished %s in %s\n", bid, time.Since(time0))
}
}()
if verbose {
log.Println("dbs call", rurl)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second*60))
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", rurl, nil)
if err != nil {
log.Println("ERROR: dbsApiCall new request", err)
return
}
req.Header.Add("Accept", "application/ndjson")
client := HttpClient(verbose)
resp, err := client.Do(req)
atomic.AddUint64(&TotalURLCalls, 1)
if err != nil {
if verbose {
log.Println("ERROR: dbsApiCall client.Do", err)
}
return
}
defer resp.Body.Close()
// we'll use json decoder to walk through our json stream (ndjson)
// see explanation about json decoder in this blog post:
// https://mottaquikarim.github.io/dev/posts/you-might-not-be-using-json.decoder-correctly-in-golang/
dec := json.NewDecoder(resp.Body)
for {
var rec T
err := dec.Decode(&rec)
if err == io.EOF {
return
}
*out = append(*out, rec)
}
}
// helper function to get unique number of lumis for given list of blocks
func dbsBlocksLumis(blocks []string, verbose bool) (int64, int64, error) {
time0 := time.Now()
var out []RunLumi
group := pool.Group()
for _, b := range blocks {
if b == "" {
continue
}
bid := blockID(b)
rurl := fmt.Sprintf("%s/filelumis?block_name=%s", dbsUrl, url.QueryEscape(b))
// usage of pool provides controlled (fixed size) environment to call DBS
// where at most we will place number of calls limited by max pool size
group.Submit(func() {
dbsApiCall(rurl, bid, verbose, &out)
})
}
group.Wait()
if verbose {
log.Printf("Make %d calls to DBS to fetch block lumis in %s\n", len(blocks), time.Since(time0))
}
totalLumis := int64(len(out))
uniqueLumis := int64(len(uniqueRunLumis(out)))
return totalLumis, uniqueLumis, nil
}
// helper function to get unique number of RunLumi records
func uniqueRunLumis(records []RunLumi) []RunLumi {
var out []RunLumi
for _, rec := range records {
found := false
for _, r := range out {
if r.Run == rec.Run && r.Lumi == rec.Lumi {
found = true
}
}
if !found {
out = append(out, rec)
}
}
return out
}
// helper function to get unique number of lumis for given list of blocks
func dbsFilesummariesLumis(blocks []string, verbose bool) (int64, error) {
time0 := time.Now()
var out []Lumi
group := pool.Group()
for _, b := range blocks {
if b == "" {
continue
}
bid := blockID(b)
rurl := fmt.Sprintf("%s/filesummaries?block_name=%s", dbsUrl, url.QueryEscape(b))
// usage of pool provides controlled (fixed size) environment to call DBS
// where at most we will place number of calls limited by max pool size
group.Submit(func() {
dbsApiCall(rurl, bid, verbose, &out)
})
}
group.Wait()
if verbose {
log.Printf("Make %d calls to DBS to fetch block lumis in %s\n", len(blocks), time.Since(time0))
}
var totalLumis int64
for _, r := range out {
totalLumis += r.NumLumi
}
return totalLumis, nil
}
// helper function to perform dbs call
func dbsDatasetStats(input string, validFileOnly int, verbose bool) (*DBSRecord, error) {
rurl := fmt.Sprintf("%s/filesummaries?dataset=%s", dbsUrl, input)
if validFileOnly == 1 {
rurl = fmt.Sprintf("%s/filesummaries?dataset=%s&validFileOnly=%d", dbsUrl, input, validFileOnly)
}
records, err := dbsCall(rurl, verbose)
if err != nil {
return nil, err
}
rec := records[0]
// find out number of valid files in given dataset
validFiles, err := dbsValidFiles(input, verbose)
if err == nil {
// the number of files includes all files (valid and invalid)
rec.NumInvalidFiles = rec.NumFiles - int64(len(validFiles))
}
return &rec, err
}
// helper function to find out number of invalid files
func dbsInvalidFiles(input string, verbose bool) ([]DBSRecord, error) {
rurl := fmt.Sprintf("%s/files?dataset=%s&validFileOnly=0", dbsUrl, input)
return dbsCall(rurl, verbose)
}
// helper function to find out number of valid files
func dbsValidFiles(input string, verbose bool) ([]DBSRecord, error) {
rurl := fmt.Sprintf("%s/files?dataset=%s&validFileOnly=1", dbsUrl, input)
return dbsCall(rurl, verbose)
}
// helper function to perform dbs call
func dbsCall(rurl string, verbose bool) ([]DBSRecord, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second*60))
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", rurl, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
client := HttpClient(verbose)
resp, err := client.Do(req)
atomic.AddUint64(&TotalURLCalls, 1)
if err != nil {
if verbose {
log.Println("dbsCall client.Do", err)
}
return nil, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
var records []DBSRecord
if err != nil {
if verbose {
log.Println("dbsCall io.ReadAll", err)
}
return nil, err
}
err = json.Unmarshal(data, &records)
if err != nil {
if verbose {
log.Println("dbsCall json.Unmarshal", err)
}
return nil, err
}
return records, err
}