From 3ce969fd79242a4e1f46d070603fb3c57553ac88 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Wed, 20 Apr 2022 10:58:58 +0800 Subject: [PATCH] refactor: always recreate resources no matter it is connected or not --- apps/emqx_connector/src/emqx_connector_mqtt.erl | 1 + .../test/emqx_connector_api_SUITE.erl | 7 ++++--- apps/emqx_resource/src/emqx_resource_instance.erl | 13 +++---------- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/apps/emqx_connector/src/emqx_connector_mqtt.erl b/apps/emqx_connector/src/emqx_connector_mqtt.erl index 60484b018..d59cdf239 100644 --- a/apps/emqx_connector/src/emqx_connector_mqtt.erl +++ b/apps/emqx_connector/src/emqx_connector_mqtt.erl @@ -207,6 +207,7 @@ basic_config(#{ replayq => ReplayQ, %% connection opts server => Server, + connect_timeout => 30, %% 30s reconnect_interval => ReconnIntv, proto_ver => ProtoVer, bridge_mode => true, diff --git a/apps/emqx_connector/test/emqx_connector_api_SUITE.erl b/apps/emqx_connector/test/emqx_connector_api_SUITE.erl index d90fae5fd..9291af624 100644 --- a/apps/emqx_connector/test/emqx_connector_api_SUITE.erl +++ b/apps/emqx_connector/test/emqx_connector_api_SUITE.erl @@ -366,9 +366,10 @@ t_mqtt_conn_update(_) -> BridgeIDEgress = emqx_bridge:bridge_id(?CONNECTR_TYPE, ?BRIDGE_NAME_EGRESS), wait_for_resource_ready(BridgeIDEgress, 5), - %% then we try to update 'server' of the connector, to an unavailable IP address - %% the update should fail because of 'unreachable' or 'connrefused' - {ok, 500, _ErrorMsg} = request(put, uri(["connectors", ConnctorID]), + %% Then we try to update 'server' of the connector, to an unavailable IP address + %% The update OK, we recreate the resource even if the resource is current connected, + %% and the target resource we're going to update is unavailable. + {ok, 200, _} = request(put, uri(["connectors", ConnctorID]), ?MQTT_CONNECTOR2(<<"127.0.0.1:2603">>)), %% we fix the 'server' parameter to a normal one, it should work {ok, 200, _} = request(put, uri(["connectors", ConnctorID]), diff --git a/apps/emqx_resource/src/emqx_resource_instance.erl b/apps/emqx_resource/src/emqx_resource_instance.erl index af266f763..66f86e010 100644 --- a/apps/emqx_resource/src/emqx_resource_instance.erl +++ b/apps/emqx_resource/src/emqx_resource_instance.erl @@ -166,16 +166,9 @@ code_change(_OldVsn, State, _Extra) -> do_recreate(InstId, ResourceType, NewConfig, Opts) -> case lookup(InstId) of - {ok, Group, #{mod := ResourceType, status := connected} = Data} -> - %% If this resource is in use (status='connected'), we should make sure - %% the new config is OK before removing the old one. - case do_create_dry_run(ResourceType, NewConfig) of - ok -> - do_remove(Group, Data, false), - do_create(InstId, Group, ResourceType, NewConfig, Opts); - Error -> - Error - end; + %% We recreate the resource no matter if it is connected and in use! + %% As we can not know if the resource is "really disconnected" or we mark the status + %% to "disconnected" because the emqx_resource_instance process is not responding. {ok, Group, #{mod := ResourceType, status := _} = Data} -> do_remove(Group, Data, false), do_create(InstId, Group, ResourceType, NewConfig, Opts);