在將cassandra的節點配置爲互備以後,某天其中一個節點突然down機了,這個時候發現通過cassandra的thrift api往另外的節點寫數據也沒法成功了。研究了下thrift的api以後,推測估計是跟client的consistency level有關係。
consistency level即一致性級別,用來根據配置的副本因子(ReplicationFactor )來控制cassandra的讀寫行爲,在讀或寫操作下,不同的一致性級別有不同的含義。所以讀寫的一致性級別定義是不完全一樣的。
Write
Level |
Behavior |
ANY |
Ensure that the write has been written to at least 1 node, including HintedHandoff recipients. |
ONE |
Ensure that the write has been written to at least 1 replica's commit log and memory table before responding to the client. |
QUORUM |
Ensure that the write has been written to N / 2 + 1 replicas before responding to the client. |
LOCAL_QUORUM |
Ensure that the write has been written to <ReplicationFactor> / 2 + 1 nodes, within the local datacenter (requires NetworkTopologyStrategy ) |
EACH_QUORUM |
Ensure that the write has been written to <ReplicationFactor> / 2 + 1 nodes in each datacenter (requires NetworkTopologyStrategy ) |
ALL |
Ensure that the write is written to all N replicas before responding to the client. Any unresponsive replicas will fail the operation. |
Read
Level |
Behavior |
ANY |
Not supported. You probably want ONE instead. |
ONE |
Will return the record returned by the first replica to respond. A consistency check is always done in a background thread to fix any consistency issues when ConsistencyLevel.ONE is used. This means subsequent calls will have correct data even if the initial read gets an older value. (This is called ReadRepair ) |
QUORUM |
Will query all replicas and return the record with the most recent timestamp once it has at least a majority of replicas (N / 2 + 1 ) reported. Again, the remaining replicas will be checked in the background. |
LOCAL_QUORUM |
Returns the record with the most recent timestamp once a majority of replicas within the local datacenter have replied. |
EACH_QUORUM |
Returns the record with the most recent timestamp once a majority of replicas within each datacenter have replied. |
ALL |
Will query all replicas and return the record with the most recent timestamp once all replicas have replied. Any unresponsive replicas will fail the operation. |
在這些級別中,爲了保證讀寫的一致性,我們基本都採用Quorum這級,但是在我們這個場景中,如果在一個節點down的情況下,還有保證另一個節點寫入的話,只能使用寫入ONE這個級別了。讀取的時候還是Quorum,如果也用ONE的話,會存在不一致的情況,不過在這種情況下,會有一個後臺線程來做同步
參考
http://wiki.apache.org/cassandra/API