Added flags to write network data and then read it back. This is useful to speed...
[arvados.git] / services / datamanager / summary / summary.go
1 /* Computes Summary based on data read from API server. */
2
3 package summary
4
5 import (
6         "encoding/gob"
7         "flag"
8         "fmt"
9         "git.curoverse.com/arvados.git/sdk/go/logger"
10         "git.curoverse.com/arvados.git/services/datamanager/collection"
11         "git.curoverse.com/arvados.git/services/datamanager/keep"
12         "git.curoverse.com/arvados.git/services/datamanager/loggerutil"
13         "log"
14         "os"
15 )
16
17 var (
18         // These are just used for development, to save network i/o
19         writeDataTo  string
20         readDataFrom string
21 )
22
23 type serializedData struct {
24         ReadCollections collection.ReadCollections
25         KeepServerInfo  keep.ReadServers
26 }
27
28 func init() {
29         flag.StringVar(&writeDataTo,
30                 "write-data-to",
31                 "",
32                 "Write summary of data received to this file. Used for development only.")
33         flag.StringVar(&readDataFrom,
34                 "read-data-from",
35                 "",
36                 "Avoid network i/o and read summary data from this file instead. Used for development only.")
37 }
38
39 // Writes data we've read to a file.
40 //
41 // This is useful for development, so that we don't need to read all our data from the network every time we tweak something.
42 //
43 // This should not be used outside of development, since you'll be
44 // working with stale data.
45 func MaybeWriteData(arvLogger *logger.Logger,
46         readCollections collection.ReadCollections,
47         keepServerInfo keep.ReadServers) bool {
48         if writeDataTo == "" {
49                 return false
50         } else {
51                 summaryFile, err := os.Create(writeDataTo)
52                 if err != nil {
53                         loggerutil.FatalWithMessage(arvLogger,
54                                 fmt.Sprintf("Failed to open %s: %v", writeDataTo, err))
55                 }
56                 defer summaryFile.Close()
57
58                 enc := gob.NewEncoder(summaryFile)
59                 data := serializedData{
60                         ReadCollections: readCollections,
61                         KeepServerInfo:  keepServerInfo}
62                 err = enc.Encode(data)
63                 if err != nil {
64                         loggerutil.FatalWithMessage(arvLogger,
65                                 fmt.Sprintf("Failed to write summary data: %v", err))
66                 }
67                 log.Printf("Wrote summary data to: %s", writeDataTo)
68                 return true
69         }
70 }
71
72 // Reads data that we've read to a file.
73 //
74 // This is useful for development, so that we don't need to read all our data from the network every time we tweak something.
75 //
76 // This should not be used outside of development, since you'll be
77 // working with stale data.
78 func MaybeReadData(arvLogger *logger.Logger,
79         readCollections *collection.ReadCollections,
80         keepServerInfo *keep.ReadServers) bool {
81         if readDataFrom == "" {
82                 return false
83         } else {
84                 summaryFile, err := os.Open(readDataFrom)
85                 if err != nil {
86                         loggerutil.FatalWithMessage(arvLogger,
87                                 fmt.Sprintf("Failed to open %s: %v", readDataFrom, err))
88                 }
89                 defer summaryFile.Close()
90
91                 dec := gob.NewDecoder(summaryFile)
92                 data := serializedData{}
93                 err = dec.Decode(&data)
94                 if err != nil {
95                         loggerutil.FatalWithMessage(arvLogger,
96                                 fmt.Sprintf("Failed to read summary data: %v", err))
97                 }
98                 log.Printf("Read summary data from: %s", readDataFrom)
99                 return true
100         }
101 }