From 517e21c564d8b732abf434cb3ea98d5b3c516bf5 Mon Sep 17 00:00:00 2001 From: Michael Klishin Date: Wed, 1 Nov 2023 06:38:10 -0400 Subject: [PATCH] Type specs: maybe/1 => option/1 so that we don't have to think about quoting. 'maybe' is a keyword in Erlang 25+ and as of 26, it is an always-on language feature. Closes #401 --- src/ra.hrl | 2 +- src/ra_directory.erl | 8 ++++---- src/ra_log.erl | 10 +++++----- src/ra_log_segment.erl | 12 ++++++------ src/ra_log_wal.erl | 6 +++--- src/ra_server.erl | 10 +++++----- src/ra_server_proc.erl | 2 +- src/ra_snapshot.erl | 16 ++++++++-------- test/ra_fifo.erl | 10 +++++----- test/ra_fifo_client.erl | 6 +++--- test/ra_log_memory.erl | 10 +++++----- 11 files changed, 46 insertions(+), 46 deletions(-) diff --git a/src/ra.hrl b/src/ra.hrl index 8f1538cf..a965f980 100644 --- a/src/ra.hrl +++ b/src/ra.hrl @@ -4,7 +4,7 @@ %% %% Copyright (c) 2017-2022 VMware, Inc. or its affiliates. All rights reserved. %% --type 'maybe'(T) :: undefined | T. +-type option(T) :: undefined | T. %% %% Most of the records here are covered on Figure 2 diff --git a/src/ra_directory.erl b/src/ra_directory.erl index de841475..298949e1 100644 --- a/src/ra_directory.erl +++ b/src/ra_directory.erl @@ -63,7 +63,7 @@ deinit(#{directory := Name, _ = dets:close(NameRev), ok. --spec register_name(ra_system:names() | atom(), ra_uid(), pid(), 'maybe'(pid()), atom(), +-spec register_name(ra_system:names() | atom(), ra_uid(), pid(), option(pid()), atom(), ra_cluster_name()) -> ok. register_name(System, UId, Pid, ParentPid, ServerName, ClusterName) when is_atom(System) -> @@ -135,7 +135,7 @@ where_is_parent(#{directory := Dir}, UId) when is_binary(UId) -> [] -> undefined end. --spec name_of(atom() | ra_system:names(), ra_uid()) -> 'maybe'(atom()). +-spec name_of(atom() | ra_system:names(), ra_uid()) -> option(atom()). name_of(SystemOrNames, UId) -> Tbl = get_name(SystemOrNames), case ets:lookup(Tbl, UId) of @@ -144,7 +144,7 @@ name_of(SystemOrNames, UId) -> end. -spec cluster_name_of(ra_system:names() | atom(), ra_uid()) -> - 'maybe'(ra_cluster_name()). + option(ra_cluster_name()). cluster_name_of(SystemOrNames, UId) -> Tbl = get_name(SystemOrNames), case ets:lookup(Tbl, UId) of @@ -153,7 +153,7 @@ cluster_name_of(SystemOrNames, UId) -> end. --spec pid_of(atom() | ra_system:names(), ra_uid()) -> 'maybe'(pid()). +-spec pid_of(atom() | ra_system:names(), ra_uid()) -> option(pid()). pid_of(SystemOrNames, UId) -> case ets:lookup(get_name(SystemOrNames), UId) of [{_, Pid, _, _, _}] -> Pid; diff --git a/src/ra_log.erl b/src/ra_log.erl index 421546de..83ac369f 100644 --- a/src/ra_log.erl +++ b/src/ra_log.erl @@ -100,7 +100,7 @@ % if this is set a snapshot write is in progress for the % index specified cache = ra_log_cache:init() :: ra_log_cache:state(), - last_resend_time :: 'maybe'(integer()), + last_resend_time :: option(integer()), reader :: ra_log_reader:state(), readers = [] :: [pid()] }). @@ -546,7 +546,7 @@ next_index(#?MODULE{last_index = LastIdx}) -> LastIdx + 1. -spec fetch(ra_index(), state()) -> - {maybe(log_entry()), state()}. + {option(log_entry()), state()}. fetch(Idx, State0) -> case fold(Idx, Idx, fun(E, Acc) -> [E | Acc] end, [], State0) of {[], State} -> @@ -556,7 +556,7 @@ fetch(Idx, State0) -> end. -spec fetch_term(ra_index(), state()) -> - {'maybe'(ra_term()), state()}. + {option(ra_term()), state()}. fetch_term(Idx, #?MODULE{last_index = LastIdx, first_index = FirstIdx} = State0) when Idx < FirstIdx orelse Idx > LastIdx -> @@ -598,7 +598,7 @@ install_snapshot({SnapIdx, _} = IdxTerm, SnapState, Effs}. -spec recover_snapshot(State :: state()) -> - 'maybe'({ra_snapshot:meta(), term()}). + option({ra_snapshot:meta(), term()}). recover_snapshot(#?MODULE{snapshot_state = SnapState}) -> case ra_snapshot:recover(SnapState) of {ok, Meta, MacState} -> @@ -607,7 +607,7 @@ recover_snapshot(#?MODULE{snapshot_state = SnapState}) -> undefined end. --spec snapshot_index_term(State :: state()) -> 'maybe'(ra_idxterm()). +-spec snapshot_index_term(State :: state()) -> option(ra_idxterm()). snapshot_index_term(#?MODULE{snapshot_state = SS}) -> ra_snapshot:current(SS). diff --git a/src/ra_log_segment.erl b/src/ra_log_segment.erl index bb43277c..f211ad92 100644 --- a/src/ra_log_segment.erl +++ b/src/ra_log_segment.erl @@ -46,7 +46,7 @@ max_count = ?SEGMENT_MAX_ENTRIES :: non_neg_integer(), max_pending = ?SEGMENT_MAX_PENDING :: non_neg_integer(), filename :: file:filename_all(), - fd :: 'maybe'(file:io_device()), + fd :: option(file:io_device()), index_size :: pos_integer(), access_pattern :: sequential | random, mode = append :: read | append, @@ -59,8 +59,8 @@ data_start :: pos_integer(), data_offset :: pos_integer(), data_write_offset :: pos_integer(), - index = undefined :: maybe(ra_segment_index()), - range :: 'maybe'({ra_index(), ra_index()}), + index = undefined :: option(ra_segment_index()), + range :: option({ra_index(), ra_index()}), pending_data = [] :: iodata(), pending_index = [] :: iodata(), pending_count = 0 :: non_neg_integer(), @@ -331,7 +331,7 @@ map_get_(Key, Map) when is_map_key(Key, Map) -> map_get_(Key, _Map) -> exit({missing_key, Key}). --spec term_query(state(), Idx :: ra_index()) -> 'maybe'(ra_term()). +-spec term_query(state(), Idx :: ra_index()) -> option(ra_term()). term_query(#state{index = Index}, Idx) -> case Index of #{Idx := {Term, _, _, _}} -> @@ -367,7 +367,7 @@ fold0(Cfg, Cache0, Idx, FinalIdx, Index, Fun, AccFun, Acc0) -> Cfg#cfg.filename}) end. --spec range(state()) -> maybe({ra_index(), ra_index()}). +-spec range(state()) -> option({ra_index(), ra_index()}). range(#state{range = Range}) -> Range. @@ -379,7 +379,7 @@ max_count(#state{cfg = #cfg{max_count = Max}}) -> filename(#state{cfg = #cfg{filename = Fn}}) -> Fn. --spec segref(state()) -> 'maybe'(ra_log:segment_ref()). +-spec segref(state()) -> option(ra_log:segment_ref()). segref(#state{range = undefined}) -> undefined; segref(#state{range = {Start, End}, diff --git a/src/ra_log_wal.erl b/src/ra_log_wal.erl index 2b3936f8..095e5189 100644 --- a/src/ra_log_wal.erl +++ b/src/ra_log_wal.erl @@ -94,8 +94,8 @@ compress_mem_tables = false :: boolean() }). --record(wal, {fd :: 'maybe'(file:io_device()), - filename :: 'maybe'(file:filename()), +-record(wal, {fd :: option(file:io_device()), + filename :: option(file:filename()), writer_name_cache = {0, #{}} :: writer_name_cache(), max_size :: non_neg_integer(), entry_count = 0 :: non_neg_integer() @@ -117,7 +117,7 @@ % and the last index seen writers = #{} :: #{ra_uid() => {in_seq | out_of_seq, ra_index()}}, - batch :: 'maybe'(#batch{}) + batch :: option(#batch{}) }). -type state() :: #state{}. diff --git a/src/ra_server.erl b/src/ra_server.erl index 406728ac..6f42cfdd 100644 --- a/src/ra_server.erl +++ b/src/ra_server.erl @@ -64,14 +64,14 @@ -type ra_server_state() :: #{cfg := #cfg{}, - leader_id => 'maybe'(ra_server_id()), + leader_id => option(ra_server_id()), cluster := ra_cluster(), cluster_change_permitted := boolean(), cluster_index_term := ra_idxterm(), previous_cluster => {ra_index(), ra_term(), ra_cluster()}, current_term := ra_term(), log := term(), - voted_for => 'maybe'(ra_server_id()), % persistent + voted_for => option(ra_server_id()), % persistent votes => non_neg_integer(), membership => ra_membership(), commit_index := ra_index(), @@ -87,7 +87,7 @@ query_index := non_neg_integer(), queries_waiting_heartbeats := queue:queue({non_neg_integer(), consistent_query_ref()}), pending_consistent_queries := [consistent_query_ref()], - commit_latency => 'maybe'(non_neg_integer()) + commit_latency => option(non_neg_integer()) }. -type ra_state() :: leader | follower | candidate @@ -1498,7 +1498,7 @@ uid(#{cfg := #cfg{uid = UId}}) -> UId. -spec system_config(ra_server_state()) -> ra_system:config(). system_config(#{cfg := #cfg{system_config = SC}}) -> SC. --spec leader_id(ra_server_state()) -> 'maybe'(ra_server_id()). +-spec leader_id(ra_server_state()) -> option(ra_server_id()). leader_id(State) -> maps:get(leader_id, State, undefined). @@ -1506,7 +1506,7 @@ leader_id(State) -> clear_leader_id(State) -> State#{leader_id => undefined}. --spec current_term(ra_server_state()) -> 'maybe'(ra_term()). +-spec current_term(ra_server_state()) -> option(ra_term()). current_term(State) -> maps:get(current_term, State). diff --git a/src/ra_server_proc.erl b/src/ra_server_proc.erl index c985aa4a..bd8546b9 100644 --- a/src/ra_server_proc.erl +++ b/src/ra_server_proc.erl @@ -85,7 +85,7 @@ -type gen_statem_start_ret() :: {ok, pid()} | ignore | {error, term()}. %% ra_event types --type ra_event_reject_detail() :: {not_leader, Leader :: 'maybe'(ra_server_id()), +-type ra_event_reject_detail() :: {not_leader, Leader :: option(ra_server_id()), ra_server:command_correlation()}. -type ra_event_body() :: diff --git a/src/ra_snapshot.erl b/src/ra_snapshot.erl index fa4d5eaa..4687341b 100644 --- a/src/ra_snapshot.erl +++ b/src/ra_snapshot.erl @@ -60,9 +60,9 @@ %% snapshot subdirs are store below %% this as /snapshots/Term_Index directory :: file:filename(), - pending :: 'maybe'({pid(), ra_idxterm()}), - accepting :: 'maybe'(#accept{}), - current :: 'maybe'(ra_idxterm())}). + pending :: option({pid(), ra_idxterm()}), + accepting :: option(#accept{}), + current :: option(ra_idxterm())}). -define(ETSTBL, ra_log_snapshot_state). @@ -197,14 +197,14 @@ init_ets() -> _ = ets:new(?ETSTBL, TableFlags), ok. --spec current(state()) -> 'maybe'(ra_idxterm()). +-spec current(state()) -> option(ra_idxterm()). current(#?MODULE{current = Current}) -> Current. --spec pending(state()) -> 'maybe'({pid(), ra_idxterm()}). +-spec pending(state()) -> option({pid(), ra_idxterm()}). pending(#?MODULE{pending = Pending}) -> Pending. --spec accepting(state()) -> 'maybe'(ra_idxterm()). +-spec accepting(state()) -> option(ra_idxterm()). accepting(#?MODULE{accepting = undefined}) -> undefined; accepting(#?MODULE{accepting = #accept{idxterm = Accepting}}) -> @@ -213,7 +213,7 @@ accepting(#?MODULE{accepting = #accept{idxterm = Accepting}}) -> -spec directory(state()) -> file:filename(). directory(#?MODULE{directory = Dir}) -> Dir. --spec last_index_for(ra_uid()) -> 'maybe'(ra_index()). +-spec last_index_for(ra_uid()) -> option(ra_index()). last_index_for(UId) -> case ets:lookup(?ETSTBL, UId) of [] -> undefined; @@ -400,7 +400,7 @@ read_meta(Module, Location) -> Module:read_meta(Location). -spec current_snapshot_dir(state()) -> - 'maybe'(file:filename()). + option(file:filename()). current_snapshot_dir(#?MODULE{directory = Dir, current = {Idx, Term}}) -> make_snapshot_dir(Dir, Idx, Term); diff --git a/test/ra_fifo.erl b/test/ra_fifo.erl index c6b4c8bc..9a68dd0a 100644 --- a/test/ra_fifo.erl +++ b/test/ra_fifo.erl @@ -89,7 +89,7 @@ cancel. -type protocol() :: - {enqueue, Sender :: 'maybe'(pid()), MsgSeq :: 'maybe'(msg_seqno()), + {enqueue, Sender :: option(pid()), MsgSeq :: option(msg_seqno()), Msg :: raw_msg()} | {checkout, Spec :: checkout_spec(), Customer :: customer_id()} | {settle, MsgIds :: [msg_id()], Customer :: customer_id()} | @@ -160,10 +160,10 @@ % customers that require further service are queued here % needs to be part of snapshot service_queue = queue:new() :: queue:queue(customer_id()), - dead_letter_handler :: 'maybe'(applied_mfa()), - cancel_customer_handler :: 'maybe'(applied_mfa()), - become_leader_handler :: 'maybe'(applied_mfa()), - metrics_handler :: 'maybe'(applied_mfa()), + dead_letter_handler :: option(applied_mfa()), + cancel_customer_handler :: option(applied_mfa()), + become_leader_handler :: option(applied_mfa()), + metrics_handler :: option(applied_mfa()), prefix_msg_count = 0 :: non_neg_integer() }). diff --git a/test/ra_fifo_client.erl b/test/ra_fifo_client.erl index 675bb25c..0f516725 100644 --- a/test/ra_fifo_client.erl +++ b/test/ra_fifo_client.erl @@ -30,9 +30,9 @@ -record(state, {cluster_name :: ra_cluster_name(), servers = [] :: [ra_server_id()], - leader :: 'maybe'(ra_server_id()), + leader :: option(ra_server_id()), next_seq = 0 :: seq(), - last_applied :: 'maybe'(seq()), + last_applied :: option(seq()), next_enqueue_seq = 1 :: seq(), %% indicates that we've exceeded the soft limit slow = false :: boolean(), @@ -40,7 +40,7 @@ {[seq()], [seq()], [seq()]}}, soft_limit = ?SOFT_LIMIT :: non_neg_integer(), pending = #{} :: #{seq() => - {'maybe'(term()), ra_fifo:command()}}, + {option(term()), ra_fifo:command()}}, customer_deliveries = #{} :: #{ra_fifo:customer_tag() => seq()}, priority = normal :: normal | low, diff --git a/test/ra_log_memory.erl b/test/ra_log_memory.erl index bce65e44..0d777eef 100644 --- a/test/ra_log_memory.erl +++ b/test/ra_log_memory.erl @@ -46,7 +46,7 @@ last_written = {0, 0} :: ra_idxterm(), % only here to fake the async api of the file based one entries = #{0 => {0, undefined}} :: #{ra_term() => {ra_index(), term()}}, meta = #{} :: ra_log_memory_meta(), - snapshot :: 'maybe'(snapshot())}). + snapshot :: option(snapshot())}). -type ra_log_memory_state() :: #state{} | ra_log:state(). @@ -127,7 +127,7 @@ sparse_take(Idx, Log, Num, Max, Res) -> end. --spec last_index_term(ra_log_memory_state()) -> 'maybe'(ra_idxterm()). +-spec last_index_term(ra_log_memory_state()) -> option(ra_idxterm()). last_index_term(#state{last_index = LastIdx, entries = Log, snapshot = Snapshot}) -> @@ -184,7 +184,7 @@ next_index(#state{last_index = LastIdx}) -> LastIdx + 1. -spec fetch(ra_index(), ra_log_memory_state()) -> - {'maybe'(log_entry()), ra_log_memory_state()}. + {option(log_entry()), ra_log_memory_state()}. fetch(Idx, #state{entries = Log} = State) -> case Log of #{Idx := {T, D}} -> @@ -193,7 +193,7 @@ fetch(Idx, #state{entries = Log} = State) -> end. -spec fetch_term(ra_index(), ra_log_memory_state()) -> - {'maybe'(ra_term()), ra_log_memory_state()}. + {option(ra_term()), ra_log_memory_state()}. fetch_term(Idx, #state{entries = Log} = State) -> case Log of #{Idx := {T, _}} -> @@ -229,7 +229,7 @@ recover_snapshot(#state{snapshot = {Meta, Data}}) -> -spec read_meta(Key :: ra_log:ra_meta_key(), State :: ra_log_memory_state()) -> - 'maybe'(term()). + option(term()). read_meta(Key, #state{meta = Meta}) -> maps:get(Key, Meta, undefined).