Skip to content

A1-2: Define richer DistributedCoordinator interface with cache coordination methods #129

Closed

Description

@scttfrdmn

Context

types.DistributedCoordinator (pkg/types/interfaces.go) has only ExecuteOperation(ctx, op any) (any, error) and GetStats(). That is enough to run one operation on one node and nothing else: there is no way for a node to tell peers it holds a key, to ask which peers hold one, or to tell them a key is stale. The v0.13.0 cache-warming chain (#140, #141, #142, #143) needs all three, and every one of those issues is blocked on the interface rather than on its own logic.

Body revised 2026-08-08 after #284 landed. Two items in the original are gone, and the two comments below are the record of why. Read them if the change of premise matters; the section following is what to build.

What to implement

Extend pkg/types/interfaces.go:

type DistributedCoordinator interface {
    ExecuteOperation(ctx context.Context, op any) (any, error)
    GetStats() map[string]any

    // Cache coordination
    AnnounceKey(ctx context.Context, ann KeyAnnouncement) error
    QueryKeyOwnership(ctx context.Context, key string) ([]KeyAnnouncement, error)
    InvalidateKey(ctx context.Context, key, etag string) error
}

Add to pkg/types:

type KeyAnnouncement struct {
    Key      string    `json:"key"`
    NodeID   string    `json:"node_id"`
    ETag     string    `json:"etag"`
    Size     int64     `json:"size"`
    CachedAt time.Time `json:"cached_at"`
    Offset   int64     `json:"offset"`
    Length   int64     `json:"length"` // 0 = full object
}

JSON tags rather than bare fields: this crosses the gossip wire, where every neighboring field is snake_case. types.Precondition shipped without tags and had to be fixed in #284 before release; do not repeat it.

Update coordinatorWrapper in internal/distributed/cluster.go to satisfy the widened interface, and add var _ types.DistributedCoordinator = (*coordinatorWrapper)(nil).

ConsistencyLevel is not promoted — it is deleted

The original body proposed moving ConsistencyLevel and its three constants into pkg/types, and adding NegotiateConsistency. Do not. #284 deleted the type. All three levels issued the same unconditional PutObject and differed only in how many nodes issued it, so an agreement between two nodes on a level selected the same request from both — there was nothing to negotiate. What replaced it is per-operation and evaluated by the store: DistributedOperation.Precondition, a types.Precondition. A level enum beside that field would be a second encoding of Precondition.IsZero().

The original acceptance criterion "ConsistencyLevel defined in exactly one place" is therefore satisfied by zero places, and #144 (implement NegotiateConsistency) is closed as not planned.

InvalidateKey takes an ETag

Not InvalidateKey(ctx, key). Gossip is unordered and retransmits, so a receiver handed a bare key cannot tell an invalidation for a version it has already moved past from one for the version it holds. This is requirement R4 of docs/design/conditional-writes-vs-raft.md §1.

CacheInvalidateMessage already carries ETag and Deleted, and GossipProtocol.markInvalidationApplied already keeps a bounded set of applied (key, ETag) pairs — #284 built the receiving half. This issue is the interface those messages travel under, so the signature has to admit the version or the plumbing stops at the interface boundary.

Empty ETag stays legal and means "the caller cannot name a version" — a delete, or an unconditional put whose ETag was not read back. Such an invalidation is applied by every receiver every time, which is safe and merely wasteful.

Stubs return ErrNotSupported, never nil

The original said "stub implementations returning nil/empty are acceptable." Tighten that: a stub that returns nil reports success for work it did not do. #284 deleted a CacheReplicator that added bytes to BytesReplicated when gossip was not running and had never sent anything, and the test that let it survive four releases asserted only that its field was non-nil. AnnounceKey and QueryKeyOwnership have no implementation until #140, so they return types.ErrNotSupported and a caller finds out.

InvalidateKey is not a stub — ClusterManager.InvalidateCacheKey(key, etag) already exists and broadcasts, so wire it through.

Acceptance criteria

  • KeyAnnouncement defined in pkg/types with JSON tags, and a test pinning the wire names
  • DistributedCoordinator carries the three cache-coordination methods
  • coordinatorWrapper satisfies it, with the compile-time assertion in cluster.go
  • AnnounceKey and QueryKeyOwnership return types.ErrNotSupported, asserted with errors.Is — not asserted to be non-nil, which is the mistake that hid CacheReplicator
  • InvalidateKey broadcasts a CacheInvalidateMessage whose ETag is the one passed in, asserted by receiving it rather than by inspecting the call
  • ConsistencyLevel is not reintroduced anywhere
  • go build ./..., make lint, and make test pass

Sequencing

Blocks #140 (AnnounceKey/QueryKeyOwnership implementations) and #142 (cache-warming read). #141 needs InvalidateKey on the write path.

Activity

  1. added
    type: enhancement
    priority: critical
    area: distributed
    on Apr 7, 2026
  2. added
    effort: large
    on Aug 3, 2026
  3. scttfrdmn commented on Aug 3, 2026

    @scttfrdmn
    OwnerAuthor

    Heads-up from the CAS decision on #169: part of this issue is now wrong, and the rest survives.

    The Raft build-out (#128, #130, #133, #151) is closed as not-the-direction; per-key S3 conditional
    writes replace it. That has one specific consequence here.

    Drop or revise: this issue proposes promoting ConsistencyLevel and its three constants from
    internal/distributed/coordinator.go into pkg/types:

    type ConsistencyLevel string  // promoted from internal/distributed/coordinator.go
    const (
        ConsistencyEventual ConsistencyLevel = "eventual"
        ConsistencyStrong   ConsistencyLevel = "strong"
        ConsistencySession  ConsistencyLevel = "session"
    )

    NegotiateConsistency goes with it. #284 will delete or redefine that taxonomy, so promoting it into
    the public types package would be moving a construct into a wider blast radius on its way out. Two
    reasons it is going:

    • ConsistencyStrong is mislabeled. executeStrongConsistency fans the same PUT with the same
      bytes to N nodes and calls a majority "strong" — that is a reachability signal, not a consistency
      guarantee.
    • ConsistencySession and ConsistencyEventual are nearly the same function on today's code:
      both execute on targetNodes[0] then replicateAsync the remainder. Session's only
      distinguishing feature is a comment saying it will "try to use the same node for related
      operations," and there is no session state anywhere to make that true.

    Survives unchanged: AnnounceKey, QueryKeyOwnership, InvalidateKey, KeyAnnouncement, the
    coordinatorWrapper implementation, and the compile-time assertion. None of that is consensus work
    — it is cache coordination, and the v0.13.0 warming issues (#140, #141, #142, #143) still need it.

    One addition worth making while here: per §1 (R4) of docs/design/conditional-writes-vs-raft.md, an
    invalidation is only ordered correctly if it carries the ETag it was computed from.
    KeyAnnouncement already has an ETag field, so InvalidateKey should take one too — otherwise a
    receiver cannot discard an invalidation referring to a version it has already moved past.

    No action needed until #284 is scoped; recording it here so this issue is not implemented as written.

  4. scttfrdmn commented on Aug 8, 2026

    @scttfrdmn
    OwnerAuthor

    #284 has landed and two of this issue's items no longer hold. Recording it here rather than editing the body, so the change of premise is visible.

    Drop NegotiateConsistency and the ConsistencyLevel promotion. The type is deleted, not moved. ConsistencyEventual, ConsistencySession and ConsistencyStrong all issued the same unconditional PutObject and differed only in how many nodes issued it, so there was nothing to negotiate — an agreement between two nodes on a level selected the same request from both. What replaced it is per-operation and evaluated by the store: DistributedOperation.Precondition, a types.Precondition. A two-name enum beside it would be a second encoding of Precondition.IsZero().

    So the acceptance criterion "ConsistencyLevel defined in exactly one place (pkg/types)" is satisfied by it being defined in zero places, and internal/distributed/coordinator.go no longer needs to import pkg/types for it (it imports pkg/types for Precondition instead).

    InvalidateKey and AnnounceKey need an ETag in the signature. KeyAnnouncement already has the field; CacheInvalidateMessage (gossip.go) is {Key, From} and carries no version, so a receiver cannot tell an invalidation for a version it has already moved past from one for the version it holds. That is the remaining half of #284's R4 and belongs in whichever of the two lands first — flagging it here because this issue defines the interface those messages travel under.

    AnnounceKey/QueryKeyOwnership are unaffected and are what the cache-warming work (#141) actually needs.

    Suggested revision to the interface:

    type DistributedCoordinator interface {
        ExecuteOperation(ctx context.Context, op interface{}) (interface{}, error)
        GetStats() map[string]interface{}
    
        // Cache coordination
        AnnounceKey(ctx context.Context, ann KeyAnnouncement) error
        QueryKeyOwnership(ctx context.Context, key string) ([]KeyAnnouncement, error)
        InvalidateKey(ctx context.Context, key, etag string) error
    }

    One other thing to check while implementing: coordinatorWrapper must not grow a stub that reports success for work it did not do. The CacheReplicator #284 just deleted counted bytes as replicated when gossip was not running, and the test that let it survive asserted only that its field was non-nil. A stub here should return types.ErrNotSupported, not nil.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: distributedeffort: largepriority: criticaltype: enhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions