Skip to content

Commit

Permalink
Add option to reject duplicates
Browse files Browse the repository at this point in the history
Fixes #545.

Co-authored-by: Jesús García Crespo <[email protected]>
  • Loading branch information
camlyall and sevein committed Jul 19, 2023
1 parent 10bf0dc commit bebbae3
Show file tree
Hide file tree
Showing 27 changed files with 159 additions and 1 deletion.
2 changes: 2 additions & 0 deletions enduro.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bucket = "sips"
pipeline = "am"
retentionPeriod = "10s"
stripTopLevelDir = true
rejectDuplicates = false

[[watcher.filesystem]]
name = "dev-fs"
Expand All @@ -38,6 +39,7 @@ pipeline = "am"
completedDir = "./hack/landfill"
ignore = '(^\.gitkeep)|(^*\.mft)$'
stripTopLevelDir = true
rejectDuplicates = false

[[pipeline]]
name = "am"
Expand Down
1 change: 1 addition & 0 deletions internal/api/design/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var _ = Service("batch", func() {
Attribute("processing_config", String)
Attribute("completed_dir", String)
Attribute("retention_period", String)
Attribute("reject_duplicates", Boolean, func() { Default(false) })
Required("path")
})
Result(BatchResult)
Expand Down
1 change: 1 addition & 0 deletions internal/api/gen/batch/service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion internal/api/gen/http/batch/client/cli.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/api/gen/http/batch/client/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions internal/api/gen/http/batch/server/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/api/gen/http/cli/enduro/cli.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/api/gen/http/openapi.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions internal/api/gen/http/openapi.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions internal/api/gen/http/openapi3.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/api/gen/http/openapi3.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/batch/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (s *batchImpl) Submit(ctx context.Context, payload *goabatch.SubmitPayload)
}
input.RetentionPeriod = &dur
}
input.RejectDuplicates = payload.RejectDuplicates
opts := temporalsdk_client.StartWorkflowOptions{
ID: BatchWorkflowID,
WorkflowIDReusePolicy: temporalapi_enums.WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE,
Expand Down
2 changes: 2 additions & 0 deletions internal/batch/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type BatchWorkflowInput struct {
ProcessingConfig string
CompletedDir string
RetentionPeriod *time.Duration
RejectDuplicates bool
}

func BatchWorkflow(ctx temporalsdk_workflow.Context, params BatchWorkflowInput) error {
Expand Down Expand Up @@ -69,6 +70,7 @@ func (a *BatchActivity) Execute(ctx context.Context, params BatchWorkflowInput)
ProcessingConfig: params.ProcessingConfig,
CompletedDir: params.CompletedDir,
RetentionPeriod: params.RetentionPeriod,
RejectDuplicates: params.RejectDuplicates,
}
_ = a.batchsvc.InitProcessingWorkflow(ctx, &req)
}
Expand Down
11 changes: 11 additions & 0 deletions internal/collection/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Service interface {
// Goa returns an implementation of the goacollection Service.
Goa() goacollection.Service
Create(context.Context, *Collection) error
CheckDuplicate(ctx context.Context, id uint) (bool, error)
UpdateWorkflowStatus(ctx context.Context, ID uint, name string, workflowID, runID, transferID, aipID, pipelineID string, status Status, storedAt time.Time) error
SetStatus(ctx context.Context, ID uint, status Status) error
SetStatusInProgress(ctx context.Context, ID uint, startedAt time.Time) error
Expand Down Expand Up @@ -88,6 +89,16 @@ func (svc *collectionImpl) Create(ctx context.Context, col *Collection) error {
return nil
}

func (svc *collectionImpl) CheckDuplicate(ctx context.Context, id uint) (bool, error) {
query := `SELECT EXISTS(SELECT 1 FROM collection c1 WHERE c1.name = (SELECT name FROM collection WHERE id = ?) AND c1.id <> ? AND c1.status NOT IN (3, 6))`
var exists bool
err := svc.db.GetContext(ctx, &exists, query, id, id)
if err != nil {
return false, fmt.Errorf("sql error: %w", err)

Check warning on line 97 in internal/collection/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/collection/collection.go#L93-L97

Added lines #L93 - L97 were not covered by tests
}
return exists, nil

Check warning on line 99 in internal/collection/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/collection/collection.go#L99

Added line #L99 was not covered by tests
}

func publishEvent(ctx context.Context, events EventService, eventType string, id uint) {
// TODO: publish updated collection?
var item *goacollection.EnduroStoredCollection
Expand Down
15 changes: 15 additions & 0 deletions internal/collection/fake/mock_collection.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/collection/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ type ProcessingWorkflowRequest struct {

// Processing configuration name.
ProcessingConfig string

// Whether we reject duplicates based on name (key).
RejectDuplicates bool
}

func InitProcessingWorkflow(ctx context.Context, c temporalsdk_client.Client, taskQueue string, req *ProcessingWorkflowRequest) error {
Expand Down
2 changes: 2 additions & 0 deletions internal/watcher/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type FilesystemConfig struct {
RetentionPeriod *time.Duration
CompletedDir string
StripTopLevelDir bool
RejectDuplicates bool
}

// See minio.go for more.
Expand All @@ -56,4 +57,5 @@ type MinioConfig struct {
Pipeline []string
RetentionPeriod *time.Duration
StripTopLevelDir bool
RejectDuplicates bool
}
4 changes: 4 additions & 0 deletions internal/watcher/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type BlobEvent struct {
// Whether the top-level directory is meant to be stripped.
StripTopLevelDir bool

// Whether duplicates are rejected or not.
RejectDuplicates bool

// Key of the blob.
Key string

Expand All @@ -46,6 +49,7 @@ func NewBlobEvent(w Watcher, key string, isDir bool) *BlobEvent {
RetentionPeriod: w.RetentionPeriod(),
CompletedDir: w.CompletedDir(),
StripTopLevelDir: w.StripTopLevelDir(),
RejectDuplicates: w.RejectDuplicates(),
Key: key,
IsDir: isDir,
}
Expand Down
1 change: 1 addition & 0 deletions internal/watcher/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func NewFilesystemWatcher(ctx context.Context, config *FilesystemConfig) (*files
retentionPeriod: config.RetentionPeriod,
completedDir: config.CompletedDir,
stripTopLevelDir: config.StripTopLevelDir,
rejectDuplicates: config.RejectDuplicates,

Check warning on line 79 in internal/watcher/filesystem.go

View check run for this annotation

Codecov / codecov/patch

internal/watcher/filesystem.go#L79

Added line #L79 was not covered by tests
},
}

Expand Down
1 change: 1 addition & 0 deletions internal/watcher/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func NewMinioWatcher(ctx context.Context, config *MinioConfig) (*minioWatcher, e
pipeline: config.Pipeline,
retentionPeriod: config.RetentionPeriod,
stripTopLevelDir: config.StripTopLevelDir,
rejectDuplicates: config.RejectDuplicates,
},
}, nil
}
Expand Down
6 changes: 6 additions & 0 deletions internal/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Watcher interface {
RetentionPeriod() *time.Duration
CompletedDir() string
StripTopLevelDir() bool
RejectDuplicates() bool

// Full path of the watched bucket when available, empty string otherwise.
Path() string
Expand All @@ -42,6 +43,7 @@ type commonWatcherImpl struct {
retentionPeriod *time.Duration
completedDir string
stripTopLevelDir bool
rejectDuplicates bool
}

func (w *commonWatcherImpl) String() string {
Expand All @@ -64,6 +66,10 @@ func (w *commonWatcherImpl) StripTopLevelDir() bool {
return w.stripTopLevelDir
}

func (w *commonWatcherImpl) RejectDuplicates() bool {
return w.rejectDuplicates
}

type Service interface {
// Watchers return all known watchers.
Watchers() []Watcher
Expand Down
4 changes: 4 additions & 0 deletions internal/workflow/local_activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func updatePackageLocalActivity(ctx context.Context, logger logr.Logger, colsvc
return nil
}

func checkDuplicatePackageLocalActivity(ctx context.Context, logger logr.Logger, colsvc collection.Service, id uint) (bool, error) {
return colsvc.CheckDuplicate(ctx, id)

Check warning on line 64 in internal/workflow/local_activities.go

View check run for this annotation

Codecov / codecov/patch

internal/workflow/local_activities.go#L64

Added line #L64 was not covered by tests
}

func loadConfigLocalActivity(ctx context.Context, m *manager.Manager, pipeline string, tinfo *TransferInfo) (*TransferInfo, error) {
p, err := m.Pipelines.ByName(pipeline)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions internal/workflow/processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,21 @@ func (w *ProcessingWorkflow) Execute(ctx temporalsdk_workflow.Context, req *coll
}).Get(activityOpts, nil)
}()

// Reject duplicate collection if applicable.
{
if req.RejectDuplicates {
var exists bool
activityOpts := withLocalActivityOpts(ctx)
err := temporalsdk_workflow.ExecuteLocalActivity(activityOpts, checkDuplicatePackageLocalActivity, w.manager.Logger, w.manager.Collection, tinfo.CollectionID).Get(activityOpts, &exists)
if err != nil {
return fmt.Errorf("error checking duplicate: %v", err)

Check warning on line 236 in internal/workflow/processing.go

View check run for this annotation

Codecov / codecov/patch

internal/workflow/processing.go#L232-L236

Added lines #L232 - L236 were not covered by tests
}
if exists {
return fmt.Errorf("duplicate detected: key: %s", tinfo.Key)

Check warning on line 239 in internal/workflow/processing.go

View check run for this annotation

Codecov / codecov/patch

internal/workflow/processing.go#L238-L239

Added lines #L238 - L239 were not covered by tests
}
}
}

// Extract details from transfer name.
{
activityOpts := withLocalActivityWithoutRetriesOpts(ctx)
Expand Down
Loading

0 comments on commit bebbae3

Please sign in to comment.