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) error {
55 if WriteDataTo == "" {
58 summaryFile, err := os.Create(WriteDataTo)
62 defer summaryFile.Close()
64 enc := gob.NewEncoder(summaryFile)
65 data := serializedData{
66 ReadCollections: readCollections,
67 KeepServerInfo: keepServerInfo}
68 err = enc.Encode(data)
72 log.Printf("Wrote summary data to: %s", WriteDataTo)
76 // ShouldReadData should not be used outside of development
77 func ShouldReadData() bool {
78 return readDataFrom != ""
81 // ReadData reads data that we've written to a file.
83 // This is useful for development, so that we don't need to read all
84 // our data from the network every time we tweak something.
86 // This should not be used outside of development, since you'll be
87 // working with stale data.
88 func ReadData(arvLogger *logger.Logger,
89 readCollections *collection.ReadCollections,
90 keepServerInfo *keep.ReadServers) {
91 if readDataFrom == "" {
92 loggerutil.FatalWithMessage(arvLogger,
93 "ReadData() called with empty filename.")
95 summaryFile, err := os.Open(readDataFrom)
97 loggerutil.FatalWithMessage(arvLogger,
98 fmt.Sprintf("Failed to open %s: %v", readDataFrom, err))
100 defer summaryFile.Close()
102 dec := gob.NewDecoder(summaryFile)
103 data := serializedData{}
104 err = dec.Decode(&data)
106 loggerutil.FatalWithMessage(arvLogger,
107 fmt.Sprintf("Failed to read summary data: %v", err))
110 // re-summarize data, so that we can update our summarizing
111 // functions without needing to do all our network i/o
112 data.ReadCollections.Summarize(arvLogger)
113 data.KeepServerInfo.Summarize(arvLogger)
115 *readCollections = data.ReadCollections
116 *keepServerInfo = data.KeepServerInfo
117 log.Printf("Read summary data from: %s", readDataFrom)