2012-02-17 05:42:22 +00:00
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%%
|
|
|
|
%% wterl_conn: manage a connection to WiredTiger
|
|
|
|
%%
|
|
|
|
%% Copyright (c) 2012 Basho Technologies, Inc. All Rights Reserved.
|
|
|
|
%%
|
|
|
|
%% This file is provided to you under the Apache License,
|
|
|
|
%% Version 2.0 (the "License"); you may not use this file
|
|
|
|
%% except in compliance with the License. You may obtain
|
|
|
|
%% a copy of the License at
|
|
|
|
%%
|
|
|
|
%% http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
%%
|
|
|
|
%% Unless required by applicable law or agreed to in writing,
|
|
|
|
%% software distributed under the License is distributed on an
|
|
|
|
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
%% KIND, either express or implied. See the License for the
|
|
|
|
%% specific language governing permissions and limitations
|
|
|
|
%% under the License.
|
|
|
|
%%
|
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
-module(wterl_conn).
|
|
|
|
-author('Steve Vinoski <steve@basho.com>').
|
|
|
|
|
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
-ifdef(TEST).
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
-endif.
|
|
|
|
|
|
|
|
%% API
|
2012-02-28 03:00:02 +00:00
|
|
|
-export([start_link/0, stop/0,
|
2013-04-06 21:19:59 +00:00
|
|
|
open/1, open/2, open/3, is_open/0, get/0, close/1]).
|
2012-02-17 05:42:22 +00:00
|
|
|
|
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
|
|
|
|
2013-03-17 03:28:13 +00:00
|
|
|
-record(state, { conn :: wterl:connection() }).
|
2012-02-17 05:42:22 +00:00
|
|
|
|
2012-04-09 20:50:01 +00:00
|
|
|
-type config_list() :: [{atom(), any()}].
|
|
|
|
|
2012-02-17 05:42:22 +00:00
|
|
|
%% ====================================================================
|
|
|
|
%% API
|
|
|
|
%% ====================================================================
|
|
|
|
|
2012-03-12 00:18:59 +00:00
|
|
|
-spec start_link() -> {ok, pid()} | {error, term()}.
|
2012-02-17 05:42:22 +00:00
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
2012-03-12 00:18:59 +00:00
|
|
|
-spec stop() -> ok.
|
2012-02-28 03:00:02 +00:00
|
|
|
stop() ->
|
|
|
|
gen_server:cast(?MODULE, stop).
|
|
|
|
|
2012-03-12 00:18:59 +00:00
|
|
|
-spec open(string()) -> {ok, wterl:connection()} | {error, term()}.
|
2012-04-09 20:50:01 +00:00
|
|
|
-spec open(string(), config_list()) -> {ok, wterl:connection()} | {error, term()}.
|
2013-04-06 21:19:59 +00:00
|
|
|
-spec open(string(), config_list(), config_list()) -> {ok, wterl:connection()} | {error, term()}.
|
|
|
|
open(Dir) ->
|
|
|
|
open(Dir, [], []).
|
|
|
|
open(Dir, ConnectionConfig) ->
|
|
|
|
gen_server:call(?MODULE, {open, Dir, ConnectionConfig, [], self()}, infinity).
|
|
|
|
open(Dir, ConnectionConfig, SessionConfig) ->
|
|
|
|
gen_server:call(?MODULE, {open, Dir, ConnectionConfig, SessionConfig, self()}, infinity).
|
2012-02-17 05:42:22 +00:00
|
|
|
|
2012-03-12 00:18:59 +00:00
|
|
|
-spec is_open() -> boolean().
|
2012-02-28 03:00:02 +00:00
|
|
|
is_open() ->
|
|
|
|
gen_server:call(?MODULE, is_open, infinity).
|
|
|
|
|
2012-03-12 00:18:59 +00:00
|
|
|
-spec get() -> {ok, reference()} | {error, term()}.
|
2012-02-17 05:42:22 +00:00
|
|
|
get() ->
|
|
|
|
gen_server:call(?MODULE, get, infinity).
|
|
|
|
|
2012-03-12 00:18:59 +00:00
|
|
|
-spec close(wterl:connection()) -> ok.
|
|
|
|
close(_Conn) ->
|
2012-02-28 02:12:52 +00:00
|
|
|
gen_server:call(?MODULE, {close, self()}, infinity).
|
2012-02-17 05:42:22 +00:00
|
|
|
|
|
|
|
%% ====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%% ====================================================================
|
|
|
|
|
|
|
|
init([]) ->
|
2012-04-05 20:10:50 +00:00
|
|
|
true = wterl_ets:table_ready(),
|
2012-02-17 05:42:22 +00:00
|
|
|
{ok, #state{}}.
|
|
|
|
|
2013-04-06 21:19:59 +00:00
|
|
|
handle_call({open, Dir, ConnectionConfig, SessionConfig, Caller}, _From, #state{conn=undefined}=State) ->
|
2013-03-21 18:12:03 +00:00
|
|
|
{Reply, NState} =
|
2013-04-22 21:45:48 +00:00
|
|
|
case wterl:connection_open(Dir, ConnectionConfig, SessionConfig) of
|
|
|
|
{ok, ConnRef}=OK ->
|
|
|
|
Monitor = erlang:monitor(process, Caller),
|
|
|
|
true = ets:insert(wterl_ets, {Monitor, Caller}),
|
|
|
|
{OK, State#state{conn = ConnRef}};
|
|
|
|
Error ->
|
|
|
|
{Error, State}
|
|
|
|
end,
|
2012-02-17 05:42:22 +00:00
|
|
|
{reply, Reply, NState};
|
2013-04-08 02:16:44 +00:00
|
|
|
handle_call({open, _Dir, _ConnectionConfig, _SessionConfig, Caller}, _From, #state{conn=ConnRef}=State) ->
|
2012-02-28 02:12:52 +00:00
|
|
|
Monitor = erlang:monitor(process, Caller),
|
2012-04-05 20:10:50 +00:00
|
|
|
true = ets:insert(wterl_ets, {Monitor, Caller}),
|
2012-02-17 05:42:22 +00:00
|
|
|
{reply, {ok, ConnRef}, State};
|
|
|
|
|
2012-02-28 03:00:02 +00:00
|
|
|
handle_call(is_open, _From, #state{conn=ConnRef}=State) ->
|
|
|
|
{reply, ConnRef /= undefined, State};
|
|
|
|
|
2012-02-28 02:12:52 +00:00
|
|
|
handle_call(get, _From, #state{conn=undefined}=State) ->
|
2012-02-17 05:42:22 +00:00
|
|
|
{reply, {error, "no connection"}, State};
|
2012-02-28 02:12:52 +00:00
|
|
|
handle_call(get, _From, #state{conn=ConnRef}=State) ->
|
2012-02-17 05:42:22 +00:00
|
|
|
{reply, {ok, ConnRef}, State};
|
|
|
|
|
2012-04-05 20:10:50 +00:00
|
|
|
handle_call({close, Caller}, _From, #state{conn=ConnRef}=State) ->
|
|
|
|
{[{Monitor, Caller}], _} = ets:match_object(wterl_ets, {'_', Caller}, 1),
|
2012-02-28 02:12:52 +00:00
|
|
|
true = erlang:demonitor(Monitor, [flush]),
|
2012-04-05 20:10:50 +00:00
|
|
|
true = ets:delete(wterl_ets, Monitor),
|
|
|
|
NState = case ets:info(wterl_ets, size) of
|
2012-02-28 02:12:52 +00:00
|
|
|
0 ->
|
2012-03-12 00:18:59 +00:00
|
|
|
do_close(ConnRef),
|
2012-04-05 20:10:50 +00:00
|
|
|
State#state{conn=undefined};
|
2012-02-28 02:12:52 +00:00
|
|
|
_ ->
|
|
|
|
State
|
|
|
|
end,
|
2012-04-05 20:10:50 +00:00
|
|
|
{reply, ok, NState};
|
|
|
|
handle_call(_Msg, _From, State) ->
|
|
|
|
{reply, ok, State}.
|
2012-02-17 05:42:22 +00:00
|
|
|
|
2012-02-28 03:00:02 +00:00
|
|
|
handle_cast(stop, #state{conn=undefined}=State) ->
|
2012-04-05 20:10:50 +00:00
|
|
|
{stop, normal, State};
|
|
|
|
handle_cast(stop, #state{conn=ConnRef}=State) ->
|
2012-03-12 00:18:59 +00:00
|
|
|
do_close(ConnRef),
|
2012-02-28 03:00:02 +00:00
|
|
|
ets:foldl(fun({Monitor, _}, _) ->
|
2012-04-05 20:10:50 +00:00
|
|
|
true = erl:demonitor(Monitor, [flush]),
|
|
|
|
ets:delete(wterl_ets, Monitor)
|
|
|
|
end, true, wterl_ets),
|
|
|
|
{stop, normal, State#state{conn=undefined}};
|
2012-02-17 05:42:22 +00:00
|
|
|
handle_cast(_Msg, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
2012-04-05 20:10:50 +00:00
|
|
|
handle_info({'DOWN', Monitor, _, _, _}, #state{conn=ConnRef}=State) ->
|
|
|
|
NState = case ets:lookup(wterl_ets, Monitor) of
|
2012-02-28 02:12:52 +00:00
|
|
|
[{Monitor, _}] ->
|
2012-04-05 20:10:50 +00:00
|
|
|
true = ets:delete(wterl_ets, Monitor),
|
|
|
|
case ets:info(wterl_ets, size) of
|
2012-02-28 02:12:52 +00:00
|
|
|
0 ->
|
2012-03-12 00:18:59 +00:00
|
|
|
do_close(ConnRef),
|
2012-04-05 20:10:50 +00:00
|
|
|
State#state{conn=undefined};
|
2012-02-28 02:12:52 +00:00
|
|
|
_ ->
|
|
|
|
State
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
State
|
|
|
|
end,
|
|
|
|
{noreply, NState};
|
2012-02-17 05:42:22 +00:00
|
|
|
handle_info(_Info, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, #state{conn = ConnRef}) ->
|
2012-03-12 00:18:59 +00:00
|
|
|
do_close(ConnRef),
|
2012-02-17 05:42:22 +00:00
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%% ====================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%% ====================================================================
|
|
|
|
|
2012-04-09 20:50:01 +00:00
|
|
|
%% @private
|
2012-03-12 00:18:59 +00:00
|
|
|
do_close(undefined) ->
|
2012-02-17 05:42:22 +00:00
|
|
|
ok;
|
2012-03-12 00:18:59 +00:00
|
|
|
do_close(ConnRef) ->
|
2013-03-28 14:24:26 +00:00
|
|
|
wterl:connection_close(ConnRef).
|
2012-02-17 05:42:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
-ifdef(TEST).
|
|
|
|
|
2012-02-28 03:00:02 +00:00
|
|
|
-define(DATADIR, "test/wterl-backend").
|
|
|
|
|
|
|
|
simple_test_() ->
|
|
|
|
{spawn,
|
|
|
|
[{setup,
|
|
|
|
fun() ->
|
|
|
|
?assertCmd("rm -rf " ++ ?DATADIR),
|
|
|
|
?assertMatch(ok, filelib:ensure_dir(filename:join(?DATADIR, "x"))),
|
2012-04-05 20:10:50 +00:00
|
|
|
EtsPid = case wterl_ets:start_link() of
|
|
|
|
{ok, Pid1} ->
|
|
|
|
Pid1;
|
|
|
|
{error, {already_started, Pid1}} ->
|
|
|
|
Pid1
|
|
|
|
end,
|
|
|
|
MyPid = case start_link() of
|
|
|
|
{ok, Pid2} ->
|
|
|
|
Pid2;
|
|
|
|
{error, {already_started, Pid2}} ->
|
|
|
|
Pid2
|
|
|
|
end,
|
|
|
|
{EtsPid, MyPid}
|
2012-02-28 03:00:02 +00:00
|
|
|
end,
|
|
|
|
fun(_) ->
|
2012-04-05 20:10:50 +00:00
|
|
|
stop(),
|
|
|
|
wterl_ets:stop()
|
2012-02-28 03:00:02 +00:00
|
|
|
end,
|
|
|
|
fun(_) ->
|
|
|
|
{inorder,
|
|
|
|
[{"open one connection",
|
|
|
|
fun open_one/0},
|
|
|
|
{"open two connections",
|
|
|
|
fun open_two/0},
|
|
|
|
{"open two connections, kill one",
|
|
|
|
fun kill_one/0}
|
|
|
|
]}
|
|
|
|
end}]}.
|
|
|
|
|
|
|
|
open_one() ->
|
2013-03-18 19:32:03 +00:00
|
|
|
{ok, Ref} = open("test/wterl-backend", [{create, true}, {session_max, 20},{cache_size, "1MB"}]),
|
2012-02-28 03:00:02 +00:00
|
|
|
true = is_open(),
|
2012-03-12 00:18:59 +00:00
|
|
|
close(Ref),
|
2012-02-28 03:00:02 +00:00
|
|
|
false = is_open(),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
open_and_wait(Pid) ->
|
2013-03-18 19:32:03 +00:00
|
|
|
{ok, Ref} = open("test/wterl-backend", [{create, true}]),
|
2012-02-28 03:00:02 +00:00
|
|
|
Pid ! open,
|
|
|
|
receive
|
|
|
|
close ->
|
2012-03-12 00:18:59 +00:00
|
|
|
close(Ref),
|
2012-02-28 03:00:02 +00:00
|
|
|
Pid ! close;
|
|
|
|
exit ->
|
|
|
|
exit(normal)
|
|
|
|
end.
|
|
|
|
|
|
|
|
open_two() ->
|
|
|
|
Self = self(),
|
|
|
|
Pid1 = spawn_link(fun() -> open_and_wait(Self) end),
|
|
|
|
receive
|
|
|
|
open ->
|
|
|
|
true = is_open()
|
|
|
|
end,
|
|
|
|
Pid2 = spawn_link(fun() -> open_and_wait(Self) end),
|
|
|
|
receive
|
|
|
|
open ->
|
|
|
|
true = is_open()
|
|
|
|
end,
|
|
|
|
Pid1 ! close,
|
|
|
|
receive
|
|
|
|
close ->
|
|
|
|
true = is_open()
|
|
|
|
end,
|
|
|
|
Pid2 ! close,
|
|
|
|
receive
|
|
|
|
close ->
|
|
|
|
false = is_open()
|
|
|
|
end,
|
|
|
|
ok.
|
|
|
|
|
|
|
|
kill_one() ->
|
|
|
|
Self = self(),
|
|
|
|
Pid1 = spawn_link(fun() -> open_and_wait(Self) end),
|
|
|
|
receive
|
|
|
|
open ->
|
|
|
|
true = is_open()
|
|
|
|
end,
|
|
|
|
Pid2 = spawn_link(fun() -> open_and_wait(Self) end),
|
|
|
|
receive
|
|
|
|
open ->
|
|
|
|
true = is_open()
|
|
|
|
end,
|
|
|
|
Pid1 ! exit,
|
|
|
|
timer:sleep(100),
|
|
|
|
true = is_open(),
|
|
|
|
Pid2 ! close,
|
|
|
|
receive
|
|
|
|
close ->
|
|
|
|
false = is_open()
|
|
|
|
end,
|
|
|
|
ok.
|
2012-02-17 05:42:22 +00:00
|
|
|
|
|
|
|
-endif.
|