Last Updated: 2023-10-20
分散SQLデータベースであるYugabyteDBは、従来の単一ノードのデータベースとは異なり、デフォルトで複数 (3以上) のノードでクラスタを構成します。このノードは、同じデータセンターのサーバー・ラックに配置することも、異なるデータセンターや異なるリージョンに配置することも可能です。
このハンズオンでは、YugabyteDB Managedを使用して、様々なトポロジーのクラスタを作成します。そして簡単なベンチマークやSQLを使用して、それぞれのクラスタ・トポロジーの特性を確認します。
このハンズオンでは、YugabyteDB Managedで3種類のクラスタを構成します。以下の内容を実施します:
YugabyteDB ManagedはフルマネージドのDBaaS (データベース・アズ・サービス)です。サインアップしてアカウントを作成することで、すぐにデータベースを使い始めることができます。初めてYugabyteDB Managedを使用する方は、以下の手順に従ってアカウントを作成してください。
以上で、YugabyteDB Managedへのサインアップは完了です。
YugabyteDB Managedでは、UIの項目選択によってクラスタ構成やノード仕様を設定することができます。
以下のような項目を設定します:
このハンズオンでは、シングル・リージョンにAZレベルの耐障害性をもつクラスタ、マルチリージョン・クラスタ、ジオ・パーティショニング(上の図の右3つ)を作成します。
ここではAWS東京リージョンの各アベイラビリティ・ゾーンにノードを配置して、ゾーンレベルの耐障害性をもつ3ノードクラスタを構成します。
以上で、クラスタの作成は完了です。
クラスタにアクセスするには、接続情報の設定とクライアントツールが必要です。このセクションでは、以下を行います。
curl -sSL https://downloads.yugabyte.com/get_clients.sh | bash
docker pull yugabytedb/yugabyte-client:latest
docker run -d --name yugabyte-client yugabytedb/yugabyte-client
docker cp <CERT_FILE> yugabyte-client:/home/yugabyte/<CERT_FILE>
cd yugabyte-client-2.16.0.1/bin/ysqlsh
./ysqlsh "host=<HOST ADDRESS> \
user=<DB USER> \
dbname=yugabyte \
sslmode=verify-full \
sslrootcert=<ROOT_CERT_PATH>"
docker exec -it yugabyte-client bash
./ysqlsh "host=<HOST ADDRESS> \
user=<DB USER> \
dbname=yugabyte \
sslmode=verify-full \
sslrootcert=<ROOT_CERT_PATH>"
YSQLコマンドの入力モードになったら、クライアント・シェルからのクラスタへのアクセスは成功です。
\l
および \dt
と入力して、既存のデータベースやテーブルを確認してください。クラスタにはデフォルトでいくつかのデータベースが作成されていますが、接続先のyugabyteデータベースでは、テーブルやインデックス等のオブジェクトは何も作成されていないはずです。exit
または \q
と入力してください。以上で、このセクションは完了です。
コロケーションとは、YugabyteDBのデフォルトである自動シャーディングと分散配置を行わず、1つのタブレットにテーブル全体を配置する機能です。比較的小さくデータが増加しないテーブルが多数あるような場合、分散による同期的なコンセンサスがネットワークの負荷を大きくしたり、細分化されたタブレットがクエリ実行のパフォーマンスに影響したりすることがあります。
CREATE DATABASE col_db WITH colocation = true;
\c col_db
CREATE TABLE tbl1 (k int primary key, v int);
CREATE TABLE tbl2 (k int primary key, v int);
CREATE TABLE tbl3 (k int primary key, v int) with (colocation=false);
CREATE TABLE tbl4 (k int primary key, v int) with (colocation=false);
insert into tbl1 select i, i%10 from generate_series(1,100000) as i;
insert into tbl2 select i, i%10 from generate_series(1,100000) as i;
insert into tbl3 select i, i%10 from generate_series(1,100000) as i;
insert into tbl4 select i, i%10 from generate_series(1,100000) as i;
create index on tbl1 (v);
create index on tbl3 (v);
これで、コロケーション有無とセカンダリ・インデックス有無が異なる4つのテーブルが作成されました。
\timing
explain (analyze, costs off) select * from tbl1 where v=6;
explain (analyze, costs off) select * from tbl3 where v=6;
explain (analyze, costs off) insert into tbl1 values(100001,77);
explain (analyze, costs off) insert into tbl3 values(100001,77);
explain (analyze, costs off) select * from tbl1, tbl2 where tbl1.k=tbl2.k and tbl1.v=6;
explain (analyze, costs off) select * from tbl3, tbl4 where tbl3.k=tbl4.k and tbl3.v=6;
set yb_bnl_batch_size=1024;
以上で、このセクションは完了です。
ここではAWS東京リージョン、大阪リージョン、シンガポールリージョンにノードを配置して、リージョン・レベルの耐障害性をもつ3ノードクラスタを構成します。
以上で、このセクションは完了です。
ここでは、グローバルに分散したジオ・パーティション・クラスタを作成します。
以上で、このセクションは完了です。
ジオ・パーティションのクラスタでは、行レベルでデータの配置場所を指定することが可能です。場所の指定に使用されるのがテーブル・スペースです。YugabyteDB Managedのジオ・パーティションのクラスタでは、クラスタの作成時に自動的に各リージョンのテーブルスペースが作成されます。
./ysqlsh "host=<HOST ADDRESS> \
user=<DB USER> \
dbname=yugabyte \
sslmode=verify-full \
sslrootcert=<ROOT_CERT_PATH>"
YSQLコマンドの入力モードになったら、クライアント・シェルからのクラスタへのアクセスは成功です。
\x auto
\db+
---create customers table
CREATE TABLE customers (
cust_id int not null,
name text not null,
email text,
geo_partition text
) PARTITION BY LIST (geo_partition);
--create partitioned table
CREATE TABLE customers_eu PARTITION OF customers (
cust_id unique, name, email, geo_partition,
PRIMARY KEY (cust_id hash, geo_partition)
) FOR VALUES IN ('EU') TABLESPACE eu_central_1_ts;
CREATE TABLE customers_ap PARTITION OF customers (
cust_id unique, name, email, geo_partition,
PRIMARY KEY (cust_id hash, geo_partition)
) FOR VALUES IN ('ASIA') TABLESPACE ap_northeast_1_ts;
CREATE TABLE customers_us PARTITION OF customers (
cust_id unique, name, email, geo_partition,
PRIMARY KEY (cust_id hash, geo_partition)
) FOR VALUES IN ('US') TABLESPACE us_west_1_ts;
--create orders table
CREATE TABLE orders (
order_id int not null,
product_id int not null,
product_name text,
amount int,
geo_partition text,
cust_id int
) PARTITION BY LIST (geo_partition);
--create partitioned table
CREATE TABLE orders_eu PARTITION OF orders (
order_id, product_id, amount, geo_partition, cust_id,
PRIMARY KEY (order_id hash, product_id, geo_partition),
FOREIGN KEY (cust_id) REFERENCES customers_eu (cust_id) ON DELETE CASCADE
) FOR VALUES IN ('EU') TABLESPACE eu_central_1_ts;
CREATE TABLE orders_ap PARTITION OF orders (
order_id, product_id, amount, geo_partition, cust_id,
PRIMARY KEY (order_id hash, product_id, geo_partition),
FOREIGN KEY (cust_id) REFERENCES customers_ap (cust_id) ON DELETE CASCADE
) FOR VALUES IN ('ASIA') TABLESPACE ap_northeast_1_ts;
CREATE TABLE orders_us PARTITION OF orders (
order_id, product_id, amount, geo_partition, cust_id,
PRIMARY KEY (order_id hash, product_id, geo_partition),
FOREIGN KEY (cust_id) REFERENCES customers_us (cust_id) ON DELETE CASCADE
) FOR VALUES IN ('US') TABLESPACE us_west_1_ts;
\d+ customers
\d+ orders
--insert some customers data
INSERT INTO customers VALUES(1001,'山田花子','hanako@acme.com','ASIA');
INSERT INTO customers VALUES(1002,'鈴木太郎','taro@gmail.com','ASIA');
INSERT INTO customers VALUES(3001,'Franck Ribery','franckr@abc-de.com','EU');
INSERT INTO customers VALUES(3002,'Natalie Wood','natalie@xxx.com','EU');
INSERT INTO customers VALUES(5001,'John Doe','john@yyy.com','US');
INSERT INTO customers VALUES(5002,'Cathy Freeman','cathy@abcde.com','US');
--insert some orders data
INSERT INTO orders VALUES(9001,35,'YB T-Shirt M',2,'ASIA',1001);
INSERT INTO orders VALUES(9002,41,'YB Mug black',10,'ASIA',1002);
INSERT INTO orders VALUES(9003,35,'YB T-Shirt M',5,'EU',3001);
INSERT INTO orders VALUES(9004,70,'YB Multi-color Pen',24,'EU',3002);
INSERT INTO orders VALUES(9005,35,'YB T-Shirt M',10,'US',5001);
INSERT INTO orders VALUES(9006,54,'YB Sticker',100,'US',5002);
/timing
explain (analyze, costs off) select * from customers_ap;
explain (analyze, costs off) select * from customers;
INSERT INTO customers VALUES(5003,'Yuga Hero','hero@yugabyte.com','EU');
INSERT INTO orders VALUES(9901,54,'YB Sticker',100,'EU',5003);
SELECT tableoid::regclass, cust_id, name, email from customers where cust_id=5003;
explain (analyze, costs off) select order_id, product_name, c.geo_partition, c.name from orders_ap join customers_ap c on orders_ap.cust_id=c.cust_id;
explain (analyze, costs off) select order_id, product_name, c.geo_partition, c.name from orders join customers c on orders.cust_id=c.cust_id where c.geo_partition='ASIA';
以上で、このセクションは完了です。
以上で、YugabyteDB Managedの分散トポロジー体験ハンズオンは完了です。
YugabyteDB Managedでは、AZ障害やリージョン障害に耐えるクラスタ構成や、データの配置場所を特定した構成が簡単にできることを確認できたと思います。このハンズオンではシンプルに単一のクライアントからクエリを実行しましたが、実際のワークロードを想定した負荷をかける等の検証を行いたい場合は、以下のリンクを参考にしてください。
以下のハンズオンも実施してみてください。