package objectgateway import "context" // sendCreate performs exactly one non-retried versioned PUT and then confirms // the result. Versioned object stores do not provide atomic create-if-absent, // so the confirmation re-reads the current version to make sure this request's // version is still the one a reader would observe. func sendCreate(ctx context.Context, b Backend, r Request, body []byte) (Version, error) { v, err := b.Put(ctx, r.Key, body, r.token()) if err != nil { // Unknown outcome: the intent stays unresolved, nothing is retried and // nothing is deleted because no version id is confirmed. return Version{}, ErrHeld } return confirmCreate(ctx, b, r, v) } // confirmCreate binds the receipt to a version that is both the exact version // this request wrote and the current version of the key. It is read-only: the // create intent is already committed, so withdrawing a version here would be an // unrecorded mutation with no delete intent or receipt and would leave the // create intent permanently held. A concurrent writer that produced a newer // current version therefore wins by explicit conflict; both versions stay in // the store and an operator resolves the key with evidence. func confirmCreate(ctx context.Context, b Backend, r Request, v Version) (Version, error) { exact, exists, err := b.Observe(ctx, r.Key, v.ID) if err != nil || !exists || !r.matches(exact) || v != exact { return Version{}, ErrHeld } latest, current, err := b.Observe(ctx, r.Key, "") if err != nil || !current { // Cannot tell whether our write is still current: keep the intent held // rather than mutating anything on a guess. return Version{}, ErrHeld } if latest.ID != v.ID { // Another writer owns the current version. Report the conflict and leave // both versions untouched; this path never merges them automatically. return Version{}, ErrConflict } return v, nil }