Skip to content
Merged
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
17 changes: 11 additions & 6 deletions lib/autoPipelining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,17 @@ export function executeWithAutoPipelining(
}

// If we have slot information, we can improve routing by grouping slots served by the same subset of nodes
const prefix = client.options.keyPrefix || "";
let slotKey = client.isCluster
? client.slots[
calculateSlot(`${prefix}${getFirstKeyForCommand(commandName, args)}`)
].join(",")
: "main";
let slotKey = "main";
if (client.isCluster) {
const prefix = client.options.keyPrefix || "";
const key = getFirstKeyForCommand(commandName, args);
// Hash Buffer keys by their bytes, as Command#getSlot does; decoding
// them as UTF-8 can change the slot.
const slot = Buffer.isBuffer(key)
? calculateSlot(Buffer.concat([Buffer.from(prefix), key]))
: calculateSlot(`${prefix}${key}`);
slotKey = client.slots[slot].join(",");
}

// When scaleReads is enabled, separate read and write commands into different pipelines
// so they can be routed to replicas and masters respectively
Expand Down
26 changes: 26 additions & 0 deletions test/functional/cluster/autopipelining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ describe("autoPipelining for cluster", () => {
return "bar6";
}

// Buffer.from([0xfe]) hashes to slot 3793.
if (argv[0] === "get" && argv[1] === "\ufffd") {
return "binary-fe";
}

if (argv[0] === "get" && argv[1] === "baz:foo10") {
return "bar10";
}
Expand All @@ -57,6 +62,11 @@ describe("autoPipelining for cluster", () => {
if (argv[0] === "get" && argv[1] === "foo4") {
return "bar4";
}

// Buffer.from([0xff]) hashes to slot 7920.
if (argv[0] === "get" && argv[1] === "\ufffd") {
return "binary-ff";
}
});

new MockServer(30003, (argv) => {
Expand Down Expand Up @@ -289,6 +299,22 @@ describe("autoPipelining for cluster", () => {
cluster.disconnect();
});

it("should bucket binary Buffer keys by their raw bytes", async () => {
const cluster = new Cluster(hosts, { enableAutoPipelining: true });
await new Promise((resolve) => cluster.once("connect", resolve));

// Both keys decode to "\ufffd" as UTF-8 strings, but their bytes hash to
// slots served by different nodes.
expect(
await Promise.all([
cluster.get(Buffer.from([0xff])),
cluster.get(Buffer.from([0xfe])),
])
).to.eql(["binary-ff", "binary-fe"]);

cluster.disconnect();
});

it("should support commands queued after a pipeline is already queued for execution", (done) => {
const cluster = new Cluster(hosts, { enableAutoPipelining: true });

Expand Down
Loading