Skip to content

BatchSettings.builder().build() encourages failure by making required arguments optional #2542

Closed

Description

@kir-titievsky

Current pubsub Publisher settings seem to require an instance of BatchingSettings with multiple values set to non-null values. However, the builder pattern encourages setting a single setting at a time. For example, I wanted to make sure that messages were batched into 100 messages batches. So I did this:

publisher = Publisher.defaultBuilder(topicName).setBatchingSettings( BatchingSettings.newBuilder().setElementCountThreshold(100L).build()).build();
which fails with a NullPointerException .
Turns out I must set all other values of BatchingSettings to set one since the call above produces an object like this:
BatchingSettings{elementCountThreshold=100, requestByteThreshold=null, delayThreshold=null, isEnabled=true, flowControlSettings=FlowControlSettings{maxOutstandingElementCount=null, maxOutstandingRequestBytes=null, limitExceededBehavior=Ignore}}

Which took me a good 20 minutes to figure out.

What I wish we did here was an API that required all required parameters, like so (for example):
BatchingSettings.newBuilder().setThresholds(elementCount, requestByte, delay)

Activity

  1. garrettjonesgoogle commented on Oct 20, 2017

    @garrettjonesgoogle
    Contributor

    What about giving everything defaults, so that it's ok if you only set one value?

  2. added
    api: pubsubIssues related to the Pub/Sub API.
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Oct 23, 2017
  3. pongad commented on Oct 24, 2017

    @pongad
    Contributor

    Proposal:
    I think we should fix this by documentation.

    1. Give error message so at least the user knows what we're complaining about.

    2. Expose either DEFAULT_BATCHING_SETTINGS or defaultBatchingSettings(), and recommend users to call builder.setBatchingSettings(DEFAULT_BATCHING_SETTINGS.toBuilder().setElementCountThreshold(100L).build()). This call will work and keep all other parameters default.

    WDYT?

  4. garrettjonesgoogle commented on Oct 26, 2017

    @garrettjonesgoogle
    Contributor

    I think we should just set defaults in newBuilder().

  5. pongad commented on Oct 27, 2017

    @pongad
    Contributor

    Note that there are two defaults at play here. One is BatchingSettings.DEFAULT and another is Subscriber.DEFAULT_BATCHING_SETTINGS.

    I think Subscriber.DEFAULT_BATCHING_SETTINGS can stay as is.

    BatchingSettings.DEFAULT defaulted to null for "never trigger". Eg, if you set element count to null, batch will only trigger on number of bytes and duration. What should default be here, Long.MAX?

  6. garrettjonesgoogle commented on Oct 27, 2017

    @garrettjonesgoogle
    Contributor

    I think the default for BatchingSettings should be the values for no batching (send one at a time).

  7. kir-titievsky commented on Oct 27, 2017

    @kir-titievsky
    Author
  8. added a commit that references this issue on Oct 31, 2017
    b6da833
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: pubsubIssues related to the Pub/Sub API.priority: p2Moderately-important priority. Fix may not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions