前言
前几篇文章主要为大家介绍了aeron的订阅和发布流程,本篇文章则会开启一个新的知识点:aeron archive,以下就是aeron官方对archive组件的职责描述:
csharp
Aeron Archive enables the recording and replay of streams from durable storage.
即:aeron archive可以用来记录和回放来自持久化存储的流,架构图如下:
可以看到aeron archive是由两部分组成:
- aeron archive:用来持久化数据流到硬盘当中,并可以回放流数据
- aeron archive client:用来接收回放的数据流。
这里可以做个类比,areon archive持久化与回放的过程就好比mysql binlog回放,只不过回放bin log可能还要借助一些中间件,aeron框架则天然就支持,这个功能可以给我们架构设计带来更多的可能,毕竟异步解藕的设计谁能不爱呢。下面就让我们逐层的揭开archive的神秘面纱。
Archive设计
这里我更喜欢把Archive对象称为archive server,用来区分数据的流转,其初始化是通过Archive.launch方法实现的整体初始化流程如下:

上面也简单介绍过,archive主要用来持久化数据并回放的,那么,其到底是借助什么来实现的持久化呢?
观察流程图的右半部分可以看出,在DriverConductor里,有很多方法都会调用onAvailableImage,image这个对象实际和aeron的sub有关,也就是说,一旦某台机器上的aeron集群,可以正常的接收到其他服务器或者业务的pub数据,则就可以触发数据的持久化,也就是流程图中标红的recording。
当调用AeronArchive的addRecordedPublication添加recorded的发布者时,会触发start recording, 该动作会被封装成对应的task放到一个queue里。
Archive.lanunch方法最终实现,就是遍历上述的queue,执行对应的task逻辑。
AeronArchive 设计
AeronArchive对象可以看作archive client,主要包含client的各种功能。和前文介绍的aeron对象初始化类似,AeronArchive对象同样也有一个connect方法,查看实现如下:
aeron archive connect
ini
public static AeronArchive connect(Context ctx) {
Subscription subscription = null;
Publication publication = null;
AsyncConnect asyncConnect = null;
try {
ctx.conclude();
Aeron aeron = ctx.aeron();
subscription = aeron.addSubscription(ctx.controlResponseChannel(), ctx.controlResponseStreamId());
publication = aeron.addExclusivePublication(ctx.controlRequestChannel(), ctx.controlRequestStreamId());
ControlResponsePoller controlResponsePoller = new ControlResponsePoller(subscription);
ArchiveProxy archiveProxy = new ArchiveProxy(publication, ctx.idleStrategy(), aeron.context().nanoClock(), ctx.messageTimeoutNs(), 3, ctx.credentialsSupplier());
asyncConnect = new AsyncConnect(ctx, controlResponsePoller, archiveProxy);
IdleStrategy idleStrategy = ctx.idleStrategy();
AgentInvoker aeronClientInvoker = aeron.conductorAgentInvoker();
AgentInvoker delegatingInvoker = ctx.agentInvoker();
int previousStep = asyncConnect.step();
AeronArchive aeronArchive;
while(null == (aeronArchive = asyncConnect.poll())) {
if (asyncConnect.step() == previousStep) {
idleStrategy.idle();
} else {
idleStrategy.reset();
previousStep = asyncConnect.step();
}
if (null != aeronClientInvoker) {
aeronClientInvoker.invoke();
}
if (null != delegatingInvoker) {
delegatingInvoker.invoke();
}
}
return aeronArchive;
} catch (ConcurrentConcludeException var12) {
// 异常处理
} catch (Exception var13) {
// 异常处理
}
}
可以看到,其connect方法,主要初始化 control Channel的订阅和发布,这个channel是发布者和订阅者之间用来进行通信的。整体流程如下:
AeronArchive可以还用来发布recording命令,replayer命令,具体流程后面在单独分析。 执行connect过程中还会涉及到一些其他对象,对应的职责描述如下:
- ArchiveProxy:对命令record、replay进行encoder
- ArchiveConductor:持有Replayer,Recorder
- ControlSessionDemuxer:用来处理命令decoder。处理replay、record的入口
- Session:ReplaySession、RecorderSession用来处理具体读写操作
小节
本篇文章主要为大家简单介绍了aeron archive的职责和初始化的流程,从而引出了两个新的概念,recoder以及replayer,这两个新的概念会在下一篇文章进行揭秘。