1 // Handles writing data to and reading data from disk to speed up development.
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"
17 // Used to locally cache data read from servers to reduce execution
18 // time when developing. Not for use in production.
19 type serializedData struct {
20 ReadCollections collection.ReadCollections
21 KeepServerInfo keep.ReadServers
30 flag.StringVar(&writeDataTo,
33 "Write summary of data received to this file. Used for development only.")
34 flag.StringVar(&readDataFrom,
37 "Avoid network i/o and read summary data from this file instead. Used for development only.")
40 // Writes data we've read to a file.
42 // This is useful for development, so that we don't need to read all
43 // our data from the network every time we tweak something.
45 // This should not be used outside of development, since you'll be
46 // working with stale data.
47 func MaybeWriteData(arvLogger *logger.Logger,
48 readCollections collection.ReadCollections,
49 keepServerInfo keep.ReadServers) bool {
50 if writeDataTo == "" {
53 summaryFile, err := os.Create(writeDataTo)
55 loggerutil.FatalWithMessage(arvLogger,
56 fmt.Sprintf("Failed to open %s: %v", writeDataTo, err))
58 defer summaryFile.Close()
60 enc := gob.NewEncoder(summaryFile)
61 data := serializedData{
62 ReadCollections: readCollections,
63 KeepServerInfo: keepServerInfo}
64 err = enc.Encode(data)
66 loggerutil.FatalWithMessage(arvLogger,
67 fmt.Sprintf("Failed to write summary data: %v", err))
69 log.Printf("Wrote summary data to: %s", writeDataTo)
74 // Reads data that we've written to a file.
76 // This is useful for development, so that we don't need to read all
77 // our data from the network every time we tweak something.
79 // This should not be used outside of development, since you'll be
80 // working with stale data.
81 func MaybeReadData(arvLogger *logger.Logger,
82 readCollections *collection.ReadCollections,
83 keepServerInfo *keep.ReadServers) bool {
84 if readDataFrom == "" {
87 summaryFile, err := os.Open(readDataFrom)
89 loggerutil.FatalWithMessage(arvLogger,
90 fmt.Sprintf("Failed to open %s: %v", readDataFrom, err))
92 defer summaryFile.Close()
94 dec := gob.NewDecoder(summaryFile)
95 data := serializedData{}
96 err = dec.Decode(&data)
98 loggerutil.FatalWithMessage(arvLogger,
99 fmt.Sprintf("Failed to read summary data: %v", err))
102 // re-summarize data, so that we can update our summarizing
103 // functions without needing to do all our network i/o
104 data.ReadCollections.Summarize(arvLogger)
105 data.KeepServerInfo.Summarize(arvLogger)
107 *readCollections = data.ReadCollections
108 *keepServerInfo = data.KeepServerInfo
109 log.Printf("Read summary data from: %s", readDataFrom)