summaryrefslogtreecommitdiff
path: root/src/bootstrap_receiver.erl
blob: 3b4907cb08c8db7a145fba4187867aba6f0493aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
%%%-------------------------------------------------------------------
%%% File:      bootstrap_receiver.erl
%%% @author    Brad Anderson <brad@cloudant.com>
%%% @copyright 2009 Brad Anderson
%%% @doc
%%%
%%% @end
%%%
%%% @since 2009-09-22 by Brad Anderson
%%%-------------------------------------------------------------------
-module(bootstrap_receiver).
-author('brad@cloudant.com').

-include("../include/config.hrl").
-include("../include/common.hrl").

%% API
-export([start_link/6, loop/6, fetch_shard/5]).


%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% @spec
%% @doc
%% @end
%%--------------------------------------------------------------------
start_link(FromNode, ToNode, Partition, DbName, Timeout, Manager) ->
    Pid = proc_lib:spawn_link(ToNode, bootstrap_receiver, loop,
                              [FromNode, Partition, DbName, Timeout, Manager,
                               self()]),
    sync_wait(Pid, Timeout).


loop(FromNode, Partition, DbName, Timeout, Manager, Parent) ->
    proc_lib:init_ack(Parent, {ok, self()}),
    fetch_shard(FromNode, Partition, DbName, Timeout, Manager).


%% @doc run at "ToNode" via spawn_link
fetch_shard(FromNode, Partition, DbName, Timeout, Manager) ->
    Directory = couch_config:get("couchdb", "database_dir"),
    [_NodeName, Hostname] = string:tokens(atom_to_list(FromNode), "@"),
    SrcFile = binary_to_list(partitions:shard_name(Partition, DbName)),
    DestFile = showroom_utils:full_filename(Partition, DbName, Directory),
    Authn = fetch_authn(),
    Port = fetch_port(),
    Url = lists:concat(["http://", Authn, Hostname, Port, "/", SrcFile,
                        ".couch"]),
    Options = [{save_response_to_file, DestFile},
               {inactivity_timeout, Timeout}],
    case filelib:ensure_dir(DestFile) of
    ok -> ok;
    {error, eexist} -> ok; % duh!
    Other -> throw(Other)
    end,
    ?LOG_DEBUG("~n"
               "Directory: ~p~n"
               "Hostname : ~p~n"
               "SrcFile  : ~p~n"
               "DestFile : ~p~n"
               "Url      : ~p~n"
               "Options  : ~p~n"
               , [Directory, Hostname, SrcFile, DestFile, Url, Options]),
    case ibrowse:send_req(Url, [], get, [], Options, infinity) of
        {ok, "200", _Headers, Body} ->
            ?LOG_DEBUG("~nBootstrap ibrowse req Body: ~p~n", [Body]),
            Manager ! {receiver_done, FromNode, node(), Partition, DbName,
                       self()};
        Error ->
            ?LOG_ERROR("~nBootstrap ibrowse req Error: ~p~n", [Error]),
            throw(Error)
    end.


%%====================================================================
%% Internal functions
%%====================================================================


%% from proc_lib.erl in otp r13b01
sync_wait(Pid, Timeout) ->
    receive
	{ack, Pid, Return} ->
	    Return;
	{'EXIT', Pid, Reason} ->
	    {error, Reason}
    after Timeout ->
	    unlink(Pid),
	    exit(Pid, kill),
	    flush(Pid),
	    {error, timeout}
    end.


flush(Pid) ->
    receive
	{'EXIT', Pid, _} ->
	    true
    after 0 ->
	    true
    end.


fetch_authn() ->
    User = couch_config:get("shard_moving", "user", ""),
    Pass = couch_config:get("shard_moving", "pass", ""),
    if
    length(User) > 0 andalso length(Pass) > 0 ->
            lists:concat([User, ":", Pass, "@"]);
    true -> ""
    end.


fetch_port() ->
    Port = couch_config:get("shard_moving", "port", "8080"),
    if
    Port =:= "80" -> "";
    true -> lists:concat([":", Port])
    end.