Skip to content

Commit f0ef1b9

Browse files
committed
reconciler: Add revision to Update and Delete ops
For symmetry with BatchOperations add the object revision to the [Operations.Update] and [Operations.Delete]. Fixes: #56 Signed-off-by: Jussi Maki <[email protected]>
1 parent 3d15207 commit f0ef1b9

File tree

6 files changed

+22
-15
lines changed

6 files changed

+22
-15
lines changed

reconciler/benchmark/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type mockOps struct {
6060
}
6161

6262
// Delete implements reconciler.Operations.
63-
func (mt *mockOps) Delete(ctx context.Context, txn statedb.ReadTxn, obj *testObject) error {
63+
func (mt *mockOps) Delete(ctx context.Context, txn statedb.ReadTxn, rev statedb.Revision, obj *testObject) error {
6464
return nil
6565
}
6666

@@ -70,7 +70,7 @@ func (mt *mockOps) Prune(ctx context.Context, txn statedb.ReadTxn, objects iter.
7070
}
7171

7272
// Update implements reconciler.Operations.
73-
func (mt *mockOps) Update(ctx context.Context, txn statedb.ReadTxn, obj *testObject) error {
73+
func (mt *mockOps) Update(ctx context.Context, txn statedb.ReadTxn, rev statedb.Revision, obj *testObject) error {
7474
mt.numUpdates.Add(1)
7575
return nil
7676
}

reconciler/example/ops.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func NewMemoOps(lc cell.Lifecycle, log *slog.Logger, cfg Config) reconciler.Oper
3535
}
3636

3737
// Delete a memo.
38-
func (ops *MemoOps) Delete(ctx context.Context, txn statedb.ReadTxn, memo *Memo) error {
38+
func (ops *MemoOps) Delete(ctx context.Context, txn statedb.ReadTxn, rev statedb.Revision, memo *Memo) error {
3939
filename := path.Join(ops.directory, memo.Name)
4040
err := os.Remove(filename)
4141
ops.log.Info("Delete", "filename", filename, "error", err)
@@ -76,7 +76,7 @@ func (ops *MemoOps) Prune(ctx context.Context, txn statedb.ReadTxn, objects iter
7676
}
7777

7878
// Update a memo.
79-
func (ops *MemoOps) Update(ctx context.Context, txn statedb.ReadTxn, memo *Memo) error {
79+
func (ops *MemoOps) Update(ctx context.Context, txn statedb.ReadTxn, rev statedb.Revision, memo *Memo) error {
8080
filename := path.Join(ops.directory, memo.Name)
8181
err := os.WriteFile(filename, []byte(memo.Content), 0644)
8282
ops.log.Info("Update", "filename", filename, "error", err)

reconciler/incremental.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (round *incrementalRound[Obj]) processSingle(obj Obj, rev statedb.Revision,
201201
)
202202
if delete {
203203
op = OpDelete
204-
err = round.config.Operations.Delete(round.ctx, round.txn, obj)
204+
err = round.config.Operations.Delete(round.ctx, round.txn, rev, obj)
205205
if err != nil {
206206
// Deletion failed. Retry again later.
207207
round.retries.Add(obj, rev, true, err)
@@ -211,7 +211,7 @@ func (round *incrementalRound[Obj]) processSingle(obj Obj, rev statedb.Revision,
211211
orig := obj
212212
obj = round.config.CloneObject(obj)
213213
op = OpUpdate
214-
err = round.config.Operations.Update(round.ctx, round.txn, obj)
214+
err = round.config.Operations.Update(round.ctx, round.txn, rev, obj)
215215
status := round.config.GetObjectStatus(obj)
216216
round.results[obj] = opResult{original: orig, id: status.ID, rev: rev, err: err}
217217
}

reconciler/multi_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type multiMockOps struct {
4848
}
4949

5050
// Delete implements reconciler.Operations.
51-
func (m *multiMockOps) Delete(context.Context, statedb.ReadTxn, *multiStatusObject) error {
51+
func (m *multiMockOps) Delete(context.Context, statedb.ReadTxn, statedb.Revision, *multiStatusObject) error {
5252
return nil
5353
}
5454

@@ -58,7 +58,7 @@ func (m *multiMockOps) Prune(context.Context, statedb.ReadTxn, iter.Seq2[*multiS
5858
}
5959

6060
// Update implements reconciler.Operations.
61-
func (m *multiMockOps) Update(ctx context.Context, txn statedb.ReadTxn, obj *multiStatusObject) error {
61+
func (m *multiMockOps) Update(ctx context.Context, txn statedb.ReadTxn, rev statedb.Revision, obj *multiStatusObject) error {
6262
m.numUpdates++
6363
if m.faulty.Load() {
6464
return errors.New("fail")

reconciler/script_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,22 +340,26 @@ type mockOps struct {
340340
updates intMap
341341
}
342342

343-
// DeleteBatch implements recogciler.BatchOperations.
343+
// DeleteBatch implements reconciler.BatchOperations.
344344
func (mt *mockOps) DeleteBatch(ctx context.Context, txn statedb.ReadTxn, batch []reconciler.BatchEntry[*testObject]) {
345345
for i := range batch {
346-
batch[i].Result = mt.Delete(ctx, txn, batch[i].Object)
346+
batch[i].Result = mt.Delete(ctx, txn, batch[i].Revision, batch[i].Object)
347347
}
348348
}
349349

350350
// UpdateBatch implements reconciler.BatchOperations.
351351
func (mt *mockOps) UpdateBatch(ctx context.Context, txn statedb.ReadTxn, batch []reconciler.BatchEntry[*testObject]) {
352352
for i := range batch {
353-
batch[i].Result = mt.Update(ctx, txn, batch[i].Object)
353+
batch[i].Result = mt.Update(ctx, txn, batch[i].Revision, batch[i].Object)
354354
}
355355
}
356356

357357
// Delete implements reconciler.Operations.
358-
func (mt *mockOps) Delete(ctx context.Context, txn statedb.ReadTxn, obj *testObject) error {
358+
func (mt *mockOps) Delete(ctx context.Context, txn statedb.ReadTxn, rev statedb.Revision, obj *testObject) error {
359+
if rev == 0 {
360+
panic("BUG: revision must not be 0")
361+
}
362+
359363
if mt.faulty.Load() || obj.Faulty {
360364
mt.history.add(opFail(opDelete(obj.ID)))
361365
return errors.New("delete fail")
@@ -377,7 +381,10 @@ func (mt *mockOps) Prune(ctx context.Context, txn statedb.ReadTxn, objects iter.
377381
}
378382

379383
// Update implements reconciler.Operations.
380-
func (mt *mockOps) Update(ctx context.Context, txn statedb.ReadTxn, obj *testObject) error {
384+
func (mt *mockOps) Update(ctx context.Context, txn statedb.ReadTxn, rev statedb.Revision, obj *testObject) error {
385+
if rev == 0 {
386+
panic("BUG: revision must not be 0")
387+
}
381388
mt.updates.incr(obj.ID)
382389

383390
op := opUpdate(obj.ID)

reconciler/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ type Operations[Obj any] interface {
6565
// The object handed to Update is a clone produced by Config.CloneObject
6666
// and thus Update can mutate the object. The mutations are only guaranteed
6767
// to be retained if the object has a single reconciler (one Status).
68-
Update(ctx context.Context, txn statedb.ReadTxn, obj Obj) error
68+
Update(ctx context.Context, txn statedb.ReadTxn, revision statedb.Revision, obj Obj) error
6969

7070
// Delete the object in the target. Same semantics as with Update.
7171
// Deleting a non-existing object is not an error and returns nil.
72-
Delete(context.Context, statedb.ReadTxn, Obj) error
72+
Delete(context.Context, statedb.ReadTxn, statedb.Revision, Obj) error
7373

7474
// Prune undesired state. It is given an iterator for the full set of
7575
// desired objects. The implementation should diff the desired state against

0 commit comments

Comments
 (0)