Skip to content

Enhance deletion for set of resources in parallel#2107

Merged
cb-github-robot merged 1 commit intocloud-barista:mainfrom
seokho-son:main
Aug 21, 2025
Merged

Enhance deletion for set of resources in parallel#2107
cb-github-robot merged 1 commit intocloud-barista:mainfrom
seokho-son:main

Conversation

@seokho-son
Copy link
Copy Markdown
Member

No description provided.

Signed-off-by: Seokho Son <shsongist@gmail.com>
Copilot AI review requested due to automatic review settings August 21, 2025 01:34
@seokho-son seokho-son requested a review from yunkon-kim as a code owner August 21, 2025 01:34
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the deletion of resources by implementing parallel processing with CSP-specific concurrency limits and improved error handling. The main goal is to optimize performance when deleting multiple resources by grouping them by cloud service provider (CSP) connection and processing them concurrently.

  • Implements parallel deletion with connection-based grouping and semaphore-controlled concurrency
  • Adds comprehensive logging for debugging and monitoring deletion operations
  • Fixes JSON serialization issues by avoiding mutex in response structures

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
src/interface/rest/server/resource/common.go Adds detailed logging and creates clean response structure without mutex to avoid JSON serialization issues
src/core/resource/common.go Implements parallel deletion logic with CSP connection grouping, semaphore-based concurrency control, and enhanced error handling

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

func getResourceConnectionName(nsId, resourceType, resourceId string) (string, error) {
// For performance, try to extract connection name from resourceId pattern first
// Many resources follow the pattern: {connectionName}-{resourceName}
parts := strings.Split(resourceId, "-")
Copy link

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function assumes resource IDs follow a specific naming pattern (connectionName-resourceName) but this assumption may not hold for all resources. Consider adding validation or documentation about this assumption.

Copilot uses AI. Check for mistakes.
errChan <- err // Send error to the error channel
}
// Create semaphores for each connection (limit concurrent operations per CSP)
const maxConcurrentPerCSP = 20
Copy link

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hardcoded concurrency limit of 20 should be configurable or at least documented with justification for this specific value. Different CSPs may have different rate limits.

Suggested change
const maxConcurrentPerCSP = 20
maxConcurrentPerCSP := getMaxConcurrentPerCSP() // configurable via env var CBTB_MAX_CONCURRENT_PER_CSP, default 20

Copilot uses AI. Check for mistakes.
log.Debug().Msgf("Starting deletion of %s:%s (connection: %s)", resourceType, resourceId, connName)

// Minimal random sleep to avoid thundering herd (reduced significantly)
common.RandomSleep(0, 100)
Copy link

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 100 for random sleep duration should be defined as a named constant or made configurable, with documentation explaining its purpose.

Suggested change
common.RandomSleep(0, 100)
common.RandomSleep(0, maxRandomSleepMsForDeletion)

Copilot uses AI. Check for mistakes.
select {
case errChan <- err:
// Successfully sent error to channel
case <-time.After(10 * time.Millisecond):
Copy link

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The timeout value of 10 milliseconds for error channel operations should be defined as a named constant with documentation explaining the rationale for this specific duration.

Suggested change
case <-time.After(10 * time.Millisecond):
case <-time.After(errorChanTimeout):

Copilot uses AI. Check for mistakes.
errString = " (" + err.Error() + ")"

// Safe error channel send - check if channel is still open
if atomic.LoadInt32(&errChanClosed) == 0 {
Copy link

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The atomic flag check for channel closure has a race condition. Between checking the flag and sending to the channel, another goroutine could close the channel, potentially causing a panic. Consider using a more robust pattern like a context for cancellation.

Copilot uses AI. Check for mistakes.
// Safely close the error channel with atomic flag
if atomic.CompareAndSwapInt32(&errChanClosed, 0, 1) {
close(errChan)
}
Copy link

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This atomic operation for channel closure protection is complex and error-prone. Consider using sync.Once or a context-based approach for cleaner concurrency control.

Suggested change
}
// Safely close the error channel using sync.Once
errChanCloseOnce.Do(func() { close(errChan) })

Copilot uses AI. Check for mistakes.
@seokho-son
Copy link
Copy Markdown
Member Author

/approve

@github-actions github-actions bot added the approved This PR is approved and will be merged soon. label Aug 21, 2025
@cb-github-robot cb-github-robot merged commit 7885701 into cloud-barista:main Aug 21, 2025
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved This PR is approved and will be merged soon.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants