Skip to content

Commit

Permalink
Add imports location lock to serialize non-atomic operations
Browse files Browse the repository at this point in the history
Adding and removing items from the imports location list is currently
implemented in a non-atomic way, by first reading a list of items from the
server, updating it locally, and sending a new list to the server.

Introduce a (per-resource, per-location) lock to all resources that require
modifications of the import list so that those operations are done sequentially.
  • Loading branch information
kklimonda-cl committed Dec 11, 2024
1 parent 761708c commit 75c37c1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/translate/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ func RenderImports(templateTypes ...string) (string, error) {

for _, templateType := range templateTypes {
switch templateType {
case "sync":
manager.AddStandardImport("sync", "")
case "strings":
manager.AddStandardImport("strings", "")
case "config":
//manager.AddStandardImport("fmt", "")
manager.AddStandardImport("encoding/xml", "")
Expand Down
57 changes: 55 additions & 2 deletions templates/sdk/service.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package {{packageName .GoSdkPath}}
{{- if .Entry}}
{{- if $.Imports}}
{{- if $.Spec.Params.uuid}}
{{renderImports "service" "filtering" "audit" "rule" "version"}}
{{renderImports "service" "filtering" "sync" "strings" "audit" "rule" "version"}}
{{- else}}
{{renderImports "service" "filtering"}}
{{renderImports "service" "filtering" "sync" "strings"}}
{{- end}}
{{- else}}
{{- if $.Spec.Params.uuid}}
Expand All @@ -22,6 +22,39 @@ package {{packageName .GoSdkPath}}
{{- end}}
{{- end}}

{{- if and $.Imports .Entry }}
var (
importsMutexMap = make(map[string]*sync.Mutex)
importsMutexMapLock = sync.Mutex{}
)

func (s *Service) getImportMutexes(importLocs []ImportLocation, location Location) ([]*sync.Mutex, error) {
importsMutexMapLock.Lock()
defer importsMutexMapLock.Unlock()

var result []*sync.Mutex
for _, elt := range importLocs {
importXpath, err := elt.XpathForLocation(s.client.Versioning(), location)
if err != nil {
return nil, err
}
importMutexKey := strings.Join(importXpath, "/")

var importMutex *sync.Mutex
var ok bool
importMutex, ok = importsMutexMap[importMutexKey]
if !ok {
importMutex = &sync.Mutex{}
importsMutexMap[importMutexKey] = importMutex
}
result = append(result, importMutex)
}

return result, nil

}
{{- end }}

type Service struct {
client util.PangoClient
}
Expand Down Expand Up @@ -84,6 +117,16 @@ return nil, err
}

{{- if .Imports }}
importMutexes, err := s.getImportMutexes(importLocations, loc)
if err != nil {
return nil, err
}

for _, elt := range importMutexes {
elt.Lock()
defer elt.Unlock()
}

err = s.importToLocations(ctx, loc, importLocations, entry.Name)
if err != nil {
return nil, err
Expand Down Expand Up @@ -529,6 +572,16 @@ vn := s.client.Versioning()
var err error
deletes := xmlapi.NewMultiConfig(len(values))
{{- if .Imports }}
importMutexes, err := s.getImportMutexes(importLocations, loc)
if err != nil {
return err
}

for _, elt := range importMutexes {
elt.Lock()
defer elt.Unlock()
}

err = s.unimportFromLocations(ctx, deletes, loc, importLocations, values)
if err != nil {
return err
Expand Down

0 comments on commit 75c37c1

Please sign in to comment.