MOBIN-F commented on code in PR #3643: URL: https://github.com/apache/flink-cdc/pull/3643#discussion_r2015711719
########## flink-cdc-e2e-tests/flink-cdc-pipeline-e2e-tests/src/test/java/org/apache/flink/cdc/pipeline/tests/utils/PipelineTestOnYarnEnvironment.java: ########## @@ -0,0 +1,327 @@ +/* + * 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.flink.cdc.pipeline.tests.utils; + +import org.apache.flink.api.common.time.Deadline; +import org.apache.flink.cdc.common.test.utils.TestUtils; +import org.apache.flink.cdc.common.utils.Preconditions; +import org.apache.flink.configuration.ConfigConstants; +import org.apache.flink.core.testutils.CommonTestUtils; +import org.apache.flink.util.TestLogger; + +import org.apache.commons.io.FileUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.service.Service; +import org.apache.hadoop.yarn.api.records.ApplicationId; +import org.apache.hadoop.yarn.api.records.YarnApplicationState; +import org.apache.hadoop.yarn.client.api.YarnClient; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.server.MiniYARNCluster; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.time.Duration; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static java.lang.Thread.sleep; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Fail.fail; + +/** Test environment running pipeline job on YARN mini-cluster. */ +public class PipelineTestOnYarnEnvironment extends TestLogger { + private static final Logger LOG = LoggerFactory.getLogger(PipelineTestOnYarnEnvironment.class); + + protected static final YarnConfiguration YARN_CONFIGURATION; + private YarnClient yarnClient = null; + protected static MiniYARNCluster yarnCluster = null; + + protected static final String TEST_CLUSTER_NAME_KEY = "flink-yarn-minicluster-name"; + protected static final int NUM_NODEMANAGERS = 2; + + protected static File yarnSiteXML = null; + + @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private static final Duration yarnAppTerminateTimeout = Duration.ofSeconds(120); + private static final int sleepIntervalInMS = 100; + + // copy from org.apache.flink.yarn.YarnTestBase + static { + YARN_CONFIGURATION = new YarnConfiguration(); + YARN_CONFIGURATION.setInt(YarnConfiguration.RM_SCHEDULER_MINIMUM_ALLOCATION_MB, 32); + YARN_CONFIGURATION.setInt( + YarnConfiguration.RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, + 4096); // 4096 is the available memory anyways + YARN_CONFIGURATION.setBoolean( + YarnConfiguration.RM_SCHEDULER_INCLUDE_PORT_IN_NODE_NAME, true); + YARN_CONFIGURATION.setInt(YarnConfiguration.RM_AM_MAX_ATTEMPTS, 2); + YARN_CONFIGURATION.setInt(YarnConfiguration.RM_MAX_COMPLETED_APPLICATIONS, 2); + YARN_CONFIGURATION.setInt(YarnConfiguration.RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES, 4); + YARN_CONFIGURATION.setInt(YarnConfiguration.DEBUG_NM_DELETE_DELAY_SEC, 3600); + YARN_CONFIGURATION.setBoolean(YarnConfiguration.LOG_AGGREGATION_ENABLED, false); + YARN_CONFIGURATION.setInt( + YarnConfiguration.NM_VCORES, 666); // memory is overwritten in the MiniYARNCluster. + // so we have to change the number of cores for testing. + YARN_CONFIGURATION.setFloat( + YarnConfiguration.NM_MAX_PER_DISK_UTILIZATION_PERCENTAGE, 99.0F); + YARN_CONFIGURATION.set(YarnConfiguration.YARN_APPLICATION_CLASSPATH, getYarnClasspath()); + YARN_CONFIGURATION.setInt( + YarnConfiguration.RESOURCEMANAGER_CONNECT_RETRY_INTERVAL_MS, 1000); + YARN_CONFIGURATION.setInt(YarnConfiguration.RESOURCEMANAGER_CONNECT_MAX_WAIT_MS, 5000); + YARN_CONFIGURATION.set(TEST_CLUSTER_NAME_KEY, "flink-yarn-tests-application"); + } + + @Before + public void setupYarnClient() throws Exception { + if (yarnClient == null) { + yarnClient = YarnClient.createYarnClient(); + yarnClient.init(getYarnConfiguration()); + yarnClient.start(); + } + } + + @After + public void shutdownYarnClient() { + yarnClient.stop(); + } + + @AfterClass + public static void teardown() { + + if (yarnCluster != null) { + LOG.info("Stopping MiniYarn Cluster"); + yarnCluster.stop(); + yarnCluster = null; + } + + // Unset FLINK_CONF_DIR, as it might change the behavior of other tests + Map<String, String> map = new HashMap<>(System.getenv()); + map.remove(ConfigConstants.ENV_FLINK_CONF_DIR); + map.remove("YARN_CONF_DIR"); + map.remove("IN_TESTS"); + CommonTestUtils.setEnv(map); + + if (yarnSiteXML != null) { + yarnSiteXML.delete(); + } + } + + protected static YarnConfiguration getYarnConfiguration() { + return YARN_CONFIGURATION; + } + + public static void startMiniYARNCluster() { + try { + LOG.info("Starting up MiniYARNCluster"); + if (yarnCluster == null) { + final String testName = + YARN_CONFIGURATION.get(PipelineTestOnYarnEnvironment.TEST_CLUSTER_NAME_KEY); + yarnCluster = + new MiniYARNCluster( + testName == null ? "YarnTest_" + UUID.randomUUID() : testName, + NUM_NODEMANAGERS, + 1, + 1); + + yarnCluster.init(YARN_CONFIGURATION); + yarnCluster.start(); + } + + File targetTestClassesFolder = new File("target/test-classes"); + writeYarnSiteConfigXML(YARN_CONFIGURATION, targetTestClassesFolder); + + Map<String, String> map = new HashMap<String, String>(System.getenv()); + map.put( + "IN_TESTS", + "yes we are in tests"); // see YarnClusterDescriptor() for more infos + map.put("YARN_CONF_DIR", targetTestClassesFolder.getAbsolutePath()); + map.put("MAX_LOG_FILE_NUMBER", "10"); + CommonTestUtils.setEnv(map); + + assertThat(yarnCluster.getServiceState()).isEqualTo(Service.STATE.STARTED); + // wait for the nodeManagers to connect + while (!yarnCluster.waitForNodeManagersToConnect(500)) { + LOG.info("Waiting for Nodemanagers to connect"); + } + } catch (Exception ex) { + ex.printStackTrace(); + LOG.error("setup failure", ex); + fail(""); Review Comment: Done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
