Merge pull request #10289 from kjellwinblad/pr_for_merge_into_release-50

Fix clickhouse bug in release-50
This commit is contained in:
Kjell Winblad 2023-03-31 09:40:31 +02:00 committed by GitHub
commit 1c799af065
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 74 additions and 15 deletions

View File

@ -0,0 +1 @@
Clickhouse has got a fix that makes the error message better when users click the test button in the settings dialog.

View File

@ -0,0 +1 @@
Clickhouse 已经修复了一个问题,当用户在设置对话框中点击测试按钮时,错误信息会更清晰。

View File

@ -12,4 +12,15 @@ emqx_ee_connector_clickhouse {
}
}
connect_timeout {
desc {
en: "The timeout when connecting to the Clickhouse server."
zh: "连接HTTP服务器的超时时间。"
}
label: {
en: "Clickhouse Timeout"
zh: "连接超时"
}
}
}

View File

@ -3,7 +3,7 @@
{hstreamdb_erl, {git, "https://github.com/hstreamdb/hstreamdb_erl.git", {tag, "0.2.5"}}},
{influxdb, {git, "https://github.com/emqx/influxdb-client-erl", {tag, "1.1.9"}}},
{tdengine, {git, "https://github.com/emqx/tdengine-client-erl", {tag, "0.1.5"}}},
{clickhouse, {git, "https://github.com/emqx/clickhouse-client-erl", {tag, "0.2"}}},
{clickhouse, {git, "https://github.com/emqx/clickhouse-client-erl", {tag, "0.3"}}},
{erlcloud, {git, "https://github.com/emqx/erlcloud.git", {tag,"3.5.16-emqx-1"}}},
{emqx, {path, "../../apps/emqx"}}
]}.

View File

@ -53,7 +53,6 @@
%% Internal exports used to execute code with ecpool worker
-export([
check_database_status/1,
execute_sql_in_clickhouse_server_using_connection/2
]).
@ -102,6 +101,14 @@ fields(config) ->
end,
desc => ?DESC("base_url")
}
)},
{connect_timeout,
hoconsc:mk(
emqx_schema:duration_ms(),
#{
default => <<"15s">>,
desc => ?DESC("connect_timeout")
}
)}
] ++ emqx_connector_schema_lib:relational_db_fields().
@ -137,7 +144,8 @@ on_start(
#{
url := URL,
database := DB,
pool_size := PoolSize
pool_size := PoolSize,
connect_timeout := ConnectTimeout
} = Config
) ->
?SLOG(info, #{
@ -155,7 +163,10 @@ on_start(
{pool_size, PoolSize},
{pool, PoolName}
],
InitState = #{poolname => PoolName},
InitState = #{
poolname => PoolName,
connect_timeout => ConnectTimeout
},
try
Templates = prepare_sql_templates(Config),
State = maps:merge(InitState, #{templates => Templates}),
@ -282,18 +293,52 @@ on_stop(ResourceID, #{poolname := PoolName}) ->
%% on_get_status emqx_resouce callback and related functions
%% -------------------------------------------------------------------
on_get_status(_ResourceID, #{poolname := Pool} = _State) ->
case
emqx_plugin_libs_pool:health_check_ecpool_workers(Pool, fun ?MODULE:check_database_status/1)
of
true ->
connected;
false ->
connecting
on_get_status(
_InstId,
#{
poolname := PoolName,
connect_timeout := Timeout
} = State
) ->
case do_get_status(PoolName, Timeout) of
ok ->
{connected, State};
{error, Reason} ->
{disconnected, State, Reason}
end.
check_database_status(Connection) ->
clickhouse:status(Connection).
do_get_status(PoolName, Timeout) ->
Workers = [Worker || {_WorkerName, Worker} <- ecpool:workers(PoolName)],
DoPerWorker =
fun(Worker) ->
case ecpool_worker:exec(Worker, fun clickhouse:detailed_status/1, Timeout) of
ok ->
ok;
{error, Reason} = Error ->
?SLOG(error, #{
msg => "clickhouse_connector_get_status_failed",
reason => Reason,
worker => Worker
}),
Error
end
end,
try emqx_misc:pmap(DoPerWorker, Workers, Timeout) of
Results ->
case [E || {error, _} = E <- Results] of
[] ->
ok;
Errors ->
hd(Errors)
end
catch
exit:timeout ->
?SLOG(error, #{
msg => "clickhouse_connector_pmap_failed",
reason => timeout
}),
{error, timeout}
end.
%% -------------------------------------------------------------------
%% on_query emqx_resouce callback and related functions

View File

@ -190,7 +190,8 @@ clickhouse_config() ->
?CLICKHOUSE_DEFAULT_PORT
]
)
)
),
connect_timeout => 10000
},
#{<<"config">> => Config}.