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

test/raftstore: add random latency filter for simulation transport #1120

Merged

Conversation

hhkbp2
Copy link
Contributor

@hhkbp2 hhkbp2 commented Sep 28, 2016

For issue #963
PTAL @siddontang @BusyJay

}

impl Filter<RaftMessage> for RandomLatencyFilter {
fn before(&self, msgs: &mut Vec<RaftMessage>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If msgs has only one message, we will return error for this send, and raft will re-send the message again. So this may be not for delay, but duplicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A filter result type is added to let RandomLatencyFilter be capable to ignore this message dropped error. PTAL.

@@ -288,7 +288,7 @@ struct StaleSnap {
}

impl Filter<RaftMessage> for Arc<StaleSnap> {
fn before(&self, msgs: &mut Vec<RaftMessage>) {
fn before(&self, msgs: &mut Vec<RaftMessage>) -> FilterResult {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can return Result<()> directly and no need to check msg.empty() later.
In the delay filter, we can return ok, and for other filters, we can use a function like

fn check_droppable_err(msg:&Vec<RaftMessage>) -> Result<()> {
   if msg.empty() {
       return DropableError
   } else {
       return ok
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er, what's the advantage of use Result<()> instead of FilterResult?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused with FilterResult, and in fact, I don't think it is FilterResult, but only returns a result and let outer check, so why not using Result directly? And we can ignore checking msg.empty() too.

Copy link
Contributor Author

@hhkbp2 hhkbp2 Sep 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe FilterResult is a bad name. But it's a helpful pattern for a filter to return a flag, and the SimulateTransport acts differently according to the returned flag, such as to skip checking whether the messages is dropped, to shortcut the following filters, etc. In the previous comment of the definition of Filter::before()
https://github.com/pingcap/tikv/blob/master/tests/raftstore/transport_simulate.rs#L48
(this comment is obsolete since before does not return bool any more), this pattern is used by returning a bool. A bool is not good for naming and future extension. An enum is better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Result is also an enum.
  2. Now you return the result for every Filter::before, so the filter can check whether return ok or error itself, if error, send returns error, we don't need to check the message is empty and whether we should return DroppableError or not again.
  3. If the filter meets an error but want to go on, it can return ok.
  4. We can check error if we really want to filter some cases later, but I think we don't need this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

address comment

if res.is_err() {
break;
}
for msg in msgs {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing check res error here.

@siddontang
Copy link
Contributor

LGTM
PATL @BusyJay

@hhkbp2
Copy link
Contributor Author

hhkbp2 commented Oct 8, 2016

PTAL

let mut to_delay = vec![];
let mut delayed_msgs = self.delayed_msgs.lock().unwrap();
// check whether to send those messages which are delayed previouly
for m in delayed_msgs.drain(..) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for m in delayed_msgs.drain(..).chain(msgs)

if self.stale.load(Ordering::Relaxed) {
return;
return check_messages(msgs);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just return Ok(())?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check_messages() will return Ok(()) if msgs is not empty. An error returned by before will be delivered to SimulateTransport.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

msgs can't be empty here.

}


pub struct RandomLatencyFilterFactory {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think use CloneFilterFactory is enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that Mutex does not implement Clone.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not impl Clone for RandomLatencyFilter?

@BusyJay
Copy link
Member

BusyJay commented Oct 8, 2016

LGTM

@hhkbp2 hhkbp2 merged commit 9f27684 into master Oct 8, 2016
@hhkbp2 hhkbp2 deleted the hhkbp2/add-random-latency-filter-for-simulation-transport branch October 8, 2016 10:43
@sre-bot sre-bot added the contribution This PR is from a community contributor. label Dec 18, 2019
iosmanthus pushed a commit to iosmanthus/tikv that referenced this pull request Jan 4, 2024
Signed-off-by: Ping Yu <yuping@pingcap.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution This PR is from a community contributor.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants