在cloudstack management端,AgentManagerImpl组件负责与agent通信。
在AagentManagerImpl组件中的configure()中对相关线程初始化
_executor = new ThreadPoolExecutor(threads, threads, 60l, TimeUnit.SECONDS, new LinkedBlockingQueue(), new NamedThreadFactory("AgentTaskPool"));
_connectExecutor = new ThreadPoolExecutor(100, 500, 60l, TimeUnit.SECONDS, new LinkedBlockingQueue(), new NamedThreadFactory("AgentConnectTaskPool"));
//allow core threads to time out even when there are no items in the queue
_connectExecutor.allowCoreThreadTimeOut(true);
_connection = new NioServer("AgentManager", Port.value(), Workers.value() + 10, this);
s_logger.info("Listening on " + Port.value() + " with " + Workers.value() + " workers");
// executes all agent commands other than cron and ping
_directAgentExecutor = new ScheduledThreadPoolExecutor(DirectAgentPoolSize.value(), new NamedThreadFactory("DirectAgent"));
// executes cron and ping agent commands
_cronJobExecutor = new ScheduledThreadPoolExecutor(DirectAgentPoolSize.value(), new NamedThreadFactory("DirectAgentCronJob"));
s_logger.debug("Created DirectAgentAttache pool with size: " + DirectAgentPoolSize.value());
_directAgentThreadCap = Math.round(DirectAgentPoolSize.value() * DirectAgentThreadCap.value()) + 1; // add 1 to always make the value > 0
_monitorExecutor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("AgentMonitor"));
在agent端会启动agentShell,agentShell读取配置文件agent.properties,从agent.properties文件中读取resource的值,通过反射得到底层hypervisor的资源服务代理类,例如KVM的为com.cloud.hypervisor.kvm.resource.LibvirtComputingResource.
对resource值的每一种hypervisor,启动一个agent组件。
agent组件启动会启动NioClient,NioClient会运行一个Agent-selector的线程运行IO多路复用器/选择器。
Agent-selector的线程初始运行时执行NioClient的init,NioClient的init方法会创建连接到manager的connect socket,并把connetcSocket register到Seletor,然后创建类型为connect的ServerHandler Task,该类型Task向对端发送StartupCommand request,
请对端(management)发送启动本端的资源(系统VM consoleProxy,secondaryStorage等)。
AgentManagerImpl组件与agent组件进行NioClient类与NioSerer类进行异步通信。NioClient类与NioSerer类由Link封装NioConnection进行数据收发。
Agent的NioClient创建ServerHandler Task来处理接收对端NioServer发送的request和respone数据,其中在处理接收request数据时创建AgentRequestHandler Task来执行。
AgentManagerImpl的NioServer创建AgentHandler Task处理对端的request和response数据。
api命令传入ApiServer通过ApiDispatcher调度和AsnyJob管理框架最终执行指定BaseCmd的excute方法,若该命令需要调用agent端,则excute方法会构建特定的command对象,使用AgentManagerImpl的send封装了command对象的request数据给agent端。