-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
identity: Add batch entity deletion endpoint #8785
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d5330a1
identity: Add batch entity deletion endpoint
f192735
Update the parameter description
f8de8be
Update error message
a7b1175
Update helper/storagepacker/storagepacker.go
briankassouf d9ecd0f
Review feedback
a8800e1
Update vault/identity_store_entities.go
briankassouf f7cb2ae
Merge branch 'master' into entity-bulk-delete
briankassouf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,113 +127,85 @@ func (s *StoragePacker) BucketKey(itemID string) string { | |
|
||
// DeleteItem removes the item from the respective bucket | ||
func (s *StoragePacker) DeleteItem(_ context.Context, itemID string) error { | ||
return s.DeleteMultipleItems(context.Background(), nil, itemID) | ||
return s.DeleteMultipleItems(context.Background(), nil, []string{itemID}) | ||
} | ||
|
||
func (s *StoragePacker) DeleteMultipleItems(ctx context.Context, logger hclog.Logger, itemIDs ...string) error { | ||
func (s *StoragePacker) DeleteMultipleItems(ctx context.Context, logger hclog.Logger, itemIDs []string) error { | ||
defer metrics.MeasureSince([]string{"storage_packer", "delete_items"}, time.Now()) | ||
var err error | ||
switch len(itemIDs) { | ||
case 0: | ||
// Nothing | ||
if len(itemIDs) == 0 { | ||
return nil | ||
|
||
case 1: | ||
logger = hclog.NewNullLogger() | ||
fallthrough | ||
|
||
default: | ||
lockIndexes := make(map[string]struct{}, len(s.storageLocks)) | ||
for _, itemID := range itemIDs { | ||
bucketKey := s.BucketKey(itemID) | ||
if _, ok := lockIndexes[bucketKey]; !ok { | ||
lockIndexes[bucketKey] = struct{}{} | ||
} | ||
} | ||
|
||
lockKeys := make([]string, 0, len(lockIndexes)) | ||
for k := range lockIndexes { | ||
lockKeys = append(lockKeys, k) | ||
} | ||
|
||
locks := locksutil.LocksForKeys(s.storageLocks, lockKeys) | ||
for _, lock := range locks { | ||
lock.Lock() | ||
defer lock.Unlock() | ||
} | ||
} | ||
|
||
if logger == nil { | ||
logger = hclog.NewNullLogger() | ||
} | ||
|
||
bucketCache := make(map[string]*Bucket, len(s.storageLocks)) | ||
// Sort the ids by the bucket they will be deleted from | ||
lockKeys := make([]string, 0) | ||
byBucket := make(map[string]map[string]struct{}) | ||
for _, id := range itemIDs { | ||
bucketKey := s.BucketKey(id) | ||
bucket, ok := byBucket[bucketKey] | ||
if !ok { | ||
bucket = make(map[string]struct{}) | ||
byBucket[bucketKey] = bucket | ||
|
||
logger.Debug("deleting multiple items from storagepacker; caching and deleting from buckets", "total_items", len(itemIDs)) | ||
// Add the lock key once | ||
lockKeys = append(lockKeys, bucketKey) | ||
} | ||
|
||
var pctDone int | ||
for idx, itemID := range itemIDs { | ||
bucketKey := s.BucketKey(itemID) | ||
bucket[id] = struct{}{} | ||
} | ||
|
||
bucket, bucketFound := bucketCache[bucketKey] | ||
if !bucketFound { | ||
// Read from storage | ||
storageEntry, err := s.view.Get(context.Background(), bucketKey) | ||
if err != nil { | ||
return errwrap.Wrapf("failed to read packed storage value: {{err}}", err) | ||
} | ||
if storageEntry == nil { | ||
return nil | ||
} | ||
locks := locksutil.LocksForKeys(s.storageLocks, lockKeys) | ||
for _, lock := range locks { | ||
lock.Lock() | ||
defer lock.Unlock() | ||
} | ||
|
||
uncompressedData, notCompressed, err := compressutil.Decompress(storageEntry.Value) | ||
if err != nil { | ||
return errwrap.Wrapf("failed to decompress packed storage value: {{err}}", err) | ||
} | ||
if notCompressed { | ||
uncompressedData = storageEntry.Value | ||
} | ||
logger.Debug("deleting multiple items from storagepacker; caching and deleting from buckets", "total_items", len(itemIDs)) | ||
|
||
bucket = new(Bucket) | ||
err = proto.Unmarshal(uncompressedData, bucket) | ||
if err != nil { | ||
return errwrap.Wrapf("failed decoding packed storage entry: {{err}}", err) | ||
} | ||
// For each bucket, load from storage, remove the necessary items, and add | ||
// write it back out to storage | ||
pctDone := 0 | ||
idx := 0 | ||
for bucketKey, itemsToRemove := range byBucket { | ||
// Read bucket from storage | ||
storageEntry, err := s.view.Get(context.Background(), bucketKey) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be using |
||
if err != nil { | ||
return errwrap.Wrapf("failed to read packed storage value: {{err}}", err) | ||
} | ||
|
||
// Look for a matching storage entry | ||
foundIdx := -1 | ||
for itemIdx, item := range bucket.Items { | ||
if item.ID == itemID { | ||
foundIdx = itemIdx | ||
break | ||
} | ||
if storageEntry == nil { | ||
logger.Warn("could not find bucket", "bucket", bucketKey) | ||
continue | ||
} | ||
|
||
// If there is a match, remove it from the collection and persist the | ||
// resulting collection | ||
if foundIdx != -1 { | ||
bucket.Items[foundIdx] = bucket.Items[len(bucket.Items)-1] | ||
bucket.Items = bucket.Items[:len(bucket.Items)-1] | ||
if !bucketFound { | ||
bucketCache[bucketKey] = bucket | ||
} | ||
uncompressedData, notCompressed, err := compressutil.Decompress(storageEntry.Value) | ||
if err != nil { | ||
return errwrap.Wrapf("failed to decompress packed storage value: {{err}}", err) | ||
} | ||
if notCompressed { | ||
uncompressedData = storageEntry.Value | ||
} | ||
|
||
newPctDone := idx * 100.0 / len(itemIDs) | ||
if int(newPctDone) > pctDone { | ||
pctDone = int(newPctDone) | ||
logger.Trace("bucket item removal progress", "percent", pctDone, "items_removed", idx) | ||
bucket := new(Bucket) | ||
err = proto.Unmarshal(uncompressedData, bucket) | ||
if err != nil { | ||
return errwrap.Wrapf("failed decoding packed storage entry: {{err}}", err) | ||
} | ||
} | ||
|
||
logger.Debug("persisting buckets", "total_buckets", len(bucketCache)) | ||
// Look for a matching storage entries and delete them from the list. | ||
for i := 0; i < len(bucket.Items); i++ { | ||
if _, ok := itemsToRemove[bucket.Items[i].ID]; ok { | ||
bucket.Items[i] = bucket.Items[len(bucket.Items)-1] | ||
catsby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bucket.Items = bucket.Items[:len(bucket.Items)-1] | ||
|
||
// Since we just moved a value to position i we need to | ||
// decrement i so we replay this position | ||
i-- | ||
} | ||
} | ||
|
||
// Persist all buckets in the cache; these will be the ones that had | ||
// deletions | ||
pctDone = 0 | ||
idx := 0 | ||
for _, bucket := range bucketCache { | ||
// Fail if the context is canceled, the storage calls will fail anyways | ||
if ctx.Err() != nil { | ||
return ctx.Err() | ||
|
@@ -244,7 +216,7 @@ func (s *StoragePacker) DeleteMultipleItems(ctx context.Context, logger hclog.Lo | |
return err | ||
} | ||
|
||
newPctDone := idx * 100.0 / len(bucketCache) | ||
newPctDone := idx * 100.0 / len(byBucket) | ||
if int(newPctDone) > pctDone { | ||
pctDone = int(newPctDone) | ||
logger.Trace("bucket persistence progress", "percent", pctDone, "buckets_persisted", idx) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to this PR in particular, but why do we ignore the passed in context and provide a fresh
context.Background()
in here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too sure off hand but we could revisit that in another PR