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

Add local dictionary support #61

Merged
merged 34 commits into from
Aug 20, 2021
Merged

Add local dictionary support #61

merged 34 commits into from
Aug 20, 2021

Conversation

JakeChampion
Copy link
Contributor

@JakeChampion JakeChampion commented Aug 9, 2021

Resolves #11

This reads a new section from the local_server part of fastly.toml named dictionaries.

The local_server.dictionaries section is an array of tables with two fields: name, and file.

The name field is a string which is used as the name of the dictionary and is used via the Dictionary.open method. This field uses the same validation rules asthe Fastly API (Name must start with alphabetical and contain only alphanumeric, underscore, and whitespace).

The file field is a string which is a file-path pointing to a JSON file which contains all the dictionary items within an object.

An example of a full local_server section of fastly.toml now looks like this:

[local_server]
  [local_server.backends]
    [local_server.backends.dog]
      url = "http://localhost:7878/dog-mocks"
  [local_server.dictionaries]
    [local_server.dictionaries.a]
      file="./a.json"
      format = "json"

Note for reviewers:

I couldn't figure out how the tests are implements for the wiggle abi parts such as lib/src/wiggle_abi/dictionary_impl.rs. I assume they are implemented in an internal/private repo that Fastly has?


  • Reading dictionaries from fasty.toml file
    • Note: I added the local dictionaries to the local_server section
  • Construct Dictionary and DictionaryHandles
    • Dictionaries have two fields: name, and file
    • Validate name field with Fastly' validation rules (Name must start with alphabetical and contain only alphanumeric, underscore, and whitespace)
    • Validate file file (perhaps by doing a file existence check and checking it is JSON which adheres to our schema)
  • Implement Dictionary.open
  • Implement Dictionary.get
  • Tests
  • documentation

lib/src/wiggle_abi/dictionary_impl.rs Outdated Show resolved Hide resolved
lib/src/wiggle_abi/dictionary_impl.rs Show resolved Hide resolved
lib/src/wiggle_abi/dictionary_impl.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@cratelyn cratelyn left a comment

Choose a reason for hiding this comment

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

😻 This is incredible stuff! Thank you for opening this PR.

I have left assorted comments below; the two high-level points are that we should think about how we deserialize the configuration, and how we assign handles.

lib/src/error.rs Show resolved Hide resolved
cli/src/main.rs Outdated Show resolved Hide resolved
lib/src/config.rs Outdated Show resolved Hide resolved
lib/src/config.rs Outdated Show resolved Hide resolved
cli/src/main.rs Outdated Show resolved Hide resolved
use DictionaryConfigError::UnrecognizedKey;
static BAD_DEFAULT: &str = r#"
[dictionaries]
thing = { name = "thing", file = "./file.json", shrimp = true }
Copy link
Contributor

Choose a reason for hiding this comment

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

🅰️

Copy link

Choose a reason for hiding this comment

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

ya love to see a real shrimp-:a:ck in the wild

lib/src/config/unit_tests.rs Show resolved Hide resolved
lib/src/config/dictionaries.rs Show resolved Hide resolved
lib/src/config/dictionaries.rs Outdated Show resolved Hide resolved
lib/src/session.rs Outdated Show resolved Hide resolved
@JakeChampion
Copy link
Contributor Author

Thanks for the thorough review @cratelyn - I've added the PrimaryMap implementation and moved the validation logic out of main, along with using DictionaryName instead of String as the keys in the Dictionaries HashMap

I think this is ready for another round of reviewing now 😄

@aturon
Copy link
Contributor

aturon commented Aug 10, 2021

I think this is ready for another round of reviewing now 😄

Huzzah! I've got the review baton for this next round, and will do that first thing tomorrow morning (Pacific). Thanks again for this work!

Copy link
Contributor

@aturon aturon left a comment

Choose a reason for hiding this comment

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

Thanks again for taking this on! I made a review pass and left a few suggestions.

FWIW, I do think your impulse toward memoization was a good one, though I also agree that hot-reloading is nice. I think loading the file on every get is OK for now, but we should file a follow-up issue to preload dictionary contents and use file-watching to asynchronously reload on changes. That'll be helpful for people using really big dictionary files.

I didn't have a chance to carefully review the updated TOML loading and schema here. I know @cratelyn has thought more about that so I'll pass the baton back over on that one.

Cheers!

lib/src/config.rs Outdated Show resolved Hide resolved
lib/src/config/dictionaries.rs Outdated Show resolved Hide resolved
lib/src/wiggle_abi/dictionary_impl.rs Outdated Show resolved Hide resolved
lib/src/wiggle_abi/dictionary_impl.rs Outdated Show resolved Hide resolved
lib/src/wiggle_abi/dictionary_impl.rs Outdated Show resolved Hide resolved
lib/src/error.rs Outdated Show resolved Hide resolved
lib/src/config/unit_tests.rs Show resolved Hide resolved
lib/src/session.rs Show resolved Hide resolved
@JakeChampion
Copy link
Contributor Author

Thanks for the review @aturon - I have addressed all the issues now and I learnt about "deref coercion" ☺️

The Dictionary Handles are now created on demand and not reused, to match C@E behaviour 👍

I also spotted some .expect("..") which I left by accident and have converted those to errors under the DictionaryConfigError enum.

I believe this is ready for another round of reviews 🙂

Copy link
Contributor

@aturon aturon left a comment

Choose a reason for hiding this comment

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

Thank you, the changes look perfect!

I looked over the TOML deserialization now, and just left one tiny request there. Otherwise, I'm very happy with this PR!

That said, I believe that @cratelyn may have one further though re: the TOML schema (future-facing, thinking about "inline" dictionary definitions), but I'm not sure. I'll pass the baton back over to her to finish this out with you!

lib/src/config/unit_tests.rs Show resolved Hide resolved
Comment on lines 98 to 100
let dictionary_max_len: usize = 1000;
let dictionary_item_key_max_len: usize = 256;
let dictionary_item_value_max_len: usize = 8000;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we pull these out into a dedicated limits.rs file? We're going to want to build more awareness of C@E limits in Viceroy like this, and having them in one place will make life easier for keeping them up to date.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in b75566d (#61) 👍

Should these also go in the fastly-shared crate?

Copy link
Contributor

@cratelyn cratelyn left a comment

Choose a reason for hiding this comment

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

once again, great work! this is looking excellent 😻

i have provided a few more comments, but almost all of these are very minor quibbles. 🙂 this is looking just about complete!

lib/src/config.rs Outdated Show resolved Hide resolved
lib/src/config/dictionaries.rs Outdated Show resolved Hide resolved
lib/src/config/dictionaries.rs Show resolved Hide resolved
lib/src/config/dictionaries.rs Outdated Show resolved Hide resolved
lib/src/config/dictionaries.rs Outdated Show resolved Hide resolved
lib/Cargo.toml Outdated Show resolved Hide resolved
lib/src/wiggle_abi/dictionary_impl.rs Outdated Show resolved Hide resolved
lib/src/wiggle_abi/dictionary_impl.rs Outdated Show resolved Hide resolved
lib/src/config/dictionaries.rs Outdated Show resolved Hide resolved
lib/src/config/dictionaries.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@cratelyn cratelyn left a comment

Choose a reason for hiding this comment

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

one final comment, about structuring the fastly.toml data in a manner that'll be a bit more future-proof ✨

let me know if this part gives you trouble, i am more than happy to provide some help 👯

lib/src/config/unit_tests.rs Outdated Show resolved Hide resolved
@JakeChampion JakeChampion requested a review from cratelyn August 16, 2021 23:02
This reads a new section from the `local_server` part of fastly.toml named `dictionaries`.

The `local_server.dictionaries` section is an array of tables with two fields: name, and file.

The name field is a string which is used as the name of the dictionary and is used via the Dictionary.open method. This field uses the same validation rules asthe Fastly API (Name must start with alphabetical and contain only alphanumeric, underscore, and whitespace).

The file field is a string which is a file-path pointing to a JSON file which contains all the dictionary items within an object.

An example of a full local_server section of fastly.toml now looks like this:
```toml
[local_server]
              [local_server.backends]
                [local_server.backends.dog]
                url = "http://localhost:7878/dog-mocks"
              [local_server.dictionaries]
                    [local_server.dictionaries.a]
                    name="a"
                    file="./a.json"
```
…ame dictionary be faster and avoid re-reading the same file too many times
…s platform

Currently the limits are:
1000 items per dictionary
256 characters for dictionary item keys
8000 characters for dictionary item values
This mimics the 'liveness' of edge dictionaries - a user can edit a dictionary file and see the changes immediately without having to restart viceroy
cratelyn
cratelyn previously approved these changes Aug 17, 2021
Copy link
Contributor

@cratelyn cratelyn left a comment

Choose a reason for hiding this comment

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

thank you so much for implementing this @JakeChampion! this is wonderful, wonderful work ✨ ✔️

@cratelyn
Copy link
Contributor

ah! it looks like the ci problems are due to formatting. cargo fmt --all should do the trick!

@JakeChampion
Copy link
Contributor Author

ah! it looks like the ci problems are due to formatting. cargo fmt --all should do the trick!

The formatting issues should be fixed now 👍

@aturon
Copy link
Contributor

aturon commented Aug 17, 2021

Looks like Clippy is unhappy. If you run make ci locally, that should simulate the CI checks we do, and once that passes we should be good to merge!

@JakeChampion
Copy link
Contributor Author

Looks like Clippy is unhappy. If you run make ci locally, that should simulate the CI checks we do, and once that passes we should be good to merge!

make ci now passes for me 👍

@aturon
Copy link
Contributor

aturon commented Aug 17, 2021

Looks like there are some dictionary-related test failures on Windows: https://github.com/fastly/Viceroy/pull/61/checks?check_run_id=3355092000

@aturon
Copy link
Contributor

aturon commented Aug 18, 2021

My best guess of what's happening:

@aturon
Copy link
Contributor

aturon commented Aug 18, 2021

The TOML spec indicates we should be using ' for paths:

Literal strings are surrounded by single quotes. No escaping is performed so what you see is what you get:
path = 'C:\Users\nodejs\templates'

Sidenote, this will be an issue for eventual inline dictionaries.

@JakeChampion
Copy link
Contributor Author

Thanks for looking into the issue @aturon -- I've pushed a commit to fix this and can confirm that the tests pass on windows now 🎉 -- https://github.com/JakeChampion/Viceroy/runs/3369942476?check_suite_focus=true

@JakeChampion JakeChampion requested a review from cratelyn August 19, 2021 21:13
@aturon aturon merged commit 9edda3c into fastly:main Aug 20, 2021
@aturon
Copy link
Contributor

aturon commented Aug 20, 2021

Passed CI and merged! 🎉

Thanks for both doing this work, and following through these rounds of review and CI issues. This is an important improvement that will benefit a lot of people!

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.

Implement edge dictionary support
5 participants