Skip to content

Commit

Permalink
Add store and first test
Browse files Browse the repository at this point in the history
  • Loading branch information
allada committed Dec 25, 2020
1 parent 4f5aad9 commit ed4bde4
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 9 deletions.
1 change: 1 addition & 0 deletions cas/BUILD
Expand Up @@ -8,6 +8,7 @@ rust_binary(
"//cas/grpc_service:ac_server",
"//cas/grpc_service:capabilities_server",
"//cas/grpc_service:execution_server",
"//cas/store",
"//third_party:tokio",
"//third_party:tonic",
],
Expand Down
5 changes: 4 additions & 1 deletion cas/cas_main.rs
Expand Up @@ -6,13 +6,16 @@ use ac_server::AcServer;
use capabilities_server::CapabilitiesServer;
use cas_server::CasServer;
use execution_server::ExecutionServer;
use store;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "0.0.0.0:50051".parse()?;

let store = store::create_store(&store::StoreType::Memory);

Server::builder()
.add_service(CasServer::default().into_service())
.add_service(CasServer::new(store).into_service())
.add_service(AcServer::default().into_service())
.add_service(CapabilitiesServer::default().into_service())
.add_service(ExecutionServer::default().into_service())
Expand Down
15 changes: 14 additions & 1 deletion cas/grpc_service/BUILD
@@ -1,4 +1,4 @@
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library")
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library", "rust_test")

rust_library(
name = "cas_server",
Expand All @@ -8,6 +8,7 @@ rust_library(
"//third_party:tonic",
"//third_party:futures_core",
"//third_party:stdext",
"//cas/store",
],
visibility = ["//cas:__pkg__"]
)
Expand Down Expand Up @@ -44,3 +45,15 @@ rust_library(
],
visibility = ["//cas:__pkg__"]
)

rust_test(
name = "cas_server_test",
srcs = ["tests/cas_server_test.rs"],
deps = [
":cas_server",
"//cas/store",
"//proto",
"//third_party:tonic",
"//third_party:tokio",
],
)
27 changes: 20 additions & 7 deletions cas/grpc_service/cas_server.rs
Expand Up @@ -12,11 +12,18 @@ use proto::build::bazel::remote::execution::v2::{
BatchUpdateBlobsResponse, FindMissingBlobsRequest, FindMissingBlobsResponse, GetTreeRequest,
GetTreeResponse,
};
use store;

#[derive(Debug, Default)]
pub struct CasServer {}
#[derive(Debug)]
pub struct CasServer {
pub store: Box<dyn store::Store>,
}

impl CasServer {
pub fn new(in_store: Box<dyn store::Store>) -> Self {
CasServer { store: in_store }
}

pub fn into_service(self) -> Server<CasServer> {
Server::new(self)
}
Expand All @@ -26,12 +33,18 @@ impl CasServer {
impl ContentAddressableStorage for CasServer {
async fn find_missing_blobs(
&self,
_request: Request<FindMissingBlobsRequest>,
request: Request<FindMissingBlobsRequest>,
) -> Result<Response<FindMissingBlobsResponse>, Status> {
use stdext::function_name;
let output = format!("{} not yet implemented", function_name!());
println!("{}", output);
Err(Status::unimplemented(output))
let request_data = request.into_inner();
let mut response = FindMissingBlobsResponse {
missing_blob_digests: vec![],
};
for digest in request_data.blob_digests.into_iter() {
if !self.store.has(&digest.hash) {
response.missing_blob_digests.push(digest);
}
}
Ok(Response::new(response))
}

async fn batch_update_blobs(
Expand Down
37 changes: 37 additions & 0 deletions cas/grpc_service/tests/cas_server_test.rs
@@ -0,0 +1,37 @@
// Copyright 2020 Nathan (Blaise) Bruer. All rights reserved.

extern crate cas_server;
extern crate store;

use tonic::Request;

use proto::build::bazel::remote::execution::v2::Digest;

use cas_server::CasServer;
use store::{create_store, StoreType};

#[cfg(test)]
mod find_missing_blobs {
use super::*;

use proto::build::bazel::remote::execution::v2::{
content_addressable_storage_server::ContentAddressableStorage, FindMissingBlobsRequest,
};

#[tokio::test]
async fn empty_store() {
let cas_server = CasServer::new(create_store(&StoreType::Memory));
let raw_response = cas_server
.find_missing_blobs(Request::new(FindMissingBlobsRequest {
instance_name: "foo".to_string(),
blob_digests: vec![Digest {
hash: "".to_string(),
size_bytes: 0,
}],
}))
.await;
assert!(raw_response.is_ok());
let response = raw_response.unwrap().into_inner();
assert_eq!(response.missing_blob_digests.len(), 1);
}
}
26 changes: 26 additions & 0 deletions cas/store/BUILD
@@ -0,0 +1,26 @@
# Copyright 2020 Nathan (Blaise) Bruer. All rights reserved.

load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library")

rust_library(
name = "store",
srcs = ["lib.rs"],
deps = [
":memory_store",
":traits",
],
visibility = ["//cas:__pkg__", "//cas:__subpackages__"]
)

rust_library(
name = "traits",
srcs = ["store_trait.rs"],
visibility = ["//cas:__pkg__"]
)

rust_library(
name = "memory_store",
srcs = ["memory_store.rs"],
deps = [":traits"],
visibility = ["//cas:__pkg__"]
)
15 changes: 15 additions & 0 deletions cas/store/lib.rs
@@ -0,0 +1,15 @@
// Copyright 2020 Nathan (Blaise) Bruer. All rights reserved.

pub use traits::StoreTrait as Store;

use memory_store::MemoryStore;

pub enum StoreType {
Memory,
}

pub fn create_store(store_type: &StoreType) -> Box<dyn Store> {
match store_type {
StoreType::Memory => Box::new(MemoryStore::new()),
}
}
18 changes: 18 additions & 0 deletions cas/store/memory_store.rs
@@ -0,0 +1,18 @@
// Copyright 2020 Nathan (Blaise) Bruer. All rights reserved.

use traits::StoreTrait;

#[derive(Debug)]
pub struct MemoryStore {}

impl MemoryStore {
pub fn new() -> Self {
MemoryStore {}
}
}

impl StoreTrait for MemoryStore {
fn has(&self, _hash: &str) -> bool {
false
}
}
7 changes: 7 additions & 0 deletions cas/store/store_trait.rs
@@ -0,0 +1,7 @@
// Copyright 2020 Nathan (Blaise) Bruer. All rights reserved.

use std::fmt::Debug;

pub trait StoreTrait: Sync + Send + Debug {
fn has(&self, hash: &str) -> bool;
}

0 comments on commit ed4bde4

Please sign in to comment.