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(cache/oss): add support for oss #2046

Merged
merged 7 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ all = [
"azure",
"gha",
"webdav",
"oss",
]
azure = ["opendal", "reqsign"]
default = ["all"]
Expand All @@ -158,6 +159,7 @@ native-zlib = []
redis = ["url", "opendal/services-redis"]
s3 = ["opendal", "reqsign"]
webdav = ["opendal"]
oss = ["opendal", "reqsign"]
# Enable features that will build a vendored version of openssl and
# statically linked with it, instead of linking against the system-wide openssl
# dynamically or statically.
Expand Down
42 changes: 23 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,28 @@ sccache is also available as a [GitHub Actions](https://github.com/marketplace/a
Table of Contents (ToC)
=======================

* [Installation](#installation)
* [Usage](#usage)
* [Build Requirements](#build-requirements)
* [Build](#build)
* [Separating caches between invocations](#separating-caches-between-invocations)
* [Overwriting the cache](#overwriting-the-cache)
* [Debugging](#debugging)
* [Interaction with GNU `make` jobserver](#interaction-with-gnu-make-jobserver)
* [Known Caveats](#known-caveats)
* [Storage Options](#storage-options)
* [Local](docs/Local.md)
* [S3](docs/S3.md)
* [R2](docs/S3.md#R2)
* [Redis](docs/Redis.md)
* [Memcached](docs/Memcached.md)
* [Google Cloud Storage](docs/Gcs.md)
* [Azure](docs/Azure.md)
* [GitHub Actions](docs/GHA.md)
* [WebDAV (Ccache/Bazel/Gradle compatible)](docs/Webdav.md)
- [sccache - Shared Compilation Cache](#sccache---shared-compilation-cache)
- [Table of Contents (ToC)](#table-of-contents-toc)
PsiACE marked this conversation as resolved.
Show resolved Hide resolved
- [Installation](#installation)
- [macOS](#macos)
- [Windows](#windows)
- [Via cargo](#via-cargo)
- [Usage](#usage)
- [Build Requirements](#build-requirements)
- [Build](#build)
- [Building portable binaries](#building-portable-binaries)
- [Linux](#linux)
- [macOS](#macos-1)
- [Windows](#windows-1)
- [Separating caches between invocations](#separating-caches-between-invocations)
- [Overwriting the cache](#overwriting-the-cache)
- [Debugging](#debugging)
- [Interaction with GNU `make` jobserver](#interaction-with-gnu-make-jobserver)
- [Known Caveats](#known-caveats)
- [General](#general)
- [Rust](#rust)
- [Symbolic links](#symbolic-links)
- [Storage Options](#storage-options)

---

Expand Down Expand Up @@ -295,3 +298,4 @@ Storage Options
* [Azure](docs/Azure.md)
* [GitHub Actions](docs/GHA.md)
* [WebDAV (Ccache/Bazel/Gradle compatible)](docs/Webdav.md)
* [Alibaba OSS](docs/OSS.md)
7 changes: 7 additions & 0 deletions docs/OSS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# OSS

If you want to use OSS storage for the sccache cache, you need to set the `SCCACHE_OSS_BUCKET` environment variable to the name of the OSS bucket to use.
PsiACE marked this conversation as resolved.
Show resolved Hide resolved

You **must** configure the region using the `SCCACHE_ENDPOINT` environment variable.
PsiACE marked this conversation as resolved.
Show resolved Hide resolved

You can also define a prefix that will be prepended to the keys of all cache objects created and read within the OSS bucket, effectively creating a scope. To do that use the `SCCACHE_OSS_KEY_PREFIX` environment variable. This can be useful when sharing a bucket with another application.
21 changes: 20 additions & 1 deletion src/cache/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use crate::cache::redis::RedisCache;
use crate::cache::s3::S3Cache;
#[cfg(feature = "webdav")]
use crate::cache::webdav::WebdavCache;
#[cfg(feature = "oss")]
use crate::cache::oss::OSSCache;
use crate::compiler::PreprocessorCacheEntry;
use crate::config::Config;
#[cfg(any(
Expand All @@ -36,7 +38,8 @@ use crate::config::Config;
feature = "memcached",
feature = "redis",
feature = "s3",
feature = "webdav"
feature = "webdav",
feature = "oss"
))]
use crate::config::{self, CacheType};
use async_trait::async_trait;
Expand Down Expand Up @@ -653,6 +656,22 @@ pub fn storage_from_config(

return Ok(Arc::new(storage));
}
#[cfg(feature = "oss")]
CacheType::OSS(ref c) => {
debug!(
"Init oss cache with bucket {}, endpoint {:?}",
c.bucket, c.endpoint
);

let storage = OSSCache::build(
&c.bucket,
&c.key_prefix,
c.endpoint.as_deref(),
)
.map_err(|err| anyhow!("create oss 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 @@ -29,5 +29,7 @@ pub mod redis;
pub mod s3;
#[cfg(feature = "webdav")]
pub mod webdav;
#[cfg(feature = "oss")]
pub mod oss;

pub use crate::cache::cache::*;
41 changes: 41 additions & 0 deletions src/cache/oss.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 opendal::layers::LoggingLayer;
use opendal::services::Oss;
use opendal::Operator;

use crate::errors::*;

pub struct OSSCache;

impl OSSCache {
PsiACE marked this conversation as resolved.
Show resolved Hide resolved
pub fn build(
bucket: &str,
key_prefix: &str,
endpoint: Option<&str>,
) -> Result<Operator> {
let mut builder = Oss::default();
builder.bucket(bucket);
builder.root(key_prefix);

Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
if let Some(endpoint) = endpoint {
builder.endpoint(endpoint);
}


let op = Operator::new(builder)?
.layer(LoggingLayer::default())
.finish();
Ok(op)
}
}
52 changes: 50 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ pub struct S3CacheConfig {
pub server_side_encryption: Option<bool>,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OSSCacheConfig {
pub bucket: String,
pub key_prefix: String,
pub endpoint: Option<String>,
}

#[derive(Debug, PartialEq, Eq)]
pub enum CacheType {
Azure(AzureCacheConfig),
Expand All @@ -270,6 +278,7 @@ pub enum CacheType {
Redis(RedisCacheConfig),
S3(S3CacheConfig),
Webdav(WebdavCacheConfig),
OSS(OSSCacheConfig),
}

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

impl CacheConfigs {
Expand All @@ -298,6 +308,7 @@ impl CacheConfigs {
redis,
s3,
webdav,
oss,
} = self;

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

let fallback = disk.unwrap_or_default();

Expand All @@ -325,6 +337,7 @@ impl CacheConfigs {
redis,
s3,
webdav,
oss,
} = other;

if azure.is_some() {
Expand All @@ -351,6 +364,10 @@ impl CacheConfigs {
if webdav.is_some() {
self.webdav = webdav
}

if oss.is_some() {
self.oss = oss
}
}
}

Expand Down Expand Up @@ -720,6 +737,26 @@ fn config_from_env() -> Result<EnvConfig> {
None
};

// ======= OSS =======
let oss = if let Ok(bucket) = env::var("SCCACHE_OSS_BUCKET") {
let endpoint= env::var("SCCACHE_OSS_ENDPOINT").ok();
let key_prefix = env::var("SCCACHE_OSS_KEY_PREFIX")
.ok()
.as_ref()
.map(|s| s.trim_end_matches('/'))
.filter(|s| !s.is_empty())
.unwrap_or_default()
.to_owned();

Some(OSSCacheConfig {
bucket,
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 Down Expand Up @@ -759,6 +796,7 @@ fn config_from_env() -> Result<EnvConfig> {
redis,
s3,
webdav,
oss,
};

Ok(EnvConfig { cache })
Expand Down Expand Up @@ -1305,6 +1343,11 @@ key_prefix = "webdavprefix"
username = "webdavusername"
password = "webdavpassword"
token = "webdavtoken"

[cache.oss]
bucket = "name"
endpoint = "oss-us-east-1.oss.com"
key_prefix = "ossprefix"
"#;

let file_config: FileConfig = toml::from_str(CONFIG_STR).expect("Is valid toml.");
Expand Down Expand Up @@ -1353,7 +1396,12 @@ token = "webdavtoken"
username: Some("webdavusername".to_string()),
password: Some("webdavpassword".to_string()),
token: Some("webdavtoken".to_string()),
})
}),
oss: Some(OSSCacheConfig {
bucket: "name".to_owned(),
endpoint: Some("oss-us-east-1.oss.com".to_owned()),
key_prefix: "ossprefix".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 @@ -155,6 +155,7 @@ pub fn sccache_client_cfg(
redis: None,
s3: None,
webdav: None,
oss: None,
},
dist: sccache::config::DistConfig {
auth: Default::default(), // dangerously_insecure
Expand Down
Loading