All Known Implementing Classes:
InMemoryTokenStore, JdbcTokenStore, JpaTokenStore

public interface TokenStore
Describes a component capable of storing and retrieving event tracking tokens.

A StreamingEventProcessor that is tracking an event stream can use the store to keep track of its position in the event stream. Tokens are stored by process name and segment index, enabling the same processor to be distributed over multiple processes or machines.

A token store is always part of a larger unit of work: every operation takes the ProcessingContext it is part of, which must not be null. Operations that consist of several statements, or that are combined by a caller (such as splitting or merging segments), rely on that context for atomicity.

Implementations that persist tokens in a database require the context to carry a transaction. The JpaTokenStore fails with a TokenStoreTransactionRequiredException when its entity manager is not part of a transaction.

Since:
3.0.0
Author:
Allard Buijze, Rene de Waele
  • Method Details

    • initializeTokenSegments

      CompletableFuture<List<Segment>> initializeTokenSegments(String processorName, int segmentCount, @Nullable TrackingToken initialToken, ProcessingContext context)
      Initializes a given number of segments for the given processorName to track its tokens.

      Returns a CompletableFuture that completes when the segments were successfully initialized. The returned future will complete exceptionally with an UnableToClaimTokenException when a segment to be initialized already exists.

      This method should only be invoked when no tokens have been stored for the given processor, yet.

      This method will store the given initialToken for all segments as the starting point for the processor with the given processorName, but not claim them. It will create the segments ranging from 0 until segmentCount - 1.

      The exact behavior when this method is called while tokens were already present is undefined in case the token already present is not owned by the initializing process.

      Parameters:
      processorName - the name of the processor to initialize segments for
      segmentCount - the number of segments to initialize
      initialToken - the initial token which is used as a starting point for the processor
      context - the processing context to use when initializing the segments, cannot be null
      Returns:
      a CompletableFuture that completes when the segments have been initialized
      Throws:
      NullPointerException - if context is null
    • storeToken

      CompletableFuture<Void> storeToken(@Nullable TrackingToken token, String processorName, int segmentId, ProcessingContext context)
      Stores the given token in the store.

      Returns a CompletableFuture that completes when the token was successfully stored. The returned future will complete exceptionally with an UnableToClaimTokenException if the token was not initialized, or was claimed by another process.

      The token marks the current position of the process with given processorName and segment. The given token may be null. Any claims made by the current process have their timestamp updated.

      Parameters:
      token - the token to store for a given process and segment, may be null
      processorName - the name of the process for which to store the token
      segmentId - the index of the segment for which to store the token
      context - the current ProcessingContext, cannot be null
      Returns:
      a CompletableFuture that completes when the token has been stored
      Throws:
      NullPointerException - if context is null
    • fetchToken

      CompletableFuture<TrackingToken> fetchToken(String processorName, int segmentId, ProcessingContext context)
      Fetches the last stored token for the given processorName and segmentId.

      Returns a CompletableFuture that completes with the fetched token, or null if the token was null. The returned future will complete exceptionally with an UnableToClaimTokenException if the token did not exist or was claimed by another process.

      The token will be claimed by the current process (JVM instance), preventing access by other instances. To release the claim, use releaseClaim(String, int, ProcessingContext)

      Parameters:
      processorName - the process name for which to fetch the token
      segmentId - the segment index for which to fetch the token
      context - the current ProcessingContext, cannot be null
      Returns:
      a CompletableFuture with the last stored TrackingToken or null if the store holds no token for the given process and segment
      Throws:
      NullPointerException - if context is null
    • fetchToken

      default CompletableFuture<TrackingToken> fetchToken(String processorName, Segment segment, ProcessingContext context)
      Fetches the last stored token for the given processorName and segment.

      Returns a CompletableFuture that completes with the fetched token, or null if the token was null. The returned future will complete exceptionally with an UnableToClaimTokenException if the token did not exist or was claimed by another process.

      The token will be claimed by the current process (JVM instance), preventing access by other instances. To release the claim, use releaseClaim(String, int, ProcessingContext)

      Parameters:
      processorName - the process name for which to fetch the token
      segment - the segment for which to fetch the token
      context - the current ProcessingContext, cannot be null
      Returns:
      a CompletableFuture with the last stored token or null if the store holds no token for the given process and segment
      Throws:
      NullPointerException - if context is null
    • extendClaim

      default CompletableFuture<Void> extendClaim(String processorName, int segmentId, ProcessingContext context)
      Extends the claim on the current token held by this node for the given processorName and segment.

      Returns a CompletableFuture that completes when the claim has been successfully extended. The returned future will complete exceptionally with an UnableToClaimTokenException if the token did not exist or was claimed by another process.

      Parameters:
      processorName - the process name for which to fetch the token
      segmentId - the segment index for which to fetch the token
      context - the current ProcessingContext, cannot be null
      Returns:
      a CompletableFuture that completes when the claim-extension has been completed
      Throws:
      NullPointerException - if context is null
      Implementation Requirements:
      By default, this method invokes fetchToken(String, int, ProcessingContext), which also extends the claim if the token is held. Token store implementations may choose to implement this method if they can provide a more efficient way of extending this claim.
    • releaseClaim

      CompletableFuture<Void> releaseClaim(String processorName, int segmentId, ProcessingContext context)
      Release a claim of the token for given processorName and segment. If no such claim existed, nothing happens.

      The caller must ensure not to use any streams opened based on the token for which the claim is released.

      Parameters:
      processorName - the name of the process owning the token (e.g. a PooledStreamingEventProcessor name)
      segmentId - the segment for which a token was obtained
      context - the current ProcessingContext, cannot be null
      Returns:
      a CompletableFuture that completes when the claim-release has been completed
      Throws:
      NullPointerException - if context is null
    • initializeSegment

      CompletableFuture<Void> initializeSegment(@Nullable TrackingToken token, String processorName, Segment segment, ProcessingContext context)
      Initializes a segment with given segment for the processor with given processorName to contain the given token.

      Returns a CompletableFuture that completes when the token segment has been successfully initialized. The returned future will complete exceptionally with an UnableToInitializeTokenException if the token already exists.

      This method fails if a token already exists for the given processor and segment, even if that token has been claimed by the active instance. This method will not claim the initialized segment. Use fetchToken(String, int, ProcessingContext) to retrieve and claim the token.

      Parameters:
      token - the token to initialize the segment with
      processorName - the name of the processor to create the segment for
      segment - the segment to initialize
      context - the current ProcessingContext, cannot be null
      Returns:
      a CompletableFuture that completes when the segment has been initialized
      Throws:
      UnsupportedOperationException - if this implementation does not support explicit initialization
      NullPointerException - if context is null
    • deleteToken

      CompletableFuture<Void> deleteToken(String processorName, int segmentId, ProcessingContext context)
      Deletes the token associated with the specified processorName and segmentId.

      Returns a CompletableFuture that completes when the token has been successfully deleted. The returned future will complete exceptionally with an UnableToClaimTokenException if the token is not currently claimed by this node.

      The token must be owned by the current process (JVM instance) to be able to delete it.

      Parameters:
      processorName - the name of the processor to remove the token for
      segmentId - the segment to delete
      context - the current ProcessingContext, cannot be null
      Returns:
      a CompletableFuture that completes when the token deletion is complete
      Throws:
      UnsupportedOperationException - if this operation is not supported by this implementation
      NullPointerException - if context is null
    • fetchSegment

      CompletableFuture<Segment> fetchSegment(String processorName, int segmentId, ProcessingContext context)
      Returns a CompletableFuture that supplies the specified Segment, or null if there was no such segment.
      Parameters:
      processorName - the process name for which to fetch the segment
      segmentId - the segment index to fetch
      context - the current ProcessingContext, cannot be null
      Returns:
      a CompletableFuture with the segment, or null on completion
      Throws:
      NullPointerException - if context is null
    • fetchSegments

      CompletableFuture<List<Segment>> fetchSegments(String processorName, ProcessingContext context)
      Returns a CompletableFuture that supplies a list of known segments for a given processorName on completion.

      The segments returned are segments for which a token has been stored previously. When the TokenStore is empty, the CompletableFuture will return an empty array.

      Parameters:
      processorName - the process name for which to fetch the segments
      context - the current ProcessingContext, cannot be null
      Returns:
      a CompletableFuture with a list of segments on completion
      Throws:
      NullPointerException - if context is null
    • fetchAvailableSegments

      CompletableFuture<List<Segment>> fetchAvailableSegments(String processorName, ProcessingContext context)
      Returns a CompletableFuture supplying a List of known available Segments for a given processorName.

      A segment is considered available if it is not claimed by any other event processor.

      The segments returned are segments for which a token has been stored previously and have not been claimed by another processor. When the TokenStore is empty, an empty list is returned.

      Parameters:
      processorName - the processor's name for which to fetch the segments
      context - the current ProcessingContext, cannot be null
      Returns:
      a List of available Segments for the specified processorName
      Throws:
      NullPointerException - if context is null
    • retrieveStorageIdentifier

      CompletableFuture<String> retrieveStorageIdentifier(ProcessingContext context)
      Retrieves the storage identifier associated with this store. The returned identifier uniquely identifies the storage location for the tokens in this store.

      Returns a CompletableFuture that completes with the storage identifier associated with this store. The returned future will complete exceptionally with an UnableToRetrieveIdentifierException if the identifier could not be retrieved.

      Two token store implementations that share state must return the same identifier. Two token store implementations that do not share a location must return a different identifier (or an empty optional if identifiers are not supported).

      Note that this method may require the implementation to consult its underlying storage. Therefore, the given context must carry a transaction, similarly to invocations like fetchToken(String, int, ProcessingContext), fetchSegments(String, ProcessingContext), etc.

      Parameters:
      context - the current ProcessingContext, cannot be null
      Returns:
      a CompletableFuture that provides an identifier to uniquely identify the storage location of tokens in this TokenStore on completion
      Throws:
      NullPointerException - if context is null