|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.e2e.connector.fake; |
| 19 | + |
| 20 | +import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.JsonNode; |
| 21 | +import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | + |
| 23 | +import org.apache.seatunnel.api.common.metrics.MetricNames; |
| 24 | +import org.apache.seatunnel.common.utils.JsonUtils; |
| 25 | +import org.apache.seatunnel.e2e.common.TestSuiteBase; |
| 26 | +import org.apache.seatunnel.e2e.common.container.EngineType; |
| 27 | +import org.apache.seatunnel.e2e.common.container.TestContainer; |
| 28 | +import org.apache.seatunnel.e2e.common.container.flink.Flink13Container; |
| 29 | +import org.apache.seatunnel.e2e.common.junit.DisabledOnContainer; |
| 30 | + |
| 31 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 32 | +import org.apache.http.client.methods.HttpGet; |
| 33 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 34 | +import org.apache.http.impl.client.HttpClients; |
| 35 | +import org.apache.http.util.EntityUtils; |
| 36 | + |
| 37 | +import org.awaitility.Awaitility; |
| 38 | +import org.junit.jupiter.api.Assertions; |
| 39 | +import org.junit.jupiter.api.TestTemplate; |
| 40 | +import org.slf4j.Logger; |
| 41 | +import org.slf4j.LoggerFactory; |
| 42 | +import org.testcontainers.containers.Container; |
| 43 | + |
| 44 | +import java.io.IOException; |
| 45 | +import java.net.URI; |
| 46 | +import java.util.HashMap; |
| 47 | +import java.util.Map; |
| 48 | +import java.util.concurrent.TimeUnit; |
| 49 | + |
| 50 | +@DisabledOnContainer( |
| 51 | + value = {}, |
| 52 | + type = {EngineType.SPARK, EngineType.SEATUNNEL}) |
| 53 | +public class FlinkMetricsIT extends TestSuiteBase { |
| 54 | + |
| 55 | + private static final Logger LOGGER = LoggerFactory.getLogger(FlinkMetricsIT.class); |
| 56 | + |
| 57 | + @TestTemplate |
| 58 | + public void testFlinkMetrics(TestContainer container) throws IOException, InterruptedException { |
| 59 | + Container.ExecResult executeResult = |
| 60 | + container.executeJob("/fake_to_assert_verify_flink_metrics.conf"); |
| 61 | + Assertions.assertEquals(0, executeResult.getExitCode()); |
| 62 | + final String jobListUrl = "http://%s:8081/jobs/overview"; |
| 63 | + final String jobDetailsUrl = "http://%s:8081/jobs/%s"; |
| 64 | + final String jobAccumulatorUrl = "http://%s:8081/jobs/%s/vertices/%s/accumulators"; |
| 65 | + final String jobManagerHost; |
| 66 | + String dockerHost = System.getenv("DOCKER_HOST"); |
| 67 | + if (dockerHost == null) { |
| 68 | + jobManagerHost = "localhost"; |
| 69 | + } else { |
| 70 | + URI uri = URI.create(dockerHost); |
| 71 | + jobManagerHost = uri.getHost(); |
| 72 | + } |
| 73 | + // create http client |
| 74 | + CloseableHttpClient httpClient = HttpClients.createDefault(); |
| 75 | + |
| 76 | + // get job id |
| 77 | + HttpGet httpGet = new HttpGet(String.format(jobListUrl, jobManagerHost)); |
| 78 | + CloseableHttpResponse response = httpClient.execute(httpGet); |
| 79 | + Assertions.assertEquals(response.getStatusLine().getStatusCode(), 200); |
| 80 | + String responseContent = EntityUtils.toString(response.getEntity()); |
| 81 | + ObjectNode jsonNode = JsonUtils.parseObject(responseContent); |
| 82 | + String jobId = jsonNode.get("jobs").get(0).get("jid").asText(); |
| 83 | + Assertions.assertNotNull(jobId); |
| 84 | + |
| 85 | + // get job vertices |
| 86 | + httpGet = new HttpGet(String.format(jobDetailsUrl, jobManagerHost, jobId)); |
| 87 | + response = httpClient.execute(httpGet); |
| 88 | + Assertions.assertEquals(response.getStatusLine().getStatusCode(), 200); |
| 89 | + |
| 90 | + responseContent = EntityUtils.toString(response.getEntity()); |
| 91 | + jsonNode = JsonUtils.parseObject(responseContent); |
| 92 | + String verticeId = jsonNode.get("vertices").get(0).get("id").asText(); |
| 93 | + |
| 94 | + Awaitility.given() |
| 95 | + .ignoreExceptions() |
| 96 | + .await() |
| 97 | + .atMost(10L, TimeUnit.SECONDS) |
| 98 | + .untilAsserted( |
| 99 | + () -> { |
| 100 | + HttpGet httpGetTemp = |
| 101 | + new HttpGet( |
| 102 | + String.format( |
| 103 | + jobAccumulatorUrl, |
| 104 | + jobManagerHost, |
| 105 | + jobId, |
| 106 | + verticeId)); |
| 107 | + CloseableHttpResponse responseTemp = httpClient.execute(httpGetTemp); |
| 108 | + String responseContentTemp = |
| 109 | + EntityUtils.toString(responseTemp.getEntity()); |
| 110 | + JsonNode jsonNodeTemp = JsonUtils.parseObject(responseContentTemp); |
| 111 | + JsonNode metrics = jsonNodeTemp.get("user-accumulators"); |
| 112 | + int size = metrics.size(); |
| 113 | + if (size <= 0) { |
| 114 | + throw new IllegalStateException( |
| 115 | + "Flink metrics not synchronized yet, next round"); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + // get metrics |
| 120 | + httpGet = new HttpGet(String.format(jobAccumulatorUrl, jobManagerHost, jobId, verticeId)); |
| 121 | + response = httpClient.execute(httpGet); |
| 122 | + responseContent = EntityUtils.toString(response.getEntity()); |
| 123 | + jsonNode = JsonUtils.parseObject(responseContent); |
| 124 | + JsonNode metrics = jsonNode.get("user-accumulators"); |
| 125 | + |
| 126 | + int size = metrics.size(); |
| 127 | + |
| 128 | + Assertions.assertTrue(size > 0); |
| 129 | + |
| 130 | + Map<String, String> metricsMap = new HashMap<>(); |
| 131 | + |
| 132 | + for (JsonNode metric : metrics) { |
| 133 | + String name = metric.get("name").asText(); |
| 134 | + String value = metric.get("value").asText(); |
| 135 | + metricsMap.put(name, value); |
| 136 | + } |
| 137 | + |
| 138 | + String sourceReceivedCount = metricsMap.get(MetricNames.SOURCE_RECEIVED_COUNT); |
| 139 | + String sourceReceivedBytes = metricsMap.get(MetricNames.SOURCE_RECEIVED_BYTES); |
| 140 | + |
| 141 | + Assertions.assertEquals(5, Integer.valueOf(sourceReceivedCount)); |
| 142 | + Assertions.assertEquals(2160, Integer.valueOf(sourceReceivedBytes)); |
| 143 | + |
| 144 | + // Due to limitations in Flink 13 version and code, the metrics on the writer side cannot be |
| 145 | + // aggregated into the global accumulator and can only be viewed in the operator based on |
| 146 | + // parallelism dimensions |
| 147 | + if (!(container instanceof Flink13Container)) { |
| 148 | + String sinkWriteCount = metricsMap.get(MetricNames.SINK_WRITE_COUNT); |
| 149 | + String sinkWriteBytes = metricsMap.get(MetricNames.SINK_WRITE_BYTES); |
| 150 | + Assertions.assertEquals(5, Integer.valueOf(sinkWriteCount)); |
| 151 | + Assertions.assertEquals(2160, Integer.valueOf(sinkWriteBytes)); |
| 152 | + } |
| 153 | + |
| 154 | + httpClient.close(); |
| 155 | + } |
| 156 | +} |
0 commit comments