Skip to content

Commit 18b6fdb

Browse files
authored
Merge pull request #8 from aws-samples/develop
chore: add the total object count to the finder log
2 parents 9079d2d + fdc3d9f commit 18b6fdb

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

dth/job.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"math"
2525
"strings"
2626
"sync"
27+
"sync/atomic"
2728
"time"
2829

2930
"github.com/aws/aws-sdk-go-v2/service/s3/types"
@@ -129,7 +130,7 @@ func NewFinder(ctx context.Context, cfg *JobConfig) (f *Finder) {
129130
func (f *Finder) Run(ctx context.Context) {
130131

131132
if !f.sqs.IsQueueEmpty(ctx) {
132-
log.Fatalf("Queue might not be empty or Unknown error... Please try again later")
133+
log.Fatalf("Queue might not be empty ... Please try again later")
133134
}
134135

135136
// Maximum number of queued batches to be sent to SQS
@@ -156,6 +157,7 @@ func (f *Finder) Run(ctx context.Context) {
156157
prefixes = f.srcClient.ListCommonPrefixes(ctx, f.cfg.FinderDepth, f.cfg.MaxKeys)
157158
}
158159
var wg sync.WaitGroup
160+
var totalObjCount int64 = 0
159161

160162
start := time.Now()
161163

@@ -164,9 +166,9 @@ func (f *Finder) Run(ctx context.Context) {
164166
log.Printf("prefix: %s", *p)
165167
wg.Add(1)
166168
if f.cfg.SkipCompare {
167-
go f.directSend(ctx, p, batchCh, msgCh, compareCh, &wg)
169+
go f.directSend(ctx, p, batchCh, msgCh, compareCh, &wg, &totalObjCount)
168170
} else {
169-
go f.compareAndSend(ctx, p, batchCh, msgCh, compareCh, &wg)
171+
go f.compareAndSend(ctx, p, batchCh, msgCh, compareCh, &wg, &totalObjCount)
170172
}
171173
}
172174
wg.Wait()
@@ -176,7 +178,7 @@ func (f *Finder) Run(ctx context.Context) {
176178
close(compareCh)
177179

178180
end := time.Since(start)
179-
log.Printf("Finder Job Completed in %v\n", end)
181+
log.Printf("Finder Job Completed in %v, found %d objects in total\n", end, totalObjCount)
180182
}
181183

182184
// List objects in destination bucket, load the full list into a map
@@ -214,14 +216,15 @@ func (f *Finder) getTargetObjects(ctx context.Context, prefix *string) (objects
214216

215217
// This function will compare source and target and get a list of delta,
216218
// and then send delta to SQS Queue.
217-
func (f *Finder) compareAndSend(ctx context.Context, prefix *string, batchCh chan struct{}, msgCh chan *string, compareCh chan struct{}, wg *sync.WaitGroup) {
219+
func (f *Finder) compareAndSend(ctx context.Context, prefix *string, batchCh chan struct{}, msgCh chan *string, compareCh chan struct{}, wg *sync.WaitGroup, totalObjCount *int64) {
218220
defer wg.Done()
219221

220222
log.Printf("Comparing within prefix /%s\n", *prefix)
221223
target := f.getTargetObjects(ctx, prefix)
222224

223225
token := ""
224226
i, j := 0, 0
227+
var objCount int64 = 0
225228
retry := 0
226229
// batch := make([]*string, f.cfg.MessageBatchSize)
227230

@@ -261,6 +264,7 @@ func (f *Finder) compareAndSend(ctx context.Context, prefix *string, batchCh cha
261264
i++
262265
if i%f.cfg.MessageBatchSize == 0 {
263266
wg.Add(1)
267+
objCount += int64(f.cfg.MessageBatchSize)
264268
j++
265269
if j%100 == 0 {
266270
log.Printf("Found %d batches in prefix /%s\n", j, *prefix)
@@ -286,6 +290,7 @@ func (f *Finder) compareAndSend(ctx context.Context, prefix *string, batchCh cha
286290
// For remainning objects.
287291
if i != 0 {
288292
j++
293+
objCount += int64(i)
289294
wg.Add(1)
290295
batchCh <- struct{}{}
291296
go func(i int) {
@@ -299,21 +304,23 @@ func (f *Finder) compareAndSend(ctx context.Context, prefix *string, batchCh cha
299304
<-batchCh
300305
}(i)
301306
}
307+
atomic.AddInt64(totalObjCount, objCount)
302308

303309
// end := time.Since(start)
304310
// log.Printf("Compared and Sent %d batches in %v", j, end)
305-
log.Printf("Completed in prefix /%s, found %d batches in total", *prefix, j)
311+
log.Printf("Completed in prefix /%s, found %d batches (%d objects) in total", *prefix, j, objCount)
306312
<-compareCh
307313
}
308314

309315
// This function will send the task to SQS Queue directly, without comparison.
310-
func (f *Finder) directSend(ctx context.Context, prefix *string, batchCh chan struct{}, msgCh chan *string, compareCh chan struct{}, wg *sync.WaitGroup) {
316+
func (f *Finder) directSend(ctx context.Context, prefix *string, batchCh chan struct{}, msgCh chan *string, compareCh chan struct{}, wg *sync.WaitGroup, totalObjCount *int64) {
311317
defer wg.Done()
312318

313319
log.Printf("Scanning prefix /%s\n", *prefix)
314320

315321
token := ""
316322
i, j := 0, 0
323+
var objCount int64 = 0
317324
retry := 0
318325
// batch := make([]*string, f.cfg.MessageBatchSize)
319326

@@ -350,6 +357,7 @@ func (f *Finder) directSend(ctx context.Context, prefix *string, batchCh chan st
350357
i++
351358
if i%f.cfg.MessageBatchSize == 0 {
352359
wg.Add(1)
360+
objCount += int64(f.cfg.MessageBatchSize)
353361
j++
354362
if j%100 == 0 {
355363
log.Printf("Found %d batches in prefix /%s\n", j, *prefix)
@@ -374,6 +382,7 @@ func (f *Finder) directSend(ctx context.Context, prefix *string, batchCh chan st
374382
// For remainning objects.
375383
if i != 0 {
376384
j++
385+
objCount += int64(i)
377386
wg.Add(1)
378387
batchCh <- struct{}{}
379388
go func(i int) {
@@ -387,10 +396,11 @@ func (f *Finder) directSend(ctx context.Context, prefix *string, batchCh chan st
387396
<-batchCh
388397
}(i)
389398
}
399+
atomic.AddInt64(totalObjCount, objCount)
390400

391401
// end := time.Since(start)
392402
// log.Printf("Compared and Sent %d batches in %v", j, end)
393-
log.Printf("Completed in prefix /%s, found %d batches in total", *prefix, j)
403+
log.Printf("Completed in prefix /%s, found %d batches (%d objects) in total", *prefix, j, objCount)
394404
<-compareCh
395405
}
396406

0 commit comments

Comments
 (0)