garnet icon indicating copy to clipboard operation
garnet copied to clipboard

Implement CLIENT REPLY ON|OFF|SKIP (#1626)

Open crprashant opened this issue 6 months ago • 0 comments

Fixes #1626

Summary

Implements CLIENT REPLY ON|OFF|SKIP matching Redis semantics:

  • ON (default): replies sent normally.
  • OFF: all replies suppressed until next CLIENT REPLY ON.
  • SKIP: one-shot — suppresses the reply of the next non-CLIENT REPLY command (including unknown / parse-error commands), then auto-returns to ON.

Per Redis behavior, CLIENT REPLY OFF and CLIENT REPLY SKIP themselves return no reply; CLIENT REPLY ON returns +OK.

Implementation

Per-session state lives on RespServerSession:

  • clientReplyMode (On/Off/Skip)
  • suppressCurrentReply (per-iteration flag)
  • cmdReplyFloor (snapshot of dcurr at the start of the current command — the rewind floor under suppression so prior queued replies in the same batch are preserved)

In ProcessMessages, before ParseCommand, we snapshot modeAtStart and cmdReplyFloor = dcurr, then set suppressCurrentReply = (modeAtStart != On). This ensures parse-time errors (e.g. malformed / unknown command) are also gated.

SendAndReset() (suppression path):

  1. If the suppressed write made progress (dcurr > cmdReplyFloor): rewind to cmdReplyFloor — keeps [head, cmdReplyFloor) intact.
  2. Else if prior replies are queued (cmdReplyFloor > head): flush [head, cmdReplyFloor), rotate buffer, set cmdReplyFloor = new head. Suppression continues in the fresh buffer.
  3. Else (no prior bytes, no progress): throw — same fatal "buffer too small" condition as the non-suppressed path. Prevents the infinite-loop scenario where a suppressed reply is larger than the entire response buffer.

SendAndReset(IMemoryOwner<byte>, int) (suppression path): drop the payload outright (do not flush the buffer, which may contain this command's earlier bytes between cmdReplyFloor and dcurr).

End-of-command: if still flagged, dcurr = cmdReplyFloor rewinds the accumulated reply. CLIENT REPLY ON clears the flag inside its own handler so its +OK survives. After dispatch, if mode was Skip and the command wasn't CLIENT REPLY, mode auto-resets to On — this fires for unknown / INVALID commands too, matching Redis.

Files

File Change
libs/server/Resp/Parser/RespCommand.cs CLIENT_REPLY enum + parser case + AofIndependentCommands entry
libs/server/Resp/ClientCommands.cs ClientReplyMode enum + NetworkCLIENTREPLY handler
libs/server/Resp/RespServerSession.cs Per-session state + floor, snapshot/rewind hook in ProcessMessages, suppression-aware SendAndReset overloads
libs/server/Resp/CmdStrings.cs REPLY / SKIP u8 constants
libs/resources/RespCommandsInfo.json, RespCommandsDocs.json CLIENT|REPLY metadata
playground/CommandInfoUpdater/SupportedCommand.cs Subcommand registration
test/Garnet.test/RespCommandTests.cs Added CLIENT_REPLY to AofIndependent list
test/Garnet.test/ClientReplyTests.cs (new) 12 raw-TCP NUnit tests

Testing

  • Build: 0 warnings, 0 errors.
  • New ClientReplyTests: 12/12 pass on both net8.0 and net10.0. Coverage:
    • ClientReplyOnReturnsOK — ON returns +OK.
    • ClientReplyOffSuppressesResponses — OFF gates PING/SET/GET; ON resumes.
    • ClientReplySkipSuppressesOnlyNextCommand — SKIP is one-shot.
    • ClientReplyWrongArityReturnsError — wrong arity returns -ERR… (uses CRLF-aware ReadSimpleReply).
    • ClientReplyInvalidModeReturnsSyntaxError — unknown mode returns -ERR syntax error.
    • ClientReplyCaseInsensitive — lower/mixed case modes accepted.
    • ClientReplyOffPersistsAcrossManyCommands — 50 SETs under OFF, all execute, no reply bytes; verified out-of-band via a second connection.
    • ClientReplyOffErrorAlsoSuppressed — error replies (parse + syntax) are suppressed under OFF.
    • ClientReplySkipDoesNotStack — two consecutive SKIPs only suppress one following command.
    • ClientReplySkipBurnedByUnknownCommand — SKIP followed by unknown command suppresses the error AND burns the SKIP (next command replies normally).
    • ClientReplyPipelinedSkipBetweenReplies — PING / SKIP / PING / PING pipelined in a single TCP write yields exactly two +PONG replies. Verifies the prior reply isn't lost by the in-batch suppression rewind.
    • ClientReplyPipelinedOffOnOrdering — PING / OFF / PING / ON pipelined yields +PONG +OK in order.
  • RespCommandTests class: 49/49 pass (incl. AofIndependentCommandsTest and command info/docs metadata).
  • Broader ~Client filter: 130/131 — single failure (RespRangeIndexTests.RIConcurrentMultiClientTest) reproduces on a clean tree, pre-existing flake unrelated to this change.

Known limitation

Inside MULTI/EXEC, CLIENT REPLY queues through the existing NetworkSKIP path and takes effect at EXEC replay time. This matches Redis semantics for v1.

Pub/sub interaction is not specifically tested in this PR — CLIENT REPLY is gated at the generic response buffer layer, so it applies uniformly to all reply types, but a targeted pub/sub test could be added in a follow-up.

crprashant avatar May 14 '26 15:05 crprashant