PersistentQueue类主要用于在内存中维护一个队列,该类是Kestrel的核心类,是真正对于一个队列的维护。我们看到上文中QueueCollection中主要是调用下面的setup方法,下面我们看看setup方法到底做了什么
def setup() {
synchronized {
queueSize = 0
replayJournal()
}
}
def replayJournal() {
if (!config.keepJournal) return
log.info("Replaying transaction journal for '%s'", name)
xidCounter = 0
journal.replay {
case JournalItem.Add(item) =>
_add(item)
// when processing the journal, this has to happen after:
if (!journal.inReadBehind && queueSize >= config.maxMemorySize.inBytes) {
log.info("Dropping to read-behind for queue '%s' (%d bytes)", name, queueSize)
journal.startReadBehind()
}
case JournalItem.Remove => _remove(false, None)
case JournalItem.RemoveTentative(xid) =>
_remove(true, Some(xid))
xidCounter = xid
case JournalItem.SavedXid(xid) => xidCounter = xid
case JournalItem.Unremove(xid) => _unremove(xid)
case JournalItem.ConfirmRemove(xid) => openTransactions.remove(xid)
case JournalItem.Continue(item, xid) =>
openTransactions.remove(xid)
_add(item)
case x => log.error("Unexpected item in journal: %s", x)
}
log.info("Finished transaction journal for '%s' (%d items, %d bytes) xid=%d", name, queueLength,
journal.size, xidCounter)
journal.open()
// now, any unfinished transactions must be backed out.
for (xid <- openTransactionIds) {
journal.unremove(xid)
_unremove(xid)
}
}
我们看到setup方法主要是回放journal文件中的redo Log。我们看到该类主要是读Journal文件,并解析为JournalItem对象,并根据对象类型对PersistentQueue进行入队和出队。我们看到一共有7种不同的日志类型,下面我们看看几个主要的类型日志的结构
ADDX:
Remove:
我们会看到在回放日志文件的同时,他会记录所有到目前为止所有的没有结束的事务在openTransactions中,其中他们的key的集合为openTransactionIds,对这些没有结束的事务做unremove操作。
另外还有一个关键是在对队列进行Add和Remove操作时调用checkRotateJournal()方法,即检查是否需要对日志文件进行切分、删除等操作。下面我们看看具体的代码
// you are holding the lock, and config.keepJournal is true.
private def checkRotateJournal() {
/*
* if the queue is empty, and the journal is larger than defaultJournalSize, rebuild it.
* if the queue is smaller than maxMemorySize, and the combined journals are larger than
* maxJournalSize, rebuild them. (we are not in read-behind.)
* if the current journal is larger than maxMemorySize, rotate to a new file. if the combined
* journals are larger than maxJournalSize, checkpoint in preparation for rebuilding the
* older files in the background.
*/
if ((journal.size >= config.defaultJournalSize.inBytes && queueLength == 0) ||
(journal.size + journal.archivedSize > config.maxJournalSize.inBytes &&
queueSize < config.maxMemorySize.inBytes)) {
log.info("Rewriting journal file for '%s' (qsize=%d)", name, queueSize)
journal.rewrite(openTransactionIds.map { openTransactions(_) }, queue)
} else if (journal.size > config.maxMemorySize.inBytes) {
log.info("Rotating journal file for '%s' (qsize=%d)", name, queueSize)
val setCheckpoint = (journal.size + journal.archivedSize > config.maxJournalSize.inBytes)
journal.rotate(openTransactionIds.map { openTransactions(_) }, setCheckpoint)
}
}
1. 当内存中queue为空,并且日志文件的大小大于defaultJournalSize时,将创建一个新的日志文件并删除该时间点之前的文件。
2. 当之前的日志文件大小加上当前日志文件大小大于maxJournalSize,并且queue中数据的大小小于maxMemorySize时,将创建一个新的日志文件并删除该时间点之前的文件。
3. 当日志文件的大小大于maxMemorySize,将创建新的日志文件,之后达到的消息将存入新的文件。
分享到:
相关推荐
NULL 博文链接:https://vanadiumlin.iteye.com/blog/1461152
NULL 博文链接:https://snowolf.iteye.com/blog/1604531
这一步就不详细说了,有免费的和收费的,申请完成之后会给你一个*.pfx结尾的文件。 添加NuGet包 nuget中查找然后再程序中添加引用Microsoft.AspNetCore.Server.Kestrel.Https 配置 把*.pfx结尾的文件...
kestrel项目源文件包
在.NET 6.0上使用Kestrel配置和自定义HTTPS.doc
资源分类:Python库 所属语言:Python 资源全名:kestrel-lang-1.0.5.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
NULL 博文链接:https://snowolf.iteye.com/blog/1605229
addlog-kestrel
背景 ...新建一个webapi项目,如下图 添加Controller using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace WebApiNet_v5.Controllers { [Route("api/[controller]")]
红隼节点Node.js 的 Kestrel 客户端安装 npm install kestrel.node用法 var Kestrel = require ( 'kestrel.node' ) ;var client = new Kestrel ( 'localhost:22133' ) ;// get can optionally take a timeout in ...
本示例可直接运行,方便快速了解Kestrel框架. Kestrel 是包含在 ASP.NET Core 项目模板中的 Web 服务器, .NET Core 支持的所有平台和版本均支持 Kestrel。
NULL 博文链接:https://snowolf.iteye.com/blog/1612207
Kestrel是不是Unix或Windows的内核。
Atari ST ,演示如何组装商品硬件以形成一个长期存在的,由风扇支持的环境,该环境已从最初的开源社区转变为开源社区。 Commodore Amiga的原因与Atari 800相同,只是针对更快的总线体系结构和更宽的数据路径所提供...
FastGateway 一个超级简单方便的网关,基于Kestrel+Yarp实现的网关。 支持动态配置路由,支持动态配置集群,支持动态配置HTTPS证书,无需重启即可使用。
Kestrel(Kotlin 事件溯源) 用于在 Kotlin 中构建基于事件的 CQRS 应用程序的框架。 概括 事件溯源是一种架构范式,其中应用程序状态被建模并存储为在您的应用程序域中有意义的语义事件的不可变序列。 CQRS,命令...
python库。 资源全名:kestrel_lang-1.1.0-py3-none-any.whl