Cassandraのクラスター構成、Repair

#はじめに
CassandraはBigData分散データベースとして使っております。
クラスターにNode追加、Node削除、replication_factor変更後のRepairなどをまとめてみます。

#Cassandraとは
URL: http://cassandra.apache.org/

2020年4月時点は11位となっております。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/346425/264209af-383f-c421-d2e0-14564121fc12.png)

出典：https://db-engines.com/en/ranking

#Cluster構成のStrategy
参考ページ：http://cassandra.apache.org/doc/latest/architecture/dynamo.html#replication-strategy
##NetworkTopologyStrategy
複数のデータセンターの構成の場合はNetworkTopologyStrategyを選択

##SimpleStrategy
一つのデータセンターの構成の場合はSimpleStrategyを選択

###Keyspaceの現在のStrategy確認
```sql
select * from system_schema.keyspaces  where keyspace_name = 'my_keyspcae'
```

例：
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/346425/d34f5eaa-2441-ddc8-b754-dac12d85abd7.png)


###KeyspaceのStrategyの変更
```sql
ALTER KEYSPACE my_keyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};
```


#JVMパラメータの調整
設定ファイル：/etc/cassandra/conf/jvm.options
HEAP SETTINGS、GCなどの設定になります。
例：

```
#################
# HEAP SETTINGS #
#################

# Heap size is automatically calculated by cassandra-env based on this
# formula: max(min(1/2 ram, 1024MB), min(1/4 ram, 8GB))
# That is:
# - calculate 1/2 ram and cap to 1024MB
# - calculate 1/4 ram and cap to 8192MB
# - pick the max
#
# For production use you may wish to adjust this for your environment.
# If that's the case, uncomment the -Xmx and Xms options below to override the
# automatic calculation of JVM heap memory.
#
# It is recommended to set min (-Xms) and max (-Xmx) heap sizes to
# the same value to avoid stop-the-world GC pauses during resize, and
# so that we can lock the heap in memory on startup to prevent any
# of it from being swapped out.
-Xms8G
-Xmx8G
```

参考記事：https://docs.datastax.com/en/ddac/doc/datastax_enterprise/operations/opsTuneJVM.html

#ClusterにNode追加
##cassandra.yaml設定変更
設定ファイル：/etc/cassandra/conf/cassandra.yaml

```yaml
# The name of the cluster. This is mainly used to prevent machines in
# one logical cluster from joining another.
cluster_name: 'MyCluster'
 
# Whether to start the thrift rpc server.
start_rpc: true

# For security reasons, you should not expose this port to the internet.  Firewall it if needed.
rpc_address: <Local ip>
 
# any class that implements the SeedProvider interface and has a
# constructor that takes a Map<String, String> of parameters will do.
seed_provider:
    # Addresses of hosts that are deemed contact points. 
    # Cassandra nodes use this list of hosts to find each other and learn
    # the topology of the ring.  You must change this if you are running
    # multiple nodes!
    - class_name: org.apache.cassandra.locator.SimpleSeedProvider
      parameters:
          # seeds is actually a comma-delimited list of addresses.
          # Ex: "<ip1>,<ip2>,<ip3>"
          - seeds: "<Local ip>,<Node1 ip>"
 
# Address or interface to bind to and tell other Cassandra nodes to connect to.
# You _must_ change this if you want multiple nodes to be able to communicate!
#
# Set listen_address OR listen_interface, not both.
#
# Leaving it blank leaves it up to InetAddress.getLocalHost(). This
# will always do the Right Thing _if_ the node is properly configured
# (hostname, name resolution, etc), and the Right Thing is to use the
# address associated with the hostname (it might not be).
#
# Setting listen_address to 0.0.0.0 is always wrong.
#
listen_address: <Local ip>

```

参考ページ：http://cassandra.apache.org/doc/latest/configuration/cassandra_config_file.html

##再起動でClusterに追加
```shell
rm -rf /var/lib/cassandra/data/system/*

service cassandra start
```

##クラスターステータス確認
```shell
nodetool status
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/346425/2bfe656a-3542-b77b-bee1-05a189b5b35c.png)

複数Nodeの場合は、リストに表示されて、Ownsに各Nodeのデータの割合もわかります。
Node一つの場合は100%になります。replication_factorは３でNodeも３の場合はOwnsは100%です。
Nodeは４以上の場合はOwnsは100%より小さい数字になります。


#ClusterからNode削除
```shell
nodetool --host 外したいNodeのIP decommission -f
```

または外したいサーバにログインした後

```shell
nodetool decommission -f
```

#replication_factor変更後のRepair

##Full Repair
一番時間かかります。replication_factorの変更がある場合はこれを実行したほうがよいです。

いずれかのNodeにログインして、下記のコマンドの実行（一回のみ）

```shell
nodetool repair --full
```

##partitioner-range Repair
```shell
nodetool repair -pr
```

##Keyspace指定でRepair
```shell
nodetool repair mykeyspace
```

##テーブル指定でRepair
`nodetool repair [options] <keyspace_name> <table1> <table2>`

```shell
nodetool repair mykeyspace mytable
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/346425/8d121600-36ba-7ad6-75fe-f9eb00869d39.png)


参考ページ：http://cassandra.apache.org/doc/latest/operating/repair.html?highlight=repair

以上