Skip to content

Commit 1541c5b

Browse files
committed
MB-47766 Support for analytics settings manager
The analytics settings manager is patterned after the eventing settings manager and so far provides support for numReplicas. $ curl -s -u Administrator:asdasd localhost:9000/settings/analytics | jq { "numReplicas": 0 } $ curl -s -u Administrator:asdasd localhost:9000/settings/analytics \ -d "numReplicas=5" {"numReplicas":5} $ curl -s -u Administrator:asdasd localhost:9000/settings/analytics | jq { "numReplicas": 5 } An audit event for the modification is also provided. Change-Id: Ib2caf4b28fee1ff111e4126140cd435d04b2b8be Reviewed-on: http://review.couchbase.org/c/ns_server/+/159170 Well-Formed: Build Bot <[email protected]> Reviewed-by: Artem Stemkovski <[email protected]> Tested-by: Build Bot <[email protected]>
1 parent b91ca38 commit 1541c5b

File tree

8 files changed

+161
-4
lines changed

8 files changed

+161
-4
lines changed

etc/audit_descriptor.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,25 @@
13231323
"optional_fields" : {
13241324
"sessionid" : ""
13251325
}
1326+
},
1327+
{
1328+
"id" : 8267,
1329+
"name" : "modify analytics settings",
1330+
"description" : "Analytics service settings were modified",
1331+
"sync" : false,
1332+
"enabled" : true,
1333+
"filtering_permitted" : false,
1334+
"mandatory_fields" : {
1335+
"timestamp" : "",
1336+
"real_userid" : {"source" : "", "user" : ""},
1337+
"remote" : {"ip" : "", "port" : 1},
1338+
"local" : {"ip" : "", "port" : 1},
1339+
"settings" : ""
1340+
},
1341+
"optional_fields" : {
1342+
"sessionid" : ""
1343+
}
13261344
}
1345+
13271346
]
13281347
}

src/analytics_settings_manager.erl

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
%% @author Couchbase <[email protected]>
2+
%% @copyright 2021-Present Couchbase, Inc.
3+
%%
4+
%% Use of this software is governed by the Business Source License included
5+
%% in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
6+
%% in that file, in accordance with the Business Source License, use of this
7+
%% software will be governed by the Apache License, Version 2.0, included in
8+
%% the file licenses/APL2.txt.
9+
%%
10+
-module(analytics_settings_manager).
11+
12+
-include("ns_common.hrl").
13+
14+
-behavior(json_settings_manager).
15+
16+
-export([start_link/0,
17+
get/1,
18+
get_from_config/3,
19+
update/2,
20+
config_default/0]).
21+
22+
-export([cfg_key/0,
23+
is_enabled/0,
24+
known_settings/0,
25+
on_update/2]).
26+
27+
-import(json_settings_manager,
28+
[id_lens/1]).
29+
30+
-define(ANALYTICS_CONFIG_KEY, {metakv, <<"/analytics/settings/config">>}).
31+
32+
start_link() ->
33+
json_settings_manager:start_link(?MODULE).
34+
35+
get(Key) ->
36+
json_settings_manager:get(?MODULE, Key, []).
37+
38+
get_from_config(Config, Key, Default) ->
39+
json_settings_manager:get_from_config(?MODULE, Config, Key, Default).
40+
41+
cfg_key() ->
42+
?ANALYTICS_CONFIG_KEY.
43+
44+
is_enabled() ->
45+
cluster_compat_mode:is_cluster_NEO().
46+
47+
on_update(_Key, _Value) ->
48+
ok.
49+
50+
update(Key, Value) ->
51+
json_settings_manager:update(?MODULE, [{Key, Value}]).
52+
53+
config_default() ->
54+
{?ANALYTICS_CONFIG_KEY, json_settings_manager:build_settings_json(
55+
default_settings(), dict:new(), known_settings())}.
56+
57+
known_settings() ->
58+
[{generalSettings, general_settings_lens()}].
59+
60+
default_settings() ->
61+
[{generalSettings, general_settings_defaults()}].
62+
63+
general_settings() ->
64+
[{numReplicas, "analytics.settings.num_replicas", 0}].
65+
66+
general_settings_defaults() ->
67+
[{K, D} || {K, _, D} <- general_settings()].
68+
69+
general_settings_lens() ->
70+
json_settings_manager:props_lens(
71+
[{K, id_lens(list_to_binary(L))} || {K, L, _} <- general_settings()]).

src/menelaus_util.erl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
assert_is_enterprise/0,
6565
assert_is_65/0,
6666
assert_is_66/0,
67+
assert_is_NEO/0,
6768
strip_json_struct/1,
6869
choose_node_consistently/2,
6970
compute_sec_headers/0,
@@ -624,6 +625,9 @@ assert_is_65() ->
624625
assert_is_66() ->
625626
assert_cluster_version(fun cluster_compat_mode:is_cluster_66/0).
626627

628+
assert_is_NEO() ->
629+
assert_cluster_version(fun cluster_compat_mode:is_cluster_NEO/0).
630+
627631
assert_cluster_version(Fun) ->
628632
case Fun() of
629633
true ->

src/menelaus_web.erl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,9 @@ get_action(Req, {AppRoot, IsSSL, Plugins}, Path, PathTokens) ->
469469
fun menelaus_web_indexes:handle_index_status/1};
470470
["settings", "indexes"] ->
471471
{{[settings, indexes], read}, fun menelaus_web_indexes:handle_settings_get/1};
472+
["settings", "analytics"] ->
473+
{{[settings, analytics], read},
474+
fun menelaus_web_analytics:handle_settings_get/1};
472475
["diag"] ->
473476
{{[admin, diag], read}, fun diag_handler:handle_diag/1, []};
474477
["diag", "vbuckets"] ->
@@ -847,6 +850,9 @@ get_action(Req, {AppRoot, IsSSL, Plugins}, Path, PathTokens) ->
847850
fun menelaus_web_stats:handle_range_post/1, []};
848851
["settings", "indexes"] ->
849852
{{[settings, indexes], write}, fun menelaus_web_indexes:handle_settings_post/1};
853+
["settings", "analytics"] ->
854+
{{[settings, analytics], write},
855+
fun menelaus_web_analytics:handle_settings_post/1};
850856
["_cbauth"] ->
851857
{no_check, fun menelaus_cbauth:handle_cbauth_post/1};
852858
["_cbauth", "extractUserFromCert"] ->

src/menelaus_web_analytics.erl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
%% @author Couchbase <[email protected]>
2+
%% @copyright 2021-Present Couchbase, Inc.
3+
%% %% Use of this software is governed by the Business Source License included
4+
%% in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
5+
%% in that file, in accordance with the Business Source License, use of this
6+
%% software will be governed by the Apache License, Version 2.0, included in
7+
%% the file licenses/APL2.txt.
8+
%%
9+
-module(menelaus_web_analytics).
10+
11+
-include("ns_common.hrl").
12+
-include("cut.hrl").
13+
-export([handle_settings_get/1, handle_settings_post/1]).
14+
15+
handle_settings_get(Req) ->
16+
menelaus_util:assert_is_NEO(),
17+
Settings = get_settings(),
18+
menelaus_util:reply_json(Req, {Settings}).
19+
20+
get_settings() ->
21+
analytics_settings_manager:get(generalSettings).
22+
23+
settings_post_validators() ->
24+
[validator:has_params(_),
25+
validator:integer(numReplicas, 0, 16, _),
26+
validator:unsupported(_)].
27+
28+
update_settings(Key, Value) ->
29+
case analytics_settings_manager:update(Key, Value) of
30+
{ok, _} ->
31+
ok;
32+
retry_needed ->
33+
erlang:error(exceeded_retries)
34+
end.
35+
36+
handle_settings_post(Req) ->
37+
menelaus_util:assert_is_NEO(),
38+
validator:handle(
39+
fun (Values) ->
40+
case Values of
41+
[] ->
42+
ok;
43+
_ ->
44+
ok = update_settings(generalSettings, Values),
45+
ns_audit:modify_analytics_settings(Req, Values)
46+
end,
47+
menelaus_util:reply_json(Req, {get_settings()})
48+
end, Req, form, settings_post_validators()).

src/ns_audit.erl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
modify_index_settings/2,
6969
modify_query_curl_whitelist_setting/2,
7070
modify_query_settings/2,
71+
modify_analytics_settings/2,
7172
read_doc/3,
7273
mutate_doc/4,
7374
set_user_group/6,
@@ -367,7 +368,9 @@ code(auth_failure) ->
367368
code(rbac_info_retrieved) ->
368369
8265;
369370
code(admin_password_reset) ->
370-
8266.
371+
8266;
372+
code(modify_analytics_settings) ->
373+
8267.
371374

372375
to_binary({list, List}) ->
373376
[to_binary(A) || A <- List];
@@ -827,6 +830,9 @@ print_audit_records(Queue) ->
827830
modify_index_settings(Req, Settings) ->
828831
put(modify_index_settings, Req, [{settings, {prepare_list(Settings)}}]).
829832

833+
modify_analytics_settings(Req, Settings) ->
834+
put(modify_analytics_settings, Req, [{settings, {prepare_list(Settings)}}]).
835+
830836
modify_query_settings(Req, Settings) ->
831837
put(modify_query_settings, Req, [{settings, {prepare_list(Settings)}}]).
832838

src/ns_config_default.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ default() ->
142142
{{couchdb, max_parallel_indexers}, 4},
143143
{{couchdb, max_parallel_replica_indexers}, 2},
144144

145-
%% Default config for metakv index settings in minimum supported version,
145+
%% Default config for metakv settings in minimum supported version for
146+
%% the various services that use it.
146147
index_settings_manager:config_default(),
147-
%% Default config for metakv eventing settings in minimum supported version
148148
eventing_settings_manager:config_default(),
149-
%% Default config for metakv query settings in minimum supported version
150149
query_settings_manager:config_default(),
150+
analytics_settings_manager:config_default(),
151151

152152
%% {rest_creds, {User, {password, {Salt, Mac}}}}
153153
%% {rest_creds, null} means no login/password auth check.

src/ns_server_sup.erl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ child_specs() ->
151151
{eventing_settings_manager, {eventing_settings_manager, start_link, []},
152152
permanent, 1000, worker, [work_queue]},
153153

154+
{analytics_settings_manager, {analytics_settings_manager, start_link, []},
155+
permanent, 1000, worker, [analytics_settings_manager]},
156+
154157
{audit_events,
155158
{gen_event, start_link, [{local, audit_events}]},
156159
permanent, brutal_kill, worker, dynamic},

0 commit comments

Comments
 (0)