|
| 1 | +package com.webank.wedatasphere.streamis.jobmanager.log.collector.flink; |
| 2 | + |
| 3 | +import com.webank.wedatasphere.streamis.jobmanager.log.collector.config.StreamisLogAppenderConfig; |
| 4 | +import com.webank.wedatasphere.streamis.jobmanager.plugin.StreamisConfigAutowired; |
| 5 | +import org.apache.commons.lang3.StringUtils; |
| 6 | +import org.apache.flink.configuration.Configuration; |
| 7 | +import org.apache.flink.configuration.GlobalConfiguration; |
| 8 | +import org.apache.flink.runtime.util.EnvironmentInformation; |
| 9 | +import org.apache.flink.yarn.configuration.YarnConfigOptions; |
| 10 | +import org.apache.logging.log4j.Level; |
| 11 | +import org.apache.logging.log4j.core.Filter; |
| 12 | +import org.apache.logging.log4j.core.filter.LevelMatchFilter; |
| 13 | +import org.apache.logging.log4j.core.filter.RegexFilter; |
| 14 | + |
| 15 | +import java.util.Enumeration; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Properties; |
| 18 | + |
| 19 | +import static com.webank.wedatasphere.streamis.jobmanager.log.collector.flink.FlinkStreamisConfigDefine.*; |
| 20 | + |
| 21 | +/** |
| 22 | + * Autoconfigure the streamis config inf Flink environment |
| 23 | + */ |
| 24 | +public class FlinkStreamisConfigAutowired implements StreamisConfigAutowired { |
| 25 | + |
| 26 | + /** |
| 27 | + * Flink configuration |
| 28 | + */ |
| 29 | + private Configuration configuration; |
| 30 | + |
| 31 | + public FlinkStreamisConfigAutowired(){ |
| 32 | + // First to load configuration |
| 33 | + // We should sleep and wait for append of the flink-yaml.conf |
| 34 | + } |
| 35 | + @Override |
| 36 | + public StreamisLogAppenderConfig logAppenderConfig(StreamisLogAppenderConfig.Builder builder) throws Exception{ |
| 37 | + this.configuration = loadConfiguration(); |
| 38 | + String applicationName = |
| 39 | + this.configuration.getString(YarnConfigOptions.APPLICATION_NAME); |
| 40 | + if (StringUtils.isNotBlank(applicationName)){ |
| 41 | + builder.setAppName(applicationName); |
| 42 | + } |
| 43 | + String gateway = this.configuration.getString(LOG_GATEWAY_ADDRESS); |
| 44 | + if (StringUtils.isNotBlank(gateway)){ |
| 45 | + if (gateway.endsWith("/")){ |
| 46 | + gateway = gateway.substring(0, gateway.length() - 1); |
| 47 | + } |
| 48 | + gateway += this.configuration.getString(LOG_COLLECT_PATH, "/"); |
| 49 | + builder.setRpcAddress(gateway); |
| 50 | + } |
| 51 | + List<String> filterStrategies = this.configuration.get(LOG_FILTER_STRATEGIES); |
| 52 | + for(String filterStrategy : filterStrategies){ |
| 53 | + if ("LevelMatch".equals(filterStrategy)){ |
| 54 | + builder.withFilter(LevelMatchFilter.newBuilder().setOnMatch(Filter.Result.ACCEPT).setOnMismatch(Filter.Result.DENY) |
| 55 | + .setLevel(Level.getLevel(this.configuration.getString(LOG_FILTER_LEVEL_MATCH))).build()); |
| 56 | + } else if ("RegexMatch".equals(filterStrategy)){ |
| 57 | + builder.withFilter(RegexFilter.createFilter( this.configuration.getString(LOG_FILTER_REGEX), |
| 58 | + null, true, Filter.Result.ACCEPT, Filter.Result.DENY)); |
| 59 | + } |
| 60 | + } |
| 61 | + String hadoopUser = EnvironmentInformation.getHadoopUser(); |
| 62 | + if (hadoopUser.equals("<no hadoop dependency found>") || hadoopUser.equals("<unknown>")){ |
| 63 | + hadoopUser = ""; |
| 64 | + } |
| 65 | + return builder.setRpcConnTimeout(this.configuration.getInteger(LOG_RPC_CONN_TIMEOUT)) |
| 66 | + .setRpcSocketTimeout(this.configuration.getInteger(LOG_RPC_SOCKET_TIMEOUT)) |
| 67 | + .setRpcSendRetryCnt(this.configuration.getInteger(LOG_RPC_SEND_RETRY_COUNT)) |
| 68 | + .setRpcServerRecoveryTimeInSec(this.configuration.getInteger(LOG_RPC_SERVER_RECOVERY_TIME)) |
| 69 | + .setRpcMaxDelayTimeInSec(this.configuration.getInteger(LOG_RPC_MAX_DELAY_TIME)) |
| 70 | + .setRpcAuthTokenCodeKey(this.configuration.getString(LOG_RPC_AUTH_TOKEN_CODE_KEY)) |
| 71 | + .setRpcAuthTokenUserKey(this.configuration.getString(LOG_RPC_AUTH_TOKEN_USER_KEY)) |
| 72 | + .setRpcAuthTokenCode(this.configuration.getString(LOG_RPC_AUTH_TOKEN_CODE)) |
| 73 | + .setRpcAuthTokenUser(this.configuration.getString(LOG_RPC_AUTH_TOKEN_USER, |
| 74 | + hadoopUser)) |
| 75 | + .setRpcCacheSize(this.configuration.getInteger(LOG_RPC_CACHE_SIZE)) |
| 76 | + .setRpcCacheMaxConsumeThread(this.configuration.getInteger(LOG_PRC_CACHE_MAX_CONSUME_THREAD)) |
| 77 | + .setRpcBufferSize(this.configuration.getInteger(LOG_RPC_BUFFER_SIZE)) |
| 78 | + .setRpcBufferExpireTimeInSec(this.configuration.getInteger(LOG_RPC_BUFFER_EXPIRE_TIME)).build(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * According to : |
| 83 | + * String launchCommand = |
| 84 | + * BootstrapTools.getTaskManagerShellCommand( |
| 85 | + * flinkConfig, |
| 86 | + * tmParams, |
| 87 | + * ".", |
| 88 | + * ApplicationConstants.LOG_DIR_EXPANSION_VAR, |
| 89 | + * hasLogback, |
| 90 | + * hasLog4j, |
| 91 | + * hasKrb5, |
| 92 | + * taskManagerMainClass, |
| 93 | + * taskManagerDynamicProperties); |
| 94 | + * the configuration directory of Flink yarn container is always ".", |
| 95 | + * @return configuration |
| 96 | + */ |
| 97 | + private synchronized Configuration loadConfiguration(){ |
| 98 | +// String configDir = System.getenv("FLINK_CONF_DIR"); |
| 99 | +// if (null == configDir){ |
| 100 | +// configDir = "."; |
| 101 | +// } |
| 102 | + String configDir = "."; |
| 103 | + Properties properties = System.getProperties(); |
| 104 | + Enumeration<?> enumeration = properties.propertyNames(); |
| 105 | + Configuration dynamicConfiguration = new Configuration(); |
| 106 | + while(enumeration.hasMoreElements()){ |
| 107 | + String prop = String.valueOf(enumeration.nextElement()); |
| 108 | + dynamicConfiguration.setString(prop, properties.getProperty(prop)); |
| 109 | + } |
| 110 | + return GlobalConfiguration.loadConfiguration(configDir, dynamicConfiguration); |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments