-
Notifications
You must be signed in to change notification settings - Fork 4.1k
STORM-2087 1.x #1826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
STORM-2087 1.x #1826
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
245 changes: 245 additions & 0 deletions
245
...rm-kafka-client/src/test/java/org/apache/storm/kafka/spout/SingleTopicKafkaSpoutTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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; | ||
|
|
||
| import info.batey.kafka.unit.KafkaUnitRule; | ||
| import kafka.producer.KeyedMessage; | ||
| import org.apache.kafka.clients.consumer.OffsetAndMetadata; | ||
| import org.apache.storm.kafka.spout.builders.SingleTopicKafkaSpoutConfiguration; | ||
| import org.apache.storm.spout.SpoutOutputCollector; | ||
| import org.apache.storm.task.TopologyContext; | ||
| import org.apache.storm.tuple.Values; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
|
|
||
| import static org.junit.Assert.*; | ||
|
|
||
| import java.util.Map; | ||
| import static org.mockito.Mockito.*; | ||
| import static org.apache.storm.kafka.spout.builders.SingleTopicKafkaSpoutConfiguration.*; | ||
|
|
||
| public class SingleTopicKafkaSpoutTest { | ||
|
|
||
| private class SpoutContext { | ||
| public KafkaSpout<String, String> spout; | ||
| public SpoutOutputCollector collector; | ||
|
|
||
| public SpoutContext(KafkaSpout<String, String> spout, | ||
| SpoutOutputCollector collector) { | ||
| this.spout = spout; | ||
| this.collector = collector; | ||
| } | ||
| } | ||
|
|
||
| @Rule | ||
| public KafkaUnitRule kafkaUnitRule = new KafkaUnitRule(); | ||
|
|
||
| void populateTopicData(String topicName, int msgCount) { | ||
| kafkaUnitRule.getKafkaUnit().createTopic(topicName); | ||
|
|
||
| for (int i = 0; i < msgCount; i++){ | ||
| KeyedMessage<String, String> keyedMessage = new KeyedMessage<>( | ||
| topicName, Integer.toString(i), | ||
| Integer.toString(i)); | ||
|
|
||
| kafkaUnitRule.getKafkaUnit().sendMessages(keyedMessage); | ||
| }; | ||
| } | ||
|
|
||
| SpoutContext initializeSpout(int msgCount) { | ||
| populateTopicData(SingleTopicKafkaSpoutConfiguration.TOPIC, msgCount); | ||
| int kafkaPort = kafkaUnitRule.getKafkaPort(); | ||
|
|
||
| TopologyContext topology = mock(TopologyContext.class); | ||
| SpoutOutputCollector collector = mock(SpoutOutputCollector.class); | ||
| Map conf = mock(Map.class); | ||
|
|
||
| KafkaSpout<String, String> spout = new KafkaSpout<>(getKafkaSpoutConfig(getKafkaSpoutStreams(), kafkaPort)); | ||
| spout.open(conf, topology, collector); | ||
| spout.activate(); | ||
| return new SpoutContext(spout, collector); | ||
| } | ||
| /* | ||
| * Asserts that the next possible offset to commit or the committed offset is the provided offset. | ||
| * An offset that is ready to be committed is not guarenteed to be already committed. | ||
| */ | ||
| private void assertOffsetCommitted(int offset, KafkaSpout.OffsetEntry entry) { | ||
|
|
||
| boolean currentOffsetMatch = entry.getCommittedOffset() == offset; | ||
| OffsetAndMetadata nextOffset = entry.findNextCommitOffset(); | ||
| boolean nextOffsetMatch = nextOffset != null && nextOffset.offset() == offset; | ||
| assertTrue("Next offset: " + | ||
| entry.findNextCommitOffset() + | ||
| " OR current offset: " + | ||
| entry.getCommittedOffset() + | ||
| " must equal desired offset: " + | ||
| offset, | ||
| currentOffsetMatch | nextOffsetMatch); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldContinueWithSlowDoubleAcks() throws Exception { | ||
| int messageCount = 20; | ||
| SpoutContext context = initializeSpout(messageCount); | ||
|
|
||
| //play 1st tuple | ||
| ArgumentCaptor<Object> messageIdToDoubleAck = ArgumentCaptor.forClass(Object.class); | ||
| context.spout.nextTuple(); | ||
| verify(context.collector).emit(anyString(), anyList(), messageIdToDoubleAck.capture()); | ||
| context.spout.ack(messageIdToDoubleAck.getValue()); | ||
|
|
||
| for (int i = 0; i < messageCount/2; i++) { | ||
| context.spout.nextTuple(); | ||
| }; | ||
|
|
||
| context.spout.ack(messageIdToDoubleAck.getValue()); | ||
|
|
||
| for (int i = 0; i < messageCount; i++) { | ||
| context.spout.nextTuple(); | ||
| }; | ||
|
|
||
| ArgumentCaptor<Object> remainingIds = ArgumentCaptor.forClass(Object.class); | ||
|
|
||
| verify(context.collector, times(messageCount)).emit( | ||
| eq(SingleTopicKafkaSpoutConfiguration.STREAM), | ||
| anyList(), | ||
| remainingIds.capture()); | ||
| for (Object id : remainingIds.getAllValues()) { | ||
| context.spout.ack(id); | ||
| } | ||
|
|
||
| for(Object item : context.spout.acked.values()) { | ||
| assertOffsetCommitted(messageCount - 1, (KafkaSpout.OffsetEntry) item); | ||
| }; | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldEmitAllMessages() throws Exception { | ||
| int messageCount = 10; | ||
| SpoutContext context = initializeSpout(messageCount); | ||
|
|
||
|
|
||
| for (int i = 0; i < messageCount; i++) { | ||
| context.spout.nextTuple(); | ||
| ArgumentCaptor<Object> messageId = ArgumentCaptor.forClass(Object.class); | ||
| verify(context.collector).emit( | ||
| eq(SingleTopicKafkaSpoutConfiguration.STREAM), | ||
| eq(new Values(SingleTopicKafkaSpoutConfiguration.TOPIC, | ||
| Integer.toString(i), | ||
| Integer.toString(i))), | ||
| messageId.capture()); | ||
| context.spout.ack(messageId.getValue()); | ||
| reset(context.collector); | ||
| }; | ||
|
|
||
| for (Object item : context.spout.acked.values()) { | ||
| assertOffsetCommitted(messageCount - 1, (KafkaSpout.OffsetEntry) item); | ||
| }; | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReplayInOrderFailedMessages() throws Exception { | ||
| int messageCount = 10; | ||
| SpoutContext context = initializeSpout(messageCount); | ||
|
|
||
| //play and ack 1 tuple | ||
| ArgumentCaptor<Object> messageIdAcked = ArgumentCaptor.forClass(Object.class); | ||
| context.spout.nextTuple(); | ||
| verify(context.collector).emit(anyString(), anyList(), messageIdAcked.capture()); | ||
| context.spout.ack(messageIdAcked.getValue()); | ||
| reset(context.collector); | ||
|
|
||
| //play and fail 1 tuple | ||
| ArgumentCaptor<Object> messageIdFailed = ArgumentCaptor.forClass(Object.class); | ||
| context.spout.nextTuple(); | ||
| verify(context.collector).emit(anyString(), anyList(), messageIdFailed.capture()); | ||
| context.spout.fail(messageIdFailed.getValue()); | ||
| reset(context.collector); | ||
|
|
||
| //pause so that failed tuples will be retried | ||
| Thread.sleep(200); | ||
|
|
||
|
|
||
| //allow for some calls to nextTuple() to fail to emit a tuple | ||
| for (int i = 0; i < messageCount + 5; i++) { | ||
| context.spout.nextTuple(); | ||
| }; | ||
|
|
||
| ArgumentCaptor<Object> remainingMessageIds = ArgumentCaptor.forClass(Object.class); | ||
|
|
||
| //1 message replayed, messageCount - 2 messages emitted for the first time | ||
| verify(context.collector, times(messageCount - 1)).emit( | ||
| eq(SingleTopicKafkaSpoutConfiguration.STREAM), | ||
| anyList(), | ||
| remainingMessageIds.capture()); | ||
| for (Object id : remainingMessageIds.getAllValues()) { | ||
| context.spout.ack(id); | ||
| } | ||
|
|
||
| for (Object item : context.spout.acked.values()) { | ||
| assertOffsetCommitted(messageCount - 1, (KafkaSpout.OffsetEntry) item); | ||
| }; | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReplayFirstTupleFailedOutOfOrder() throws Exception { | ||
| int messageCount = 10; | ||
| SpoutContext context = initializeSpout(messageCount); | ||
|
|
||
|
|
||
| //play 1st tuple | ||
| ArgumentCaptor<Object> messageIdToFail = ArgumentCaptor.forClass(Object.class); | ||
| context.spout.nextTuple(); | ||
| verify(context.collector).emit(anyString(), anyList(), messageIdToFail.capture()); | ||
| reset(context.collector); | ||
|
|
||
| //play 2nd tuple | ||
| ArgumentCaptor<Object> messageIdToAck = ArgumentCaptor.forClass(Object.class); | ||
| context.spout.nextTuple(); | ||
| verify(context.collector).emit(anyString(), anyList(), messageIdToAck.capture()); | ||
| reset(context.collector); | ||
|
|
||
| //ack 2nd tuple | ||
| context.spout.ack(messageIdToAck.getValue()); | ||
| //fail 1st tuple | ||
| context.spout.fail(messageIdToFail.getValue()); | ||
|
|
||
| //pause so that failed tuples will be retried | ||
| Thread.sleep(200); | ||
|
|
||
| //allow for some calls to nextTuple() to fail to emit a tuple | ||
| for (int i = 0; i < messageCount + 5; i++) { | ||
| context.spout.nextTuple(); | ||
| }; | ||
|
|
||
| ArgumentCaptor<Object> remainingIds = ArgumentCaptor.forClass(Object.class); | ||
| //1 message replayed, messageCount - 2 messages emitted for the first time | ||
| verify(context.collector, times(messageCount - 1)).emit( | ||
| eq(SingleTopicKafkaSpoutConfiguration.STREAM), | ||
| anyList(), | ||
| remainingIds.capture()); | ||
| for (Object id : remainingIds.getAllValues()) { | ||
| context.spout.ack(id); | ||
| }; | ||
|
|
||
| for (Object item : context.spout.acked.values()) { | ||
| assertOffsetCommitted(messageCount - 1, (KafkaSpout.OffsetEntry) item); | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is for testing purposes, right? There are ways for testing frameworks to access private fields, if really necessary. I am in favor of not break encapsulation and expose internal state for the purposes of testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree to some extent, though this also needs to be fixed on master if we change it here. Breaking encapsulation with package private fields is not a big issue IMO since it doesn't change the public API. Also it probably wouldn't hurt to split some of the code into smaller packages, which limits visibility.
I'd really like to avoid using reflection if possible. It's brittle and prevents the IDE from helping you when you're refactoring (renaming a field for example).
How would you feel about adding acked to the testing constructor and injecting it from the tests instead? That way the tests still have access to it without the field being package private. We could also pretty easily move OffsetEntry out into the spout.internal package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hmcl (sorry, forgot to ping you on the other comment) The same applies to Timer btw, thinking of your comment about the spout code getting a little big. We could easily move that to the internal package too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@srdo using reflection for tests only wouldn't look to concerning to me, but I also agree with your point of not being able to do find usages in the IDE. I would also say that if the tests and the source class are correctly implemented, the tests should never need to query the internal state to assert of the correctness of the code. If one has to do that, that to me feels like something is not as good as it could be.
In my opinion package private methods have a very specific use, which is for the construction of libraries. Although they are not part of the public API, they are easy to misuse by clients who may want to take shortcuts. With this said, I think that having a package where we put some of the nested classes would make the code significantly more modular. I really think that it is getting to the point where I start seeing
if / elseblocks all over the place, added toinstance of... and I really want to avoid that at all costs... otherwise soon we wont' have a hand on this.However extracting the nested classes will also add an extra complexity, because there is a significant amount of state that the
OffsetEntryclass uses that would have to be passed around if it becomes it's own class. I still thing we should do it, though. When we do it should be in such a way that we can plugin a different implementation ofOffsetEntry. Perhaps also rename this class, as it is really anOffsetManager. What do you think ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree that we should move OffsetEntry out of KafkaSpout. I also agree that we should be able to test the spout without peeking directly into the spout's fields.
Here's what I suggest:
We move Timer out into a separate class, and use a TimerFactory to create instances in the KafkaSpout (basically the same solution as we use for KafkaConsumer mocking on master). That way we can write tests where time is simulated, which is helpful if we want to test that the spout commits offsets correctly (or any other periodic action).
We move and rename OffsetEntry out into a new class. It looks like the only part of KafkaSpout state it reads is
numUncommittedOffsetsfromOffsetEntry.commit(). Since that method returns void, we can solve that problem very easily by returningnumCommittedOffsetsfrom there. If we want to support other OffsetEntry implementations we can just set up a factory for that too, but I think that can be postponed until we actually need to support multiple implementations.We rewrite the offending tests here to use a mocked KafkaConsumer and KafkaTimer instead of KafkaUnit, and we make the tests check that the offsets are committed on the consumer instead of directly reading
KafkaSpout.acked. I think KafkaUnit is a good idea for integration tests, but I don't think it fits that well for the tests in this PR.I'll try implementing this on master, if it works out I'll open a PR there first. We can port the improvements to 1.x once we agree on the solution on master.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hmcl The changes are here #1832. I'd like to merge this branch, then we can backport the refactoring in a separate PR once it has been merged to master.