8000 Validates `consistency` parameter by shinnya · Pull Request #217 · frugalos/frugalos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Validates consistency parameter #217

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

Merged
merged 1 commit into from
Oct 1, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 54 additions & 6 deletions frugalos_segment/src/client/mds.rs
10000
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ use fibers_rpc::client::ClientServiceHandle as RpcServiceHandle;
use frugalos_core::tracer::SpanExt;
use frugalos_mds::{Error as MdsError, ErrorKind as MdsErrorKind};
use frugalos_raft::{LocalNodeId, NodeId};
use futures::future::Either;
use futures::{Async, Future, Poll};
use libfrugalos::client::mds::Client as RaftMdsClient;
use libfrugalos::consistency::ReadConsistency;
Expand Down Expand Up @@ -141,6 +142,10 @@ impl MdsClient {
parent: SpanHandle,
) -> impl Future<Item = Option<ObjectValue>, Error = Error> {
debug!(self.logger, "Starts GET: id={:?}", id);
let member_size = self.member_size();
if let Err(e) = validate_consistency(consistency.clone(), member_size) {
return Either::A(futures::future::err(track!(e)));
}
let concurrency = match consistency {
ReadConsistency::Quorum => Some(self.majority_size()),
ReadConsistency::Subset(n) => Some(n),
Expand Down Expand Up @@ -178,7 +183,7 @@ impl MdsClient {
Box::new(future)
}))
};
Request::new(self.clone(), parent, request)
Either::B(Request::new(self.clone(), parent, request))
}

pub fn head(
Expand All @@ -188,6 +193,10 @@ impl MdsClient {
parent: SpanHandle,
) -> impl Future<Item = Option<ObjectVersion>, Error = Error> {
debug!(self.logger, "Starts HEAD: id={:?}", id);
let member_size = self.member_size();
if let Err(e) = validate_consistency(consistency.clone(), member_size) {
return Either::A(futures::future::err(track!(e)));
}
let concurrency = match consistency {
ReadConsistency::Quorum => Some(self.majority_size()),
ReadConsistency::Subset(n) => Some(n),
Expand Down Expand Up @@ -224,7 +233,7 @@ impl MdsClient {
)
}))
};
Request::new(self.clone(), parent, request)
Either::B(Request::new(self.clone(), parent, request))
}

pub fn delete(
Expand Down Expand Up @@ -339,7 +348,7 @@ impl MdsClient {
// for backward compatibility
MdsRequestPolicy::Conservative => RequestTimeout::Never,
MdsRequestPolicy::Speculative { timeout, .. } => {
let factor = 2u32.pow((self.max_retry().saturating_sub(max_retry)) as u32);
let factor = 2u32.pow((self.member_size().saturating_sub(max_retry)) as u32);
RequestTimeout::Speculative {
timer: timer::timeout(*timeout * factor),
}
Expand All @@ -354,9 +363,9 @@ impl MdsClient {
}
}
fn majority_size(&self) -> usize {
((self.max_retry() as f64 / 2.0).ceil()) as usize
((self.member_size() as f64 / 2.0).ceil()) as usize
}
fn max_retry(&self) -> usize {
fn member_size(&self) -> usize {
self.inner
.lock()
.unwrap_or_else(|e| panic!("{}", e))
Expand Down Expand Up @@ -456,6 +465,26 @@ fn to_object_value(
)
}

fn validate_consistency(consistency: ReadConsistency, member_size: usize) -> Result<()> {
if member_size == 0 {
return track!(Err(ErrorKind::Invalid
.cause("The size of cluster member must be bigger than 0")
.into()));
}
match consistency {
ReadConsistency::Subset(n) => {
if n == 0 || member_size < n {
track!(Err(ErrorKind::Invalid
.cause(format!("subset must be 0 < n <= {}", member_size))
.into()))
} else {
Ok(())
}
}
ReadConsistency::Quorum | ReadConsistency::Stale | ReadConsistency::Consistent => Ok(()),
}
}

fn make_request_span(parent: &SpanHandle, peer: &NodeId) -> Span {
parent.child("mds_request", |span| {
span.tag(StdTag::component(module_path!()))
Expand Down Expand Up @@ -533,7 +562,7 @@ where
T::Item: Send + 'static,
{
pub fn new(client: MdsClient, parent: SpanHandle, request: T) -> Self {
let max_retry = client.max_retry();
let max_retry = client.member_size();
let timeout = client.timeout(request.kind(), max_retry);
Request {
client,
Expand Down Expand Up @@ -868,3 +897,22 @@ where
Ok(Async::NotReady)
}
}

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

#[test]
fn validate_consistency_works() {
assert!(validate_consistency(ReadConsistency::Consistent, 3).is_ok());
assert!(validate_consistency(ReadConsistency::Consistent, 0).is_err());
assert!(validate_consistency(ReadConsistency::Stale, 2).is_ok());
assert!(validate_consistency(ReadConsistency::Stale, 0).is_err());
assert!(validate_consistency(ReadConsistency::Quorum, 1).is_ok());
assert!(validate_consistency(ReadConsistency::Quorum, 0).is_err());
assert!(validate_consistency(ReadConsistency::Subset(4), 12).is_ok());
assert!(validate_consistency(ReadConsistency::Subset(2), 2).is_ok());
assert!(validate_consistency(ReadConsistency::Subset(2), 1).is_err());
assert!(validate_consistency(ReadConsistency::Subset(0), 1).is_err());
}
}
41 changes: 41 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,3 +758,44 @@ fn get_consistency(url: &Url) -> Result<ReadConsistency> {
}
Ok(Default::default())
}

#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
use trackable::result::TestResult;

#[test]
fn get_subset_works() -> TestResult {
let url = Url::from_str("http://example.com/").unwrap();
let subset = track!(get_subset(&url))?;
assert_eq!(1, subset);
let url = Url::from_str("http://example.com/?subset=2").unwrap();
let subset = track!(get_subset(&url))?;
assert_eq!(2, subset);
let url = Url::from_str("http://example.com/?subset=-1").unwrap();
let subset = get_subset(&url);
assert!(subset.is_err());
Ok(())
}

#[test]
fn get_consistency_works() -> TestResult {
let url = Url::from_str("http://example.com/?consistency=consistent").unwrap();
let consistency = track!(get_consistency(&url))?;
assert_eq!(ReadConsistency::Consistent, consistency);
let url = Url::from_str("http://example.com/?consistency=stale").unwrap();
let consistency = track!(get_consistency(&url))?;
assert_eq!(ReadConsistency::Stale, consistency);
let url = Url::from_str("http://example.com/?consistency=subset").unwrap();
let consistency = track!(get_consistency(&url))?;
assert_eq!(ReadConsistency::Subset(1), consistency);
let url = Url::from_str("http://example.com/?consistency=quorum").unwrap();
let consistency = track!(get_consistency(&url))?;
assert_eq!(ReadConsistency::Quorum, consistency);
let url = Url::from_str("http://example.com/").unwrap();
let consistency = track!(get_consistency(&url))?;
assert_eq!(ReadConsistency::Consistent, consistency);
Ok(())
}
}
0