Class RespTransactionHandler


public class RespTransactionHandler extends CacheRespRequestHandler
Handles the commands while in the transaction state.

The transaction state is entered when the MULTI command is received and exited when the EXEC or DISCARD command is received. Additional commands that change the handler include QUIT and the subscribe commands.

The handler will queue all commands and copy the arguments received. Observe that, this information is kept in-memory, so it will not survive restarts. If the node stops in some way and come back, the queued commands are lost. In the same way, it does not offer atomicity during execution. If the server stops mid-way applying operations, it does not recover, leaving a half-applied transaction.

Talking in isolation levels, this implementation is next to read-uncommitted. Another client could see the state of the transaction before it is finished, even on a single node.

Author:
José Bolina
See Also:
  • Constructor Details

    • RespTransactionHandler

      public RespTransactionHandler(RespServer respServer, AdvancedCache<byte[],byte[]> cache)
  • Method Details

    • actualHandleRequest

      protected CompletionStage<RespRequestHandler> actualHandleRequest(io.netty.channel.ChannelHandlerContext ctx, RespCommand command, List<byte[]> arguments)
      Description copied from class: RespRequestHandler
      Handles the RESP request returning a stage that when complete notifies the command has completed as well as providing the request handler for subsequent commands.

      Implementations should never use the ByteBufAllocator in the context. Instead, they should use RespRequestHandler.writer to retrieve a ResponseWriter This ByteBuffer should only have bytes written to it adding up to the size requested. The ByteBuffer itself should never be written to the context or channel and flush should also never be invoked. Failure to do so may cause mis-ordering or responses as requests support pipelining and a ByteBuf may not be sent down stream until later in the pipeline.

      Overrides:
      actualHandleRequest in class RespRequestHandler
      Parameters:
      ctx - Netty context pipeline for this request
      command - The command type
      arguments - The remaining arguments to the command
      Returns:
      stage that when complete returns the new handler to instate. This stage must be completed on the event loop
    • commandNotFound

      protected void commandNotFound()
      Overrides:
      commandNotFound in class RespRequestHandler
    • handleChannelDisconnect

      public void handleChannelDisconnect(io.netty.channel.ChannelHandlerContext ctx)
      Overrides:
      handleChannelDisconnect in class RespRequestHandler
    • errorInTransactionContext

      public void errorInTransactionContext()
    • hasFailed

      public boolean hasFailed()
    • dropTransaction

      public CompletionStage<?> dropTransaction(io.netty.channel.ChannelHandlerContext ctx)
    • performingOperations

      public CompletionStage<List<TransactionCommand>> performingOperations(io.netty.channel.ChannelHandlerContext ctx)
      Prepare for performing the queued operations.

      This method verifies for possible watched keys to verify if the operation needs to be aborted. A null return means the transaction needs to be aborted. After this execution, all keys are un-watched, removing the listeners from the cache.

      Returns:
      The list of commands to be executed, or null if the transaction needs to be aborted.
    • unregisterListeners

      public CompletionStage<List<WATCH.TxKeysListener>> unregisterListeners(io.netty.channel.ChannelHandlerContext ctx)