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
28 flag.StringVar(&logEventTypePrefix,
29 "log-event-type-prefix",
30 "experimental-data-manager",
31 "Prefix to use in the event_type of our arvados log entries. Set to empty to turn off logging")
32 flag.IntVar(&logFrequencySeconds,
33 "log-frequency-seconds",
35 "How frequently we'll write log entries in seconds.")
36 flag.IntVar(&minutesBetweenRuns,
37 "minutes-between-runs",
39 "How many minutes we wait betwen data manager runs. 0 means run once and exit.")
44 if minutesBetweenRuns == 0 {
45 err := singlerun(makeArvadosClient())
47 log.Fatalf("singlerun: %v", err)
50 waitTime := time.Minute * time.Duration(minutesBetweenRuns)
52 log.Println("Beginning Run")
53 err := singlerun(makeArvadosClient())
55 log.Printf("singlerun: %v", err)
57 log.Printf("Sleeping for %d minutes", minutesBetweenRuns)
63 func makeArvadosClient() arvadosclient.ArvadosClient {
64 arv, err := arvadosclient.MakeArvadosClient()
66 log.Fatalf("Error setting up arvados client: %s", err)
71 func singlerun(arv arvadosclient.ArvadosClient) error {
73 if isAdmin, err := util.UserIsAdmin(arv); err != nil {
74 return errors.New("Error verifying admin token: " + err.Error())
76 return errors.New("Current user is not an admin. Datamanager requires a privileged token.")
79 var arvLogger *logger.Logger
80 if logEventTypePrefix != "" {
81 arvLogger = logger.NewLogger(logger.LoggerParams{
83 EventTypePrefix: logEventTypePrefix,
84 WriteInterval: time.Second * time.Duration(logFrequencySeconds)})
87 loggerutil.LogRunInfo(arvLogger)
89 arvLogger.AddWriteHook(loggerutil.LogMemoryAlloc)
93 dataFetcher summary.DataFetcher
94 readCollections collection.ReadCollections
95 keepServerInfo keep.ReadServers
98 if summary.ShouldReadData() {
99 dataFetcher = summary.ReadData
101 dataFetcher = BuildDataFetcher(arv)
104 dataFetcher(arvLogger, &readCollections, &keepServerInfo)
106 summary.MaybeWriteData(arvLogger, readCollections, keepServerInfo)
108 buckets := summary.BucketReplication(readCollections, keepServerInfo)
109 bucketCounts := buckets.Counts()
111 replicationSummary := buckets.SummarizeBuckets(readCollections)
112 replicationCounts := replicationSummary.ComputeCounts()
114 log.Printf("Blocks In Collections: %d, "+
115 "\nBlocks In Keep: %d.",
116 len(readCollections.BlockToDesiredReplication),
117 len(keepServerInfo.BlockToServers))
118 log.Println(replicationCounts.PrettyPrint())
120 log.Printf("Blocks Histogram:")
121 for _, rlbss := range bucketCounts {
122 log.Printf("%+v: %10d",
127 kc, err := keepclient.MakeKeepClient(&arv)
129 loggerutil.FatalWithMessage(arvLogger,
130 fmt.Sprintf("Error setting up keep client %s", err.Error()))
133 // Log that we're finished. We force the recording, since go will
134 // not wait for the write timer before exiting.
135 if arvLogger != nil {
136 defer arvLogger.FinalUpdate(func(p map[string]interface{}, e map[string]interface{}) {
137 summaryInfo := logger.GetOrCreateMap(p, "summary_info")
138 summaryInfo["block_replication_counts"] = bucketCounts
139 summaryInfo["replication_summary"] = replicationCounts
140 p["summary_info"] = summaryInfo
142 p["run_info"].(map[string]interface{})["finished_at"] = time.Now()
146 pullServers := summary.ComputePullServers(kc,
148 readCollections.BlockToDesiredReplication,
149 replicationSummary.UnderReplicatedBlocks)
151 pullLists := summary.BuildPullLists(pullServers)
153 trashLists, trashErr := summary.BuildTrashLists(kc,
155 replicationSummary.KeepBlocksNotInCollections)
157 summary.WritePullLists(arvLogger, pullLists)
162 keep.SendTrashLists(kc, trashLists)
167 // BuildDataFetcher returns a data fetcher that fetches data from remote servers.
168 func BuildDataFetcher(arv arvadosclient.ArvadosClient) summary.DataFetcher {
169 return func(arvLogger *logger.Logger,
170 readCollections *collection.ReadCollections,
171 keepServerInfo *keep.ReadServers) {
172 collectionChannel := make(chan collection.ReadCollections)
175 collectionChannel <- collection.GetCollectionsAndSummarize(
176 collection.GetCollectionsParams{
182 *keepServerInfo = keep.GetKeepServersAndSummarize(
183 keep.GetKeepServersParams{
188 *readCollections = <-collectionChannel