summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorILYA Khlopotov <iilyak@apache.org>2018-07-11 09:19:45 -0700
committerILYA Khlopotov <iilyak@apache.org>2018-08-07 13:22:11 -0700
commitcf65280466499d652cff1171a2039af49c5677e8 (patch)
tree60ff3647d66b82122d780bc2ba6daf4d1fed8d21
parent0fefc859eb9c18120064317da61a30adaeac5f92 (diff)
downloadcouchdb-cf65280466499d652cff1171a2039af49c5677e8.tar.gz
Add 'callback_module' provider
-rw-r--r--src/couch_epi/README.md3
-rw-r--r--src/couch_epi/src/couch_epi_data.erl5
-rw-r--r--src/couch_epi/src/couch_epi_sup.erl1
-rw-r--r--src/couch_epi/test/couch_epi_tests.erl57
4 files changed, 62 insertions, 4 deletions
diff --git a/src/couch_epi/README.md b/src/couch_epi/README.md
index 66421ceb9..445b7837a 100644
--- a/src/couch_epi/README.md
+++ b/src/couch_epi/README.md
@@ -39,7 +39,8 @@ could add an entry in its implementation of couch_epi_plugin behaviour:
{priv_file, "stats_descriptions.cfg"}, [{interval, 5000}]}
{{couch_stats, descriptions},
{file, "/tmp/extra_stats.cfg"}, [{interval, 5000}]},
- {{couch_stats, descriptions}, {module, my_stats}}
+ {{couch_stats, descriptions}, {static_module, my_stats}},
+ {{couch_stats, descriptions}, {callback_module, my_stats}}
].
When service provider wants to learn about all the installed config data for it to use
diff --git a/src/couch_epi/src/couch_epi_data.erl b/src/couch_epi/src/couch_epi_data.erl
index 3567edf23..2bb09f6cf 100644
--- a/src/couch_epi/src/couch_epi_data.erl
+++ b/src/couch_epi/src/couch_epi_data.erl
@@ -107,7 +107,10 @@ definitions({static_module, Module}) when is_atom(Module) ->
definitions({static_module, Modules}) ->
Data = lists:append([M:data() || M <- Modules]),
Hash = couch_epi_functions_gen:hash(Modules),
- {ok, Hash, Data}.
+ {ok, Hash, Data};
+definitions({callback_module, Module}) ->
+ Data = Module:data(),
+ {ok, erlang:phash2(Data), Data}.
hash_of_file(FilePath) ->
{ok, Data} = file:read_file(FilePath),
diff --git a/src/couch_epi/src/couch_epi_sup.erl b/src/couch_epi/src/couch_epi_sup.erl
index fbf5736be..509f5a1c2 100644
--- a/src/couch_epi/src/couch_epi_sup.erl
+++ b/src/couch_epi/src/couch_epi_sup.erl
@@ -122,6 +122,7 @@ modules(#couch_epi_spec{kind = services, value = Module}) ->
modules(#couch_epi_spec{kind = data_providers, value = Value}) ->
case Value of
{static_module, Module} -> [Module];
+ {callback_module, Module} -> [Module];
_ -> []
end;
modules(#couch_epi_spec{kind = data_subscriptions, behaviour = Module}) ->
diff --git a/src/couch_epi/test/couch_epi_tests.erl b/src/couch_epi/test/couch_epi_tests.erl
index 00e1a50d7..042753215 100644
--- a/src/couch_epi/test/couch_epi_tests.erl
+++ b/src/couch_epi/test/couch_epi_tests.erl
@@ -17,7 +17,7 @@
-define(DATA_FILE1, ?ABS_PATH("test/fixtures/app_data1.cfg")).
-define(DATA_FILE2, ?ABS_PATH("test/fixtures/app_data2.cfg")).
--export([notify_cb/4, save/3]).
+-export([notify_cb/4, save/3, get/2]).
-record(ctx, {file, handle, pid, kv, key, modules = []}).
@@ -83,6 +83,14 @@
].
").
+-define(DATA_MODULE3(Name, Kv), "
+ -export([data/0]).
+
+data() ->
+ {ok, Data} = couch_epi_tests:get('" ++ atom_to_list(Kv) ++ "', data),
+ Data.
+").
+
%% ------------------------------------------------------------------
%% couch_epi_plugin behaviour
%% ------------------------------------------------------------------
@@ -194,6 +202,33 @@ setup(static_data_module) ->
modules = [Handle, provider],
kv = KV,
pid = Pid};
+setup(callback_data_module) ->
+ error_logger:tty(false),
+
+ Key = {test_app, descriptions},
+
+ KV = start_state_storage(),
+ Value = [
+ {[complex, key, 1], [
+ {type, counter},
+ {desc, foo}
+ ]}
+ ],
+ save(KV, data, Value),
+
+ ok = generate_module(provider, ?DATA_MODULE3(provider, KV)),
+
+ ok = start_epi([{provider_epi, plugin_module([KV, {callback_module, provider}])}]),
+
+ Pid = whereis(couch_epi:get_handle(Key)),
+ Handle = couch_epi:get_handle(Key),
+
+ #ctx{
+ key = Key,
+ handle = Handle,
+ modules = [Handle, provider],
+ kv = KV,
+ pid = Pid};
setup(functions) ->
Key = my_service,
error_logger:tty(false),
@@ -245,6 +280,7 @@ epi_config_update_test_() ->
Cases = [
data_file,
static_data_module,
+ callback_data_module,
functions
],
{
@@ -264,7 +300,8 @@ epi_data_source_test_() ->
],
Cases = [
data_file,
- static_data_module
+ static_data_module,
+ callback_data_module
],
{
"epi data API tests",
@@ -306,6 +343,7 @@ epi_reload_test_() ->
Cases = [
data_file,
static_data_module,
+ callback_data_module,
functions
],
Funs = [
@@ -567,6 +605,19 @@ update_definitions(data_file, #ctx{file = File}) ->
ok;
update_definitions(static_data_module, #ctx{}) ->
ok = generate_module(provider, ?DATA_MODULE2(provider));
+update_definitions(callback_data_module, #ctx{kv = Kv}) ->
+ Value = [
+ {[complex, key, 2], [
+ {type, counter},
+ {desc, bar}
+ ]},
+ {[complex, key, 1], [
+ {type, counter},
+ {desc, updated_foo}
+ ]}
+ ],
+ save(Kv, data, Value),
+ ok;
update_definitions(functions, #ctx{}) ->
ok = generate_module(provider1, ?MODULE2(provider1)).
@@ -597,6 +648,8 @@ save(Kv, Key, Value) ->
call(Kv, {set, Key, Value}).
get(#ctx{kv = Kv}, Key) ->
+ call(Kv, {get, Key});
+get(Kv, Key) ->
call(Kv, {get, Key}).
call(Server, Msg) ->