Amoro核心概念和机制


发布于 2025-09-05 / 3 阅读 / 0 评论 /
本文基于amoro-0.9.0,解析amoro中核心概念和机制

Amoro内存使用

AmoroServiceContainer类结构如下图所示:

其中TableService会缓存TableRuntime实例,此对象随着业务的扩大会伴随着内存使用率增加。

Amoro高可用机制

Amoro支持主从模式的高可用,通过LeaderLatch来进行选主。

Amoro的启动过程如下所示:

org.apache.amoro.server.AmoroServiceContainer#main
	// 初始化AmoroServiceContainer实例
	service = new AmoroServiceContainer();
		haContainer = new HighAvailabilityContainer(serviceConfig);
			如果ha.enabled配置为true
				// 在zk上创建leaderPath节点,节点类型为PERSISTENT
				// leaderPath为“{ha.cluster-name:default}/amoro/ams/leader”
				zkClient.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(leaderPath);
				// 初始化leaderLatch实例
				leaderLatch = new LeaderLatch(zkClient, leaderPath);
				leaderLatch.addListener(this);
				// 启动选举
				leaderLatch.start();
			如果ha.enabled配置为false,默认为false
				leaderLatch = null;
				followerLath = new CountDownLatch(1);
	while (true) {
		try {
			service.waitLeaderShip();
				haContainer.waitLeaderShip();
					leaderLatch.await();
			// 服务初始化
			service.startService();
			service.waitFollowerShip();
				haContainer.waitFollowerShip();
					followerLath.await();
		} catch (Exception e) {
			LOG.error("AMS start error", e);
		} finally {
			// 停止leader服务
			service.dispose();
				tableManagementServer.stop();
				optimizingServiceServer.stop();
				httpServer.close();
				tableService.dispose();
				terminalManager.dispose();
				optimizingService.dispose();
				amsServiceMetrics.unregister();
				EventsManager.dispose();
				MetricManager.dispose();
		}
	}

节点成为leader后,对外提供服务,然后等待成为follower,一旦成为follower,则停止对外服务,表示一轮循环结束,然后重新等待成为leader。