Cassandra是如何序列化并发请求的?



我正在阅读关于Cassandra以及其他数据库是如何维护walmemtable的。

WAL-顾名思义,cassandra按顺序在这个文件中写入所有的突变

我的问题是-如果有数千个并行请求来到cassandra的同一节点,它如何保持顺序写入WAL

我检查了cassandra代码库,我找不到任何线程锁或任何互斥操作。

Cassandra写的代码

private CommitLogPosition addToCommitLog(Mutation mutation)
{
// Usually one of these will be true, so first check if that's the case.
boolean allSkipCommitlog = true;
boolean noneSkipCommitlog = true;
for (PartitionUpdate update : mutation.getPartitionUpdates())
{
if (update.metadata().params.memtable.factory().writesShouldSkipCommitLog())
noneSkipCommitlog = false;
else
allSkipCommitlog = false;
}
if (!noneSkipCommitlog)
{
if (allSkipCommitlog)
return null;
else
{
Set<TableId> ids = new HashSet<>();
for (PartitionUpdate update : mutation.getPartitionUpdates())
{
if (update.metadata().params.memtable.factory().writesShouldSkipCommitLog())
ids.add(update.metadata().id);
}
mutation = mutation.without(ids);
}
}
// Note: It may be a good idea to precalculate none/all for the set of all tables in the keyspace,
// or memoize the mutation.getTableIds()->ids map (needs invalidation on schema version change).
Tracing.trace("Appending to commitlog");
return CommitLog.instance.add(mutation);   --- ** Actual commit log write ***
}

如有任何帮助,不胜感激。

所有的突变都被立即添加到提交日志的末尾——无论它们到达副本的顺序是什么。

写操作通过Cassandra处理的先入先出的请求队列进入。没有任何额外的排序发生。

由于所有的变化都简单地附加到提交日志中,这使得在Cassandra中写入非常非常快。干杯!

最新更新