Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Produce message with key to allow ordered message delivery across clu…
…sters (#55) Currently ProduceService produces message without key to the specific partition. Messages to the same partition will have incremental sequence number in the payload to allow ConsumeService to detect message loss and out-of-order message delivery. However, when we monitor a pipeline of clusters connected by MirrorMaker, the message to the same partition in the destination cluster may not have incremental sequence number since MM produces message without key. This patch addresses the problem by producing message with key that will map to the given partition using the specified partitioner class. The partitioner class needs to be the same partitioner used by MM. It is configured as org.apache.kafka.clients.producer.internals.DefaultPartitioner by default.
- Loading branch information
1 parent
b13b43c
commit 1ebe99c
Showing
8 changed files
with
98 additions
and
8 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
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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/linkedin/kmf/partitioner/KMPartitioner.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Copyright 2016 LinkedIn Corp. 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. | ||
*/ | ||
package com.linkedin.kmf.partitioner; | ||
|
||
public interface KMPartitioner { | ||
|
||
int partition(String key, int numPartitions); | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/linkedin/kmf/partitioner/NewKMPartitioner.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright 2016 LinkedIn Corp. 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. | ||
*/ | ||
package com.linkedin.kmf.partitioner; | ||
|
||
import static org.apache.kafka.common.utils.Utils.murmur2; | ||
|
||
public class NewKMPartitioner implements KMPartitioner { | ||
|
||
public int partition(String key, int partitionNum) { | ||
byte[] keyBytes = key.getBytes(); | ||
return toPositive(murmur2(keyBytes)) % partitionNum; | ||
} | ||
|
||
private static int toPositive(int number) { | ||
return number & 0x7fffffff; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/linkedin/kmf/partitioner/OldKMPartitioner.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Copyright 2016 LinkedIn Corp. 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. | ||
*/ | ||
package com.linkedin.kmf.partitioner; | ||
|
||
public class OldKMPartitioner implements KMPartitioner { | ||
|
||
public int partition(String key, int partitionNum) { | ||
return Math.abs(key.hashCode()) % partitionNum; | ||
} | ||
} |
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