diff --git a/apps/emqx_authn/src/simple_authn/emqx_authn_http.erl b/apps/emqx_authn/src/simple_authn/emqx_authn_http.erl index 9308bbe95..7bf9e1503 100644 --- a/apps/emqx_authn/src/simple_authn/emqx_authn_http.erl +++ b/apps/emqx_authn/src/simple_authn/emqx_authn_http.erl @@ -134,7 +134,7 @@ create(#{method := Method, emqx_connector_http, Config#{base_url => maps:remove(query, URIMap), pool_type => random}, - #{waiting_connect_complete => 5000}) of + #{}) of {ok, already_created} -> {ok, State}; {ok, _} -> diff --git a/apps/emqx_authn/src/simple_authn/emqx_authn_mongodb.erl b/apps/emqx_authn/src/simple_authn/emqx_authn_mongodb.erl index 158306a87..762813e5c 100644 --- a/apps/emqx_authn/src/simple_authn/emqx_authn_mongodb.erl +++ b/apps/emqx_authn/src/simple_authn/emqx_authn_mongodb.erl @@ -116,7 +116,7 @@ create(#{selector := Selector} = Config) -> ?RESOURCE_GROUP, emqx_connector_mongo, Config, - #{waiting_connect_complete => 5000}) of + #{}) of {ok, already_created} -> {ok, NState}; {ok, _} -> diff --git a/apps/emqx_authn/src/simple_authn/emqx_authn_mysql.erl b/apps/emqx_authn/src/simple_authn/emqx_authn_mysql.erl index b347ff30c..61a1cdf81 100644 --- a/apps/emqx_authn/src/simple_authn/emqx_authn_mysql.erl +++ b/apps/emqx_authn/src/simple_authn/emqx_authn_mysql.erl @@ -85,7 +85,7 @@ create(#{password_hash_algorithm := Algorithm, ?RESOURCE_GROUP, emqx_connector_mysql, Config, - #{waiting_connect_complete => 5000}) of + #{}) of {ok, already_created} -> {ok, State}; {ok, _} -> diff --git a/apps/emqx_authn/src/simple_authn/emqx_authn_pgsql.erl b/apps/emqx_authn/src/simple_authn/emqx_authn_pgsql.erl index 856271db3..7f9735349 100644 --- a/apps/emqx_authn/src/simple_authn/emqx_authn_pgsql.erl +++ b/apps/emqx_authn/src/simple_authn/emqx_authn_pgsql.erl @@ -81,7 +81,7 @@ create(#{query := Query0, resource_id => ResourceId}, case emqx_resource:create_local(ResourceId, ?RESOURCE_GROUP, emqx_connector_pgsql, Config#{named_queries => #{ResourceId => Query}}, - #{waiting_connect_complete => 5000}) of + #{}) of {ok, already_created} -> {ok, State}; {ok, _} -> diff --git a/apps/emqx_authn/src/simple_authn/emqx_authn_redis.erl b/apps/emqx_authn/src/simple_authn/emqx_authn_redis.erl index e604acfe3..19d590287 100644 --- a/apps/emqx_authn/src/simple_authn/emqx_authn_redis.erl +++ b/apps/emqx_authn/src/simple_authn/emqx_authn_redis.erl @@ -93,7 +93,7 @@ create(#{cmd := Cmd, resource_id => ResourceId}, case emqx_resource:create_local(ResourceId, ?RESOURCE_GROUP, emqx_connector_redis, Config, - #{waiting_connect_complete => 5000}) of + #{}) of {ok, already_created} -> {ok, NState}; {ok, _} -> diff --git a/apps/emqx_authn/test/emqx_authn_mysql_SUITE.erl b/apps/emqx_authn/test/emqx_authn_mysql_SUITE.erl index 4199564c2..b83f710f1 100644 --- a/apps/emqx_authn/test/emqx_authn_mysql_SUITE.erl +++ b/apps/emqx_authn/test/emqx_authn_mysql_SUITE.erl @@ -63,7 +63,7 @@ init_per_suite(Config) -> ?RESOURCE_GROUP, emqx_connector_mysql, mysql_config(), - #{waiting_connect_complete => 5000}), + #{}), Config; false -> {skip, no_mysql} diff --git a/apps/emqx_authn/test/emqx_authn_pgsql_SUITE.erl b/apps/emqx_authn/test/emqx_authn_pgsql_SUITE.erl index a4d6f8c07..572e45f00 100644 --- a/apps/emqx_authn/test/emqx_authn_pgsql_SUITE.erl +++ b/apps/emqx_authn/test/emqx_authn_pgsql_SUITE.erl @@ -64,7 +64,7 @@ init_per_suite(Config) -> ?RESOURCE_GROUP, emqx_connector_pgsql, pgsql_config(), - #{waiting_connect_complete => 5000}), + #{}), Config; false -> {skip, no_pgsql} diff --git a/apps/emqx_authn/test/emqx_authn_redis_SUITE.erl b/apps/emqx_authn/test/emqx_authn_redis_SUITE.erl index c02830ab7..fcc00612e 100644 --- a/apps/emqx_authn/test/emqx_authn_redis_SUITE.erl +++ b/apps/emqx_authn/test/emqx_authn_redis_SUITE.erl @@ -63,7 +63,7 @@ init_per_suite(Config) -> ?RESOURCE_GROUP, emqx_connector_redis, redis_config(), - #{waiting_connect_complete => 5000}), + #{}), Config; false -> {skip, no_redis} diff --git a/apps/emqx_authz/src/emqx_authz_postgresql.erl b/apps/emqx_authz/src/emqx_authz_postgresql.erl index a127a9c2b..9a783ddcf 100644 --- a/apps/emqx_authz/src/emqx_authz_postgresql.erl +++ b/apps/emqx_authz/src/emqx_authz_postgresql.erl @@ -56,7 +56,7 @@ init(#{query := SQL0} = Source) -> ?RESOURCE_GROUP, emqx_connector_pgsql, Source#{named_queries => #{ResourceID => SQL}}, - #{waiting_connect_complete => 5000}) of + #{}) of {ok, _} -> Source#{annotations => #{id => ResourceID, diff --git a/apps/emqx_authz/src/emqx_authz_utils.erl b/apps/emqx_authz/src/emqx_authz_utils.erl index 73e387d81..4a0e447e9 100644 --- a/apps/emqx_authz/src/emqx_authz_utils.erl +++ b/apps/emqx_authz/src/emqx_authz_utils.erl @@ -38,7 +38,7 @@ create_resource(Module, Config) -> case emqx_resource:create_local(ResourceID, ?RESOURCE_GROUP, Module, Config, - #{waiting_connect_complete => 5000}) of + #{}) of {ok, already_created} -> {ok, ResourceID}; {ok, _} -> {ok, ResourceID}; {error, Reason} -> {error, Reason} diff --git a/apps/emqx_authz/test/emqx_authz_mysql_SUITE.erl b/apps/emqx_authz/test/emqx_authz_mysql_SUITE.erl index 1bdff9455..8852ecc98 100644 --- a/apps/emqx_authz/test/emqx_authz_mysql_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_mysql_SUITE.erl @@ -45,7 +45,7 @@ init_per_suite(Config) -> ?RESOURCE_GROUP, emqx_connector_mysql, mysql_config(), - #{waiting_connect_complete => 5000}), + #{}), Config; false -> {skip, no_mysql} diff --git a/apps/emqx_authz/test/emqx_authz_postgresql_SUITE.erl b/apps/emqx_authz/test/emqx_authz_postgresql_SUITE.erl index 5f8c914fe..41a1a504b 100644 --- a/apps/emqx_authz/test/emqx_authz_postgresql_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_postgresql_SUITE.erl @@ -45,7 +45,7 @@ init_per_suite(Config) -> ?RESOURCE_GROUP, emqx_connector_pgsql, pgsql_config(), - #{waiting_connect_complete => 5000}), + #{}), Config; false -> {skip, no_pgsql} diff --git a/apps/emqx_authz/test/emqx_authz_redis_SUITE.erl b/apps/emqx_authz/test/emqx_authz_redis_SUITE.erl index 519973ebe..0045ce926 100644 --- a/apps/emqx_authz/test/emqx_authz_redis_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_redis_SUITE.erl @@ -46,7 +46,7 @@ init_per_suite(Config) -> ?RESOURCE_GROUP, emqx_connector_redis, redis_config(), - #{waiting_connect_complete => 5000}), + #{}), Config; false -> {skip, no_redis} diff --git a/apps/emqx_bridge/src/emqx_bridge.erl b/apps/emqx_bridge/src/emqx_bridge.erl index 7aa9f4b7b..d0c4e389c 100644 --- a/apps/emqx_bridge/src/emqx_bridge.erl +++ b/apps/emqx_bridge/src/emqx_bridge.erl @@ -225,7 +225,7 @@ create(Type, Name, Conf) -> <<"emqx_bridge">>, emqx_bridge:resource_type(Type), parse_confs(Type, Name, Conf), - #{waiting_connect_complete => 5000}) of + #{}) of {ok, already_created} -> maybe_disable_bridge(Type, Name, Conf); {ok, _} -> maybe_disable_bridge(Type, Name, Conf); {error, Reason} -> {error, Reason} @@ -272,7 +272,7 @@ recreate(Type, Name, Conf) -> emqx_resource:recreate_local(resource_id(Type, Name), emqx_bridge:resource_type(Type), parse_confs(Type, Name, Conf), - #{waiting_connect_complete => 5000}). + #{}). create_dry_run(Type, Conf) -> Conf0 = Conf#{<<"ingress">> => #{<<"remote_topic">> => <<"t">>}}, diff --git a/apps/emqx_bridge/test/emqx_bridge_api_SUITE.erl b/apps/emqx_bridge/test/emqx_bridge_api_SUITE.erl index 4eb457af4..2c69dc457 100644 --- a/apps/emqx_bridge/test/emqx_bridge_api_SUITE.erl +++ b/apps/emqx_bridge/test/emqx_bridge_api_SUITE.erl @@ -79,8 +79,14 @@ init_per_testcase(_, Config) -> {ok, _} = emqx_cluster_rpc:start_link(node(), emqx_cluster_rpc, 1000), Config. end_per_testcase(_, _Config) -> + clear_resources(), ok. +clear_resources() -> + lists:foreach(fun(#{type := Type, name := Name}) -> + ok = emqx_bridge:remove(Type, Name) + end, emqx_bridge:list()). + %%------------------------------------------------------------------------------ %% HTTP server for testing %%------------------------------------------------------------------------------ diff --git a/apps/emqx_connector/test/emqx_connector_mysql_SUITE.erl b/apps/emqx_connector/test/emqx_connector_mysql_SUITE.erl index 47d8f31c8..29ba2c181 100644 --- a/apps/emqx_connector/test/emqx_connector_mysql_SUITE.erl +++ b/apps/emqx_connector/test/emqx_connector_mysql_SUITE.erl @@ -71,7 +71,7 @@ perform_lifecycle_check(PoolName, InitialConfig) -> ?CONNECTOR_RESOURCE_GROUP, ?MYSQL_RESOURCE_MOD, CheckedConfig, - #{waiting_connect_complete => 5000} + #{} ), ?assertEqual(InitialStatus, connected), % Instance should match the state and status of the just started resource diff --git a/apps/emqx_connector/test/emqx_connector_pgsql_SUITE.erl b/apps/emqx_connector/test/emqx_connector_pgsql_SUITE.erl index bc7b2eb4d..0252e0816 100644 --- a/apps/emqx_connector/test/emqx_connector_pgsql_SUITE.erl +++ b/apps/emqx_connector/test/emqx_connector_pgsql_SUITE.erl @@ -72,7 +72,7 @@ perform_lifecycle_check(PoolName, InitialConfig) -> ?CONNECTOR_RESOURCE_GROUP, ?PGSQL_RESOURCE_MOD, CheckedConfig, - #{waiting_connect_complete => 5000} + #{} ), ?assertEqual(InitialStatus, connected), % Instance should match the state and status of the just started resource diff --git a/apps/emqx_connector/test/emqx_connector_redis_SUITE.erl b/apps/emqx_connector/test/emqx_connector_redis_SUITE.erl index f1fcee67c..8e473c397 100644 --- a/apps/emqx_connector/test/emqx_connector_redis_SUITE.erl +++ b/apps/emqx_connector/test/emqx_connector_redis_SUITE.erl @@ -86,7 +86,7 @@ perform_lifecycle_check(PoolName, InitialConfig, RedisCommand) -> ?CONNECTOR_RESOURCE_GROUP, ?REDIS_RESOURCE_MOD, CheckedConfig, - #{waiting_connect_complete => 5000} + #{} ), ?assertEqual(InitialStatus, connected), % Instance should match the state and status of the just started resource diff --git a/apps/emqx_plugin_libs/src/emqx_plugin_libs_metrics.erl b/apps/emqx_plugin_libs/src/emqx_plugin_libs_metrics.erl index eef945a81..7eefe8701 100644 --- a/apps/emqx_plugin_libs/src/emqx_plugin_libs_metrics.erl +++ b/apps/emqx_plugin_libs/src/emqx_plugin_libs_metrics.erl @@ -157,6 +157,8 @@ init(Name) -> persistent_term:put(?CntrRef(Name), #{}), {ok, #state{}}. +handle_call({get_rate, _Id}, _From, State = #state{rates = undefined}) -> + {reply, #{}, State}; handle_call({get_rate, Id}, _From, State = #state{rates = Rates}) -> {reply, case maps:get(Id, Rates, undefined) of undefined -> #{}; diff --git a/apps/emqx_resource/src/emqx_resource_instance.erl b/apps/emqx_resource/src/emqx_resource_instance.erl index b828f1b40..059ceefdf 100644 --- a/apps/emqx_resource/src/emqx_resource_instance.erl +++ b/apps/emqx_resource/src/emqx_resource_instance.erl @@ -178,14 +178,17 @@ do_recreate(InstId, ResourceType, NewConfig, Opts) -> {error, not_found} end. -wait_for_resource_ready(InstId, 0) -> - force_lookup(InstId); -wait_for_resource_ready(InstId, Retry) -> +wait_for_resource_ready(InstId, WaitTime) -> + do_wait_for_resource_ready(InstId, WaitTime div 100). + +do_wait_for_resource_ready(_InstId, 0) -> + timeout; +do_wait_for_resource_ready(InstId, Retry) -> case force_lookup(InstId) of - #{status := connected} = Data -> Data; + #{status := connected} -> ok; _ -> timer:sleep(100), - wait_for_resource_ready(InstId, Retry-1) + do_wait_for_resource_ready(InstId, Retry-1) end. do_create(InstId, Group, ResourceType, Config, Opts) -> @@ -197,8 +200,7 @@ do_create(InstId, Group, ResourceType, Config, Opts) -> ok -> ok = emqx_plugin_libs_metrics:create_metrics(resource_metrics, InstId, [matched, success, failed, exception], [matched]), - WaitTime = maps:get(waiting_connect_complete , Opts, 0), - {ok, wait_for_resource_ready(InstId, WaitTime div 100)}; + {ok, force_lookup(InstId)}; Error -> Error end @@ -252,6 +254,7 @@ do_start(InstId, Group, ResourceType, Config, Opts) when is_binary(InstId) -> spawn(fun() -> start_and_check(InstId, Group, ResourceType, Config, Opts, InitData) end), + _ = wait_for_resource_ready(InstId, maps:get(wait_for_resource_ready, Opts, 5000)), ok. start_and_check(InstId, Group, ResourceType, Config, Opts, Data) -> diff --git a/apps/emqx_retainer/src/emqx_retainer.erl b/apps/emqx_retainer/src/emqx_retainer.erl index 0a1b769a8..247f597b0 100644 --- a/apps/emqx_retainer/src/emqx_retainer.erl +++ b/apps/emqx_retainer/src/emqx_retainer.erl @@ -361,7 +361,7 @@ create_resource(Context, #{type := DB} = Config) -> <<"emqx_retainer">>, list_to_existing_atom(io_lib:format("~ts_~ts", [emqx_connector, DB])), Config, - #{waiting_connect_complete => 5000}) of + #{}) of {ok, already_created} -> Context#{resource_id => ResourceID}; {ok, _} ->