-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-4914: Partition re-assignment tool should check types before pe… #2708
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
ijuma
merged 12 commits into
apache:trunk
from
nicktrav:nickt.reassign-partitions-validation
Mar 31, 2018
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
97c0a4d
KAFKA-4914: Partition re-assignment tool should check types before pe…
f51e498
[feedback] Pattern matching
7b05f21
[feedback] Relocate POJOs and update documentation
13583dc
[feedback] JsonTest companion object
5aa7f4b
[feedback] parseAs
8a1c7f2
Use PartitionAssignment/ReplicaAssignment in ReassignPartitionsZNode
ijuma 2f36f0b
[rebase fixups]
ec7881b
[feedback]
fae8341
[fixup] Encode bytes as UTF-8
5b2c54c
[fixup] Add missing license
ec7f36d
[fixup] Revert unnecessary newline removal
019ab70
Minor fixes
ijuma 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
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
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 |
|---|---|---|
|
|
@@ -18,15 +18,22 @@ package kafka.utils | |
|
|
||
| import java.nio.charset.StandardCharsets | ||
|
|
||
| import org.junit.Assert._ | ||
| import org.junit.Test | ||
| import com.fasterxml.jackson.annotation.JsonProperty | ||
| import com.fasterxml.jackson.core.JsonParseException | ||
| import com.fasterxml.jackson.databind.JsonNode | ||
| import com.fasterxml.jackson.databind.node._ | ||
| import kafka.utils.JsonTest.TestObject | ||
| import kafka.utils.json.JsonValue | ||
| import org.junit.Assert._ | ||
| import org.junit.Test | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
| import scala.collection.Map | ||
|
|
||
| object JsonTest { | ||
| case class TestObject(@JsonProperty("foo") foo: String, @JsonProperty("bar") bar: Int) | ||
| } | ||
|
|
||
| class JsonTest { | ||
|
|
||
| @Test | ||
|
|
@@ -125,5 +132,23 @@ class JsonTest { | |
| assertEquals(""""str1\\,str2"""", new String(Json.encodeAsBytes("""str1\,str2"""), StandardCharsets.UTF_8)) | ||
| assertEquals(""""\"quoted\""""", new String(Json.encodeAsBytes(""""quoted""""), StandardCharsets.UTF_8)) | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| def testParseTo() = { | ||
| val foo = "baz" | ||
| val bar = 1 | ||
|
|
||
| val result = Json.parseStringAs[TestObject](s"""{"foo": "$foo", "bar": $bar}""") | ||
|
|
||
| assertTrue(result.isRight) | ||
| assertEquals(TestObject(foo, bar), result.right.get) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can also have a test where we can roundtrip (serialize and then deserialize)? It would be nice to have a method in |
||
| } | ||
|
|
||
| @Test | ||
| def testParseTo_invalidJson() = { | ||
| val result = Json.parseStringAs[TestObject]("{invalid json}") | ||
|
|
||
| assertTrue(result.isLeft) | ||
| assertEquals(classOf[JsonParseException], result.left.get.getClass) | ||
| } | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
core/src/test/scala/unit/kafka/zk/ReassignPartitionsZNodeTest.scala
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,61 @@ | ||
| /** | ||
| * 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 unit.kafka.zk | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException | ||
| import kafka.zk.ReassignPartitionsZNode | ||
| import org.apache.kafka.common.TopicPartition | ||
| import org.junit.Assert._ | ||
| import org.junit.Test | ||
|
|
||
| class ReassignPartitionsZNodeTest { | ||
|
|
||
| val topic = "foo" | ||
| val partition1 = 0 | ||
| val replica1 = 1 | ||
| val replica2 = 2 | ||
|
|
||
| private val reassignPartitionData = Map( | ||
| new TopicPartition(topic, partition1) -> Seq(replica1, replica2)) | ||
| private val reassignmentJson = "{\"version\":1,\"partitions\":[{\"topic\":\"foo\",\"partition\":0,\"replicas\":[1,2]}]}" | ||
|
|
||
| @Test | ||
| def testEncode() { | ||
| val encodedJsonString = ReassignPartitionsZNode.encode(reassignPartitionData) | ||
| .map(_.toChar) | ||
| .mkString | ||
|
|
||
| assertEquals(reassignmentJson, encodedJsonString) | ||
| } | ||
|
|
||
| @Test | ||
| def testDecodeInvalidJson() { | ||
| val result = ReassignPartitionsZNode.decode("invalid json".getBytes) | ||
|
|
||
| assertTrue(result.isLeft) | ||
| assertTrue(result.left.get.isInstanceOf[JsonProcessingException]) | ||
| } | ||
|
|
||
| @Test | ||
| def testDecodeValidJson() { | ||
| val result = ReassignPartitionsZNode.decode(reassignmentJson.getBytes) | ||
|
|
||
| assertTrue(result.isRight) | ||
| val assignmentMap = result.right.get | ||
| assertEquals(Seq(replica1, replica2), assignmentMap(new TopicPartition(topic, partition1))) | ||
| } | ||
| } |
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.
Hmm, just a very basic question: why are you using here Either/Left/Right instead of Try/Success/Failure?
As from my previous experiences we used Try when we wanted to propagate the exception.
And please not I don't mind using Either, just asking for reasons. :)
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.
My (idiomatic) Scala isn't great, but this came at the suggestion of @ijuma (above):
#2708 (comment)
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.
Trydoesn't let you specify an exception subclass and behaves a bit different if an exception is thrown duringmap, etc.