Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Jc/oc 4352/chef cache #39

Merged
merged 9 commits into from
Nov 14, 2012
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DEPS = deps/erlzmq deps/jiffy deps/gproc deps/ej \
deps/chef_authn deps/sqerl deps/mixer deps\lager \
deps/folsom
deps/chef_authn deps/sqerl deps/mixer deps/lager \
deps/folsom deps/pushy_common

ERLANG_APPS = erts kernel stdlib tools compiler syntax_tools runtime_tools\
crypto public_key ssl xmerl edoc eunit mnesia inets \
Expand All @@ -9,7 +9,9 @@ ERLANG_APPS = erts kernel stdlib tools compiler syntax_tools runtime_tools\

PLT_FILE = .pushy.plt

all: dialyze eunit
# Until we pass dialyzer - OC-1926
#all: dialyze eunit
all: eunit

use_locked_config = $(wildcard USE_REBAR_LOCKED)
ifeq ($(use_locked_config),USE_REBAR_LOCKED)
Expand Down Expand Up @@ -41,7 +43,7 @@ plt_deps:


dialyze: compile $(PLT_FILE)
dialyzer -nn --plt $(PLT_FILE) -Wunmatched_returns -Werror_handling -Wrace_conditions -r apps/pushy/ebin -I deps
dialyzer -nn --plt $(PLT_FILE) -Wunmatched_returns -Werror_handling -r apps/pushy/ebin -I deps

#dialyzer:
# @rm -rf apps/pushy/.eunit
Expand Down
2 changes: 0 additions & 2 deletions apps/pushy/include/pushy_sql.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
timed_out.

%% random PoC hard-codings
-define(POC_ORG_ID, <<"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa">>).
-define(POC_ORG_NAME, <<"pushy">>).
-define(POC_ACTOR_ID, <<"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb">>).
-define(POC_HB_THRESHOLD, 3).

Expand Down
4 changes: 4 additions & 0 deletions apps/pushy/priv/pgsql_statements.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
" WHERE (org_id= $4 AND node_name= $5)">>}.

%% TODO: Temporarily disabling date fields
{list_node_statuses,
<<"SELECT node_name, org_id, status, last_updated_by, created_at, updated_at"
" FROM node_status">>}.

{list_node_statuses_for_org,
<<"SELECT node_name, org_id, status, last_updated_by, created_at, updated_at"
" FROM node_status"
Expand Down
2 changes: 1 addition & 1 deletion apps/pushy/src/pushy_config_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ init(_Config) ->
service_available(Req, State) ->
NodeName = wrq:path_info(node_name, Req),
OrgName = wrq:path_info(organization_id, Req),
OrgGuid = ?POC_ORG_ID,
OrgGuid = pushy_object:fetch_org_id(OrgName),
State1 = State#config_state{orgname = OrgName, organization_guid = OrgGuid,
node_name = NodeName},
{true, Req, State1}.
Expand Down
2 changes: 1 addition & 1 deletion apps/pushy/src/pushy_node_state_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ init([]) ->
%% ------------------------------------------------------------------

load_from_db() ->
case pushy_sql:fetch_node_statuses(?POC_ORG_ID) of
case pushy_sql:fetch_node_statuses() of
{ok, none} ->
lager:info("No existing node status records found in database, FSM proceses will not be pre-created.");
{ok, NodeStatuses} ->
Expand Down
17 changes: 13 additions & 4 deletions apps/pushy/src/pushy_object.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@
make_org_prefix_id/2
]).


%%% TODO: Make this actually work: (OC-4352)
fetch_org_id(_OrgName) ->
?POC_ORG_ID.
fetch_org_id(OrgName) ->
case pushy_cache:get(org_guid, OrgName) of
Error when Error =:= not_found orelse Error =:= no_cache ->
case pushy_org:fetch_org_id(OrgName) of
not_found ->
not_found;
Guid ->
pushy_cache:put(org_guid, OrgName, Guid),
Guid
end;
{ok, Guid} ->
Guid
end.

new_record(pushy_node_status, OrgId, NodeStatusData) ->
Name = proplists:get_value(<<"node">>, NodeStatusData),
Expand Down
22 changes: 22 additions & 0 deletions apps/pushy/src/pushy_org.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil; fill-column: 92-*-
%% ex: ts=4 sw=4 et
%% @author James Casey <[email protected]>
%% @copyright 2012 Opscode, Inc.

%% @doc Handle org_name to org_guid mappings by calling out to
%% opscode-account
-module(pushy_org).

-define(POC_ORG_ID, <<"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa">>).

-export([
fetch_org_id/1
]).

%% Stub it out for now
-spec fetch_org_id(OrgName :: string()) -> binary() | not_found.
fetch_org_id(_OrgName) ->
?POC_ORG_ID.



20 changes: 16 additions & 4 deletions apps/pushy/src/pushy_sql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

-export([
%% node status ops
fetch_node_statuses/0,
fetch_node_statuses/1,
fetch_node_status/2,
create_node_status/1,
Expand All @@ -25,16 +26,27 @@ sql_now() -> calendar:now_to_universal_time(os:timestamp()).

%% node status ops

-spec fetch_node_statuses(binary() | string()) -> {ok, list()} | {error, term()}.
-spec fetch_node_statuses() -> {ok, list()} | {error, term()}.
fetch_node_statuses() ->
case sqerl:select(list_node_statuses, []) of
{ok, none} ->
{ok, []};
{ok, Response} ->
{ok, [node_status_row_to_record(Row) || Row <- Response]};
{error, Reason} ->
{error, Reason}
end.

-spec fetch_node_statuses(binary() | string())-> {ok, list()} | {error, term()}.
fetch_node_statuses(OrgId) ->
case sqerl:select(list_node_statuses_for_org, [OrgId]) of
{ok, none} ->
{ok, []};
{ok, Response} ->
{ok, [node_status_row_to_record(Row) || Row <- Response]};
{ok, [node_status_row_to_record(Row) || Row <- Response]};
{error, Reason} ->
{error, Reason}
end.
{error, Reason}
end.

%% TODO Figure out correct spec
-spec fetch_node_status(binary() | string(), binary() | string() ) -> {ok, list()} | {error, term()}.
Expand Down
5 changes: 5 additions & 0 deletions apps/pushy/src/pushy_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ init([#pushy_state{ctx=_Ctx} = PushyState]) ->
%% This attaches the ownership of the ETS table for keys to this process.
pushy_key_manager:init(),

%% This attaches the ownership of the ETS table for keys to this process.
pushy_cache:init([{name, org_guid},
{max_size, 10000},
{ttl, 100000000}]),

Ip = case os:getenv("WEBMACHINE_IP") of false -> "0.0.0.0"; Any -> Any end,
{ok, Dispatch} = file:consult(filename:join(
[code:priv_dir(pushy), "dispatch.conf"])),
Expand Down
69 changes: 69 additions & 0 deletions apps/pushy/test/pushy_object_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil; fill-column: 92 -*-
%% ex: ts=4 sw=4 et
%%
%% @author James Casey <[email protected]>
%%
%% @copyright 2012 Opscode Inc.
%% @end

-module(pushy_object_tests).

-include_lib("eunit/include/eunit.hrl").

-define(CACHE, org_guid).

setup_cache() ->
pushy_cache:init([{name, ?CACHE},
{ttl, 100000},
{max_size, 10000}]).

shutdown_cache() ->
pushy_cache:close(?CACHE).

%% Check the caching works on fetch_object ID
fetch_object_id_test_() ->
MockedModules = [pushy_org],
{foreach,
fun() ->
setup_cache(),
meck:new(MockedModules, [])
end,
fun(_) ->
shutdown_cache(),
meck:unload()
end,
[{"A get on unknown value returns not_found",
fun() ->
meck:expect(pushy_org, fetch_org_id, fun(_OrgName) -> not_found end),
Got = pushy_object:fetch_org_id("foo"),
?assertEqual(not_found, Got)
end
},
{"A get on a cached value served from the cache",
fun() ->
meck:expect(pushy_org, fetch_org_id, fun(_OrgName) -> will_not_be_called end),
Expected = <<"foo_id">>,
pushy_cache:put(?CACHE, <<"foo">>, Expected),
Got = pushy_object:fetch_org_id(<<"foo">>),
?assertEqual(Expected, Got)
end
},
{"A get on a new value adds it to the cache",
fun() ->
Expected = <<"foo_id">>,
meck:expect(pushy_org, fetch_org_id, fun(_OrgName) -> Expected end),
pushy_cache:put(?CACHE, <<"foo">>, Expected),

Got = pushy_object:fetch_org_id(<<"foo">>),
?assertEqual(Expected, Got),

% Now check if a second call returns the right value
meck:expect(pushy_org, fetch_org_id, fun(_OrgName) -> will_not_be_called end),
?assertEqual(will_not_be_called, pushy_org:fetch_org_id(<<"foo">>)),
Got1 = pushy_object:fetch_org_id(<<"foo">>),
?assertEqual(Expected, Got1)
end
}
]
}.