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

pes8fvy9  于 2021-06-14  发布在  Cassandra
关注(0)|答案(1)|浏览(374)

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

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

来自sstabledump的响应

[
  {
    "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

mc-1的sstabledump

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

mc-2的sstabledump

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

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

4uqofj5v

4uqofj5v1#

在2.1之后,它实际上是先读后写,然后存储一个压缩元组,这个元组不是很明显,也不容易反序列化。可能值得打开一个jira,让sstabledump反序列化上下文并使其更具可读性。
有关详细信息,请参阅:https://www.datastax.com/dev/blog/whats-new-in-cassandra-2-1-a-better-implementation-of-counters

相关问题