Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 1024: Message Router should take
numPartitions
as parameter i…
…n choosing partition (#1025) * Expose partition number in choosePartition method * Use `ThreadLocalRandom`
- Loading branch information
Showing
11 changed files
with
363 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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 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
33 changes: 33 additions & 0 deletions
33
pulsar-client/src/main/java/org/apache/pulsar/client/api/TopicMetadata.java
This file contains 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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,33 @@ | |||
/** | |||
* 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.pulsar.client.api; | |||
|
|||
/** | |||
* Metadata of a topic that can be used for message routing. | |||
*/ | |||
public interface TopicMetadata { | |||
|
|||
/** | |||
* Return the number of partitions per topic. | |||
* | |||
* @return the number of partitions per topic. | |||
*/ | |||
int numPartitions(); | |||
|
|||
} |
This file contains 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 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 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
35 changes: 35 additions & 0 deletions
35
pulsar-client/src/main/java/org/apache/pulsar/client/impl/TopicMetadataImpl.java
This file contains 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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,35 @@ | |||
/** | |||
* 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.pulsar.client.impl; | |||
|
|||
import org.apache.pulsar.client.api.TopicMetadata; | |||
|
|||
class TopicMetadataImpl implements TopicMetadata { | |||
|
|||
private final int numPartitions; | |||
|
|||
TopicMetadataImpl(int numPartitions) { | |||
this.numPartitions = numPartitions; | |||
} | |||
|
|||
@Override | |||
public int numPartitions() { | |||
return numPartitions; | |||
} | |||
} |
57 changes: 57 additions & 0 deletions
57
pulsar-client/src/test/java/org/apache/pulsar/client/api/MessageRouterTest.java
This file contains 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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,57 @@ | |||
/** | |||
* 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.pulsar.client.api; | |||
|
|||
import static org.mockito.Matchers.eq; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.spy; | |||
import static org.mockito.Mockito.times; | |||
import static org.mockito.Mockito.verify; | |||
import static org.testng.Assert.assertEquals; | |||
|
|||
import org.testng.annotations.Test; | |||
|
|||
/** | |||
* Unit test of {@link MessageRouter}. | |||
*/ | |||
public class MessageRouterTest { | |||
|
|||
@SuppressWarnings("deprecation") | |||
private static class TestMessageRouter implements MessageRouter { | |||
|
|||
@Override | |||
public int choosePartition(Message msg) { | |||
return 1234; | |||
} | |||
} | |||
|
|||
@SuppressWarnings("deprecation") | |||
@Test | |||
public void testChoosePartition() { | |||
MessageRouter router = spy(new TestMessageRouter()); | |||
Message mockedMsg = mock(Message.class); | |||
TopicMetadata mockedMetadata = mock(TopicMetadata.class); | |||
|
|||
assertEquals(1234, router.choosePartition(mockedMsg)); | |||
assertEquals(1234, router.choosePartition(mockedMsg, mockedMetadata)); | |||
|
|||
verify(router, times(2)).choosePartition(eq(mockedMsg)); | |||
} | |||
|
|||
} |
Oops, something went wrong.