Cassandra 中的计数器是如何存储在磁盘上的?

How are counters in Cassandra stored on the disk?

我无法理解 Cassandra 计数器是如何存储在磁盘上的。

创建测试table

create table testcounter (
id text,
count counter,
PRIMARY KEY(id))
WITH compaction = {'class': 
'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 
'max_threshold': '32', 'min_threshold': '4'}
AND compression = {'chunk_length_in_kb': '64', 'class': 
'org.apache.cassandra.io.compress.LZ4Compressor'}

添加数据

update testcounter set count = count + 10 where id = 'testrow';

检查sstable

nodetool flush test testcounter 
sstabledump /usr/local/var/lib/cassandra/data/test/testcounter-87d6ae20908e11e9a5779f988085883a/mc-1-big-Data.db

来自 ss 的响应tabledump

[
  {
    "partition" : {
      "key" : [ "testrow" ],
      "position" : 0
    },
    "rows" : [
      {
        "type" : "row",
        "position" : 63,
        "cells" : [
          { "name" : "count", "value" : 422215477737628, "tstamp" : "2019-06-16T23:30:34.423470Z" }
        ]
      }
    ]
  }

更新现有数据

update testcounter set count = count + 10 where id = 'testrow';
update testcounter set count = count + 10 where id = 'testrow';

同花顺

nodetool flush test testcounter

此时,有两组db文件。

ls /usr/local/var/lib/cassandra/data/test/testcounter-87d6ae20908e11e9a5779f988085883a/
backups             mc-1-big-Digest.crc32       mc-1-big-Statistics.db      mc-2-big-CompressionInfo.db mc-2-big-Filter.db      mc-2-big-Summary.db
mc-1-big-CompressionInfo.db mc-1-big-Filter.db      mc-1-big-Summary.db     mc-2-big-Data.db        mc-2-big-Index.db       mc-2-big-TOC.txt
mc-1-big-Data.db        mc-1-big-Index.db       mc-1-big-TOC.txt        mc-2-big-Digest.crc32       mc-2-big-Statistics.db

sstablemc-1 转储

[
  {
    "partition" : {
      "key" : [ "testrow" ],
      "position" : 0
    },
    "rows" : [
      {
        "type" : "row",
        "position" : 63,
        "cells" : [
          { "name" : "count", "value" : 422215477737628, "tstamp" : "2019-06-16T23:30:34.423470Z" }
        ]
      }
    ]
  }

sstablemc-2 转储

[
  {
    "partition" : {
      "key" : [ "testrow" ],
      "position" : 0
    },
    "rows" : [
      {
        "type" : "row",
        "position" : 65,
        "cells" : [
          { "name" : "count", "value" : 422215477737628, "tstamp" : "2019-06-16T23:34:37.245893Z" }
        ]
      }
    ]
  }

看起来没有墓碑,甚至连计数器值都没有存储。幕后发生了什么?

在 2.1 之后,它实际上是先读后写,然后存储一个打包的元组,这不是很明显或不容易反序列化。可能值得打开一个 jira 让 sstabledump 反序列化上下文并使其更具可读性。

有关详细信息,请参阅:https://www.datastax.com/dev/blog/whats-new-in-cassandra-2-1-a-better-implementation-of-counters