2015-03-31 07:46:03 +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 Erlang API for the Machi FLU TCP protocol version 1.
|
2015-05-20 02:11:54 +00:00
|
|
|
%%
|
|
|
|
%% This client API handles low-level PDU serialization/deserialization
|
|
|
|
%% and low-level TCP session management, e.g. open, receive, write,
|
|
|
|
%% close. The API for higher-level session management and Machi state
|
|
|
|
%% management can be found in {@link machi_proxy_flu1_client} and
|
|
|
|
%% {@link machi_cr_client}.
|
|
|
|
%%
|
|
|
|
%% TODO This EDoc was written first, and the EDoc and also `-type' and
|
|
|
|
%% `-spec' definitions for {@link machi_proxy_flu1_client} and {@link
|
|
|
|
%% machi_cr_client} must be improved.
|
|
|
|
%%
|
|
|
|
%% === Protocol origins ===
|
|
|
|
%%
|
|
|
|
%% The protocol implemented here is an artisanal, hand-crafted, silly
|
|
|
|
%% thing that was very quick to put together for a "demo day" proof of
|
|
|
|
%% concept. It will almost certainly be replaced with something else,
|
|
|
|
%% both in terms of wire format and better code separation of
|
|
|
|
%% serialization/deserialization vs. network transport management,
|
|
|
|
%% etc.
|
|
|
|
%%
|
|
|
|
%% For the moment, this module implements a rudimentary TCP-based
|
|
|
|
%% protocol as the sole supported access method to the server,
|
|
|
|
%% sequencer, and projection store. Conceptually, those three
|
|
|
|
%% services are independent and ought to have their own protocols. As
|
|
|
|
%% a practical matter, there is no need for wire protocol
|
|
|
|
%% compatibility. Furthermore, from the perspective of failure
|
|
|
|
%% detection, it is very convenient that all three FLU-related
|
|
|
|
%% services are accessed using the same single TCP port.
|
2015-04-08 05:24:07 +00:00
|
|
|
|
2015-03-31 07:46:03 +00:00
|
|
|
-module(machi_flu1_client).
|
|
|
|
|
|
|
|
-include("machi.hrl").
|
2015-04-03 03:33:47 +00:00
|
|
|
-include("machi_projection.hrl").
|
2015-03-31 07:46:03 +00:00
|
|
|
|
2015-05-18 10:06:06 +00:00
|
|
|
-define(HARD_TIMEOUT, 2500).
|
|
|
|
|
2015-03-31 07:46:03 +00:00
|
|
|
-export([
|
2015-04-03 03:33:47 +00:00
|
|
|
%% File API
|
2015-04-02 09:08:42 +00:00
|
|
|
append_chunk/4, append_chunk/5,
|
2015-05-17 05:10:42 +00:00
|
|
|
append_chunk_extra/5, append_chunk_extra/6,
|
2015-04-02 11:31:10 +00:00
|
|
|
read_chunk/5, read_chunk/6,
|
2015-04-02 11:49:45 +00:00
|
|
|
checksum_list/3, checksum_list/4,
|
2015-04-02 12:01:48 +00:00
|
|
|
list_files/2, list_files/3,
|
2015-05-08 07:53:10 +00:00
|
|
|
wedge_status/1, wedge_status/2,
|
2015-04-03 03:33:47 +00:00
|
|
|
|
|
|
|
%% Projection API
|
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_projection/3, read_projection/4,
|
2015-04-03 03:33:47 +00:00
|
|
|
write_projection/3, write_projection/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 03:33:47 +00:00
|
|
|
|
|
|
|
%% Common API
|
2015-05-17 07:18:30 +00:00
|
|
|
quit/1,
|
|
|
|
|
|
|
|
%% Connection management API
|
|
|
|
connected_p/1, connect/1, disconnect/1
|
2015-03-31 07:46:03 +00:00
|
|
|
]).
|
|
|
|
%% For "internal" replication only.
|
|
|
|
-export([
|
2015-04-02 12:18:41 +00:00
|
|
|
write_chunk/5, write_chunk/6,
|
|
|
|
delete_migration/3, delete_migration/4,
|
|
|
|
trunc_hack/3, trunc_hack/4
|
2015-03-31 07:46:03 +00:00
|
|
|
]).
|
|
|
|
|
2015-06-19 07:04:34 +00:00
|
|
|
-type port_wrap() :: {w,atom(),term()}.
|
2015-04-02 11:49:45 +00:00
|
|
|
|
2015-03-31 07:46:03 +00:00
|
|
|
%% @doc Append a chunk (binary- or iolist-style) of data to a file
|
|
|
|
%% with `Prefix'.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec append_chunk(port_wrap(), machi_dt:epoch_id(), machi_dt:file_prefix(), machi_dt:chunk()) ->
|
|
|
|
{ok, machi_dt:chunk_pos()} | {error, machi_dt:error_general()} | {error, term()}.
|
2015-04-02 09:08:42 +00:00
|
|
|
append_chunk(Sock, EpochID, Prefix, Chunk) ->
|
2015-05-17 05:10:42 +00:00
|
|
|
append_chunk2(Sock, EpochID, Prefix, Chunk, 0).
|
2015-03-31 07:46:03 +00:00
|
|
|
|
|
|
|
%% @doc Append a chunk (binary- or iolist-style) of data to a file
|
|
|
|
%% with `Prefix'.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec append_chunk(machi_dt:inet_host(), machi_dt:inet_port(),
|
|
|
|
machi_dt:epoch_id(), machi_dt:file_prefix(), machi_dt:chunk()) ->
|
|
|
|
{ok, machi_dt:chunk_pos()} | {error, machi_dt:error_general()} | {error, term()}.
|
2015-04-02 09:08:42 +00:00
|
|
|
append_chunk(Host, TcpPort, EpochID, Prefix, Chunk) ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock = connect(#p_srvr{proto_mod=?MODULE, address=Host, port=TcpPort}),
|
2015-03-31 07:46:03 +00:00
|
|
|
try
|
2015-05-17 05:10:42 +00:00
|
|
|
append_chunk2(Sock, EpochID, Prefix, Chunk, 0)
|
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
disconnect(Sock)
|
2015-05-17 05:10:42 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc Append a chunk (binary- or iolist-style) of data to a file
|
|
|
|
%% with `Prefix' and also request an additional `Extra' bytes.
|
|
|
|
%%
|
|
|
|
%% For example, if the `Chunk' size is 1 KByte and `Extra' is 4K Bytes, then
|
|
|
|
%% the file offsets that follow `Chunk''s position for the following 4K will
|
|
|
|
%% be reserved by the file sequencer for later write(s) by the
|
|
|
|
%% `write_chunk()' API.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec append_chunk_extra(port_wrap(), machi_dt:epoch_id(), machi_dt:file_prefix(), machi_dt:chunk(), machi_dt:chunk_size()) ->
|
|
|
|
{ok, machi_dt:chunk_pos()} | {error, machi_dt:error_general()} | {error, term()}.
|
2015-05-17 05:10:42 +00:00
|
|
|
append_chunk_extra(Sock, EpochID, Prefix, Chunk, ChunkExtra)
|
|
|
|
when is_integer(ChunkExtra), ChunkExtra >= 0 ->
|
|
|
|
append_chunk2(Sock, EpochID, Prefix, Chunk, ChunkExtra).
|
|
|
|
|
|
|
|
%% @doc Append a chunk (binary- or iolist-style) of data to a file
|
|
|
|
%% with `Prefix' and also request an additional `Extra' bytes.
|
|
|
|
%%
|
|
|
|
%% For example, if the `Chunk' size is 1 KByte and `Extra' is 4K Bytes, then
|
|
|
|
%% the file offsets that follow `Chunk''s position for the following 4K will
|
|
|
|
%% be reserved by the file sequencer for later write(s) by the
|
|
|
|
%% `write_chunk()' API.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec append_chunk_extra(machi_dt:inet_host(), machi_dt:inet_port(),
|
|
|
|
machi_dt:epoch_id(), machi_dt:file_prefix(), machi_dt:chunk(), machi_dt:chunk_size()) ->
|
|
|
|
{ok, machi_dt:chunk_pos()} | {error, machi_dt:error_general()} | {error, term()}.
|
2015-05-17 05:10:42 +00:00
|
|
|
append_chunk_extra(Host, TcpPort, EpochID, Prefix, Chunk, ChunkExtra)
|
|
|
|
when is_integer(ChunkExtra), ChunkExtra >= 0 ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock = connect(#p_srvr{proto_mod=?MODULE, address=Host, port=TcpPort}),
|
2015-05-17 05:10:42 +00:00
|
|
|
try
|
|
|
|
append_chunk2(Sock, EpochID, Prefix, Chunk, ChunkExtra)
|
2015-03-31 07:46:03 +00:00
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
disconnect(Sock)
|
2015-03-31 07:46:03 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc Read a chunk of data of size `Size' from `File' at `Offset'.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec read_chunk(port_wrap(), machi_dt:epoch_id(), machi_dt:file_name(), machi_dt:file_offset(), machi_dt:chunk_size()) ->
|
|
|
|
{ok, machi_dt:chunk_s()} |
|
|
|
|
{error, machi_dt:error_general() | 'not_written' | 'partial_read'} |
|
2015-05-08 10:07:57 +00:00
|
|
|
{error, term()}.
|
2015-04-02 11:31:10 +00:00
|
|
|
read_chunk(Sock, EpochID, File, Offset, Size)
|
2015-04-02 03:38:12 +00:00
|
|
|
when Offset >= ?MINIMUM_OFFSET, Size >= 0 ->
|
2015-04-02 11:31:10 +00:00
|
|
|
read_chunk2(Sock, EpochID, File, Offset, Size).
|
2015-03-31 07:46:03 +00:00
|
|
|
|
|
|
|
%% @doc Read a chunk of data of size `Size' from `File' at `Offset'.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec read_chunk(machi_dt:inet_host(), machi_dt:inet_port(), machi_dt:epoch_id(),
|
|
|
|
machi_dt:file_name(), machi_dt:file_offset(), machi_dt:chunk_size()) ->
|
|
|
|
{ok, machi_dt:chunk_s()} |
|
|
|
|
{error, machi_dt:error_general() | 'not_written' | 'partial_read'} |
|
2015-05-08 10:07:57 +00:00
|
|
|
{error, term()}.
|
2015-04-02 11:31:10 +00:00
|
|
|
read_chunk(Host, TcpPort, EpochID, File, Offset, Size)
|
2015-04-02 03:38:12 +00:00
|
|
|
when Offset >= ?MINIMUM_OFFSET, Size >= 0 ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock = connect(#p_srvr{proto_mod=?MODULE, address=Host, port=TcpPort}),
|
2015-03-31 07:46:03 +00:00
|
|
|
try
|
2015-04-02 11:31:10 +00:00
|
|
|
read_chunk2(Sock, EpochID, File, Offset, Size)
|
2015-03-31 07:46:03 +00:00
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
disconnect(Sock)
|
2015-03-31 07:46:03 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc Fetch the list of chunk checksums for `File'.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec checksum_list(port_wrap(), machi_dt:epoch_id(), machi_dt:file_name()) ->
|
|
|
|
{ok, [machi_dt:chunk_summary()]} |
|
|
|
|
{error, machi_dt:error_general() | 'no_such_file' | 'partial_read'} |
|
2015-05-08 10:07:57 +00:00
|
|
|
{error, term()}.
|
2015-05-17 07:18:30 +00:00
|
|
|
checksum_list(Sock, EpochID, File) ->
|
2015-04-02 11:49:45 +00:00
|
|
|
checksum_list2(Sock, EpochID, File).
|
2015-03-31 07:46:03 +00:00
|
|
|
|
|
|
|
%% @doc Fetch the list of chunk checksums for `File'.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec checksum_list(machi_dt:inet_host(), machi_dt:inet_port(), machi_dt:epoch_id(), machi_dt:file_name()) ->
|
|
|
|
{ok, [machi_dt:chunk_summary()]} |
|
|
|
|
{error, machi_dt:error_general() | 'no_such_file'} | {error, term()}.
|
2015-04-02 11:49:45 +00:00
|
|
|
checksum_list(Host, TcpPort, EpochID, File) when is_integer(TcpPort) ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock = connect(#p_srvr{proto_mod=?MODULE, address=Host, port=TcpPort}),
|
2015-03-31 07:46:03 +00:00
|
|
|
try
|
2015-04-02 11:49:45 +00:00
|
|
|
checksum_list2(Sock, EpochID, File)
|
2015-03-31 07:46:03 +00:00
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
disconnect(Sock)
|
2015-03-31 07:46:03 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc Fetch the list of all files on the remote FLU.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec list_files(port_wrap(), machi_dt:epoch_id()) ->
|
|
|
|
{ok, [machi_dt:file_info()]} | {error, term()}.
|
2015-05-17 07:18:30 +00:00
|
|
|
list_files(Sock, EpochID) ->
|
2015-04-02 12:01:48 +00:00
|
|
|
list2(Sock, EpochID).
|
2015-03-31 07:46:03 +00:00
|
|
|
|
|
|
|
%% @doc Fetch the list of all files on the remote FLU.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec list_files(machi_dt:inet_host(), machi_dt:inet_port(), machi_dt:epoch_id()) ->
|
|
|
|
{ok, [machi_dt:file_info()]} | {error, term()}.
|
2015-04-02 12:01:48 +00:00
|
|
|
list_files(Host, TcpPort, EpochID) when is_integer(TcpPort) ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock = connect(#p_srvr{proto_mod=?MODULE, address=Host, port=TcpPort}),
|
2015-03-31 07:46:03 +00:00
|
|
|
try
|
2015-04-02 12:01:48 +00:00
|
|
|
list2(Sock, EpochID)
|
2015-03-31 07:46:03 +00:00
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
disconnect(Sock)
|
2015-03-31 07:46:03 +00:00
|
|
|
end.
|
|
|
|
|
2015-05-08 07:53:10 +00:00
|
|
|
%% @doc Fetch the wedge status from the remote FLU.
|
|
|
|
|
2015-05-17 07:18:30 +00:00
|
|
|
-spec wedge_status(port_wrap()) ->
|
2015-06-19 07:24:57 +00:00
|
|
|
{ok, {boolean(), machi_dt:epoch_id()}} | {error, term()}.
|
2015-05-08 07:53:10 +00:00
|
|
|
|
2015-05-17 07:18:30 +00:00
|
|
|
wedge_status(Sock) ->
|
2015-05-08 07:53:10 +00:00
|
|
|
wedge_status2(Sock).
|
|
|
|
|
|
|
|
%% @doc Fetch the wedge status from the remote FLU.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec wedge_status(machi_dt:inet_host(), machi_dt:inet_port()) ->
|
|
|
|
{ok, {boolean(), machi_dt:epoch_id()}} | {error, term()}.
|
2015-05-08 07:53:10 +00:00
|
|
|
wedge_status(Host, TcpPort) when is_integer(TcpPort) ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock = connect(#p_srvr{proto_mod=?MODULE, address=Host, port=TcpPort}),
|
2015-05-08 07:53:10 +00:00
|
|
|
try
|
|
|
|
wedge_status2(Sock)
|
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
disconnect(Sock)
|
2015-05-08 07:53:10 +00:00
|
|
|
end.
|
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Get the latest epoch number + checksum from the FLU's projection store.
|
2015-04-03 08:55:35 +00:00
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec get_latest_epochid(port_wrap(), machi_dt:projection_type()) ->
|
|
|
|
{ok, machi_dt:epoch_id()} | {error, term()}.
|
2015-05-18 10:06:06 +00:00
|
|
|
get_latest_epochid(Sock, ProjType)
|
2015-04-03 08:55:35 +00:00
|
|
|
when ProjType == 'public' orelse ProjType == 'private' ->
|
2015-05-18 10:06:06 +00:00
|
|
|
get_latest_epochid2(Sock, ProjType).
|
2015-04-03 08:55:35 +00:00
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Get the latest epoch number + checksum from the FLU's projection store.
|
2015-04-03 08:55:35 +00:00
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec get_latest_epochid(machi_dt:inet_host(), machi_dt:inet_port(), machi_dt:projection_type()) ->
|
|
|
|
{ok, machi_dt:epoch_id()} | {error, term()}.
|
2015-05-18 10:06:06 +00:00
|
|
|
get_latest_epochid(Host, TcpPort, ProjType)
|
2015-04-03 08:55:35 +00:00
|
|
|
when ProjType == 'public' orelse ProjType == 'private' ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock = connect(#p_srvr{proto_mod=?MODULE, address=Host, port=TcpPort}),
|
2015-04-03 08:55:35 +00:00
|
|
|
try
|
2015-05-18 10:06:06 +00:00
|
|
|
get_latest_epochid2(Sock, ProjType)
|
2015-04-03 08:55:35 +00:00
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
disconnect(Sock)
|
2015-04-03 08:55:35 +00:00
|
|
|
end.
|
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Get the latest projection from the FLU's projection store for `ProjType'
|
2015-04-03 09:37:09 +00:00
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec read_latest_projection(port_wrap(), machi_dt:projection_type()) ->
|
|
|
|
{ok, machi_dt:projection()} | {error, not_written} | {error, term()}.
|
2015-04-03 09:37:09 +00:00
|
|
|
read_latest_projection(Sock, ProjType)
|
|
|
|
when ProjType == 'public' orelse ProjType == 'private' ->
|
|
|
|
read_latest_projection2(Sock, ProjType).
|
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Get the latest projection from the FLU's projection store for `ProjType'
|
2015-04-03 09:37:09 +00:00
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec read_latest_projection(machi_dt:inet_host(), machi_dt:inet_port(),
|
|
|
|
machi_dt:projection_type()) ->
|
|
|
|
{ok, machi_dt:projection()} | {error, not_written} | {error, term()}.
|
2015-04-03 09:37:09 +00:00
|
|
|
read_latest_projection(Host, TcpPort, ProjType)
|
|
|
|
when ProjType == 'public' orelse ProjType == 'private' ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock = connect(#p_srvr{proto_mod=?MODULE, address=Host, port=TcpPort}),
|
2015-04-03 09:37:09 +00:00
|
|
|
try
|
|
|
|
read_latest_projection2(Sock, ProjType)
|
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
disconnect(Sock)
|
2015-04-03 09:37:09 +00:00
|
|
|
end.
|
|
|
|
|
2015-04-03 08:55:35 +00:00
|
|
|
%% @doc Read a projection `Proj' of type `ProjType'.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec read_projection(port_wrap(), machi_dt:projection_type(), machi_dt:epoch_num()) ->
|
|
|
|
{ok, machi_dt:projection()} | {error, not_written} | {error, term()}.
|
2015-04-03 08:55:35 +00:00
|
|
|
read_projection(Sock, ProjType, Epoch)
|
|
|
|
when ProjType == 'public' orelse ProjType == 'private' ->
|
|
|
|
read_projection2(Sock, ProjType, Epoch).
|
|
|
|
|
|
|
|
%% @doc Read a projection `Proj' of type `ProjType'.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec read_projection(machi_dt:inet_host(), machi_dt:inet_port(),
|
|
|
|
machi_dt:projection_type(), machi_dt:epoch_num()) ->
|
|
|
|
{ok, machi_dt:projection()} | {error, not_written} | {error, term()}.
|
2015-04-03 08:55:35 +00:00
|
|
|
read_projection(Host, TcpPort, ProjType, Epoch)
|
|
|
|
when ProjType == 'public' orelse ProjType == 'private' ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock = connect(#p_srvr{proto_mod=?MODULE, address=Host, port=TcpPort}),
|
2015-04-03 08:55:35 +00:00
|
|
|
try
|
|
|
|
read_projection2(Sock, ProjType, Epoch)
|
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
disconnect(Sock)
|
2015-04-03 08:55:35 +00:00
|
|
|
end.
|
|
|
|
|
2015-04-03 03:33:47 +00:00
|
|
|
%% @doc Write a projection `Proj' of type `ProjType'.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec write_projection(port_wrap(), machi_dt:projection_type(), machi_dt:projection()) ->
|
2015-05-19 06:15:05 +00:00
|
|
|
'ok' | {error, 'written'} | {error, term()}.
|
2015-04-03 08:10:52 +00:00
|
|
|
write_projection(Sock, ProjType, Proj)
|
|
|
|
when ProjType == 'public' orelse ProjType == 'private',
|
|
|
|
is_record(Proj, projection_v1) ->
|
2015-04-03 03:33:47 +00:00
|
|
|
write_projection2(Sock, ProjType, Proj).
|
|
|
|
|
|
|
|
%% @doc Write a projection `Proj' of type `ProjType'.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec write_projection(machi_dt:inet_host(), machi_dt:inet_port(),
|
|
|
|
machi_dt:projection_type(), machi_dt:projection()) ->
|
2015-05-19 06:15:05 +00:00
|
|
|
'ok' | {error, 'written'} | {error, term()}.
|
2015-04-03 08:10:52 +00:00
|
|
|
write_projection(Host, TcpPort, ProjType, Proj)
|
|
|
|
when ProjType == 'public' orelse ProjType == 'private',
|
|
|
|
is_record(Proj, projection_v1) ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock = connect(#p_srvr{proto_mod=?MODULE, address=Host, port=TcpPort}),
|
2015-04-03 03:33:47 +00:00
|
|
|
try
|
|
|
|
write_projection2(Sock, ProjType, Proj)
|
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
disconnect(Sock)
|
2015-04-03 03:33:47 +00:00
|
|
|
end.
|
|
|
|
|
2015-04-03 09:37:09 +00:00
|
|
|
%% @doc Get all projections from the FLU's projection store.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec get_all_projections(port_wrap(), machi_dt:projection_type()) ->
|
|
|
|
{ok, [machi_dt:projection()]} | {error, term()}.
|
2015-04-06 09:43:52 +00:00
|
|
|
get_all_projections(Sock, ProjType)
|
2015-04-03 09:37:09 +00:00
|
|
|
when ProjType == 'public' orelse ProjType == 'private' ->
|
2015-04-06 09:43:52 +00:00
|
|
|
get_all_projections2(Sock, ProjType).
|
2015-04-03 09:37:09 +00:00
|
|
|
|
|
|
|
%% @doc Get all projections from the FLU's projection store.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec get_all_projections(machi_dt:inet_host(), machi_dt:inet_port(), machi_dt:projection_type()) ->
|
|
|
|
{ok, [machi_dt:projection()]} | {error, term()}.
|
2015-04-06 09:43:52 +00:00
|
|
|
get_all_projections(Host, TcpPort, ProjType)
|
2015-04-03 09:37:09 +00:00
|
|
|
when ProjType == 'public' orelse ProjType == 'private' ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock = connect(#p_srvr{proto_mod=?MODULE, address=Host, port=TcpPort}),
|
2015-04-03 09:37:09 +00:00
|
|
|
try
|
2015-04-06 09:43:52 +00:00
|
|
|
get_all_projections2(Sock, ProjType)
|
2015-04-03 09:37:09 +00:00
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
disconnect(Sock)
|
2015-04-03 09:37:09 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc Get all epoch numbers from the FLU's projection store.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec list_all_projections(port_wrap(), machi_dt:projection_type()) ->
|
2015-04-03 09:37:09 +00:00
|
|
|
{ok, [non_neg_integer()]} | {error, term()}.
|
2015-04-06 09:43:52 +00:00
|
|
|
list_all_projections(Sock, ProjType)
|
2015-04-03 09:37:09 +00:00
|
|
|
when ProjType == 'public' orelse ProjType == 'private' ->
|
2015-04-06 09:43:52 +00:00
|
|
|
list_all_projections2(Sock, ProjType).
|
2015-04-03 09:37:09 +00:00
|
|
|
|
|
|
|
%% @doc Get all epoch numbers from the FLU's projection store.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec list_all_projections(machi_dt:inet_host(), machi_dt:inet_port(), machi_dt:projection_type()) ->
|
2015-04-03 09:37:09 +00:00
|
|
|
{ok, [non_neg_integer()]} | {error, term()}.
|
2015-04-06 09:43:52 +00:00
|
|
|
list_all_projections(Host, TcpPort, ProjType)
|
2015-04-03 09:37:09 +00:00
|
|
|
when ProjType == 'public' orelse ProjType == 'private' ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock = connect(#p_srvr{proto_mod=?MODULE, address=Host, port=TcpPort}),
|
2015-04-03 09:37:09 +00:00
|
|
|
try
|
2015-04-06 09:43:52 +00:00
|
|
|
list_all_projections2(Sock, ProjType)
|
2015-04-03 09:37:09 +00:00
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
disconnect(Sock)
|
2015-04-03 09:37:09 +00:00
|
|
|
end.
|
|
|
|
|
2015-03-31 07:46:03 +00:00
|
|
|
%% @doc Quit & close the connection to remote FLU.
|
|
|
|
|
2015-05-17 07:18:30 +00:00
|
|
|
-spec quit(port_wrap()) ->
|
2015-03-31 07:46:03 +00:00
|
|
|
ok.
|
2015-05-17 07:18:30 +00:00
|
|
|
quit(Sock) ->
|
|
|
|
catch (_ = w_send(Sock, <<"QUIT\n">>)),
|
|
|
|
disconnect(Sock),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
connected_p({w,tcp,Sock}) ->
|
|
|
|
case (catch inet:peername(Sock)) of
|
|
|
|
{ok, _} -> true;
|
|
|
|
_ -> false
|
|
|
|
end;
|
|
|
|
connected_p(_) ->
|
|
|
|
false.
|
|
|
|
|
|
|
|
connect(#p_srvr{}=P) ->
|
|
|
|
w_connect(P).
|
|
|
|
|
|
|
|
disconnect({w,tcp,_Sock}=WS) ->
|
|
|
|
w_close(WS),
|
|
|
|
ok;
|
|
|
|
disconnect(_) ->
|
2015-03-31 07:46:03 +00:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
%% @doc Restricted API: Write a chunk of already-sequenced data to
|
|
|
|
%% `File' at `Offset'.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec write_chunk(port_wrap(), machi_dt:epoch_id(), machi_dt:file_name(), machi_dt:file_offset(), machi_dt:chunk()) ->
|
|
|
|
ok | {error, machi_dt:error_general()} | {error, term()}.
|
2015-04-02 12:18:41 +00:00
|
|
|
write_chunk(Sock, EpochID, File, Offset, Chunk)
|
2015-04-02 03:38:12 +00:00
|
|
|
when Offset >= ?MINIMUM_OFFSET ->
|
2015-04-02 12:18:41 +00:00
|
|
|
write_chunk2(Sock, EpochID, File, Offset, Chunk).
|
2015-03-31 07:46:03 +00:00
|
|
|
|
|
|
|
%% @doc Restricted API: Write a chunk of already-sequenced data to
|
|
|
|
%% `File' at `Offset'.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec write_chunk(machi_dt:inet_host(), machi_dt:inet_port(),
|
|
|
|
machi_dt:epoch_id(), machi_dt:file_name(), machi_dt:file_offset(), machi_dt:chunk()) ->
|
|
|
|
ok | {error, machi_dt:error_general()} | {error, term()}.
|
2015-04-02 12:18:41 +00:00
|
|
|
write_chunk(Host, TcpPort, EpochID, File, Offset, Chunk)
|
2015-04-02 03:38:12 +00:00
|
|
|
when Offset >= ?MINIMUM_OFFSET ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock = connect(#p_srvr{proto_mod=?MODULE, address=Host, port=TcpPort}),
|
2015-03-31 07:46:03 +00:00
|
|
|
try
|
2015-04-02 12:18:41 +00:00
|
|
|
write_chunk2(Sock, EpochID, File, Offset, Chunk)
|
2015-03-31 07:46:03 +00:00
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
disconnect(Sock)
|
2015-03-31 07:46:03 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc Restricted API: Delete a file after it has been successfully
|
|
|
|
%% migrated.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec delete_migration(port_wrap(), machi_dt:epoch_id(), machi_dt:file_name()) ->
|
|
|
|
ok | {error, machi_dt:error_general() | 'no_such_file'} | {error, term()}.
|
2015-05-17 07:18:30 +00:00
|
|
|
delete_migration(Sock, EpochID, File) ->
|
2015-04-02 12:18:41 +00:00
|
|
|
delete_migration2(Sock, EpochID, File).
|
2015-03-31 07:46:03 +00:00
|
|
|
|
|
|
|
%% @doc Restricted API: Delete a file after it has been successfully
|
|
|
|
%% migrated.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec delete_migration(machi_dt:inet_host(), machi_dt:inet_port(), machi_dt:epoch_id(), machi_dt:file_name()) ->
|
|
|
|
ok | {error, machi_dt:error_general() | 'no_such_file'} | {error, term()}.
|
2015-04-02 12:18:41 +00:00
|
|
|
delete_migration(Host, TcpPort, EpochID, File) when is_integer(TcpPort) ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock = connect(#p_srvr{proto_mod=?MODULE, address=Host, port=TcpPort}),
|
2015-03-31 07:46:03 +00:00
|
|
|
try
|
2015-04-02 12:18:41 +00:00
|
|
|
delete_migration2(Sock, EpochID, File)
|
2015-03-31 07:46:03 +00:00
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
disconnect(Sock)
|
2015-03-31 07:46:03 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc Restricted API: Truncate a file after it has been successfully
|
|
|
|
%% erasure coded.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec trunc_hack(port_wrap(), machi_dt:epoch_id(), machi_dt:file_name()) ->
|
|
|
|
ok | {error, machi_dt:error_general() | 'no_such_file'} | {error, term()}.
|
2015-05-17 07:18:30 +00:00
|
|
|
trunc_hack(Sock, EpochID, File) ->
|
2015-04-02 12:18:41 +00:00
|
|
|
trunc_hack2(Sock, EpochID, File).
|
2015-03-31 07:46:03 +00:00
|
|
|
|
|
|
|
%% @doc Restricted API: Truncate a file after it has been successfully
|
|
|
|
%% erasure coded.
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec trunc_hack(machi_dt:inet_host(), machi_dt:inet_port(), machi_dt:epoch_id(), machi_dt:file_name()) ->
|
|
|
|
ok | {error, machi_dt:error_general() | 'no_such_file'} | {error, term()}.
|
2015-04-02 12:18:41 +00:00
|
|
|
trunc_hack(Host, TcpPort, EpochID, File) when is_integer(TcpPort) ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock = connect(#p_srvr{proto_mod=?MODULE, address=Host, port=TcpPort}),
|
2015-03-31 07:46:03 +00:00
|
|
|
try
|
2015-04-02 12:18:41 +00:00
|
|
|
trunc_hack2(Sock, EpochID, File)
|
2015-03-31 07:46:03 +00:00
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
disconnect(Sock)
|
2015-03-31 07:46:03 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2015-05-17 05:10:42 +00:00
|
|
|
append_chunk2(Sock, EpochID, Prefix0, Chunk0, ChunkExtra) ->
|
2015-04-06 09:43:52 +00:00
|
|
|
erase(bad_sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
try
|
|
|
|
%% TODO: add client-side checksum to the server's protocol
|
2015-04-08 05:24:07 +00:00
|
|
|
%% _ = machi_util:checksum_chunk(Chunk),
|
2015-03-31 07:46:03 +00:00
|
|
|
Prefix = machi_util:make_binary(Prefix0),
|
2015-06-01 05:25:55 +00:00
|
|
|
{CSum, Chunk} = case Chunk0 of
|
|
|
|
{_,_} ->
|
|
|
|
Chunk0;
|
|
|
|
XX when is_binary(XX) ->
|
|
|
|
SHA = machi_util:checksum_chunk(Chunk0),
|
2015-06-23 05:08:10 +00:00
|
|
|
{<<?CSUM_TAG_CLIENT_SHA:8, SHA/binary>>, Chunk0}
|
2015-06-01 05:25:55 +00:00
|
|
|
end,
|
|
|
|
Len = iolist_size(Chunk),
|
2015-03-31 07:46:03 +00:00
|
|
|
true = (Len =< ?MAX_CHUNK_SIZE),
|
2015-04-02 09:08:42 +00:00
|
|
|
{EpochNum, EpochCSum} = EpochID,
|
2015-05-13 09:57:38 +00:00
|
|
|
EpochIDHex = machi_util:bin_to_hexstr(
|
|
|
|
<<EpochNum:(4*8)/big, EpochCSum/binary>>),
|
2015-06-01 05:25:55 +00:00
|
|
|
CSumHex = machi_util:bin_to_hexstr(CSum),
|
2015-03-31 07:46:03 +00:00
|
|
|
LenHex = machi_util:int_to_hexbin(Len, 32),
|
2015-05-17 05:10:42 +00:00
|
|
|
ExtraHex = machi_util:int_to_hexbin(ChunkExtra, 32),
|
2015-06-01 05:25:55 +00:00
|
|
|
Cmd = [<<"A ">>, EpochIDHex, CSumHex, LenHex, ExtraHex, Prefix, 10],
|
2015-05-17 07:18:30 +00:00
|
|
|
ok = w_send(Sock, [Cmd, Chunk]),
|
|
|
|
{ok, Line} = w_recv(Sock, 0),
|
2015-06-01 05:25:55 +00:00
|
|
|
PathLen = byte_size(Line) - 3 - (2*(1+20)) - 16 - 1 - 1 - 1,
|
2015-03-31 07:46:03 +00:00
|
|
|
case Line of
|
2015-06-01 05:25:55 +00:00
|
|
|
<<"OK ", ServerCSum:(2*(1+20))/binary, " ",
|
|
|
|
OffsetHex:16/binary, " ",
|
2015-03-31 07:46:03 +00:00
|
|
|
Path:PathLen/binary, _:1/binary>> ->
|
|
|
|
Offset = machi_util:hexstr_to_int(OffsetHex),
|
|
|
|
{ok, {Offset, Len, Path}};
|
|
|
|
<<"ERROR BAD-ARG", _/binary>> ->
|
|
|
|
{error, bad_arg};
|
2015-05-08 10:07:57 +00:00
|
|
|
<<"ERROR WEDGED", _/binary>> ->
|
|
|
|
{error, wedged};
|
2015-06-01 05:25:55 +00:00
|
|
|
<<"ERROR BAD-CHECKSUM", _/binary>> ->
|
|
|
|
{error, bad_checksum};
|
2015-03-31 07:46:03 +00:00
|
|
|
<<"ERROR ", Rest/binary>> ->
|
|
|
|
{error, Rest}
|
|
|
|
end
|
|
|
|
catch
|
|
|
|
throw:Error ->
|
2015-04-06 09:43:52 +00:00
|
|
|
put(bad_sock, Sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
Error;
|
|
|
|
error:{badmatch,_}=BadMatch ->
|
2015-04-06 09:43:52 +00:00
|
|
|
put(bad_sock, Sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
{error, {badmatch, BadMatch, erlang:get_stacktrace()}}
|
|
|
|
end.
|
|
|
|
|
2015-04-02 11:31:10 +00:00
|
|
|
read_chunk2(Sock, EpochID, File0, Offset, Size) ->
|
2015-04-06 09:43:52 +00:00
|
|
|
erase(bad_sock),
|
|
|
|
try
|
|
|
|
{EpochNum, EpochCSum} = EpochID,
|
2015-05-13 09:57:38 +00:00
|
|
|
EpochIDHex = machi_util:bin_to_hexstr(
|
|
|
|
<<EpochNum:(4*8)/big, EpochCSum/binary>>),
|
2015-04-06 09:43:52 +00:00
|
|
|
File = machi_util:make_binary(File0),
|
|
|
|
PrefixHex = machi_util:int_to_hexbin(Offset, 64),
|
|
|
|
SizeHex = machi_util:int_to_hexbin(Size, 32),
|
2015-05-13 09:57:38 +00:00
|
|
|
CmdLF = [$R, 32, EpochIDHex, PrefixHex, SizeHex, File, 10],
|
2015-05-17 07:18:30 +00:00
|
|
|
ok = w_send(Sock, CmdLF),
|
2015-05-18 10:06:06 +00:00
|
|
|
ok = w_setopts(Sock, [{packet, raw}]),
|
2015-05-17 07:18:30 +00:00
|
|
|
case w_recv(Sock, 3) of
|
2015-04-06 09:43:52 +00:00
|
|
|
{ok, <<"OK\n">>} ->
|
2015-05-17 07:18:30 +00:00
|
|
|
{ok, _Chunk}=Res = w_recv(Sock, Size),
|
2015-04-06 09:43:52 +00:00
|
|
|
Res;
|
|
|
|
{ok, Else} ->
|
2015-05-17 07:18:30 +00:00
|
|
|
ok = w_setopts(Sock, [{packet, line}]),
|
|
|
|
{ok, Else2} = w_recv(Sock, 0),
|
2015-04-06 09:43:52 +00:00
|
|
|
case Else of
|
|
|
|
<<"ERA">> ->
|
|
|
|
{error, todo_erasure_coded}; %% escript_cc_parse_ec_info(Sock, Line, Else2);
|
|
|
|
<<"ERR">> ->
|
|
|
|
case Else2 of
|
2015-05-18 14:26:21 +00:00
|
|
|
<<"OR NO-SUCH-FILE\n">> ->
|
|
|
|
{error, not_written};
|
2015-04-06 09:43:52 +00:00
|
|
|
<<"OR NOT-ERASURE\n">> ->
|
2015-05-19 06:15:05 +00:00
|
|
|
%% {error, no_such_file};
|
|
|
|
%% Ignore the fact that the file doesn't exist.
|
|
|
|
{error, not_written};
|
2015-04-06 09:43:52 +00:00
|
|
|
<<"OR BAD-ARG\n">> ->
|
|
|
|
{error, bad_arg};
|
|
|
|
<<"OR PARTIAL-READ\n">> ->
|
|
|
|
{error, partial_read};
|
2015-05-08 10:07:57 +00:00
|
|
|
<<"OR WEDGED", _/binary>> ->
|
|
|
|
{error, wedged};
|
2015-04-06 09:43:52 +00:00
|
|
|
_ ->
|
|
|
|
{error, Else2}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{error, {whaaa_todo, <<Else/binary, Else2/binary>>}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
catch
|
|
|
|
throw:Error ->
|
|
|
|
put(bad_sock, Sock),
|
|
|
|
Error;
|
2015-05-18 10:06:06 +00:00
|
|
|
error:{case_clause,_}=Noo ->
|
|
|
|
put(bad_sock, Sock),
|
|
|
|
{error, {badmatch, Noo, erlang:get_stacktrace()}};
|
2015-04-06 09:43:52 +00:00
|
|
|
error:{badmatch,_}=BadMatch ->
|
|
|
|
put(bad_sock, Sock),
|
|
|
|
{error, {badmatch, BadMatch, erlang:get_stacktrace()}}
|
2015-03-31 07:46:03 +00:00
|
|
|
end.
|
|
|
|
|
2015-04-02 12:01:48 +00:00
|
|
|
list2(Sock, EpochID) ->
|
2015-03-31 07:46:03 +00:00
|
|
|
try
|
2015-04-02 12:01:48 +00:00
|
|
|
{EpochNum, EpochCSum} = EpochID,
|
2015-05-13 09:57:38 +00:00
|
|
|
EpochIDHex = machi_util:bin_to_hexstr(
|
|
|
|
<<EpochNum:(4*8)/big, EpochCSum/binary>>),
|
2015-05-17 07:18:30 +00:00
|
|
|
ok = w_send(Sock, [<<"L ">>, EpochIDHex, <<"\n">>]),
|
|
|
|
ok = w_setopts(Sock, [{packet, line}]),
|
|
|
|
case w_recv(Sock, 0) of
|
2015-05-08 12:37:19 +00:00
|
|
|
{ok, <<"OK\n">>} ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Res = list3(w_recv(Sock, 0), Sock),
|
|
|
|
ok = w_setopts(Sock, [{packet, raw}]),
|
2015-05-08 12:37:19 +00:00
|
|
|
{ok, Res};
|
|
|
|
{ok, <<"ERROR WEDGED\n">>} ->
|
|
|
|
{error, wedged};
|
|
|
|
{ok, <<"ERROR ", Rest/binary>>} ->
|
|
|
|
{error, Rest}
|
|
|
|
end
|
2015-03-31 07:46:03 +00:00
|
|
|
catch
|
|
|
|
throw:Error ->
|
|
|
|
Error;
|
2015-05-18 10:06:06 +00:00
|
|
|
error:{case_clause,_}=Noo ->
|
|
|
|
put(bad_sock, Sock),
|
|
|
|
{error, {badmatch, Noo, erlang:get_stacktrace()}};
|
2015-03-31 07:46:03 +00:00
|
|
|
error:{badmatch,_}=BadMatch ->
|
2015-05-18 10:06:06 +00:00
|
|
|
put(bad_sock, Sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
{error, {badmatch, BadMatch}}
|
|
|
|
end.
|
|
|
|
|
2015-04-02 12:01:48 +00:00
|
|
|
list3({ok, <<".\n">>}, _Sock) ->
|
2015-03-31 07:46:03 +00:00
|
|
|
[];
|
2015-04-02 12:01:48 +00:00
|
|
|
list3({ok, Line}, Sock) ->
|
2015-04-01 08:59:40 +00:00
|
|
|
FileLen = byte_size(Line) - 16 - 1 - 1,
|
|
|
|
<<SizeHex:16/binary, " ", File:FileLen/binary, _/binary>> = Line,
|
|
|
|
Size = machi_util:hexstr_to_int(SizeHex),
|
2015-05-17 07:18:30 +00:00
|
|
|
[{Size, File}|list3(w_recv(Sock, 0), Sock)];
|
2015-04-02 12:01:48 +00:00
|
|
|
list3(Else, _Sock) ->
|
2015-03-31 07:46:03 +00:00
|
|
|
throw({server_protocol_error, Else}).
|
|
|
|
|
2015-05-08 07:53:10 +00:00
|
|
|
wedge_status2(Sock) ->
|
|
|
|
try
|
2015-05-17 07:18:30 +00:00
|
|
|
ok = w_send(Sock, [<<"WEDGE-STATUS\n">>]),
|
|
|
|
ok = w_setopts(Sock, [{packet, line}]),
|
2015-05-08 07:53:10 +00:00
|
|
|
{ok, <<"OK ",
|
|
|
|
BooleanHex:2/binary, " ",
|
|
|
|
EpochHex:8/binary, " ",
|
2015-05-17 07:18:30 +00:00
|
|
|
CSumHex:40/binary, "\n">>} = w_recv(Sock, 0),
|
|
|
|
ok = w_setopts(Sock, [{packet, raw}]),
|
2015-05-08 07:53:10 +00:00
|
|
|
Boolean = if BooleanHex == <<"00">> -> false;
|
|
|
|
BooleanHex == <<"01">> -> true
|
|
|
|
end,
|
|
|
|
Res = {Boolean, {machi_util:hexstr_to_int(EpochHex),
|
|
|
|
machi_util:hexstr_to_bin(CSumHex)}},
|
|
|
|
{ok, Res}
|
|
|
|
catch
|
|
|
|
throw:Error ->
|
|
|
|
Error;
|
2015-05-18 10:06:06 +00:00
|
|
|
error:{case_clause,_}=Noo ->
|
|
|
|
put(bad_sock, Sock),
|
|
|
|
{error, {badmatch, Noo, erlang:get_stacktrace()}};
|
2015-05-08 07:53:10 +00:00
|
|
|
error:{badmatch,_}=BadMatch ->
|
2015-05-18 10:06:06 +00:00
|
|
|
put(bad_sock, Sock),
|
2015-05-08 07:53:10 +00:00
|
|
|
{error, {badmatch, BadMatch}}
|
|
|
|
end.
|
|
|
|
|
2015-04-02 11:49:45 +00:00
|
|
|
checksum_list2(Sock, EpochID, File) ->
|
2015-04-06 09:43:52 +00:00
|
|
|
erase(bad_sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
try
|
2015-04-02 11:49:45 +00:00
|
|
|
{EpochNum, EpochCSum} = EpochID,
|
2015-05-13 09:57:38 +00:00
|
|
|
EpochIDHex = machi_util:bin_to_hexstr(
|
|
|
|
<<EpochNum:(4*8)/big, EpochCSum/binary>>),
|
2015-05-17 07:18:30 +00:00
|
|
|
ok = w_send(Sock, [<<"C ">>, EpochIDHex, File, <<"\n">>]),
|
|
|
|
ok = w_setopts(Sock, [{packet, line}]),
|
|
|
|
case w_recv(Sock, 0) of
|
2015-03-31 07:46:03 +00:00
|
|
|
{ok, <<"OK ", Rest/binary>> = Line} ->
|
|
|
|
put(status, ok), % may be unset later
|
|
|
|
RestLen = byte_size(Rest) - 1,
|
|
|
|
<<LenHex:RestLen/binary, _:1/binary>> = Rest,
|
|
|
|
<<Len:64/big>> = machi_util:hexstr_to_bin(LenHex),
|
2015-05-17 07:18:30 +00:00
|
|
|
ok = w_setopts(Sock, [{packet, raw}]),
|
2015-04-01 08:59:40 +00:00
|
|
|
{ok, checksum_list_finish(checksum_list_fast(Sock, Len))};
|
2015-03-31 07:46:03 +00:00
|
|
|
{ok, <<"ERROR NO-SUCH-FILE", _/binary>>} ->
|
|
|
|
{error, no_such_file};
|
|
|
|
{ok, <<"ERROR BAD-ARG", _/binary>>} ->
|
|
|
|
{error, bad_arg};
|
2015-05-08 10:07:57 +00:00
|
|
|
{ok, <<"ERROR WEDGED", _/binary>>} ->
|
|
|
|
{error, wedged};
|
2015-03-31 07:46:03 +00:00
|
|
|
{ok, Else} ->
|
2015-05-18 10:06:06 +00:00
|
|
|
throw({server_protocol_error, Else})
|
2015-03-31 07:46:03 +00:00
|
|
|
end
|
|
|
|
catch
|
|
|
|
throw:Error ->
|
2015-04-06 09:43:52 +00:00
|
|
|
put(bad_sock, Sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
Error;
|
2015-05-18 10:06:06 +00:00
|
|
|
error:{case_clause,_}=Noo ->
|
|
|
|
put(bad_sock, Sock),
|
|
|
|
{error, {badmatch, Noo, erlang:get_stacktrace()}};
|
2015-03-31 07:46:03 +00:00
|
|
|
error:{badmatch,_}=BadMatch ->
|
2015-04-06 09:43:52 +00:00
|
|
|
put(bad_sock, Sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
{error, {badmatch, BadMatch}}
|
|
|
|
end.
|
|
|
|
|
|
|
|
checksum_list_fast(Sock, 0) ->
|
2015-05-17 07:18:30 +00:00
|
|
|
{ok, <<".\n">> = _Line} = w_recv(Sock, 2),
|
2015-03-31 07:46:03 +00:00
|
|
|
[];
|
|
|
|
checksum_list_fast(Sock, Remaining) ->
|
|
|
|
Num = erlang:min(Remaining, 1024*1024),
|
2015-05-17 07:18:30 +00:00
|
|
|
{ok, Bytes} = w_recv(Sock, Num),
|
2015-03-31 07:46:03 +00:00
|
|
|
[Bytes|checksum_list_fast(Sock, Remaining - byte_size(Bytes))].
|
|
|
|
|
2015-04-01 08:59:40 +00:00
|
|
|
checksum_list_finish(Chunks) ->
|
|
|
|
Bin = case Chunks of
|
|
|
|
[X] ->
|
|
|
|
X;
|
|
|
|
_ ->
|
|
|
|
iolist_to_binary(Chunks)
|
|
|
|
end,
|
|
|
|
[begin
|
|
|
|
CSumLen = byte_size(Line) - 16 - 1 - 8 - 1,
|
|
|
|
<<OffsetHex:16/binary, " ", SizeHex:8/binary, " ",
|
|
|
|
CSum:CSumLen/binary>> = Line,
|
|
|
|
{machi_util:hexstr_to_int(OffsetHex),
|
|
|
|
machi_util:hexstr_to_int(SizeHex),
|
|
|
|
machi_util:hexstr_to_bin(CSum)}
|
|
|
|
end || Line <- re:split(Bin, "\n", [{return, binary}]),
|
|
|
|
Line /= <<>>].
|
2015-03-31 07:46:03 +00:00
|
|
|
|
2015-04-02 12:18:41 +00:00
|
|
|
write_chunk2(Sock, EpochID, File0, Offset, Chunk0) ->
|
2015-04-06 09:43:52 +00:00
|
|
|
erase(bad_sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
try
|
2015-04-02 12:18:41 +00:00
|
|
|
{EpochNum, EpochCSum} = EpochID,
|
2015-05-13 09:57:38 +00:00
|
|
|
EpochIDHex = machi_util:bin_to_hexstr(
|
|
|
|
<<EpochNum:(4*8)/big, EpochCSum/binary>>),
|
2015-03-31 07:46:03 +00:00
|
|
|
%% TODO: add client-side checksum to the server's protocol
|
2015-04-08 05:24:07 +00:00
|
|
|
%% _ = machi_util:checksum_chunk(Chunk),
|
2015-03-31 07:46:03 +00:00
|
|
|
File = machi_util:make_binary(File0),
|
|
|
|
true = (Offset >= ?MINIMUM_OFFSET),
|
|
|
|
OffsetHex = machi_util:int_to_hexbin(Offset, 64),
|
2015-06-01 05:25:55 +00:00
|
|
|
{CSum, Chunk} = case Chunk0 of
|
|
|
|
{_,_} ->
|
|
|
|
Chunk0;
|
|
|
|
XX when is_binary(XX) ->
|
|
|
|
SHA = machi_util:checksum_chunk(Chunk0),
|
2015-06-23 05:08:10 +00:00
|
|
|
{<<?CSUM_TAG_CLIENT_SHA:8, SHA/binary>>, Chunk0}
|
2015-06-01 05:25:55 +00:00
|
|
|
end,
|
|
|
|
CSumHex = machi_util:bin_to_hexstr(CSum),
|
|
|
|
Len = iolist_size(Chunk),
|
2015-03-31 07:46:03 +00:00
|
|
|
true = (Len =< ?MAX_CHUNK_SIZE),
|
|
|
|
LenHex = machi_util:int_to_hexbin(Len, 32),
|
2015-06-01 05:25:55 +00:00
|
|
|
Cmd = [<<"W-repl ">>, EpochIDHex, CSumHex, OffsetHex,
|
2015-04-02 12:18:41 +00:00
|
|
|
LenHex, File, <<"\n">>],
|
2015-05-17 07:18:30 +00:00
|
|
|
ok = w_send(Sock, [Cmd, Chunk]),
|
|
|
|
{ok, Line} = w_recv(Sock, 0),
|
2015-03-31 07:46:03 +00:00
|
|
|
PathLen = byte_size(Line) - 3 - 16 - 1 - 1,
|
|
|
|
case Line of
|
|
|
|
<<"OK\n">> ->
|
|
|
|
ok;
|
|
|
|
<<"ERROR BAD-ARG", _/binary>> ->
|
|
|
|
{error, bad_arg};
|
2015-05-08 10:07:57 +00:00
|
|
|
<<"ERROR WEDGED", _/binary>> ->
|
|
|
|
{error, wedged};
|
2015-06-01 05:25:55 +00:00
|
|
|
<<"ERROR BAD-CHECKSUM", _/binary>> ->
|
|
|
|
{error, bad_checksum};
|
2015-03-31 07:46:03 +00:00
|
|
|
<<"ERROR ", _/binary>>=Else ->
|
|
|
|
{error, {server_said, Else}}
|
|
|
|
end
|
|
|
|
catch
|
|
|
|
throw:Error ->
|
2015-04-06 09:43:52 +00:00
|
|
|
put(bad_sock, Sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
Error;
|
|
|
|
error:{badmatch,_}=BadMatch ->
|
2015-04-06 09:43:52 +00:00
|
|
|
put(bad_sock, Sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
{error, {badmatch, BadMatch, erlang:get_stacktrace()}}
|
|
|
|
end.
|
|
|
|
|
2015-04-02 12:18:41 +00:00
|
|
|
delete_migration2(Sock, EpochID, File) ->
|
2015-04-06 09:43:52 +00:00
|
|
|
erase(bad_sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
try
|
2015-04-02 12:18:41 +00:00
|
|
|
{EpochNum, EpochCSum} = EpochID,
|
2015-05-13 09:57:38 +00:00
|
|
|
EpochIDHex = machi_util:bin_to_hexstr(
|
|
|
|
<<EpochNum:(4*8)/big, EpochCSum/binary>>),
|
|
|
|
Cmd = [<<"DEL-migration ">>, EpochIDHex, File, <<"\n">>],
|
2015-05-17 07:18:30 +00:00
|
|
|
ok = w_send(Sock, Cmd),
|
|
|
|
ok = w_setopts(Sock, [{packet, line}]),
|
|
|
|
case w_recv(Sock, 0) of
|
2015-03-31 07:46:03 +00:00
|
|
|
{ok, <<"OK\n">>} ->
|
|
|
|
ok;
|
|
|
|
{ok, <<"ERROR NO-SUCH-FILE", _/binary>>} ->
|
|
|
|
{error, no_such_file};
|
|
|
|
{ok, <<"ERROR BAD-ARG", _/binary>>} ->
|
|
|
|
{error, bad_arg};
|
2015-05-08 10:07:57 +00:00
|
|
|
{ok, <<"ERROR WEDGED", _/binary>>} ->
|
|
|
|
{error, wedged};
|
2015-03-31 07:46:03 +00:00
|
|
|
{ok, Else} ->
|
|
|
|
throw({server_protocol_error, Else})
|
|
|
|
end
|
|
|
|
catch
|
|
|
|
throw:Error ->
|
2015-04-06 09:43:52 +00:00
|
|
|
put(bad_sock, Sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
Error;
|
2015-05-18 10:06:06 +00:00
|
|
|
error:{case_clause,_}=Noo ->
|
|
|
|
put(bad_sock, Sock),
|
|
|
|
{error, {badmatch, Noo, erlang:get_stacktrace()}};
|
2015-03-31 07:46:03 +00:00
|
|
|
error:{badmatch,_}=BadMatch ->
|
2015-04-06 09:43:52 +00:00
|
|
|
put(bad_sock, Sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
{error, {badmatch, BadMatch}}
|
|
|
|
end.
|
|
|
|
|
2015-04-02 12:18:41 +00:00
|
|
|
trunc_hack2(Sock, EpochID, File) ->
|
2015-04-06 09:43:52 +00:00
|
|
|
erase(bad_sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
try
|
2015-04-02 12:18:41 +00:00
|
|
|
{EpochNum, EpochCSum} = EpochID,
|
2015-05-13 09:57:38 +00:00
|
|
|
EpochIDHex = machi_util:bin_to_hexstr(
|
|
|
|
<<EpochNum:(4*8)/big, EpochCSum/binary>>),
|
|
|
|
Cmd = [<<"TRUNC-hack--- ">>, EpochIDHex, File, <<"\n">>],
|
2015-05-17 07:18:30 +00:00
|
|
|
ok = w_send(Sock, Cmd),
|
|
|
|
ok = w_setopts(Sock, [{packet, line}]),
|
|
|
|
case w_recv(Sock, 0) of
|
2015-03-31 07:46:03 +00:00
|
|
|
{ok, <<"OK\n">>} ->
|
|
|
|
ok;
|
|
|
|
{ok, <<"ERROR NO-SUCH-FILE", _/binary>>} ->
|
|
|
|
{error, no_such_file};
|
|
|
|
{ok, <<"ERROR BAD-ARG", _/binary>>} ->
|
|
|
|
{error, bad_arg};
|
2015-05-08 10:07:57 +00:00
|
|
|
{ok, <<"ERROR WEDGED", _/binary>>} ->
|
|
|
|
{error, wedged};
|
2015-03-31 07:46:03 +00:00
|
|
|
{ok, Else} ->
|
|
|
|
throw({server_protocol_error, Else})
|
|
|
|
end
|
|
|
|
catch
|
|
|
|
throw:Error ->
|
2015-04-06 09:43:52 +00:00
|
|
|
put(bad_sock, Sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
Error;
|
2015-05-18 10:06:06 +00:00
|
|
|
error:{case_clause,_}=Noo ->
|
|
|
|
put(bad_sock, Sock),
|
|
|
|
{error, {badmatch, Noo, erlang:get_stacktrace()}};
|
2015-03-31 07:46:03 +00:00
|
|
|
error:{badmatch,_}=BadMatch ->
|
2015-04-06 09:43:52 +00:00
|
|
|
put(bad_sock, Sock),
|
2015-03-31 07:46:03 +00:00
|
|
|
{error, {badmatch, BadMatch}}
|
|
|
|
end.
|
2015-04-03 03:33:47 +00:00
|
|
|
|
2015-05-18 10:06:06 +00:00
|
|
|
get_latest_epochid2(Sock, ProjType) ->
|
2015-06-24 08:20:18 +00:00
|
|
|
Req = machi_pb_wrap:make_projection_req(
|
|
|
|
<<42>>, {get_latest_epochid, ProjType}),
|
|
|
|
do_projection_common(Sock, Req).
|
2015-04-03 08:55:35 +00:00
|
|
|
|
2015-04-03 09:37:09 +00:00
|
|
|
read_latest_projection2(Sock, ProjType) ->
|
2015-06-24 08:20:18 +00:00
|
|
|
Req = machi_pb_wrap:make_projection_req(
|
|
|
|
<<42>>, {read_latest_projection, ProjType}),
|
|
|
|
do_projection_common(Sock, Req).
|
2015-04-03 09:37:09 +00:00
|
|
|
|
2015-04-03 08:55:35 +00:00
|
|
|
read_projection2(Sock, ProjType, Epoch) ->
|
2015-06-24 08:20:18 +00:00
|
|
|
Req = machi_pb_wrap:make_projection_req(
|
|
|
|
<<42>>, {read_projection, ProjType, Epoch}),
|
|
|
|
do_projection_common(Sock, Req).
|
2015-04-03 08:55:35 +00:00
|
|
|
|
2015-04-03 03:33:47 +00:00
|
|
|
write_projection2(Sock, ProjType, Proj) ->
|
2015-06-24 08:20:18 +00:00
|
|
|
Req = machi_pb_wrap:make_projection_req(
|
|
|
|
<<42>>, {write_projection, ProjType, Proj}),
|
|
|
|
do_projection_common(Sock, Req).
|
2015-04-03 09:37:09 +00:00
|
|
|
|
2015-04-06 09:43:52 +00:00
|
|
|
get_all_projections2(Sock, ProjType) ->
|
2015-06-24 08:20:18 +00:00
|
|
|
Req = machi_pb_wrap:make_projection_req(
|
|
|
|
<<42>>, {get_all_projections, ProjType}),
|
|
|
|
do_projection_common(Sock, Req).
|
2015-04-03 09:37:09 +00:00
|
|
|
|
2015-04-06 09:43:52 +00:00
|
|
|
list_all_projections2(Sock, ProjType) ->
|
2015-06-24 08:20:18 +00:00
|
|
|
Req = machi_pb_wrap:make_projection_req(
|
|
|
|
<<42>>, {list_all_projections, ProjType}),
|
|
|
|
do_projection_common(Sock, Req).
|
2015-04-03 03:33:47 +00:00
|
|
|
|
2015-06-24 08:20:18 +00:00
|
|
|
do_projection_common(Sock, Req) ->
|
2015-04-06 09:43:52 +00:00
|
|
|
erase(bad_sock),
|
2015-04-03 03:33:47 +00:00
|
|
|
try
|
2015-06-25 06:08:40 +00:00
|
|
|
ReqBin = list_to_binary(machi_pb:encode_mpb_ll_request(Req)),
|
2015-06-24 08:20:18 +00:00
|
|
|
Len = iolist_size(ReqBin),
|
2015-04-03 03:33:47 +00:00
|
|
|
true = (Len =< ?MAX_CHUNK_SIZE),
|
|
|
|
LenHex = machi_util:int_to_hexbin(Len, 32),
|
|
|
|
Cmd = [<<"PROJ ">>, LenHex, <<"\n">>],
|
2015-06-24 08:20:18 +00:00
|
|
|
ok = w_send(Sock, [Cmd, ReqBin]),
|
2015-05-17 07:18:30 +00:00
|
|
|
ok = w_setopts(Sock, [{packet, line}]),
|
|
|
|
case w_recv(Sock, 0) of
|
2015-05-12 12:45:40 +00:00
|
|
|
{ok, Line} ->
|
|
|
|
case Line of
|
|
|
|
<<"OK ", ResLenHex:8/binary, "\n">> ->
|
|
|
|
ResLen = machi_util:hexstr_to_int(ResLenHex),
|
2015-05-17 07:18:30 +00:00
|
|
|
ok = w_setopts(Sock, [{packet, raw}]),
|
2015-06-24 08:20:18 +00:00
|
|
|
{ok, RespBin} = w_recv(Sock, ResLen),
|
2015-05-17 07:18:30 +00:00
|
|
|
ok = w_setopts(Sock, [{packet, line}]),
|
2015-06-24 09:00:25 +00:00
|
|
|
Resp = machi_pb:decode_mpb_ll_response(RespBin),
|
2015-06-24 08:20:18 +00:00
|
|
|
machi_pb_wrap:unmake_projection_resp(Resp);
|
2015-05-12 12:45:40 +00:00
|
|
|
Else ->
|
|
|
|
{error, Else}
|
2015-05-18 10:06:06 +00:00
|
|
|
end
|
2015-04-03 03:33:47 +00:00
|
|
|
end
|
|
|
|
catch
|
|
|
|
throw:Error ->
|
2015-04-06 09:43:52 +00:00
|
|
|
put(bad_sock, Sock),
|
2015-04-03 03:33:47 +00:00
|
|
|
Error;
|
2015-05-18 10:06:06 +00:00
|
|
|
error:{case_clause,_}=Noo ->
|
|
|
|
put(bad_sock, Sock),
|
|
|
|
{error, {badmatch, Noo, erlang:get_stacktrace()}};
|
2015-04-03 03:33:47 +00:00
|
|
|
error:{badmatch,_}=BadMatch ->
|
2015-04-06 09:43:52 +00:00
|
|
|
put(bad_sock, Sock),
|
2015-04-03 03:33:47 +00:00
|
|
|
{error, {badmatch, BadMatch, erlang:get_stacktrace()}}
|
|
|
|
end.
|
2015-05-17 07:18:30 +00:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2015-05-17 07:46:50 +00:00
|
|
|
w_connect(#p_srvr{proto_mod=?MODULE, address=Host, port=Port, props=Props})->
|
2015-05-17 07:18:30 +00:00
|
|
|
try
|
2015-05-17 07:46:50 +00:00
|
|
|
case proplists:get_value(session_proto, Props, tcp) of
|
|
|
|
tcp ->
|
2015-05-18 10:06:06 +00:00
|
|
|
Sock = machi_util:connect(Host, Port, ?HARD_TIMEOUT),
|
2015-05-17 07:46:50 +00:00
|
|
|
{w,tcp,Sock};
|
|
|
|
%% sctp ->
|
|
|
|
%% %% TODO: not implemented
|
|
|
|
%% {w,sctp,Sock}
|
|
|
|
ssl ->
|
|
|
|
%% TODO: veryveryuntested
|
|
|
|
SslOptions = proplists:get_value(ssl_options, Props),
|
|
|
|
Sock = machi_util:connect(Port, Port),
|
|
|
|
{ok, SslSock} = ssl:connect(Sock, SslOptions),
|
|
|
|
{w,ssl,SslSock}
|
|
|
|
end
|
2015-05-17 07:18:30 +00:00
|
|
|
catch
|
|
|
|
_:_ ->
|
|
|
|
undefined
|
|
|
|
end.
|
|
|
|
|
|
|
|
w_close({w,tcp,Sock}) ->
|
|
|
|
catch gen_tcp:close(Sock),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
w_recv({w,tcp,Sock}, Amt) ->
|
2015-05-18 10:06:06 +00:00
|
|
|
gen_tcp:recv(Sock, Amt, ?HARD_TIMEOUT).
|
2015-05-17 07:18:30 +00:00
|
|
|
|
|
|
|
w_send({w,tcp,Sock}, IoData) ->
|
|
|
|
gen_tcp:send(Sock, IoData).
|
|
|
|
|
|
|
|
w_setopts({w,tcp,Sock}, Opts) ->
|
|
|
|
inet:setopts(Sock, Opts).
|
|
|
|
|