From adab722937d0950e81464265067dd88b134b87a9 Mon Sep 17 00:00:00 2001 From: fr33m0nk Date: Sat, 29 Jun 2024 13:08:20 +0530 Subject: [PATCH] Adds ScyllaDB based docker-compose --- Dockerfile | 4 +- README.md | 2 + docker-compose/.env | 4 + docker-compose/README.md | 59 +++++- .../initialize_database.clj | 18 +- docker-compose/docker-compose-scylla-db.yml | 174 ++++++++++++++++++ 6 files changed, 253 insertions(+), 8 deletions(-) create mode 100644 docker-compose/docker-compose-scylla-db.yml diff --git a/Dockerfile b/Dockerfile index 4667ad5..258f418 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,7 @@ RUN apk add --no-cache --update \ RUN curl -fsSL -o datomic-pro.zip https://datomic-pro-downloads.s3.amazonaws.com/${DATOMIC_VERSION}/datomic-pro-${DATOMIC_VERSION}.zip RUN unzip -qq ./datomic-pro.zip -d / && true RUN mv "/datomic-pro-${DATOMIC_VERSION}" /datomic-pro && true -RUN curl -fsSL -o /datomic-pro/lib/logstash-logback-encoder-7.4.jar https://repo1.maven.org/maven2/net/logstash/logback/logstash-logback-encoder/7.4/logstash-logback-encoder-7.4.jar +RUN curl -fsSL -o /datomic-pro/lib/logstash-logback-encoder-7.3.jar https://repo1.maven.org/maven2/net/logstash/logback/logstash-logback-encoder/7.3/logstash-logback-encoder-7.3.jar COPY ./artifacts/logback.xml /datomic-pro/bin/logback.xml RUN rm -rf ./datomic-pro.zip @@ -60,6 +60,8 @@ ENV SQL_DRIVER_CLASS "" # TRANSACTOR DDB-LOCAL Backend ENV DYNAMO_DB_TABLE "" ENV LOCAL_DYNAMO_DB_ENDPOINT "" +ENV AWS_ACCESS_KEY_ID="none" +ENV AWS_SECRET_ACCESS_KEY="none" # TRANSACTOR DDB Backend ENV AWS_DYNAMO_DB_REGION "" diff --git a/README.md b/README.md index 0a12a54..4dac419 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,8 @@ | AWS_CLOUDWATCH_DIMENSION_VALUE | `TRANSACTOR`, `PEER`, `BACKUP_DB`, `RESTORE_DB`, `CONSOLE` WITH PROTOCOL `DDB` | | **None, provided by user** | | DYNAMO_DB_TABLE | `TRANSACTOR`, `PEER`, `BACKUP_DB`, `RESTORE_DB`, `CONSOLE` WITH PROTOCOL `DDB` OR `DDB-LOCAL` | | **None, provided by user** | | LOCAL_DYNAMO_DB_ENDPOINT | `TRANSACTOR`, `PEER`, `BACKUP_DB`, `RESTORE_DB`, `CONSOLE` WITH PROTOCOL `DDB` OR `DDB-LOCAL` | | **None, provided by user** | +| AWS_ACCESS_KEY_ID | `TRANSACTOR`, `PEER`, `BACKUP_DB`, `RESTORE_DB`, `CONSOLE` WITH PROTOCOL `DDB` OR `DDB-LOCAL` | | `none` | +| AWS_SECRET_ACCESS_KEY | `TRANSACTOR`, `PEER`, `BACKUP_DB`, `RESTORE_DB`, `CONSOLE` WITH PROTOCOL `DDB` OR `DDB-LOCAL` | | `none` | | PEER_HOST | `PEER` | | `0.0.0.0` | | PEER_PORT | `PEER` | | `8998` | | PEER_ACCESSKEY | `PEER` | | `myaccesskey` | diff --git a/docker-compose/.env b/docker-compose/.env index 0d686b4..edd4380 100644 --- a/docker-compose/.env +++ b/docker-compose/.env @@ -25,3 +25,7 @@ CONSOLE_HOST_MAPPING_PORT=8888 IPV4_SUBNET_CIDR=192.168.29.0/24 IP_RANGE=192.168.29.64/26 GATEWAY_IP=192.168.29.1 + +SCYLLA_DB_PROTOCOL=DDB-LOCAL +SCYLLA_DB_HOST_MAPPING_PORT=8000 +DYNAMO_DB_TABLE=test-database diff --git a/docker-compose/README.md b/docker-compose/README.md index 1ac1760..6fa158b 100644 --- a/docker-compose/README.md +++ b/docker-compose/README.md @@ -16,12 +16,20 @@ ## The configuration variables for docker compose file are in [`.env`](./.env) file -## Usage +## Usage with SQL backend ### Starting the docker compose 1. Start containers ```bash -docker compose up --build transactor peer datomic-db-initialization console -d +DOCKER_BUILDKIT=1 docker compose up \ + --build \ + transactor \ + datomic-db-initialization \ + peer \ + console \ + --force-recreate \ + --remove-orphans \ + -d ``` 2. Connect via Datomic peer ```clojure @@ -52,6 +60,53 @@ user=> (dc/connect client {:db-name "test-database"}) ;; DATOMIC_DB_NAME ``` +## Usage with Scylla DB backend + +### Starting the docker compose + +```bash +DOCKER_BUILDKIT=1 docker compose \ + -f ./docker-compose-scylla-db.yml \ + up \ + --build \ + transactor \ + datomic-db-initialization \ + peer \ + console \ + --force-recreate \ + --remove-orphans \ + -d +``` + +2. Connect via Datomic peer +```clojure +(require '[datomic.api :as d]) +;; nil + +;; assuming DATOMIC DB name is stored in environment variable `DATOMIC_DB_NAME` +(def db-uri (format "datomic:ddb-local://localhost:8000/%s" (System/getenv "DATOMIC_DB_NAME"))) +;; #'user/db-uri + +user=> (d/connect db-uri) +;; #object[datomic.peer.Connection 0x738da6f2 "{:unsent-updates-queue 0, :pending-txes 0, :next-t 1000, :basis-t 66, :index-rev 0, :db-id \"hello-275c1f24-482a-4ae2-86a8-844555f04f46\"}"] + +``` +3. Connect via Datomic client +```clojure +(require '[datomic.client.api :as dc]) + +(def client (dd/client {:server-type :peer-server + :access-key "myaccesskey" ;; PEER_ACCESSKEY + :secret "mysecret" ;; PEER_SECRET + :endpoint "localhost:8998" + :validate-hostnames false})) +;; #'user/client + +user=> (dc/connect client {:db-name "test-database"}) ;; DATOMIC_DB_NAME +{:db-name "hello", :database-id "hello-275c1f24-482a-4ae2-86a8-844555f04f46", :t 66, :next-t 1000, :type :datomic.client/conn} + +``` + ### Stopping the docker compose ```bash docker compose down diff --git a/docker-compose/datomic-database-initialization/initialize_database.clj b/docker-compose/datomic-database-initialization/initialize_database.clj index ef0d2d5..54e8209 100644 --- a/docker-compose/datomic-database-initialization/initialize_database.clj +++ b/docker-compose/datomic-database-initialization/initialize_database.clj @@ -1,11 +1,19 @@ (require '[datomic.api :as d]) (import '[clojure.lang ExceptionInfo]) -(def uri (format "datomic:sql://%s?%s?user=%s&password=%s" - (System/getenv "DATOMIC_DB_NAME") - (System/getenv "SQL_JDBC_URL") - (System/getenv "SQL_USER") - (System/getenv "SQL_PASSWORD"))) +(def uri + (cond + (= (System/getenv "PROTOCOL") "DDB-LOCAL") + (format "datomic:ddb-local://%s/%s/%s" + (System/getenv "LOCAL_DYNAMO_DB_ENDPOINT") + (System/getenv "DYNAMO_DB_TABLE") + (System/getenv "DATOMIC_DB_NAME")) + + :else (format "datomic:sql://%s?%s?user=%s&password=%s" + (System/getenv "DATOMIC_DB_NAME") + (System/getenv "SQL_JDBC_URL") + (System/getenv "SQL_USER") + (System/getenv "SQL_PASSWORD")))) (defn database-migrator [uri] diff --git a/docker-compose/docker-compose-scylla-db.yml b/docker-compose/docker-compose-scylla-db.yml new file mode 100644 index 0000000..f2cc816 --- /dev/null +++ b/docker-compose/docker-compose-scylla-db.yml @@ -0,0 +1,174 @@ +version: '3.9' +name: datomic-cluster + +services: + memcached: + container_name: memcached + image: memcached:${MEMCACHED_IMAGE_TAG} + command: memcached -m ${MEMCACHED_MAX_RAM} + ports: + - ${MEMCACHED_HOST_MAPPING_PORT}:11211 + restart: always + networks: + datomic-network: + ipv4_address: 192.168.200.2 + healthcheck: + test: echo stats | nc 127.0.0.1 11211 + interval: 10s + retries: 60 + + scylla-db: + container_name: scylla-db + image: scylladb/scylla + ports: + - ${SCYLLA_DB_HOST_MAPPING_PORT}:8000 + command: "--smp 1 --memory=750M --alternator-port=8000 --alternator-write-isolation=always_use_lwt" + volumes: + - scylla-db:/var/lib/scylla + networks: + datomic-network: + ipv4_address: 192.168.200.3 + restart: always + healthcheck: + test: cqlsh -e "SHOW VERSION" || exit 1 + interval: 15s + retries: 10 + start_period: 30s + timeout: 30s + + transactor: + build: + context: ../ + args: + DATOMIC_VERSION: ${DATOMIC_VERSION} + dockerfile: Dockerfile + ports: + - "4336:4336" + - "4335:4335" + - "4334:4334" + - "9999:9999" + environment: + - RUN_MODE=TRANSACTOR + - PROTOCOL=${SCYLLA_DB_PROTOCOL} + - DYNAMO_DB_TABLE=${DYNAMO_DB_TABLE} + - LOCAL_DYNAMO_DB_ENDPOINT=scylla-db:8000 + - TRANSACTOR_HOST=192.168.200.4 + - TRANSACTOR_ALT_HOST=127.0.0.1 + - MEMCACHED_HOST=192.168.200.2 + volumes: + - datomic-transactor-log:/datomic-pro/log + healthcheck: + test: ["CMD-SHELL", 'if [[ $(curl -s -o /dev/null -w "%{http_code}" -X GET http://transactor:9999/health) = "200" ]]; then echo 0; else echo 1; fi'] + interval: 10s + timeout: 3s + retries: 30 + networks: + datomic-network: + ipv4_address: 192.168.200.4 + restart: always + depends_on: + scylla-db: + condition: service_healthy + memcached: + condition: service_healthy + + datomic-db-initialization: + build: + context: ../ + args: + DATOMIC_VERSION: ${DATOMIC_VERSION} + dockerfile: Dockerfile + environment: + - RUN_MODE=TRANSACTOR + - PROTOCOL=${SCYLLA_DB_PROTOCOL} + - DYNAMO_DB_TABLE=${DYNAMO_DB_TABLE} + - LOCAL_DYNAMO_DB_ENDPOINT=scylla-db:8000 + - DATOMIC_DB_NAME=${DATOMIC_DB_NAME} + - RETRIES=${DATOMIC_DB_CREATION_RETRIES} + volumes: + - "./datomic-database-initialization/initialize_database.clj:/datomic-pro/initialize_database.clj" + restart: on-failure:1 + entrypoint: /datomic-pro/bin/run /datomic-pro/initialize_database.clj + networks: + datomic-network: + ipv4_address: 192.168.200.254 + depends_on: + scylla-db: + condition: service_healthy + memcached: + condition: service_healthy + transactor: + condition: service_healthy + + peer: + build: + context: ../ + args: + DATOMIC_VERSION: ${DATOMIC_VERSION} + dockerfile: Dockerfile + ports: + - "8998:8998" + environment: + - RUN_MODE=PEER + - PROTOCOL=${PROTOCOL} + - PEER_HOST=192.168.200.5 + - PROTOCOL=${SCYLLA_DB_PROTOCOL} + - DYNAMO_DB_TABLE=${DYNAMO_DB_TABLE} + - LOCAL_DYNAMO_DB_ENDPOINT=scylla-db:8000 + - DATOMIC_DB_NAME=${DATOMIC_DB_NAME} + - PEER_ACCESSKEY=${PEER_ACCESSKEY} + - PEER_SECRET=${PEER_SECRET} + - MEMCACHED_HOST=192.168.200.2 + restart: always + networks: + datomic-network: + ipv4_address: 192.168.200.5 + depends_on: + scylla-db: + condition: service_healthy + memcached: + condition: service_healthy + transactor: + condition: service_healthy + datomic-db-initialization: + condition: service_completed_successfully + + console: + build: + context: ../ + args: + DATOMIC_VERSION: ${DATOMIC_VERSION} + dockerfile: Dockerfile + ports: + - ${CONSOLE_HOST_MAPPING_PORT}:8888 + environment: + - RUN_MODE=CONSOLE + - PROTOCOL=${SCYLLA_DB_PROTOCOL} + - DYNAMO_DB_TABLE=${DYNAMO_DB_TABLE} + - LOCAL_DYNAMO_DB_ENDPOINT=scylla-db:8000 + - TRANSACTOR_ALIAS=home-txn + networks: + datomic-network: + ipv4_address: 192.168.200.6 + restart: always + depends_on: + scylla-db: + condition: service_healthy + transactor: + condition: service_healthy + datomic-db-initialization: + condition: service_completed_successfully + + +networks: + datomic-network: + driver: bridge + ipam: + driver: default + config: + - + subnet: 192.168.200.0/24 + +volumes: + scylla-db: + datomic-transactor-log: