Search in sources :

Example 1 with PendingModifyAckDeadline

use of com.google.cloud.pubsub.spi.v1.MessageDispatcher.PendingModifyAckDeadline in project google-cloud-java by GoogleCloudPlatform.

the class StreamingSubscriberConnectionTest method testPartitionAckOperations.

@Test
public void testPartitionAckOperations() {
    List<StreamingPullRequest> requests;
    requests = StreamingSubscriberConnection.partitionAckOperations(Collections.<String>emptyList(), Collections.<PendingModifyAckDeadline>emptyList(), 3);
    Truth.assertThat(requests).isEmpty();
    requests = StreamingSubscriberConnection.partitionAckOperations(Arrays.asList("a", "b", "c"), Collections.<PendingModifyAckDeadline>emptyList(), 3);
    Truth.assertThat(requests).containsExactly(StreamingPullRequest.newBuilder().addAckIds("a").addAckIds("b").addAckIds("c").build()).inOrder();
    requests = StreamingSubscriberConnection.partitionAckOperations(Arrays.asList("a", "b", "c", "d"), Collections.<PendingModifyAckDeadline>emptyList(), 3);
    Truth.assertThat(requests).containsExactly(StreamingPullRequest.newBuilder().addAckIds("a").addAckIds("b").addAckIds("c").build(), StreamingPullRequest.newBuilder().addAckIds("d").build()).inOrder();
    requests = StreamingSubscriberConnection.partitionAckOperations(Arrays.asList("a", "b", "c", "d"), Arrays.asList(new PendingModifyAckDeadline(42, "w")), 3);
    Truth.assertThat(requests).containsExactly(StreamingPullRequest.newBuilder().addAckIds("a").addAckIds("b").addAckIds("c").addModifyDeadlineAckIds("w").addModifyDeadlineSeconds(42).build(), StreamingPullRequest.newBuilder().addAckIds("d").build()).inOrder();
    requests = StreamingSubscriberConnection.partitionAckOperations(Arrays.asList("a"), Arrays.asList(new PendingModifyAckDeadline(42, "w", "x")), 3);
    Truth.assertThat(requests).containsExactly(StreamingPullRequest.newBuilder().addAckIds("a").addModifyDeadlineAckIds("w").addModifyDeadlineSeconds(42).addModifyDeadlineAckIds("x").addModifyDeadlineSeconds(42).build()).inOrder();
    requests = StreamingSubscriberConnection.partitionAckOperations(Arrays.asList("a"), Arrays.asList(new PendingModifyAckDeadline(42, "w", "x"), new PendingModifyAckDeadline(43, "y", "z")), 3);
    Truth.assertThat(requests).containsExactly(StreamingPullRequest.newBuilder().addAckIds("a").addModifyDeadlineAckIds("w").addModifyDeadlineSeconds(42).addModifyDeadlineAckIds("x").addModifyDeadlineSeconds(42).addModifyDeadlineAckIds("y").addModifyDeadlineSeconds(43).build(), StreamingPullRequest.newBuilder().addModifyDeadlineAckIds("z").addModifyDeadlineSeconds(43).build()).inOrder();
}
Also used : StreamingPullRequest(com.google.pubsub.v1.StreamingPullRequest) PendingModifyAckDeadline(com.google.cloud.pubsub.spi.v1.MessageDispatcher.PendingModifyAckDeadline) Test(org.junit.Test)

Example 2 with PendingModifyAckDeadline

use of com.google.cloud.pubsub.spi.v1.MessageDispatcher.PendingModifyAckDeadline in project google-cloud-java by GoogleCloudPlatform.

the class StreamingSubscriberConnection method partitionAckOperations.

@VisibleForTesting
static List<StreamingPullRequest> partitionAckOperations(List<String> acksToSend, List<PendingModifyAckDeadline> ackDeadlineExtensions, int size) {
    int numExtensions = 0;
    for (PendingModifyAckDeadline modify : ackDeadlineExtensions) {
        numExtensions += modify.ackIds.size();
    }
    int numChanges = Math.max(numExtensions, acksToSend.size());
    int numRequests = numChanges / size + (numChanges % size == 0 ? 0 : 1);
    List<StreamingPullRequest.Builder> requests = new ArrayList<>(numRequests);
    for (int i = 0; i < numRequests; i++) {
        requests.add(StreamingPullRequest.newBuilder());
    }
    int reqCount = 0;
    for (List<String> acksChunk : Lists.partition(acksToSend, size)) {
        requests.get(reqCount).addAllAckIds(acksChunk);
        reqCount++;
    }
    reqCount = 0;
    int ackCount = 0;
    for (PendingModifyAckDeadline modify : ackDeadlineExtensions) {
        for (String ackId : modify.ackIds) {
            requests.get(reqCount).addModifyDeadlineSeconds(modify.deadlineExtensionSeconds).addModifyDeadlineAckIds(ackId);
            ackCount++;
            if (ackCount == size) {
                reqCount++;
                ackCount = 0;
            }
        }
    }
    List<StreamingPullRequest> ret = new ArrayList<>(requests.size());
    for (StreamingPullRequest.Builder builder : requests) {
        ret.add(builder.build());
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) StreamingPullRequest(com.google.pubsub.v1.StreamingPullRequest) PendingModifyAckDeadline(com.google.cloud.pubsub.spi.v1.MessageDispatcher.PendingModifyAckDeadline) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

PendingModifyAckDeadline (com.google.cloud.pubsub.spi.v1.MessageDispatcher.PendingModifyAckDeadline)2 StreamingPullRequest (com.google.pubsub.v1.StreamingPullRequest)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1