Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation/release #1

Merged
merged 2 commits into from
May 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.beam
ebin/sidejob.app
Binary file added rebar
Binary file not shown.
5 changes: 5 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{erl_opts, [debug_info, warnings_as_errors]}.
{cover_enabled, true}.
{eunit_opts, [verbose]}.
{edoc_opts, [{preprocess, true}]}.
{deps, []}.
13 changes: 13 additions & 0 deletions src/sidejob.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{application, sidejob,
[
{description, ""},
{vsn, "0.0.1"},
{registered, []},
{applications, [
kernel,
stdlib
]},
{registered, []},
{mod, {sidejob_app, []}},
{env, []}
]}.
132 changes: 132 additions & 0 deletions src/sidejob.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2013 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(sidejob).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module definitely needs some good tests, dare I say eve EQC based? It's too important.

-export([new_resource/3, new_resource/4, call/2, call/3, cast/2]).

%%%===================================================================
%%% API
%%%===================================================================

%% @doc
%% Create a new sidejob resource that uses the provided worker module,
%% enforces the requested usage limit, and is managed by the specified
%% number of worker processes.
%%
%% This call will generate and load a new module, via {@link sidejob_config},
%% that provides information about the new resource. It will also start up the
%% supervision hierarchy that manages this resource: ensuring that the workers
%% and stats aggregation server for this resource remain running.
new_resource(Name, Mod, Limit, Workers) ->
ETS = sidejob_resource_sup:ets(Name),
StatsETS = sidejob_resource_sup:stats_ets(Name),
WorkerNames = sidejob_worker:workers(Name, Workers),
StatsName = sidejob_resource_stats:reg_name(Name),
WorkerLimit = Limit div Workers,
sidejob_config:load_config(Name, [{width, Workers},
{limit, Limit},
{worker_limit, WorkerLimit},
{ets, ETS},
{stats_ets, StatsETS},
{workers, list_to_tuple(WorkerNames)},
{stats, StatsName}]),
sidejob_sup:add_resource(Name, Mod).

%% @doc
%% Same as {@link new_resource/4} except that the number of workers defaults
%% to the number of scheduler threads.
new_resource(Name, Mod, Limit) ->
Workers = erlang:system_info(schedulers),
new_resource(Name, Mod, Limit, Workers).


%% @doc
%% Same as {@link call/3} with a default timeout of 5 seconds.
call(Name, Msg) ->
call(Name, Msg, 5000).

%% @doc
%% Perform a synchronous call to the specified resource, failing if the
%% resource has reached its usage limit.
call(Name, Msg, Timeout) ->
case available(Name) of
none ->
overload;
Worker ->
gen_server:call(Worker, Msg, Timeout)
end.

%% @doc
%% Perform an asynchronous cast to the specified resource, failing if the
%% resource has reached its usage limit.
cast(Name, Msg) ->
case available(Name) of
none ->
overload;
Worker ->
gen_server:cast(Worker, Msg)
end.

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

%% Find an available worker or return none if all workers at limit
available(Name) ->
ETS = Name:ets(),
Width = Name:width(),
Limit = Name:worker_limit(),
Scheduler = erlang:system_info(scheduler_id),
Worker = Scheduler rem Width,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, specifying a number of workers > erlang:system_info(schedulers) is useless, right? I see we default to number of schedulers in our use anyway, but maybe issuing a warning would be good.

case is_available(ETS, Limit, Worker) of
true ->
worker_reg_name(Name, Worker);
false ->
available(Name, ETS, Width, Limit, Worker+1, Worker)
end.

available(Name, _ETS, _Width, _Limit, End, End) ->
ets:update_counter(Name:stats_ets(), rejected, 1),
none;
available(Name, ETS, Width, Limit, X, End) ->
Worker = X rem Width,
case is_available(ETS, Limit, Worker) of
false ->
available(Name, ETS, Width, Limit, Worker+1, End);
true ->
worker_reg_name(Name, Worker)
end.

is_available(ETS, Limit, Worker) ->
case ets:lookup_element(ETS, {full, Worker}, 2) of
1 ->
false;
0 ->
Value = ets:update_counter(ETS, Worker, 1),
case Value >= Limit of
true ->
ets:insert(ETS, {{full, Worker}, 1}),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we are updating the full flag for a worker both in the worker after each message is processed and by the client when an available worker is found. That is so unresponsive workers can be flagged as full when enough incoming requests are queued. Once the worker processes one more request, they will update their usage and full flag with the right values. I tried to think of anything racy here, but it looks good. Sorry, this is not a question/issue, just a ramble :)

false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, the worker gets full after incrementing. But it wasn't full to begin with, so this request should return true anyway. This guy just took the last slot. The next request should return false. I totally found that by myself. It's not like like it was Quickcheck and I had nothing to do with it. Not at all. :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For any other readers: discussed with Joe and there is a race condition here, but we believe the only effect would be to occasionally go over the limit with enough schedulers. Clients check and increment counter here, worker updates it to its actual mailbox size on processing a message. Messages still not placed in the mailbox are unaccounted for.

false ->
true
end
end.

worker_reg_name(Name, Id) ->
element(Id+1, Name:workers()).
48 changes: 48 additions & 0 deletions src/sidejob_app.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2013 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(sidejob_app).
-behaviour(application).

%% Application callbacks
-export([start/2, start_phase/3, prep_stop/1, stop/1, config_change/3]).

%%%===================================================================
%%% Application callbacks
%%%===================================================================

start(_Type, _StartArgs) ->
case sidejob_sup:start_link() of
{ok, Pid} ->
{ok, Pid};
Error ->
Error
end.

stop(_State) ->
ok.

start_phase(_Phase, _Type, _PhaseArgs) ->
ok.

prep_stop(State) ->
State.

config_change(_Changed, _New, _Removed) ->
ok.
66 changes: 66 additions & 0 deletions src/sidejob_config.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2013 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.
%%
%% -------------------------------------------------------------------

%% @doc
%% Utility that converts a given property list into a module that provides
%% constant time access to the various key/value pairs.
%%
%% Example:
%% load_config(test, [{limit, 1000},
%% {num_workers, 4},
%% {workers, [{test_1, test_2, test_3, test_4}]}]).
%%
%% creates the module `test' such that:
%% test:limit(). => 1000
%% test:num_workers(). => 16
%% test:workers(). => [{test_1, test_2, test_3, test_4}]}]
%%
-module(sidejob_config).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module is a perfect candidate for some unit tests.

-export([load_config/2]).

load_config(Resource, Config) ->
Module = make_module(Resource),
Exports = [make_export(Key) || {Key, _} <- Config],
Functions = [make_function(Key, Value) || {Key, Value} <- Config],
ExportAttr = make_export_attribute(Exports),
Abstract = [Module, ExportAttr | Functions],
Forms = erl_syntax:revert_forms(Abstract),
{ok, Resource, Bin} = compile:forms(Forms, [verbose, report_errors]),
code:purge(Resource),
{module, Resource} = code:load_binary(Resource,
atom_to_list(Resource) ++ ".erl",
Bin),
ok.

make_module(Module) ->
erl_syntax:attribute(erl_syntax:atom(module),
[erl_syntax:atom(Module)]).

make_export(Key) ->
erl_syntax:arity_qualifier(erl_syntax:atom(Key),
erl_syntax:integer(0)).

make_export_attribute(Exports) ->
erl_syntax:attribute(erl_syntax:atom(export),
[erl_syntax:list(Exports)]).

make_function(Key, Value) ->
Constant = erl_syntax:clause([], none, [erl_syntax:abstract(Value)]),
erl_syntax:function(erl_syntax:atom(Key), [Constant]).
Loading