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

[close #372] add circuit breaker and smart rawkv client #358

Merged
merged 18 commits into from
Dec 9, 2021

Conversation

marsishandsome
Copy link
Collaborator

@marsishandsome marsishandsome commented Dec 2, 2021

Signed-off-by: marsishandsome marsishandsome@gmail.com

add Circuit Breaker

@marsishandsome
Copy link
Collaborator Author

/run-all-tests

this.listeners = new ArrayList<>();
this.currentMetrics = new AtomicReference<>(new SingleWindowMetrics());

scheduler =
Copy link
Member

Choose a reason for hiding this comment

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

should this background task be managed by the circuit breaker instead of the circuit metrics?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

why?

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 it's a subtask belonging to the circuit breaker. For the metrics, it only needs to record the traffic statistics.

Copy link
Member

Choose a reason for hiding this comment

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

BTW, we should not start this background thread when the circuit breaker is configured to be disabled.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed in 5fcf19b

Signed-off-by: marsishandsome <marsishandsome@gmail.com>
Signed-off-by: marsishandsome <marsishandsome@gmail.com>
Signed-off-by: marsishandsome <marsishandsome@gmail.com>
Signed-off-by: marsishandsome <marsishandsome@gmail.com>
Signed-off-by: marsishandsome <marsishandsome@gmail.com>
this.listeners = new ArrayList<>();
this.currentMetrics = new AtomicReference<>(new SingleWindowMetrics());

scheduler =
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 it's a subtask belonging to the circuit breaker. For the metrics, it only needs to record the traffic statistics.

Comment on lines +43 to +46
private final AtomicLong circuitOpened = new AtomicLong(-1);
private final AtomicReference<Status> status = new AtomicReference<>(Status.CLOSED);
private final AtomicLong attemptCount = new AtomicLong(0);
private final AtomicLong attemptSuccessCount = new AtomicLong(0);
Copy link
Member

Choose a reason for hiding this comment

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

  1. do we really need to set these variables as atomic variables? I can only see they are accessed within the current TiSession.

  2. we add the 'final' keyword to these fields but their values are changed during the execution time, seems a little counterintuitive.

Copy link
Member

Choose a reason for hiding this comment

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

There are two-time windows and their corresponding traffic stats:

  1. the first time window is to record normal traffic stats when the circuit breaker is closed or disabled in the background and triggers circuit breaker, which is limited to TiKV_CIRCUIT_BREAK_AVAILABILITY_WINDOW_IN_SECONDS and is called windowOfClosed.
  2. the second time window to record the attempt traffic stats when the circuit breaker is half-opened, which is called windowOfHalfOpened.

these two windows can be posited like this: windowOfClosed -> (circuit breaker opened) -> (after sleep, circuit breaker half-opened) -> windowOfHalfOpened

So can we:

  1. Define two traffic metrics for these two time windows like the following and remove the atomic variables for attempts:
private final CircuitBreakerMetrics metricsOfClosed;
private final CircuitBreakerMetrics metricsOfHalfOpened
  1. Make CircuitBreakerImpl be the owner of these two windows, don't start the background task to collect metricsOfClosed when the circuit breaker is disabled.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

do we really need to set these variables as atomic variables?

yes, in order to make CircuitBreakerImpl thread-safe.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

we add the 'final' keyword to these fields but their values are changed during the execution time, seems a little counterintuitive.

yes, we should add final and the object reference does not change. The object content does change.

this.listeners = new ArrayList<>();
this.currentMetrics = new AtomicReference<>(new SingleWindowMetrics());

scheduler =
Copy link
Member

Choose a reason for hiding this comment

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

BTW, we should not start this background thread when the circuit breaker is configured to be disabled.

Signed-off-by: marsishandsome <marsishandsome@gmail.com>
Copy link
Collaborator

@birdstorm birdstorm left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

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

LGTM

@zz-jason zz-jason changed the title add Circuit Breaker [close #372] add circuit breaker and smart rawkv client Dec 9, 2021
@zz-jason zz-jason merged commit 48504bc into tikv:master Dec 9, 2021
ti-srebot pushed a commit to ti-srebot/client-java that referenced this pull request Dec 9, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Collaborator

cherry pick to release-3.1 in PR #373

marsishandsome added a commit to ti-srebot/client-java that referenced this pull request Dec 9, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
marsishandsome added a commit to ti-srebot/client-java that referenced this pull request Dec 9, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
Signed-off-by: marsishandsome <marsishandsome@gmail.com>
marsishandsome added a commit to ti-srebot/client-java that referenced this pull request Dec 9, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
Signed-off-by: marsishandsome <marsishandsome@gmail.com>
marsishandsome added a commit to ti-srebot/client-java that referenced this pull request Dec 9, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
Signed-off-by: marsishandsome <marsishandsome@gmail.com>
marsishandsome added a commit to ti-srebot/client-java that referenced this pull request Dec 9, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
Signed-off-by: marsishandsome <marsishandsome@gmail.com>
marsishandsome added a commit that referenced this pull request Dec 9, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
Signed-off-by: marsishandsome <marsishandsome@gmail.com>

Co-authored-by: Liangliang Gu <marsishandsome@gmail.com>
ankita25 pushed a commit to ankita25/client-java that referenced this pull request Dec 14, 2021
Signed-off-by: Ankita Wagh <awagh@pinterest.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants