Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new option for bulk gets in KV #3628

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

teresalves
Copy link

@teresalves teresalves commented Feb 28, 2025

Creating a new part in the binding for new bulk gets, along with tests for regular gets and the bulk gets
We consider we have a POST endpoint in SGW and we send the keys in a json format as such:

{
   "keys": ["key1", "key2"]
}

However, we decided that the binding here will only be a passthrough and everything will processed in the endpoint. On top of that, since everything is processed in the binding and comes as a single response, we don't need extra logic to process metadata, we just need to make sure that the binding responds what we what. Because of these two things, we added two more parameters in the request body:

{
   "keys": ["key1", "key2"],
   "type": "text",
   "withMetadata": true
}

We added tests for:
Regular gets:

  • 400s
  • 500s
  • 200s with text, json, stream and arrayBuffer

Bulk Gets:

  • 500s
  • 200s with text and json, as these will be the only supported types for now.

Note that we do not test for 400s because we will not have them. If none of the keys exist, we return a 200 but with empty values. Example:

{
   "key1": null,
   "key2": null,
 }

@teresalves teresalves requested review from a team as code owners February 28, 2025 01:15
@teresalves teresalves requested review from jasnell and mar-cf February 28, 2025 01:15
Copy link

github-actions bot commented Feb 28, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@teresalves teresalves marked this pull request as draft February 28, 2025 01:15
@teresalves teresalves force-pushed the talves/bulk-gets-for-kv branch from 6e63cdc to 44f5025 Compare February 28, 2025 01:27
@teresalves
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request Feb 28, 2025
@teresalves
Copy link
Author

recheck

Copy link
Contributor

@sesteves sesteves left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work! Left some comments with suggestions to improve tests.

const decoder = new TextDecoder(); // UTF-8 by default
let r = "";
while (true) {
const { done, value } = await reader.read();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - any particular reason for doing this in stream, instead of just doing request.arrayBuffer()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried some other ways and it was not reading the stream properly. Both here and in the type "stream" test below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you expand on what "not reading the stream properly" means? ;-)

Also... you can do the following to simplify this a bit:

for await (const chunk of request.body) {
  r += decoder.decode(value, { stream: true });
}
// But also remember to grab the final buffered chunk if any.
r += decoder.decode();

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not try this one in particular. Added it, thank you!

@teresalves teresalves force-pushed the talves/bulk-gets-for-kv branch 2 times, most recently from ff830c5 to 39f063b Compare February 28, 2025 14:32
if(parsedBody.type == "json") {
for(const key of keys) {
if(key == "key-not-json") {
return new Response(null, {status: 500})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw, We have Response.error() now that would end up sending a 500 response back to the eyeball.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried this one but the error message would be more generic and less specific to our particular case, so I would rather keep it like it is, if that's ok.

Comment on lines 89 to 91
assert.ok(false);
} catch {
assert.ok(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the expectation is that the env.KV.get promise will reject, use

await assert.rejects(env.KV.get('fail-server'), {
  message: '... whatever the expected error message is',
});

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so much cleaner. Thanks a bunch!
And thanks for all the suggestions :)

Comment on lines 104 to 91
let result = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
result += decoder.decode(value, { stream: true });
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let result = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
result += decoder.decode(value, { stream: true });
}
let result = "";
for await (const chunk of response) {
result += decoder.decode(value, { stream: true });
}
result += decoder.decode();

jsg::Promise<KvNamespace::GetWithMetadataResult> KvNamespace::getWithMetadataSingle(
jsg::Lock& js, kj::String name, jsg::Optional<kj::OneOf<kj::String, GetOptions>> options) {
return getWithMetadataImpl(js, kj::mv(name), kj::mv(options), LimitEnforcer::KvOpType::GET_WITH);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be sure to check the linting. Looks like the indentation on a few of these is out of whack.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That line in particular was already here, but I agree that it seems generally funky. Tried to adjust it a bit. Is there a linter I can run on top of this?
Thank you!

@teresalves teresalves force-pushed the talves/bulk-gets-for-kv branch 2 times, most recently from 02c9cc8 to 7e8640a Compare February 28, 2025 17:54
@teresalves teresalves force-pushed the talves/bulk-gets-for-kv branch from 7e8640a to 804c58c Compare February 28, 2025 17:57
@teresalves teresalves marked this pull request as ready for review March 3, 2025 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants