Skip to content

Commit

Permalink
Merge branch 'STORM-2850' of https://github.com/srdo/storm into asfgi…
Browse files Browse the repository at this point in the history
…t-master
  • Loading branch information
srdo committed Dec 10, 2017
2 parents 67f13b5 + c71f4c3 commit b246d37
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public void refreshAssignment() {
Collections.sort(allPartitions, TopicPartitionComparator.INSTANCE);
Set<TopicPartition> newAssignment = new HashSet<>(partitioner.partition(allPartitions, context));
if (!newAssignment.equals(currentAssignment)) {
consumer.assign(newAssignment);
if (currentAssignment != null) {
listener.onPartitionsRevoked(currentAssignment);
}
currentAssignment = newAssignment;
consumer.assign(newAssignment);
listener.onPartitionsAssigned(newAssignment);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2017 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.storm.kafka.spout.subscription;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.apache.storm.kafka.spout.config.builder.SingleTopicKafkaSpoutConfiguration;
import org.apache.storm.task.TopologyContext;
import org.junit.Test;
import org.mockito.InOrder;

public class ManualPartitionSubscriptionTest {

@Test
public void testCanReassignPartitions() {
ManualPartitioner partitionerMock = mock(ManualPartitioner.class);
TopicFilter filterMock = mock(TopicFilter.class);
KafkaConsumer<String, String> consumerMock = mock(KafkaConsumer.class);
ConsumerRebalanceListener listenerMock = mock(ConsumerRebalanceListener.class);
TopologyContext contextMock = mock(TopologyContext.class);
ManualPartitionSubscription subscription = new ManualPartitionSubscription(partitionerMock, filterMock);

List<TopicPartition> onePartition = Collections.singletonList(new TopicPartition(SingleTopicKafkaSpoutConfiguration.TOPIC, 0));
List<TopicPartition> twoPartitions = new ArrayList<>();
twoPartitions.add(new TopicPartition(SingleTopicKafkaSpoutConfiguration.TOPIC, 0));
twoPartitions.add(new TopicPartition(SingleTopicKafkaSpoutConfiguration.TOPIC, 1));
when(partitionerMock.partition(anyList(), any(TopologyContext.class)))
.thenReturn(onePartition)
.thenReturn(twoPartitions);

//Set the first assignment
subscription.subscribe(consumerMock, listenerMock, contextMock);

InOrder inOrder = inOrder(consumerMock, listenerMock);
inOrder.verify(consumerMock).assign(new HashSet<>(onePartition));
inOrder.verify(listenerMock).onPartitionsAssigned(new HashSet<>(onePartition));

clearInvocations(consumerMock, listenerMock);

//Update to set the second assignment
subscription.refreshAssignment();

//The partition revocation hook must be called before the new partitions are assigned to the consumer,
//to allow the revocation hook to commit offsets for the revoked partitions.
inOrder.verify(listenerMock).onPartitionsRevoked(new HashSet<>(onePartition));
inOrder.verify(consumerMock).assign(new HashSet<>(twoPartitions));
inOrder.verify(listenerMock).onPartitionsAssigned(new HashSet<>(twoPartitions));
}

}

0 comments on commit b246d37

Please sign in to comment.