Skip to content

Commit b32ecc4

Browse files
committed
Merge remote-tracking branch 'couchbase/trinity' into phoenix
* couchbase/trinity: MB-67857: ns_node_disco should depend on ns_node_disco_events MB-67857: Fix flakiness of menelaus_cbauth cbauth_sync_t MB-66812: Use sha-256 hash instead of full VC for metakv rev Change-Id: I6dc031b97c12f8394f80d45c88ad51ce9a47421b
2 parents ad4b84e + 90f66d5 commit b32ecc4

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

apps/ns_server/src/menelaus_cbauth.erl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ setup_t() ->
673673
fake_chronicle_kv:setup(),
674674
%% Test setups return a map of pids for later shutdown in the teardown
675675
PidMap = mock_helpers:setup_mocks([json_rpc_events,
676-
ns_node_disco_events,
676+
ns_node_disco,
677677
user_storage_events,
678678
ssl_service_events,
679679
json_rpc_connection_sup,
@@ -716,7 +716,9 @@ start_fake_json_rpc_connection(Label) ->
716716
gen_event:notify(json_rpc_events,
717717
{started, Label, [internal,
718718
{heartbeat, ?HEARTBEAT_TIME_S}],
719-
self()}).
719+
self()}),
720+
%% Wait for initial notify to be handled, to avoid racy tests
721+
meck:wait(1, menelaus_cbauth_worker, notify, ['_', '_'], ?LONG_TIMEOUT).
720722

721723
cbauth_init_t() ->
722724
%% UpdateDB gets called once almost immediately
@@ -776,9 +778,6 @@ cbauth_stats_t() ->
776778
[] = stats().
777779

778780
cbauth_many_notify_t() ->
779-
%% Wait for initial notify to be handled
780-
meck:wait(1, menelaus_cbauth_worker, notify, ['_', '_'],
781-
?LONG_TIMEOUT),
782781
meck:wait(1, json_rpc_connection, perform_call,
783782
['_', "AuthCacheSvc.UpdateDB", '_', '_'], ?LONG_TIMEOUT),
784783
%% Ensure that the next perform_call doesn't immediately return

apps/ns_server/src/menelaus_metakv.erl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ handle_normal_get(Req, Key) ->
5252
true ->
5353
menelaus_util:reply_json(Req, [], 404);
5454
false ->
55-
Rev = base64:encode(erlang:term_to_binary(VC)),
55+
Rev = base64:encode(metakv:convert_vc_to_opaque_hash(VC)),
5656
Val = base64:encode(Val0),
5757
menelaus_util:reply_json(Req, {[{rev, Rev},
5858
{value, Val}]})
@@ -70,8 +70,7 @@ handle_mutate(Req, Key, Value, Params) ->
7070
missing
7171
end;
7272
XRev ->
73-
XRevB = list_to_binary(XRev),
74-
binary_to_term(XRevB)
73+
list_to_binary(XRev)
7574
end,
7675
Sensitive = proplists:get_value("sensitive", Params) =:= "true",
7776
case metakv:mutate(Key, Value,
@@ -142,7 +141,7 @@ handle_iterate(Req, Path, Continuous) ->
142141
output_kv(HTTPRes, {K, V}) ->
143142
write_chunk(HTTPRes, null, K, base64:encode(V), false);
144143
output_kv(HTTPRes, {K, V, VC, Sensitive}) ->
145-
Rev0 = base64:encode(erlang:term_to_binary(VC)),
144+
Rev0 = base64:encode(metakv:convert_vc_to_opaque_hash(VC)),
146145
{Rev, Value} = case V of
147146
?DELETED_MARKER ->
148147
{null, null};

apps/ns_server/src/metakv.erl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
ns_config_get/2,
2121
mutate/2, mutate/3,
2222
iterate_matching/1, iterate_matching/2, iterate_matching/3,
23-
check_continuous_allowed/1]).
23+
check_continuous_allowed/1,
24+
convert_vc_to_opaque_hash/1]).
2425

2526
%% Exported APIs
2627

@@ -180,12 +181,17 @@ ns_config_mutation(Key, Value, Params) ->
180181
false ->
181182
RV = ns_config:run_txn(
182183
fun (Cfg, SetFn) ->
183-
OldData = ns_config:search_with_vclock(Cfg, K),
184+
OldData = ns_config:search_with_vclock(Cfg,
185+
K),
184186
OldValue = get_old_value(OldData),
185187
NewValue = add_sensitive(Sensitive,
186188
OldValue, Value),
187-
OldVC = get_old_vclock(OldData),
188-
case Rev =:= OldVC of
189+
OldVCHash =
190+
case get_old_vclock(OldData) of
191+
missing -> missing;
192+
VC -> convert_vc_to_opaque_hash(VC)
193+
end,
194+
case Rev =:= OldVCHash of
189195
true ->
190196
{commit, SetFn(K, NewValue, Cfg)};
191197
false ->
@@ -351,3 +357,7 @@ ns_config_matching_kvs(Key, KVList) ->
351357
%% This will retain the behaviour as it existed
352358
%% before this code was moved here from menelaus_metakv.erl.
353359
[{K, V} || {K, V} <- KVList, Filter(K)].
360+
361+
%% Takes in a vector clock and returns an opaque SHA256 hash of that clock.
362+
convert_vc_to_opaque_hash(VC) ->
363+
crypto:hash(sha256, erlang:term_to_binary(VC)).

apps/ns_server/test/mock_helpers.erl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,14 @@ compat_mode_events(PidMap) ->
156156
PidMap#{?FUNCTION_NAME => CompatModeEventsPid}.
157157

158158
ns_node_disco(PidMap) ->
159-
setup_mocks([dist_manager], PidMap),
159+
PidMap0 = setup_mocks([dist_manager, ns_node_disco_events], PidMap),
160160
{ok, NsNodeDiscoPid} = ns_node_disco:start_link(),
161-
PidMap#{?FUNCTION_NAME => NsNodeDiscoPid}.
161+
PidMap0#{?FUNCTION_NAME => NsNodeDiscoPid}.
162162

163163
ns_node_disco_events(PidMap) ->
164-
PidMap0 = setup_mocks([ns_node_disco], PidMap),
165164
{ok, NSNodeDiscoEventsPid} = gen_event:start_link({local,
166165
?FUNCTION_NAME}),
167-
PidMap0#{?FUNCTION_NAME => NSNodeDiscoEventsPid}.
166+
PidMap#{?FUNCTION_NAME => NSNodeDiscoEventsPid}.
168167

169168
json_rpc_events(PidMap) ->
170169
{ok, JsonRpcEventsPid} = gen_event:start_link({local, ?FUNCTION_NAME}),

0 commit comments

Comments
 (0)