Cassandra复制因子大于节点数 [英] Cassandra replication factor greater than number of nodes

查看:149
本文介绍了Cassandra复制因子大于节点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用用于Apache Cassandra(2.1.9版)的datastax Java驱动程序,并且我想知道将Replication_factor设置为大于节点数时会发生什么。我已经读过Cassandra允许进行此操作的地方,但是当我尝试保存一些数据时应该会失败(当然,这取决于写一致性级别,但是我的意思是ALL)。

问题是,即使我尝试保存数据,也可以正常工作,不会引发任何异常。为什么?

对于旧版本的Cassandra,我阅读的信息也许是旧的?
比将另一个节点添加到集群时会发生的问题多一个问题,是否属实?


I am using the datastax java driver for Apache Cassandra (v. 2.1.9) and I am wondering what should happen when I set replication_factor greater than number of nodes. I've read somewhere that Cassandra allows for this operation, but should fail when I will try to save some data (of course it depends on the write consistency level, but I mean the case of ALL).
The problem is that everything works, no exception is being thrown, even if I try to save data. Why?
Maybe the pieces of information which I've read were old, for older versions of Cassandra? One more question, whether it's true, than what would happen when I add another node to the cluster?

推荐答案

Cassandra具有可调一致性的概念,这在某种程度上意味着您可以控制读/写操作的一致性级别设置。

Cassandra has a concept of "tunable consistency" which in part means you can control the consistency level setting for read/write operations.

您可以在文档中阅读更多内容解释一致性级别以及如何设置它们 cqlsh shell

You can read a bit more in the docs explaining consistency levels and how to set them in the cqlsh shell.

要了解更多信息,建议在Cassandra的单节点上尝试使用cqlsh。例如,我们可以创建一个复制因子为2的键空间,并将一些数据加载到其中:

To learn more I suggest experimenting with the cqlsh on a single-node of Cassandra. For example we can create a keyspace with replication factor of 2 and load some data into it:

cqlsh> create keyspace test with replication = {'class': 'SimpleStrategy', 'replication_factor':2};
cqlsh> create table test.keys (key int primary key, val int);
cqlsh> insert into test.keys (key, val) values (1, 1);
cqlsh> select * from test.keys;

 key | val
-----+-----
   1 |   1 

一切正常,因为默认的一致性级别为ONE,所以只有1个节点必须在线。现在尝试相同的操作,但将其设置为ALL:

Everything works fine because the default consistency level is ONE, so only 1 node had to be online. Now try the same but setting it to ALL:

cqlsh> CONSISTENCY ALL;
Consistency level set to ALL.
cqlsh> insert into test.keys (key, val) values (2, 2);
Traceback (most recent call last):
  File "resources/cassandra/bin/cqlsh.py", line 1324, in perform_simple_statement
    result = future.result()
  File "resources/cassandra/bin/../lib/cassandra-driver.zip/cassandra-driver/cassandra/cluster.py", line 3133, in result
    raise self._final_exception
Unavailable: code=1000 [Unavailable exception] message="Cannot achieve consistency level ALL" info={'required_replicas': 2, 'alive_replicas': 1, 'consistency': 'ALL'}

cqlsh> select * from test.keys;
Traceback (most recent call last):
  File "resources/cassandra/bin/cqlsh.py", line 1324, in perform_simple_statement
    result = future.result()
  File "resources/cassandra/bin/../lib/cassandra-driver.zip/cassandra-driver/cassandra/cluster.py", line 3133, in result
    raise self._final_exception
Unavailable: code=1000 [Unavailable exception] message="Cannot achieve consistency level ALL" info={'required_replicas': 2, 'alive_replicas': 1, 'consistency': 'ALL'}

因为第二个节点不存在,所以读和写都不起作用。实际上,错误消息会提供有用的线索,表明需要两个副本,但只有一个可用。

Neither reads nor writes will work because the 2nd node doesn't exist. In fact the error message will give a helpful clue that two replicas were needed but only one was available.

一旦您了解了cqlsh,就可以使用Java驱动程序,具体取决于您的应用程序需要。

Once you have an understanding using cqlsh, you can apply the same using the Java drivers, depending on what your application needs.

这篇关于Cassandra复制因子大于节点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆