More changes in response to Peter's review.
[arvados.git] / services / datamanager / datamanager.go
index dc6a431a87175008b8ba41b62480005912c59e5c..5b45153007d4aff3b1e0305cd552758eb6f1e625 100644 (file)
@@ -9,22 +9,22 @@ import (
        "git.curoverse.com/arvados.git/sdk/go/util"
        "git.curoverse.com/arvados.git/services/datamanager/collection"
        "git.curoverse.com/arvados.git/services/datamanager/keep"
+       "git.curoverse.com/arvados.git/services/datamanager/loggerutil"
        "log"
-       "os"
        "time"
 )
 
 var (
-       logEventType string
+       logEventTypePrefix        string
        logFrequencySeconds int
 )
 
 func init() {
-       flag.StringVar(&logEventType
-               "log-event-type",
-               "experimental-data-manager-report",
-               "event_type to use in our arvados log entries. Set to empty to turn off logging")
-       flag.IntVar(&logFrequencySeconds, 
+       flag.StringVar(&logEventTypePrefix,
+               "log-event-type-prefix",
+               "experimental-data-manager",
+               "Prefix to use in the event_type of our arvados log entries. Set to empty to turn off logging")
+       flag.IntVar(&logFrequencySeconds,
                "log-frequency-seconds",
                20,
                "How frequently we'll write log entries in seconds.")
@@ -45,73 +45,39 @@ func main() {
        }
 
        var arvLogger *logger.Logger
-       if logEventType != "" {
+       if logEventTypePrefix != "" {
                arvLogger = logger.NewLogger(logger.LoggerParams{Client: arv,
-                       EventType: logEventType,
-                       MinimumWriteInterval: time.Second * time.Duration(logFrequencySeconds)})
+                       EventTypePrefix:     logEventTypePrefix,
+                       WriteInterval: time.Second * time.Duration(logFrequencySeconds)})
        }
 
+       loggerutil.LogRunInfo(arvLogger)
        if arvLogger != nil {
-               properties, _ := arvLogger.Edit()
-               runInfo := make(map[string]interface{})
-               runInfo["start_time"] = time.Now()
-               runInfo["args"] = os.Args
-               hostname, err := os.Hostname()
-               if err != nil {
-                       runInfo["hostname_error"] = err.Error()
-               } else {
-                       runInfo["hostname"] = hostname
-               }
-               runInfo["pid"] = os.Getpid()
-               properties["run_info"] = runInfo
-               arvLogger.Record()
+               arvLogger.AddWriteHook(loggerutil.LogMemoryAlloc)
        }
 
-       // TODO(misha): Read Collections and Keep Contents concurrently as goroutines.
-       // This requires waiting on them to finish before you let main() exit.
+       collectionChannel := make(chan collection.ReadCollections)
 
-       RunCollections(collection.GetCollectionsParams{
-               Client: arv, Logger: arvLogger, BatchSize: 500})
+       go func() {
+               collectionChannel <- collection.GetCollectionsAndSummarize(
+                       collection.GetCollectionsParams{
+                               Client: arv, Logger: arvLogger, BatchSize: 50})
+       }()
 
-       RunKeep(keep.GetKeepServersParams{Client: arv, Limit: 1000})
-}
-
-func RunCollections(params collection.GetCollectionsParams) {
-       readCollections := collection.GetCollections(params)
-
-       UserUsage := ComputeSizeOfOwnedCollections(readCollections)
-       log.Printf("Uuid to Size used: %v", UserUsage)
-
-       // TODO(misha): Add a "readonly" flag. If we're in readonly mode,
-       // lots of behaviors can become warnings (and obviously we can't
-       // write anything).
-       // if !readCollections.ReadAllCollections {
-       //      log.Fatalf("Did not read all collections")
-       // }
-
-       log.Printf("Read and processed %d collections",
-               len(readCollections.UuidToCollection))
-}
+       keepServerInfo := keep.GetKeepServersAndSummarize(
+               keep.GetKeepServersParams{Client: arv, Logger: arvLogger, Limit: 1000})
 
-func RunKeep(params keep.GetKeepServersParams) {
-       readServers := keep.GetKeepServers(params)
+       readCollections := <-collectionChannel
 
-       log.Printf("Returned %d keep disks", len(readServers.ServerToContents))
+       // TODO(misha): Use these together to verify replication.
+       _ = readCollections
+       _ = keepServerInfo
 
-       blockReplicationCounts := make(map[int]int)
-       for _, infos := range readServers.BlockToServers {
-               replication := len(infos)
-               blockReplicationCounts[replication] += 1
-       }
-
-       log.Printf("Replication level distribution: %v", blockReplicationCounts)
-}
-
-func ComputeSizeOfOwnedCollections(readCollections collection.ReadCollections) (
-       results map[string]int) {
-       results = make(map[string]int)
-       for _, coll := range readCollections.UuidToCollection {
-               results[coll.OwnerUuid] = results[coll.OwnerUuid] + coll.TotalSize
+       // Log that we're finished. We force the recording, since go will
+       // not wait for the timer before exiting.
+       if arvLogger != nil {
+               arvLogger.FinalUpdate(func(p map[string]interface{}, e map[string]interface{}) {
+                       p["run_info"].(map[string]interface{})["finished_at"] = time.Now()
+               })
        }
-       return
 }