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 Webdav (a.k.a HTTP Cache) support #1597

Merged
merged 10 commits into from
Feb 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,50 @@ jobs:
${SCCACHE_PATH} --show-stats

${SCCACHE_PATH} --show-stats | grep -e "Cache hits\s*[1-9]"

webdav:
runs-on: ubuntu-latest
needs: build

env:
SCCACHE_WEBDAV_ENDPOINT: "http://127.0.0.1:8080"
RUSTC_WRAPPER: /home/runner/.cargo/bin/sccache

steps:
- uses: actions/checkout@v3

- name: Start nginx
shell: bash
run: |
mkdir /tmp/static
nginx -c `pwd`/tests/nginx_http_cache.conf

- name: Install rust
uses: ./.github/actions/rust-toolchain
with:
toolchain: "stable"

- uses: actions/download-artifact@v3
with:
name: integration-tests
path: /home/runner/.cargo/bin/
- name: Chmod for binary
run: chmod +x ${SCCACHE_PATH}

- name: Test
run: cargo clean && cargo build

- name: Output
run: |
${SCCACHE_PATH} --show-stats

${SCCACHE_PATH} --show-stats | grep webdav

- name: Test Twice for Cache Read
run: cargo clean && cargo build

- name: Output
run: |
${SCCACHE_PATH} --show-stats

${SCCACHE_PATH} --show-stats | grep -e "Cache hits\s*[1-9]"
40 changes: 7 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bincode = "1"
blake3 = "1"
byteorder = "1.0"
bytes = "1"
opendal = { version= "0.26.2", optional=true }
opendal = { version= "0.27.0", optional=true }
reqsign = {version="0.8.1", optional=true}
clap = { version = "4.0.32", features = ["derive", "env", "wrap_help"] }
directories = "4.0.1"
Expand Down Expand Up @@ -117,11 +117,12 @@ features = [

[features]
default = ["all"]
all = ["dist-client", "redis", "s3", "memcached", "gcs", "azure", "gha"]
all = ["dist-client", "redis", "s3", "memcached", "gcs", "azure", "gha", "webdav"]
azure = ["opendal","reqsign"]
s3 = ["opendal","reqsign"]
gcs = ["opendal","reqsign", "url", "reqwest/blocking"]
gha = ["opendal"]
webdav = ["opendal"]
memcached = ["opendal/services-memcached"]
native-zlib = []
redis = ["url", "opendal/services-redis"]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Table of Contents (ToC)
* [Google Cloud Storage](docs/Gcs.md)
* [Azure](docs/Azure.md)
* [GitHub Actions](docs/GHA.md)
* [WebDAV (Ccache/Bazel/Gradle compatible)](docs/Webdav.md)
* [Debugging](#debugging)
* [Interaction with GNU `make` jobserver](#interaction-with-gnu-make-jobserver)
* [Known Caveats](#known-caveats)
Expand Down
11 changes: 11 additions & 0 deletions docs/Webdav.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# WebDAV

Set `SCCACHE_WEBDAV_ENDPOINT` to a wevdav service endpoint to store cache in a webdav service. Set `SCCACHE_WEBDAV_KEY_PREFIX` to specify the key prefix of cache.

The webdav cache is compatible with:

- [Ccache HTTP storage backend](https://ccache.dev/manual/4.7.4.html#_http_storage_backend)
- [Bazel Remote Caching](https://bazel.build/remote/caching).
- [Gradle Build Cache](https://docs.gradle.org/current/userguide/build_cache.html)

Users can set `SCCACHE_WEBDAV_ENDPOINT` to those services directly.
12 changes: 11 additions & 1 deletion src/cache/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ use crate::cache::memcached::MemcachedCache;
use crate::cache::redis::RedisCache;
#[cfg(feature = "s3")]
use crate::cache::s3::S3Cache;
#[cfg(feature = "webdav")]
use crate::cache::webdav::WebdavCache;
use crate::config::Config;
#[cfg(any(
feature = "azure",
feature = "gcs",
feature = "gha",
feature = "memcached",
feature = "redis",
feature = "s3"
feature = "s3",
feature = "webdav"
))]
use crate::config::{self, CacheType};
use std::fmt;
Expand Down Expand Up @@ -546,6 +549,13 @@ pub fn storage_from_config(

return Ok(Arc::new(storage));
}
#[cfg(feature = "webdav")]
CacheType::Webdav(ref c) => {
debug!("Init webdav cache with endpoint {}", c.endpoint);
let storage = WebdavCache::build(&c.endpoint, &c.key_prefix)
.map_err(|err| anyhow!("create webdav cache failed: {err:?}"))?;
return Ok(Arc::new(storage));
}
#[allow(unreachable_patterns)]
_ => bail!("cache type is not enabled"),
}
Expand Down
2 changes: 2 additions & 0 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ pub mod memcached;
pub mod redis;
#[cfg(feature = "s3")]
pub mod s3;
#[cfg(feature = "webdav")]
pub mod webdav;

pub use crate::cache::cache::*;
33 changes: 33 additions & 0 deletions src/cache/webdav.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::errors::*;
use opendal::layers::LoggingLayer;
use opendal::services::Webdav;
use opendal::Operator;

/// A cache that stores entries in a Webdav.
pub struct WebdavCache;

impl WebdavCache {
/// Create a new `WebdavCache`.
pub fn build(endpoint: &str, key_prefix: &str) -> Result<Operator> {
let mut builder = Webdav::default();
builder.endpoint(endpoint);
builder.root(key_prefix);

let op = Operator::create(builder)?
.layer(LoggingLayer::default())
.finish();
Ok(op)
}
}
44 changes: 43 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ pub struct RedisCacheConfig {
pub url: String,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WebdavCacheConfig {
pub endpoint: String,
pub key_prefix: String,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct S3CacheConfig {
Expand All @@ -243,6 +250,7 @@ pub enum CacheType {
Memcached(MemcachedCacheConfig),
Redis(RedisCacheConfig),
S3(S3CacheConfig),
Webdav(WebdavCacheConfig),
}

#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
Expand All @@ -255,6 +263,7 @@ pub struct CacheConfigs {
pub memcached: Option<MemcachedCacheConfig>,
pub redis: Option<RedisCacheConfig>,
pub s3: Option<S3CacheConfig>,
pub webdav: Option<WebdavCacheConfig>,
}

impl CacheConfigs {
Expand All @@ -269,6 +278,7 @@ impl CacheConfigs {
memcached,
redis,
s3,
webdav,
} = self;

let cache_type = s3
Expand All @@ -277,7 +287,8 @@ impl CacheConfigs {
.or_else(|| memcached.map(CacheType::Memcached))
.or_else(|| gcs.map(CacheType::GCS))
.or_else(|| gha.map(CacheType::GHA))
.or_else(|| azure.map(CacheType::Azure));
.or_else(|| azure.map(CacheType::Azure))
.or_else(|| webdav.map(CacheType::Webdav));

let fallback = disk.unwrap_or_default();

Expand All @@ -294,6 +305,7 @@ impl CacheConfigs {
memcached,
redis,
s3,
webdav,
} = other;

if azure.is_some() {
Expand All @@ -317,6 +329,9 @@ impl CacheConfigs {
if s3.is_some() {
self.s3 = s3
}
if webdav.is_some() {
self.webdav = webdav
}
}
}

Expand Down Expand Up @@ -637,6 +652,24 @@ fn config_from_env() -> Result<EnvConfig> {
None
};

// ======= WebDAV =======
let webdav = if let Ok(endpoint) = env::var("SCCACHE_WEBDAV_ENDPOINT") {
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
let key_prefix = env::var("SCCACHE_WEBDAV_KEY_PREFIX")
.ok()
.as_ref()
.map(|s| s.trim_end_matches('/'))
.filter(|s| !s.is_empty())
.unwrap_or_default()
.to_owned();

Some(WebdavCacheConfig {
endpoint,
key_prefix,
})
} else {
None
};

// ======= Local =======
let disk_dir = env::var_os("SCCACHE_DIR").map(PathBuf::from);
let disk_sz = env::var("SCCACHE_CACHE_SIZE")
Expand All @@ -660,6 +693,7 @@ fn config_from_env() -> Result<EnvConfig> {
memcached,
redis,
s3,
webdav,
};

Ok(EnvConfig { cache })
Expand Down Expand Up @@ -1126,6 +1160,10 @@ endpoint = "s3-us-east-1.amazonaws.com"
use_ssl = true
key_prefix = "s3prefix"
no_credentials = true

[cache.webdav]
endpoint = "http://127.0.0.1:8080"
key_prefix = "webdavprefix"
"#;

let file_config: FileConfig = toml::from_str(CONFIG_STR).expect("Is valid toml.");
Expand Down Expand Up @@ -1165,6 +1203,10 @@ no_credentials = true
key_prefix: "s3prefix".into(),
no_credentials: true,
}),
webdav: Some(WebdavCacheConfig {
endpoint: "http://127.0.0.1:8080".to_string(),
key_prefix: "webdavprefix".into(),
})
},
dist: DistConfig {
auth: DistAuth::Token {
Expand Down
1 change: 1 addition & 0 deletions tests/harness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub fn sccache_client_cfg(tmpdir: &Path) -> sccache::config::FileConfig {
memcached: None,
redis: None,
s3: None,
webdav: None,
},
dist: sccache::config::DistConfig {
auth: Default::default(), // dangerously_insecure
Expand Down