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
29 // DataFetcher to fetch data from keep servers
30 type DataFetcher func(arvLogger *logger.Logger,
31 readCollections *collection.ReadCollections,
32 keepServerInfo *keep.ReadServers)
35 flag.StringVar(&writeDataTo,
38 "Write summary of data received to this file. Used for development only.")
39 flag.StringVar(&readDataFrom,
42 "Avoid network i/o and read summary data from this file instead. Used for development only.")
45 // MaybeWriteData writes data we've read to a file.
47 // This is useful for development, so that we don't need to read all
48 // our data from the network every time we tweak something.
50 // This should not be used outside of development, since you'll be
51 // working with stale data.
52 func MaybeWriteData(arvLogger *logger.Logger,
53 readCollections collection.ReadCollections,
54 keepServerInfo keep.ReadServers) bool {
55 if writeDataTo == "" {
58 summaryFile, err := os.Create(writeDataTo)
60 loggerutil.FatalWithMessage(arvLogger,
61 fmt.Sprintf("Failed to open %s: %v", writeDataTo, err))
63 defer summaryFile.Close()
65 enc := gob.NewEncoder(summaryFile)
66 data := serializedData{
67 ReadCollections: readCollections,
68 KeepServerInfo: keepServerInfo}
69 err = enc.Encode(data)
71 loggerutil.FatalWithMessage(arvLogger,
72 fmt.Sprintf("Failed to write summary data: %v", err))
74 log.Printf("Wrote summary data to: %s", writeDataTo)
78 // ShouldReadData should not be used outside of development
79 func ShouldReadData() bool {
80 return readDataFrom != ""
83 // ReadData reads data that we've written to a file.
85 // This is useful for development, so that we don't need to read all
86 // our data from the network every time we tweak something.
88 // This should not be used outside of development, since you'll be
89 // working with stale data.
90 func ReadData(arvLogger *logger.Logger,
91 readCollections *collection.ReadCollections,
92 keepServerInfo *keep.ReadServers) {
93 if readDataFrom == "" {
94 loggerutil.FatalWithMessage(arvLogger,
95 "ReadData() called with empty filename.")
97 summaryFile, err := os.Open(readDataFrom)
99 loggerutil.FatalWithMessage(arvLogger,
100 fmt.Sprintf("Failed to open %s: %v", readDataFrom, err))
102 defer summaryFile.Close()
104 dec := gob.NewDecoder(summaryFile)
105 data := serializedData{}
106 err = dec.Decode(&data)
108 loggerutil.FatalWithMessage(arvLogger,
109 fmt.Sprintf("Failed to read summary data: %v", err))
112 // re-summarize data, so that we can update our summarizing
113 // functions without needing to do all our network i/o
114 data.ReadCollections.Summarize(arvLogger)
115 data.KeepServerInfo.Summarize(arvLogger)
117 *readCollections = data.ReadCollections
118 *keepServerInfo = data.KeepServerInfo
119 log.Printf("Read summary data from: %s", readDataFrom)