1 /* Deals with parsing Collection responses from API Server. */
8 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
9 "git.curoverse.com/arvados.git/sdk/go/blockdigest"
10 "git.curoverse.com/arvados.git/sdk/go/logger"
11 "git.curoverse.com/arvados.git/sdk/go/manifest"
12 "git.curoverse.com/arvados.git/sdk/go/util"
13 "git.curoverse.com/arvados.git/services/datamanager/loggerutil"
22 heap_profile_filename string
23 // globals for debugging
24 totalManifestSize uint64
25 maxManifestSize uint64
28 type Collection struct {
32 BlockDigestToSize map[blockdigest.BlockDigest]int
36 type ReadCollections struct {
37 ReadAllCollections bool
38 UuidToCollection map[string]Collection
39 OwnerToCollectionSize map[string]int
42 type GetCollectionsParams struct {
43 Client arvadosclient.ArvadosClient
48 type SdkCollectionInfo struct {
49 Uuid string `json:"uuid"`
50 OwnerUuid string `json:"owner_uuid"`
51 Redundancy int `json:"redundancy"`
52 ModifiedAt time.Time `json:"modified_at"`
53 ManifestText string `json:"manifest_text"`
56 type SdkCollectionList struct {
57 ItemsAvailable int `json:"items_available"`
58 Items []SdkCollectionInfo `json:"items"`
62 flag.StringVar(&heap_profile_filename,
65 "File to write the heap profiles to. Leave blank to skip profiling.")
68 // Write the heap profile to a file for later review.
69 // Since a file is expected to only contain a single heap profile this
70 // function overwrites the previously written profile, so it is safe
71 // to call multiple times in a single run.
72 // Otherwise we would see cumulative numbers as explained here:
73 // https://groups.google.com/d/msg/golang-nuts/ZyHciRglQYc/2nh4Ndu2fZcJ
74 func WriteHeapProfile() {
75 if heap_profile_filename != "" {
77 heap_profile, err := os.Create(heap_profile_filename)
82 defer heap_profile.Close()
84 err = pprof.WriteHeapProfile(heap_profile)
91 func GetCollectionsAndSummarize(params GetCollectionsParams) (results ReadCollections) {
92 results = GetCollections(params)
93 ComputeSizeOfOwnedCollections(&results)
95 if params.Logger != nil {
96 params.Logger.Update(func(p map[string]interface{}, e map[string]interface{}) {
97 collectionInfo := p["collection_info"].(map[string]interface{})
98 // Since maps are shallow copied, we run a risk of concurrent
99 // updates here. By copying results.OwnerToCollectionSize into
100 // the log, we're assuming that it won't be updated.
101 collectionInfo["owner_to_collection_size"] = results.OwnerToCollectionSize
105 log.Printf("Uuid to Size used: %v", results.OwnerToCollectionSize)
106 log.Printf("Read and processed %d collections",
107 len(results.UuidToCollection))
109 // TODO(misha): Add a "readonly" flag. If we're in readonly mode,
110 // lots of behaviors can become warnings (and obviously we can't
112 // if !readCollections.ReadAllCollections {
113 // log.Fatalf("Did not read all collections")
119 func GetCollections(params GetCollectionsParams) (results ReadCollections) {
120 if ¶ms.Client == nil {
121 log.Fatalf("params.Client passed to GetCollections() should " +
122 "contain a valid ArvadosClient, but instead it is nil.")
125 fieldsWanted := []string{"manifest_text",
128 // TODO(misha): Start using the redundancy field.
132 sdkParams := arvadosclient.Dict{
133 "select": fieldsWanted,
134 "order": []string{"modified_at ASC"},
135 "filters": [][]string{[]string{"modified_at", ">=", "1900-01-01T00:00:00Z"}}}
137 if params.BatchSize > 0 {
138 sdkParams["limit"] = params.BatchSize
141 initialNumberOfCollectionsAvailable, err :=
142 util.NumberItemsAvailable(params.Client, "collections")
144 loggerutil.FatalWithMessage(params.Logger,
145 fmt.Sprintf("Error querying collection count: %v", err))
147 // Include a 1% margin for collections added while we're reading so
148 // that we don't have to grow the map in most cases.
149 maxExpectedCollections := int(
150 float64(initialNumberOfCollectionsAvailable) * 1.01)
151 results.UuidToCollection = make(map[string]Collection, maxExpectedCollections)
153 if params.Logger != nil {
154 params.Logger.Update(func(p map[string]interface{}, e map[string]interface{}) {
155 collectionInfo := make(map[string]interface{})
156 collectionInfo["num_collections_at_start"] = initialNumberOfCollectionsAvailable
157 collectionInfo["batch_size"] = params.BatchSize
158 p["collection_info"] = collectionInfo
162 // These values are just for getting the loop to run the first time,
163 // afterwards they'll be set to real values.
164 previousTotalCollections := -1
165 totalCollections := 0
166 for totalCollections > previousTotalCollections {
167 // We're still finding new collections
169 // Write the heap profile for examining memory usage
172 // Get next batch of collections.
173 var collections SdkCollectionList
174 err := params.Client.List("collections", sdkParams, &collections)
176 loggerutil.FatalWithMessage(params.Logger,
177 fmt.Sprintf("Error querying collections: %v", err))
180 // Process collection and update our date filter.
181 sdkParams["filters"].([][]string)[0][2] =
182 ProcessCollections(params.Logger,
184 results.UuidToCollection).Format(time.RFC3339)
187 previousTotalCollections = totalCollections
188 totalCollections = len(results.UuidToCollection)
190 log.Printf("%d collections read, %d new in last batch, "+
191 "%s latest modified date, %.0f %d %d avg,max,total manifest size",
193 totalCollections-previousTotalCollections,
194 sdkParams["filters"].([][]string)[0][2],
195 float32(totalManifestSize)/float32(totalCollections),
196 maxManifestSize, totalManifestSize)
198 if params.Logger != nil {
199 params.Logger.Update(func(p map[string]interface{}, e map[string]interface{}) {
200 collectionInfo := p["collection_info"].(map[string]interface{})
201 collectionInfo["collections_read"] = totalCollections
202 collectionInfo["latest_modified_date_seen"] = sdkParams["filters"].([][]string)[0][2]
203 collectionInfo["total_manifest_size"] = totalManifestSize
204 collectionInfo["max_manifest_size"] = maxManifestSize
209 // Just in case this lowers the numbers reported in the heap profile.
212 // Write the heap profile for examining memory usage
218 // StrCopy returns a newly allocated string.
219 // It is useful to copy slices so that the garbage collector can reuse
220 // the memory of the longer strings they came from.
221 func StrCopy(s string) string {
222 return string([]byte(s))
225 func ProcessCollections(arvLogger *logger.Logger,
226 receivedCollections []SdkCollectionInfo,
227 uuidToCollection map[string]Collection) (latestModificationDate time.Time) {
228 for _, sdkCollection := range receivedCollections {
229 collection := Collection{Uuid: StrCopy(sdkCollection.Uuid),
230 OwnerUuid: StrCopy(sdkCollection.OwnerUuid),
231 ReplicationLevel: sdkCollection.Redundancy,
232 BlockDigestToSize: make(map[blockdigest.BlockDigest]int)}
234 if sdkCollection.ModifiedAt.IsZero() {
235 loggerutil.FatalWithMessage(arvLogger,
237 "Arvados SDK collection returned with unexpected zero "+
238 "modifcation date. This probably means that either we failed to "+
239 "parse the modification date or the API server has changed how "+
240 "it returns modification dates: %v",
244 if sdkCollection.ModifiedAt.After(latestModificationDate) {
245 latestModificationDate = sdkCollection.ModifiedAt
247 manifest := manifest.Manifest{sdkCollection.ManifestText}
248 manifestSize := uint64(len(sdkCollection.ManifestText))
250 if _, alreadySeen := uuidToCollection[collection.Uuid]; !alreadySeen {
251 totalManifestSize += manifestSize
253 if manifestSize > maxManifestSize {
254 maxManifestSize = manifestSize
257 blockChannel := manifest.BlockIterWithDuplicates()
258 for block := range blockChannel {
259 if stored_size, stored := collection.BlockDigestToSize[block.Digest]; stored && stored_size != block.Size {
260 message := fmt.Sprintf(
261 "Collection %s contains multiple sizes (%d and %d) for block %s",
266 loggerutil.FatalWithMessage(arvLogger, message)
268 collection.BlockDigestToSize[block.Digest] = block.Size
270 collection.TotalSize = 0
271 for _, size := range collection.BlockDigestToSize {
272 collection.TotalSize += size
274 uuidToCollection[collection.Uuid] = collection
276 // Clear out all the manifest strings that we don't need anymore.
277 // These hopefully form the bulk of our memory usage.
279 sdkCollection.ManifestText = ""
285 func ComputeSizeOfOwnedCollections(readCollections *ReadCollections) {
286 readCollections.OwnerToCollectionSize = make(map[string]int)
287 for _, coll := range readCollections.UuidToCollection {
288 readCollections.OwnerToCollectionSize[coll.OwnerUuid] =
289 readCollections.OwnerToCollectionSize[coll.OwnerUuid] + coll.TotalSize