Class SubscriberBufferLimits

java.lang.Object
io.github.flink.gcp.connector.pubsub.source.SubscriberBufferLimits

@Internal public final class SubscriberBufferLimits extends Object
The subscriber-buffer bounds the given options resolve to, and the one relation they must keep with SDK flow control.

Flow control is what holds a backpressured subscriber back. Because the source acknowledges only at checkpoint completion, a subscriber whose downstream drains more slowly than Pub/Sub delivers fills its buffer until the client's flow control stops pulling, which is at most one flow-control window. A bound below that window is therefore crossed by ordinary backpressure, and a job resumed over a backlog crosses it again within seconds of every restart (#2020). The lapse the bounds exist for is larger: once maxAckExtensionPeriod passes for a message nobody is draining, the client releases a whole window of permits per expiry wave, so the first wave carries the buffer to 2 × limit + 1 (measured: a 50-message window stepped to 101). Twice the limit, the lapse bound, is crossed by the lapse and by nothing smaller, without anybody choosing a number.

Why not the limit itself. The buffer is nearly a subset of what the client holds outstanding, but a bound at the limit has one message of headroom, and at least three healthy paths pass it (measured against google-cloud-pubsub 1.152.0 and gax 2.x):

  • a message larger than the byte limit is admitted anyway — gax's BlockingSemaphore.acquirePartial clamps the request to the limit and lets its permits go negative;
  • on a subscription with a dead-letter policy the client adds a googclient_deliveryattempt attribute after reserving, so every buffered message is a few dozen bytes larger than what was reserved for it;
  • a redelivery is buffered beside the copy it supersedes while the acknowledgement tracker nacks the superseded handle, releasing that delivery's permit.

The lapse bound is the paused-split bound's default. An unset reader-wide budget gives every subscriber the reader holds a share of the lapse bound, or of an explicit paused-split bound when that is larger, so that the per-split park the operator configured is reached before the reader-wide response; the share is per subscriber because each has a flow controller of its own, and parallelPullCount does not multiply it, because the client shares one flow controller across a subscriber's streams. An explicit budget is taken as given — it is the operator's number, not a base to scale — and PubSubSubscriberOptions.Builder.build() rejects one below the lapse bound, which no reader can hold under backpressure. A reader that holds several subscribers is only known at run time, so the reader warns there instead.

The SDK's defaults are read from Subscriber.Builder.getDefaultFlowControlSettings() rather than mirrored as constants: they are public, and production code already reads them.

  • Method Details

    • of

      public static SubscriberBufferLimits of(PubSubSubscriberOptions options)
      Resolves the limits the given options ask for.
      Parameters:
      options - the subscriber options
      Returns:
      the resolved limits
    • fixed

      public static SubscriberBufferLimits fixed(long maxMessages, long maxBytes)
      Returns fixed reader-wide limits that no subscriber count scales and no derived budget is compared against.
      Parameters:
      maxMessages - the reader-wide message cap
      maxBytes - the reader-wide byte cap
      Returns:
      the fixed limits
    • maxMessages

      public long maxMessages(int subscribers)
      Returns the message cap for a reader holding the given number of subscribers.
      Parameters:
      subscribers - the subscribers the reader holds; fewer than one counts as one
      Returns:
      the configured cap, or the derived one
    • maxBytes

      public long maxBytes(int subscribers)
      Returns the byte cap for a reader holding the given number of subscribers.
      Parameters:
      subscribers - the subscribers the reader holds; fewer than one counts as one
      Returns:
      the configured cap, or the derived one
    • maxBytes

      public long maxBytes(int subscribers, long largestMessageBytes)
      Returns the byte cap for a reader holding the given number of subscribers that has received a message of the given size.

      The client admits a message larger than its byte limit once its window is free, so a subscriber's window is never smaller than the largest message it was handed; an unset cap counts that message as the window, and an explicit cap is the operator's number either way.

      Parameters:
      subscribers - the subscribers the reader holds; fewer than one counts as one
      largestMessageBytes - the largest serialized message the reader has received
      Returns:
      the configured cap, or the derived one widened to twice that message per subscriber
    • derivedMaxMessages

      public long derivedMaxMessages(int subscribers)
      Returns the message cap an unset budget derives for the given number of subscribers: each subscriber's lapse bound, or its explicit paused-split bound when that is larger.
      Parameters:
      subscribers - the subscribers the reader holds; fewer than one counts as one
      Returns:
      the derived message cap, saturating at Long.MAX_VALUE
    • derivedMaxBytes

      public long derivedMaxBytes(int subscribers)
      Returns the byte cap an unset budget derives for the given number of subscribers, as derivedMaxMessages(int) returns the message cap.
      Parameters:
      subscribers - the subscribers the reader holds; fewer than one counts as one
      Returns:
      the derived byte cap, saturating at Long.MAX_VALUE
    • coversDerived

      public boolean coversDerived(int subscribers)
      Returns whether both caps for the given subscriber count are at least the ones an unset budget would derive, so that ordinary backpressure cannot reach them.
      Parameters:
      subscribers - the subscribers the reader holds
      Returns:
      true when neither cap is below its derived value
    • lapseBoundMessages

      public long lapseBoundMessages()
      Returns one subscriber's lapse bound in messages: twice the effective flow-control message limit, and the paused-split bound's default.
      Returns:
      the message lapse bound
    • lapseBoundBytes

      public long lapseBoundBytes()
      Returns one subscriber's lapse bound in bytes: twice the effective flow-control byte limit, and the paused-split bound's default.
      Returns:
      the byte lapse bound
    • checkCoversFlowControl

      public static void checkCoversFlowControl(@Nullable Long maxMessages, @Nullable Long maxBytes, @Nullable Long flowControlMessages, @Nullable Long flowControlBytes, SubscriberBufferLimits.Names names)
      Rejects an explicit budget below one subscriber's lapse bound, naming the settings with the given names so the builder and the table layer can each speak in their own keys. The SDK's flow-control defaults are read only for a budget that is set.
      Parameters:
      maxMessages - the explicit message budget, or null when it is derived
      maxBytes - the explicit byte budget, or null when it is derived
      flowControlMessages - the flow-control message limit, or null for the SDK's
      flowControlBytes - the flow-control byte limit, or null for the SDK's
      names - what to call the four settings in the rejection
      Throws:
      IllegalArgumentException - if an explicit budget is below one subscriber's lapse bound