1 /* Keep Datamanager. Responsible for checking on and reporting on Keep Storage */
9 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
10 "git.curoverse.com/arvados.git/sdk/go/keepclient"
11 "git.curoverse.com/arvados.git/sdk/go/logger"
12 "git.curoverse.com/arvados.git/sdk/go/util"
13 "git.curoverse.com/arvados.git/services/datamanager/collection"
14 "git.curoverse.com/arvados.git/services/datamanager/keep"
15 "git.curoverse.com/arvados.git/services/datamanager/loggerutil"
16 "git.curoverse.com/arvados.git/services/datamanager/summary"
22 logEventTypePrefix string
23 logFrequencySeconds int
24 minutesBetweenRuns int
29 flag.StringVar(&logEventTypePrefix,
30 "log-event-type-prefix",
31 "experimental-data-manager",
32 "Prefix to use in the event_type of our arvados log entries. Set to empty to turn off logging")
33 flag.IntVar(&logFrequencySeconds,
34 "log-frequency-seconds",
36 "How frequently we'll write log entries in seconds.")
37 flag.IntVar(&minutesBetweenRuns,
38 "minutes-between-runs",
40 "How many minutes we wait between data manager runs. 0 means run once and exit.")
44 "Perform a dry run. Log how many blocks would be deleted/moved, but do not issue any changes to keepstore.")
50 if minutesBetweenRuns == 0 {
51 arv, err := arvadosclient.MakeArvadosClient()
53 loggerutil.FatalWithMessage(arvLogger, fmt.Sprintf("Error making arvados client: %v", err))
57 loggerutil.FatalWithMessage(arvLogger, fmt.Sprintf("singlerun: %v", err))
60 waitTime := time.Minute * time.Duration(minutesBetweenRuns)
62 log.Println("Beginning Run")
63 arv, err := arvadosclient.MakeArvadosClient()
65 loggerutil.FatalWithMessage(arvLogger, fmt.Sprintf("Error making arvados client: %v", err))
69 log.Printf("singlerun: %v", err)
71 log.Printf("Sleeping for %d minutes", minutesBetweenRuns)
77 var arvLogger *logger.Logger
79 func singlerun(arv arvadosclient.ArvadosClient) error {
81 if isAdmin, err := util.UserIsAdmin(arv); err != nil {
82 return errors.New("Error verifying admin token: " + err.Error())
84 return errors.New("Current user is not an admin. Datamanager requires a privileged token.")
87 if logEventTypePrefix != "" {
88 arvLogger, err = logger.NewLogger(logger.LoggerParams{
90 EventTypePrefix: logEventTypePrefix,
91 WriteInterval: time.Second * time.Duration(logFrequencySeconds)})
94 loggerutil.LogRunInfo(arvLogger)
96 arvLogger.AddWriteHook(loggerutil.LogMemoryAlloc)
100 dataFetcher summary.DataFetcher
101 readCollections collection.ReadCollections
102 keepServerInfo keep.ReadServers
105 if summary.ShouldReadData() {
106 dataFetcher = summary.ReadData
108 dataFetcher = BuildDataFetcher(arv)
111 dataFetcher(arvLogger, &readCollections, &keepServerInfo)
113 if readCollections.Err != nil {
114 return readCollections.Err
117 err = summary.MaybeWriteData(arvLogger, readCollections, keepServerInfo)
122 buckets := summary.BucketReplication(readCollections, keepServerInfo)
123 bucketCounts := buckets.Counts()
125 replicationSummary := buckets.SummarizeBuckets(readCollections)
126 replicationCounts := replicationSummary.ComputeCounts()
128 log.Printf("Blocks In Collections: %d, "+
129 "\nBlocks In Keep: %d.",
130 len(readCollections.BlockToDesiredReplication),
131 len(keepServerInfo.BlockToServers))
132 log.Println(replicationCounts.PrettyPrint())
134 log.Printf("Blocks Histogram:")
135 for _, rlbss := range bucketCounts {
136 log.Printf("%+v: %10d",
141 kc, err := keepclient.MakeKeepClient(&arv)
143 return fmt.Errorf("Error setting up keep client %v", err.Error())
146 // Log that we're finished. We force the recording, since go will
147 // not wait for the write timer before exiting.
148 if arvLogger != nil {
149 defer arvLogger.FinalUpdate(func(p map[string]interface{}, e map[string]interface{}) {
150 summaryInfo := logger.GetOrCreateMap(p, "summary_info")
151 summaryInfo["block_replication_counts"] = bucketCounts
152 summaryInfo["replication_summary"] = replicationCounts
153 p["summary_info"] = summaryInfo
155 p["run_info"].(map[string]interface{})["finished_at"] = time.Now()
159 pullServers := summary.ComputePullServers(kc,
161 readCollections.BlockToDesiredReplication,
162 replicationSummary.UnderReplicatedBlocks)
164 pullLists := summary.BuildPullLists(pullServers)
166 trashLists, trashErr := summary.BuildTrashLists(kc,
168 replicationSummary.KeepBlocksNotInCollections)
170 err = summary.WritePullLists(arvLogger, pullLists, dryRun)
178 keep.SendTrashLists(arvLogger, kc, trashLists, dryRun)
183 // BuildDataFetcher returns a data fetcher that fetches data from remote servers.
184 func BuildDataFetcher(arv arvadosclient.ArvadosClient) summary.DataFetcher {
185 return func(arvLogger *logger.Logger,
186 readCollections *collection.ReadCollections,
187 keepServerInfo *keep.ReadServers) {
188 collectionChannel := make(chan collection.ReadCollections)
191 collectionChannel <- collection.GetCollectionsAndSummarize(
192 collection.GetCollectionsParams{
199 *keepServerInfo, err = keep.GetKeepServersAndSummarize(
200 keep.GetKeepServersParams{
209 *readCollections = <-collectionChannel