Skip to content

Commit 5faeefc

Browse files
committed
check wire_version in case of sessions
1 parent 0f3a838 commit 5faeefc

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

.travis.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ elixir:
55
- 1.8
66
- 1.9
77

8-
matrix:
9-
include:
10-
- elixir: '1.6'
11-
otp_release: '19.3'
12-
- elixir: '1.7'
13-
otp_release: '19.3'
14-
158
install:
169
- wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-${MONGODB}.tgz
1710
- tar xzf mongodb-linux-x86_64-${MONGODB}.tgz

lib/mongo/session.ex

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule Mongo.Session do
2626
# * `server_session` the server_session data
2727
# * `opts` options
2828
# * `implicit` true or false
29-
defstruct [conn: nil, server_session: nil, implicit: false, opts: []]
29+
defstruct [conn: nil, server_session: nil, implicit: false, wire_version: 0, opts: []]
3030

3131
@impl true
3232
def callback_mode() do
@@ -36,9 +36,9 @@ defmodule Mongo.Session do
3636
@doc """
3737
Start the generic state machine.
3838
"""
39-
@spec start_link(GenServer.server, ServerSession.t, atom, keyword()) :: {:ok, Session.t} | :ignore | {:error, term()}
40-
def start_link(conn, server_session, type, opts) do
41-
:gen_statem.start_link(__MODULE__, {conn, server_session, type, opts}, [])
39+
@spec start_link(GenServer.server, ServerSession.t, atom, integer, keyword()) :: {:ok, Session.t} | :ignore | {:error, term()}
40+
def start_link(conn, server_session, type, wire_version, opts) do
41+
:gen_statem.start_link(__MODULE__, {conn, server_session, type, wire_version, opts}, [])
4242
end
4343

4444
@doc """
@@ -126,21 +126,29 @@ defmodule Mongo.Session do
126126
def alive?(pid), do: Process.alive?(pid)
127127

128128
@impl true
129-
def init({conn, server_session, type, opts}) do
129+
def init({conn, server_session, type, wire_version, opts}) do
130130
data = %Session{conn: conn,
131131
server_session: server_session,
132-
implicit: (type == :implicit), opts: opts}
132+
implicit: (type == :implicit),
133+
wire_version: wire_version,
134+
opts: opts}
133135
{:ok, :no_transaction, data}
134136
end
135137

136138
@impl true
137139
def handle_event({:call, from}, {:start_transaction}, state, %Session{server_session: session} = data) when state in [:no_transaction, :transaction_aborted, :transaction_committed] do
138140
{:next_state, :starting_transaction, %Session{data | server_session: ServerSession.next_txn_num(session)}, {:reply, from, :ok}}
139141
end
140-
def handle_event({:call, from}, {:bind_session, cmd}, :no_transaction, %Session{conn: conn, server_session: %ServerSession{session_id: id}}) do
142+
##
143+
# bind session: only if wire_version >= 6, MongoDB 3.6.x
144+
#
145+
def handle_event({:call, from}, {:bind_session, cmd}, :no_transaction, %Session{conn: conn, wire_version: wire_version, server_session: %ServerSession{session_id: id}}) when wire_version >= 6 do
141146
{:keep_state_and_data, {:reply, from, {:ok, conn, Keyword.merge(cmd, lsid: %{id: id})}}}
142147
end
143-
def handle_event({:call, from}, {:bind_session, cmd}, :starting_transaction, %Session{conn: conn, server_session: %ServerSession{session_id: id, txn_num: txn_num}, opts: opts} = data) do
148+
def handle_event({:call, from}, {:bind_session, cmd}, :starting_transaction,
149+
%Session{conn: conn,
150+
server_session: %ServerSession{session_id: id, txn_num: txn_num},
151+
wire_version: wire_version, opts: opts} = data) when wire_version >= 6 do
144152
result = Keyword.merge(cmd,
145153
readConcern: Keyword.get(opts, :read_concern),
146154
lsid: %{id: id},
@@ -149,13 +157,18 @@ defmodule Mongo.Session do
149157
autocommit: false) |> filter_nils()
150158
{:next_state, :transaction_in_progress, data, {:reply, from, {:ok, conn, result}}}
151159
end
152-
def handle_event({:call, from}, {:bind_session, cmd}, :transaction_in_progress, %Session{conn: conn, server_session: %ServerSession{session_id: id, txn_num: txn_num}}) do
160+
def handle_event({:call, from}, {:bind_session, cmd}, :transaction_in_progress,
161+
%Session{conn: conn, wire_version: wire_version,
162+
server_session: %ServerSession{session_id: id, txn_num: txn_num}}) when wire_version >= 6 do
153163
result = Keyword.merge(cmd,
154164
lsid: %{id: id},
155165
txnNumber: %BSON.LongNumber{value: txn_num},
156166
autocommit: false)
157167
{:keep_state_and_data, {:reply, from, {:ok, conn, result}}}
158168
end
169+
def handle_event({:call, from}, {:bind_session, cmd}, transaction, %Session{conn: conn}) when transaction in [:no_transaction, :starting_transaction, :transaction_in_progress] do
170+
{:keep_state_and_data, {:reply, from, {:ok, conn, cmd}}}
171+
end
159172

160173
def handle_event({:call, from}, {:commit_transaction}, :transaction_in_progress, data) do
161174
{:next_state, :transaction_committed, data, {:reply, from, run_commit_command(data)}}
@@ -172,7 +185,7 @@ defmodule Mongo.Session do
172185
def handle_event({:call, from}, {:end_implicit_session}, _state, %Session{server_session: session_server, implicit: true}) do
173186
{:stop_and_reply, :normal, {:reply, from, {:ok, session_server}}}
174187
end
175-
def handle_event({:call, from}, {:end_implicit_session}, _state, %Session{server_session: session_server, implicit: false}) do
188+
def handle_event({:call, from}, {:end_implicit_session}, _state, %Session{implicit: false}) do
176189
{:keep_state_and_data, {:reply, from, :noop}}
177190
end
178191

lib/mongo/topology.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,10 @@ defmodule Mongo.Topology do
252252
with {:ok, connection} <- servers
253253
|> Enum.take_random(1)
254254
|> get_connection(state),
255+
{:ok, wire_version} <- Mongo.wire_version(connection),
255256
server_session <- checkout_server_session(state),
256-
{:ok, session} <- Session.start_link(connection, server_session, type, opts) do
257+
{:ok, session} <- Session.start_link(connection, server_session, type, wire_version, opts) do
258+
257259
Logger.debug("select_server: connection is #{inspect connection}, server_session is #{inspect server_session}")
258260
{:reply, {:ok, session}, state}
259261
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule Mongodb.Mixfile do
77
[app: :mongodb_driver,
88
version: @version,
99
elixirc_paths: elixirc_paths(Mix.env),
10-
elixir: "~> 1.6",
10+
elixir: "~> 1.8",
1111
name: "mongodb-driver",
1212
deps: deps(),
1313
docs: docs(),

0 commit comments

Comments
 (0)