Skip to content

Git designed for AI agents.

Merge commit 'e775f84' into c1-conflicts

Federico Elia6903808fbff4
diff --cc sdk/land.go
index 17eab29,f14d6dd..005700c
--- a/sdk/land.go
+++ b/sdk/land.go
@@@ -54,16 -56,106 +56,114 @@@ type LandOptions struct
// configured conflict resolver (see doc/graft/RESOLVE.md). A resolved
// landing is a merge commit of the branch tip and the change. If the
// resolver is disabled, refuses a file or fails, the landing fails with a
- // merge conflict as without this option.
+ // merge conflict as without this option, and the error carries the
+ // conflict package (see LandConflict).
ResolveConflicts bool
+ // Resolution lands Commit with the caller's resolution of the conflicts of
+ // a conflict package (see LandConflict and ResolutionFor), as a merge
+ // commit of the branch tip and the change. The server accepts it only when
+ // it was made against the current tip and resolves exactly the conflicting
+ // paths; otherwise Land fails with a fresh package. The request must fit in
+ // MaxLandRequestBytes.
+ Resolution *Resolution
+ // RetryUnavailable is how Land retries a call that the server answers
+ // with Unavailable (see UnavailableRetry). It is on by default for
+ // LandMerge and LandFastForwardOnly, and off for LandRebase.
+ RetryUnavailable UnavailableRetry
+ }
+
+ // UnavailableRetry is the client side of the server's contract for
+ // Unavailable: a node that cannot serve a land right now (its storage core
+ // sync ran out of sync_timeout, its land lane was closed) answers Unavailable,
+ // and the client retries later (doc/graft/LAND-BATCHING.md, Limits). Land
+ // retries only codes.Unavailable, never another code, with an exponential
+ // backoff with full jitter: before retry n it waits a random time between zero
+ // and min(Cap, Base x 2^(n-1)). A context that ends stops the waiting.
+ //
+ // The response of a call can be lost after the server landed the change. With
+ // LandMerge and LandFastForwardOnly, Commit is then reachable from the tip, and
+ // the retry is reported as AlreadyLanded: these lands are retried by default.
+ // With LandRebase, ResolveConflicts included (a land without conflicts is a
+ // plain rebase), the branch holds a rebased copy, not Commit itself, and the
+ // server cannot tell yet that the change landed: the retry replays the change
+ // onto the tip. Every commit of it comes out empty and is dropped, unless a
+ // later commit reverted the change in between, for example graft-verify
+ // reverting the culprit: the retry would then land the change again. Rebase
+ // lands are therefore not retried unless RebaseLands is set by a caller that
+ // accepts this. A retried land with ResolveConflicts may run the conflict
+ // resolver again, a model included, for the same conflict; LandResult reports
+ // the retries (UnavailableRetries), so that this cost is visible.
+ type UnavailableRetry struct {
+ // Disabled turns the retries off: Land returns Unavailable at once.
+ Disabled bool
+ // RebaseLands also retries LandRebase lands, which a retry after a lost
+ // response may land again when the change was reverted in between.
+ RebaseLands bool
+ // MaxRetries bounds the retries after the first call. Zero means 5.
+ MaxRetries int
+ // Base is the backoff of the first retry. Zero means 100ms.
+ Base time.Duration
+ // Cap bounds the backoff of every retry. Zero means 2s.
+ Cap time.Duration
+ }
+
+ // UnavailableRetriesError is the error of a land that failed after the SDK
+ // retried Unavailable answers: the error of the last call, or of the context
+ // that ended while Land waited to retry. It carries the status of that error,
+ // so Code, LandError and the Is* helpers read it as they read the error itself.
+ type UnavailableRetriesError struct {
+ // Retries is the number of Unavailable answers that were retried.
+ Retries int
+ // LastUnavailable is the message of the last of them.
+ LastUnavailable string
+ // Err is the error the land failed with.
+ Err error
+ }
+
+ func (e *UnavailableRetriesError) Error() string {
+ return fmt.Sprintf("%v (after %d retries of Unavailable, the last: %s)", e.Err, e.Retries, e.LastUnavailable)
+ }
+
+ // Unwrap returns the error the land failed with.
+ func (e *UnavailableRetriesError) Unwrap() error { return e.Err }
+
+ // GRPCStatus returns the status of the error the land failed with.
+ func (e *UnavailableRetriesError) GRPCStatus() *status.Status { return status.Convert(e.Err) }
+
+ // Defaults of UnavailableRetry.
+ const (
+ defaultUnavailableRetries = 5
+ defaultUnavailableBase = 100 * time.Millisecond
+ defaultUnavailableCap = 2 * time.Second
+ )
+
+ // backoff returns the random wait before retry n (n >= 1).
+ func (r UnavailableRetry) backoff(n int) time.Duration {
+ base, maxBackoff := r.Base, r.Cap
+ if base <= 0 {
+ base = defaultUnavailableBase
+ }
+ if maxBackoff <= 0 {
+ maxBackoff = defaultUnavailableCap
+ }
+ limit := base
+ for i := 1; i < n && limit < maxBackoff; i++ {
+ limit *= 2
+ }
+ limit = min(limit, maxBackoff)
+ return time.Duration(rand.Int64N(int64(limit) + 1))
+ }
+
+ // maxRetries returns the retries of a land with the given strategy.
+ func (r UnavailableRetry) maxRetries(strategy LandStrategy) int {
+ switch {
+ case r.Disabled, strategy == LandRebase && !r.RebaseLands:
+ return 0
+ case r.MaxRetries > 0:
+ return r.MaxRetries
+ default:
+ return defaultUnavailableRetries
+ }
}
// LandResult describes the outcome of a successful Land call.
@@@ -108,9 -206,12 +214,13 @@@ func oldTip(oid string) string
// concurrent updates, so many writers can land changes on the same branch
// without a merge queue.
//
+ // An Unavailable answer is retried as opts.RetryUnavailable says. A land that
+ // fails after retries returns an *UnavailableRetriesError, with the retries and
+ // the status of the last error.
+ //
// Failures carry structured details: use IsMergeConflict, IsNotAncestor,
-// IsReferenceUpdateConflict, IsRejectedByHook or LandError.
+// IsReferenceUpdateConflict, IsRejectedByHook or LandError, and LandConflict
+// for the conflict package of a land that asked for resolution.
func (r *Repo) Land(ctx context.Context, opts LandOptions) (*LandResult, error) {
switch {
case trimBranchPrefix(opts.Branch) == "":
@@@ -135,15 -238,33 +247,39 @@@
if !opts.Timestamp.IsZero() {
req.Timestamp = timestamppb.New(opts.Timestamp)
}
+ if opts.Resolution != nil {
+ req.Resolution = opts.Resolution.toProto()
+ if err := checkLandRequestSize(req); err != nil {
+ return nil, err
+ }
+ }
- resp, err := r.client.operations.UserLand(ctx, req)
+ var (
+ resp *gitalypb.UserLandResponse
+ err error
+ retries int
+ lastUnavailable string
+ )
+ for {
+ resp, err = r.client.operations.UserLand(ctx, req)
+ if status.Code(err) != codes.Unavailable || retries >= opts.RetryUnavailable.maxRetries(opts.Strategy) {
+ break
+ }
+ retries++
+ lastUnavailable = status.Convert(err).Message()
+
+ timer := time.NewTimer(opts.RetryUnavailable.backoff(retries))
+ select {
+ case <-ctx.Done():
+ timer.Stop()
+ return nil, &UnavailableRetriesError{Retries: retries, LastUnavailable: lastUnavailable, Err: status.FromContextError(ctx.Err()).Err()}
+ case <-timer.C:
+ }
+ }
if err != nil {
+ if retries > 0 {
+ return nil, &UnavailableRetriesError{Retries: retries, LastUnavailable: lastUnavailable, Err: err}
+ }
return nil, err
}