|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dataflux |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "cloud.google.com/go/storage" |
| 23 | + "golang.org/x/sync/errgroup" |
| 24 | + "google.golang.org/api/iterator" |
| 25 | +) |
| 26 | + |
| 27 | +// listingMethod represents the method of listing. |
| 28 | +type listingMethod int |
| 29 | + |
| 30 | +const ( |
| 31 | + // open when any method can be used to list. |
| 32 | + open listingMethod = iota |
| 33 | + // sequential when the listing is done sequentially. |
| 34 | + sequential |
| 35 | + // worksteal when the listing is done using work stealing algorithm. |
| 36 | + worksteal |
| 37 | +) |
| 38 | + |
| 39 | +// ListerInput contains options for listing objects. |
| 40 | +type ListerInput struct { |
| 41 | + // BucketName is the name of the bucket to list objects from. Required. |
| 42 | + BucketName string |
| 43 | + |
| 44 | + // Parallelism is number of parallel workers to use for listing. Default value is 10x number of available CPU. Optional. |
| 45 | + Parallelism int |
| 46 | + |
| 47 | + // BatchSize is the number of objects to list. Default value returns all objects at once. Optional. |
| 48 | + // The number of objects returned will be rounded up to a multiple of gcs page size. |
| 49 | + BatchSize int |
| 50 | + |
| 51 | + // Query is the query to filter objects for listing. Default value is nil. Optional. |
| 52 | + //Use ProjectionNoACL For faster listing. ACL is expensive and this results in fewer objects |
| 53 | + // to be returned from GCS in each API call. |
| 54 | + Query storage.Query |
| 55 | + |
| 56 | + // SkipDirectoryObjects is to indicate whether to list directory objects. Default value is false. Optional. |
| 57 | + SkipDirectoryObjects bool |
| 58 | +} |
| 59 | + |
| 60 | +// Lister is used for interacting with Dataflux fast-listing. |
| 61 | +// The caller should initialize it with NewLister() instead of creating it directly. |
| 62 | +type Lister struct { |
| 63 | + // method indicates the listing method(open, sequential, worksteal) to be used for listing. |
| 64 | + method listingMethod |
| 65 | + |
| 66 | + // pageToken is the token to use for sequential listing. |
| 67 | + pageToken string |
| 68 | + |
| 69 | + // bucket is the bucket handle to list objects from. |
| 70 | + bucket *storage.BucketHandle |
| 71 | + |
| 72 | + // batchSize is the number of objects to list. |
| 73 | + batchSize int |
| 74 | + |
| 75 | + // query is the query to filter objects for listing. |
| 76 | + query storage.Query |
| 77 | + |
| 78 | + // skipDirectoryObjects is to indicate whether to list directory objects. |
| 79 | + skipDirectoryObjects bool |
| 80 | +} |
| 81 | + |
| 82 | +// NewLister creates a new dataflux Lister to list objects in the give bucket. |
| 83 | +func NewLister(c *storage.Client, in *ListerInput) *Lister { |
| 84 | + bucket := c.Bucket(in.BucketName) |
| 85 | + lister := &Lister{ |
| 86 | + method: open, |
| 87 | + pageToken: "", |
| 88 | + bucket: bucket, |
| 89 | + batchSize: in.BatchSize, |
| 90 | + query: in.Query, |
| 91 | + skipDirectoryObjects: in.SkipDirectoryObjects, |
| 92 | + } |
| 93 | + return lister |
| 94 | +} |
| 95 | + |
| 96 | +// NextBatch runs worksteal algorithm and sequential listing in parallel to quickly |
| 97 | +// return a list of objects in the bucket. For smaller dataset, |
| 98 | +// sequential listing is expected to be faster. For larger dataset, |
| 99 | +// worksteal listing is expected to be faster. |
| 100 | +func (c *Lister) NextBatch(ctx context.Context) ([]*storage.ObjectAttrs, error) { |
| 101 | + // countError tracks the number of failed listing methods. |
| 102 | + countError := 0 |
| 103 | + var results []*storage.ObjectAttrs |
| 104 | + ctx, cancel := context.WithCancel(ctx) |
| 105 | + defer cancel() |
| 106 | + // Errgroup takes care of running both methods in parallel. As soon as one of the method |
| 107 | + // is complete, the running method also stops. |
| 108 | + g, childCtx := errgroup.WithContext(ctx) |
| 109 | + |
| 110 | + // To start listing method is Open and runs both worksteal and sequential listing in parallel. |
| 111 | + // The method which completes first is used for all subsequent runs. |
| 112 | + // TODO: Run worksteal listing when method is Open or WorkSteal. |
| 113 | + // Run sequential listing when method is Open or Sequential. |
| 114 | + if c.method != worksteal { |
| 115 | + |
| 116 | + g.Go(func() error { |
| 117 | + objects, nextToken, err := c.sequentialListing(childCtx) |
| 118 | + if err != nil { |
| 119 | + countError++ |
| 120 | + return fmt.Errorf("error in running sequential listing: %w", err) |
| 121 | + } |
| 122 | + // If sequential listing completes first, set method to sequential listing and ranges to nil. |
| 123 | + // The nextToken will be used to continue sequential listing. |
| 124 | + results = objects |
| 125 | + c.pageToken = nextToken |
| 126 | + c.method = sequential |
| 127 | + // Close context when sequential listing is complete. |
| 128 | + cancel() |
| 129 | + return nil |
| 130 | + }) |
| 131 | + } |
| 132 | + |
| 133 | + // Close all functions if either sequential listing or worksteal listing is complete. |
| 134 | + err := g.Wait() |
| 135 | + |
| 136 | + // If the error is not context.Canceled, then return error instead of falling back |
| 137 | + // to the other method. This is so that the error can be fixed and user can take |
| 138 | + // advantage of fast-listing. |
| 139 | + // As one of the listing method completes, it is expected to cancel context for the other method. |
| 140 | + // If both sequential and worksteal listing fail due to context canceled, only then return error. |
| 141 | + if err != nil && (!errors.Is(err, context.Canceled) || countError > 1) { |
| 142 | + return nil, fmt.Errorf("failed waiting for sequntial and work steal lister : %w", err) |
| 143 | + } |
| 144 | + |
| 145 | + // If ranges for worksteal and pageToken for sequential listing is empty, then listing is complete. |
| 146 | + if c.pageToken == "" { |
| 147 | + return results, iterator.Done |
| 148 | + } |
| 149 | + return results, nil |
| 150 | +} |
| 151 | + |
| 152 | +// Close closes the range channel of the Lister. |
| 153 | +func (c *Lister) Close() { |
| 154 | + |
| 155 | + // TODO: Close range channel for worksteal lister. |
| 156 | +} |
0 commit comments