From f771a24288e47efbcd5a0daab002147530004932 Mon Sep 17 00:00:00 2001 From: jpappa200 Date: Wed, 8 Feb 2023 14:26:50 -0500 Subject: [PATCH 1/8] added routines to find and remove old git lock files --- cache-config/t3c-apply/util/gitutil.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/cache-config/t3c-apply/util/gitutil.go b/cache-config/t3c-apply/util/gitutil.go index 3706c3be2a..a84371c281 100644 --- a/cache-config/t3c-apply/util/gitutil.go +++ b/cache-config/t3c-apply/util/gitutil.go @@ -24,6 +24,7 @@ import ( "errors" "fmt" "io/ioutil" + "os" "os/exec" "strconv" "strings" @@ -201,3 +202,28 @@ func makeGitCommitMsg(cfg config.Cfg, now time.Time, self bool, success bool) st const sep = " " return strings.Join([]string{appStr, selfStr, modeStr, successStr, timeStr}, sep) } + +const gitLock = "/.git/index.lock" + +func IsGitLockFileOld(cfg config.Cfg, now time.Time, maxAge time.Duration) (bool, error) { + + lockFile := cfg.TsConfigDir + gitLock + oldLock := maxAge * time.Minute + lockFileInfo, err := os.Stat(lockFile) + if err != nil { + return false, fmt.Errorf("stat returned error: %v on file %v", err, lockFile) + } + if diff := now.Sub(lockFileInfo.ModTime()); diff > oldLock { + return true, nil + } + return false, nil +} + +func RemoveGitLock(cfg config.Cfg) error { + lockFile := cfg.TsConfigDir + gitLock + err := os.Remove(lockFile) + if err != nil { + return fmt.Errorf("error removing file: %v, %v", lockFile, err.Error()) + } + return nil +} From de08b0f09664504e7b65d63ffa6d91d38f96f7ec Mon Sep 17 00:00:00 2001 From: jpappa200 Date: Wed, 8 Feb 2023 14:28:24 -0500 Subject: [PATCH 2/8] updated to find and remove a git lock file older than 5 minutes. --- cache-config/t3c-apply/t3c-apply.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cache-config/t3c-apply/t3c-apply.go b/cache-config/t3c-apply/t3c-apply.go index 353e24a864..3925c75d2c 100644 --- a/cache-config/t3c-apply/t3c-apply.go +++ b/cache-config/t3c-apply/t3c-apply.go @@ -146,6 +146,19 @@ func Main() int { } if cfg.UseGit == config.UseGitYes || cfg.UseGit == config.UseGitAuto { + //need to see if there is an old lock file laying around. + const gitMaxLockAge = 5 + oldLock, err := util.IsGitLockFileOld(cfg, time.Now(), gitMaxLockAge) + if err != nil { + log.Errorln("checking for git lock file: " + err.Error()) + } + if oldLock { + log.Errorf("removing git lock file older than %dm", gitMaxLockAge) + err := util.RemoveGitLock(cfg) + if err != nil { + log.Errorf("couldn't remove git lock file: %v", err.Error()) + } + } // commit anything someone else changed when we weren't looking, // with a keyword indicating it wasn't our change if err := util.MakeGitCommitAll(cfg, util.GitChangeNotSelf, true); err != nil { From 316e84144a3b0f6da3de2abeefe4c9e5d3a95047 Mon Sep 17 00:00:00 2001 From: jpappa200 Date: Wed, 8 Feb 2023 14:58:25 -0500 Subject: [PATCH 3/8] added CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4205c4f8b1..c12dcd8740 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - [#7204](https://github.com/apache/trafficcontrol/pull/7204) *Traffic Control Cache Config (t3c)* strategies.yaml hash_key only for consistent_hash - [#7277](https://github.com/apache/trafficcontrol/pull/7277) *Traffic Control Cache Config (t3c)* remapdotconfig: remove skip check at mids for nocache/live - [#7282](https://github.com/apache/trafficcontrol/pull/7282) *Traffic Ops* Fixed issue with user getting correctly logged when using an access or bearer token authentication. +- [#7346](https://github.com/apache/trafficcontrol/pull/7346) *Traffic Control Cache Config (t3c)* Fixed issue with stale lock file when using git to track changes. ## [7.0.0] - 2022-07-19 ### Added From 578ca6c4b1826d76978844c428dbeccc37a362dd Mon Sep 17 00:00:00 2001 From: jpappa200 Date: Wed, 8 Feb 2023 15:20:30 -0500 Subject: [PATCH 4/8] using filepath instead of string concatenation --- cache-config/t3c-apply/util/gitutil.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cache-config/t3c-apply/util/gitutil.go b/cache-config/t3c-apply/util/gitutil.go index a84371c281..e0905cdb3a 100644 --- a/cache-config/t3c-apply/util/gitutil.go +++ b/cache-config/t3c-apply/util/gitutil.go @@ -26,6 +26,7 @@ import ( "io/ioutil" "os" "os/exec" + "path/filepath" "strconv" "strings" "time" @@ -203,11 +204,11 @@ func makeGitCommitMsg(cfg config.Cfg, now time.Time, self bool, success bool) st return strings.Join([]string{appStr, selfStr, modeStr, successStr, timeStr}, sep) } -const gitLock = "/.git/index.lock" +const gitLock = ".git/index.lock" func IsGitLockFileOld(cfg config.Cfg, now time.Time, maxAge time.Duration) (bool, error) { - lockFile := cfg.TsConfigDir + gitLock + lockFile := filepath.Join(cfg.TsConfigDir, gitLock) oldLock := maxAge * time.Minute lockFileInfo, err := os.Stat(lockFile) if err != nil { @@ -220,7 +221,7 @@ func IsGitLockFileOld(cfg config.Cfg, now time.Time, maxAge time.Duration) (bool } func RemoveGitLock(cfg config.Cfg) error { - lockFile := cfg.TsConfigDir + gitLock + lockFile := filepath.Join(cfg.TsConfigDir, gitLock) err := os.Remove(lockFile) if err != nil { return fmt.Errorf("error removing file: %v, %v", lockFile, err.Error()) From 353669f12cac00d7dfa56421a6cc9071515778a3 Mon Sep 17 00:00:00 2001 From: jpappa200 Date: Fri, 10 Feb 2023 09:14:32 -0500 Subject: [PATCH 5/8] added comment and time units to variable name --- cache-config/t3c-apply/t3c-apply.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cache-config/t3c-apply/t3c-apply.go b/cache-config/t3c-apply/t3c-apply.go index 3925c75d2c..e1d554a991 100644 --- a/cache-config/t3c-apply/t3c-apply.go +++ b/cache-config/t3c-apply/t3c-apply.go @@ -147,13 +147,14 @@ func Main() int { if cfg.UseGit == config.UseGitYes || cfg.UseGit == config.UseGitAuto { //need to see if there is an old lock file laying around. - const gitMaxLockAge = 5 - oldLock, err := util.IsGitLockFileOld(cfg, time.Now(), gitMaxLockAge) + //older than 5 minutes + const gitMaxLockAgeMinutes = 5 + oldLock, err := util.IsGitLockFileOld(cfg, time.Now(), gitMaxLockAgeMinutes) if err != nil { log.Errorln("checking for git lock file: " + err.Error()) } if oldLock { - log.Errorf("removing git lock file older than %dm", gitMaxLockAge) + log.Errorf("removing git lock file older than %dm", gitMaxLockAgeMinutes) err := util.RemoveGitLock(cfg) if err != nil { log.Errorf("couldn't remove git lock file: %v", err.Error()) From 528b06795f92bcc56164c63bba0b2bb2f8b0937a Mon Sep 17 00:00:00 2001 From: jpappa200 Date: Fri, 10 Feb 2023 12:01:29 -0500 Subject: [PATCH 6/8] go routine gets file path instead of whole config --- cache-config/t3c-apply/util/gitutil.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/cache-config/t3c-apply/util/gitutil.go b/cache-config/t3c-apply/util/gitutil.go index e0905cdb3a..33d05b0529 100644 --- a/cache-config/t3c-apply/util/gitutil.go +++ b/cache-config/t3c-apply/util/gitutil.go @@ -26,7 +26,6 @@ import ( "io/ioutil" "os" "os/exec" - "path/filepath" "strconv" "strings" "time" @@ -204,24 +203,18 @@ func makeGitCommitMsg(cfg config.Cfg, now time.Time, self bool, success bool) st return strings.Join([]string{appStr, selfStr, modeStr, successStr, timeStr}, sep) } -const gitLock = ".git/index.lock" - -func IsGitLockFileOld(cfg config.Cfg, now time.Time, maxAge time.Duration) (bool, error) { - - lockFile := filepath.Join(cfg.TsConfigDir, gitLock) - oldLock := maxAge * time.Minute +func IsGitLockFileOld(lockFile string, now time.Time, maxAge time.Duration) (bool, error) { lockFileInfo, err := os.Stat(lockFile) if err != nil { return false, fmt.Errorf("stat returned error: %v on file %v", err, lockFile) } - if diff := now.Sub(lockFileInfo.ModTime()); diff > oldLock { + if diff := now.Sub(lockFileInfo.ModTime()); diff > maxAge { return true, nil } return false, nil } -func RemoveGitLock(cfg config.Cfg) error { - lockFile := filepath.Join(cfg.TsConfigDir, gitLock) +func RemoveGitLock(lockFile string) error { err := os.Remove(lockFile) if err != nil { return fmt.Errorf("error removing file: %v, %v", lockFile, err.Error()) From 76658df29dbba6e183fe7c169156436cd1eb1950 Mon Sep 17 00:00:00 2001 From: jpappa200 Date: Fri, 10 Feb 2023 12:02:21 -0500 Subject: [PATCH 7/8] pass file path not config --- cache-config/t3c-apply/t3c-apply.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cache-config/t3c-apply/t3c-apply.go b/cache-config/t3c-apply/t3c-apply.go index e1d554a991..5008837a21 100644 --- a/cache-config/t3c-apply/t3c-apply.go +++ b/cache-config/t3c-apply/t3c-apply.go @@ -149,13 +149,15 @@ func Main() int { //need to see if there is an old lock file laying around. //older than 5 minutes const gitMaxLockAgeMinutes = 5 - oldLock, err := util.IsGitLockFileOld(cfg, time.Now(), gitMaxLockAgeMinutes) + const gitLock = ".git/index.lock" + gitLockFile := filepath.Join(cfg.TsConfigDir, gitLock) + oldLock, err := util.IsGitLockFileOld(gitLockFile, time.Now(), gitMaxLockAgeMinutes * time.Minute) if err != nil { log.Errorln("checking for git lock file: " + err.Error()) } if oldLock { log.Errorf("removing git lock file older than %dm", gitMaxLockAgeMinutes) - err := util.RemoveGitLock(cfg) + err := util.RemoveGitLock(gitLockFile) if err != nil { log.Errorf("couldn't remove git lock file: %v", err.Error()) } From 3afd237ab7bb31aede3f82ad9c34f8ad61a34737 Mon Sep 17 00:00:00 2001 From: jpappa200 Date: Fri, 10 Feb 2023 12:06:52 -0500 Subject: [PATCH 8/8] fixed formatting issue --- cache-config/t3c-apply/t3c-apply.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cache-config/t3c-apply/t3c-apply.go b/cache-config/t3c-apply/t3c-apply.go index 5008837a21..959d9f4a58 100644 --- a/cache-config/t3c-apply/t3c-apply.go +++ b/cache-config/t3c-apply/t3c-apply.go @@ -151,7 +151,7 @@ func Main() int { const gitMaxLockAgeMinutes = 5 const gitLock = ".git/index.lock" gitLockFile := filepath.Join(cfg.TsConfigDir, gitLock) - oldLock, err := util.IsGitLockFileOld(gitLockFile, time.Now(), gitMaxLockAgeMinutes * time.Minute) + oldLock, err := util.IsGitLockFileOld(gitLockFile, time.Now(), gitMaxLockAgeMinutes*time.Minute) if err != nil { log.Errorln("checking for git lock file: " + err.Error()) }