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

[metasrv] Fix flaky test #6242 #6248

Merged
merged 3 commits into from
Jun 27, 2022
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
22 changes: 19 additions & 3 deletions common/meta/grpc/src/grpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use common_meta_types::protobuf::meta_service_client::MetaServiceClient;
use common_meta_types::protobuf::Empty;
use common_meta_types::protobuf::ExportedChunk;
use common_meta_types::protobuf::HandshakeRequest;
use common_meta_types::protobuf::MemberListReply;
use common_meta_types::protobuf::MemberListRequest;
use common_meta_types::protobuf::RaftReply;
use common_meta_types::protobuf::RaftRequest;
Expand Down Expand Up @@ -593,12 +594,27 @@ impl MetaGrpcClient {
#[tracing::instrument(level = "debug", skip(self))]
pub async fn sync_endpoints(&self) -> std::result::Result<(), MetaError> {
let mut client = self.make_client().await?;
let endpoints = client
let result = client
.member_list(Request::new(MemberListRequest {
data: "".to_string(),
}))
.await?;
let result: Vec<String> = endpoints.into_inner().data;
.await;
let endpoints: std::result::Result<MemberListReply, Status> = match result {
Ok(r) => Ok(r.into_inner()),
Err(s) => {
if status_is_retryable(&s) {
self.mark_as_unhealthy().await;
let mut client = self.make_client().await?;
let req = Request::new(MemberListRequest {
data: "".to_string(),
});
Ok(client.member_list(req).await?.into_inner())
} else {
Err(s)
}
}
};
let result: Vec<String> = endpoints?.data;
self.set_endpoints(result).await?;
Ok(())
}
Expand Down
66 changes: 40 additions & 26 deletions metasrv/tests/it/grpc/metasrv_grpc_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,32 +373,25 @@ async fn test_auto_sync_addr() -> anyhow::Result<()> {
let mut srv = tc0.grpc_srv.take().unwrap();
srv.stop(None).await?;

let metrics = meta_node
.raft
.wait(Some(Duration::from_millis(30_000)))
.metrics(
|m| m.current_leader.is_some() && m.current_term > old_term,
"a leader is observed",
)
.await?;

tracing::debug!("got leader, metrics: {:?}", metrics);

// wait for auto sync work.
tokio::time::sleep(Duration::from_secs(20)).await;

// wait for leader observed
// if tc0 is old leader, then we need to check both current_leader is some and current_term > old_term
// if tc0 isn't old leader, then we need do nothing.
if meta_node.get_leader().await == 0 {
let metrics = meta_node
.raft
.wait(Some(Duration::from_millis(30_000)))
.metrics(
|m| m.current_leader.is_some() && m.current_term > old_term,
"a leader is observed",
)
.await?;

tracing::debug!("got leader, metrics: {metrics:?}");
}
let res = client.get_endpoints().await?;
let res: HashSet<String> = HashSet::from_iter(res.into_iter());

assert_eq!(3, res.len());

// still can get kv from cluster
let k = "join-k".to_string();

let res = client.get_kv(k.as_str()).await;

let res = res?;
assert_eq!(k.into_bytes(), res.unwrap().data);
}

tracing::info!("--- endpoints should changed when add node");
Expand All @@ -412,12 +405,30 @@ async fn test_auto_sync_addr() -> anyhow::Result<()> {

start_metasrv_with_context(&mut tc3).await?;

let addr3 = tc3.config.grpc_api_address.clone();
let g = tc3.grpc_srv.as_ref().unwrap();
let meta_node = g.get_meta_node();

tokio::time::sleep(Duration::from_secs(15)).await;
let metrics = meta_node
.raft
.wait(Some(Duration::from_millis(30_000)))
ariesdevil marked this conversation as resolved.
Show resolved Hide resolved
.metrics(|m| m.current_leader.is_some(), "a leader is observed")
.await?;

let res = client.get_endpoints().await?;
let res: HashSet<String> = HashSet::from_iter(res.into_iter());
tracing::debug!("got leader, metrics: {metrics:?}");

let addr3 = tc3.config.grpc_api_address.clone();

let mut i = 0;
let mut res = vec![];
while i < 15 {
res = client.get_endpoints().await?;
if res.contains(&addr3) {
break;
} else {
i += 1;
tokio::time::sleep(Duration::from_secs(1)).await;
}
}

assert!(
res.contains(&addr3),
Expand All @@ -433,5 +444,8 @@ async fn test_auto_sync_addr() -> anyhow::Result<()> {
assert_eq!(k.into_bytes(), res.unwrap().data);
}

// TODO(ariesdevil): remove node from cluster then get endpoints
// tracing::info!("--- endpoints should changed after remove node");

Ok(())
}