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

时间:2019-06-16 23:49:56

标签: cassandra cassandra-3.0

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

创建测试表

/*Page Background Color Animation*/
@keyframes body {
    /*Eplepse Warning*/
    10% {background-color: yellow;}
    20% {background-color: orange;}
    30% {background-color: red;}
    40% {background-color: lightblue;}
    50% {background-color: purple;}
    60% {background-color: lightgreen;}
    70% {background-color: #DFB900;}
    80% {background-color: #CAFFAA;}
    90% {background-color: #98FF4A;}
    100% {background-color: #36FFFF;}
    /*Party in the house!!!!!*/
}
#body.animate {
    background-color: yellow;
    animation-name: body;
    animation-duration: 4s;
    animation-iteration-count: 8000;
}

添加数据

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'}

检查sstable

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

来自sstabledump的响应

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

更新现有数据

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

这时,有两套db文件。

nodetool flush test testcounter

mc-1的稳定转储

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-2的稳定转储

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

似乎没有逻辑删除,甚至没有存储计数器值。幕后发生了什么?

1 个答案:

答案 0 :(得分:1)

在2.1之后,它实际上是在写入之前先读取,然后本质上存储了一个打包的元组,该元组不是很明显或不容易反序列化。也许值得开一个jira,让sstabledump对上下文进行反序列化并使其更具可读性。

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