-
Notifications
You must be signed in to change notification settings - Fork 554
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
cephfs: safeguard localClusterState struct from race conditions #4163
Merged
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -22,6 +22,7 @@ import ( | |
"fmt" | ||
"path" | ||
"strings" | ||
"sync" | ||
|
||
cerrors "github.com/ceph/ceph-csi/internal/cephfs/errors" | ||
fsutil "github.com/ceph/ceph-csi/internal/cephfs/util" | ||
|
@@ -32,12 +33,17 @@ import ( | |
"github.com/ceph/go-ceph/rados" | ||
) | ||
|
||
// clusterAdditionalInfo contains information regarding if resize is | ||
// supported in the particular cluster and subvolumegroup is | ||
// created or not. | ||
// Subvolumegroup creation and volume resize decisions are | ||
// taken through this additional cluster information. | ||
var clusterAdditionalInfo = make(map[string]*localClusterState) | ||
var ( | ||
// clusterAdditionalInfo contains information regarding if resize is | ||
// supported in the particular cluster and subvolumegroup is | ||
// created or not. | ||
// Subvolumegroup creation and volume resize decisions are | ||
// taken through this additional cluster information. | ||
clusterAdditionalInfo = make(map[string]*localClusterState) | ||
// clusterAdditionalInfoMutex is used to protect against | ||
// concurrent writes. | ||
clusterAdditionalInfoMutex = sync.Mutex{} | ||
) | ||
|
||
// Subvolume holds subvolume information. This includes only the needed members | ||
// from fsAdmin.SubVolumeInfo. | ||
|
@@ -209,14 +215,18 @@ type localClusterState struct { | |
// set true once a subvolumegroup is created | ||
// for corresponding filesystem in a cluster. | ||
subVolumeGroupsCreated map[string]bool | ||
// subVolumeGroupsRWMutex is used to protect subVolumeGroupsCreated map | ||
// against concurrent writes while allowing multiple readers. | ||
subVolumeGroupsRWMutex sync.RWMutex | ||
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. describe the usage of this mutex, what does it protect, when do callers need to lock it? |
||
} | ||
|
||
func newLocalClusterState(clusterID string) { | ||
// verify if corresponding clusterID key is present in the map, | ||
// and if not, initialize with default values(false). | ||
clusterAdditionalInfoMutex.Lock() | ||
defer clusterAdditionalInfoMutex.Unlock() | ||
if _, keyPresent := clusterAdditionalInfo[clusterID]; !keyPresent { | ||
clusterAdditionalInfo[clusterID] = &localClusterState{} | ||
clusterAdditionalInfo[clusterID].subVolumeGroupsCreated = make(map[string]bool) | ||
} | ||
} | ||
|
||
|
@@ -232,7 +242,7 @@ func (s *subVolumeClient) CreateVolume(ctx context.Context) error { | |
} | ||
|
||
// create subvolumegroup if not already created for the cluster. | ||
if !clusterAdditionalInfo[s.clusterID].subVolumeGroupsCreated[s.FsName] { | ||
if !s.isSubVolumeGroupCreated() { | ||
opts := fsAdmin.SubVolumeGroupOptions{} | ||
err = ca.CreateSubVolumeGroup(s.FsName, s.SubvolumeGroup, &opts) | ||
if err != nil { | ||
|
@@ -246,7 +256,7 @@ func (s *subVolumeClient) CreateVolume(ctx context.Context) error { | |
return err | ||
} | ||
log.DebugLog(ctx, "cephfs: created subvolume group %s", s.SubvolumeGroup) | ||
clusterAdditionalInfo[s.clusterID].subVolumeGroupsCreated[s.FsName] = true | ||
s.updateSubVolumeGroupCreated(true) | ||
} | ||
|
||
opts := fsAdmin.SubVolumeOptions{ | ||
|
@@ -264,7 +274,7 @@ func (s *subVolumeClient) CreateVolume(ctx context.Context) error { | |
if errors.Is(err, rados.ErrNotFound) { | ||
// Reset the subVolumeGroupsCreated so that we can try again to create the | ||
// subvolumegroup in next request if the error is Not Found. | ||
clusterAdditionalInfo[s.clusterID].subVolumeGroupsCreated[s.FsName] = false | ||
s.updateSubVolumeGroupCreated(false) | ||
} | ||
|
||
return err | ||
|
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.
Would a
sync.RWMutex
not be better?If you only mean to protect against concurrent writes, please state it clearly.