Explorar el Código

refactor: always recreate resources no matter it is connected or not

Shawn hace 3 años
padre
commit
3ce969fd79

+ 1 - 0
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,

+ 4 - 3
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]),

+ 3 - 10
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);