ClickHouse / clickhouse-js

Official JS client for ClickHouse DB
https://clickhouse.com
Apache License 2.0
214 stars 26 forks source link

Cannot create a cluster on ClickHouse within testcontainer via @clickhouse/client #335

Open jinalraj opened 3 days ago

jinalraj commented 3 days ago

Describe the bug

I am trying to create a table on a cluster that exists in a testcontainer:

export const createProgramsTable = async (client: ClickHouseClient) => {
  await client.exec({
    query: `
      CREATE TABLE IF NOT EXISTS programs ON CLUSTER '<cluster_name>' (
        id String,
        name String,
        facility_id String,
        active Boolean
      ) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/{installation}/<cluster_name>/tables/{database}/{table}','{replica}') ORDER BY id
    `
  });
};

using clickhouse/client:

const createCHClient = (): ClickHouseClient => {
  const disableCompression = process.env.CLICKHOUSE_ENABLE_COMPRESSION === 'false';
  console.log(process.env.CLICKHOUSE_URL, 'CLICKHOUSE_URL');
  const config: ClickHouseClientConfigOptions = {
    url: process.env.CLICKHOUSE_URL ?? '<url>',
    username: process.env.CLICKHOUSE_USER ?? 'admin',
    password: process.env.CLICKHOUSE_PASSWORD ?? '',
    database: process.env.CLICKHOUSE_DATABASE ?? 'default',
    request_timeout: process.env.CLICKHOUSE_REQUEST_TIMEOUT ? Number(process.env.CLICKHOUSE_REQUEST_TIMEOUT) : 15000, // 15 seconds
    max_open_connections: process.env.CLICKHOUSE_MAX_OPEN_CONNECTIONS ? Number(process.env.CLICKHOUSE_MAX_OPEN_CONNECTIONS) : 10,
    keep_alive: {
      enabled: !(process.env.CLICKHOUSE_KEEP_ALIVE === 'false'),
    },
    compression: disableCompression ? undefined : {
      response: true,
      request: true
    },
    log: {
      level: !(process.env.CLICKHOUSE_ENABLE_LOG === 'false') ? ClickHouseLogLevel.TRACE : ClickHouseLogLevel.ERROR
    },
    clickhouse_settings: {
      cluster_for_parallel_replicas: '<cluster-name>'
    }
  };
  return createClient(config);
};

but when I try to create a ClickHouseClient with:

clickhouse_settings: {
      cluster_for_parallel_replicas: '<cluster-name>'
    }

I still receive an error saying Requested cluster '<cluster_name>' not found.

Error log

console.error
      [2024-10-04T21:20:37.927Z][ERROR][@clickhouse/client][Connection] Exec: HTTP request error. 
      Arguments: {
        query: "CREATE TABLE IF NOT EXISTS programs ON CLUSTER '<cluster-name>' (\n" +
          '        id String,\n' +
          '        name String,\n' +
          '        facility_id String,\n' +
          '        active Boolean\n' +
          "      ) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/{installation}<cluster_name>/tables/{database}/{table}','{replica}') ORDER BY id",
        search_params: 'query_id=<query_id>&cluster_for_parallel_replicas=<cluster_name>&database=',
        with_abort_signal: false,
        session_id: undefined,
        query_id: '<query_id>',
        clickhouse_settings: { cluster_for_parallel_replicas: '<cluster-name>' }
      } 
      Caused by: ClickHouseError: Requested cluster '<cluster_name>' not found. 

ClickHouse server

slvrtrn commented 3 days ago

Does it work if you send the same query via cURL? In the logs, I see that the setting is properly attached to the search params: query_id=<query_id>&cluster_for_parallel_replicas=<cluster_name>&database=