Skip to content

(WIP) Introduce Thrift.Serializable #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,15 @@ end

## Serialization

A `BinaryProtocol` module is generated for each Thrift struct, union, and
exception type. You can use this interface to easily serialize and deserialize
your own types.
Each thrift struct, union and exception also has a `SerDe` protocol generated for
it. This module lets you serialize and deserialize its own type easily via `Thrift.Serializable`.

```elixir
iex> alias Calculator.Generated.Vector
iex> data = %Vector{x: 1, y: 2, z: 3}
|> Vector.BinaryProtocol.serialize
|> IO.iodata_to_binary
iex> Vector.BinaryProtocol.deserialize(data)
{%Calculator.Generated.Vector{x: 1.0, y: 2.0, z: 3.0}, ""}
|> Thrift.Serializable.serialize(%Thrift.Binary{payload: ""})
iex> Thrift.Serializable.deserialize(%Vector{}, data)
{%Calculator.Generated.Vector{x: 1.0, y: 2.0, z: 3.0}, %Thrift.Protocol.Binary{payload: ""}}
```

## Thrift IDL Parsing
Expand Down
18 changes: 9 additions & 9 deletions lib/thrift.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ defmodule Thrift do
... the generated code will be placed in the following modules under
`lib/thrift/`:

Definition | Module
------------------------- | -----------------------------------------------
`User` struct | `Thrift.Test.User`
*└ binary protocol* | `Thrift.Test.User.BinaryProtocol`
`UserNotFound` exception | `Thrift.Test.UserNotFound`
*└ binary protocol* | `Thrift.Test.UserNotFound.BinaryProtocol`
`UserService` service | `Thrift.Test.UserService.Handler`
*└ binary framed client* | `Thrift.Test.UserService.Binary.Framed.Client`
*└ binary framed server* | `Thrift.Test.UserService.Binary.Framed.Server`
Definition | Module
-------------------------- | -----------------------------------------------
`User` struct | `Thrift.Test.User`
*└ serialization protocol* | `Thrift.Test.User.SerDe`
`UserNotFound` exception | `Thrift.Test.UserNotFound`
*└ serialization protocol* | `Thrift.Test.UserNotFound.SerDe`
`UserService` service | `Thrift.Test.UserService.Handler`
*└ binary framed client* | `Thrift.Test.UserService.Binary.Framed.Client`
*└ binary framed server* | `Thrift.Test.UserService.Binary.Framed.Server`

### Namespaces

Expand Down
43 changes: 28 additions & 15 deletions lib/thrift/binary/framed/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -182,32 +182,34 @@ defmodule Thrift.Binary.Framed.Client do
end
end

@spec oneway(pid, String.t(), iodata, options) :: :ok
@spec oneway(pid, String.t(), struct, options) :: :ok
@doc """
Execute a one way RPC. One way RPC calls do not generate a response,
and as such, this implementation uses `GenServer.cast`.
The data argument must be a properly formatted Thrift message.
The argument must be a Thrift struct.
"""
def oneway(conn, rpc_name, serialized_args, _opts) do
def oneway(conn, rpc_name, args, _opts) do
serialized_args = Thrift.Serializable.serialize(args, %Binary{payload: ""})
:ok = Connection.cast(conn, {:oneway, rpc_name, serialized_args})
end

@spec call(pid, String.t(), iodata, module, options) :: protocol_response
@spec call(pid, String.t(), struct, struct, options) :: protocol_response
@doc """
Executes a Thrift RPC. The data argument must be a correctly formatted
Thrift message.
Executes a Thrift RPC. The argument and response arguments must be Thrift structs.

The `opts` argument takes the same type of keyword list that `start_link` takes.
"""
def call(conn, rpc_name, serialized_args, deserialize_module, opts) do
def call(conn, rpc_name, args, resp, opts) do
tcp_opts = Keyword.get(opts, :tcp_opts, [])
gen_server_opts = Keyword.get(opts, :gen_server_opts, [])
gen_server_timeout = Keyword.get(gen_server_opts, :timeout, 5000)

serialized_args = Thrift.Serializable.serialize(args, %Binary{payload: ""})

case Connection.call(conn, {:call, rpc_name, serialized_args, tcp_opts}, gen_server_timeout) do
{:ok, data} ->
data
|> deserialize_module.deserialize
resp
|> Thrift.Serializable.deserialize(%Binary{payload: data})
|> unpack_response

{:error, _} = err ->
Expand All @@ -222,7 +224,7 @@ defmodule Thrift.Binary.Framed.Client do
#
# As a small optimization, we only use this function if the response struct
# has at least one exception field (hence the `map_size/1` guard check).
defp unpack_response({%{success: nil} = response, ""}) when map_size(response) > 2 do
defp unpack_response({%{success: nil} = response, %Binary{payload: ""}}) when map_size(response) > 2 do
exception =
response
|> Map.from_struct()
Expand All @@ -236,15 +238,15 @@ defmodule Thrift.Binary.Framed.Client do
end
end

defp unpack_response({%{success: result}, ""}), do: {:ok, result}
defp unpack_response({%{success: result}, %Binary{payload: ""}}), do: {:ok, result}
defp unpack_response({:error, _} = error), do: error

def handle_call(_, _, %{sock: nil} = s) do
{:reply, {:error, :closed}, s}
end

def handle_call(
{:call, rpc_name, serialized_args, tcp_opts},
{:call, rpc_name, %Binary{payload: serialized_args}, tcp_opts},
_,
%{sock: {transport, sock}, seq_id: seq_id, timeout: default_timeout} = s
) do
Expand Down Expand Up @@ -276,7 +278,7 @@ defmodule Thrift.Binary.Framed.Client do
end

def handle_cast(
{:oneway, rpc_name, serialized_args},
{:oneway, rpc_name, %Binary{payload: serialized_args}},
%{sock: {transport, sock}, seq_id: seq_id} = s
) do
s = %{s | seq_id: seq_id + 1}
Expand Down Expand Up @@ -304,8 +306,7 @@ defmodule Thrift.Binary.Framed.Client do
seq_id,
rpc_name
) do
exception = Binary.deserialize(:application_exception, serialized_response)
{:error, {:exception, exception}}
{:error, {:exception, deserialize_exception(serialized_response)}}
end

defp handle_message({:ok, {message_type, seq_id, rpc_name, _}}, seq_id, rpc_name) do
Expand Down Expand Up @@ -345,6 +346,18 @@ defmodule Thrift.Binary.Framed.Client do
err
end

defp deserialize_exception(payload) do
case TApplicationException.SerDe.deserialize(%Binary{payload: payload}) do
{exception, %Binary{payload: ""}} ->
exception
_ ->
TApplicationException.exception(
type: :protocol_error,
message: "Unable to decode exception (#{inspect payload})"
)
end
end

defp to_host(host) when is_bitstring(host) do
String.to_charlist(host)
end
Expand Down
35 changes: 24 additions & 11 deletions lib/thrift/binary/framed/protocol_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -170,31 +170,44 @@ defmodule Thrift.Binary.Framed.ProtocolHandler do
server_module,
handler_module
) do
case server_module.handle_thrift(name, args_binary, handler_module) do
{:reply, serialized_reply} ->
{args, _} = server_module.deserialize(name, %Thrift.Protocol.Binary{payload: args_binary})
case server_module.handle_thrift(args, handler_module) do
{:reply, reply} ->
message = Protocol.Binary.serialize(:message_begin, {:reply, sequence_id, name})
%Thrift.Protocol.Binary{payload: serialized_msg} =
Thrift.Serializable.serialize(reply, %Thrift.Protocol.Binary{payload: message})

{:ok, :reply, [message | serialized_reply]}

{:server_error, %TApplicationException{} = exc} ->
message = Protocol.Binary.serialize(:message_begin, {:exception, sequence_id, name})
serialized_exception = Protocol.Binary.serialize(:application_exception, exc)

{:error, {:server_error, [message | serialized_exception]}}

{:ok, :reply, serialized_msg}
:noreply ->
message = Protocol.Binary.serialize(:message_begin, {:reply, sequence_id, name})

{:ok, :reply, [message | <<0>>]}
end
catch
kind, reason ->
formatted_exception = Exception.format(kind, reason, System.stacktrace())
Logger.error("Exception not defined in thrift spec was thrown: #{formatted_exception}")

error =
Thrift.TApplicationException.exception(
type: :internal_error,
message: "Server error: #{formatted_exception}"
)

message = Protocol.Binary.serialize(:message_begin, {:exception, sequence_id, name})
%Thrift.Protocol.Binary{payload: serialized_msg} =
TApplicationException.SerDe.Thrift.Protocol.Binary.serialize(%Thrift.Protocol.Binary{payload: message}, error)

{:ok, :reply, serialized_msg}
end

defp handle_thrift_message(
{:ok, {:oneway, _seq_id, name, args_binary}},
server_module,
handler_module
) do
spawn(server_module, :handle_thrift, [name, args_binary, handler_module])
{args, _} = server_module.deserialize(name, %Thrift.Protocol.Binary{payload: args_binary})
spawn(server_module, :handle_thrift, [args, handler_module])
{:ok, :reply, <<0>>}
end

Expand Down
37 changes: 36 additions & 1 deletion lib/thrift/exceptions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Thrift.TApplicationException do
Application-level exception
"""

@type t() :: %__MODULE__{message: String.t, type: exception_type()}
@enforce_keys [:message, :type]
defexception message: "unknown", type: :unknown

Expand All @@ -27,6 +28,11 @@ defmodule Thrift.TApplicationException do
injected_failure: 13
]

@typedoc """
Exception types
"""
@type exception_type :: unquote(Enum.reduce(@exception_types, fn {type, _}, acc -> {:|, [] , [type, acc]} end))

def exception(args) when is_list(args) do
type = normalize_type(Keyword.fetch!(args, :type))
message = args[:message] || Atom.to_string(type)
Expand All @@ -36,7 +42,7 @@ defmodule Thrift.TApplicationException do
@doc """
Converts an exception type to its integer identifier.
"""
@spec type_id(atom) :: non_neg_integer
@spec type_id(exception_type()) :: non_neg_integer()
def type_id(type)

for {type, id} <- @exception_types do
Expand All @@ -46,6 +52,35 @@ defmodule Thrift.TApplicationException do
end

defp normalize_type(type) when is_integer(type), do: :unknown

defprotocol SerDe do
@moduledoc """
Serialize and deserialize protocol for `Thrift.TApplicationException.t`
"""

@doc """
Serialize `Thrift.TApplicationException.t` with a protocol payload.
"""
@spec serialize(payload, TApplicationException.t()) :: payload when payload: var
def serialize(payload, err)

@doc """
Deserialize `Thrift.TApplication.t` with a protocol payload.
"""
@spec deserialize(payload) :: {TApplicationException.t(), payload} | :error when payload: var
def deserialize(payload)

@doc """
Deserialize `Thrift.TApplication.t` with a protocol payload and default values.
"""
@spec deserialize(payload, TApplicationException.t()) :: {TApplicationException.t(), payload} | :error when payload: var
def deserialize(payload, err)
end

defimpl Thrift.Serializable do
def serialize(err, payload), do: SerDe.serialize(payload, err)
def deserialize(err, payload), do: SerDe.deserialize(payload, err)
end
end

defmodule Thrift.ConnectionError do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Thrift.Generator.Binary.Framed.Client do
defmodule Thrift.Generator.Client do
@moduledoc false

alias Thrift.AST.Function
Expand Down Expand Up @@ -27,11 +27,6 @@ defmodule Thrift.Generator.Binary.Framed.Client do
end

defp generate_handler_function(function) do
args_module = Service.module_name(function, :args)
args_binary_module = Module.concat(args_module, :BinaryProtocol)
response_module = Service.module_name(function, :response)
rpc_name = Atom.to_string(function.name)

# Make two Elixir-friendly function names: an underscored version of the
# Thrift function name and a "bang!" exception-raising variant.
function_name =
Expand Down Expand Up @@ -75,9 +70,7 @@ defmodule Thrift.Generator.Binary.Framed.Client do

quote do
def(unquote(function_name)(client, unquote_splicing(vars), rpc_opts \\ [])) do
args = %unquote(args_module){unquote_splicing(assignments)}
serialized_args = unquote(args_binary_module).serialize(args)
unquote(build_response_handler(function, rpc_name, response_module))
unquote(build_call(function, assignments))
end

def(unquote(bang_name)(client, unquote_splicing(vars), rpc_opts \\ [])) do
Expand All @@ -95,18 +88,24 @@ defmodule Thrift.Generator.Binary.Framed.Client do
end
end

defp build_response_handler(%Function{oneway: true}, rpc_name, _response_module) do
defp build_call(%Function{oneway: true} = function, assignments) do
rpc_name = Atom.to_string(function.name)
args_module = Service.module_name(function, :args)
quote do
:ok = ClientImpl.oneway(client, unquote(rpc_name), serialized_args, rpc_opts)
args = %unquote(args_module){unquote_splicing(assignments)}
:ok = ClientImpl.oneway(client, unquote(rpc_name), args, rpc_opts)
{:ok, nil}
end
end

defp build_response_handler(%Function{oneway: false}, rpc_name, response_module) do
module = Module.concat(response_module, :BinaryProtocol)

defp build_call(%Function{oneway: false} = function, assignments) do
rpc_name = Atom.to_string(function.name)
args_module = Service.module_name(function, :args)
response_module = Service.module_name(function, :response)
quote do
ClientImpl.call(client, unquote(rpc_name), serialized_args, unquote(module), rpc_opts)
args = %unquote(args_module){unquote_splicing(assignments)}
resp = %unquote(response_module){}
ClientImpl.call(client, unquote(rpc_name), args, resp, rpc_opts)
end
end
end
Loading