Skip to content

Commit 94c27c0

Browse files
committed
switch init-timeout to init-max-failures per review
- rename flag and helper to count-based (int, not duration) - skip --max-failures during init when init-max-failures is set so main-max does not override init tolerance - add SYNC PHASES section to --man and README describing init vs steady-state - update e2e test to use a bad server path, as suggested in review
1 parent 5c9a247 commit 94c27c0

4 files changed

Lines changed: 102 additions & 50 deletions

File tree

‎README.md‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,24 @@ CONTRACT
206206
use as little disk space as possible (see the --depth and --git-gc flags),
207207
but this is not part of the contract.
208208
209+
SYNC PHASES
210+
211+
git-sync operates in two phases:
212+
213+
Initial sync:
214+
git-sync retries until its first successful sync with the remote
215+
repo. During this phase, the retry interval is controlled by
216+
--init-period (falling back to --period if unset) and the failure
217+
limit is controlled by --init-max-failures (falling back to
218+
--max-failures when set to 0). This phase is useful for tolerating
219+
transient connectivity issues at startup while still giving up
220+
eventually.
221+
222+
Steady state:
223+
Once the first sync succeeds, git-sync polls the remote at the
224+
--period interval and tolerates failures up to --max-failures before
225+
aborting. --init-period and --init-max-failures no longer apply.
226+
209227
OPTIONS
210228
211229
Many options can be specified as either a commandline flag or an environment
@@ -365,16 +383,18 @@ OPTIONS
365383
Enable the pprof debug endpoints on git-sync's HTTP endpoint at
366384
/debug/pprof. Requires --http-bind to be specified.
367385
386+
--init-max-failures <int>, $GITSYNC_INIT_MAX_FAILURES
387+
The number of consecutive failures allowed before aborting during
388+
the initial sync phase (before the first successful sync). Once
389+
the initial sync succeeds, --max-failures applies instead. Set
390+
to 0 (the default) to disable this separate limit and fall
391+
through to --max-failures for the entire run.
392+
368393
--init-period <duration>, $GITSYNC_INIT_PERIOD
369394
How long to wait between sync attempts until the first successful
370-
sync. This must be at least 10ms if set. If not specified, the
371-
value of --period is used for initial sync attempts as well.
372-
373-
--init-timeout <duration>, $GITSYNC_INIT_TIMEOUT
374-
The maximum time allowed for the initial sync to succeed. If the
375-
first successful sync does not occur within this duration, git-sync
376-
will exit with an error. If not specified, there is no timeout and
377-
git-sync will retry until --max-failures is reached.
395+
sync. Once the initial sync succeeds, --period is used instead.
396+
This must be at least 10ms if set. If not specified, --period is
397+
used for all sync attempts.
378398
379399
--link <string>, $GITSYNC_LINK
380400
The path to at which to create a symlink which points to the

‎main.go‎

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@ func main() {
183183
flErrorFile := pflag.String("error-file",
184184
envString("", "GITSYNC_ERROR_FILE", "GIT_SYNC_ERROR_FILE"),
185185
"the path (absolute or relative to --root) to an optional file into which errors will be written (defaults to disabled)")
186+
flInitMaxFailures := pflag.Int("init-max-failures",
187+
envInt(0, "GITSYNC_INIT_MAX_FAILURES"),
188+
"the number of consecutive failures allowed before aborting during the initial sync phase; 0 disables the check (falls through to --max-failures)")
186189
flInitPeriod := pflag.Duration("init-period",
187190
envDuration(0, "GITSYNC_INIT_PERIOD"),
188191
"how long to wait between sync attempts until the first success, must be >= 10ms if set; if unset, --period is used")
189-
flInitTimeout := pflag.Duration("init-timeout",
190-
envDuration(0, "GITSYNC_INIT_TIMEOUT"),
191-
"the max time allowed for the initial sync to succeed; if unset, there is no timeout (retries forever until --max-failures)")
192192
flPeriod := pflag.Duration("period",
193193
envDuration(10*time.Second, "GITSYNC_PERIOD", "GIT_SYNC_PERIOD"),
194194
"how long to wait between syncs, must be >= 10ms; --wait overrides this")
@@ -478,8 +478,8 @@ func main() {
478478
if *flInitPeriod != 0 && *flInitPeriod < 10*time.Millisecond {
479479
fatalConfigErrorf(log, true, "invalid flag: --init-period must be at least 10ms")
480480
}
481-
if *flInitTimeout != 0 && *flInitTimeout < 10*time.Millisecond {
482-
fatalConfigErrorf(log, true, "invalid flag: --init-timeout must be at least 10ms")
481+
if *flInitMaxFailures < 0 {
482+
fatalConfigErrorf(log, true, "invalid flag: --init-max-failures must not be negative")
483483
}
484484

485485
if *flDeprecatedChmod != 0 {
@@ -925,22 +925,22 @@ func main() {
925925
failCount := 0
926926
syncCount := uint64(0)
927927
initialSyncDone := false
928-
var initStart time.Time
929-
if *flInitPeriod != 0 || *flInitTimeout != 0 {
930-
initStart = time.Now()
931-
}
932928
for {
933929
start := time.Now()
934930
ctx, cancel := context.WithTimeout(context.Background(), *flSyncTimeout)
935931

936932
if changed, hash, err := git.SyncRepo(ctx, refreshCreds); err != nil {
937933
failCount++
938934
updateSyncMetrics(metricKeyError, start)
939-
if isInitTimedOut(*flInitTimeout, initialSyncDone, initStart) {
940-
log.Error(err, "initial sync timed out", "initTimeout", flInitTimeout.String(), "failCount", failCount)
935+
if isInitFailuresExceeded(*flInitMaxFailures, initialSyncDone, failCount) {
936+
log.Error(err, "too many initial sync failures, aborting", "initMaxFailures", *flInitMaxFailures, "failCount", failCount)
941937
os.Exit(1)
942938
}
943-
if *flMaxFailures >= 0 && failCount >= *flMaxFailures {
939+
// During the initial sync phase, --init-max-failures (if set) is
940+
// the authoritative limit; --max-failures applies once init is
941+
// done, or when --init-max-failures is not set.
942+
useMainLimit := initialSyncDone || *flInitMaxFailures == 0
943+
if useMainLimit && *flMaxFailures >= 0 && failCount >= *flMaxFailures {
944944
// Exit after too many retries, maybe the error is not recoverable.
945945
log.Error(err, "too many failures, aborting", "failCount", failCount)
946946
os.Exit(1)
@@ -1183,12 +1183,14 @@ func chooseWaitTime(period, initPeriod time.Duration, initialSyncDone bool) time
11831183
return period
11841184
}
11851185

1186-
// isInitTimedOut returns true if the initial sync has exceeded the init-timeout.
1187-
func isInitTimedOut(initTimeout time.Duration, initialSyncDone bool, initStart time.Time) bool {
1188-
if initTimeout == 0 || initialSyncDone {
1186+
// isInitFailuresExceeded returns true if the initial sync has failed more than
1187+
// initMaxFailures consecutive times. A non-positive initMaxFailures disables
1188+
// the check.
1189+
func isInitFailuresExceeded(initMaxFailures int, initialSyncDone bool, failCount int) bool {
1190+
if initMaxFailures <= 0 || initialSyncDone {
11891191
return false
11901192
}
1191-
return time.Since(initStart) >= initTimeout
1193+
return failCount >= initMaxFailures
11921194
}
11931195

11941196
// Do no work, but don't do something that triggers go's runtime into thinking
@@ -2444,6 +2446,24 @@ CONTRACT
24442446
use as little disk space as possible (see the --depth and --git-gc flags),
24452447
but this is not part of the contract.
24462448
2449+
SYNC PHASES
2450+
2451+
git-sync operates in two phases:
2452+
2453+
Initial sync:
2454+
git-sync retries until its first successful sync with the remote
2455+
repo. During this phase, the retry interval is controlled by
2456+
--init-period (falling back to --period if unset) and the failure
2457+
limit is controlled by --init-max-failures (falling back to
2458+
--max-failures when set to 0). This phase is useful for tolerating
2459+
transient connectivity issues at startup while still giving up
2460+
eventually.
2461+
2462+
Steady state:
2463+
Once the first sync succeeds, git-sync polls the remote at the
2464+
--period interval and tolerates failures up to --max-failures before
2465+
aborting. --init-period and --init-max-failures no longer apply.
2466+
24472467
OPTIONS
24482468
24492469
Many options can be specified as either a commandline flag or an environment
@@ -2603,18 +2623,19 @@ OPTIONS
26032623
Enable the pprof debug endpoints on git-sync's HTTP endpoint at
26042624
/debug/pprof. Requires --http-bind to be specified.
26052625
2626+
--init-max-failures <int>, $GITSYNC_INIT_MAX_FAILURES
2627+
The number of consecutive failures allowed before aborting during
2628+
the initial sync phase (before the first successful sync). Once
2629+
the initial sync succeeds, --max-failures applies instead. Set
2630+
to 0 (the default) to disable this separate limit and fall
2631+
through to --max-failures for the entire run.
2632+
26062633
--init-period <duration>, $GITSYNC_INIT_PERIOD
26072634
How long to wait between sync attempts until the first successful
26082635
sync. Once the initial sync succeeds, --period is used instead.
26092636
This must be at least 10ms if set. If not specified, --period is
26102637
used for all sync attempts.
26112638
2612-
--init-timeout <duration>, $GITSYNC_INIT_TIMEOUT
2613-
The maximum amount of time to keep retrying the initial sync
2614-
before failing. If not specified, git-sync retries forever (or
2615-
until --max-failures is reached). This must be at least 10ms if
2616-
set.
2617-
26182639
--link <string>, $GITSYNC_LINK
26192640
The path to at which to create a symlink which points to the
26202641
current git directory, at the currently synced hash. This may be

‎main_test.go‎

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -471,43 +471,54 @@ func TestChooseWaitTime(t *testing.T) {
471471
}
472472
}
473473

474-
func TestIsInitTimedOut(t *testing.T) {
474+
func TestIsInitFailuresExceeded(t *testing.T) {
475475
cases := []struct {
476476
name string
477-
initTimeout time.Duration
477+
initMaxFailures int
478478
initialSyncDone bool
479-
elapsed time.Duration
479+
failCount int
480480
expected bool
481481
}{{
482-
name: "no timeout set",
483-
initTimeout: 0,
482+
name: "disabled (zero)",
483+
initMaxFailures: 0,
484484
initialSyncDone: false,
485-
elapsed: time.Hour,
485+
failCount: 100,
486486
expected: false,
487487
}, {
488-
name: "timeout set, already done",
489-
initTimeout: time.Second,
488+
name: "disabled (negative)",
489+
initMaxFailures: -1,
490+
initialSyncDone: false,
491+
failCount: 100,
492+
expected: false,
493+
}, {
494+
name: "limit set, already done",
495+
initMaxFailures: 3,
490496
initialSyncDone: true,
491-
elapsed: time.Hour,
497+
failCount: 100,
492498
expected: false,
493499
}, {
494-
name: "timeout set, not done, not expired",
495-
initTimeout: 10 * time.Second,
500+
name: "limit set, not done, under limit",
501+
initMaxFailures: 5,
496502
initialSyncDone: false,
497-
elapsed: time.Second,
503+
failCount: 3,
498504
expected: false,
499505
}, {
500-
name: "timeout set, not done, expired",
501-
initTimeout: time.Second,
506+
name: "limit set, not done, at limit",
507+
initMaxFailures: 3,
508+
initialSyncDone: false,
509+
failCount: 3,
510+
expected: true,
511+
}, {
512+
name: "limit set, not done, over limit",
513+
initMaxFailures: 3,
502514
initialSyncDone: false,
503-
elapsed: 2 * time.Second,
515+
failCount: 5,
504516
expected: true,
505517
}}
506518

507519
for _, tc := range cases {
508520
t.Run(tc.name, func(t *testing.T) {
509-
initStart := time.Now().Add(-tc.elapsed)
510-
got := isInitTimedOut(tc.initTimeout, tc.initialSyncDone, initStart)
521+
got := isInitFailuresExceeded(tc.initMaxFailures, tc.initialSyncDone, tc.failCount)
511522
if got != tc.expected {
512523
t.Errorf("expected %v, got %v", tc.expected, got)
513524
}

‎test_e2e.sh‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3718,13 +3718,13 @@ function e2e::init_period_faster_initial_sync() {
37183718
}
37193719

37203720
##############################################
3721-
# Test init-timeout exits on timeout
3721+
# Test init-max-failures aborts after N failed attempts
37223722
##############################################
3723-
function e2e::init_timeout_expires() {
3723+
function e2e::init_max_failures_exceeded() {
37243724
assert_fail \
37253725
GIT_SYNC \
37263726
--period=100ms \
3727-
--init-timeout=1s \
3727+
--init-max-failures=3 \
37283728
--max-failures=-1 \
37293729
--repo="file:///does/not/exist" \
37303730
--root="$ROOT" \

0 commit comments

Comments
 (0)