Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Commit

Permalink
remove redundant request tracking
Browse files Browse the repository at this point in the history
Both jetty and hermes handles request timeouts so there is no need for
apollo to also perform incoming request timeout tracking.
  • Loading branch information
Daniel Norberg committed Feb 24, 2016
1 parent 3028ff0 commit 4a51c03
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* -\-\-
* Spotify Apollo API Implementations
* --
* Copyright (C) 2013 - 2015 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/
package com.spotify.apollo.request;

import com.spotify.apollo.RequestContext;
import com.spotify.apollo.dispatch.Endpoint;
import com.spotify.apollo.meta.IncomingCallsGatherer;

/**
* An {@link EndpointRunnableFactory} that gathers incoming calls.
*/
class GatheringEndpointRunnableFactory implements EndpointRunnableFactory {

private final EndpointRunnableFactory delegate;
private final IncomingCallsGatherer incomingCallsGatherer;

GatheringEndpointRunnableFactory(
EndpointRunnableFactory delegate,
IncomingCallsGatherer incomingCallsGatherer) {
this.delegate = delegate;
this.incomingCallsGatherer = incomingCallsGatherer;
}

public Runnable create(
OngoingRequest ongoingRequest,
RequestContext requestContext,
Endpoint endpoint) {

incomingCallsGatherer.gatherIncomingCall(ongoingRequest, endpoint);

return delegate.create(ongoingRequest, requestContext, endpoint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static EndpointRunnableFactory endpointRunnableFactory() {
return new EndpointInvocationHandler();
}

@Deprecated
public static EndpointRunnableFactory withTracking(
EndpointRunnableFactory endpointRunnableFactory,
IncomingCallsGatherer incomingCallsGatherer,
Expand All @@ -64,4 +65,11 @@ public static EndpointRunnableFactory withTracking(
incomingCallsGatherer,
requestTracker);
}

public static EndpointRunnableFactory withGathering(final EndpointRunnableFactory endpointRunnableFactory,
final IncomingCallsGatherer incomingCallsGatherer) {
return new GatheringEndpointRunnableFactory(
endpointRunnableFactory,
incomingCallsGatherer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import static com.google.common.util.concurrent.Futures.getUnchecked;

@Deprecated
public class RequestTracker implements Closeable {

private static final Logger LOG = LoggerFactory.getLogger(RequestTracker.class);
Expand All @@ -51,14 +52,17 @@ public class RequestTracker implements Closeable {
.setNameFormat("apollo-request-reaper")
.build());

private static final long REAPING_INTERVAL_MS = Long.getLong("com.spotify.apollo.request.reaping_interval_ms", 10);

private final Set<OngoingRequest> outstanding = ConcurrentHashMap.newKeySet();

private final ScheduledFuture<?> future;

public RequestTracker() {
// As the number of outstanding requests is relatively small, it is cheaper to simply
// regularly iterate over all outstanding requests instead of scheduling callbacks.
this.future = TRACKER_EXECUTOR.scheduleWithFixedDelay(this::reap, 10, 10, TimeUnit.MILLISECONDS);
this.future = TRACKER_EXECUTOR.scheduleWithFixedDelay(
this::reap, REAPING_INTERVAL_MS, REAPING_INTERVAL_MS, TimeUnit.MILLISECONDS);
}

public void register(OngoingRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import okio.ByteString;

@Deprecated
class TrackedOngoingRequestImpl extends ForwardingOngoingRequest {

private final RequestTracker requestTracker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
/**
* An {@link EndpointRunnableFactory} that collect statistics
*/
@Deprecated
class TrackingEndpointRunnableFactory implements EndpointRunnableFactory {

private final EndpointRunnableFactory delegate;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* -\-\-
* Spotify Apollo API Implementations
* --
* Copyright (C) 2013 - 2015 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/
package com.spotify.apollo.request;

import com.spotify.apollo.Request;
import com.spotify.apollo.RequestContext;
import com.spotify.apollo.dispatch.Endpoint;
import com.spotify.apollo.dispatch.EndpointInfo;
import com.spotify.apollo.meta.IncomingCallsGatherer;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class GatheringEndpointRunnableFactoryTest {

@Mock IncomingCallsGatherer incomingCallsGatherer;

@Mock OngoingRequest ongoingRequest;
@Mock RequestContext requestContext;
@Mock Endpoint endpoint;
@Mock EndpointInfo info;

@Mock EndpointRunnableFactory delegate;
@Mock Runnable delegateRunnable;

GatheringEndpointRunnableFactory endpointRunnableFactory;

@Before
public void setUp() throws Exception {
when(ongoingRequest.request()).thenReturn(Request.forUri("http://foo"));
when(endpoint.info()).thenReturn(info);
when(info.getName()).thenReturn("foo");

when(delegate.create(any(), any(), any())).thenReturn(delegateRunnable);

endpointRunnableFactory = new GatheringEndpointRunnableFactory(
delegate, incomingCallsGatherer);
}

@Test
public void shouldRunDelegate() throws Exception {
endpointRunnableFactory.create(ongoingRequest, requestContext, endpoint).run();

verify(delegateRunnable).run();
}

@Test
public void shouldGatherCalls() throws Exception {
endpointRunnableFactory.create(ongoingRequest, requestContext, endpoint).run();

verify(incomingCallsGatherer).gatherIncomingCall(eq(ongoingRequest), eq(endpoint));
}
}
2 changes: 1 addition & 1 deletion apollo-environment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ The decoration chain looks like:

### EndpointRunnableFactory

1. [`TrackingEndpointRunnableFactory`](../apollo-api-impl/src/main/java/com/spotify/apollo/request/TrackingEndpointRunnableFactory.java)
1. [`GatheringEndpointRunnableFactory`](../apollo-api-impl/src/main/java/com/spotify/apollo/request/GatheringEndpointRunnableFactory.java)
1. [`[EndpointRunnableFactory]*`](../apollo-api-impl/src/main/java/com/spotify/apollo/request/EndpointRunnableFactory.java) <- [`Set<EndpointRunnableFactoryDecorator>`](../apollo-environment/src/main/java/com/spotify/apollo/environment/EndpointRunnableFactoryDecorator.java)
1. [`EndpointInvocationHandler`](../apollo-api-impl/src/main/java/com/spotify/apollo/dispatch/EndpointInvocationHandler.java)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import com.spotify.apollo.request.EndpointRunnableFactory;
import com.spotify.apollo.request.RequestHandler;
import com.spotify.apollo.request.RequestRunnableFactory;
import com.spotify.apollo.request.RequestTracker;
import com.spotify.apollo.route.ApplicationRouter;
import com.spotify.apollo.route.Routers;
import com.typesafe.config.Config;
Expand All @@ -53,7 +52,7 @@
import static com.spotify.apollo.request.Handlers.endpointRunnableFactory;
import static com.spotify.apollo.request.Handlers.requestHandler;
import static com.spotify.apollo.request.Handlers.requestRunnableFactory;
import static com.spotify.apollo.request.Handlers.withTracking;
import static com.spotify.apollo.request.Handlers.withGathering;

/**
* A module setting up implementations of Apollo API Framework components such as {@link
Expand Down Expand Up @@ -184,16 +183,14 @@ private RequestHandler createRequestHandler() {
final EndpointRunnableFactory decoratedEndpointRunnableFactory =
foldDecorators(baseEndpointRunnableFactory, erfDecorators);

final RequestTracker requestTracker = new RequestTracker();
final RequestHandler requestHandler = requestHandler(
decoratedRequestRunnableFactory,
withTracking(
withGathering(
decoratedEndpointRunnableFactory,
metaInfoTracker.incomingCallsGatherer(),
requestTracker),
metaInfoTracker.incomingCallsGatherer()
),
incomingRequestAwareClient);

closer.register(requestTracker);
closer.register(() -> LOG.info("Shutting down Apollo instance"));

return requestHandler;
Expand Down

0 comments on commit 4a51c03

Please sign in to comment.