Skip to content
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

fix: Add retry on locks #4997

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ const (
APISecretFlag = "api-secret"
HidePrevPlanComments = "hide-prev-plan-comments"
QuietPolicyChecks = "quiet-policy-checks"
LockAcquireTimeoutSeconds = "lock-acquire-timeout"
LockingDBType = "locking-db-type"
LogLevelFlag = "log-level"
MarkdownTemplateOverridesDirFlag = "markdown-template-overrides-dir"
Expand Down Expand Up @@ -175,6 +176,7 @@ const (
DefaultGiteaBaseURL = "https://gitea.com"
DefaultGiteaPageSize = 30
DefaultGitlabHostname = "gitlab.com"
DefaultLockAcquireTimeoutSeconds = 900
DefaultLockingDBType = "boltdb"
DefaultLogLevel = "info"
DefaultIgnoreVCSStatusNames = ""
Expand Down Expand Up @@ -619,6 +621,10 @@ var intFlags = map[string]intFlag{
" If merge base is further behind than this number of commits from any of branches heads, full fetch will be performed.",
defaultValue: DefaultCheckoutDepth,
},
LockAcquireTimeoutSeconds: {
description: fmt.Sprintf("The number of seconds to wait for a lock to be acquired before timing out. The default value is %d", DefaultLockAcquireTimeoutSeconds),
defaultValue: DefaultLockAcquireTimeoutSeconds,
},
MaxCommentsPerCommand: {
description: "If non-zero, the maximum number of comments to split command output into before truncating.",
defaultValue: DefaultMaxCommentsPerCommand,
Expand Down Expand Up @@ -945,6 +951,9 @@ func (s *ServerCmd) setDefaults(c *server.UserConfig, v *viper.Viper) {
if c.AutoDiscoverModeFlag == "" {
c.AutoDiscoverModeFlag = DefaultAutoDiscoverMode
}
if c.LockAcquireTimeoutSeconds == 0 {
c.LockAcquireTimeoutSeconds = DefaultLockAcquireTimeoutSeconds
}
}

func (s *ServerCmd) validate(userConfig server.UserConfig) error {
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ func setupE2E(t *testing.T, repoDir string, opt setupOption) (events_controllers
}

defaultTFVersion := terraformClient.DefaultVersion()
locker := events.NewDefaultWorkingDirLocker()
locker := events.NewDefaultWorkingDirLocker(1)
parser := &config.ParserValidator{}

globalCfgArgs := valid.GlobalCfgArgs{
Expand Down
6 changes: 3 additions & 3 deletions server/controllers/locks_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func TestDeleteLock_UpdateProjectStatus(t *testing.T) {
cp := vcsmocks.NewMockClient()
l := mocks2.NewMockDeleteLockCommand()
workingDir := mocks2.NewMockWorkingDir()
workingDirLocker := events.NewDefaultWorkingDirLocker()
workingDirLocker := events.NewDefaultWorkingDirLocker(1)
pull := models.PullRequest{
BaseRepo: models.Repo{FullName: repoName},
}
Expand Down Expand Up @@ -345,7 +345,7 @@ func TestDeleteLock_CommentFailed(t *testing.T) {
}, nil)
cp := vcsmocks.NewMockClient()
workingDir := mocks2.NewMockWorkingDir()
workingDirLocker := events.NewDefaultWorkingDirLocker()
workingDirLocker := events.NewDefaultWorkingDirLocker(1)
var backend locking.Backend
tmp := t.TempDir()
backend, err := db.New(tmp)
Expand All @@ -372,7 +372,7 @@ func TestDeleteLock_CommentSuccess(t *testing.T) {
cp := vcsmocks.NewMockClient()
dlc := mocks2.NewMockDeleteLockCommand()
workingDir := mocks2.NewMockWorkingDir()
workingDirLocker := events.NewDefaultWorkingDirLocker()
workingDirLocker := events.NewDefaultWorkingDirLocker(1)
var backend locking.Backend
tmp := t.TempDir()
backend, err := db.New(tmp)
Expand Down
2 changes: 1 addition & 1 deletion server/events/delete_lock_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestDeleteLock_Success(t *testing.T) {
l := lockmocks.NewMockLocker()
When(l.Unlock("id")).ThenReturn(&models.ProjectLock{}, nil)
workingDir := events.NewMockWorkingDir()
workingDirLocker := events.NewDefaultWorkingDirLocker()
workingDirLocker := events.NewDefaultWorkingDirLocker(1)
workspace := "workspace"
path := "path"
projectName := ""
Expand Down
5 changes: 2 additions & 3 deletions server/events/mock_workingdir_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions server/events/project_command_builder_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ projects:
&DefaultProjectFinder{},
vcsClient,
workingDir,
NewDefaultWorkingDirLocker(),
NewDefaultWorkingDirLocker(1),
globalCfg,
&DefaultPendingPlanFinder{},
&CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -873,7 +873,7 @@ projects:
&DefaultProjectFinder{},
vcsClient,
workingDir,
NewDefaultWorkingDirLocker(),
NewDefaultWorkingDirLocker(1),
globalCfg,
&DefaultPendingPlanFinder{},
&CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -1120,7 +1120,7 @@ workflows:
&DefaultProjectFinder{},
vcsClient,
workingDir,
NewDefaultWorkingDirLocker(),
NewDefaultWorkingDirLocker(1),
globalCfg,
&DefaultPendingPlanFinder{},
&CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -1272,7 +1272,7 @@ projects:
&DefaultProjectFinder{},
vcsClient,
workingDir,
NewDefaultWorkingDirLocker(),
NewDefaultWorkingDirLocker(1),
globalCfg,
&DefaultPendingPlanFinder{},
&CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -1414,7 +1414,7 @@ projects:
&DefaultProjectFinder{},
vcsClient,
workingDir,
NewDefaultWorkingDirLocker(),
NewDefaultWorkingDirLocker(1),
globalCfg,
&DefaultPendingPlanFinder{},
&CommentParser{ExecutableName: "atlantis"},
Expand Down
26 changes: 13 additions & 13 deletions server/events/project_command_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ terraform {
&events.DefaultProjectFinder{},
vcsClient,
workingDir,
events.NewDefaultWorkingDirLocker(),
events.NewDefaultWorkingDirLocker(1),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -624,7 +624,7 @@ projects:
&events.DefaultProjectFinder{},
vcsClient,
workingDir,
events.NewDefaultWorkingDirLocker(),
events.NewDefaultWorkingDirLocker(1),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -812,7 +812,7 @@ projects:
&events.DefaultProjectFinder{},
vcsClient,
workingDir,
events.NewDefaultWorkingDirLocker(),
events.NewDefaultWorkingDirLocker(1),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -1141,7 +1141,7 @@ projects:
&events.DefaultProjectFinder{},
vcsClient,
workingDir,
events.NewDefaultWorkingDirLocker(),
events.NewDefaultWorkingDirLocker(1),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -1239,7 +1239,7 @@ func TestDefaultProjectCommandBuilder_BuildMultiApply(t *testing.T) {
&events.DefaultProjectFinder{},
nil,
workingDir,
events.NewDefaultWorkingDirLocker(),
events.NewDefaultWorkingDirLocker(1),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -1325,7 +1325,7 @@ projects:
&events.DefaultProjectFinder{},
nil,
workingDir,
events.NewDefaultWorkingDirLocker(),
events.NewDefaultWorkingDirLocker(1),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -1413,7 +1413,7 @@ func TestDefaultProjectCommandBuilder_EscapeArgs(t *testing.T) {
&events.DefaultProjectFinder{},
vcsClient,
workingDir,
events.NewDefaultWorkingDirLocker(),
events.NewDefaultWorkingDirLocker(1),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -1575,7 +1575,7 @@ projects:
&events.DefaultProjectFinder{},
vcsClient,
workingDir,
events.NewDefaultWorkingDirLocker(),
events.NewDefaultWorkingDirLocker(1),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -1685,7 +1685,7 @@ projects:
&events.DefaultProjectFinder{},
vcsClient,
workingDir,
events.NewDefaultWorkingDirLocker(),
events.NewDefaultWorkingDirLocker(1),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -1754,7 +1754,7 @@ func TestDefaultProjectCommandBuilder_WithPolicyCheckEnabled_BuildAutoplanComman
&events.DefaultProjectFinder{},
vcsClient,
workingDir,
events.NewDefaultWorkingDirLocker(),
events.NewDefaultWorkingDirLocker(1),
globalCfg,
&events.DefaultPendingPlanFinder{},
&events.CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -1842,7 +1842,7 @@ func TestDefaultProjectCommandBuilder_BuildVersionCommand(t *testing.T) {
&events.DefaultProjectFinder{},
nil,
workingDir,
events.NewDefaultWorkingDirLocker(),
events.NewDefaultWorkingDirLocker(1),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -1972,7 +1972,7 @@ func TestDefaultProjectCommandBuilder_BuildPlanCommands_Single_With_RestrictFile
&events.DefaultProjectFinder{},
vcsClient,
workingDir,
events.NewDefaultWorkingDirLocker(),
events.NewDefaultWorkingDirLocker(1),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{ExecutableName: "atlantis"},
Expand Down Expand Up @@ -2083,7 +2083,7 @@ func TestDefaultProjectCommandBuilder_BuildPlanCommands_with_IncludeGitUntracked
&events.DefaultProjectFinder{},
vcsClient,
workingDir,
events.NewDefaultWorkingDirLocker(),
events.NewDefaultWorkingDirLocker(1),
valid.NewGlobalCfgFromArgs(globalCfgArgs),
&events.DefaultPendingPlanFinder{},
&events.CommentParser{ExecutableName: "atlantis"},
Expand Down
18 changes: 9 additions & 9 deletions server/events/project_command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestDefaultProjectCommandRunner_Plan(t *testing.T) {
PullApprovedChecker: nil,
WorkingDir: mockWorkingDir,
Webhooks: nil,
WorkingDirLocker: events.NewDefaultWorkingDirLocker(),
WorkingDirLocker: events.NewDefaultWorkingDirLocker(1),
CommandRequirementHandler: mockCommandRequirementHandler,
}

Expand Down Expand Up @@ -248,7 +248,7 @@ func TestDefaultProjectCommandRunner_ApplyNotApproved(t *testing.T) {
mockWorkingDir := mocks.NewMockWorkingDir()
runner := &events.DefaultProjectCommandRunner{
WorkingDir: mockWorkingDir,
WorkingDirLocker: events.NewDefaultWorkingDirLocker(),
WorkingDirLocker: events.NewDefaultWorkingDirLocker(1),
CommandRequirementHandler: &events.DefaultCommandRequirementHandler{
WorkingDir: mockWorkingDir,
},
Expand All @@ -269,7 +269,7 @@ func TestDefaultProjectCommandRunner_ApplyNotMergeable(t *testing.T) {
mockWorkingDir := mocks.NewMockWorkingDir()
runner := &events.DefaultProjectCommandRunner{
WorkingDir: mockWorkingDir,
WorkingDirLocker: events.NewDefaultWorkingDirLocker(),
WorkingDirLocker: events.NewDefaultWorkingDirLocker(1),
CommandRequirementHandler: &events.DefaultCommandRequirementHandler{
WorkingDir: mockWorkingDir,
},
Expand All @@ -293,7 +293,7 @@ func TestDefaultProjectCommandRunner_ApplyDiverged(t *testing.T) {
mockWorkingDir := mocks.NewMockWorkingDir()
runner := &events.DefaultProjectCommandRunner{
WorkingDir: mockWorkingDir,
WorkingDirLocker: events.NewDefaultWorkingDirLocker(),
WorkingDirLocker: events.NewDefaultWorkingDirLocker(1),
CommandRequirementHandler: &events.DefaultCommandRequirementHandler{
WorkingDir: mockWorkingDir,
},
Expand Down Expand Up @@ -415,7 +415,7 @@ func TestDefaultProjectCommandRunner_Apply(t *testing.T) {
EnvStepRunner: mockEnv,
WorkingDir: mockWorkingDir,
Webhooks: mockSender,
WorkingDirLocker: events.NewDefaultWorkingDirLocker(),
WorkingDirLocker: events.NewDefaultWorkingDirLocker(1),
CommandRequirementHandler: applyReqHandler,
}
repoDir := t.TempDir()
Expand Down Expand Up @@ -496,7 +496,7 @@ func TestDefaultProjectCommandRunner_ApplyRunStepFailure(t *testing.T) {
LockURLGenerator: mockURLGenerator{},
ApplyStepRunner: mockApply,
WorkingDir: mockWorkingDir,
WorkingDirLocker: events.NewDefaultWorkingDirLocker(),
WorkingDirLocker: events.NewDefaultWorkingDirLocker(1),
CommandRequirementHandler: applyReqHandler,
Webhooks: mockSender,
}
Expand Down Expand Up @@ -565,7 +565,7 @@ func TestDefaultProjectCommandRunner_RunEnvSteps(t *testing.T) {
EnvStepRunner: &env,
WorkingDir: mockWorkingDir,
Webhooks: nil,
WorkingDirLocker: events.NewDefaultWorkingDirLocker(),
WorkingDirLocker: events.NewDefaultWorkingDirLocker(1),
CommandRequirementHandler: mockCommandRequirementHandler,
}

Expand Down Expand Up @@ -699,7 +699,7 @@ func TestDefaultProjectCommandRunner_Import(t *testing.T) {
StateRmStepRunner: mockStateRm,
WorkingDir: mockWorkingDir,
Webhooks: mockSender,
WorkingDirLocker: events.NewDefaultWorkingDirLocker(),
WorkingDirLocker: events.NewDefaultWorkingDirLocker(1),
CommandRequirementHandler: applyReqHandler,
}
ctx := command.ProjectContext{
Expand Down Expand Up @@ -1244,7 +1244,7 @@ func TestDefaultProjectCommandRunner_ApprovePolicies(t *testing.T) {
EnvStepRunner: mockEnv,
WorkingDir: mockWorkingDir,
Webhooks: mockSender,
WorkingDirLocker: events.NewDefaultWorkingDirLocker(),
WorkingDirLocker: events.NewDefaultWorkingDirLocker(1),
}
repoDir := t.TempDir()
When(mockWorkingDir.GetWorkingDir(
Expand Down
Loading
Loading