Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@

import rx.functions.Func1;

import java.nio.ByteBuffer;

public interface RouterFactory {
public <T> Router<T> scalarStageToStageRouter(String name, final Func1<T, byte[]> toBytes);

public default <K, V> Router<KeyValuePair<K, V>> keyedRouter(String name,
final Func1<K, byte[]> keyEncoder,
final Func1<V, byte[]> valueEncoder) {
return new ConsistentHashingRouter<K, V>(name, new Func1<KeyValuePair<K, V>, byte[]>() {
@Override
public byte[] call(KeyValuePair<K, V> kvp) {
byte[] keyBytes = kvp.getKeyBytes();
byte[] valueBytes = valueEncoder.call(kvp.getValue());
return
// length + opcode + notification type + key length
ByteBuffer.allocate(4 + 1 + 1 + 4 + keyBytes.length + valueBytes.length)
.putInt(1 + 1 + 4 + keyBytes.length + valueBytes.length) // length
.put((byte) 1) // opcode
.put((byte) 1) // notification type
.putInt(keyBytes.length) // key length
.put(keyBytes) // key bytes
.put(valueBytes) // value bytes
.array();
}
}, HashFunctions.xxh3());
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,14 @@ public class Routers implements RouterFactory {
public Routers() {}


/**
* Deprecated: use RouterFactory.keyedRouter instead
*/
@Deprecated
public static <K, V> Router<KeyValuePair<K, V>> consistentHashingLegacyTcpProtocol(String name,
final Func1<K, byte[]> keyEncoder,
final Func1<V, byte[]> valueEncoder) {
return new ConsistentHashingRouter<K, V>(name, new Func1<KeyValuePair<K, V>, byte[]>() {
@Override
public byte[] call(KeyValuePair<K, V> kvp) {
byte[] keyBytes = kvp.getKeyBytes();
byte[] valueBytes = valueEncoder.call(kvp.getValue());
return
// length + opcode + notification type + key length
ByteBuffer.allocate(4 + 1 + 1 + 4 + keyBytes.length + valueBytes.length)
.putInt(1 + 1 + 4 + keyBytes.length + valueBytes.length) // length
.put((byte) 1) // opcode
.put((byte) 1) // notification type
.putInt(keyBytes.length) // key length
.put(keyBytes) // key bytes
.put(valueBytes) // value bytes
.array();
}
}, HashFunctions.xxh3());
return new Routers().keyedRouter(name, keyEncoder, valueEncoder);
}

private static byte[] dataPayload(byte[] data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ private <K> LegacyTcpPushServer<KeyValuePair<K, T>> startKeyValueStage(KeyValueS
Func1<T, byte[]> valueEncoder = t1 -> stage.getOutputCodec().encode(t1);
Func1<K, byte[]> keyEncoder = t1 -> stage.getOutputKeyCodec().encode(t1);

Router<KeyValuePair<K, T>> router = this.routerFactory.keyedRouter(jobName, keyEncoder, valueEncoder);

ServerConfig<KeyValuePair<K, T>> config = new ServerConfig.Builder<KeyValuePair<K, T>>()
.name(name)
.port(serverPort)
Expand All @@ -144,7 +146,7 @@ private <K> LegacyTcpPushServer<KeyValuePair<K, T>> startKeyValueStage(KeyValueS
.maxChunkTimeMSec(maxChunkTimeMSec())
.bufferCapacity(bufferCapacity())
.useSpscQueue(useSpsc())
.router(Routers.consistentHashingLegacyTcpProtocol(jobName, keyEncoder, valueEncoder))
.router(router)
.build();

if (stage instanceof ScalarToGroup || stage instanceof GroupToGroup) {
Expand Down
Loading