-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RTC-543] Request routing first iteration (#212)
* [RTC-543] Request routing first iteration * Add integration tests, review fixes * fix types * fix more types * Increase timeout * Review fixes vol.2 * Review fixes vol.3 * Review fixes vol.4
- Loading branch information
Showing
56 changed files
with
2,089 additions
and
1,421 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
defmodule Fishjam.Cluster.Room do | ||
@moduledoc """ | ||
Module responsible for managing a room present anywhere in the cluster. | ||
""" | ||
|
||
@behaviour Fishjam.Room | ||
|
||
alias Fishjam.{Room, RPCClient} | ||
|
||
@local_module Fishjam.Local.Room | ||
|
||
@impl true | ||
def add_peer(room_id, peer_type, options \\ %{}), | ||
do: route_request(room_id, :add_peer, [room_id, peer_type, options]) | ||
|
||
@impl true | ||
def set_peer_connected(room_id, peer_id), | ||
do: route_request(room_id, :set_peer_connected, [room_id, peer_id]) | ||
|
||
@impl true | ||
def get_peer_connection_status(room_id, peer_id), | ||
do: route_request(room_id, :get_peer_connection_status, [room_id, peer_id]) | ||
|
||
@impl true | ||
def remove_peer(room_id, peer_id), | ||
do: route_request(room_id, :remove_peer, [room_id, peer_id]) | ||
|
||
@impl true | ||
def add_component(room_id, component_type, options \\ %{}), | ||
do: route_request(room_id, :add_component, [room_id, component_type, options]) | ||
|
||
@impl true | ||
def remove_component(room_id, component_id), | ||
do: route_request(room_id, :remove_component, [room_id, component_id]) | ||
|
||
@impl true | ||
def subscribe(room_id, component_id, origins), | ||
do: route_request(room_id, :subscribe, [room_id, component_id, origins]) | ||
|
||
@impl true | ||
def dial(room_id, component_id, phone_number), | ||
do: route_request(room_id, :dial, [room_id, component_id, phone_number]) | ||
|
||
@impl true | ||
def end_call(room_id, component_id), | ||
do: route_request(room_id, :end_call, [room_id, component_id]) | ||
|
||
@impl true | ||
def receive_media_event(room_id, peer_id, event), | ||
do: route_request(room_id, :receive_media_event, [room_id, peer_id, event]) | ||
|
||
defp route_request(room_id, fun, args) do | ||
with {:ok, node} <- Room.ID.determine_node(room_id), | ||
{:ok, result} <- RPCClient.call(node, @local_module, fun, args) do | ||
result | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
defmodule Fishjam.Cluster.RoomService do | ||
@moduledoc """ | ||
Module responsible for managing rooms in the entire cluster. | ||
""" | ||
|
||
@behaviour Fishjam.RoomService | ||
|
||
require Logger | ||
|
||
alias Fishjam.{FeatureFlags, Room, RPCClient} | ||
|
||
@local_module Fishjam.Local.RoomService | ||
|
||
@impl true | ||
def create_room(config) do | ||
node_resources = RPCClient.multicall(@local_module, :get_resource_usage) | ||
min_node = find_best_node(node_resources) | ||
|
||
if length(node_resources) > 1, | ||
do: Logger.info("Node with least used resources is #{inspect(min_node)}") | ||
|
||
with {:ok, result} <- RPCClient.call(min_node, @local_module, :create_room, [config]) do | ||
result | ||
end | ||
end | ||
|
||
@impl true | ||
def list_rooms() do | ||
if FeatureFlags.request_routing_enabled?() do | ||
@local_module |> RPCClient.multicall(:list_rooms) |> List.flatten() | ||
else | ||
apply(@local_module, :list_rooms, []) | ||
end | ||
end | ||
|
||
@impl true | ||
def find_room(room_id), do: route_request(room_id, :find_room, [room_id]) | ||
|
||
@impl true | ||
def get_room(room_id), do: route_request(room_id, :get_room, [room_id]) | ||
|
||
@impl true | ||
def delete_room(room_id), do: route_request(room_id, :delete_room, [room_id]) | ||
|
||
defp find_best_node(node_resources) do | ||
%{node: min_node} = | ||
Enum.min( | ||
node_resources, | ||
fn | ||
%{forwarded_tracks_number: forwarded_tracks, rooms_number: rooms_num1}, | ||
%{forwarded_tracks_number: forwarded_tracks, rooms_number: rooms_num2} -> | ||
rooms_num1 < rooms_num2 | ||
|
||
%{forwarded_tracks_number: forwarded_tracks1}, | ||
%{forwarded_tracks_number: forwarded_tracks2} -> | ||
forwarded_tracks1 < forwarded_tracks2 | ||
end | ||
) | ||
|
||
min_node | ||
end | ||
|
||
defp route_request(room_id, fun, args) do | ||
with {:ok, node} <- Room.ID.determine_node(room_id), | ||
{:ok, result} <- RPCClient.call(node, @local_module, fun, args) do | ||
result | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
defmodule Fishjam.Component.HLS.Cluster.RequestHandler do | ||
@moduledoc """ | ||
Module responsible for handling HLS Retrieve Content requests in the cluster. | ||
""" | ||
|
||
@behaviour Fishjam.Component.HLS.RequestHandler | ||
|
||
alias Fishjam.{Room, RPCClient} | ||
|
||
@local_module Fishjam.Component.HLS.Local.RequestHandler | ||
|
||
@impl true | ||
def handle_file_request(room_id, filename), | ||
do: route_request(room_id, :handle_file_request, [room_id, filename]) | ||
|
||
@impl true | ||
def handle_partial_request(room_id, filename), | ||
do: route_request(room_id, :handle_partial_request, [room_id, filename]) | ||
|
||
@impl true | ||
def handle_manifest_request(room_id, partial), | ||
do: route_request(room_id, :handle_manifest_request, [room_id, partial]) | ||
|
||
@impl true | ||
def handle_delta_manifest_request(room_id, partial), | ||
do: route_request(room_id, :handle_delta_manifest_request, [room_id, partial]) | ||
|
||
defp route_request(room_id, fun, args) do | ||
with {:ok, node} <- Room.ID.determine_node(room_id), | ||
{:here?, false} <- {:here?, node == Node.self()}, | ||
# FIXME: Fishjam addresses could easily be cached | ||
{:ok, address} <- RPCClient.call(node, Fishjam, :address) do | ||
{:redirect, address} | ||
else | ||
{:here?, true} -> apply(@local_module, fun, args) | ||
{:error, _reason} = error -> error | ||
end | ||
end | ||
end |
Oops, something went wrong.