ablog

不器用で落着きのない技術者のメモ

Amazon KeySpaces で VPC エンドポイントを使用した場合の system.peers のエントリの違い

結論

  • VPC エンドポイントを使用すると、system.peers は AZ の数に依存する。
    • パブリックエンドポイントの場合の 9 から VPCエンドポイントを使用すると 2-5 に減るが、CQL のスループット(QPS)限界が小さくなるわけではない。ただし、VPC エンドポイントを使う場合はスループットを上げるためにアプリからの接続数を増やす必要がある。

プライベート VPC エンドポイントを介して接続する場合、使用可能なエンドポイントは 9 個未満であることがほとんどです。さらに、VPC エンドポイントの数は、アベイラビリティーゾーンの数と、割り当てられた VPC 内のサブネットの数に基づいて、リージョンごとに異なる場合があります。米国東部 (バージニア北部) リージョンには 5 つのアベイラビリティーゾーンがあり、最大 5 つの Amazon Keyspaces エンドポイントを設定できます。米国西部 (北カリフォルニア) リージョンには 2 つのアベイラビリティーゾーンがあり、最大 2 つの Amazon Keyspaces エンドポイントを持つことができます

Amazon Keyspaces へのクライアントドライバー接続 (Apache Cassandra 用) - Amazon Keyspaces (Apache Cassandra 向け)

別のエンドポイントを使用してプライベート VPC エンドポイントから Amazon Keyspaces にアクセスする場合AWS アカウント、Amazon Keyspaces エンドポイントは 1 つしか表示されない可能性があります。繰り返しますが、これは Amazon Keyspaces へのスループットの規模には影響しませんが、ドライバー設定の接続数を増やす必要がある場合があります。この例では、使用可能な 1 つのエンドポイントについて同じ計算を行っています。

Amazon Keyspaces へのクライアントドライバー接続 (Apache Cassandra 用) - Amazon Keyspaces (Apache Cassandra 向け)
  • VPC エンドポイントを使用する場合、SigV4 認証しないと system.peers のエントリが空になる。
    • EC2 に IAM 権限をアタッチしても、KeySpaces にパスワード認証すると IAM で認証していないため
    • cassandra.ap-northeast-1.amazonaws.com については上記の通りだが、VPC エンドポイントの DNS 名 vpce-********.cassandra.ap-northeast-1.vpce.amazonaws.com に接続すると system.peers にエントリが表示される。

To look up and store available interface VPC endpoints in the system.peers table, Amazon Keyspaces requires that you grant the IAM entity used to connect to Amazon Keyspaces access permissions to query your VPC for the endpoint and network interface information.

Using Amazon Keyspaces with interface VPC endpoints - Amazon Keyspaces (for Apache Cassandra)

検証結果

  • VPC エンドポイントがないと system.peers に 9 つのエントリが表示される。
$  cqlsh cassandra.ap-northeast-1.amazonaws.com 9142 --ssl
Connected to Amazon Keyspaces at cassandra.ap-northeast-1.amazonaws.com:9142
[cqlsh 6.1.0 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> select * from  system.peers;

 peer           | data_center    | host_id                              | preferred_ip   | rack           | release_version | rpc_address    | schema_version                       | tokens
----------------+----------------+--------------------------------------+----------------+----------------+-----------------+----------------+--------------------------------------+--------------------------
 18.177.156.237 | ap-northeast-1 | 7afb0ca0-cfef-3ab4-a9d8-8919c468a586 | 18.177.156.237 | ap-northeast-1 |          3.11.2 | 18.177.156.237 | 05deae2d-6405-494d-a965-c0e5836bcb3c | {'-7173733806442603407'}
 18.177.156.232 | ap-northeast-1 | 0984ca2e-8ee7-397b-88a0-7d8783efd305 | 18.177.156.232 | ap-northeast-1 |          3.11.2 | 18.177.156.232 | 05deae2d-6405-494d-a965-c0e5836bcb3c | {'-5124095576030431006'}
 18.177.156.200 | ap-northeast-1 | 0dde4e04-6c73-3afb-b93c-2f5e12a441f3 | 18.177.156.200 | ap-northeast-1 |          3.11.2 | 18.177.156.200 | 05deae2d-6405-494d-a965-c0e5836bcb3c | {'-3074457345618258605'}
 18.177.156.231 | ap-northeast-1 | 33d42b23-cc2c-3460-9646-b4bffedf646f | 18.177.156.231 | ap-northeast-1 |          3.11.2 | 18.177.156.231 | 05deae2d-6405-494d-a965-c0e5836bcb3c | {'-1024819115206086204'}
 18.177.156.213 | ap-northeast-1 | 01290fa4-e20f-3fb4-ace4-7406d48c52b3 | 18.177.156.213 | ap-northeast-1 |          3.11.2 | 18.177.156.213 | 05deae2d-6405-494d-a965-c0e5836bcb3c |  {'1024819115206086197'}
 18.177.156.253 | ap-northeast-1 | def25f13-8435-31b6-b250-c900fe4785aa | 18.177.156.253 | ap-northeast-1 |          3.11.2 | 18.177.156.253 | 05deae2d-6405-494d-a965-c0e5836bcb3c |  {'3074457345618258598'}
 18.177.156.214 | ap-northeast-1 | 49074ce2-2f74-36bd-8ed4-54568cc53079 | 18.177.156.214 | ap-northeast-1 |          3.11.2 | 18.177.156.214 | 05deae2d-6405-494d-a965-c0e5836bcb3c |  {'5124095576030430999'}
 18.177.156.212 | ap-northeast-1 | 7dacf8a2-b751-3494-9062-327c622e645c | 18.177.156.212 | ap-northeast-1 |          3.11.2 | 18.177.156.212 | 05deae2d-6405-494d-a965-c0e5836bcb3c |  {'7173733806442603400'}
 18.177.156.192 | ap-northeast-1 | 973a0ad2-4c73-360e-9928-732d864bced6 | 18.177.156.192 | ap-northeast-1 |          3.11.2 | 18.177.156.192 | 05deae2d-6405-494d-a965-c0e5836bcb3c |  {'9223372036854775801'}

(9 rows)
  • VPC エンドポイントがあると AZ 数分のエントリが表示される。
    • 2 AZ(サブネット)から 3 AZ(サブネット)に増やすとエントリ数が増える。
$  cqlsh cassandra.ap-northeast-1.amazonaws.com 9142 --ssl
Connected to Amazon Keyspaces at cassandra.ap-northeast-1.amazonaws.com:9142
[cqlsh 6.1.0 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> select * from  system.peers;

 peer         | data_center    | host_id                              | preferred_ip | rack           | release_version | rpc_address  | schema_version                       | tokens
--------------+----------------+--------------------------------------+--------------+----------------+-----------------+--------------+--------------------------------------+-------------------------
 172.31.27.50 | ap-northeast-1 | fd6026af-497c-3bf5-8eb2-6369591daa2b | 172.31.27.50 | ap-northeast-1 |          3.11.2 | 172.31.27.50 | 05deae2d-6405-494d-a965-c0e5836bcb3c |                  {'-1'}
 172.31.2.222 | ap-northeast-1 | 6f997ca5-e120-390b-a657-1449de9a3005 | 172.31.2.222 | ap-northeast-1 |          3.11.2 | 172.31.2.222 | 05deae2d-6405-494d-a965-c0e5836bcb3c | {'9223372036854775806'}

(2 rows)
$  cqlsh cassandra.ap-northeast-1.amazonaws.com 9142 --ssl
Connected to Amazon Keyspaces at cassandra.ap-northeast-1.amazonaws.com:9142
[cqlsh 6.1.0 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> select * from  system.peers;

 peer          | data_center    | host_id                              | preferred_ip  | rack           | release_version | rpc_address   | schema_version                       | tokens
---------------+----------------+--------------------------------------+---------------+----------------+-----------------+---------------+--------------------------------------+--------------------------
  172.31.3.159 | ap-northeast-1 | caaf4736-f9c8-3eb5-8c7d-aada28a89452 |  172.31.3.159 | ap-northeast-1 |          3.11.2 |  172.31.3.159 | 05deae2d-6405-494d-a965-c0e5836bcb3c | {'-3074457345618258603'}
 172.31.30.103 | ap-northeast-1 | 76a77963-9462-30e7-89f2-dbb975ec5fbf | 172.31.30.103 | ap-northeast-1 |          3.11.2 | 172.31.30.103 | 05deae2d-6405-494d-a965-c0e5836bcb3c |  {'3074457345618258602'}
 172.31.46.196 | ap-northeast-1 | 90ab788e-055a-3799-a5e6-1c682d38e7ab | 172.31.46.196 | ap-northeast-1 |          3.11.2 | 172.31.46.196 | 05deae2d-6405-494d-a965-c0e5836bcb3c |  {'9223372036854775807'}

(3 rows)
  • VPC エンドポイントがある場合、preferred_ip は各サブネットの範囲内の IP アドレスになる。
$  cqlsh cassandra.ap-northeast-1.amazonaws.com 9142 --ssl
Connected to Amazon Keyspaces at cassandra.ap-northeast-1.amazonaws.com:9142
[cqlsh 6.1.0 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> select * from  system.peers;

 peer         | data_center    | host_id                              | preferred_ip | rack           | release_version | rpc_address  | schema_version                       | tokens
--------------+----------------+--------------------------------------+--------------+----------------+-----------------+--------------+--------------------------------------+-------------------------
 172.31.2.222 | ap-northeast-1 | 6f997ca5-e120-390b-a657-1449de9a3005 | 172.31.2.222 | ap-northeast-1 |          3.11.2 | 172.31.2.222 | 05deae2d-6405-494d-a965-c0e5836bcb3c |                  {'-1'}
 172.31.27.50 | ap-northeast-1 | fd6026af-497c-3bf5-8eb2-6369591daa2b | 172.31.27.50 | ap-northeast-1 |          3.11.2 | 172.31.27.50 | 05deae2d-6405-494d-a965-c0e5836bcb3c | {'9223372036854775806'}

(2 rows)

$ dig +short vpce-0611********6q73ju.cassandra.ap-northeast-1.vpce.amazonaws.com
172.31.27.50
172.31.2.222

  • VPC エンドポイントがある場合、IAM 権限がないと system.peers が空になる(SigV4認証)
$  cqlsh cassandra.ap-northeast-1.amazonaws.com 9142 --ssl
Connected to Amazon Keyspaces at cassandra.ap-northeast-1.amazonaws.com:9142
[cqlsh 6.1.0 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> select * from  system.peers;

 peer | data_center | host_id | preferred_ip | rack | release_version | rpc_address | schema_version | tokens
------+-------------+---------+--------------+------+-----------------+-------------+----------------+--------

(0 rows)
  • VPC エンドポイントがある場合、IAM 権限があると system.peers にエントリが表示される(SigV4認証)
$  cqlsh cassandra.ap-northeast-1.amazonaws.com 9142 --ssl
Connected to Amazon Keyspaces at cassandra.ap-northeast-1.amazonaws.com:9142
[cqlsh 6.1.0 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> select * from  system.peers;

 peer         | data_center    | host_id                              | preferred_ip | rack           | release_version | rpc_address  | schema_version                       | tokens
--------------+----------------+--------------------------------------+--------------+----------------+-----------------+--------------+--------------------------------------+-------------------------
 172.31.27.50 | ap-northeast-1 | fd6026af-497c-3bf5-8eb2-6369591daa2b | 172.31.27.50 | ap-northeast-1 |          3.11.2 | 172.31.27.50 | 05deae2d-6405-494d-a965-c0e5836bcb3c |                  {'-1'}
 172.31.2.222 | ap-northeast-1 | 6f997ca5-e120-390b-a657-1449de9a3005 | 172.31.2.222 | ap-northeast-1 |          3.11.2 | 172.31.2.222 | 05deae2d-6405-494d-a965-c0e5836bcb3c | {'9223372036854775806'}

(2 rows)
  • VPC エンドポイントがある場合、IAM 権限があっても system.peers にエントリが表示されない(パスワード認証)
$ cqlsh cassandra.ap-northeast-1.amazonaws.com 9142 -u "keyspaces_user+1-at-********" -p "********" --ssl
/home/ec2-user/.local/bin/cqlsh:460: DeprecationWarning: Legacy execution parameters will be removed in 4.0. Consider using execution profiles.
/home/ec2-user/.local/bin/cqlsh:460: DeprecationWarning: Using ssl_options without ssl_context is deprecated and will result in an error in the next major release. Please use ssl_context to prepare for that release.
/home/ec2-user/.local/bin/cqlsh:490: DeprecationWarning: Setting the consistency level at the session level will be removed in 4.0. Consider using execution profiles and setting the desired consistency level to the EXEC_PROFILE_DEFAULT profile.
Connected to Amazon Keyspaces at cassandra.ap-northeast-1.amazonaws.com:9142
[cqlsh 6.0.0 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
keyspaces_user+1-at-542203247656@cqlsh> select * from  system.peers;

 peer | data_center | host_id | preferred_ip | rack | release_version | rpc_address | schema_version | tokens
------+-------------+---------+--------------+------+-----------------+-------------+----------------+--------

(0 rows)

補足

VPC エンドポイント作成手順
  • マネジメントコンソールから [VPC]-[エンドポイント]-[エンドポイントを作成]
    • サービスカテゴリ: AWS サービス
    • サービス名: com.amazonaws.ap-northeast-1.cassandra
    • 追加設定
      • DNS 名を有効化: チェック
EC2 に以下の IAM 権限を持つ IAM ロールをアタッチする
{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Sid":"ListVPCEndpoints",
         "Effect":"Allow",
         "Action":[
            "ec2:DescribeNetworkInterfaces",
            "ec2:DescribeVpcEndpoints"
         ],
         "Resource":"*"
      }
   ]
}

参考

パブリックエンドポイント経由で接続している場合、利用可能なエンドポイントは 9 つあります。VPC エンドポイントを使用している場合は、リージョンに応じて 2 ~ 5 つのエンドポイントを使用できます。

Amazon Keyspaces へのクライアントドライバー接続 (Apache Cassandra 用) - Amazon Keyspaces (Apache Cassandra 向け)

別のエンドポイントを使用してプライベート VPC エンドポイントから Amazon Keyspaces にアクセスする場合AWS アカウント、Amazon Keyspaces エンドポイントは 1 つしか表示されない可能性があります。繰り返しますが、これは Amazon Keyspaces へのスループットの規模には影響しませんが、ドライバー設定の接続数を増やす必要がある場合があります。この例では、使用可能な 1 つのエンドポイントについて同じ計算を行っています。

Amazon Keyspaces へのクライアントドライバー接続 (Apache Cassandra 用) - Amazon Keyspaces (Apache Cassandra 向け)

Linux で設定可能な仮想IPアドレスの最大数

Linux 2.2 <= は 255 と言う制約があったが、Linux 2.2 < は制約ない。実装が linked list に変わったため。

Issue

How many network interfaces aliases does Red Hat Enterprise Linux support?
What is the maximum number of interface aliases I can use on Red Hat Enterprise Linux?

Resolution

There is no practical limit.

Root Cause

In Linux kernel 2.2 there was a limit of 255 interfaces.
In modern Linux kernels, interfaces and IP addresses are implemented as a linked list, removing any hard limit of these items.
You can add IP addresses until you run out of system RAM from allocating the (very tiny) kernel data structures. This is kernel function __inet_insert_ifa()

What is the maximum number of interface aliases supported in Red Hat Enterprise Linux? - Red Hat Customer Portal

仮想IPアドレスという呼び方は正確ではなく IP エイリアスというのが正しいと思うが、日本のIT業界で「仮想IP」という呼び方をされていることが多い気がするので、こういうタイトルにしてみた。
知らんけど。

IP-aliases are an obsolete way to manage multiple IP-addresses/masks per interface. Newer tools such as iproute2 support multiple address/prefixes per interface, but aliases are still supported for backwards compatibility.

IP-Aliasing — The Linux Kernel documentation

cqlsh から Amazon KeySpaces に接続しようとすると "Can't authenticate without a region name" と怒られる

事象

cqlsh から Amazon KeySpaces に接続しようとすると "Can't authenticate without a region name" と怒られる

Connection error: ('Unable to connect to any servers', {'172.31.4.223:9142': ValueError("Can't authenticate without a region name"), '172.31.20.37:9142': ValueError("Can't authenticate without a region name")})

原因

  • AWS CLI でリージョンが設定されてないからと思われる。

解決策

  • AWS CLI でリージョンを指定する。
$ aws configure
AWS Secret Access Key [None]:
Default region name [None]: ap-northeast-1
Default output format [None]:
  • 接続に成功する
$  cqlsh cassandra.ap-northeast-1.amazonaws.com 9142 --ssl
Connected to Amazon Keyspaces at cassandra.ap-northeast-1.amazonaws.com:9142
[cqlsh 6.1.0 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> 

YCSB を Amazon KeySpaces にベンチマークをかけれるようカスタマイズしたメモ

YCSB(Yahoo! Cloud Serving Benchmark)を Amazon KeySpaces にベンチマークをかけれるようカスタマイズしたメモ。

前提

前提条件および環境セットアップ手順

Amazon Linux 2023 on EC2
  • Amazon Linux 2023 on EC2 を使用
  • OS ユーザーは ec2-user を使用
Python 2.7.18 がインストールされていること
  • パッケージをインストール
$ sudo yum -y install git \
  bzip2 \
  bzip2-devel \
  gcc \
  git \
  libffi-devel \
  make \
  openssl \
  openssl-devel \
  readline \
  readline-devel \
  sqlite \
  sqlite-devel \
  zlib-devel \
  xz-devel
  • pyenv をインストール
$ curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
  • パスを通す
$ cat << 'EOS' >> ~/.bashrc
export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
EOS
$ source ~/.bashrc
  • patch コマンドをインストール
$ sudo yum -y install patch
$ pyenv install 2.7.18
  • インストールした Python 2.7.18 が使われるよう設定する。
$ pyenv global 2.7.18
$ pyenv rehash
  • 有効になっている Python のバージョンを確認する
$ pyenv versions
  system
* 2.7.18 (set by /home/ec2-user/.pyenv/version)
OpenJDK 8 がインストールされていること
$ sudo yum install java-1.8.0-amazon-corretto-devel
$ sudo alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*  1           /usr/lib/jvm/java-11-amazon-corretto.x86_64/bin/java
 + 2           /usr/lib/jvm/java-1.8.0-amazon-corretto.x86_64/jre/bin/java

Enter to keep the current selection[+], or type selection number: 2
$ java -version
openjdk version "1.8.0_382"
OpenJDK Runtime Environment Corretto-8.382.05.1 (build 1.8.0_382-b05)
OpenJDK 64-Bit Server VM Corretto-8.382.05.1 (build 25.382-b05, mixed mode)
Maven 3.9.4 がインストールされていること
$ curl -L -O https://dlcdn.apache.org/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.tar.gz
$ tar xvfz apache-maven-3.9.4-bin.tar.gz
~/.bashrc の編集
$ vi ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-amazon-corretto.x86_64
export PATH=$PATH:/home/ec2-user/apache-maven-3.9.4/bin
$ source ~/.bashrc
EC2 に KeySpaces へのアクセス権を持った IAM ロールがアタッチされていること(以下は例)
  • IAM 権限 "AmazonKeyspacesFullAccess" をアタッチする
  • 信頼関係
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "ec2.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
cassandra_truststore.jks をダウンロードする
$ cd ~
$ mkdir -p .cassandra
$ cd ~/.cassandra
$ curl -L -O https://github.com/aws-samples/aws-sigv4-auth-cassandra-java-driver-examples/raw/master/cassandra_truststore.jks

YCSB のカスタマイズ・ビルド

  • YCSB を入手する
$ pwd
/home/ec2-user
$ git clone https://github.com/yoheia/YCSB.git
    <cassandra.cql.version>3.7.2</cassandra.cql.version>
  • YCSB/cassandra/pom.xml に以下を追記する
    <dependency>
        <groupId>software.aws.mcs</groupId>
        <artifactId>aws-sigv4-auth-cassandra-java-driver-plugin_3</artifactId>
        <version>3.0.3</version>
    </dependency>
<!--
    <dependency>
      <groupId>org.cassandraunit</groupId>
      <artifactId>cassandra-unit</artifactId>
      <version>3.0.0.1</version>
      <classifier>shaded</classifier>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.21</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
-->
    <!-- only for Cassandra test (Cassandra 2.2+ uses Sigar for collecting system information, and Sigar requires some native lib files) -->
<!--
	  <dependency>
	    <groupId>org.hyperic</groupId>
	    <artifactId>sigar-dist</artifactId>
	    <version>1.6.4.129</version>
	    <type>zip</type>
	    <scope>test</scope>
	  </dependency>
-->
  • YCSB/cassandra/src/main/java/site/ycsb/db/CassandraCQLClient.java を以下の通り編集する
    • リージョンやエンドポイントはとりあえず直書きになっているので、リージョンに合わせて変更する
import software.aws.mcs.auth.SigV4AuthProvider;

...

/*
        if ((username != null) && !username.isEmpty()) {
          Cluster.Builder clusterBuilder = Cluster.builder().withCredentials(username, password)
              .withPort(Integer.valueOf(port)).addContactPoints(hosts);
          if (useSSL) {
            clusterBuilder = clusterBuilder.withSSL();
          } 
          cluster = clusterBuilder.build();
        } else {
          cluster = Cluster.builder().withPort(Integer.valueOf(port))
              .addContactPoints(hosts).build();
        }
*/

        String endPoint = "cassandra.ap-northeast-1.amazonaws.com";
        int portNumber = 9142;
        cluster = Cluster.builder()
                                .addContactPoint(hosts)
                                .withPort(Integer.valueOf(port))
                                .withAuthProvider(new SigV4AuthProvider("ap-northeast-1"))
                                .withSSL()
                                .build();
  • ビルドする
$ mvn clean package
  • YCSB/workloads/workloada_keyspaces を作成する
    • workloada をコピーして以下を変更
recordcount=1000000
operationcount=100000000

実行する

  • データロード
$ ./bin/ycsb load cassandra-cql -p hosts=cassandra.ap-northeast-1.amazonaws.com -p port=9142 -P ./workloads/workloada_keyspaces -s \
-threads 8 -p cassandra.writeconsistencylevel=LOCAL_QUORUM -p cassandra.ssl=true \
-jvm-args=" -Djavax.net.ssl.trustStore=/home/ec2-user/.cassandra/cassandra_truststore.jks -Djavax.net.ssl.trustStorePassword=amazon"
  • 実行する
$ ./bin/ycsb run cassandra-cql -p hosts=cassandra.ap-northeast-1.amazonaws.com -p port=9142 -P ./workloads/workloada_keyspaces -s \
-threads 256 -p cassandra.writeconsistencylevel=LOCAL_QUORUM -p cassandra.ssl=true \
-jvm-args=" -Djavax.net.ssl.trustStore=/home/ec2-user/.cassandra/cassandra_truststore.jks -Djavax.net.ssl.trustStorePassword=amazon"

リモートリポジトリからブランチを指定して git clone する

$ git clone -b 3.x-Driver-Compatible https://github.com/aws/aws-sigv4-auth-cassandra-java-driver-plugin.git

参考

リモートから特定のブランチを指定してcloneする方法

git clone -b ブランチ名 https://リポジトリのアドレス
リモートから特定のブランチを指定してcloneする - Qiita

Amazon KeySpaces に SigV4 認証でアクセスするサンプル Java アプリを実行する(DataStax Java Driver 3.x 版)

Amazon KeySpaces に SigV4 認証でアクセスするサンプル Java アプリを実行した手順(DataStax Java Driver 3.x 版)。

前提

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "ec2.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}

準備手順

KeySpaces にテーブルを作成してデータを登録する

マネコンの Amazon KeySpaces の CQL エディタから以下の CQL を実行する
-キースペースを作成する

create keyspace acme with replication = {'class': 'SimpleStrategy', 'replication_factor' : 1 };
  • テーブルを作成する
create table acme.orders (
       customer_id text,
       order_timestamp timestamp,
       order_id uuid,
       primary key (customer_id, order_timestamp)) with clustering order by (order_timestamp desc);
  • レコードを挿入する
insert into acme.orders (customer_id, order_timestamp, order_id) values ('1234', toTimestamp(now()), uuid());
insert into acme.orders (customer_id, order_timestamp, order_id) values ('1234', toTimestamp(now()), uuid());
insert into acme.orders (customer_id, order_timestamp, order_id) values ('1234', toTimestamp(now()), uuid());
insert into acme.orders (customer_id, order_timestamp, order_id) values ('1234', toTimestamp(now()), uuid());
サンプル Java アプリをビルドする
  • Maven プロジェクトを作成する
$ mvn -B archetype:generate \
 -DarchetypeGroupId=org.apache.maven.archetypes \
 -DgroupId=com.keySpacesSigv4AuthJavaDriver3 \
 -DartifactId=keySpacesSigv4AuthJavaDriver3
  • keySpacesSigv4AuthJavaDriver3/pom.xml に以下を追加する
<dependency>
    <groupId>software.aws.mcs</groupId>
    <artifactId>aws-sigv4-auth-cassandra-java-driver-plugin_3</artifactId>
    <version>3.0.3</version>
</dependency>
<dependency>
    <groupId>com.datastax.cassandra</groupId>
    <artifactId>cassandra-driver-core</artifactId>
    <version>3.7.2</version>
</dependency>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>3.1.1</version>
				<configuration>
					<appendAssemblyId>false</appendAssemblyId>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<archive>
						<manifest>
							<mainClass>com.keySpacesSigv4AuthJavaDriver3.App</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>3.1.1</version>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.keySpacesSigv4AuthJavaDriver3.App</mainClass>
						</manifest>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>
  • aws-sigv4-auth-cassandra-java-driver-plugin/src/main/java/KeySpacesSigV4JavaDriver3/App.java を作成する
package com.keySpacesSigv4AuthJavaDriver3;

import software.aws.mcs.auth.SigV4AuthProvider;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

public class App
{

    public static void main( String[] args )
    {
        String endPoint = "cassandra.ap-northeast-1.amazonaws.com";
        int portNumber = 9142;
        Session session = Cluster.builder()
                                         .addContactPoint(endPoint)
                                         .withPort(portNumber)
                                         .withAuthProvider(new SigV4AuthProvider("ap-northeast-1"))
                                         .withSSL()
                                         .build()
                                         .connect();

        ResultSet rs = session.execute("select * from acme.orders");
        Row row = rs.one();
        System.out.println(row.getString("customer_id"));
    }
}
  • ビルドする
$ pwd
/home/ec2-user/github/aws-sigv4-auth-cassandra-java-driver-plugin
$ mvn package

実行

  • 実行する
$ java -jar target/keySpacesSigv4AuthJavaDriver3-1.0-SNAPSHOT.jar 
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
1234

参考

YCSB から Amazon KeySpaces に SigV4 認証でアクセスする

  • YCSB/cassandra/pom.xml を以下の通り編集
    <cassandra.cql.version>3.7.2</cassandra.cql.version>
  • YCSB/cassandra/pom.xml に以下を追記
    <dependency>
        <groupId>software.aws.mcs</groupId>
        <artifactId>aws-sigv4-auth-cassandra-java-driver-plugin_3</artifactId>
        <version>3.0.3</version>
    </dependency>
  • YCSB/cassandra/src/main/java/site/ycsb/db/CassandraCQLClient.java を以下の通り編集
...

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.MessageFormatter;

import software.aws.mcs.auth.SigV4AuthProvider; ★追記

/**
 * Cassandra 2.x CQL client.
 *
 * See {@code cassandra2/README.md} for details.
 *
 * @author cmatser
 */
public class CassandraCQLClient extends DB {


...

/* ★コメントアウト
        if ((username != null) && !username.isEmpty()) {
          Cluster.Builder clusterBuilder = Cluster.builder().withCredentials(username, password)
              .withPort(Integer.valueOf(port)).addContactPoints(hosts);
          if (useSSL) {
            clusterBuilder = clusterBuilder.withSSL();
          } 
          cluster = clusterBuilder.build();
        } else {
          cluster = Cluster.builder().withPort(Integer.valueOf(port))
              .addContactPoints(hosts).build();
        }
*/
        ★追記
        Cluster.Builder clusterBuilder = Cluster.builder().withPort(Integer.valueOf(port))
            .addContactPoints(hosts).withAuthProvider(new SigV4AuthProvider("ap-northeast-1"));
        if (useSSL) {
          clusterBuilder = clusterBuilder.withSSL();
        }
        cluster = clusterBuilder.build();
  • ビルドする
$ mvn clean package