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
25 collectionBatchSize int
30 flag.StringVar(&logEventTypePrefix,
31 "log-event-type-prefix",
32 "experimental-data-manager",
33 "Prefix to use in the event_type of our arvados log entries. Set to empty to turn off logging")
34 flag.IntVar(&logFrequencySeconds,
35 "log-frequency-seconds",
37 "How frequently we'll write log entries in seconds.")
38 flag.IntVar(&minutesBetweenRuns,
39 "minutes-between-runs",
41 "How many minutes we wait between data manager runs. 0 means run once and exit.")
42 flag.IntVar(&collectionBatchSize,
43 "collection-batch-size",
45 "How many collections to request in each batch.")
49 "Perform a dry run. Log how many blocks would be deleted/moved, but do not issue any changes to keepstore.")
55 if minutesBetweenRuns == 0 {
56 arv, err := arvadosclient.MakeArvadosClient()
58 loggerutil.FatalWithMessage(arvLogger, fmt.Sprintf("Error making arvados client: %v", err))
62 loggerutil.FatalWithMessage(arvLogger, fmt.Sprintf("singlerun: %v", err))
65 waitTime := time.Minute * time.Duration(minutesBetweenRuns)
67 log.Println("Beginning Run")
68 arv, err := arvadosclient.MakeArvadosClient()
70 loggerutil.FatalWithMessage(arvLogger, fmt.Sprintf("Error making arvados client: %v", err))
74 log.Printf("singlerun: %v", err)
76 log.Printf("Sleeping for %d minutes", minutesBetweenRuns)
82 var arvLogger *logger.Logger
84 func singlerun(arv arvadosclient.ArvadosClient) error {
86 if isAdmin, err := util.UserIsAdmin(arv); err != nil {
87 return errors.New("Error verifying admin token: " + err.Error())
89 return errors.New("Current user is not an admin. Datamanager requires a privileged token.")
92 if logEventTypePrefix != "" {
93 arvLogger, err = logger.NewLogger(logger.LoggerParams{
95 EventTypePrefix: logEventTypePrefix,
96 WriteInterval: time.Second * time.Duration(logFrequencySeconds)})
99 loggerutil.LogRunInfo(arvLogger)
100 if arvLogger != nil {
101 arvLogger.AddWriteHook(loggerutil.LogMemoryAlloc)
105 dataFetcher summary.DataFetcher
106 readCollections collection.ReadCollections
107 keepServerInfo keep.ReadServers
110 if summary.ShouldReadData() {
111 dataFetcher = summary.ReadData
113 dataFetcher = BuildDataFetcher(arv)
116 err = dataFetcher(arvLogger, &readCollections, &keepServerInfo)
121 err = summary.MaybeWriteData(arvLogger, readCollections, keepServerInfo)
126 buckets := summary.BucketReplication(readCollections, keepServerInfo)
127 bucketCounts := buckets.Counts()
129 replicationSummary := buckets.SummarizeBuckets(readCollections)
130 replicationCounts := replicationSummary.ComputeCounts()
132 log.Printf("Blocks In Collections: %d, "+
133 "\nBlocks In Keep: %d.",
134 len(readCollections.BlockToDesiredReplication),
135 len(keepServerInfo.BlockToServers))
136 log.Println(replicationCounts.PrettyPrint())
138 log.Printf("Blocks Histogram:")
139 for _, rlbss := range bucketCounts {
140 log.Printf("%+v: %10d",
145 kc, err := keepclient.MakeKeepClient(&arv)
147 return fmt.Errorf("Error setting up keep client %v", err.Error())
150 // Log that we're finished. We force the recording, since go will
151 // not wait for the write timer before exiting.
152 if arvLogger != nil {
153 defer arvLogger.FinalUpdate(func(p map[string]interface{}, e map[string]interface{}) {
154 summaryInfo := logger.GetOrCreateMap(p, "summary_info")
155 summaryInfo["block_replication_counts"] = bucketCounts
156 summaryInfo["replication_summary"] = replicationCounts
157 p["summary_info"] = summaryInfo
159 p["run_info"].(map[string]interface{})["finished_at"] = time.Now()
163 pullServers := summary.ComputePullServers(kc,
165 readCollections.BlockToDesiredReplication,
166 replicationSummary.UnderReplicatedBlocks)
168 pullLists := summary.BuildPullLists(pullServers)
170 trashLists, trashErr := summary.BuildTrashLists(kc,
172 replicationSummary.KeepBlocksNotInCollections)
174 err = summary.WritePullLists(arvLogger, pullLists, dryRun)
182 keep.SendTrashLists(arvLogger, kc, trashLists, dryRun)
187 // BuildDataFetcher returns a data fetcher that fetches data from remote servers.
188 func BuildDataFetcher(arv arvadosclient.ArvadosClient) summary.DataFetcher {
190 arvLogger *logger.Logger,
191 readCollections *collection.ReadCollections,
192 keepServerInfo *keep.ReadServers,
194 collDone := make(chan struct{})
197 *readCollections, collErr = collection.GetCollectionsAndSummarize(
198 collection.GetCollectionsParams{
201 BatchSize: collectionBatchSize})
202 collDone <- struct{}{}
206 *keepServerInfo, keepErr = keep.GetKeepServersAndSummarize(
207 keep.GetKeepServersParams{
214 // Return a nil error only if both parts succeeded.