feat(ds): Add an API for DB-global variables
This commit is contained in:
parent
86d45522e3
commit
8ac9700aab
|
@ -118,6 +118,7 @@ which_dbs() ->
|
||||||
init({#?db_sup{db = DB}, DefaultOpts}) ->
|
init({#?db_sup{db = DB}, DefaultOpts}) ->
|
||||||
%% Spec for the top-level supervisor for the database:
|
%% Spec for the top-level supervisor for the database:
|
||||||
logger:notice("Starting DS DB ~p", [DB]),
|
logger:notice("Starting DS DB ~p", [DB]),
|
||||||
|
emqx_ds_builtin_sup:clean_gvars(DB),
|
||||||
emqx_ds_builtin_metrics:init_for_db(DB),
|
emqx_ds_builtin_metrics:init_for_db(DB),
|
||||||
Opts = emqx_ds_replication_layer_meta:open_db(DB, DefaultOpts),
|
Opts = emqx_ds_replication_layer_meta:open_db(DB, DefaultOpts),
|
||||||
ok = start_ra_system(DB, Opts),
|
ok = start_ra_system(DB, Opts),
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
|
|
||||||
%% API:
|
%% API:
|
||||||
-export([start_db/2, stop_db/1]).
|
-export([start_db/2, stop_db/1]).
|
||||||
|
-export([set_gvar/3, get_gvar/3, clean_gvars/1]).
|
||||||
|
|
||||||
%% behavior callbacks:
|
%% behavior callbacks:
|
||||||
-export([init/1]).
|
-export([init/1]).
|
||||||
|
@ -39,6 +40,13 @@
|
||||||
-define(top, ?MODULE).
|
-define(top, ?MODULE).
|
||||||
-define(databases, emqx_ds_builtin_databases_sup).
|
-define(databases, emqx_ds_builtin_databases_sup).
|
||||||
|
|
||||||
|
-define(gvar_tab, emqx_ds_builtin_gvar).
|
||||||
|
|
||||||
|
-record(gvar, {
|
||||||
|
k :: {emqx_ds:db(), _Key},
|
||||||
|
v :: _Value
|
||||||
|
}).
|
||||||
|
|
||||||
%%================================================================================
|
%%================================================================================
|
||||||
%% API functions
|
%% API functions
|
||||||
%%================================================================================
|
%%================================================================================
|
||||||
|
@ -61,11 +69,30 @@ stop_db(DB) ->
|
||||||
Pid when is_pid(Pid) ->
|
Pid when is_pid(Pid) ->
|
||||||
_ = supervisor:terminate_child(?databases, DB),
|
_ = supervisor:terminate_child(?databases, DB),
|
||||||
_ = supervisor:delete_child(?databases, DB),
|
_ = supervisor:delete_child(?databases, DB),
|
||||||
ok;
|
clean_gvars(DB);
|
||||||
undefined ->
|
undefined ->
|
||||||
ok
|
ok
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%% @doc Set a DB-global variable. Please don't abuse this API.
|
||||||
|
-spec set_gvar(emqx_ds:db(), _Key, _Val) -> ok.
|
||||||
|
set_gvar(DB, Key, Val) ->
|
||||||
|
ets:insert(?gvar_tab, #gvar{k = {DB, Key}, v = Val}).
|
||||||
|
|
||||||
|
-spec get_gvar(emqx_ds:db(), _Key, Val) -> Val.
|
||||||
|
get_gvar(DB, Key, Default) ->
|
||||||
|
case ets:lookup(?gvar_tab, {DB, Key}) of
|
||||||
|
[#gvar{v = Val}] ->
|
||||||
|
Val;
|
||||||
|
[] ->
|
||||||
|
Default
|
||||||
|
end.
|
||||||
|
|
||||||
|
-spec clean_gvars(emqx_ds:db()) -> ok.
|
||||||
|
clean_gvars(DB) ->
|
||||||
|
ets:match_delete(?gvar_tab, #gvar{k = {DB, '_'}, _ = '_'}),
|
||||||
|
ok.
|
||||||
|
|
||||||
%%================================================================================
|
%%================================================================================
|
||||||
%% behavior callbacks
|
%% behavior callbacks
|
||||||
%%================================================================================
|
%%================================================================================
|
||||||
|
@ -96,6 +123,7 @@ init(?top) ->
|
||||||
type => supervisor,
|
type => supervisor,
|
||||||
shutdown => infinity
|
shutdown => infinity
|
||||||
},
|
},
|
||||||
|
ets:new(?gvar_tab, [named_table, set, public, {keypos, #gvar.k}, {read_concurrency, true}]),
|
||||||
%%
|
%%
|
||||||
SupFlags = #{
|
SupFlags = #{
|
||||||
strategy => one_for_all,
|
strategy => one_for_all,
|
||||||
|
|
|
@ -363,6 +363,7 @@ shard_of_message(DB, #message{from = From, topic = Topic}, SerializeBy) ->
|
||||||
end,
|
end,
|
||||||
integer_to_binary(Hash).
|
integer_to_binary(Hash).
|
||||||
|
|
||||||
|
-spec foreach_shard(emqx_ds:db(), fun((shard_id()) -> _)) -> ok.
|
||||||
foreach_shard(DB, Fun) ->
|
foreach_shard(DB, Fun) ->
|
||||||
lists:foreach(Fun, list_shards(DB)).
|
lists:foreach(Fun, list_shards(DB)).
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue