1 /* Keep Datamanager. Responsible for checking on and reporting on Keep Storage */
8 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
9 "git.curoverse.com/arvados.git/sdk/go/keepclient"
10 "git.curoverse.com/arvados.git/sdk/go/logger"
11 "git.curoverse.com/arvados.git/sdk/go/util"
12 "git.curoverse.com/arvados.git/services/datamanager/collection"
13 "git.curoverse.com/arvados.git/services/datamanager/keep"
14 "git.curoverse.com/arvados.git/services/datamanager/loggerutil"
15 "git.curoverse.com/arvados.git/services/datamanager/summary"
21 logEventTypePrefix string
22 logFrequencySeconds int
23 minutesBetweenRuns int
27 flag.StringVar(&logEventTypePrefix,
28 "log-event-type-prefix",
29 "experimental-data-manager",
30 "Prefix to use in the event_type of our arvados log entries. Set to empty to turn off logging")
31 flag.IntVar(&logFrequencySeconds,
32 "log-frequency-seconds",
34 "How frequently we'll write log entries in seconds.")
35 flag.IntVar(&minutesBetweenRuns,
36 "minutes-between-runs",
38 "How many minutes we wait betwen data manager runs. 0 means run once and exit.")
43 if minutesBetweenRuns == 0 {
46 log.Fatalf("Got an error: %v", err)
49 waitTime := time.Minute * time.Duration(minutesBetweenRuns)
51 log.Println("Beginning Run")
54 log.Printf("Got an error: %v", err)
56 log.Printf("Sleeping for %d minutes", minutesBetweenRuns)
62 func singlerun() error {
63 arv, err := arvadosclient.MakeArvadosClient()
65 log.Fatalf("Error setting up arvados client %s", err.Error())
68 if is_admin, err := util.UserIsAdmin(arv); err != nil {
69 log.Fatalf("Error querying current arvados user %s", err.Error())
71 log.Fatalf("Current user is not an admin. Datamanager can only be run by admins.")
74 var arvLogger *logger.Logger
75 if logEventTypePrefix != "" {
76 arvLogger = logger.NewLogger(logger.LoggerParams{
78 EventTypePrefix: logEventTypePrefix,
79 WriteInterval: time.Second * time.Duration(logFrequencySeconds)})
82 loggerutil.LogRunInfo(arvLogger)
84 arvLogger.AddWriteHook(loggerutil.LogMemoryAlloc)
88 dataFetcher summary.DataFetcher
89 readCollections collection.ReadCollections
90 keepServerInfo keep.ReadServers
93 if summary.ShouldReadData() {
94 dataFetcher = summary.ReadData
96 dataFetcher = BuildDataFetcher(arv)
99 dataFetcher(arvLogger, &readCollections, &keepServerInfo)
101 summary.MaybeWriteData(arvLogger, readCollections, keepServerInfo)
103 buckets := summary.BucketReplication(readCollections, keepServerInfo)
104 bucketCounts := buckets.Counts()
106 replicationSummary := buckets.SummarizeBuckets(readCollections)
107 replicationCounts := replicationSummary.ComputeCounts()
109 log.Printf("Blocks In Collections: %d, "+
110 "\nBlocks In Keep: %d.",
111 len(readCollections.BlockToDesiredReplication),
112 len(keepServerInfo.BlockToServers))
113 log.Println(replicationCounts.PrettyPrint())
115 log.Printf("Blocks Histogram:")
116 for _, rlbss := range bucketCounts {
117 log.Printf("%+v: %10d",
122 kc, err := keepclient.MakeKeepClient(&arv)
124 loggerutil.FatalWithMessage(arvLogger,
125 fmt.Sprintf("Error setting up keep client %s", err.Error()))
128 // Log that we're finished. We force the recording, since go will
129 // not wait for the write timer before exiting.
130 if arvLogger != nil {
131 defer arvLogger.FinalUpdate(func(p map[string]interface{}, e map[string]interface{}) {
132 summaryInfo := logger.GetOrCreateMap(p, "summary_info")
133 summaryInfo["block_replication_counts"] = bucketCounts
134 summaryInfo["replication_summary"] = replicationCounts
135 p["summary_info"] = summaryInfo
137 p["run_info"].(map[string]interface{})["finished_at"] = time.Now()
141 pullServers := summary.ComputePullServers(kc,
143 readCollections.BlockToDesiredReplication,
144 replicationSummary.UnderReplicatedBlocks)
146 pullLists := summary.BuildPullLists(pullServers)
148 trashLists, trashErr := summary.BuildTrashLists(kc,
150 replicationSummary.KeepBlocksNotInCollections)
152 summary.WritePullLists(arvLogger, pullLists)
157 keep.SendTrashLists(keep.GetDataManagerToken(arvLogger), kc, trashLists)
163 // Returns a data fetcher that fetches data from remote servers.
164 func BuildDataFetcher(arv arvadosclient.ArvadosClient) summary.DataFetcher {
165 return func(arvLogger *logger.Logger,
166 readCollections *collection.ReadCollections,
167 keepServerInfo *keep.ReadServers) {
168 collectionChannel := make(chan collection.ReadCollections)
171 collectionChannel <- collection.GetCollectionsAndSummarize(
172 collection.GetCollectionsParams{
178 *keepServerInfo = keep.GetKeepServersAndSummarize(
179 keep.GetKeepServersParams{
184 *readCollections = <-collectionChannel