|
19 | 19 |
|
20 | 20 | package org.apache.spark.k8s.operator.metrics.source; |
21 | 21 |
|
| 22 | +import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED; |
| 23 | +import static org.awaitility.Awaitility.await; |
22 | 24 | import static org.junit.jupiter.api.Assertions.assertThrows; |
23 | 25 |
|
| 26 | +import java.nio.ByteBuffer; |
24 | 27 | import java.util.Arrays; |
25 | 28 | import java.util.HashMap; |
26 | 29 | import java.util.List; |
27 | 30 | import java.util.Map; |
28 | 31 |
|
| 32 | +import com.codahale.metrics.Histogram; |
29 | 33 | import com.codahale.metrics.Meter; |
30 | 34 | import com.codahale.metrics.Metric; |
| 35 | +import com.codahale.metrics.Snapshot; |
31 | 36 | import io.fabric8.kubernetes.api.model.ConfigMap; |
32 | 37 | import io.fabric8.kubernetes.api.model.ObjectMeta; |
| 38 | +import io.fabric8.kubernetes.api.model.StatusBuilder; |
| 39 | +import io.fabric8.kubernetes.client.Config; |
| 40 | +import io.fabric8.kubernetes.client.ConfigBuilder; |
33 | 41 | import io.fabric8.kubernetes.client.KubernetesClient; |
| 42 | +import io.fabric8.kubernetes.client.http.AsyncBody; |
34 | 43 | import io.fabric8.kubernetes.client.http.Interceptor; |
| 44 | +import io.fabric8.kubernetes.client.informers.SharedIndexInformer; |
35 | 45 | import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; |
36 | 46 | import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; |
37 | 47 | import org.junit.jupiter.api.AfterEach; |
|
50 | 60 | @TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
51 | 61 | class KubernetesMetricsInterceptorTest { |
52 | 62 |
|
| 63 | + private static final String CONFIG_MAP_PATH = |
| 64 | + "/api/v1/namespaces/spark-system/configmaps/spark-job-operator-configuration"; |
| 65 | + |
53 | 66 | private KubernetesMockServer mockServer; |
54 | 67 | private KubernetesClient kubernetesClient; |
55 | 68 |
|
@@ -125,6 +138,137 @@ void testWhenKubernetesServerNotWorking() { |
125 | 138 | } |
126 | 139 | } |
127 | 140 |
|
| 141 | + @Test |
| 142 | + @Order(3) |
| 143 | + void testResponseLatency() { |
| 144 | + KubernetesMetricsInterceptor metricsInterceptor = new KubernetesMetricsInterceptor(); |
| 145 | + List<Interceptor> interceptors = List.of(metricsInterceptor); |
| 146 | + try (KubernetesClient client = |
| 147 | + KubernetesClientFactory.buildKubernetesClient( |
| 148 | + interceptors, kubernetesClient.getConfiguration())) { |
| 149 | + ConfigMap configMap = createConfigMap(); |
| 150 | + mockServer |
| 151 | + .expect() |
| 152 | + .get() |
| 153 | + .delay(50) |
| 154 | + .withPath(CONFIG_MAP_PATH) |
| 155 | + .andReturn(200, configMap) |
| 156 | + .once(); |
| 157 | + client.resource(configMap).get(); |
| 158 | + |
| 159 | + Histogram latency = latency(metricsInterceptor); |
| 160 | + Assertions.assertEquals(1, latency.getCount()); |
| 161 | + Snapshot snapshot = latency.getSnapshot(); |
| 162 | + Assertions.assertTrue(snapshot.getMax() >= 50_000_000L, () -> "max: " + snapshot.getMax()); |
| 163 | + Assertions.assertTrue( |
| 164 | + snapshot.getMax() < 30_000_000_000L, () -> "max: " + snapshot.getMax()); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + @Order(4) |
| 170 | + void testResponseLatencyOfResentRequest() { |
| 171 | + KubernetesMetricsInterceptor metricsInterceptor = new KubernetesMetricsInterceptor(); |
| 172 | + List<Interceptor> interceptors = List.of(metricsInterceptor); |
| 173 | + Config config = |
| 174 | + new ConfigBuilder(kubernetesClient.getConfiguration()) |
| 175 | + .withOauthTokenProvider(() -> "token") |
| 176 | + .build(); |
| 177 | + try (KubernetesClient client = |
| 178 | + KubernetesClientFactory.buildKubernetesClient(interceptors, config)) { |
| 179 | + ConfigMap configMap = createConfigMap(); |
| 180 | + mockServer |
| 181 | + .expect() |
| 182 | + .get() |
| 183 | + .delay(200) |
| 184 | + .withPath(CONFIG_MAP_PATH) |
| 185 | + .andReturn(HTTP_UNAUTHORIZED, new StatusBuilder().withCode(HTTP_UNAUTHORIZED).build()) |
| 186 | + .once(); |
| 187 | + mockServer.expect().get().withPath(CONFIG_MAP_PATH).andReturn(200, configMap).once(); |
| 188 | + client.resource(configMap).get(); |
| 189 | + |
| 190 | + Assertions.assertEquals(2, meterCount(metricsInterceptor, "http.response")); |
| 191 | + Assertions.assertEquals(1, meterCount(metricsInterceptor, "http.response.401")); |
| 192 | + Assertions.assertEquals(0, meterCount(metricsInterceptor, "failed")); |
| 193 | + Histogram latency = latency(metricsInterceptor); |
| 194 | + Assertions.assertEquals(2, latency.getCount()); |
| 195 | + Snapshot snapshot = latency.getSnapshot(); |
| 196 | + Assertions.assertTrue(snapshot.getMax() >= 200_000_000L, () -> "max: " + snapshot.getMax()); |
| 197 | + Assertions.assertTrue(snapshot.getMin() < 200_000_000L, () -> "min: " + snapshot.getMin()); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + @Order(5) |
| 203 | + void testResponseLatencyOfWebSocketUpgrade() { |
| 204 | + KubernetesMetricsInterceptor metricsInterceptor = new KubernetesMetricsInterceptor(); |
| 205 | + List<Interceptor> interceptors = List.of(metricsInterceptor); |
| 206 | + try (KubernetesClient client = |
| 207 | + KubernetesClientFactory.buildKubernetesClient( |
| 208 | + interceptors, kubernetesClient.getConfiguration()); |
| 209 | + SharedIndexInformer<ConfigMap> informer = |
| 210 | + client.configMaps().inNamespace("spark-system").inform()) { |
| 211 | + await() |
| 212 | + .untilAsserted( |
| 213 | + () -> { |
| 214 | + Assertions.assertEquals(2, meterCount(metricsInterceptor, "http.response")); |
| 215 | + Assertions.assertEquals(1, meterCount(metricsInterceptor, "http.response.101")); |
| 216 | + Assertions.assertEquals(1, latency(metricsInterceptor).getCount()); |
| 217 | + }); |
| 218 | + Assertions.assertTrue(informer.isWatching()); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + @Order(6) |
| 224 | + void testResponseLatencyOfTwoInterceptors() { |
| 225 | + KubernetesMetricsInterceptor first = new KubernetesMetricsInterceptor(); |
| 226 | + KubernetesMetricsInterceptor second = new KubernetesMetricsInterceptor() {}; |
| 227 | + List<Interceptor> interceptors = List.of(first, second); |
| 228 | + try (KubernetesClient client = |
| 229 | + KubernetesClientFactory.buildKubernetesClient( |
| 230 | + interceptors, kubernetesClient.getConfiguration())) { |
| 231 | + ConfigMap configMap = createConfigMap(); |
| 232 | + mockServer |
| 233 | + .expect() |
| 234 | + .get() |
| 235 | + .delay(50) |
| 236 | + .withPath(CONFIG_MAP_PATH) |
| 237 | + .andReturn(200, configMap) |
| 238 | + .once(); |
| 239 | + client.resource(configMap).get(); |
| 240 | + |
| 241 | + for (KubernetesMetricsInterceptor interceptor : List.of(first, second)) { |
| 242 | + Histogram latency = latency(interceptor); |
| 243 | + Assertions.assertEquals(1, latency.getCount()); |
| 244 | + Snapshot snapshot = latency.getSnapshot(); |
| 245 | + Assertions.assertTrue(snapshot.getMin() >= 50_000_000L, () -> "min: " + snapshot.getMin()); |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + @Test |
| 251 | + @Order(7) |
| 252 | + void testConsumerUnwrapsDelegate() { |
| 253 | + AsyncBody.Consumer<List<ByteBuffer>> delegate = (value, asyncBody) -> {}; |
| 254 | + AsyncBody.Consumer<List<ByteBuffer>> consumer = |
| 255 | + new KubernetesMetricsInterceptor().consumer(delegate, null); |
| 256 | + Assertions.assertSame(delegate, consumer.unwrap(delegate.getClass())); |
| 257 | + } |
| 258 | + |
| 259 | + private static long meterCount(KubernetesMetricsInterceptor interceptor, String name) { |
| 260 | + Meter meter = interceptor.metricRegistry().getMeters().get(name); |
| 261 | + Assertions.assertNotNull(meter, name); |
| 262 | + return meter.getCount(); |
| 263 | + } |
| 264 | + |
| 265 | + private static Histogram latency(KubernetesMetricsInterceptor interceptor) { |
| 266 | + Histogram histogram = |
| 267 | + interceptor.metricRegistry().getHistograms().get("http.response.latency.nanos"); |
| 268 | + Assertions.assertNotNull(histogram, "http.response.latency.nanos"); |
| 269 | + return histogram; |
| 270 | + } |
| 271 | + |
128 | 272 | private static SparkApplication createSparkApplication() { |
129 | 273 | ObjectMeta meta = new ObjectMeta(); |
130 | 274 | meta.setName("sample-spark-application"); |
|
0 commit comments