52BlxIPBaHKVVYl95adzLA changeset

Changeset353530393535 (b)
ParentNone (a)
ab
0+%%%-------------------------------------------------------------------
0+%% Reproduces CLOSE-WAIT conditions in CouchBeam.
0+%% Observe in a terminal
0+%%  > watch -n 1 "ss -p -o state close-wait '( dport = 5984 )' | wc -l "
0+%%
0+%% Run couchbed:run() to reproduce the CLOSE-WAIT condition.
0+%% The condition appears after about 60 secondes, one CLOSE-WAIT socket
0+%% per killed couchbeam_view_stream process.
0+%%%-------------------------------------------------------------------
0+
0+-module(couchbed).
0+-export([run/0, start_client/0, start/0, get/0, generate_and_store_docs/2, create_view/1, query_view/2]).
0+
0+
0+-define(DB_NAME, "close_wait").
0+-define(VIEW, {"design", "view"}).
0+-define(COUCH_URL, "http://localhost:5984").
0+-define(OPTIONS, [{basic_auth, {"admin", "admin"}}]).
0+
0+-define(MAX, 50).
0+-spec run() -> ok.
0+run() ->
0+    start(),
0+    Db = ?MODULE:get(),
0+    io:format("~p~n", [Db]),
0+    %generate_and_store_docs(Db, 50),
0+    Pid = start_client(),
0+    create_view(Db),
0+    query_view(Db, Pid),
0+    io:format("~p~n", [hackney_pool:get_stats(default)]),
0+    ok.
0+
0+
0+-spec query_view(couchbeam:db(), pid()) -> ok.
0+query_view(Db, Pid) ->
0+    lists:foreach(fun(_N) ->
0+        {ok, _Ref } = couchbeam_view:stream(Db, ?VIEW, [{stream_to, Pid}]),
0+        ok
0+    end, lists:seq(1, ?MAX)),
0+    kill_view_stream_process(),
0+    ok.
0+
0+-spec start_client() -> pid().
0+start_client() ->
0+    Pid = spawn(fun() -> loop() end),
0+    Pid.
0+
0+-spec loop() -> no_return().
0+loop() ->
0+    receive
0+        _Message ->
0+            case rand:uniform(400) of
0+            1 ->
0+                io:format("~p~n", [hackney_pool:get_stats(default)]),
0+                loop();
0+            _ ->
0+                loop()
0+            end
0+    end.
0+
0+-spec get() -> couchbeam:db().
0+get() ->
0+    S = couchbeam:server_connection(?COUCH_URL, ?OPTIONS),
0+    DbName =  uri_string:quote(?DB_NAME),
0+    {ok, Db} = case couchbeam:db_exists(S,DbName) of
0+        true -> couchbeam:open_db(S, DbName, []);
0+        false -> couchbeam:create_db(S,DbName),
0+                    couchbeam:open_db(S, DbName, [])
0+    end,
0+
0+    Db.
0+
0+
0+-spec kill_view_stream_process() -> ok.
0+kill_view_stream_process() ->
0+    killall(ets:tab2list(couchbeam_view_streams)).
0+
0+-spec killall(list()) -> ok.
0+killall([]) -> ok;
0+killall([{_Ref, Pid} | Pids]) ->
0+        % kill 10% of
0+        case rand:uniform(10) of
0+        1 ->
0+          io:format("killing ~p~n", [Pid]),
0+          exit(Pid, kill);
0+        _ ->
0+          ok
0+        end,
0+    killall(Pids).
0+
0+-spec generate_and_store_docs(couchbeam:db(), non_neg_integer()) -> ok.
0+generate_and_store_docs(Db, N) ->
0+    io:format("creating ~p docs~n", [N]),
0+    lists:foreach(fun(_) ->
0+        Doc = {[{<<"type">>,<<"random_doc">>}, {<<"value">>, rand:uniform(1000)}]},
0+        io:format("."),
0+        {ok, _Doc} = couchbeam:save_doc(Db, Doc)
0+    end, lists:seq(1, N)),
0+    io:format("~n"),
0+    ok.
0+
0+-spec create_view(couchbeam:db()) -> ok.
0+create_view(Db) ->
0+    {Design, View} = ?VIEW,
0+    create_view(Db, list_to_binary(Design), list_to_binary(View)).
0+
0+-spec create_view(couchbeam:db(), binary(), binary()) -> couchbeam:document().
0+create_view(Db, Design, View) ->
0+    DesignDoc = {[
0+        {<<"_id">>, <<"_design/",Design/binary>>},
0+        {<<"language">>,<<"javascript">>},
0+        {<<"views">>,
0+            {[{View,
0+                {[{<<"map">>,
0+                    <<"function (doc) {\n emit(doc._id, doc); \n}">>
0+                }]}
0+            }]}
0+        }
0+    ]},
0+    case couchbeam:save_doc(Db, DesignDoc) of
0+        {ok, DesignDoc1} ->
0+            io:format("Created view"),
0+            DesignDoc1;
0+        {error, conflict} ->
0+            io:format("View already exists"),
0+            ok;
0+        {error, Reason} ->
0+            io:format("Error creating view: ~p~n", [Reason]),
0+            error
0+    end.
0+
0+-spec start() -> ok.
0+start() ->
0+    application:start(inets),
0+    application:ensure_all_started(couchbeam),
0+    io:format("Everything started~n").
...
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
--- Revision None
+++ Revision 353530393535
@@ -0,0 +1,136 @@
+%%%-------------------------------------------------------------------
+%% Reproduces CLOSE-WAIT conditions in CouchBeam.
+%% Observe in a terminal
+%% > watch -n 1 "ss -p -o state close-wait '( dport = 5984 )' | wc -l "
+%%
+%% Run couchbed:run() to reproduce the CLOSE-WAIT condition.
+%% The condition appears after about 60 secondes, one CLOSE-WAIT socket
+%% per killed couchbeam_view_stream process.
+%%%-------------------------------------------------------------------
+
+-module(couchbed).
+-export([run/0, start_client/0, start/0, get/0, generate_and_store_docs/2, create_view/1, query_view/2]).
+
+
+-define(DB_NAME, "close_wait").
+-define(VIEW, {"design", "view"}).
+-define(COUCH_URL, "http://localhost:5984").
+-define(OPTIONS, [{basic_auth, {"admin", "admin"}}]).
+
+-define(MAX, 50).
+-spec run() -> ok.
+run() ->
+ start(),
+ Db = ?MODULE:get(),
+ io:format("~p~n", [Db]),
+ %generate_and_store_docs(Db, 50),
+ Pid = start_client(),
+ create_view(Db),
+ query_view(Db, Pid),
+ io:format("~p~n", [hackney_pool:get_stats(default)]),
+ ok.
+
+
+-spec query_view(couchbeam:db(), pid()) -> ok.
+query_view(Db, Pid) ->
+ lists:foreach(fun(_N) ->
+ {ok, _Ref } = couchbeam_view:stream(Db, ?VIEW, [{stream_to, Pid}]),
+ ok
+ end, lists:seq(1, ?MAX)),
+ kill_view_stream_process(),
+ ok.
+
+-spec start_client() -> pid().
+start_client() ->
+ Pid = spawn(fun() -> loop() end),
+ Pid.
+
+-spec loop() -> no_return().
+loop() ->
+ receive
+ _Message ->
+ case rand:uniform(400) of
+ 1 ->
+ io:format("~p~n", [hackney_pool:get_stats(default)]),
+ loop();
+ _ ->
+ loop()
+ end
+ end.
+
+-spec get() -> couchbeam:db().
+get() ->
+ S = couchbeam:server_connection(?COUCH_URL, ?OPTIONS),
+ DbName = uri_string:quote(?DB_NAME),
+ {ok, Db} = case couchbeam:db_exists(S,DbName) of
+ true -> couchbeam:open_db(S, DbName, []);
+ false -> couchbeam:create_db(S,DbName),
+ couchbeam:open_db(S, DbName, [])
+ end,
+
+ Db.
+
+
+-spec kill_view_stream_process() -> ok.
+kill_view_stream_process() ->
+ killall(ets:tab2list(couchbeam_view_streams)).
+
+-spec killall(list()) -> ok.
+killall([]) -> ok;
+killall([{_Ref, Pid} | Pids]) ->
+ % kill 10% of
+ case rand:uniform(10) of
+ 1 ->
+ io:format("killing ~p~n", [Pid]),
+ exit(Pid, kill);
+ _ ->
+ ok
+ end,
+ killall(Pids).
+
+-spec generate_and_store_docs(couchbeam:db(), non_neg_integer()) -> ok.
+generate_and_store_docs(Db, N) ->
+ io:format("creating ~p docs~n", [N]),
+ lists:foreach(fun(_) ->
+ Doc = {[{<<"type">>,<<"random_doc">>}, {<<"value">>, rand:uniform(1000)}]},
+ io:format("."),
+ {ok, _Doc} = couchbeam:save_doc(Db, Doc)
+ end, lists:seq(1, N)),
+ io:format("~n"),
+ ok.
+
+-spec create_view(couchbeam:db()) -> ok.
+create_view(Db) ->
+ {Design, View} = ?VIEW,
+ create_view(Db, list_to_binary(Design), list_to_binary(View)).
+
+-spec create_view(couchbeam:db(), binary(), binary()) -> couchbeam:document().
+create_view(Db, Design, View) ->
+ DesignDoc = {[
+ {<<"_id">>, <<"_design/",Design/binary>>},
+ {<<"language">>,<<"javascript">>},
+ {<<"views">>,
+ {[{View,
+ {[{<<"map">>,
+ <<"function (doc) {\n emit(doc._id, doc); \n}">>
+ }]}
+ }]}
+ }
+ ]},
+ case couchbeam:save_doc(Db, DesignDoc) of
+ {ok, DesignDoc1} ->
+ io:format("Created view"),
+ DesignDoc1;
+ {error, conflict} ->
+ io:format("View already exists"),
+ ok;
+ {error, Reason} ->
+ io:format("Error creating view: ~p~n", [Reason]),
+ error
+ end.
+
+-spec start() -> ok.
+start() ->
+ application:start(inets),
+ application:ensure_all_started(couchbeam),
+ io:format("Everything started~n").