fix: serialize concurrent property updates
Concurrent commands for the same app could each read a setting, change it, and write it back, so all of them exited successfully while silently dropping changes made by the others. Updates to app settings are now serialized so every change is kept, and readers never see a partially written value.
This commit is contained in:
42 files changed
+1698
-642
No files matched your search
@@ -0,0 +1,46 @@
|
||||
package registry
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os/user"
|
||||
"sort"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIncrementTagVersionConcurrent(t *testing.T) {
|
||||
t.Setenv("DOKKU_LIB_ROOT", t.TempDir())
|
||||
current, err := user.Current()
|
||||
if err != nil {
|
||||
t.Fatalf("user.Current: %v", err)
|
||||
}
|
||||
group, err := user.LookupGroupId(current.Gid)
|
||||
if err != nil {
|
||||
t.Fatalf("user.LookupGroupId: %v", err)
|
||||
}
|
||||
t.Setenv("DOKKU_SYSTEM_USER", current.Username)
|
||||
t.Setenv("DOKKU_SYSTEM_GROUP", group.Name)
|
||||
|
||||
const count = 20
|
||||
var wg sync.WaitGroup
|
||||
versions := make([]int, count)
|
||||
errs := make([]error, count)
|
||||
for i := 0; i < count; i++ {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
versions[i], errs[i] = incrementTagVersion("race-test")
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
if err := errors.Join(errs...); err != nil {
|
||||
t.Fatalf("incrementTagVersion: %v", err)
|
||||
}
|
||||
|
||||
sort.Ints(versions)
|
||||
for i, version := range versions {
|
||||
if version != i+1 {
|
||||
t.Fatalf("versions = %v, want 1 through %d with no duplicates", versions, count)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -123,21 +123,28 @@ func isPushEnabled(appName string) bool {
|
||||
return reportComputedPushOnRelease(appName) == "true"
|
||||
}
|
||||
|
||||
// incrementTagVersion bumps the app's tag version and returns the new value,
|
||||
// holding the app's registry property lock across the read and the write so
|
||||
// concurrent releases never hand out the same version.
|
||||
func incrementTagVersion(appName string) (int, error) {
|
||||
tag := common.PropertyGet("registry", appName, "tag-version")
|
||||
if tag == "" {
|
||||
tag = "0"
|
||||
}
|
||||
version := 0
|
||||
err := common.PropertyLock("registry", appName, func(p *common.LockedProperties) error {
|
||||
tag := common.PropertyGet("registry", appName, "tag-version")
|
||||
if tag == "" {
|
||||
tag = "0"
|
||||
}
|
||||
|
||||
tag = strings.TrimSpace(tag)
|
||||
version, err := strconv.Atoi(tag)
|
||||
tag = strings.TrimSpace(tag)
|
||||
current, err := strconv.Atoi(tag)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to convert existing tag version (%s) to integer: %v", tag, err)
|
||||
}
|
||||
|
||||
version = current + 1
|
||||
common.LogVerboseQuiet(fmt.Sprintf("Bumping tag to %d", version))
|
||||
return p.Write("tag-version", strconv.Itoa(version))
|
||||
})
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("Unable to convert existing tag version (%s) to integer: %v", tag, err)
|
||||
}
|
||||
|
||||
version++
|
||||
common.LogVerboseQuiet(fmt.Sprintf("Bumping tag to %d", version))
|
||||
if err = common.PropertyWrite("registry", appName, "tag-version", strconv.Itoa(version)); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
|
||||
@@ -126,7 +126,7 @@ func CommandLogin(appName string, server string, username string, password strin
|
||||
env["DOCKER_CONFIG"] = GetAppRegistryConfigDir(appName)
|
||||
}
|
||||
|
||||
result, err := common.CallExecCommand(common.ExecCommandInput{
|
||||
result, err := execDockerConfigCommand(env["DOCKER_CONFIG"], common.ExecCommandInput{
|
||||
Command: common.DockerBin(),
|
||||
Args: []string{"login", "--username", username, "--password-stdin", server},
|
||||
Env: env,
|
||||
@@ -184,7 +184,7 @@ func CommandLogout(appName string, server string) error {
|
||||
env["DOCKER_CONFIG"] = GetAppRegistryConfigDir(appName)
|
||||
}
|
||||
|
||||
result, err := common.CallExecCommand(common.ExecCommandInput{
|
||||
result, err := execDockerConfigCommand(env["DOCKER_CONFIG"], common.ExecCommandInput{
|
||||
Command: common.DockerBin(),
|
||||
Args: []string{"logout", server},
|
||||
Env: env,
|
||||
@@ -226,3 +226,22 @@ func CommandSet(appName string, property string, value string) error {
|
||||
common.CommandPropertySet("registry", appName, property, value, DefaultProperties, GlobalProperties)
|
||||
return nil
|
||||
}
|
||||
|
||||
// execDockerConfigCommand runs a docker command that rewrites the credentials
|
||||
// file in configDir. docker merges the file it reads with the change it makes,
|
||||
// so the directory stays locked for the whole command to keep concurrent logins
|
||||
// and logouts for one app from dropping each other's credentials. An empty
|
||||
// configDir means docker's own config, which is left unlocked.
|
||||
func execDockerConfigCommand(configDir string, input common.ExecCommandInput) (common.ExecCommandResponse, error) {
|
||||
if configDir == "" {
|
||||
return common.CallExecCommand(input)
|
||||
}
|
||||
|
||||
var result common.ExecCommandResponse
|
||||
err := common.LockDirectory(configDir, func() error {
|
||||
var err error
|
||||
result, err = common.CallExecCommand(input)
|
||||
return err
|
||||
})
|
||||
return result, err
|
||||
}
|
||||
Reference in new issue
Block a user