#!/usr/bin/env bash # RasterizedDB: a sharded cluster on one machine, to try sharding. Three shard # servers that trust one certificate authority, a shard map, a table split # across them by its key, and a gateway in front that speaks PostgreSQL and # routes each statement by key. Needs release 1.1.0 or later. # # Everything runs in the background, with logs and process ids in RDB_WORK. # Running it again starts whatever is not running. Stop with: # kill $(cat cluster/*.pid) set -euo pipefail RDB_BIN="${RDB_BIN:-rasterizeddb}" RDB_GATEWAY_BIN="${RDB_GATEWAY_BIN:-rasterizeddb-gateway}" RDB_WORK="${RDB_WORK:-./cluster}" # certificates, map, data, logs RDB_GATEWAY_PORT="${RDB_GATEWAY_PORT:-6432}" RDB_PG_BASE="${RDB_PG_BASE:-5440}" # shard n's PostgreSQL port is base + n RDB_NATIVE_BASE="${RDB_NATIVE_BASE:-61180}" # shard n's native port is base + n RDB_RSHARD_BASE="${RDB_RSHARD_BASE:-7400}" # shard n's gateway-facing port is base + n SHARDS=(0 1 2) die() { echo "error: $*" >&2; exit 1; } command -v "$RDB_BIN" >/dev/null 2>&1 || die "'$RDB_BIN' not found; set RDB_BIN to the server binary (1.1.0 or later)" command -v "$RDB_GATEWAY_BIN" >/dev/null 2>&1 || die "'$RDB_GATEWAY_BIN' not found; set RDB_GATEWAY_BIN to the gateway binary" case "$("$RDB_BIN" --help)" in *--shard-listen*) ;; *) die "this $RDB_BIN has no sharding; it needs server 1.1.0 or later, built with the sharding feature" ;; esac command -v openssl >/dev/null 2>&1 || die "openssl is needed for the certificates" command -v psql >/dev/null 2>&1 || die "psql is needed to create the schema" : "${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD; every shard gets the role rasterized with it}" export RASTERIZEDDB_PG_PASSWORD PGPASSWORD="$RASTERIZEDDB_PG_PASSWORD" mkdir -p "$RDB_WORK/tls" cd "$RDB_WORK" work="$PWD" RDB_BIN="$(command -v "$RDB_BIN")"; RDB_BIN="$(cd "$(dirname "$RDB_BIN")" && pwd)/$(basename "$RDB_BIN")" RDB_GATEWAY_BIN="$(command -v "$RDB_GATEWAY_BIN")"; RDB_GATEWAY_BIN="$(cd "$(dirname "$RDB_GATEWAY_BIN")" && pwd)/$(basename "$RDB_GATEWAY_BIN")" running() { [ -f "$1.pid" ] && kill -0 "$(cat "$1.pid")" 2>/dev/null; } wait_for() { # wait_for for _ in $(seq 1 60); do if psql "postgresql://rasterized@127.0.0.1:$1/postgres?sslmode=$2" -Atqc "SELECT 1" >/dev/null 2>&1; then return 0 fi kill -0 "$(cat "$4")" 2>/dev/null || die "$3 exited; see $work/${4%.pid}.log" sleep 1 done die "$3 did not answer on port $1 within 60 s; see $work/${4%.pid}.log" } # 1. Certificates: a CA, one for each shard and one for the gateway. if [ ! -f tls/ca.pem ]; then ( cd tls umask 077 openssl req -x509 -newkey rsa:2048 -nodes -days 365 -subj /CN=rdb-cluster-ca -keyout ca.key -out ca.pem printf 'subjectAltName=DNS:localhost,IP:127.0.0.1\nextendedKeyUsage=serverAuth,clientAuth\n' > ext.cnf for name in shard0 shard1 shard2 gateway; do openssl req -newkey rsa:2048 -nodes -subj "/CN=$name" -keyout "$name.key" -out "$name.csr" openssl x509 -req -in "$name.csr" -CA ca.pem -CAkey ca.key -CAcreateserial -days 365 -extfile ext.cnf -out "$name.pem" done rm -f ./*.csr ) fi # 2. The shard map: which shard owns which slice of the 2^48 key space. if [ ! -f cluster.map ]; then cluster_id="$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')" { echo "rasterizeddb shard map" echo "format 1" echo "cluster $cluster_id" echo "epoch 1" echo "route_v 1" for n in "${SHARDS[@]}"; do echo "shard $n 127.0.0.1:$((RDB_RSHARD_BASE + n))"; done echo "range 0x0 0" echo "range 0x555555555555 1" echo "range 0xaaaaaaaaaaaa 2" } > cluster.map fi cluster_id="$(sed -n 's/^cluster //p' cluster.map)" # 3. Three shards. for n in "${SHARDS[@]}"; do if running "shard$n"; then echo "shard $n is running"; continue; fi mkdir -p "shard$n" nohup "$RDB_BIN" --location "$work/shard$n" --protocol both \ --pg-port $((RDB_PG_BASE + n)) --rdb-port $((RDB_NATIVE_BASE + n)) \ --role shard --cluster-map "$work/cluster.map" --shard-id "$n" --cluster-id "$cluster_id" \ --shard-listen "127.0.0.1:$((RDB_RSHARD_BASE + n))" \ --shard-cert "$work/tls/shard$n.pem" --shard-key "$work/tls/shard$n.key" \ --gateway-ca "$work/tls/ca.pem" --peer-ca "$work/tls/ca.pem" \ --log-level info > "shard$n.log" 2>&1 & echo $! > "shard$n.pid" echo "started shard $n (pid $!)" done for n in "${SHARDS[@]}"; do wait_for $((RDB_PG_BASE + n)) require "shard $n" "shard$n.pid"; done # 4. The schema, on every shard. A table's PARTITION BY HASH column is its # route key. The gateway refuses DDL, so schema changes go to every shard. for n in "${SHARDS[@]}"; do psql "postgresql://rasterized@127.0.0.1:$((RDB_PG_BASE + n))/postgres?sslmode=require" -v ON_ERROR_STOP=1 -q \ -c "CREATE TABLE IF NOT EXISTS orders (id INT PRIMARY KEY, store INT NOT NULL, v INT NOT NULL) PARTITION BY HASH (id)" done # 5. The gateway. cat > gateway.conf < gateway.log 2>&1 & echo $! > gateway.pid echo "started the gateway (pid $!)" fi wait_for "$RDB_GATEWAY_PORT" disable gateway gateway.pid echo echo "Cluster $cluster_id is up. Connect through the gateway, as one database:" echo " psql \"postgresql://rasterized@127.0.0.1:$RDB_GATEWAY_PORT/postgres?sslmode=disable\"" echo "Logs and pids: $work. Stop with: kill \$(cat $work/*.pid)"