2015-04-03 08:10:52 +00:00
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%%
|
|
|
|
%% Copyright (c) 2007-2015 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.
|
|
|
|
%%
|
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc The Machi write-once projection store service.
|
|
|
|
%%
|
|
|
|
%% This API is gen_server-style message passing, intended for use
|
|
|
|
%% within a single Erlang node to glue together the projection store
|
2015-05-20 02:11:54 +00:00
|
|
|
%% server with the node-local process that implements Machi's FLU
|
2015-04-08 05:24:07 +00:00
|
|
|
%% client access protocol (on the "server side" of the TCP connection).
|
|
|
|
%%
|
|
|
|
%% All Machi client access to the projection store SHOULD NOT use this
|
2015-05-20 02:11:54 +00:00
|
|
|
%% module's API. Instead, clients should access indirectly via {@link
|
|
|
|
%% machi_cr_client}, {@link machi_proxy_flu1_client}, or {@link
|
|
|
|
%% machi_flu1_client}.
|
2015-04-08 05:24:07 +00:00
|
|
|
%%
|
|
|
|
%% The projection store is implemented by an Erlang/OTP `gen_server'
|
|
|
|
%% process that is associated with each FLU. Conceptually, the
|
|
|
|
%% projection store is an array of write-once registers. For each
|
|
|
|
%% projection store register, the key is a 2-tuple of an epoch number
|
|
|
|
%% (`non_neg_integer()' type) and a projection type (`public' or
|
|
|
|
%% `private' type); the value is a projection data structure
|
|
|
|
%% (`projection_v1()' type).
|
|
|
|
|
2015-04-03 08:10:52 +00:00
|
|
|
-module(machi_projection_store).
|
|
|
|
|
|
|
|
-include("machi_projection.hrl").
|
2015-07-16 08:59:02 +00:00
|
|
|
-define(V(X,Y), ok).
|
|
|
|
%% -include("machi_verbose.hrl").
|
2015-04-03 08:10:52 +00:00
|
|
|
|
2015-07-16 08:59:02 +00:00
|
|
|
%% -ifdef(PULSE).
|
|
|
|
%% -compile({parse_transform, pulse_instrument}).
|
|
|
|
%% -include_lib("pulse_otp/include/pulse_otp.hrl").
|
|
|
|
%% -endif.
|
2015-07-16 07:25:38 +00:00
|
|
|
|
2015-04-03 08:10:52 +00:00
|
|
|
%% API
|
|
|
|
-export([
|
|
|
|
start_link/3,
|
2015-05-18 10:06:06 +00:00
|
|
|
get_latest_epochid/2, get_latest_epochid/3,
|
2015-04-03 09:37:09 +00:00
|
|
|
read_latest_projection/2, read_latest_projection/3,
|
2015-04-03 08:55:35 +00:00
|
|
|
read/3, read/4,
|
2015-04-03 09:37:09 +00:00
|
|
|
write/3, write/4,
|
2015-04-06 09:43:52 +00:00
|
|
|
get_all_projections/2, get_all_projections/3,
|
|
|
|
list_all_projections/2, list_all_projections/3
|
2015-04-03 08:10:52 +00:00
|
|
|
]).
|
2015-08-26 09:47:39 +00:00
|
|
|
-export([set_wedge_notify_pid/2, get_wedge_notify_pid/1,
|
|
|
|
set_consistency_mode/2]).
|
2015-04-03 08:10:52 +00:00
|
|
|
|
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
|
|
|
|
2015-05-13 08:58:54 +00:00
|
|
|
-define(NO_EPOCH, ?DUMMY_PV1_EPOCH).
|
2015-04-06 09:43:52 +00:00
|
|
|
|
2015-04-03 08:10:52 +00:00
|
|
|
-record(state, {
|
|
|
|
public_dir = "" :: string(),
|
|
|
|
private_dir = "" :: string(),
|
|
|
|
wedge_notify_pid :: pid() | atom(),
|
2015-05-18 10:06:06 +00:00
|
|
|
max_public_epochid = ?NO_EPOCH :: {-1 | non_neg_integer(), binary()},
|
2015-08-26 05:54:01 +00:00
|
|
|
max_private_epochid = ?NO_EPOCH :: {-1 | non_neg_integer(), binary()},
|
|
|
|
consistency_mode=ap_mode :: 'ap_mode' | 'cp_mode'
|
2015-04-03 08:10:52 +00:00
|
|
|
}).
|
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Start a new projection store server.
|
|
|
|
%%
|
|
|
|
%% The `DataDir' argument should be the same directory as specified
|
|
|
|
%% for use by our companion FLU data server -- all file system paths
|
|
|
|
%% used by this server are intended to be stored underneath a common
|
|
|
|
%% file system parent directory as the FLU data server & sequencer
|
|
|
|
%% servers.
|
|
|
|
|
2015-04-03 08:10:52 +00:00
|
|
|
start_link(RegName, DataDir, NotifyWedgeStateChanges) ->
|
|
|
|
gen_server:start_link({local, RegName},
|
|
|
|
?MODULE, [DataDir, NotifyWedgeStateChanges], []).
|
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Fetch the latest epoch number + checksum for type `ProjType'.
|
|
|
|
|
2015-05-18 10:06:06 +00:00
|
|
|
get_latest_epochid(PidSpec, ProjType) ->
|
|
|
|
get_latest_epochid(PidSpec, ProjType, infinity).
|
2015-04-03 08:55:35 +00:00
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Fetch the latest epoch number + checksum for type `ProjType'.
|
|
|
|
%% projection.
|
|
|
|
|
2015-05-18 10:06:06 +00:00
|
|
|
get_latest_epochid(PidSpec, ProjType, Timeout)
|
2015-04-03 08:55:35 +00:00
|
|
|
when ProjType == 'public' orelse ProjType == 'private' ->
|
2015-05-18 10:06:06 +00:00
|
|
|
g_call(PidSpec, {get_latest_epochid, ProjType}, Timeout).
|
2015-04-03 08:55:35 +00:00
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Fetch the latest projection record for type `ProjType'.
|
|
|
|
|
2015-04-03 09:37:09 +00:00
|
|
|
read_latest_projection(PidSpec, ProjType) ->
|
|
|
|
read_latest_projection(PidSpec, ProjType, infinity).
|
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Fetch the latest projection record for type `ProjType'.
|
|
|
|
|
2015-04-03 09:37:09 +00:00
|
|
|
read_latest_projection(PidSpec, ProjType, Timeout)
|
|
|
|
when ProjType == 'public' orelse ProjType == 'private' ->
|
|
|
|
g_call(PidSpec, {read_latest_projection, ProjType}, Timeout).
|
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Fetch the projection record type `ProjType' for epoch number `Epoch' .
|
|
|
|
|
2015-04-03 08:55:35 +00:00
|
|
|
read(PidSpec, ProjType, Epoch) ->
|
|
|
|
read(PidSpec, ProjType, Epoch, infinity).
|
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Fetch the projection record type `ProjType' for epoch number `Epoch' .
|
|
|
|
|
2015-04-03 08:55:35 +00:00
|
|
|
read(PidSpec, ProjType, Epoch, Timeout)
|
2015-04-03 09:37:09 +00:00
|
|
|
when ProjType == 'public' orelse ProjType == 'private',
|
|
|
|
is_integer(Epoch), Epoch >= 0 ->
|
2015-04-03 08:55:35 +00:00
|
|
|
g_call(PidSpec, {read, ProjType, Epoch}, Timeout).
|
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Write the projection record type `ProjType' for epoch number `Epoch' .
|
|
|
|
|
2015-04-03 08:10:52 +00:00
|
|
|
write(PidSpec, ProjType, Proj) ->
|
|
|
|
write(PidSpec, ProjType, Proj, infinity).
|
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Write the projection record type `ProjType' for epoch number `Epoch' .
|
|
|
|
|
2015-04-03 08:10:52 +00:00
|
|
|
write(PidSpec, ProjType, Proj, Timeout)
|
|
|
|
when ProjType == 'public' orelse ProjType == 'private',
|
2015-04-03 09:37:09 +00:00
|
|
|
is_record(Proj, projection_v1),
|
|
|
|
is_integer(Proj#projection_v1.epoch_number),
|
|
|
|
Proj#projection_v1.epoch_number >= 0 ->
|
2015-04-03 08:10:52 +00:00
|
|
|
g_call(PidSpec, {write, ProjType, Proj}, Timeout).
|
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Fetch all projection records of type `ProjType'.
|
|
|
|
|
2015-04-06 09:43:52 +00:00
|
|
|
get_all_projections(PidSpec, ProjType) ->
|
|
|
|
get_all_projections(PidSpec, ProjType, infinity).
|
2015-04-03 09:37:09 +00:00
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Fetch all projection records of type `ProjType'.
|
|
|
|
|
2015-04-06 09:43:52 +00:00
|
|
|
get_all_projections(PidSpec, ProjType, Timeout)
|
2015-04-03 09:37:09 +00:00
|
|
|
when ProjType == 'public' orelse ProjType == 'private' ->
|
2015-04-06 09:43:52 +00:00
|
|
|
g_call(PidSpec, {get_all_projections, ProjType}, Timeout).
|
2015-04-03 09:37:09 +00:00
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Fetch all projection epoch numbers of type `ProjType'.
|
|
|
|
|
2015-04-06 09:43:52 +00:00
|
|
|
list_all_projections(PidSpec, ProjType) ->
|
|
|
|
list_all_projections(PidSpec, ProjType, infinity).
|
2015-04-03 09:37:09 +00:00
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Fetch all projection epoch numbers of type `ProjType'.
|
|
|
|
|
2015-04-06 09:43:52 +00:00
|
|
|
list_all_projections(PidSpec, ProjType, Timeout)
|
2015-04-03 09:37:09 +00:00
|
|
|
when ProjType == 'public' orelse ProjType == 'private' ->
|
2015-04-06 09:43:52 +00:00
|
|
|
g_call(PidSpec, {list_all_projections, ProjType}, Timeout).
|
2015-04-03 09:37:09 +00:00
|
|
|
|
2015-05-13 08:58:54 +00:00
|
|
|
set_wedge_notify_pid(PidSpec, NotifyWedgeStateChanges) ->
|
2015-08-26 05:54:01 +00:00
|
|
|
gen_server:call(PidSpec, {set_wedge_notify_pid, NotifyWedgeStateChanges},
|
|
|
|
infinity).
|
|
|
|
|
2015-08-26 09:47:39 +00:00
|
|
|
get_wedge_notify_pid(PidSpec) ->
|
|
|
|
gen_server:call(PidSpec, {get_wedge_notify_pid},
|
|
|
|
infinity).
|
|
|
|
|
2015-08-26 05:54:01 +00:00
|
|
|
set_consistency_mode(PidSpec, CMode)
|
|
|
|
when CMode == ap_mode; CMode == cp_mode ->
|
|
|
|
gen_server:call(PidSpec, {set_consistency_mode, CMode}, infinity).
|
2015-05-13 08:58:54 +00:00
|
|
|
|
2015-04-03 09:37:09 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
g_call(PidSpec, Arg, Timeout) ->
|
|
|
|
LC1 = lclock_get(),
|
|
|
|
{Res, LC2} = gen_server:call(PidSpec, {Arg, LC1}, Timeout),
|
|
|
|
lclock_update(LC2),
|
|
|
|
Res.
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2015-04-03 08:10:52 +00:00
|
|
|
init([DataDir, NotifyWedgeStateChanges]) ->
|
|
|
|
lclock_init(),
|
|
|
|
PublicDir = machi_util:make_projection_filename(DataDir, "public"),
|
|
|
|
PrivateDir = machi_util:make_projection_filename(DataDir, "private"),
|
|
|
|
ok = filelib:ensure_dir(PublicDir ++ "/ignored"),
|
|
|
|
ok = filelib:ensure_dir(PrivateDir ++ "/ignored"),
|
2015-05-18 10:06:06 +00:00
|
|
|
MbEpoch = find_max_epochid(PublicDir),
|
|
|
|
%% MbEpoch = {Mb#projection_v1.epoch_number, Mb#projection_v1.epoch_csum},
|
|
|
|
MvEpoch = find_max_epochid(PrivateDir),
|
|
|
|
%% MvEpoch = {Mv#projection_v1.epoch_number, Mv#projection_v1.epoch_csum},
|
2015-04-03 08:10:52 +00:00
|
|
|
|
|
|
|
{ok, #state{public_dir=PublicDir,
|
|
|
|
private_dir=PrivateDir,
|
|
|
|
wedge_notify_pid=NotifyWedgeStateChanges,
|
2015-05-18 10:06:06 +00:00
|
|
|
max_public_epochid=MbEpoch,
|
|
|
|
max_private_epochid=MvEpoch}}.
|
2015-04-03 08:10:52 +00:00
|
|
|
|
2015-05-18 10:06:06 +00:00
|
|
|
handle_call({{get_latest_epochid, ProjType}, LC1}, _From, S) ->
|
2015-04-03 08:55:35 +00:00
|
|
|
LC2 = lclock_update(LC1),
|
2015-05-18 10:06:06 +00:00
|
|
|
EpochId = if ProjType == public -> S#state.max_public_epochid;
|
|
|
|
ProjType == private -> S#state.max_private_epochid
|
2015-04-06 06:49:47 +00:00
|
|
|
end,
|
2015-05-08 09:17:41 +00:00
|
|
|
{reply, {{ok, EpochId}, LC2}, S};
|
2015-04-03 09:37:09 +00:00
|
|
|
handle_call({{read_latest_projection, ProjType}, LC1}, _From, S) ->
|
|
|
|
LC2 = lclock_update(LC1),
|
2015-05-18 10:06:06 +00:00
|
|
|
{EpochNum, _CSum} = if ProjType == public -> S#state.max_public_epochid;
|
|
|
|
ProjType == private -> S#state.max_private_epochid
|
2015-04-03 09:37:09 +00:00
|
|
|
end,
|
2015-04-06 06:49:47 +00:00
|
|
|
{Reply, NewS} = do_proj_read(ProjType, EpochNum, S),
|
2015-04-03 09:37:09 +00:00
|
|
|
{reply, {Reply, LC2}, NewS};
|
2015-04-03 08:55:35 +00:00
|
|
|
handle_call({{read, ProjType, Epoch}, LC1}, _From, S) ->
|
|
|
|
LC2 = lclock_update(LC1),
|
|
|
|
{Reply, NewS} = do_proj_read(ProjType, Epoch, S),
|
|
|
|
{reply, {Reply, LC2}, NewS};
|
2015-04-03 08:10:52 +00:00
|
|
|
handle_call({{write, ProjType, Proj}, LC1}, _From, S) ->
|
|
|
|
LC2 = lclock_update(LC1),
|
|
|
|
{Reply, NewS} = do_proj_write(ProjType, Proj, S),
|
|
|
|
{reply, {Reply, LC2}, NewS};
|
2015-04-06 09:43:52 +00:00
|
|
|
handle_call({{get_all_projections, ProjType}, LC1}, _From, S) ->
|
2015-04-03 09:37:09 +00:00
|
|
|
LC2 = lclock_update(LC1),
|
|
|
|
Dir = pick_path(ProjType, S),
|
|
|
|
Epochs = find_all(Dir),
|
|
|
|
All = [begin
|
|
|
|
{{ok, Proj}, _} = do_proj_read(ProjType, Epoch, S),
|
|
|
|
Proj
|
|
|
|
end || Epoch <- Epochs],
|
|
|
|
{reply, {{ok, All}, LC2}, S};
|
2015-04-06 09:43:52 +00:00
|
|
|
handle_call({{list_all_projections, ProjType}, LC1}, _From, S) ->
|
2015-04-03 09:37:09 +00:00
|
|
|
LC2 = lclock_update(LC1),
|
|
|
|
Dir = pick_path(ProjType, S),
|
|
|
|
{reply, {{ok, find_all(Dir)}, LC2}, S};
|
2015-05-13 08:58:54 +00:00
|
|
|
handle_call({set_wedge_notify_pid, NotifyWedgeStateChanges}, _From, S) ->
|
|
|
|
{reply, ok, S#state{wedge_notify_pid=NotifyWedgeStateChanges}};
|
2015-08-26 09:47:39 +00:00
|
|
|
handle_call({get_wedge_notify_pid}, _From, S) ->
|
|
|
|
{reply, {ok, S#state.wedge_notify_pid}, S};
|
2015-08-26 05:54:01 +00:00
|
|
|
handle_call({set_consistency_mode, CMode}, _From, S) ->
|
|
|
|
{reply, ok, S#state{consistency_mode=CMode}};
|
2015-04-03 08:10:52 +00:00
|
|
|
handle_call(_Request, _From, S) ->
|
2015-06-03 02:48:55 +00:00
|
|
|
Reply = {whaaaaaaaaaaaaazz, _Request},
|
2015-04-03 08:10:52 +00:00
|
|
|
{reply, Reply, S}.
|
|
|
|
|
|
|
|
handle_cast(_Msg, S) ->
|
|
|
|
{noreply, S}.
|
|
|
|
|
|
|
|
handle_info(_Info, S) ->
|
|
|
|
{noreply, S}.
|
|
|
|
|
|
|
|
terminate(_Reason, _S) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, S, _Extra) ->
|
|
|
|
{ok, S}.
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2015-04-03 09:37:09 +00:00
|
|
|
do_proj_read(_ProjType, Epoch, S) when Epoch < 0 ->
|
|
|
|
{{error, not_written}, S};
|
2015-04-06 06:49:47 +00:00
|
|
|
do_proj_read(ProjType, Epoch, S_or_Dir) ->
|
|
|
|
Dir = if is_record(S_or_Dir, state) ->
|
|
|
|
pick_path(ProjType, S_or_Dir);
|
|
|
|
is_list(S_or_Dir) ->
|
|
|
|
S_or_Dir
|
|
|
|
end,
|
2015-04-03 08:55:35 +00:00
|
|
|
Path = filename:join(Dir, epoch2name(Epoch)),
|
|
|
|
case file:read_file(Path) of
|
|
|
|
{ok, Bin} ->
|
|
|
|
%% TODO and if Bin is corrupt? (even if binary_to_term() succeeds)
|
2015-04-06 06:49:47 +00:00
|
|
|
{{ok, binary_to_term(Bin)}, S_or_Dir};
|
2015-04-03 08:55:35 +00:00
|
|
|
{error, enoent} ->
|
2015-04-06 06:49:47 +00:00
|
|
|
{{error, not_written}, S_or_Dir};
|
2015-04-03 08:55:35 +00:00
|
|
|
{error, Else} ->
|
2015-04-06 06:49:47 +00:00
|
|
|
{{error, Else}, S_or_Dir}
|
2015-04-03 08:55:35 +00:00
|
|
|
end.
|
|
|
|
|
2015-08-27 09:45:27 +00:00
|
|
|
do_proj_write(ProjType, Proj, S) ->
|
|
|
|
do_proj_write2(ProjType, Proj, S).
|
2015-08-13 09:44:25 +00:00
|
|
|
|
2015-08-25 10:31:05 +00:00
|
|
|
do_proj_write2(ProjType, #projection_v1{epoch_csum=CSum}=Proj, S) ->
|
|
|
|
case (machi_projection:update_checksum(Proj))#projection_v1.epoch_csum of
|
|
|
|
CSum2 when CSum2 == CSum ->
|
|
|
|
do_proj_write3(ProjType, Proj, S);
|
|
|
|
_Else ->
|
2015-08-27 11:27:33 +00:00
|
|
|
{{error, bad_arg}, S}
|
2015-08-25 10:31:05 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
do_proj_write3(ProjType, #projection_v1{epoch_number=Epoch,
|
|
|
|
epoch_csum=CSum}=Proj, S) ->
|
2015-04-03 08:10:52 +00:00
|
|
|
%% TODO: We probably ought to check the projection checksum for sanity, eh?
|
|
|
|
Dir = pick_path(ProjType, S),
|
|
|
|
Path = filename:join(Dir, epoch2name(Epoch)),
|
2015-08-25 10:31:05 +00:00
|
|
|
case file:read_file(Path) of
|
|
|
|
{ok, _Bin} when ProjType == public ->
|
|
|
|
{{error, written}, S};
|
|
|
|
{ok, Bin} when ProjType == private ->
|
|
|
|
#projection_v1{epoch_number=CurEpoch,
|
|
|
|
epoch_csum=CurCSum} = _CurProj = binary_to_term(Bin),
|
2015-08-25 10:42:33 +00:00
|
|
|
%% We've already checked that CSum is correct matches the
|
|
|
|
%% contents of this new projection version. If the epoch_csum
|
|
|
|
%% values match, and if we trust the value on disk (TODO paranoid
|
|
|
|
%% check that, also), then the only difference must be the dbg2
|
|
|
|
%% list, which is ok.
|
2015-08-26 08:51:43 +00:00
|
|
|
if CurEpoch == Epoch, CurCSum == CSum ->
|
2015-08-25 10:42:33 +00:00
|
|
|
do_proj_write4(ProjType, Proj, Path, Epoch, S);
|
|
|
|
true ->
|
2015-09-04 06:23:48 +00:00
|
|
|
%% io:format(user, "OUCH: on disk: ~w\n", [machi_projection:make_summary(binary_to_term(Bin))]),
|
|
|
|
%% io:format(user, "OUCH: clobber: ~w\n", [machi_projection:make_summary(Proj)]),
|
|
|
|
%% io:format(user, "OUCH: clobber: ~p\n", [Proj#projection_v1.dbg2]),
|
|
|
|
%% {{error, written, CurEpoch, Epoch, CurCSum, CSum}, S}
|
|
|
|
{{error, written}, S}
|
2015-08-25 10:42:33 +00:00
|
|
|
end;
|
2015-04-03 08:10:52 +00:00
|
|
|
{error, enoent} ->
|
2015-08-25 10:31:05 +00:00
|
|
|
do_proj_write4(ProjType, Proj, Path, Epoch, S);
|
2015-04-03 08:10:52 +00:00
|
|
|
{error, Else} ->
|
|
|
|
{{error, Else}, S}
|
|
|
|
end.
|
|
|
|
|
2015-08-26 06:51:14 +00:00
|
|
|
do_proj_write4(ProjType, Proj, Path, Epoch, #state{consistency_mode=CMode}=S) ->
|
2015-08-25 10:31:05 +00:00
|
|
|
{ok, FH} = file:open(Path, [write, raw, binary]),
|
|
|
|
ok = file:write(FH, term_to_binary(Proj)),
|
|
|
|
ok = file:sync(FH),
|
|
|
|
ok = file:close(FH),
|
|
|
|
EffectiveProj = machi_chain_manager1:inner_projection_or_self(Proj),
|
|
|
|
EffectiveEpoch = EffectiveProj#projection_v1.epoch_number,
|
|
|
|
EpochId = {Epoch, Proj#projection_v1.epoch_csum},
|
|
|
|
EffectiveEpochId = {EffectiveEpoch, EffectiveProj#projection_v1.epoch_csum},
|
|
|
|
%%
|
|
|
|
NewS = if ProjType == public,
|
|
|
|
Epoch > element(1, S#state.max_public_epochid) ->
|
|
|
|
if Epoch == EffectiveEpoch ->
|
|
|
|
%% This is a regular projection, i.e.,
|
|
|
|
%% does not have an inner proj.
|
|
|
|
update_wedge_state(
|
|
|
|
S#state.wedge_notify_pid, true,
|
|
|
|
EffectiveEpochId);
|
|
|
|
Epoch /= EffectiveEpoch ->
|
|
|
|
%% This projection has an inner proj.
|
|
|
|
%% The outer proj is flapping, so we do
|
|
|
|
%% not bother wedging.
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
S#state{max_public_epochid=EpochId};
|
|
|
|
ProjType == private,
|
|
|
|
Epoch > element(1, S#state.max_private_epochid) ->
|
2015-08-26 08:51:43 +00:00
|
|
|
if CMode == ap_mode ->
|
|
|
|
update_wedge_state(
|
|
|
|
S#state.wedge_notify_pid, false,
|
|
|
|
EffectiveEpochId);
|
|
|
|
true ->
|
|
|
|
%% If ProjType == private and CMode == cp_mode, then
|
|
|
|
%% the unwedge action is not performed here!
|
|
|
|
ok
|
|
|
|
end,
|
2015-08-25 10:31:05 +00:00
|
|
|
S#state{max_private_epochid=EpochId};
|
|
|
|
true ->
|
|
|
|
S
|
|
|
|
end,
|
|
|
|
{ok, NewS}.
|
|
|
|
|
2015-05-08 09:17:41 +00:00
|
|
|
update_wedge_state(PidSpec, Boolean, {0,_}=EpochId) ->
|
|
|
|
%% Epoch #0 is a special case: no projection has been written yet.
|
|
|
|
%% However, given the way that machi_flu_psup starts the
|
|
|
|
%% processes, we are roughly 100% certain that the FLU for PidSpec
|
|
|
|
%% is not yet running.
|
|
|
|
catch machi_flu1:update_wedge_state(PidSpec, Boolean, EpochId);
|
|
|
|
update_wedge_state(PidSpec, Boolean, EpochId) ->
|
|
|
|
%% We have a race problem with the startup order by machi_flu_psup:
|
|
|
|
%% the order is projection store (me!), projection manager, FLU.
|
|
|
|
%% PidSpec is the FLU. It's almost certainly a registered name.
|
|
|
|
%% Wait for it to exist before sending a message to it. Racing with
|
|
|
|
%% supervisor startup/shutdown/restart is ok.
|
|
|
|
ok = wait_for_liveness(PidSpec, 10*1000),
|
|
|
|
machi_flu1:update_wedge_state(PidSpec, Boolean, EpochId).
|
|
|
|
|
|
|
|
wait_for_liveness(Pid, _WaitTime) when is_pid(Pid) ->
|
|
|
|
ok;
|
|
|
|
wait_for_liveness(PidSpec, WaitTime) ->
|
|
|
|
wait_for_liveness(PidSpec, os:timestamp(), WaitTime).
|
|
|
|
|
|
|
|
wait_for_liveness(PidSpec, StartTime, WaitTime) ->
|
|
|
|
case whereis(PidSpec) of
|
|
|
|
undefined ->
|
|
|
|
case timer:now_diff(os:timestamp(), StartTime) div 1000 of
|
|
|
|
X when X < WaitTime ->
|
|
|
|
io:format(user, "\nYOO ~p ~p\n", [PidSpec, lists:sort(registered())]),
|
|
|
|
timer:sleep(1),
|
|
|
|
wait_for_liveness(PidSpec, StartTime, WaitTime)
|
|
|
|
end;
|
|
|
|
_SomePid ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2015-04-03 08:10:52 +00:00
|
|
|
pick_path(public, S) ->
|
|
|
|
S#state.public_dir;
|
|
|
|
pick_path(private, S) ->
|
|
|
|
S#state.private_dir.
|
|
|
|
|
|
|
|
epoch2name(Epoch) ->
|
|
|
|
machi_util:int_to_hexstr(Epoch, 32).
|
|
|
|
|
|
|
|
name2epoch(Name) ->
|
|
|
|
machi_util:hexstr_to_int(Name).
|
|
|
|
|
2015-04-03 09:37:09 +00:00
|
|
|
find_all(Dir) ->
|
|
|
|
Fs = filelib:wildcard("*", Dir),
|
|
|
|
lists:sort([name2epoch(F) || F <- Fs]).
|
|
|
|
|
2015-05-18 10:06:06 +00:00
|
|
|
find_max_epochid(Dir) ->
|
2015-04-03 08:10:52 +00:00
|
|
|
Fs = lists:sort(filelib:wildcard("*", Dir)),
|
|
|
|
if Fs == [] ->
|
2015-04-06 09:43:52 +00:00
|
|
|
?NO_EPOCH;
|
2015-04-03 08:10:52 +00:00
|
|
|
true ->
|
2015-04-06 06:49:47 +00:00
|
|
|
EpochNum = name2epoch(lists:last(Fs)),
|
|
|
|
{{ok, Proj}, _} = do_proj_read(proj_type_ignored, EpochNum, Dir),
|
2015-05-18 10:06:06 +00:00
|
|
|
{EpochNum, Proj#projection_v1.epoch_csum}
|
2015-04-03 08:10:52 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
-ifdef(TEST).
|
|
|
|
|
|
|
|
lclock_init() ->
|
|
|
|
lamport_clock:init().
|
|
|
|
|
|
|
|
lclock_get() ->
|
|
|
|
lamport_clock:get().
|
|
|
|
|
|
|
|
lclock_update(LC) ->
|
|
|
|
lamport_clock:update(LC).
|
|
|
|
|
|
|
|
-else. % TEST
|
|
|
|
|
|
|
|
lclock_init() ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
lclock_get() ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
lclock_update(_LC) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
-endif. % TEST
|