Skip to content

Commit

Permalink
removed backendService constructor argument
Browse files Browse the repository at this point in the history
  • Loading branch information
VivianLopes committed Aug 23, 2018
1 parent d6cd34b commit 9f58868
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import com.hotels.styx.api.HttpResponseStatus;
import com.hotels.styx.api.metrics.MetricRegistry;
import com.hotels.styx.api.metrics.codahale.CodaHaleMetricRegistry;
import com.hotels.styx.api.extension.service.BackendService;
import com.hotels.styx.api.extension.service.RewriteRule;
import com.hotels.styx.api.extension.service.StickySessionConfig;
import com.hotels.styx.client.retry.RetryNTimes;
import com.hotels.styx.client.stickysession.StickySessionLoadBalancingStrategy;
import com.hotels.styx.server.HttpInterceptorContext;
Expand All @@ -50,6 +50,7 @@
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH;
import static com.hotels.styx.api.HttpHeaderNames.TRANSFER_ENCODING;
import static com.hotels.styx.api.StyxInternalObservables.toRxObservable;
import static com.hotels.styx.api.extension.service.StickySessionConfig.stickySessionDisabled;
import static com.hotels.styx.client.stickysession.StickySessionCookie.newStickySessionCookie;
import static io.netty.handler.codec.http.HttpMethod.HEAD;
import static java.util.Collections.emptyList;
Expand All @@ -71,14 +72,15 @@ public final class StyxHttpClient implements HttpClient {
private final LoadBalancer loadBalancer;
private final RetryPolicy retryPolicy;
private final OriginStatsFactory originStatsFactory;
private final BackendService backendService;
private final MetricRegistry metricsRegistry;
private final boolean contentValidation;
private final String originsRestrictionCookieName;
private final StickySessionConfig stickySessionConfig;

private StyxHttpClient(Builder builder) {
this.backendService = requireNonNull(builder.backendService);
this.id = backendService.id();
this.id = requireNonNull(builder.applicationId);

this.stickySessionConfig = requireNonNull(builder.stickySessionConfig);

this.originStatsFactory = requireNonNull(builder.originStatsFactory);

Expand All @@ -95,19 +97,19 @@ private StyxHttpClient(Builder builder) {
this.originsRestrictionCookieName = builder.originsRestrictionCookieName;
}


@Override
public Observable<HttpResponse> sendRequest(HttpRequest request) {
return sendRequest(rewriteUrl(request), new ArrayList<>(), 0);
}


/**
* Create a new builder.
*
* @return a new builder
*/
public static Builder newHttpClientBuilder(BackendService backendService) {
return new Builder(backendService);
public static Builder newHttpClientBuilder(Id applicationId) {
return new Builder(applicationId);
}

private static boolean isError(HttpResponseStatus status) {
Expand Down Expand Up @@ -305,7 +307,7 @@ public List<Origin> avoidOrigins() {

private HttpResponse addStickySessionIdentifier(HttpResponse httpResponse, Origin origin) {
if (this.loadBalancer instanceof StickySessionLoadBalancingStrategy) {
int maxAge = backendService.stickySessionConfig().stickySessionTimeoutSeconds();
int maxAge = stickySessionConfig.stickySessionTimeoutSeconds();
return httpResponse.newBuilder()
.addCookies(newStickySessionCookie(id, origin.id(), maxAge))
.build();
Expand All @@ -322,7 +324,7 @@ private HttpRequest rewriteUrl(HttpRequest request) {
public String toString() {
return toStringHelper(this)
.add("id", id)
.add("stickySessionConfig", backendService.stickySessionConfig())
.add("stickySessionConfig", stickySessionConfig)
.add("retryPolicy", retryPolicy)
.add("rewriteRuleset", rewriteRuleset)
.add("loadBalancingStrategy", loadBalancer)
Expand All @@ -333,19 +335,27 @@ public String toString() {
* A builder for {@link com.hotels.styx.client.StyxHttpClient}.
*/
public static class Builder {
private final BackendService backendService;

private final Id applicationId;
private MetricRegistry metricsRegistry = new CodaHaleMetricRegistry();
private List<RewriteRule> rewriteRules = emptyList();
private RetryPolicy retryPolicy = new RetryNTimes(3);
private LoadBalancer loadBalancer;
private boolean contentValidation;
private OriginStatsFactory originStatsFactory;
private String originsRestrictionCookieName;
private StickySessionConfig stickySessionConfig = stickySessionDisabled();

public Builder(Id applicationId) {
this.applicationId = checkNotNull(applicationId);
}

public Builder(BackendService backendService) {
this.backendService = checkNotNull(backendService);
public Builder stickySessionConfig(StickySessionConfig stickySessionConfig) {
this.stickySessionConfig = checkNotNull(stickySessionConfig);
return this;
}


public Builder metricsRegistry(MetricRegistry metricsRegistry) {
this.metricsRegistry = checkNotNull(metricsRegistry);
return this;
Expand Down Expand Up @@ -388,5 +398,7 @@ public StyxHttpClient build() {
}
return new StyxHttpClient(this);
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class HttpClientSpec extends FunSuite with BeforeAndAfterAll with ShouldMatchers
.responseTimeoutMillis(responseTimeout)
.build()

client = newHttpClientBuilder(backendService)
client = newHttpClientBuilder(backendService.id())
.loadBalancer(busyConnectionStrategy(activeOrigins(backendService)))
.build
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class RetryHandlingSpec extends FunSuite with BeforeAndAfterAll with Matchers wi
.origins(unhealthyOriginOne, unhealthyOriginTwo, unhealthyOriginThree, healthyOriginTwo)
.build()

val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.retryPolicy(new RetryNTimes(3))
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.build
Expand All @@ -139,7 +139,7 @@ class RetryHandlingSpec extends FunSuite with BeforeAndAfterAll with Matchers wi
val backendService = new BackendService.Builder()
.origins(unhealthyOriginOne, unhealthyOriginTwo, unhealthyOriginThree)
.build()
val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.retryPolicy(new RetryNTimes(2))
.build
Expand All @@ -155,7 +155,7 @@ class RetryHandlingSpec extends FunSuite with BeforeAndAfterAll with Matchers wi
.stickySessionConfig(StickySessionEnabled)
.build()

val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.retryPolicy(new RetryNTimes(3))
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class StickySessionSpec extends FunSuite with BeforeAndAfter with ShouldMatchers
def stickySessionStrategy(activeOrigins: ActiveOrigins) = new StickySessionLoadBalancingStrategy(activeOrigins, roundRobinStrategy(activeOrigins))

test("Responds with sticky session cookie when STICKY_SESSION_ENABLED=true") {
val client = newHttpClientBuilder(backendService)
val client = newHttpClientBuilder(backendService.id)
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.build

Expand All @@ -116,7 +116,7 @@ class StickySessionSpec extends FunSuite with BeforeAndAfter with ShouldMatchers
}

test("Responds without sticky session cookie when sticky session is not enabled") {
val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.loadBalancer(roundRobinStrategy(activeOrigins(backendService)))
.build

Expand All @@ -129,7 +129,7 @@ class StickySessionSpec extends FunSuite with BeforeAndAfter with ShouldMatchers
}

test("Routes to origins indicated by sticky session cookie.") {
val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.build

Expand All @@ -147,7 +147,7 @@ class StickySessionSpec extends FunSuite with BeforeAndAfter with ShouldMatchers
}

test("Routes to origins indicated by sticky session cookie when other cookies are provided.") {
val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.build

Expand All @@ -169,7 +169,7 @@ class StickySessionSpec extends FunSuite with BeforeAndAfter with ShouldMatchers
}

test("Routes to new origin when the origin indicated by sticky session cookie does not exist.") {
val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.build

Expand All @@ -194,7 +194,7 @@ class StickySessionSpec extends FunSuite with BeforeAndAfter with ShouldMatchers
test("Routes to new origin when the origin indicated by sticky session cookie is no longer available.") {
server1.stop()

val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.build

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void setUp() {
public void sendsRequestToHostChosenByLoadBalancer() {
StyxHostHttpClient hostClient = mockHostClient(just(response(OK).build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(
Expand All @@ -120,7 +120,7 @@ public void constructsRetryContextWhenLoadBalancerDoesNotFindAvailableOrigins()
RetryPolicy retryPolicy = mockRetryPolicy(true, true, true);
StyxHostHttpClient hostClient = mockHostClient(just(response(OK).build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(
Expand Down Expand Up @@ -158,7 +158,7 @@ public void retriesWhenRetryPolicyTellsToRetry() {

StyxHostHttpClient secondClient = mockHostClient(just(response(OK).build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(
Expand Down Expand Up @@ -198,7 +198,7 @@ public void stopsRetriesWhenRetryPolicyTellsToStop() {
StyxHostHttpClient secondClient = mockHostClient(Observable.error(new OriginUnreachableException(ORIGIN_2, new RuntimeException("An error occurred"))));
StyxHostHttpClient thirdClient = mockHostClient(Observable.error(new OriginUnreachableException(ORIGIN_2, new RuntimeException("An error occurred"))));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(
Expand Down Expand Up @@ -230,7 +230,7 @@ public void retriesAtMost3Times() {
StyxHostHttpClient thirdClient = mockHostClient(Observable.error(new OriginUnreachableException(ORIGIN_3, new RuntimeException("An error occurred"))));
StyxHostHttpClient fourthClient = mockHostClient(Observable.error(new OriginUnreachableException(ORIGIN_4, new RuntimeException("An error occurred"))));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(
Expand Down Expand Up @@ -262,7 +262,7 @@ public void retriesAtMost3Times() {
public void incrementsResponseStatusMetricsForBadResponse() {
StyxHostHttpClient hostClient = mockHostClient(just(response(BAD_REQUEST).build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient))))
Expand All @@ -279,7 +279,7 @@ public void incrementsResponseStatusMetricsForBadResponse() {
public void incrementsResponseStatusMetricsFor401() {
StyxHostHttpClient hostClient = mockHostClient(just(response(UNAUTHORIZED).build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))
Expand All @@ -297,7 +297,7 @@ public void incrementsResponseStatusMetricsFor401() {
public void incrementsResponseStatusMetricsFor500() {
StyxHostHttpClient hostClient = mockHostClient(just(response(INTERNAL_SERVER_ERROR).build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))
Expand All @@ -315,7 +315,7 @@ public void incrementsResponseStatusMetricsFor500() {
public void incrementsResponseStatusMetricsFor501() {
StyxHostHttpClient hostClient = mockHostClient(just(response(NOT_IMPLEMENTED).build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))
Expand All @@ -336,7 +336,7 @@ public void removesBadContentLength() {
.addHeader(TRANSFER_ENCODING, CHUNKED)
.build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))
Expand All @@ -358,7 +358,7 @@ public void updatesCountersWhenTransactionIsCancelled() {
PublishSubject<HttpResponse> responseSubject = PublishSubject.create();
StyxHostHttpClient hostClient = mockHostClient(responseSubject);

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendWithOrigins(origin.host().getPort()))
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.loadBalancer(
mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)))
)
Expand All @@ -382,7 +382,7 @@ public void prefersStickyOrigins() {

LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendWithOrigins(origin.host().getPort()))
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.loadBalancer(loadBalancer)
.metricsRegistry(metricRegistry)
.build();
Expand All @@ -408,7 +408,7 @@ public void prefersRestrictedOrigins() {

LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendWithOrigins(origin.host().getPort()))
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.loadBalancer(loadBalancer)
.metricsRegistry(metricRegistry)
.originsRestrictionCookieName("restrictedOrigin")
Expand All @@ -434,7 +434,7 @@ public void prefersRestrictedOriginsOverStickyOriginsWhenBothAreConfigured() {
StyxHostHttpClient hostClient = mockHostClient(just(response(OK).build()));
LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendWithOrigins(origin.host().getPort()))
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.originsRestrictionCookieName("restrictedOrigin")
.loadBalancer(loadBalancer)
.metricsRegistry(metricRegistry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public HttpClient createClient(BackendService backendService, OriginsInventory o
originRestrictionCookie
);

return new StyxHttpClient.Builder(backendService)
return new StyxHttpClient.Builder(backendService.id())
.loadBalancer(loadBalancingStrategy)
.metricsRegistry(environment.metricRegistry())
.retryPolicy(retryPolicy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.hotels.styx
import java.nio.charset.StandardCharsets.UTF_8

import com.hotels.styx.api.HttpRequest.get
import com.hotels.styx.api.Id.id
import com.hotels.styx.api.extension.ActiveOrigins
import com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer
import com.hotels.styx.api.HttpResponseStatus._
Expand Down Expand Up @@ -67,7 +68,7 @@ class HttpResponseSpec extends FunSuite
origins = Origins(originOne),
responseTimeout = responseTimeout)

client = newHttpClientBuilder(backendService.asJava)
client = newHttpClientBuilder(id(backendService.appId))
.loadBalancer(busyConnectionStrategy(activeOrigins(backendService.asJava)))
.build
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ExpiringConnectionSpec extends FunSpec
.origins(newOriginBuilder("localhost", styxServer.httpPort).build())
.build()

pooledClient = newHttpClientBuilder(backendService)
pooledClient = newHttpClientBuilder(backendService.id)
.loadBalancer(roundRobinStrategy(activeOrigins(backendService)))
.build
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class OriginClosesConnectionSpec extends FunSuite
origins = Origins(originOne),
responseTimeout = timeout.milliseconds).asJava
val styxClient = com.hotels.styx.client.StyxHttpClient.newHttpClientBuilder(
backendService)
backendService.id)
.loadBalancer(busyConnectionStrategy(activeOrigins(backendService)))
.build

Expand Down

0 comments on commit 9f58868

Please sign in to comment.