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

Refactor AsyncRequestResponseHandlerFactory #1356

Merged
merged 4 commits into from
Jan 14, 2020

Conversation

zzmao
Copy link
Contributor

@zzmao zzmao commented Jan 10, 2020

Unify rest.server.request.handler.factory and rest.server.response.handler.factory
Refactor AsyncRequestResponseHandlerFactory to factory pattern from singleton pattern
Add setupResponseHandler for RestRequestService.
Remove setupRequestHandling from AsyncRequestResponseHandler.

A regular order to start rest handler would be:

  1. Create RestRequestService
  2. Create RequestResponseHandler based on RestRequestService
  3. Setup ResponseHandler for RestRequestService

@zzmao zzmao requested review from cgtz and jsjtzyy January 10, 2020 23:15
Unify rest.server.request.handler.factory and rest.server.response.handler.factory
Refactor AsyncRequestResponseHandlerFactory to factory pattern from singleton pattern
Add setupResponseHandler for RestRquestService.
Remove setupRequestHandling from AsyncRequestResponseHandler.
@codecov-io
Copy link

codecov-io commented Jan 10, 2020

Codecov Report

❗ No coverage uploaded for pull request base (master@c802113). Click here to learn what that means.
The diff coverage is 92.45%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #1356   +/-   ##
=========================================
  Coverage          ?   72.05%           
  Complexity        ?     6612           
=========================================
  Files             ?      480           
  Lines             ?    37585           
  Branches          ?     4753           
=========================================
  Hits              ?    27082           
  Misses            ?     9232           
  Partials          ?     1271
Impacted Files Coverage Δ Complexity Δ
...github.ambry.server/StorageRestRequestService.java 0% <0%> (ø) 0 <0> (?)
...java/com.github.ambry/config/RestServerConfig.java 100% <100%> (ø) 1 <0> (?)
...ry.frontend/FrontendRestRequestServiceFactory.java 92% <100%> (ø) 2 <0> (?)
...ambry.rest/AsyncRequestResponseHandlerFactory.java 100% <100%> (ø) 6 <2> (?)
...hub.ambry.frontend/FrontendRestRequestService.java 92.39% <100%> (ø) 66 <3> (?)
...rc/main/java/com.github.ambry.rest/RestServer.java 90.96% <90.9%> (ø) 12 <0> (?)
...github.ambry.rest/AsyncRequestResponseHandler.java 87.87% <93.1%> (ø) 21 <0> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c802113...f401d23. Read the comment docs.

*/
@Config("rest.server.request.handler.factory")
@Config("rest.server.request.response.handler.factory")
@Default("com.github.ambry.rest.AsyncRequestResponseHandlerFactory")
public final String restServerRequestHandlerFactory;
Copy link
Contributor

Choose a reason for hiding this comment

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

change variable name to restServerRequestResponseHandlerFactory

this.metrics = metrics;
metrics.trackAsyncRequestResponseHandler(this);
if (isRunning()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this condition ever occur since this is logic is now in the constructor and isRunning is a non-static member?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It won't happen. Removing.


private AsyncRequestResponseHandler handler;
Copy link
Contributor

Choose a reason for hiding this comment

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

could be final, I believe

@@ -24,6 +24,11 @@
*/
public interface RestRequestService {

/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Add that this method must be called before the service is started

@cgtz
Copy link
Contributor

cgtz commented Jan 11, 2020

I reviewed the changes in the second commit

@zzmao
Copy link
Contributor Author

zzmao commented Jan 11, 2020

This PR is now independent.

@@ -233,7 +235,7 @@ public void start() throws InstantiationException {
restResponseHandler.start();
Copy link
Contributor

Choose a reason for hiding this comment

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

Had an offline discussion with Ze. restResponseHandler and restRequestHandler are actually same instance within restHandlerFactory. Hence, calling start() once should suffice. Let's leave a comment here to explain we removed restRequestHandler.start();. In the future, we probably could unify them.

@Override
public void start() throws InstantiationException {
if (responseHandler == null) {
throw new InstantiationException("ResponseHandler is not set.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Feel like this could be an IllegalStateException

Copy link
Contributor

@jsjtzyy jsjtzyy left a comment

Choose a reason for hiding this comment

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

LGTM after a few minor comments are addressed

this.restRequestService = restRequestService;
logger.trace("Request handling units count set to {}", requestWorkersCount);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

minor: line 268, could you removing the leading underscore of _restRequestService ?

}
logger.trace("Instantiated AsyncRequestResponseHandlerFactory as RestRequestHandler");
}

/**
* Returns an instance of {@link AsyncRequestResponseHandler}.
* @return an instance of {@link AsyncRequestResponseHandler}.
* @return an handler of {@link AsyncRequestResponseHandler}.
Copy link
Contributor

Choose a reason for hiding this comment

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

minor typo: return a handler or return an instance

}

/**
* Returns an instance of {@link AsyncRequestResponseHandler}.
* @return an instance of {@link AsyncRequestResponseHandler}.
* @return an handler of {@link AsyncRequestResponseHandler}.
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

@@ -171,7 +124,7 @@ private static void buildInstance(MetricRegistry metricRegistry) {
public final Counter residualResponseSetSize;

/**
* Creates an instance of RequestResponseHandlerMetrics using the given {@code metricRegistry}.
* Creates an handler of RequestResponseHandlerMetrics using the given {@code metricRegistry}.
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: no need to change previous comment.

private final RestRequestService restRequestService;

public MockRestRequestResponseHandler(RestRequestService restRequestService) {
this.restRequestService = restRequestService;
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to check if restRequestService is null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since it's mock, I think it's ok to throw null exception.

requestResponseHandler.setupRequestHandling(1, null);
fail("Setting RestRequestService to null should have thrown exception");
requestResponseHandler = getAsyncRequestResponseHandler(-1);
fail("Setting request workers < 0 should have thrown exception");
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the difference between line 165 and line 173 test cases ?

@jsjtzyy jsjtzyy merged commit 607df93 into linkedin:master Jan 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants