Skip to content

Commit

Permalink
feat: support customized initialization SQL statements (#271)
Browse files Browse the repository at this point in the history
* fix: refactor the postgresql and duckdb config management

* feat: support customized initialization SQL statements (#268)
  • Loading branch information
VWagen1989 authored Dec 9, 2024
1 parent e51a2f5 commit 3e9610d
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 57 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- [Installation](#installation)
- [Usage](#usage)
- [Replicating Data](#replicating-data)
- [Initialize MyDuck Server with Custom SQLs](#initialize-myduck-server-with-custom-sqls)
- [Connecting to Cloud MySQL & Postgres](#connecting-to-cloud-mysql--postgres)
- [HTAP Setup](#htap-setup)
- [Query Parquet Files](#query-parquet-files)
Expand Down Expand Up @@ -146,6 +147,19 @@ docker run -d --name myduck \
> [!NOTE]
> To replicate from a server running on the host machine, use `host.docker.internal` as the hostname instead of `localhost` or `127.0.0.1`. On Linux, you must also add `--add-host=host.docker.internal:host-gateway` to the `docker run` command.
### Initialize MyDuck Server with Custom SQLs

To initialize MyDuck Server by executing custom SQL statements once it’s ready, you can place the SQL statements in a file with the .sql extension and mount the file to either `/docker-entrypoint-initdb.d/mysql/` or `/docker-entrypoint-initdb.d/postgres/` in the Docker container. The choice between these directories depends on the protocol used for executing the SQL statements.

For instance:
```bash
# Execute init.sql on MySQL protocol
docker run -d -p 13306:3306 -p 15432:5432 --name=myduck -v ./init.sql:/docker-entrypoint-initdb.d/mysql/init.sql apecloud/myduckserver:latest

# Execute init.sql on PostgreSQL protocol
docker run -d -p 13306:3306 -p 15432:5432 --name=myduck -v ./init.sql:/docker-entrypoint-initdb.d/postgres/init.sql apecloud/myduckserver:latest
```

### Connecting to Cloud MySQL & Postgres

MyDuck Server supports setting up replicas from common cloud-based MySQL & Postgres offerings. For more information, please refer to the [replica setup guide](docs/tutorial/replica-setup-rds.md).
Expand Down
1 change: 0 additions & 1 deletion catalog/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func NewDBProvider(dataDir, dbFile string) (*DatabaseProvider, error) {
}

storage := stdsql.OpenDB(connector)
configuration.Init(storage)

bootQueries := []string{
"INSTALL arrow",
Expand Down
35 changes: 0 additions & 35 deletions configuration/duck_config.go

This file was deleted.

5 changes: 3 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ RUN if [ "$TARGETARCH" = "arm64" ]; then \
fi && \
rm -f /usr/local/mysqlsh.arm64

# Setup user and working directory
# Setup user and working directory, and create init directories
RUN useradd --create-home --user-group --shell /bin/bash admin \
&& echo 'admin:admin' | chpasswd \
&& echo 'admin ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
&& echo 'admin ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers \
&& mkdir -p /docker-entrypoint-initdb.d/mysql /docker-entrypoint-initdb.d/postgres

USER admin
WORKDIR /home/admin
Expand Down
41 changes: 33 additions & 8 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export LOG_PATH="${HOME}/log"
export MYSQL_REPLICA_SETUP_PATH="${HOME}/replica-setup-mysql"
export POSTGRES_REPLICA_SETUP_PATH="${HOME}/replica-setup-postgres"
export PID_FILE="${LOG_PATH}/myduck.pid"
export INIT_SQLS_DIR="/docker-entrypoint-initdb.d"

parse_dsn() {
# Check if SOURCE_DSN is set
Expand Down Expand Up @@ -126,15 +127,10 @@ run_replica_setup() {

run_server_in_background() {
cd "$DATA_PATH" || { echo "Error: Could not change directory to ${DATA_PATH}"; exit 1; }
nohup myduckserver $LOG_LEVEL >> "${LOG_PATH}"/server.log 2>&1 &
nohup myduckserver $LOG_LEVEL $PROFILER_PORT | tee -a "${LOG_PATH}"/server.log 2>&1 &
echo "$!" > "${PID_FILE}"
}

run_server_in_foreground() {
cd "$DATA_PATH" || { echo "Error: Could not change directory to ${DATA_PATH}"; exit 1; }
myduckserver $LOG_LEVEL
}

wait_for_my_duck_server_ready() {
local host="127.0.0.1"
local user="root"
Expand Down Expand Up @@ -180,6 +176,28 @@ check_process_alive() {
fi
}

execute_init_sqls() {
local host="127.0.0.1"
local mysql_user="root"
local mysql_port="3306"
local postgres_user="postgres"
local postgres_port="5432"
if [ -d "$INIT_SQLS_DIR/mysql" ] && [ "$(find "$INIT_SQLS_DIR/mysql" -maxdepth 1 -name '*.sql' -type f | head -n 1)" ]; then
echo "Executing init SQL scripts from $INIT_SQLS_DIR/mysql..."
for file in "$INIT_SQLS_DIR/mysql"/*.sql; do
echo "Executing $file..."
mysqlsh --sql --host "$host" --port "$mysql_port" --user "$mysql_user" --no-password --file="$file"
done
fi
if [ -d "$INIT_SQLS_DIR/postgres" ] && [ "$(find "$INIT_SQLS_DIR/postgres" -maxdepth 1 -name '*.sql' -type f | head -n 1)" ]; then
echo "Executing init SQL scripts from $INIT_SQLS_DIR/postgres..."
for file in "$INIT_SQLS_DIR/postgres"/*.sql; do
echo "Executing $file..."
psql -h "$host" -p "$postgres_port" -U "$postgres_user" -f "$file"
done
fi
}

# Handle the setup_mode
setup() {
# Setup signal handlers
Expand All @@ -189,19 +207,26 @@ setup() {
export LOG_LEVEL="-loglevel $LOG_LEVEL"
fi

if [ -n "$PROFILER_PORT" ]; then
export PROFILER_PORT="-profiler-port $PROFILER_PORT"
fi

# Ensure required directories exist
mkdir -p "${DATA_PATH}" "${LOG_PATH}"

case "$SETUP_MODE" in
"" | "SERVER")
echo "Starting MyDuck Server in SERVER mode..."
run_server_in_foreground
run_server_in_background
wait_for_my_duck_server_ready
execute_init_sqls
;;
"REPLICA")
echo "Starting MyDuck Server in REPLICA mode..."
parse_dsn
run_server_in_background
wait_for_my_duck_server_ready
execute_init_sqls
run_replica_setup
;;
*)
Expand All @@ -213,7 +238,7 @@ setup() {

setup

while [[ "$SETUP_MODE" == "REPLICA" ]]; do
while true; do
# Check if the processes have started
if ! check_process_alive "$PID_FILE" "MyDuck Server"; then
echo "CRITICAL: MyDuck Server process died unexpectedly."
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"github.com/apecloud/myduckserver/catalog"
"github.com/apecloud/myduckserver/myfunc"
"github.com/apecloud/myduckserver/pgserver"
"github.com/apecloud/myduckserver/pgserver/config"
"github.com/apecloud/myduckserver/pgserver/logrepl"
"github.com/apecloud/myduckserver/pgserver/pgconfig"
"github.com/apecloud/myduckserver/plugin"
"github.com/apecloud/myduckserver/replica"
"github.com/apecloud/myduckserver/transpiler"
Expand Down Expand Up @@ -188,7 +188,7 @@ func main() {
}

// Load the configuration for the Postgres server.
config.Init()
pgconfig.Init()
go pgServer.Start()
}

Expand Down
12 changes: 6 additions & 6 deletions pgserver/pg_catalog_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/apecloud/myduckserver/adapter"
"github.com/apecloud/myduckserver/catalog"
duckConfig "github.com/apecloud/myduckserver/configuration"
"github.com/apecloud/myduckserver/pgserver/pgconfig"
"github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree"
"github.com/dolthub/go-mysql-server/sql"
"github.com/jackc/pgx/v5/pgproto3"
Expand Down Expand Up @@ -199,7 +199,7 @@ func isPgCurrentSetting(query ConvertedQuery) bool {
if len(matches) != 3 {
return false
}
if duckConfig.IsValidConfig(matches[2]) {
if !pgconfig.IsValidPostgresConfigParameter(matches[2]) {
// This is a configuration of DuckDB, it should be bypassed to DuckDB
return false
}
Expand Down Expand Up @@ -290,7 +290,7 @@ var pgCatalogHandlers = map[string]PGCatalogHandler{
// Route it to the engine directly.
return false, nil
}
if duckConfig.IsValidConfig(key) {
if !pgconfig.IsValidPostgresConfigParameter(key) {
// This is a configuration of DuckDB, it should be bypassed to DuckDB
return false, nil
}
Expand All @@ -315,7 +315,7 @@ var pgCatalogHandlers = map[string]PGCatalogHandler{
// Route it to the engine directly.
return false, nil
}
if duckConfig.IsValidConfig(key) {
if !pgconfig.IsValidPostgresConfigParameter(key) {
// This is a configuration of DuckDB, it should be bypassed to DuckDB
return false, nil
}
Expand Down Expand Up @@ -344,7 +344,7 @@ var pgCatalogHandlers = map[string]PGCatalogHandler{
return false, fmt.Errorf("error: invalid reset statement: %v", stmt)
}
key := strings.ToLower(stmt.Name)
if duckConfig.IsValidConfig(key) {
if !pgconfig.IsValidPostgresConfigParameter(key) {
return false, nil
}
return true, nil
Expand All @@ -357,7 +357,7 @@ var pgCatalogHandlers = map[string]PGCatalogHandler{
return false, fmt.Errorf("error: invalid reset statement: %v", query.String)
}
key := strings.ToLower(resetVar.Name)
if duckConfig.IsValidConfig(key) {
if !pgconfig.IsValidPostgresConfigParameter(key) {
// This is a configuration of DuckDB, it should be bypassed to DuckDB
return false, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// Copied from github.com/dolthub/doltgresql/server/config/parameters.go
package config
package pgconfig

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// Copied from github.com/dolthub/doltgresql/server/config/parameters_list.go
package config
package pgconfig

import (
"math"
Expand Down
2 changes: 1 addition & 1 deletion pgtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/apecloud/myduckserver/backend"
"github.com/apecloud/myduckserver/catalog"
"github.com/apecloud/myduckserver/pgserver"
pgConfig "github.com/apecloud/myduckserver/pgserver/config"
pgConfig "github.com/apecloud/myduckserver/pgserver/pgconfig"
sqle "github.com/dolthub/go-mysql-server"
"github.com/dolthub/go-mysql-server/memory"
"github.com/dolthub/go-mysql-server/server"
Expand Down

0 comments on commit 3e9610d

Please sign in to comment.