Skip to content

Commit 3d4b55c

Browse files
committed
khepri_tx: Export the transaction API version
[Why] The transaction code is compiled on one Erlang node with a specific version of Khepri. However, it is executed on all members of the Khepri cluster. Some Erlang nodes might use another version of Khepri, newer or older, and the transaction API may differ there. For instance in Khepri 0.17.x, the return values of the `khepri_tx_adv` functions changed. The transaction code will have to handle voth versions of the API to work correctly. Thus it can use this function to adapt its behaviour. [How] The first transaction version is set to 1 and is exported as a public API. A transaction use can use it it determine how to use the `khepri_tx` and `khepri_tx_adv` modules and what to expect in terms of behaviour and return values.
1 parent ebbd7eb commit 3d4b55c

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

src/khepri_tx.erl

+32-2
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,13 @@
8686
clear_many_payloads/1, clear_many_payloads/2,
8787

8888
abort/1,
89-
is_transaction/0]).
89+
is_transaction/0,
90+
api_version/0]).
9091

9192
-compile({no_auto_import, [get/1, put/2, erase/1]}).
9293

94+
-define(TX_API_VERSION, 1).
95+
9396
%% FIXME: Dialyzer complains about several functions with "optional" arguments
9497
%% (but not all). I believe the specs are correct, but can't figure out how to
9598
%% please Dialyzer. So for now, let's disable this specific check for the
@@ -106,9 +109,13 @@
106109
-type tx_abort() :: khepri:error(any()).
107110
%% Return value after a transaction function aborted.
108111

112+
-type api_version() :: pos_integer().
113+
%% Version of the transaction API.
114+
109115
-export_type([tx_fun/0,
110116
tx_fun_result/0,
111-
tx_abort/0]).
117+
tx_abort/0,
118+
api_version/0]).
112119

113120
%% -------------------------------------------------------------------
114121
%% is_empty().
@@ -1009,3 +1016,26 @@ is_transaction() ->
10091016
{_State, _SideEffects} -> true;
10101017
_ -> false
10111018
end.
1019+
1020+
%% -------------------------------------------------------------------
1021+
%% api_version().
1022+
%% -------------------------------------------------------------------
1023+
1024+
-spec api_version() -> khepri_tx:api_version().
1025+
%% @doc Returns the version of the transaction API for the Khepri instance
1026+
%% that execute the transaction.
1027+
%%
1028+
%% The transaction code is compiled on one Erlang node with a specific version
1029+
%% of Khepri. However, it is executed on all members of the Khepri cluster.
1030+
%% Some Erlang nodes might use another version of Khepri, newer or older, and
1031+
%% the transaction API may differ.
1032+
%%
1033+
%% For instance in Khepri 0.17.x, the return values of the {@link
1034+
%% khepri_tx_adv} functions changed. The transaction code will have to handle
1035+
%% voth versions of the API to work correctly. Thus it can use this function
1036+
%% to adapt its behaviour.
1037+
%%
1038+
%% @returns the version of the Khepri transaction API.
1039+
1040+
api_version() ->
1041+
?TX_API_VERSION.

src/khepri_tx_adv.erl

+1
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ is_remote_call_valid(khepri_tx, clear_payload, _) -> true;
751751
is_remote_call_valid(khepri_tx, clear_many_payloads, _) -> true;
752752
is_remote_call_valid(khepri_tx, abort, _) -> true;
753753
is_remote_call_valid(khepri_tx, is_transaction, _) -> true;
754+
is_remote_call_valid(khepri_tx, api_version, _) -> true;
754755

755756
is_remote_call_valid(khepri_tx_adv, get, _) -> true;
756757
is_remote_call_valid(khepri_tx_adv, get_many, _) -> true;

test/cluster_SUITE.erl

+3
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,9 @@ can_use_default_store_on_single_node(_Config) ->
17211721
{error, ?khepri_error(node_not_found, _)},
17221722
khepri:get([foo])),
17231723

1724+
?assert(is_integer(khepri_tx:api_version())),
1725+
?assert(khepri_tx:api_version() > 0),
1726+
17241727
?assertEqual(ok, khepri:stop()),
17251728
?assertEqual(ok, application:stop(khepri)),
17261729
?assertEqual(ok, application:stop(ra)),

test/tx_funs.erl

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ allowed_khepri_tx_api_test() ->
5252
_ = khepri_tx:has_data([foo]),
5353
_ = khepri_tx:delete([foo]),
5454
_ = khepri_tx:abort(error),
55-
_ = khepri_tx:is_transaction()
55+
_ = khepri_tx:is_transaction(),
56+
_ = khepri_tx:api_version()
5657
end).
5758

5859
denied_khepri_tx_adv_run_4_test() ->

0 commit comments

Comments
 (0)