Skip to content

Commit e7ea600

Browse files
authored
Reduce KV bulk upload size (#7036)
1 parent 3c2fc8c commit e7ea600

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

.changeset/polite-numbers-warn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Reduce KV bulk upload bucket size to 1000 (from the previous 5000)

packages/wrangler/src/__tests__/kv.test.ts

+37-19
Original file line numberDiff line numberDiff line change
@@ -1479,8 +1479,8 @@ describe("wrangler", () => {
14791479
expect(params.namespaceId).toEqual(expectedNamespaceId);
14801480
expect(await request.json()).toEqual(
14811481
expectedKeyValues.slice(
1482-
(requests.count - 1) * 5000,
1483-
requests.count * 5000
1482+
(requests.count - 1) * 1000,
1483+
requests.count * 1000
14841484
)
14851485
);
14861486
return HttpResponse.json(createFetchResult(null), {
@@ -1510,7 +1510,7 @@ describe("wrangler", () => {
15101510
expect(std.err).toMatchInlineSnapshot(`""`);
15111511
});
15121512

1513-
it("should put the key-values in batches of 5000 parsed from a file", async () => {
1513+
it("should put the key-values in batches of 1000 parsed from a file", async () => {
15141514
const keyValues: KeyValue[] = new Array(12000).fill({
15151515
key: "someKey1",
15161516
value: "someValue1",
@@ -1520,14 +1520,23 @@ describe("wrangler", () => {
15201520
await runWrangler(
15211521
`kv bulk put --namespace-id some-namespace-id keys.json`
15221522
);
1523-
expect(requests.count).toEqual(3);
1523+
expect(requests.count).toEqual(12);
15241524
expect(std.out).toMatchInlineSnapshot(`
1525-
"Uploaded 0% (0 out of 12,000)
1526-
Uploaded 41% (5,000 out of 12,000)
1527-
Uploaded 83% (10,000 out of 12,000)
1528-
Uploaded 100% (12,000 out of 12,000)
1529-
Success!"
1530-
`);
1525+
"Uploaded 0% (0 out of 12,000)
1526+
Uploaded 8% (1,000 out of 12,000)
1527+
Uploaded 16% (2,000 out of 12,000)
1528+
Uploaded 25% (3,000 out of 12,000)
1529+
Uploaded 33% (4,000 out of 12,000)
1530+
Uploaded 41% (5,000 out of 12,000)
1531+
Uploaded 50% (6,000 out of 12,000)
1532+
Uploaded 58% (7,000 out of 12,000)
1533+
Uploaded 66% (8,000 out of 12,000)
1534+
Uploaded 75% (9,000 out of 12,000)
1535+
Uploaded 83% (10,000 out of 12,000)
1536+
Uploaded 91% (11,000 out of 12,000)
1537+
Uploaded 100% (12,000 out of 12,000)
1538+
Success!"
1539+
`);
15311540
expect(std.warn).toMatchInlineSnapshot(`""`);
15321541
expect(std.err).toMatchInlineSnapshot(`""`);
15331542
});
@@ -1629,8 +1638,8 @@ describe("wrangler", () => {
16291638
);
16301639
expect(await request.json()).toEqual(
16311640
expectedKeys.slice(
1632-
(requests.count - 1) * 5000,
1633-
requests.count * 5000
1641+
(requests.count - 1) * 1000,
1642+
requests.count * 1000
16341643
)
16351644
);
16361645
return HttpResponse.json(createFetchResult(null), {
@@ -1670,14 +1679,23 @@ describe("wrangler", () => {
16701679
await runWrangler(
16711680
`kv bulk delete --namespace-id some-namespace-id keys.json`
16721681
);
1673-
expect(requests.count).toEqual(3);
1682+
expect(requests.count).toEqual(12);
16741683
expect(std.out).toMatchInlineSnapshot(`
1675-
"Deleted 0% (0 out of 12,000)
1676-
Deleted 41% (5,000 out of 12,000)
1677-
Deleted 83% (10,000 out of 12,000)
1678-
Deleted 100% (12,000 out of 12,000)
1679-
Success!"
1680-
`);
1684+
"Deleted 0% (0 out of 12,000)
1685+
Deleted 8% (1,000 out of 12,000)
1686+
Deleted 16% (2,000 out of 12,000)
1687+
Deleted 25% (3,000 out of 12,000)
1688+
Deleted 33% (4,000 out of 12,000)
1689+
Deleted 41% (5,000 out of 12,000)
1690+
Deleted 50% (6,000 out of 12,000)
1691+
Deleted 58% (7,000 out of 12,000)
1692+
Deleted 66% (8,000 out of 12,000)
1693+
Deleted 75% (9,000 out of 12,000)
1694+
Deleted 83% (10,000 out of 12,000)
1695+
Deleted 91% (11,000 out of 12,000)
1696+
Deleted 100% (12,000 out of 12,000)
1697+
Success!"
1698+
`);
16811699
expect(std.warn).toMatchInlineSnapshot(`""`);
16821700
expect(std.err).toMatchInlineSnapshot(`""`);
16831701
});

packages/wrangler/src/kv/helpers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import type { ReplaceWorkersTypes } from "miniflare";
1313

1414
/** The largest number of kv items we can pass to the API in a single request. */
1515
const API_MAX = 10000;
16-
// The const below are halved from the API's true capacity to help avoid
16+
// The const below are lowered from the API's true capacity to help avoid
1717
// hammering it with large requests.
18-
export const BATCH_KEY_MAX = API_MAX / 2;
18+
export const BATCH_KEY_MAX = API_MAX / 10;
1919

2020
type KvArgs = {
2121
binding?: string;

0 commit comments

Comments
 (0)