Flink 源码 TaskManagerRunner 启动 Akka Actor System 源码

TaskManagerRunner 启动 Akka Actor System 源码关键流程

java 复制代码
private void startTaskManagerRunnerServices() throws Exception {
	synchronized (lock) {
    rpcSystem = RpcSystem.load(configuration);

    this.executor =
            Executors.newScheduledThreadPool(
                    Hardware.getNumberCPUCores(),
                    new ExecutorThreadFactory("taskmanager-future"));

    highAvailabilityServices =
            HighAvailabilityServicesUtils.createHighAvailabilityServices(
                    configuration,
                    executor,
                    AddressResolution.NO_ADDRESS_RESOLUTION,
                    rpcSystem,
                    this);

    JMXService.startInstance(configuration.getString(JMXServerOptions.JMX_SERVER_PORT));

    rpcService = createRpcService(configuration, highAvailabilityServices, rpcSystem);

		// ...
}

// TaskManagerRunner
static RpcService createRpcService(
        final Configuration configuration,
        final HighAvailabilityServices haServices,
        final RpcSystem rpcSystem)
        throws Exception {

    checkNotNull(configuration);
    checkNotNull(haServices);
return RpcUtils.createRemoteRpcService(
            rpcSystem,
            configuration,
						determineTaskManagerBindAddress(configuration, haServices, rpcSystem),
						//...
						);

}

// RpcUtils
public static RpcService createRemoteRpcService(
        RpcSystem rpcSystem,
        Configuration configuration,
        @Nullable String externalAddress,
        String externalPortRange,
        @Nullable String bindAddress,
        @SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<Integer> bindPort)
        throws Exception {
    RpcSystem.RpcServiceBuilder rpcServiceBuilder =
            rpcSystem.remoteServiceBuilder(configuration, externalAddress, externalPortRange);
    if (bindAddress != null) {
        rpcServiceBuilder = rpcServiceBuilder.withBindAddress(bindAddress);
    }
    if (bindPort.isPresent()) {
        rpcServiceBuilder = rpcServiceBuilder.withBindPort(bindPort.get());
    }
    return rpcServiceBuilder.createAndStart();
}




// AkkaRpcServiceUtils
public AkkaRpcService createAndStart(
            TriFunction<ActorSystem, AkkaRpcServiceConfiguration, ClassLoader, AkkaRpcService>
                    constructor)
            throws Exception {
        if (actorSystemExecutorConfiguration == null) {
            actorSystemExecutorConfiguration =
                    AkkaUtils.getForkJoinExecutorConfig(
                            AkkaBootstrapTools.getForkJoinExecutorConfiguration(configuration));
        }

        final ActorSystem actorSystem;

        // akka internally caches the context class loader
        // make sure it uses the plugin class loader
        try (TemporaryClassLoaderContext ignored =
                TemporaryClassLoaderContext.of(getClass().getClassLoader())) {
            if (externalAddress == null) {
                // create local actor system
                actorSystem =
                        AkkaBootstrapTools.startLocalActorSystem(
                                configuration,
                                actorSystemName,
                                logger,
                                actorSystemExecutorConfiguration,
                                customConfig);
            } else {
                // create remote actor system
                actorSystem =
                        AkkaBootstrapTools.startRemoteActorSystem(
                                configuration,
                                actorSystemName,
                                externalAddress,
                                externalPortRange,
                                bindAddress,
                                Optional.ofNullable(bindPort),
                                logger,
                                actorSystemExecutorConfiguration,
                                customConfig);
            }
        }

        return constructor.apply(
                actorSystem,
                AkkaRpcServiceConfiguration.fromConfiguration(configuration),
                RpcService.class.getClassLoader());
    }
}


// AkkaBootstrapTools
private static ActorSystem startRemoteActorSystem(
        Configuration configuration,
        String actorSystemName,
        String externalAddress,
        int externalPort,
        String bindAddress,
        int bindPort,
        Logger logger,
        Config actorSystemExecutorConfiguration,
        Config customConfig)
        throws Exception {

    String externalHostPortUrl =
            NetUtils.unresolvedHostAndPortToNormalizedString(externalAddress, externalPort);
    String bindHostPortUrl =
            NetUtils.unresolvedHostAndPortToNormalizedString(bindAddress, bindPort);
    logger.info(
            "Trying to start actor system, external address {}, bind address {}.",
            externalHostPortUrl,
            bindHostPortUrl);

    try {
        Config akkaConfig =
                AkkaUtils.getAkkaConfig(
                        configuration,
                        new HostAndPort(externalAddress, externalPort),
                        new HostAndPort(bindAddress, bindPort),
                        actorSystemExecutorConfiguration);

        if (customConfig != null) {
            akkaConfig = customConfig.withFallback(akkaConfig);
        }

        return startActorSystem(akkaConfig, actorSystemName, logger);
    } catch (Throwable t) {
        if (t instanceof ChannelException) {
            Throwable cause = t.getCause();
            if (cause != null && t.getCause() instanceof BindException) {
                throw new IOException(
                        "Unable to create ActorSystem at address "
                                + bindHostPortUrl
                                + " : "
                                + cause.getMessage(),
                        t);
            }
        }
        throw new Exception("Could not create actor system", t);
    }
}




private static ActorSystem startActorSystem(
        Config akkaConfig, String actorSystemName, Logger logger) {
    logger.debug("Using akka configuration\n {}", akkaConfig);
    ActorSystem actorSystem = AkkaUtils.createActorSystem(actorSystemName, akkaConfig);

    logger.info("Actor system started at {}", AkkaUtils.getAddress(actorSystem));
    return actorSystem;
}



// AkkaUtils
public static ActorSystem createActorSystem(String actorSystemName, Config akkaConfig) {
    // Initialize slf4j as logger of Akka's Netty instead of java.util.logging (FLINK-1650)
    InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
    return RobustActorSystem.create(actorSystemName, akkaConfig);
}


public static RobustActorSystem create(String name, Config applicationConfig) {
    return create(name, applicationConfig, FatalExitExceptionHandler.INSTANCE);
}


@VisibleForTesting
static RobustActorSystem create(
        String name,
        Config applicationConfig,
        Thread.UncaughtExceptionHandler uncaughtExceptionHandler) {
    return create(
            name,
            ActorSystemSetup.create(
                    BootstrapSetup.create(
                            Optional.empty(),
                            Optional.of(applicationConfig),
                            Optional.empty())),
            Option.apply(uncaughtExceptionHandler));
}


private static RobustActorSystem create(
        String name,
        ActorSystemSetup setup,
        Option<Thread.UncaughtExceptionHandler> uncaughtExceptionHandler) {
    final Optional<BootstrapSetup> bootstrapSettings = setup.get(BootstrapSetup.class);
    final ClassLoader classLoader = RobustActorSystem.class.getClassLoader();
    final Config appConfig =
            bootstrapSettings
                    .map(BootstrapSetup::config)
                    .flatMap(RobustActorSystem::toJavaOptional)
                    .orElseGet(() -> ConfigFactory.load(classLoader));
    final Option<ExecutionContext> defaultEC =
            toScalaOption(
                    bootstrapSettings
                            .map(BootstrapSetup::defaultExecutionContext)
                            .flatMap(RobustActorSystem::toJavaOptional));

    final RobustActorSystem robustActorSystem =
            new RobustActorSystem(name, appConfig, classLoader, defaultEC, setup) {
                @Override
                public Thread.UncaughtExceptionHandler uncaughtExceptionHandler() {
                    return uncaughtExceptionHandler.getOrElse(super::uncaughtExceptionHandler);
                }
            };
    robustActorSystem.start();
    return robustActorSystem;
}



private static RobustActorSystem create(
        String name,
        ActorSystemSetup setup,
        Option<Thread.UncaughtExceptionHandler> uncaughtExceptionHandler) {
    final Optional<BootstrapSetup> bootstrapSettings = setup.get(BootstrapSetup.class);
    final ClassLoader classLoader = RobustActorSystem.class.getClassLoader();
    final Config appConfig =
            bootstrapSettings
                    .map(BootstrapSetup::config)
                    .flatMap(RobustActorSystem::toJavaOptional)
                    .orElseGet(() -> ConfigFactory.load(classLoader));
    final Option<ExecutionContext> defaultEC =
            toScalaOption(
                    bootstrapSettings
                            .map(BootstrapSetup::defaultExecutionContext)
                            .flatMap(RobustActorSystem::toJavaOptional));

    final RobustActorSystem robustActorSystem =
            new RobustActorSystem(name, appConfig, classLoader, defaultEC, setup) {
                @Override
                public Thread.UncaughtExceptionHandler uncaughtExceptionHandler() {
                    return uncaughtExceptionHandler.getOrElse(super::uncaughtExceptionHandler);
                }
            };
    robustActorSystem.start();
    return robustActorSystem;
}
相关推荐
希忘auto15 小时前
详解Redis的常用命令
redis·1024程序员节
yaosheng_VALVE1 天前
探究全金属硬密封蝶阀的奥秘-耀圣控制
运维·eclipse·自动化·pyqt·1024程序员节
dami_king1 天前
SSH特性|组成|SSH是什么?
运维·ssh·1024程序员节
一个通信老学姐6 天前
专业125+总分400+南京理工大学818考研经验南理工电子信息与通信工程,真题,大纲,参考书。
考研·信息与通信·信号处理·1024程序员节
sheng12345678rui6 天前
mfc140.dll文件缺失的修复方法分享,全面分析mfc140.dll的几种解决方法
游戏·电脑·dll文件·dll修复工具·1024程序员节
huipeng9267 天前
第十章 类和对象(二)
java·开发语言·学习·1024程序员节
earthzhang20217 天前
《深入浅出HTTPS》读书笔记(19):密钥
开发语言·网络协议·算法·https·1024程序员节
爱吃生蚝的于勒8 天前
计算机基础 原码反码补码问题
经验分享·笔记·计算机网络·其他·1024程序员节
earthzhang20218 天前
《深入浅出HTTPS》读书笔记(20):口令和PEB算法
开发语言·网络协议·算法·https·1024程序员节
一个通信老学姐8 天前
专业140+总分410+浙江大学842信号系统与数字电路考研经验浙大电子信息与通信工程,真题,大纲,参考书。
考研·信息与通信·信号处理·1024程序员节