Skip to content

Conversation

PalAditya
Copy link
Contributor

Fixes issue #413 by adding an implementation of circuit breaker pattern

  • Added the Circuit Breaker Pattern
  • Wrote test cases with nearly 100% coverage for all of the component classes (except the main class)

An example output:

output

Code Coverage Report:
Capture

Please mention whether something else needs to be done for this PR 😄

Copy link
Owner

@iluwatar iluwatar left a comment

Choose a reason for hiding this comment

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

There is an error related to generating Javadocs in Travis build, although the check looks green. See the following screenshot

image

@PalAditya
Copy link
Contributor Author

There is an error related to generating Javadocs in Travis build, although the check looks green. See the following screenshot

image

I see. I did build it locally but I use Java 8 and didn't run up against this error, and I didn't go through the Travis CI logs (😅 ). Any idea on why this could be happening?

It does seem like this is a common problem on Java 11+ builds though, as written here, and I'll try to fix it as such.

Do you have any other changes in mind?

@PalAditya
Copy link
Contributor Author

Actually, I see that many modules don't seem to use the javadocs plugin, and it's not in the parent pom, so is it necessary or should I remove that from the pom.xml of circuit-breaker all together?

@iluwatar
Copy link
Owner

@PalAditya yes using that plugin is not mandatory so it is ok to remove

@PalAditya
Copy link
Contributor Author

working

I believe that the previous error stemmed from this part only, and as you can see, I have fixed that, but I am getting a JaCoCo error from a completely unrelated module. Any advice on that? 😅

Note: Could be related to this or this

@iluwatar
Copy link
Owner

I believe the jacoco issue was fixed in #996. Please merge master into this branch.

</execution>
</executions>
</plugin>
</plugins>
Copy link
Owner

Choose a reason for hiding this comment

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

The plugins are already defined in parent pom.xml and not needed here

Comment on lines 71 to 74
MonitoringService obj = new MonitoringService();
//Set the circuit Breaker parameters
CircuitBreaker circuitBreaker = new CircuitBreaker(3000, 1, 2000 * 1000 * 1000);
long serverStartTime = System.nanoTime();
Copy link
Owner

Choose a reason for hiding this comment

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

You can use local-variable type inference here (var)

Comment on lines 30 to 35
long timeout;
long retryTimePeriod;
long lastFailureTime;
int failureCount;
private final int failureThreshold;
private State state;
Copy link
Owner

Choose a reason for hiding this comment

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

Use final where member variables don't change

Comment on lines 108 to 109
DelayedService delayedService = new DelayedService(20);
String response = delayedService.response(serverStartTime);
Copy link
Owner

Choose a reason for hiding this comment

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

Use local-variable type inference (var)

this.timeout = timeout;
this.retryTimePeriod = retryTimePeriod;
//An absurd amount of time in future which basically indicates the last failure never happened
this.lastFailureTime = System.nanoTime() + 1000 * 1000 * 1000 * 1000;
Copy link
Owner

Choose a reason for hiding this comment

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

Could it be constant? Looks a bit cryptic

*/
public String response(long serverStartTime) {
long currentTime = System.nanoTime();
if ((currentTime - serverStartTime) * 1.0 / (1000 * 1000 * 1000) < delay) {
Copy link
Owner

Choose a reason for hiding this comment

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

The calculation here is hard to understand. We need at least comment there.

//Set time as current time as initially server fails
long serverStartTime = System.nanoTime();
String response = monitoringService.remoteResourceResponse(circuitBreaker, serverStartTime);
assertEquals(response, "Remote service not responding");
Copy link
Owner

Choose a reason for hiding this comment

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

Also in tests var can be used extensively

@PalAditya
Copy link
Contributor Author

Thanks for the feedback! I implemented the changes as requested, but the Sonar Integration seems to have broken something else.

Capture

Capture

A local build with mvn clean install still passes perfectly though, so I believe the error is not with the module but somewhere else, which is causing the CI to fail. Correct me if I'm wrong 🤔

Capture

@iluwatar
Copy link
Owner

I wonder why the sonar analysis worked in my branch and master, but not here. To remedy this, I now tried to set the sonar host property in 47d92bb Could you merge from the master again so we can see if it helped?

@PalAditya
Copy link
Contributor Author

Capture

After merging, I get this. It seems like it's an permission issue on your end 😅
Maybe it is enabled for master but not PRs?
Also, this might help.

@PalAditya
Copy link
Contributor Author

Capture

Funny thing: I decided to play around with the config file and made a temporary copy of the project on my SonarQube account, and guess what, it passed (with this minimal yml on another branch)!

language: java
dist: bionic
jdk:
- openjdk11
sudo: required

services:
- xvfb

addons:
  sonarcloud:
    organization: "paladitya"
    
script:
  # the following command line builds the project, runs the tests with coverage and then execute the SonarCloud analysis
  - mvn clean verify sonar:sonar -Dsonar.projectKey=PalAditya_java-design-patterns -Dsonar.host.url=https://sonarcloud.io

notifications:
  email:
    - adityapal.nghss@gmail.com
  webhooks:
    on_success: change  # options: [always|never|change] default: always
    on_failure: always  # options: [always|never|change] default: always
    on_start: never     # options: [always|never|change] default: always

I can only guess that either the encrypted key is not being read properly for PRs (I actually set the value SONAR_TOKEN in my travis settings rather than encrypting it), or maybe the execute-analysis permission needs to be set for everyone?

(Sorry if my guesses are unhelpful, but I have never worked with SonarQube before, and I find this prospect of a bug/misconfiguration quite interesting 😅 )

@iluwatar
Copy link
Owner

https://docs.travis-ci.com/user/sonarcloud/#analysis-of-internal-pull-requests
The documentation indicates that it is not supposed to work across forks

@PalAditya
Copy link
Contributor Author

https://docs.travis-ci.com/user/sonarcloud/#analysis-of-internal-pull-requests
The documentation indicates that it is not supposed to work across forks

I see. So, what should be done next from my end? 🤔

Copy link
Owner

@iluwatar iluwatar left a comment

Choose a reason for hiding this comment

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

The code in README.md explanation is not in sync with the implementation

@PalAditya
Copy link
Contributor Author

PalAditya commented Oct 14, 2019

The code in README.md explanation is not in sync with the implementation

Oh, do you mean that I should change them to use var and final too or that I shouldn't remove the comments that are in the actual code?

I am going to assume the former since I don't see a heavy usage of comments in the other READMEs and I have written the explanation too. Plus, they could always see the actual code for full understanding.

@iluwatar
Copy link
Owner

@PalAditya can you please merge master again? It shouldn't run the sonar analysis for this PR now, if everything goes fine.

@PalAditya
Copy link
Contributor Author

@iluwatar This seems to have done the trick 😄

@iluwatar
Copy link
Owner

Wonderful, thanks for your help @PalAditya 👍

@iluwatar iluwatar added this to the 1.22.0 milestone Oct 14, 2019
@iluwatar iluwatar merged commit 7e698a9 into iluwatar:master Oct 14, 2019
@iluwatar
Copy link
Owner

Thanks for the great pattern @PalAditya, well done

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.

2 participants