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"
20 HeapProfileFilename string
21 // globals for debugging
22 totalManifestSize uint64
23 maxManifestSize uint64
26 // Collection representation
27 type Collection struct {
31 BlockDigestToSize map[blockdigest.BlockDigest]int
35 // ReadCollections holds information about collections from API server
36 type ReadCollections struct {
37 ReadAllCollections bool
38 UUIDToCollection map[string]Collection
39 OwnerToCollectionSize map[string]int
40 BlockToDesiredReplication map[blockdigest.DigestWithSize]int
41 CollectionUUIDToIndex map[string]int
42 CollectionIndexToUUID []string
43 BlockToCollectionIndices map[blockdigest.DigestWithSize][]int
47 // GetCollectionsParams params
48 type GetCollectionsParams struct {
49 Client arvadosclient.ArvadosClient
54 // SdkCollectionInfo holds collection info from api
55 type SdkCollectionInfo struct {
56 UUID string `json:"uuid"`
57 OwnerUUID string `json:"owner_uuid"`
58 Redundancy int `json:"redundancy"`
59 ModifiedAt time.Time `json:"modified_at"`
60 ManifestText string `json:"manifest_text"`
63 // SdkCollectionList lists collections from api
64 type SdkCollectionList struct {
65 ItemsAvailable int `json:"items_available"`
66 Items []SdkCollectionInfo `json:"items"`
70 flag.StringVar(&HeapProfileFilename,
73 "File to write the heap profiles to. Leave blank to skip profiling.")
76 // WriteHeapProfile writes the heap profile to a file for later review.
77 // Since a file is expected to only contain a single heap profile this
78 // function overwrites the previously written profile, so it is safe
79 // to call multiple times in a single run.
80 // Otherwise we would see cumulative numbers as explained here:
81 // https://groups.google.com/d/msg/golang-nuts/ZyHciRglQYc/2nh4Ndu2fZcJ
82 func WriteHeapProfile() error {
83 if HeapProfileFilename != "" {
84 heapProfile, err := os.Create(HeapProfileFilename)
89 defer heapProfile.Close()
91 err = pprof.WriteHeapProfile(heapProfile)
98 // GetCollectionsAndSummarize gets collections from api and summarizes
99 func GetCollectionsAndSummarize(params GetCollectionsParams) (results ReadCollections) {
100 results, err := GetCollections(params)
106 results.Summarize(params.Logger)
108 log.Printf("Uuid to Size used: %v", results.OwnerToCollectionSize)
109 log.Printf("Read and processed %d collections",
110 len(results.UUIDToCollection))
112 // TODO(misha): Add a "readonly" flag. If we're in readonly mode,
113 // lots of behaviors can become warnings (and obviously we can't
115 // if !readCollections.ReadAllCollections {
116 // log.Fatalf("Did not read all collections")
122 // GetCollections gets collections from api
123 func GetCollections(params GetCollectionsParams) (results ReadCollections, err error) {
124 if ¶ms.Client == nil {
125 err = fmt.Errorf("params.Client passed to GetCollections() should " +
126 "contain a valid ArvadosClient, but instead it is nil.")
130 fieldsWanted := []string{"manifest_text",
136 sdkParams := arvadosclient.Dict{
137 "select": fieldsWanted,
138 "order": []string{"modified_at ASC"},
139 "filters": [][]string{[]string{"modified_at", ">=", "1900-01-01T00:00:00Z"}}}
141 if params.BatchSize > 0 {
142 sdkParams["limit"] = params.BatchSize
145 var defaultReplicationLevel int
147 var value interface{}
148 value, err = params.Client.Discovery("defaultCollectionReplication")
153 defaultReplicationLevel = int(value.(float64))
154 if defaultReplicationLevel <= 0 {
155 err = fmt.Errorf("Default collection replication returned by arvados SDK "+
156 "should be a positive integer but instead it was %d.",
157 defaultReplicationLevel)
162 initialNumberOfCollectionsAvailable, err :=
163 util.NumberItemsAvailable(params.Client, "collections")
167 // Include a 1% margin for collections added while we're reading so
168 // that we don't have to grow the map in most cases.
169 maxExpectedCollections := int(
170 float64(initialNumberOfCollectionsAvailable) * 1.01)
171 results.UUIDToCollection = make(map[string]Collection, maxExpectedCollections)
173 if params.Logger != nil {
174 params.Logger.Update(func(p map[string]interface{}, e map[string]interface{}) {
175 collectionInfo := logger.GetOrCreateMap(p, "collection_info")
176 collectionInfo["num_collections_at_start"] = initialNumberOfCollectionsAvailable
177 collectionInfo["batch_size"] = params.BatchSize
178 collectionInfo["default_replication_level"] = defaultReplicationLevel
182 // These values are just for getting the loop to run the first time,
183 // afterwards they'll be set to real values.
184 previousTotalCollections := -1
185 totalCollections := 0
186 for totalCollections > previousTotalCollections {
187 // We're still finding new collections
189 // Write the heap profile for examining memory usage
190 err = WriteHeapProfile()
195 // Get next batch of collections.
196 var collections SdkCollectionList
197 err = params.Client.List("collections", sdkParams, &collections)
202 // Process collection and update our date filter.
203 var latestModificationDate time.Time
204 latestModificationDate, err = ProcessCollections(params.Logger,
206 defaultReplicationLevel,
207 results.UUIDToCollection)
211 sdkParams["filters"].([][]string)[0][2] = latestModificationDate.Format(time.RFC3339)
214 previousTotalCollections = totalCollections
215 totalCollections = len(results.UUIDToCollection)
217 log.Printf("%d collections read, %d new in last batch, "+
218 "%s latest modified date, %.0f %d %d avg,max,total manifest size",
220 totalCollections-previousTotalCollections,
221 sdkParams["filters"].([][]string)[0][2],
222 float32(totalManifestSize)/float32(totalCollections),
223 maxManifestSize, totalManifestSize)
225 if params.Logger != nil {
226 params.Logger.Update(func(p map[string]interface{}, e map[string]interface{}) {
227 collectionInfo := logger.GetOrCreateMap(p, "collection_info")
228 collectionInfo["collections_read"] = totalCollections
229 collectionInfo["latest_modified_date_seen"] = sdkParams["filters"].([][]string)[0][2]
230 collectionInfo["total_manifest_size"] = totalManifestSize
231 collectionInfo["max_manifest_size"] = maxManifestSize
236 // Write the heap profile for examining memory usage
237 err = WriteHeapProfile()
242 // StrCopy returns a newly allocated string.
243 // It is useful to copy slices so that the garbage collector can reuse
244 // the memory of the longer strings they came from.
245 func StrCopy(s string) string {
246 return string([]byte(s))
249 // ProcessCollections read from api server
250 func ProcessCollections(arvLogger *logger.Logger,
251 receivedCollections []SdkCollectionInfo,
252 defaultReplicationLevel int,
253 UUIDToCollection map[string]Collection) (latestModificationDate time.Time, err error) {
254 for _, sdkCollection := range receivedCollections {
255 collection := Collection{UUID: StrCopy(sdkCollection.UUID),
256 OwnerUUID: StrCopy(sdkCollection.OwnerUUID),
257 ReplicationLevel: sdkCollection.Redundancy,
258 BlockDigestToSize: make(map[blockdigest.BlockDigest]int)}
260 if sdkCollection.ModifiedAt.IsZero() {
261 return latestModificationDate, fmt.Errorf(
262 "Arvados SDK collection returned with unexpected zero "+
263 "modification date. This probably means that either we failed to "+
264 "parse the modification date or the API server has changed how "+
265 "it returns modification dates: %+v",
269 if sdkCollection.ModifiedAt.After(latestModificationDate) {
270 latestModificationDate = sdkCollection.ModifiedAt
273 if collection.ReplicationLevel == 0 {
274 collection.ReplicationLevel = defaultReplicationLevel
277 manifest := manifest.Manifest{Text: sdkCollection.ManifestText}
278 manifestSize := uint64(len(sdkCollection.ManifestText))
280 if _, alreadySeen := UUIDToCollection[collection.UUID]; !alreadySeen {
281 totalManifestSize += manifestSize
283 if manifestSize > maxManifestSize {
284 maxManifestSize = manifestSize
287 blockChannel := manifest.BlockIterWithDuplicates()
288 for block := range blockChannel {
289 if storedSize, stored := collection.BlockDigestToSize[block.Digest]; stored && storedSize != block.Size {
291 "Collection %s contains multiple sizes (%d and %d) for block %s",
297 collection.BlockDigestToSize[block.Digest] = block.Size
299 if manifest.Err != nil {
304 collection.TotalSize = 0
305 for _, size := range collection.BlockDigestToSize {
306 collection.TotalSize += size
308 UUIDToCollection[collection.UUID] = collection
310 // Clear out all the manifest strings that we don't need anymore.
311 // These hopefully form the bulk of our memory usage.
313 sdkCollection.ManifestText = ""
319 // Summarize the collections read
320 func (readCollections *ReadCollections) Summarize(arvLogger *logger.Logger) {
321 readCollections.OwnerToCollectionSize = make(map[string]int)
322 readCollections.BlockToDesiredReplication = make(map[blockdigest.DigestWithSize]int)
323 numCollections := len(readCollections.UUIDToCollection)
324 readCollections.CollectionUUIDToIndex = make(map[string]int, numCollections)
325 readCollections.CollectionIndexToUUID = make([]string, 0, numCollections)
326 readCollections.BlockToCollectionIndices = make(map[blockdigest.DigestWithSize][]int)
328 for _, coll := range readCollections.UUIDToCollection {
329 collectionIndex := len(readCollections.CollectionIndexToUUID)
330 readCollections.CollectionIndexToUUID =
331 append(readCollections.CollectionIndexToUUID, coll.UUID)
332 readCollections.CollectionUUIDToIndex[coll.UUID] = collectionIndex
334 readCollections.OwnerToCollectionSize[coll.OwnerUUID] =
335 readCollections.OwnerToCollectionSize[coll.OwnerUUID] + coll.TotalSize
337 for block, size := range coll.BlockDigestToSize {
338 locator := blockdigest.DigestWithSize{Digest: block, Size: uint32(size)}
339 readCollections.BlockToCollectionIndices[locator] =
340 append(readCollections.BlockToCollectionIndices[locator],
342 storedReplication := readCollections.BlockToDesiredReplication[locator]
343 if coll.ReplicationLevel > storedReplication {
344 readCollections.BlockToDesiredReplication[locator] =
345 coll.ReplicationLevel
350 if arvLogger != nil {
351 arvLogger.Update(func(p map[string]interface{}, e map[string]interface{}) {
352 collectionInfo := logger.GetOrCreateMap(p, "collection_info")
353 // Since maps are shallow copied, we run a risk of concurrent
354 // updates here. By copying results.OwnerToCollectionSize into
355 // the log, we're assuming that it won't be updated.
356 collectionInfo["owner_to_collection_size"] =
357 readCollections.OwnerToCollectionSize
358 collectionInfo["distinct_blocks_named"] =
359 len(readCollections.BlockToDesiredReplication)