A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(gateway): keep the session start queue alive across failures
Hampus79425b5fe6ad
diff --git a/fluxer_gateway/src/gateway/gateway_concurrency.erl b/fluxer_gateway/src/gateway/gateway_concurrency.erlindex ee0fbf145..e7ede6e20 100644--- a/fluxer_gateway/src/gateway/gateway_concurrency.erl+++ b/fluxer_gateway/src/gateway/gateway_concurrency.erl@@ -7,6 +7,7 @@-export([start_link/0,request_session_start/1,+ cancel_session_start/1,release_session_start/0,try_acquire_guild_start/0,release_guild_start/0,@@ -20,9 +21,11 @@-type state() :: #{counters := counters:counters_ref(),- session_queue := queue:queue({pid(), term()}),- session_holders := #{pid() => non_neg_integer()},- monitors := #{pid() => reference()}+ session_queue => queue:queue({pid(), term()}),+ session_queued => non_neg_integer(),+ session_holders => #{pid() => pos_integer()},+ session_held => non_neg_integer(),+ monitors => #{pid() => reference()}}.-spec start_link() -> {ok, pid()} | {error, term()}.@@ -37,6 +40,10 @@ start_link() ->request_session_start(Tag) ->gen_server:cast(?MODULE, {request_session_start, self(), Tag}).+-spec cancel_session_start(term()) -> ok.+cancel_session_start(Tag) ->+ gen_server:cast(?MODULE, {cancel_session_start, self(), Tag}).+-spec release_session_start() -> ok.release_session_start() ->gen_server:cast(?MODULE, {release_session_start, self()}).@@ -102,7 +109,9 @@ init([]) ->{ok, #{counters => Ref,session_queue => queue:new(),+ session_queued => 0,session_holders => #{},+ session_held => 0,monitors => #{}}}.@@ -115,35 +124,33 @@ handle_call(_Request, _From, State) ->{reply, ok, State}.-spec handle_cast(term(), state()) -> {noreply, state()}.-handle_cast({request_session_start, Pid, Tag}, #{session_queue := Queue} = State) when- is_pid(Pid)-->+handle_cast({request_session_start, Pid, Tag}, State) when is_pid(Pid) ->Monitored = ensure_monitor(Pid, State),- {noreply, admit_session_starts(Monitored#{session_queue := queue:in({Pid, Tag}, Queue)})};-handle_cast({release_session_start, Pid}, #{session_holders := Holders} = State) when- is_pid(Pid)-->- case maps:get(Pid, Holders, 0) of- Held when Held > 0 ->- Released = release_held(Pid, 1, State),- {noreply, admit_session_starts(Released)};- _ ->- {noreply, State}+ Queue = maps:get(session_queue, Monitored, queue:new()),+ Queued = Monitored#{+ session_queue => queue:in({Pid, Tag}, Queue),+ session_queued => maps:get(session_queued, Monitored, 0) + 1+ },+ {noreply, admit_session_starts(Queued)};+handle_cast({cancel_session_start, Pid, Tag}, State) when is_pid(Pid) ->+ {noreply, drop_queued(fun(Entry) -> Entry =/= {Pid, Tag} end, State)};+handle_cast({release_session_start, Pid}, State) when is_pid(Pid) ->+ case maps:is_key(Pid, maps:get(session_holders, State, #{})) of+ true -> {noreply, admit_session_starts(release_held(Pid, 1, State))};+ false -> {noreply, State}end;handle_cast(_Msg, State) ->{noreply, State}.-spec handle_info(term(), state()) -> {noreply, state()}.handle_info({'DOWN', MonitorRef, process, Pid, _Reason}, State) when is_pid(Pid) ->- #{session_holders := Holders, session_queue := Queue, monitors := Monitors} = State,+ Monitors = maps:get(monitors, State, #{}),case maps:get(Pid, Monitors, undefined) ofMonitorRef ->- Released = release_held(Pid, maps:get(Pid, Holders, 0), State),- Remaining = queue:filter(fun({QueuedPid, _Tag}) -> QueuedPid =/= Pid end, Queue),- {noreply,- admit_session_starts(Released#{- session_queue := Remaining, monitors := maps:remove(Pid, Monitors)- })};+ Held = maps:get(Pid, maps:get(session_holders, State, #{}), 0),+ Released = release_held(Pid, Held, State#{monitors => maps:remove(Pid, Monitors)}),+ Dropped = drop_queued(fun({QueuedPid, _Tag}) -> QueuedPid =/= Pid end, Released),+ {noreply, admit_session_starts(Dropped)};_ ->{noreply, State}end;@@ -151,38 +158,54 @@ handle_info(_Info, State) ->{noreply, State}.-spec ensure_monitor(pid(), state()) -> state().-ensure_monitor(Pid, #{monitors := Monitors} = State) ->+ensure_monitor(Pid, State) ->+ Monitors = maps:get(monitors, State, #{}),case maps:is_key(Pid, Monitors) oftrue -> State;- false -> State#{monitors := Monitors#{Pid => erlang:monitor(process, Pid)}}+ false -> State#{monitors => Monitors#{Pid => erlang:monitor(process, Pid)}}end.+-spec drop_queued(fun(({pid(), term()}) -> boolean()), state()) -> state().+drop_queued(Keep, State) ->+ Remaining = queue:filter(Keep, maps:get(session_queue, State, queue:new())),+ State#{session_queue => Remaining, session_queued => queue:len(Remaining)}.+-spec release_held(pid(), non_neg_integer(), state()) -> state().release_held(_Pid, 0, State) ->State;-release_held(Pid, Count, #{counters := Ref, session_holders := Holders} = State) ->- counters:sub(Ref, ?SESSION_START_IDX, Count),- case maps:get(Pid, Holders, 0) - Count of- Left when Left > 0 -> State#{session_holders := Holders#{Pid => Left}};- _ -> State#{session_holders := maps:remove(Pid, Holders)}- end.+release_held(Pid, Count, State) ->+ Holders = maps:get(session_holders, State, #{}),+ NewHolders =+ case maps:get(Pid, Holders, 0) - Count of+ Left when Left > 0 -> Holders#{Pid => Left};+ _ -> maps:remove(Pid, Holders)+ end,+ set_held(max(0, maps:get(session_held, State, 0) - Count), State#{+ session_holders => NewHolders+ }).++-spec set_held(non_neg_integer(), state()) -> state().+set_held(Held, #{counters := Ref} = State) ->+ counters:put(Ref, ?SESSION_START_IDX, Held),+ State#{session_held => Held}.-spec admit_session_starts(state()) -> state().admit_session_starts(State) ->admit_session_starts(gateway_rollout_config:max_concurrent_session_starts(), State).-spec admit_session_starts(integer(), state()) -> state().-admit_session_starts(Limit, #{counters := Ref, session_queue := Queue} = State) ->- case counters:get(Ref, ?SESSION_START_IDX) < Limit andalso queue:out(Queue) of+admit_session_starts(Limit, State) ->+ Held = maps:get(session_held, State, 0),+ case Held < Limit andalso queue:out(maps:get(session_queue, State, queue:new())) of{{value, {Pid, Tag}}, Rest} ->Pid ! {session_start_admitted, Tag},- counters:add(Ref, ?SESSION_START_IDX, 1),- Holders = maps:get(session_holders, State),+ Holders = maps:get(session_holders, State, #{}),Admitted = State#{- session_queue := Rest,- session_holders := Holders#{Pid => maps:get(Pid, Holders, 0) + 1}+ session_queue => Rest,+ session_queued => max(0, maps:get(session_queued, State, 0) - 1),+ session_holders => Holders#{Pid => maps:get(Pid, Holders, 0) + 1}},- admit_session_starts(Limit, Admitted);+ admit_session_starts(Limit, set_held(Held + 1, Admitted));_ ->Stateend.diff --git a/fluxer_gateway/src/session/session_manager_shard.erl b/fluxer_gateway/src/session/session_manager_shard.erlindex 9bdfd4b63..3470ab794 100644--- a/fluxer_gateway/src/session/session_manager_shard.erl+++ b/fluxer_gateway/src/session/session_manager_shard.erl@@ -54,6 +54,7 @@worker_token := reference(),timeout_ref := reference(),queued_at := integer(),+ socket_ref => reference(),slot_held => boolean()}.@@ -184,6 +185,14 @@ handle_info({identify_fetch_result, SessionId, WorkerToken, FetchResult}, State));handle_info({identify_fetch_result, SessionId, FetchResult}, State) when is_binary(SessionId) ->session_manager_shard_lifecycle:complete_identify_fetch(SessionId, FetchResult, State);+handle_info({identify_queue_timeout, SessionId, WorkerToken}, State) when+ is_binary(SessionId), is_reference(WorkerToken)+->+ session_manager_shard_lifecycle:handle_identify_queue_timeout(+ SessionId, WorkerToken, State+ );+handle_info(resubmit_session_starts, State) ->+ session_manager_shard_lifecycle:resubmit_session_starts(State);handle_info({identify_fetch_timeout, SessionId, WorkerToken}, State) whenis_binary(SessionId), is_reference(WorkerToken)->@@ -199,7 +208,7 @@ handle_info({'DOWN', Ref, process, Pid, Reason}, State) when is_reference(Ref),SessionId, Ref, Reason, StateWithoutWorker);error ->- {noreply, session_manager_shard_session_index:cleanup_down(Ref, Pid, State)}+ session_manager_shard_lifecycle:handle_other_down(Ref, Pid, State)end;handle_info({'EXIT', Pid, _Reason}, State) when is_pid(Pid) ->{noreply, State};diff --git a/fluxer_gateway/src/session/session_manager_shard_lifecycle.erl b/fluxer_gateway/src/session/session_manager_shard_lifecycle.erlindex fc6e9e383..ec935d6af 100644--- a/fluxer_gateway/src/session/session_manager_shard_lifecycle.erl+++ b/fluxer_gateway/src/session/session_manager_shard_lifecycle.erl@@ -8,6 +8,9 @@start_session_process/4,start_identify_fetch/5,admit_identify_fetch/3,+ handle_identify_queue_timeout/3,+ handle_other_down/3,+ resubmit_session_starts/1,complete_identify_fetch/3, complete_identify_fetch/4,handle_identify_fetch_timeout/3,maybe_fail_pending_identify/4,@@ -18,7 +21,9 @@-export_type([session_id/0, session_ref/0, state/0, pending_identify/0, start_reply/0]).--define(IDENTIFY_FETCH_TIMEOUT_MS, 130000).+-define(IDENTIFY_QUEUE_TIMEOUT_MS, 60000).+-define(IDENTIFY_FETCH_TIMEOUT_MS, 70000).+-define(RESUBMIT_RETRY_MS, 100).-type session_id() :: binary().-type session_ref() :: {pid(), reference()}.@@ -37,6 +42,7 @@worker_token := reference(),timeout_ref := reference(),queued_at := integer(),+ socket_ref => reference(),slot_held => boolean()}.-type start_reply() ::@@ -132,12 +138,15 @@ handle_existing_pending() ->case maps:get(request, PendingIdentify) =:= Request oftrue ->- UpdatedPending = PendingIdentify#{+ {Watched, WatchedState} = rewatch_socket(+ SessionId, SocketPid, PendingIdentify, State+ ),+ UpdatedPending = Watched#{request => Request,froms => [From | maps:get(froms, PendingIdentify, [])]},NewPending = PendingIdentifies#{SessionId := UpdatedPending},- {noreply, State#{pending_identifies := NewPending}};+ {noreply, WatchedState#{pending_identifies := NewPending}};false ->supersede_identify_fetch(Request, SocketPid, From, State, SessionId, PendingIdentify@@ -241,19 +250,9 @@ start_identify_fetch(Request, SocketPid, SessionId, From, State) ->pending_identify()) -> {noreply, state()}.supersede_identify_fetch(Request, SocketPid, From, State, SessionId, PendingIdentify) ->- cancel_identify_timeout(PendingIdentify),- State1 = kill_identify_worker(- maps:get(worker_ref, PendingIdentify), PendingIdentify, State- ),- RemainingPending = maps:remove(SessionId, maps:get(pending_identifies, State1)),+ State1 = drop_pending(SessionId, PendingIdentify, State),Waiters = [From | maps:get(froms, PendingIdentify, [])],- start_identify_fetch_with_waiters(- Request,- SocketPid,- SessionId,- Waiters,- State1#{pending_identifies := RemainingPending}- ).+ start_identify_fetch_with_waiters(Request, SocketPid, SessionId, Waiters, State1).-spec start_identify_fetch_with_waiters(map(),@@ -262,17 +261,20 @@ supersede_identify_fetch(Request, SocketPid, From, State, SessionId, PendingIden[gen_server:from()],state()) -> {noreply, state()}.-start_identify_fetch_with_waiters(Request, SocketPid, SessionId, Froms, State) ->+start_identify_fetch_with_waiters(Request, SocketPid, SessionId, Froms, State0) ->WorkerToken = make_ref(),+ State = ensure_concurrency_monitor(State0),ok = gateway_concurrency:request_session_start({SessionId, WorkerToken}),TimeoutRef = erlang:send_after(- ?IDENTIFY_FETCH_TIMEOUT_MS, self(), {identify_fetch_timeout, SessionId, WorkerToken}+ ?IDENTIFY_QUEUE_TIMEOUT_MS, self(), {identify_queue_timeout, SessionId, WorkerToken}),+ SocketRef = erlang:monitor(process, SocketPid),PendingIdentifies = maps:get(pending_identifies, State),NewPending = PendingIdentifies#{SessionId => #{request => Request,socket_pid => SocketPid,+ socket_ref => SocketRef,froms => Froms,worker_ref => undefined,worker_token => WorkerToken,@@ -281,28 +283,142 @@ start_identify_fetch_with_waiters(Request, SocketPid, SessionId, Froms, State) -slot_held => false}},- {noreply, State#{pending_identifies := NewPending}}.+ Monitors = maps:get(socket_monitors, State, #{}),+ {noreply, State#{+ pending_identifies := NewPending,+ socket_monitors => Monitors#{SocketRef => SessionId}+ }}.++-spec rewatch_socket(session_id(), pid(), pending_identify(), state()) ->+ {pending_identify(), state()}.+rewatch_socket(SessionId, SocketPid, PendingIdentify, State) ->+ case PendingIdentify of+ #{socket_pid := SocketPid, socket_ref := _} ->+ {PendingIdentify, State};+ _ ->+ watch_socket(SessionId, SocketPid, PendingIdentify, State)+ end.++-spec watch_socket(session_id(), pid(), pending_identify(), state()) ->+ {pending_identify(), state()}.+watch_socket(SessionId, SocketPid, PendingIdentify, State) ->+ Forgotten = forget_socket(PendingIdentify, State),+ SocketRef = erlang:monitor(process, SocketPid),+ Monitors = maps:get(socket_monitors, Forgotten, #{}),+ {PendingIdentify#{socket_pid => SocketPid, socket_ref => SocketRef}, Forgotten#{+ socket_monitors => Monitors#{SocketRef => SessionId}+ }}.++-spec forget_socket(pending_identify(), state()) -> state().+forget_socket(#{socket_ref := SocketRef}, State) ->+ demonitor(SocketRef, [flush]),+ State#{socket_monitors => maps:remove(SocketRef, maps:get(socket_monitors, State, #{}))};+forget_socket(_PendingIdentify, State) ->+ State.++-spec drop_pending(session_id(), pending_identify(), state()) -> state().+drop_pending(SessionId, PendingIdentify, State) ->+ cancel_identify_timeout(PendingIdentify),+ Forgotten = forget_socket(PendingIdentify, State),+ Stopped =+ case maps:get(worker_ref, PendingIdentify, undefined) of+ undefined ->+ Tag = {SessionId, maps:get(worker_token, PendingIdentify)},+ gateway_concurrency:cancel_session_start(Tag),+ Forgotten;+ WorkerRef ->+ kill_identify_worker(WorkerRef, PendingIdentify, Forgotten)+ end,+ Stopped#{+ pending_identifies := maps:remove(SessionId, maps:get(pending_identifies, Stopped))+ }.++-spec ensure_concurrency_monitor(state()) -> state().+ensure_concurrency_monitor(State) ->+ case {maps:get(concurrency_monitor, State, undefined), whereis(gateway_concurrency)} of+ {{_Ref, Pid}, Pid} ->+ State;+ {_Monitor, undefined} ->+ State;+ {_Monitor, Pid} ->+ State#{concurrency_monitor => {erlang:monitor(process, Pid), Pid}}+ end.++-spec handle_identify_queue_timeout(session_id(), reference(), state()) -> {noreply, state()}.+handle_identify_queue_timeout(SessionId, WorkerToken, State) ->+ PendingIdentifies = maps:get(pending_identifies, State),+ case maps:get(SessionId, PendingIdentifies, undefined) of+ #{worker_token := WorkerToken, worker_ref := undefined} = PendingIdentify ->+ reply_to_waiters(maps:get(froms, PendingIdentify, []), {error, at_capacity}),+ {noreply, drop_pending(SessionId, PendingIdentify, State)};+ _ ->+ {noreply, State}+ end.++-spec handle_other_down(reference(), pid(), state()) -> {noreply, state()}.+handle_other_down(Ref, Pid, State) ->+ case maps:get(Ref, maps:get(socket_monitors, State, #{}), undefined) of+ SessionId when is_binary(SessionId) ->+ handle_socket_down(SessionId, Ref, State);+ undefined ->+ handle_non_socket_down(Ref, Pid, State)+ end.++-spec handle_socket_down(session_id(), reference(), state()) -> {noreply, state()}.+handle_socket_down(SessionId, Ref, State) ->+ Monitors = maps:remove(Ref, maps:get(socket_monitors, State, #{})),+ Cleared = State#{socket_monitors => Monitors},+ case maps:get(SessionId, maps:get(pending_identifies, Cleared), undefined) of+ #{socket_ref := Ref} = PendingIdentify ->+ reply_to_waiters(maps:get(froms, PendingIdentify, []), {error, socket_closed}),+ {noreply, drop_pending(SessionId, PendingIdentify, Cleared)};+ _ ->+ {noreply, Cleared}+ end.++-spec handle_non_socket_down(reference(), pid(), state()) -> {noreply, state()}.+handle_non_socket_down(Ref, Pid, State) ->+ case maps:get(concurrency_monitor, State, undefined) of+ {Ref, Pid} ->+ erlang:send_after(?RESUBMIT_RETRY_MS, self(), resubmit_session_starts),+ {noreply, State#{concurrency_monitor => undefined}};+ _ ->+ {noreply, session_manager_shard_session_index:cleanup_down(Ref, Pid, State)}+ end.++-spec resubmit_session_starts(state()) -> {noreply, state()}.+resubmit_session_starts(State) ->+ case ensure_concurrency_monitor(State) of+ #{concurrency_monitor := {_Ref, _Pid}} = Monitored ->+ Queued = lists:keysort(1, [+ {QueuedAt, {SessionId, Token}}+ || SessionId := #{+ worker_ref := undefined, worker_token := Token, queued_at := QueuedAt+ } <- maps:get(pending_identifies, Monitored)+ ]),+ lists:foreach(+ fun({_QueuedAt, Tag}) -> gateway_concurrency:request_session_start(Tag) end,+ Queued+ ),+ {noreply, Monitored};+ Unmonitored ->+ erlang:send_after(?RESUBMIT_RETRY_MS, self(), resubmit_session_starts),+ {noreply, Unmonitored}+ end.-spec admit_identify_fetch(session_id(), reference(), state()) -> {noreply, state()}.admit_identify_fetch(SessionId, WorkerToken, State) ->PendingIdentifies = maps:get(pending_identifies, State),case maps:get(SessionId, PendingIdentifies, undefined) of#{worker_token := WorkerToken, worker_ref := undefined} = PendingIdentify ->- admit_pending_identify(- socket_alive(maps:get(socket_pid, PendingIdentify)),- SessionId,- PendingIdentify,- State- );+ admit_pending_identify(SessionId, PendingIdentify, State);_ ->gateway_concurrency:release_session_start(),{noreply, State}end.--spec admit_pending_identify(boolean(), session_id(), pending_identify(), state()) ->- {noreply, state()}.-admit_pending_identify(false, SessionId, PendingIdentify, State) ->- gateway_concurrency:release_session_start(),+-spec admit_pending_identify(session_id(), pending_identify(), state()) -> {noreply, state()}.+admit_pending_identify(SessionId, PendingIdentify, State) ->cancel_identify_timeout(PendingIdentify),ManagerPid = self(),Request = maps:get(request, PendingIdentify),@@ -313,7 +429,12 @@ admit_pending_identify(false, SessionId, PendingIdentify, State) ->run_identify_fetch(ManagerPid, SessionId, WorkerToken, Request, QueuedAt)end),PendingIdentifies = maps:get(pending_identifies, State),- Admitted = PendingIdentify#{worker_ref => WorkerRef, slot_held => true},+ TimeoutRef = erlang:send_after(+ ?IDENTIFY_FETCH_TIMEOUT_MS, self(), {identify_fetch_timeout, SessionId, WorkerToken}+ ),+ Admitted = PendingIdentify#{+ worker_ref => WorkerRef, slot_held => true, timeout_ref => TimeoutRef+ },IdentifyWorkers = maps:get(identify_workers, State),{noreply, State#{pending_identifies := PendingIdentifies#{SessionId := Admitted},@@ -383,7 +504,9 @@ do_complete_identify(true ->cancel_identify_timeout(PendingIdentify),release_identify_slot(PendingIdentify),- State1 = State#{pending_identifies := RemainingPending},+ State1 = forget_socket(PendingIdentify, State#{+ pending_identifies := RemainingPending+ }),State2 = cleanup_identify_worker(SessionId, State1),{Reply, NewState} = resolve_identify_result(FetchResult, PendingIdentify, SessionId, State2@@ -406,13 +529,11 @@ release_identify_slot(PendingIdentify) ->handle_identify_fetch_timeout(SessionId, WorkerToken, State) ->PendingIdentifies = maps:get(pending_identifies, State),case maps:get(SessionId, PendingIdentifies, undefined) of- #{worker_token := WorkerToken} = PendingIdentify ->+ #{worker_token := WorkerToken, worker_ref := WorkerRef} = PendingIdentify when+ is_reference(WorkerRef)+ ->reply_to_waiters(maps:get(froms, PendingIdentify, []), {error, timeout}),- State1 = kill_identify_worker(- maps:get(worker_ref, PendingIdentify), PendingIdentify, State- ),- RemainingPending = maps:remove(SessionId, maps:get(pending_identifies, State1)),- {noreply, State1#{pending_identifies := RemainingPending}};+ {noreply, drop_pending(SessionId, PendingIdentify, State)};_Other ->{noreply, State}end.@@ -442,7 +563,8 @@ do_fail_pending(WorkerRef, Reason, PendingIdentify, RemainingPending, State) ->reply_to_waiters(maps:get(froms, PendingIdentify, []), {error, {network_error, Reason}}),- {noreply, State#{pending_identifies := RemainingPending}};+ {noreply,+ forget_socket(PendingIdentify, State#{pending_identifies := RemainingPending})};false ->{noreply, State}end.