8000 vrg: synchronize map writes by asn1809 · Pull Request #2065 · RamenDR/ramen · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

vrg: synchronize map writes #2065

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

Merged
merged 1 commit into from
Jun 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions internal/controller/vrg_vrgobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
package controllers

import (
"sync"

ramen "github.com/ramendr/ramen/api/v1alpha1"
"github.com/ramendr/ramen/internal/controller/util"
corev1 "k8s.io/api/core/v1"
ctrl "sigs.k8s.io/controller-runtime"
)

var vrgLastUploadVersion = map[string]string{}
var vrgLastUploadVersion sync.Map
Copy link
Member

Choose a reason for hiding this comment

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

sync.Map is recommended only for special cases:

The Map type is specialized. Most code should use a plain Go map instead, with separate locking or coordination, for better type safety and to make it easier to maintain other invariants along with the map content.

The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex.

See https://pkg.go.dev/sync#Map

If we don't use the special use case, we should use a lock to synchronize access to the map.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks @nirs. We read on this and found that we are using special case 2 and hence, chose to proceed with sync.Map.


func (v *VRGInstance) vrgObjectProtect(result *ctrl.Result) {
vrg := v.instance
log := v.log

if lastUploadVersion, ok := vrgLastUploadVersion[v.namespacedName]; ok {
if vrg.ResourceVersion == lastUploadVersion {
if lastUploadVersion, ok := vrgLastUploadVersion.Load(v.namespacedName); ok {
if lastUploadVersion == vrg.ResourceVersion {
log.Info("VRG resource version unchanged, skip S3 upload", "version", vrg.ResourceVersion)

return
Expand Down Expand Up @@ -57,7 +59,7 @@ func (v *VRGInstance) vrgObjectProtectThrottled(result *ctrl.Result,

log1.Info("VRG Kube object protected")

vrgLastUploadVersion[v.namespacedName] = vrg.ResourceVersion
vrgLastUploadVersion.Store(v.namespacedName, vrg.ResourceVersion)
v.vrgObjectProtected = newVRGClusterDataProtectedCondition(vrg.Generation, vrgClusterDataProtectedTrueMessage)
}

Expand Down
Loading
0