-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collect in progress segment download and upload gauges for controller
- Loading branch information
Showing
6 changed files
with
277 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...ain/java/org/apache/pinot/controller/api/resources/InflightRequestMetricsInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.pinot.controller.api.resources; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import java.io.IOException; | ||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerRequestFilter; | ||
import javax.ws.rs.container.ResourceInfo; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.ext.Provider; | ||
import javax.ws.rs.ext.WriterInterceptor; | ||
import javax.ws.rs.ext.WriterInterceptorContext; | ||
import org.apache.pinot.common.metrics.ControllerMetrics; | ||
|
||
|
||
/** | ||
* A class that implements a JXRS request filter and writer interceptor to track the number of in progress requests | ||
* using the gauge specified by the {@link TrackedByGauge} method annotation. | ||
* The gauge specified by this annotation will be incremented when the request starts and decremented once the | ||
* request has completed processing. | ||
* | ||
* See {@link PinotSegmentUploadDownloadRestletResource#downloadSegment} for an example of its usage. | ||
*/ | ||
@Singleton | ||
@Provider | ||
@TrackInflightRequestMetrics | ||
public class InflightRequestMetricsInterceptor implements ContainerRequestFilter, WriterInterceptor { | ||
|
||
@Inject | ||
ControllerMetrics _controllerMetrics; | ||
|
||
@Context | ||
ResourceInfo _resourceInfo; | ||
|
||
public InflightRequestMetricsInterceptor() { | ||
} | ||
|
||
@VisibleForTesting | ||
public InflightRequestMetricsInterceptor(ControllerMetrics controllerMetrics, ResourceInfo resourceInfo) { | ||
_controllerMetrics = controllerMetrics; | ||
_resourceInfo = resourceInfo; | ||
} | ||
|
||
@Override | ||
public void filter(ContainerRequestContext req) | ||
throws IOException { | ||
TrackedByGauge trackedByGauge = _resourceInfo.getResourceMethod().getAnnotation(TrackedByGauge.class); | ||
if (trackedByGauge != null) { | ||
_controllerMetrics.addValueToGlobalGauge(trackedByGauge.gauge(), 1L); | ||
} | ||
} | ||
|
||
@Override | ||
public void aroundWriteTo(WriterInterceptorContext ctx) | ||
throws IOException, WebApplicationException { | ||
try { | ||
ctx.proceed(); | ||
} finally { | ||
TrackedByGauge trackedByGauge = _resourceInfo.getResourceMethod().getAnnotation(TrackedByGauge.class); | ||
if (trackedByGauge != null) { | ||
_controllerMetrics.addValueToGlobalGauge(trackedByGauge.gauge(), -1L); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
.../src/main/java/org/apache/pinot/controller/api/resources/TrackInflightRequestMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.pinot.controller.api.resources; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import javax.ws.rs.NameBinding; | ||
|
||
/** | ||
* JAX-RS annotation used to enable request metrics interceptor {@link InflightRequestMetricsInterceptor}. | ||
* | ||
* See {@link PinotSegmentUploadDownloadRestletResource#downloadSegment} for an example of its usage. | ||
*/ | ||
@NameBinding | ||
@Target({ElementType.METHOD, ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface TrackInflightRequestMetrics { | ||
} |
37 changes: 37 additions & 0 deletions
37
pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/TrackedByGauge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.pinot.controller.api.resources; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import org.apache.pinot.common.metrics.ControllerGauge; | ||
|
||
|
||
/** | ||
* A method-level annotation for tracking inflight request metrics using {@link InflightRequestMetricsInterceptor}. | ||
* The gauge specified by this annotation will be incremented when the request starts and decremented once the request | ||
* has completed processing. | ||
*/ | ||
@Target({ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface TrackedByGauge { | ||
ControllerGauge gauge(); | ||
} |
100 changes: 100 additions & 0 deletions
100
...java/org/apache/pinot/controller/api/resources/InflightRequestMetricsInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.pinot.controller.api.resources; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Method; | ||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ResourceInfo; | ||
import javax.ws.rs.ext.WriterInterceptorContext; | ||
import org.apache.pinot.common.metrics.ControllerGauge; | ||
import org.apache.pinot.common.metrics.ControllerMetrics; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.MockitoAnnotations.initMocks; | ||
|
||
|
||
public class InflightRequestMetricsInterceptorTest { | ||
|
||
@Mock | ||
private ControllerMetrics _controllerMetrics; | ||
|
||
@Mock | ||
private ResourceInfo _resourceInfo; | ||
|
||
@Mock | ||
private ContainerRequestContext _containerRequestContext; | ||
|
||
@Mock | ||
private WriterInterceptorContext _writerInterceptorContext; | ||
|
||
private InflightRequestMetricsInterceptor _interceptor; | ||
|
||
@BeforeMethod | ||
public void setUp() { | ||
initMocks(this); | ||
_interceptor = new InflightRequestMetricsInterceptor(_controllerMetrics, _resourceInfo); | ||
} | ||
|
||
@Test | ||
public void testContainerRequestFilterIncrementsGauge() | ||
throws Exception { | ||
Method methodOne = TrackedClass.class.getDeclaredMethod("trackedMethod"); | ||
when(_resourceInfo.getResourceMethod()).thenReturn(methodOne); | ||
_interceptor.filter(_containerRequestContext); | ||
verify(_controllerMetrics) | ||
.addValueToGlobalGauge(ControllerGauge.CONTROLLER_SEGMENT_DOWNLOADS_IN_PROGRESS_COUNT, 1L); | ||
} | ||
|
||
@Test | ||
public void testWriterInterceptorDecrementsGauge() | ||
throws Exception { | ||
Method methodOne = TrackedClass.class.getDeclaredMethod("trackedMethod"); | ||
when(_resourceInfo.getResourceMethod()).thenReturn(methodOne); | ||
_interceptor.aroundWriteTo(_writerInterceptorContext); | ||
verify(_controllerMetrics) | ||
.addValueToGlobalGauge(ControllerGauge.CONTROLLER_SEGMENT_DOWNLOADS_IN_PROGRESS_COUNT, -1L); | ||
} | ||
|
||
@Test(expectedExceptions = IOException.class) | ||
public void testWriterInterceptorDecrementsGaugeWhenWriterThrowsException() | ||
throws Exception { | ||
Method methodOne = TrackedClass.class.getDeclaredMethod("trackedMethod"); | ||
when(_resourceInfo.getResourceMethod()).thenReturn(methodOne); | ||
doThrow(new IOException()).when(_writerInterceptorContext).proceed(); | ||
try { | ||
_interceptor.aroundWriteTo(_writerInterceptorContext); | ||
} finally { | ||
verify(_controllerMetrics).addValueToGlobalGauge(ControllerGauge.CONTROLLER_SEGMENT_DOWNLOADS_IN_PROGRESS_COUNT, -1L); | ||
} | ||
} | ||
|
||
private static class TrackedClass { | ||
@TrackInflightRequestMetrics | ||
@TrackedByGauge(gauge = ControllerGauge.CONTROLLER_SEGMENT_DOWNLOADS_IN_PROGRESS_COUNT) | ||
public void trackedMethod() { | ||
} | ||
} | ||
} |