Skip to content
Browse files

reformated

  • Loading branch information...
1 parent ecce7ba commit c8d762551c0471a470fd4e20a680ae3d8be032da @leonlee committed
View
30 apps/flake/src/flake.app.src
@@ -1,16 +1,16 @@
{application, flake,
- [
- {description, "flake"},
- {vsn, "0.7"},
- {modules, []},
- {registered, []},
- {applications, [
- kernel,
- stdlib,
- sasl,
- inets,
- crypto
- ]},
- {mod, {flake_app, []}},
- {env, [{cfg_node, 'cfg_node@localhost'}]}
- ]}.
+ [
+ {description, "flake"},
+ {vsn, "0.7"},
+ {modules, []},
+ {registered, []},
+ {applications, [
+ kernel,
+ stdlib,
+ sasl,
+ inets,
+ crypto
+ ]},
+ {mod, {flake_app, []}},
+ {env, [{cfg_node, 'cfg_node@localhost'}]}
+ ]}.
View
8 apps/flake/src/flake.erl
@@ -33,13 +33,13 @@
%% @spec start() -> ok
%% @doc Start the snowflake server.
start() ->
- appstart:start(flake, permanent).
+ appstart:start(flake, permanent).
%% @spec stop() -> ok
%% @doc Stop the snowflake server.
stop() ->
- Res = application:stop(flake),
- Res.
+ Res = application:stop(flake),
+ Res.
get_config_id() ->
- "flake." ++ atom_to_list(node()).
+ "flake." ++ atom_to_list(node()).
View
8 apps/flake/src/flake.hrl
@@ -5,11 +5,11 @@
-define(LOG_ERROR_FORMAT(F, A), _ = [F, A]).
-else.
-define(LOG_INFO(F),
- error_logger:info_msg(F)).
+ error_logger:info_msg(F)).
-define(LOG_ERROR(F),
- error_logger:error_msg(F)).
+ error_logger:error_msg(F)).
-define(LOG_INFO_FORMAT(F, A),
- error_logger:info_msg(F, A)).
+ error_logger:info_msg(F, A)).
-define(LOG_ERROR_FORMAT(F, A),
- error_logger:error_msg(F, A)).
+ error_logger:error_msg(F, A)).
-endif.
View
10 apps/flake/src/flake_app.erl
@@ -22,8 +22,8 @@
-behaviour(application).
-export([
- start/2,
- stop/1
+ start/2,
+ stop/1
]).
-include_lib("eunit/include/eunit.hrl").
@@ -31,10 +31,10 @@
%% @spec start(_Type, _StartArgs) -> ServerRet
%% @doc application start callback for snowflake.
start(_Type, _StartArgs) ->
- ok = xor_util:connect_cfg_node(flake, cfg_node),
- flake_sup:start_link().
+ ok = xor_util:connect_cfg_node(flake, cfg_node),
+ flake_sup:start_link().
%% @spec stop(_State) -> ServerRet
%% @doc application stop callback for snowflake.
stop(_State) ->
- ok.
+ ok.
View
40 apps/flake/src/flake_harness.erl
@@ -14,37 +14,37 @@
%%% limitations under the License.
%%%
--module (flake_harness).
--author ('Dietrich Featherston <d@boundary.com>').
+-module(flake_harness).
+-author('Dietrich Featherston <d@boundary.com>').
--export ([
- generate/1,
- generate/2,
- timed_generate/1,
- timed_generate/2
- ]).
+-export([
+ generate/1,
+ generate/2,
+ timed_generate/1,
+ timed_generate/2
+]).
-include_lib("eunit/include/eunit.hrl").
generate(N) ->
- generate_ids(N, undefined, []).
+ generate_ids(N, undefined, []).
timed_generate(N) ->
- ?debugTime("generating ids", generate(N)).
+ ?debugTime("generating ids", generate(N)).
generate(N, Base) ->
- generate_ids(N, Base, []).
+ generate_ids(N, Base, []).
timed_generate(N, Base) ->
- ?debugTime("generating ids", generate(N, Base)).
+ ?debugTime("generating ids", generate(N, Base)).
generate_ids(0, _Base, Acc) ->
- Acc;
+ Acc;
generate_ids(N, Base, Acc) ->
- {ok, Flake} = case Base of
- undefined ->
- flake_server:id();
- _ ->
- flake_server:id(Base)
- end,
- generate_ids(N-1, Base, [Flake|Acc]).
+ {ok, Flake} = case Base of
+ undefined ->
+ flake_server:id();
+ _ ->
+ flake_server:id(Base)
+ end,
+ generate_ids(N - 1, Base, [Flake | Acc]).
View
72 apps/flake/src/flake_server.erl
@@ -21,19 +21,19 @@
%% API
-export([
- start_link/1,
- id/0,
- id/1,
- id_in/2, id_in/1]).
+ start_link/1,
+ id/0,
+ id/1,
+ id_in/2, id_in/1]).
%% gen_server callbacks
-export([
- init/1,
- handle_call/3,
- handle_cast/2,
- handle_info/2,
- terminate/2,
- code_change/3
+ init/1,
+ handle_call/3,
+ handle_cast/2,
+ handle_info/2,
+ terminate/2,
+ code_change/3
]).
-include_lib("eunit/include/eunit.hrl").
@@ -46,50 +46,50 @@
% start and link to a new flake id generator
start_link(Config) ->
- Name = xor_pl:read(name, Config),
- gen_server:start_link({local, Name}, ?MODULE, Config, []).
+ Name = xor_pl:read(name, Config),
+ gen_server:start_link({local, Name}, ?MODULE, Config, []).
% generate a new snowflake id
id() ->
- id(0).
+ id(0).
id(ZoneId) ->
- respond(gen_server:call(flake, {get, ZoneId})).
+ respond(gen_server:call(flake, {get, ZoneId})).
id_in(Base) ->
- id_in(0, Base).
+ id_in(0, Base).
id_in(ZoneId, Base) ->
- respond(gen_server:call(flake, {get, ZoneId, Base})).
+ respond(gen_server:call(flake, {get, ZoneId, Base})).
respond({ok, Flake}) ->
- {ok, Flake};
+ {ok, Flake};
respond(X) ->
- X.
+ X.
%% ----------------------------------------------------------
%% gen_server callbacks
%% ----------------------------------------------------------
init(Config) ->
- WorkerId = xor_pl:read(worker_id, Config),
- {ok, #state{max_time = flake_util:curr_time_millis(), worker_id = WorkerId, sequence = 0}}.
+ WorkerId = xor_pl:read(worker_id, Config),
+ {ok, #state{max_time = flake_util:curr_time_millis(), worker_id = WorkerId, sequence = 0}}.
handle_call({get, ZoneId}, _From, State = #state{max_time = MaxTime, worker_id = WorkerId, sequence = Sequence}) ->
- {Resp, S0} = get(flake_util:curr_time_millis(), MaxTime, ZoneId, WorkerId, Sequence, State),
- {reply, Resp, S0};
+ {Resp, S0} = get(flake_util:curr_time_millis(), MaxTime, ZoneId, WorkerId, Sequence, State),
+ {reply, Resp, S0};
handle_call({get, ZoneId, Base}, _From,
- State = #state{max_time = MaxTime, worker_id = WorkerId, sequence = Sequence}) ->
- {Resp, S0} = get(flake_util:curr_time_millis(), MaxTime, ZoneId, WorkerId, Sequence, State),
- case Resp of
- {ok, Id} ->
- {reply, {ok, xor_util:to_base(Id, Base)}, S0};
- E ->
- {reply, E, S0}
- end;
+ State = #state{max_time = MaxTime, worker_id = WorkerId, sequence = Sequence}) ->
+ {Resp, S0} = get(flake_util:curr_time_millis(), MaxTime, ZoneId, WorkerId, Sequence, State),
+ case Resp of
+ {ok, Id} ->
+ {reply, {ok, xor_util:to_base(Id, Base)}, S0};
+ E ->
+ {reply, E, S0}
+ end;
handle_call(X, _From, State) ->
- error_logger:error_msg("unrecognized msg in ~p:handle_call -> ~p~n", [?MODULE, X]),
- {reply, ok, State}.
+ error_logger:error_msg("unrecognized msg in ~p:handle_call -> ~p~n", [?MODULE, X]),
+ {reply, ok, State}.
handle_cast(_, State) -> {noreply, State}.
@@ -101,11 +101,11 @@ code_change(_, State, _) -> {ok, State}.
%% clock hasn't moved, increment sequence
get(Time, Time, ZoneId, WorkerId, Seq0, State) ->
- Sequence = Seq0 + 1,
- {{ok, flake_util:gen_id(Time, ZoneId, WorkerId, Sequence)}, State#state{sequence = Sequence}};
+ Sequence = Seq0 + 1,
+ {{ok, flake_util:gen_id(Time, ZoneId, WorkerId, Sequence)}, State#state{sequence = Sequence}};
%% clock has progressed, reset sequence
get(CurrTime, MaxTime, ZoneId, WorkerId, _, State) when CurrTime > MaxTime ->
- {{ok, flake_util:gen_id(CurrTime, ZoneId, WorkerId, 0)}, State#state{max_time = CurrTime, sequence = 0}};
+ {{ok, flake_util:gen_id(CurrTime, ZoneId, WorkerId, 0)}, State#state{max_time = CurrTime, sequence = 0}};
%% clock is running backwards
get(CurrTime, MaxTime, _ZoneId, _WorkerId, _Sequence, State) when MaxTime > CurrTime ->
- {{error, clock_running_backwards}, State}.
+ {{error, clock_running_backwards}, State}.
View
120 apps/flake/src/flake_sup.erl
@@ -33,81 +33,81 @@
%% @spec start_link() -> ServerRet
%% @doc API for starting the supervisor.
start_link() ->
- supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
%% @spec upgrade() -> ok
%% @doc Add processes if necessary.
upgrade() ->
- {ok, {_, Specs}} = init([]),
+ {ok, {_, Specs}} = init([]),
- Old = sets:from_list([Name || {Name, _, _, _} <- supervisor:which_children(?MODULE)]),
- New = sets:from_list([Name || {Name, _, _, _, _, _} <- Specs]),
- Kill = sets:subtract(Old, New),
+ Old = sets:from_list([Name || {Name, _, _, _} <- supervisor:which_children(?MODULE)]),
+ New = sets:from_list([Name || {Name, _, _, _, _, _} <- Specs]),
+ Kill = sets:subtract(Old, New),
- sets:fold(
- fun(Id, ok) ->
- supervisor:terminate_child(?MODULE, Id),
- supervisor:delete_child(?MODULE, Id),
- ok
- end, ok, Kill),
+ sets:fold(
+ fun(Id, ok) ->
+ supervisor:terminate_child(?MODULE, Id),
+ supervisor:delete_child(?MODULE, Id),
+ ok
+ end, ok, Kill),
- [supervisor:start_child(?MODULE, Spec) || Spec <- Specs],
- ok.
+ [supervisor:start_child(?MODULE, Spec) || Spec <- Specs],
+ ok.
%% @spec init([]) -> SupervisorTree
%% @doc supervisor callback.
init([]) ->
- If = cfgsrv:mc(flake, "interface", "eth0"),
- error_logger:info_msg("starting flake with hardware address of ~p as worker id~n", [If]),
- {ok, WorkerId} = flake_util:get_if_hw_int(If),
- error_logger:info_msg("using worker id: ~p~n", [WorkerId]),
- Name = cfgsrv:mc(flake, "name", "flake"),
- FlakeConfig = [
- {name, Name},
- {worker_id, WorkerId}
- ],
- Flake = {flake,
- {flake_server, start_link, [FlakeConfig]},
- permanent, 5000, worker, [flake_server]},
-
- TimestampPath = cfgsrv:mc(flake, "timestamp_path", "/tmp/flake-timestamp-dets"),
- AllowableDowntime = cfgsrv:mc(flake, "allowable_downtime", 0),
-
- {ok, TimestampTable} =
+ If = cfgsrv:mc(flake, "interface", "eth0"),
+ error_logger:info_msg("starting flake with hardware address of ~p as worker id~n", [If]),
+ {ok, WorkerId} = flake_util:get_if_hw_int(If),
+ error_logger:info_msg("using worker id: ~p~n", [WorkerId]),
+ Name = cfgsrv:mc(flake, "name", "flake"),
+ FlakeConfig = [
+ {name, Name},
+ {worker_id, WorkerId}
+ ],
+ Flake = {flake,
+ {flake_server, start_link, [FlakeConfig]},
+ permanent, 5000, worker, [flake_server]},
+
+ TimestampPath = cfgsrv:mc(flake, "timestamp_path", "/tmp/flake-timestamp-dets"),
+ AllowableDowntime = cfgsrv:mc(flake, "allowable_downtime", 0),
+
+ {ok, TimestampTable} =
dets:open_file(timestamp_table, [
- {estimated_no_objects, 10},
- {type, set},
- {file, TimestampPath}
+ {estimated_no_objects, 10},
+ {type, set},
+ {file, TimestampPath}
]),
- {ok, TS} = persistent_timer:read_timestamp(TimestampTable),
- ?debugVal(TS),
- Now = flake_util:curr_time_millis(),
- ?debugVal(Now),
- TimeSinceLastRun = Now - TS,
-
- %% fail startup if
- %% 1) the clock time last recorded is later than the current time
- %% 2) the last recorded time is more than N ms in the past to prevent
- %% generating future ids in the event that the system clock is set to some point far in the future
- check_for_clock_error(Now >= TS, TimeSinceLastRun < AllowableDowntime),
-
- error_logger:info_msg("saving timestamps to ~p every 1s~n", [TimestampPath]),
- TimerConfig = [
- {table, TimestampTable},
- {interval, 1000}
- ],
- PersistentTimer = {persistent_timer,
- {persistent_timer, start_link, [TimerConfig]},
- permanent, 5000, worker, [persistent_timer]},
-
- {ok, {{one_for_one, 10, 10}, [Flake, PersistentTimer]}}.
+ {ok, TS} = persistent_timer:read_timestamp(TimestampTable),
+ ?debugVal(TS),
+ Now = flake_util:curr_time_millis(),
+ ?debugVal(Now),
+ TimeSinceLastRun = Now - TS,
+
+ %% fail startup if
+ %% 1) the clock time last recorded is later than the current time
+ %% 2) the last recorded time is more than N ms in the past to prevent
+ %% generating future ids in the event that the system clock is set to some point far in the future
+ check_for_clock_error(Now >= TS, TimeSinceLastRun < AllowableDowntime),
+
+ error_logger:info_msg("saving timestamps to ~p every 1s~n", [TimestampPath]),
+ TimerConfig = [
+ {table, TimestampTable},
+ {interval, 1000}
+ ],
+ PersistentTimer = {persistent_timer,
+ {persistent_timer, start_link, [TimerConfig]},
+ permanent, 5000, worker, [persistent_timer]},
+
+ {ok, {{one_for_one, 10, 10}, [Flake, PersistentTimer]}}.
check_for_clock_error(true, true) ->
- ok;
+ ok;
check_for_clock_error(false, _) ->
- error_logger:error_msg("system running backwards, failing startup of snowflake service~n"),
- exit(clock_running_backwards);
+ error_logger:error_msg("system running backwards, failing startup of snowflake service~n"),
+ exit(clock_running_backwards);
check_for_clock_error(_, false) ->
- error_logger:error_msg("system clock too far advanced, failing startup of snowflake service~n"),
- exit(clock_advanced).
+ error_logger:error_msg("system clock too far advanced, failing startup of snowflake service~n"),
+ exit(clock_advanced).
View
62 apps/flake/src/flake_util.erl
@@ -18,10 +18,10 @@
-author('Dietrich Featherston <d@boundary.com>').
-export([
- get_if_hw_int/1,
- hw_addr_to_int/1,
- curr_time_millis/0,
- gen_id/4
+ get_if_hw_int/1,
+ hw_addr_to_int/1,
+ curr_time_millis/0,
+ gen_id/4
]).
-include_lib("eunit/include/eunit.hrl").
@@ -31,29 +31,29 @@
%% get the mac/hardware address of the given interface as a 48-bit integer
get_if_hw_int(undefined) ->
- {error, if_not_found};
+ {error, if_not_found};
get_if_hw_int(IfName) ->
- {ok, IfAddrs} = inet:getifaddrs(),
- IfProps = proplists:get_value(IfName, IfAddrs),
- case IfProps of
- undefined ->
- {error, if_not_found};
- _ ->
- HwAddr = proplists:get_value(hwaddr, IfProps),
- {ok, hw_addr_to_int(HwAddr)}
- end.
+ {ok, IfAddrs} = inet:getifaddrs(),
+ IfProps = proplists:get_value(IfName, IfAddrs),
+ case IfProps of
+ undefined ->
+ {error, if_not_found};
+ _ ->
+ HwAddr = proplists:get_value(hwaddr, IfProps),
+ {ok, hw_addr_to_int(HwAddr)}
+ end.
%% convert an array of 6 bytes into a 48-bit integer
hw_addr_to_int(HwAddr) ->
- <<WorkerId:48/integer>> = erlang:list_to_binary(HwAddr),
- WorkerId.
+ <<WorkerId:48/integer>> = erlang:list_to_binary(HwAddr),
+ WorkerId.
curr_time_millis() ->
- {MegaSec, Sec, MicroSec} = os:timestamp(),
- (1000000000 * MegaSec + Sec * 1000 + MicroSec div 1000) - ?TW_EPOCH.
+ {MegaSec, Sec, MicroSec} = os:timestamp(),
+ (1000000000 * MegaSec + Sec * 1000 + MicroSec div 1000) - ?TW_EPOCH.
gen_id(Time, ZoneId, WorkerId, Sequence) ->
- <<Time:48/integer, ZoneId:16/integer, WorkerId:48/integer, Sequence:16/integer>>.
+ <<Time:48/integer, ZoneId:16/integer, WorkerId:48/integer, Sequence:16/integer>>.
%% ----------------------------------------------------------
@@ -61,15 +61,15 @@ gen_id(Time, ZoneId, WorkerId, Sequence) ->
%% ----------------------------------------------------------
flake_test() ->
- TS = flake_util:curr_time_millis(),
- Worker = flake_util:hw_addr_to_int(lists:seq(1, 6)),
- ZoneId = 1,
- Flake = flake_util:gen_id(TS, ZoneId, Worker, 0),
- <<Time:48/integer, ZoneId:16/integer, WorkerId:48/integer, Sequence:16/integer>> = Flake,
- ?assert(?debugVal(Time) =:= TS),
- ?assert(?debugVal(ZoneId) =:= ZoneId),
- ?assert(?debugVal(Worker) =:= WorkerId),
- ?assert(?debugVal(Sequence) =:= 0),
- <<FlakeInt:128/integer>> = Flake,
- ?debugVal(xor_util:as_list(FlakeInt, 62)),
- ok.
+ TS = flake_util:curr_time_millis(),
+ Worker = flake_util:hw_addr_to_int(lists:seq(1, 6)),
+ ZoneId = 1,
+ Flake = flake_util:gen_id(TS, ZoneId, Worker, 0),
+ <<Time:48/integer, ZoneId:16/integer, WorkerId:48/integer, Sequence:16/integer>> = Flake,
+ ?assert(?debugVal(Time) =:= TS),
+ ?assert(?debugVal(ZoneId) =:= ZoneId),
+ ?assert(?debugVal(Worker) =:= WorkerId),
+ ?assert(?debugVal(Sequence) =:= 0),
+ <<FlakeInt:128/integer>> = Flake,
+ ?debugVal(xor_util:as_list(FlakeInt, 62)),
+ ok.
View
90 apps/flake/src/persistent_timer.erl
@@ -14,31 +14,31 @@
%%% limitations under the License.
%%%
--module (persistent_timer).
--author ('Dietrich Featherston <d@boundary.com>').
+-module(persistent_timer).
+-author('Dietrich Featherston <d@boundary.com>').
--behaviour (gen_server).
+-behaviour(gen_server).
--export ([
- start_link/1,
- write_timestamp/1,
- read_timestamp/1,
- get_last_timestamp/0
- ]).
+-export([
+ start_link/1,
+ write_timestamp/1,
+ read_timestamp/1,
+ get_last_timestamp/0
+]).
%% gen_server callbacks
--export ([
- init/1,
- handle_call/3,
- handle_cast/2,
- handle_info/2,
- terminate/2,
- code_change/3
- ]).
+-export([
+ init/1,
+ handle_call/3,
+ handle_cast/2,
+ handle_info/2,
+ terminate/2,
+ code_change/3
+]).
-include_lib("eunit/include/eunit.hrl").
--record (state, {table, timer}).
+-record(state, {table, timer}).
%% ----------------------------------------------------------
%% API
@@ -46,10 +46,10 @@
% start and link to a new flake id generator
start_link(Config) ->
- gen_server:start_link({local, ?MODULE}, ?MODULE, Config, []).
+ gen_server:start_link({local, ?MODULE}, ?MODULE, Config, []).
get_last_timestamp() ->
- gen_server:call(?MODULE, get_last_timestamp).
+ gen_server:call(?MODULE, get_last_timestamp).
%% ----------------------------------------------------------
@@ -57,17 +57,17 @@ get_last_timestamp() ->
%% ----------------------------------------------------------
init(Config) ->
- Table = proplists:get_value(table, Config),
- Interval = proplists:get_value(interval,Config, 1000),
- {ok, TimerRef} = timer:send_interval(Interval, save),
- {ok,#state{table=Table, timer=TimerRef}}.
+ Table = proplists:get_value(table, Config),
+ Interval = proplists:get_value(interval, Config, 1000),
+ {ok, TimerRef} = timer:send_interval(Interval, save),
+ {ok, #state{table = Table, timer = TimerRef}}.
-handle_call(get_last_timestamp, _From, State = #state{table=Table}) ->
- {reply, read_timestamp(Table), State}.
+handle_call(get_last_timestamp, _From, State = #state{table = Table}) ->
+ {reply, read_timestamp(Table), State}.
handle_cast(_, State) -> {noreply, State}.
-handle_info(save, State = #state{table=Table}) ->
+handle_info(save, State = #state{table = Table}) ->
{ok, _} = write_timestamp(Table),
{noreply, State}.
@@ -82,19 +82,19 @@ code_change(_, State, _) -> {ok, State}.
%% write the current time stamp to disk
%% {ok,Timestamp=int()} | {error,Reason}
write_timestamp(Table) ->
- TS = flake_util:curr_time_millis(),
- ok = dets:insert(Table,{last_timestamp,TS}),
- {ok,TS}.
+ TS = flake_util:curr_time_millis(),
+ ok = dets:insert(Table, {last_timestamp, TS}),
+ {ok, TS}.
%% read the timestamp from the given file. will write the current timestamp to disk if the file does not exist
%% {ok,Timestamp=int()} | {error,Reason}
read_timestamp(Table) ->
- case dets:lookup(Table,last_timestamp) of
- [{last_timestamp,TS}] when is_integer(TS) ->
- {ok,TS};
- _ ->
- write_timestamp(Table)
- end.
+ case dets:lookup(Table, last_timestamp) of
+ [{last_timestamp, TS}] when is_integer(TS) ->
+ {ok, TS};
+ _ ->
+ write_timestamp(Table)
+ end.
%% ----------------------------------------------------------
@@ -102,12 +102,12 @@ read_timestamp(Table) ->
%% ----------------------------------------------------------
persistent_clock_test() ->
- {ok,Table} =
- dets:open_file(timestamp_table,[
- {estimated_no_objects,10},
- {type,set},
- {file,"/tmp/timestamp-dets"}
- ]),
- {ok,TS0} = write_timestamp(Table),
- {ok,TS1} = read_timestamp(Table),
- ?assert(?debugVal(TS0) =:= ?debugVal(TS1)).
+ {ok, Table} =
+ dets:open_file(timestamp_table, [
+ {estimated_no_objects, 10},
+ {type, set},
+ {file, "/tmp/timestamp-dets"}
+ ]),
+ {ok, TS0} = write_timestamp(Table),
+ {ok, TS1} = read_timestamp(Table),
+ ?assert(?debugVal(TS0) =:= ?debugVal(TS1)).

0 comments on commit c8d7625

Please sign in to comment.
Something went wrong with that request. Please try again.