Packages
kafka_protocol
4.3.3
4.3.4
4.3.3
4.3.2
4.3.1
4.3.0
4.2.9
4.2.8
4.2.7
4.2.6
4.2.5
4.2.4
4.2.3
4.2.2
4.2.1
4.2.0
4.1.10
4.1.9
4.1.8
4.1.7
4.1.6
4.1.5
4.1.4
4.1.3
4.1.2
4.1.1
4.1.0
4.0.3
4.0.2
4.0.1
3.0.1
3.0.0
2.4.1
2.3.6
2.3.5
2.3.4
2.3.3
2.3.2
2.3.1
2.3.0
2.2.9
2.2.8
2.2.7
2.2.6
2.2.5
2.2.4
2.2.3
2.2.2
2.2.1
2.2.0
2.1.2
2.1.1
2.1.0
2.0.1
2.0.0
1.1.3
1.1.2
1.1.1
1.1.0
1.0.0
0.9.2
0.9.1
0.9.0
0.8.0
0.7.8
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.0
0.4.0
0.3.2
0.3.1
0.2.3
Kafka protocol library for Erlang/Elixir
Current section
Files
Jump to
Current section
Files
src/kpro_api_vsn.erl
%%% Copyright (c) 2018-2021, Klarna Bank AB (publ)
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%% http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%
%% Supported versions of THIS lib
-module(kpro_api_vsn).
-export([range/1, kafka_09_range/1, intersect/1, intersect/2]).
-export_type([range/0]).
-type range() :: {kpro:vsn(), kpro:vsn()}.
-define(undef, undefined).
%% @doc Return supported version range of the given API.
%%
%% Majority of the APIs are supported from version 0 up to the
%% latest version when the bnf files are re-generated.
%% With two exceptions.
%%
%% 1. Version 0-1 for offset_commit are not supported:
%% version 0: Kafka commits offsets to zookeeper
%% version 1: Thre is a lack of commit retention.
%%
%% 2. offset_fetch version 0 is not supported:
%% Version 0: Kafka fetches offsets from zookeeper.
-spec range(kpro:api()) -> false | range().
range(offset_commit) -> {2, 2};
range(offset_fetch) -> {1, 2};
range(API) -> kpro_schema:vsn_range(API).
-spec kafka_09_range(kpro:api()) -> false | range().
kafka_09_range(produce) -> {0, 0};
kafka_09_range(fetch) -> {0, 0};
kafka_09_range(list_offsets) -> {0, 0};
kafka_09_range(metadata) -> {0, 0};
kafka_09_range(offset_commit) -> {2, 2};
kafka_09_range(offset_fetch) -> {1, 1};
kafka_09_range(find_coordinator) -> {0, 0};
kafka_09_range(join_group) -> {0, 0};
kafka_09_range(heartbeat) -> {0, 0};
kafka_09_range(leave_group) -> {0, 0};
kafka_09_range(sync_group) -> {0, 0};
kafka_09_range(describe_groups) -> {0, 0};
kafka_09_range(list_groups) -> {0, 0};
kafka_09_range(_) -> false.
%% @private Returns the intersection of two version ranges.
%% An error is raised if there is no intersection.
-spec intersect(atom(), false | range(), false | range()) -> false | range().
intersect(_API, Unknown = false, _) -> Unknown;
intersect(API, {Min0, Max0} = Supported, {Min1, Max1} = Received) ->
{Min2, Max2} = fix_range(API, Min1, Max1),
Min = max(Min0, Min2),
Max = min(Max0, Max2),
case Min > Max of
true -> erlang:error({no_intersection, Supported, Received});
false -> {Min, Max}
end.
%% Special adjustment for received API range.
%% - produce: Minimal version is in fact 3, but Kafka may respond 0.
%% - fetch: Minimal version is in fact 4, but Kafka may respond 0.
fix_range(produce, Min, Max) ->
case Max >= 8 of
true ->
{max(Min, 3), Max};
false ->
{Min, Max}
end;
fix_range(fetch, Min, Max) ->
case Max >= 11 of
true ->
{max(Min, 4), Max};
false ->
{Min, Max}
end;
fix_range(_API, Min, Max) ->
{Min, Max}.
%% @doc Return the intersection of supported version ranges and received version ranges.
-spec intersect(?undef | list()) -> #{kpro:api() => range()}.
intersect(?undef) ->
%% kpro_connection is configured not to query api versions (kafka-0.9)
%% always use minimum supported version in this case
lists:foldl(
fun(API, Acc) ->
case kpro_api_vsn:kafka_09_range(API) of
false -> Acc;
{Min, _Max} -> Acc#{API => {Min, Min}}
end
end, #{}, kpro_schema:all_apis());
intersect(ReceivedVsns) ->
maps:fold(
fun(API, {Min, Max}, Acc) ->
case intersect(API, {Min, Max}) of
false -> Acc;
Intersection -> Acc#{API => Intersection}
end
end, #{}, ReceivedVsns).
%% @doc Intersect received api version range with supported range.
-spec intersect(kpro:api(), range()) -> false | range().
intersect(API, Received) ->
Supported = kpro_api_vsn:range(API),
try
intersect(API, Supported, Received)
catch
error : {no_intersection, _, _} ->
Reason = #{reason => incompatible_version_ranges,
supported => Supported,
received => Received,
api => API},
erlang:error(Reason)
end.
%%%_* Emacs ====================================================================
%%% Local Variables:
%%% allout-layout: t
%%% erlang-indent-level: 2
%%% End: