浏览代码

fix(ds): Move DS backend supervision trees to their own apps

ieQu1 1 年之前
父节点
当前提交
9a58d71378

+ 1 - 1
apps/emqx_ds_builtin_local/src/emqx_ds_builtin_local.app.src

@@ -5,7 +5,7 @@
     {vsn, "0.1.0"},
     {modules, []},
     {registered, []},
-    {applications, [kernel, stdlib, rocksdb, emqx_durable_storage, emqx_utils]},
+    {applications, [kernel, stdlib, gproc, mria, rocksdb, emqx_durable_storage, emqx_utils]},
     {mod, {emqx_ds_builtin_local_app, []}},
     {env, []}
 ]}.

+ 1 - 2
apps/emqx_ds_builtin_local/src/emqx_ds_builtin_local_app.erl

@@ -27,8 +27,7 @@
 
 start(_StartType, _StartArgs) ->
     emqx_ds:register_backend(builtin_local, emqx_ds_builtin_local),
-    %% TODO: fixme
-    {ok, self()}.
+    emqx_ds_builtin_local_sup:start_top().
 
 %%================================================================================
 %% Internal exports

+ 5 - 11
apps/emqx_ds_builtin_local/src/emqx_ds_builtin_local_sup.erl

@@ -43,10 +43,13 @@
 %% API functions
 %%================================================================================
 
+-spec start_top() -> {ok, pid()}.
+start_top() ->
+    supervisor:start_link({local, ?top}, ?MODULE, ?top).
+
 -spec start_db(emqx_ds:db(), emqx_ds_builtin_local:db_opts()) ->
     supervisor:startchild_ret().
 start_db(DB, Opts) ->
-    ensure_top(),
     ChildSpec = #{
         id => DB,
         start => {?databases, start_db, [DB, Opts]},
@@ -81,7 +84,6 @@ stop_db(DB) ->
 %% Chidren are attached dynamically to this one.
 init(?top) ->
     %% Children:
-    MetricsWorker = emqx_ds_builtin_metrics:child_spec(),
     MetadataServer = #{
         id => metadata_server,
         start => {emqx_ds_builtin_local_meta, start_link, []},
@@ -103,7 +105,7 @@ init(?top) ->
         period => 1,
         auto_shutdown => never
     },
-    {ok, {SupFlags, [MetricsWorker, MetadataServer, DBsSup]}};
+    {ok, {SupFlags, [MetadataServer, DBsSup]}};
 init(?databases) ->
     %% Children are added dynamically:
     SupFlags = #{
@@ -117,17 +119,9 @@ init(?databases) ->
 %% Internal exports
 %%================================================================================
 
--spec start_top() -> {ok, pid()}.
-start_top() ->
-    supervisor:start_link({local, ?top}, ?MODULE, ?top).
-
 start_databases_sup() ->
     supervisor:start_link({local, ?databases}, ?MODULE, ?databases).
 
 %%================================================================================
 %% Internal functions
 %%================================================================================
-
-ensure_top() ->
-    {ok, _} = emqx_ds_sup:attach_backend(builtin_local, {?MODULE, start_top, []}),
-    ok.

+ 2 - 2
apps/emqx_ds_builtin_local/test/emqx_ds_builtin_local_SUITE.erl

@@ -335,12 +335,12 @@ end_per_suite(Config) ->
     ok.
 
 init_per_testcase(_TC, Config) ->
-    application:ensure_all_started(emqx_durable_storage),
+    application:ensure_all_started(emqx_ds_builtin_local),
     Config.
 
 end_per_testcase(_TC, _Config) ->
     snabbkaffe:stop(),
-    ok = application:stop(emqx_durable_storage),
+    ok = application:stop(emqx_ds_builtin_local),
     mria:stop(),
     _ = mnesia:delete_schema([node()]),
     ok.

+ 1 - 1
apps/emqx_ds_builtin_raft/src/emqx_ds_builtin_raft_app.erl

@@ -8,4 +8,4 @@
 
 start(_Type, _Args) ->
     emqx_ds:register_backend(builtin_raft, emqx_ds_replication_layer),
-    {ok, self()}.
+    emqx_ds_builtin_raft_sup:start_top().

+ 7 - 13
apps/emqx_ds_builtin_raft/src/emqx_ds_builtin_raft_sup.erl

@@ -10,14 +10,14 @@
 -behaviour(supervisor).
 
 %% API:
--export([start_db/2, stop_db/1]).
+-export([start_top/0, start_db/2, stop_db/1]).
 -export([set_gvar/3, get_gvar/3, clean_gvars/1]).
 
 %% behavior callbacks:
 -export([init/1]).
 
 %% internal exports:
--export([start_top/0, start_databases_sup/0]).
+-export([start_databases_sup/0]).
 
 -export_type([]).
 
@@ -38,10 +38,13 @@
 %% API functions
 %%================================================================================
 
+-spec start_top() -> {ok, pid()}.
+start_top() ->
+    supervisor:start_link({local, ?top}, ?MODULE, ?top).
+
 -spec start_db(emqx_ds:db(), emqx_ds_replication_layer:builtin_db_opts()) ->
     supervisor:startchild_ret().
 start_db(DB, Opts) ->
-    ensure_top(),
     ChildSpec = #{
         id => DB,
         start => {emqx_ds_builtin_raft_db_sup, start_db, [DB, Opts]},
@@ -96,7 +99,6 @@ clean_gvars(DB) ->
 %% Chidren are attached dynamically to this one.
 init(?top) ->
     %% Children:
-    MetricsWorker = emqx_ds_builtin_metrics:child_spec(),
     MetadataServer = #{
         id => metadata_server,
         start => {emqx_ds_replication_layer_meta, start_link, []},
@@ -119,7 +121,7 @@ init(?top) ->
         period => 1,
         auto_shutdown => never
     },
-    {ok, {SupFlags, [MetricsWorker, MetadataServer, DBsSup]}};
+    {ok, {SupFlags, [MetadataServer, DBsSup]}};
 init(?databases) ->
     %% Children are added dynamically:
     SupFlags = #{
@@ -133,17 +135,9 @@ init(?databases) ->
 %% Internal exports
 %%================================================================================
 
--spec start_top() -> {ok, pid()}.
-start_top() ->
-    supervisor:start_link({local, ?top}, ?MODULE, ?top).
-
 start_databases_sup() ->
     supervisor:start_link({local, ?databases}, ?MODULE, ?databases).
 
 %%================================================================================
 %% Internal functions
 %%================================================================================
-
-ensure_top() ->
-    {ok, _} = emqx_ds_sup:attach_backend(builtin_raft, {?MODULE, start_top, []}),
-    ok.

+ 2 - 23
apps/emqx_durable_storage/src/emqx_ds_sup.erl

@@ -18,7 +18,7 @@
 -behaviour(supervisor).
 
 %% API:
--export([start_link/0, attach_backend/2]).
+-export([start_link/0]).
 -export([register_db/2, unregister_db/1, which_dbs/0]).
 
 %% behaviour callbacks:
@@ -39,27 +39,6 @@
 start_link() ->
     supervisor:start_link({local, ?SUP}, ?MODULE, top).
 
-%% @doc Attach a child backend-specific supervisor to the top
-%% application supervisor, if not yet present
--spec attach_backend(_BackendId, {module(), atom(), list()}) ->
-    {ok, pid()} | {error, _}.
-attach_backend(Backend, Start) ->
-    Spec = #{
-        id => Backend,
-        start => Start,
-        significant => false,
-        shutdown => infinity,
-        type => supervisor
-    },
-    case supervisor:start_child(?SUP, Spec) of
-        {ok, Pid} ->
-            {ok, Pid};
-        {error, {already_started, Pid}} ->
-            {ok, Pid};
-        {error, Err} ->
-            {error, Err}
-    end.
-
 register_db(DB, Backend) ->
     ets:insert(?TAB, {DB, Backend}),
     ok.
@@ -77,7 +56,7 @@ which_dbs() ->
 
 init(top) ->
     _ = ets:new(?TAB, [public, set, named_table]),
-    Children = [],
+    Children = [emqx_ds_builtin_metrics:child_spec()],
     SupFlags = #{
         strategy => one_for_one,
         intensity => 10,

+ 1 - 1
changes/ce/breaking-13248.en.md

@@ -4,7 +4,7 @@
    It can't be used in a multi-node cluster.
    This backend is available in both open source and enterprise editions.
 - `builtin_raft`: A durable storage backend that uses Raft algorithm for replication.
-   This backend is available enterprise edition.
+   This backend is available only in the enterprise edition.
 
 The following Prometheus metrics have been renamed: