Remove all global state from the caching system (#863)

* Remove all global state from the caching system

This commit is a continuation of an effort to remove usages of
`lazy_static!` and similar global state macros which can otherwise be
accomodated with passing objects around. Previously there was a global
cache system initialized per-process, but it was initialized in a bit of
a roundabout way and wasn't actually reachable from the `wasmtime` crate
itself. The changes here remove all global state, refactor many of the
internals in the cache system, and makes configuration possible through
the `wasmtime` crate.

Specifically some changes here are:

* Usage of `lazy_static!` and many `static` items in the cache module
  have all been removed.
* Global `cache_config()`, `worker()`, and `init()` functions have all
  been removed. Instead a `CacheConfig` is a "root object" which
  internally owns its worker and passing around the `CacheConfig` is
  required for cache usage.
* The `wasmtime::Config` structure has grown options to load and parse
  cache files at runtime. Currently only loading files is supported,
  although we can likely eventually support programmatically configuring
  APIs as well.
* Usage of the `spin` crate has been removed and the dependency is removed.
* The internal `errors` field of `CacheConfig` is removed, instead
  changing all relevant methods to return a `Result<()>` instead of
  storing errors internally.
* Tests have all been updated with the new interfaces and APIs.

Functionally no real change is intended here. Usage of the `wasmtime`
CLI, for example, should still enable the cache by default.

* Fix lightbeam compilation
This commit is contained in:
Alex Crichton
2020-02-06 13:11:06 -06:00
committed by GitHub
parent 4ff8257b17
commit 70345aff31
24 changed files with 283 additions and 479 deletions

View File

@@ -36,11 +36,9 @@ fn test_cache_init() {
);
fs::write(&config_path, config_content).expect("Failed to write test config file");
let errors = init(true, Some(&config_path), None);
assert!(errors.is_empty());
let cache_config = CacheConfig::from_file(Some(&config_path)).unwrap();
// test if we can use config
let cache_config = cache_config();
assert!(cache_config.enabled());
// assumption: config init creates cache directory and returns canonicalized path
assert_eq!(
@@ -53,8 +51,7 @@ fn test_cache_init() {
);
// test if we can use worker
let worker = worker();
worker.on_cache_update_async(config_path);
cache_config.worker().on_cache_update_async(config_path);
}
#[test]
@@ -69,7 +66,6 @@ fn test_write_read_cache() {
cache_dir
);
assert!(cache_config.enabled());
let worker = Worker::start_new(&cache_config, None);
// assumption: config load creates cache directory and returns canonicalized path
assert_eq!(
@@ -102,7 +98,6 @@ fn test_write_read_cache() {
compiler1,
false,
&cache_config,
&worker,
));
assert!(entry1.0.is_some());
assert!(entry1.get_data().is_none());
@@ -117,7 +112,6 @@ fn test_write_read_cache() {
compiler1,
false,
&cache_config,
&worker,
));
let data2 = new_module_cache_data(&mut rng);
entry2.update_data(&data2);
@@ -131,7 +125,6 @@ fn test_write_read_cache() {
compiler1,
false,
&cache_config,
&worker,
));
let data3 = new_module_cache_data(&mut rng);
entry3.update_data(&data3);
@@ -146,7 +139,6 @@ fn test_write_read_cache() {
compiler1,
false,
&cache_config,
&worker,
));
let data4 = new_module_cache_data(&mut rng);
entry4.update_data(&data4);
@@ -162,7 +154,6 @@ fn test_write_read_cache() {
compiler2,
false,
&cache_config,
&worker,
));
let data5 = new_module_cache_data(&mut rng);
entry5.update_data(&data5);