1%%
2%%% Copyright 2011, Boundary
3%%%
4%%% Licensed under the Apache License, Version 2.0 (the "License");
5%%% you may not use this file except in compliance with the License.
6%%% You may obtain a copy of the License at
7%%%
8%%%     http://www.apache.org/licenses/LICENSE-2.0
9%%%
10%%% Unless required by applicable law or agreed to in writing, software
11%%% distributed under the License is distributed on an "AS IS" BASIS,
12%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13%%% See the License for the specific language governing permissions and
14%%% limitations under the License.
15%%%
16
17
18%%%-------------------------------------------------------------------
19%%% File:      folsom_sup.erl
20%%% @author    joe williams <j@boundary.com>
21%%% @doc
22%%%
23%%% @end
24%%%-------------------------------------------------------------------
25
26-module(folsom_sup).
27
28-behaviour(supervisor).
29
30%% API
31-export([start_link/0, create_tables/0]).
32
33%% Supervisor callbacks
34-export([init/1]).
35
36-include("folsom.hrl").
37
38-define(SERVER, ?MODULE).
39
40%%%===================================================================
41%%% API functions
42%%%===================================================================
43
44%%--------------------------------------------------------------------
45%% @doc
46%% Starts the supervisor
47%%
48%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
49%% @end
50%%--------------------------------------------------------------------
51start_link() ->
52    supervisor:start_link({local, ?SERVER}, ?MODULE, []).
53
54%%%===================================================================
55%%% Supervisor callbacks
56%%%===================================================================
57
58%%--------------------------------------------------------------------
59%% @private
60%% @doc
61%% Whenever a supervisor is started using supervisor:start_link/[2,3],
62%% this function is called by the new process to find out about
63%% restart strategy, maximum restart frequency and child
64%% specifications.
65%%
66%% @spec init(Args) -> {ok, {SupFlags, [ChildSpec]}} |
67%%                     ignore |
68%%                     {error, Reason}
69%% @end
70%%--------------------------------------------------------------------
71init([]) ->
72    create_tables(),
73
74    RestartStrategy = one_for_one,
75    MaxRestarts = 1000,
76    MaxSecondsBetweenRestarts = 3600,
77
78    SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},
79
80    Restart = permanent,
81    Shutdown = 2000,
82    Type = worker,
83
84    TimerServer = {folsom_meter_timer_server,
85              {folsom_meter_timer_server, start_link, []},
86              Restart, Shutdown, Type, [folsom_meter_timer_server]},
87
88    HistETSServer = {folsom_metrics_histogram_ets,
89              {folsom_metrics_histogram_ets, start_link, []},
90              Restart, Shutdown, Type, [folsom_metrics_histogram_ets]},
91
92    SlideSup = {folsom_sample_slide_sup, {folsom_sample_slide_sup, start_link, []},
93                permanent, 5000, supervisor, [folsom_sample_slide_sup]},
94
95    {ok, {SupFlags, [SlideSup, TimerServer, HistETSServer]}}.
96
97%%%===================================================================
98%%% Internal functions
99%%%===================================================================
100
101create_tables() ->
102    Tables = [
103              {?FOLSOM_TABLE, [set, named_table, public, {read_concurrency, true}]},
104              {?COUNTER_TABLE, [set, named_table, public, {write_concurrency, true}]},
105              {?GAUGE_TABLE, [set, named_table, public, {write_concurrency, true}]},
106              {?HISTOGRAM_TABLE, [set, named_table, public, {write_concurrency, true}]},
107              {?METER_TABLE, [set, named_table, public, {write_concurrency, true}]},
108              {?METER_READER_TABLE, [set, named_table, public, {write_concurrency, true}]},
109              {?HISTORY_TABLE, [set, named_table, public, {write_concurrency, true}]},
110              {?DURATION_TABLE, [ordered_set, named_table, public, {write_concurrency, true}]},
111              {?SPIRAL_TABLE, [set, named_table, public, {write_concurrency, true}]}
112             ],
113    [maybe_create_table(ets:info(Name), Name, Opts) || {Name, Opts} <- Tables],
114    ok.
115
116maybe_create_table(undefined, Name, Opts) ->
117    ets:new(Name, Opts);
118maybe_create_table(_, _, _) ->
119    ok.
120