WIP: epoch ID added to read protocol command

This commit is contained in:
Scott Lystig Fritchie 2015-04-02 20:31:10 +09:00
parent 44bb5e1dae
commit 9479baac46
4 changed files with 28 additions and 29 deletions

View file

@ -32,6 +32,7 @@
-compile(export_all). -compile(export_all).
-include("machi.hrl"). -include("machi.hrl").
-include("machi_projection.hrl").
-define(FLU_C, machi_flu1_client). -define(FLU_C, machi_flu1_client).
@ -73,7 +74,8 @@ verify_file_checksums_local2(Sock1, Path0) ->
verify_file_checksums_remote2(Sock1, File) -> verify_file_checksums_remote2(Sock1, File) ->
ReadChunk = fun(File_name, Offset, Size) -> ReadChunk = fun(File_name, Offset, Size) ->
?FLU_C:read_chunk(Sock1, File_name, Offset, Size) ?FLU_C:read_chunk(Sock1, ?DUMMY_PV1_EPOCH,
File_name, Offset, Size)
end, end,
verify_file_checksums_common(Sock1, File, ReadChunk). verify_file_checksums_common(Sock1, File, ReadChunk).

View file

@ -107,35 +107,23 @@ net_server_loop(Sock, #state{reg_name=RegName, data_dir=DataDir}=S) ->
case gen_tcp:recv(Sock, 0, 60*1000) of case gen_tcp:recv(Sock, 0, 60*1000) of
{ok, Line} -> {ok, Line} ->
%% machi_util:verb("Got: ~p\n", [Line]), %% machi_util:verb("Got: ~p\n", [Line]),
PrefixLenLF = byte_size(Line) - 2 - 8 - 1 - 1,
PrefixLenLF_E = byte_size(Line) - 2 - ?EpochIDSpace - 8 - 1, PrefixLenLF_E = byte_size(Line) - 2 - ?EpochIDSpace - 8 - 1,
PrefixLenCRLF = byte_size(Line) - 2 - 8 - 1 - 2, FileLenLF_E = byte_size(Line) - 2 - ?EpochIDSpace - 16 - 8 - 1,
FileLenLF = byte_size(Line) - 2 - 16 - 1 - 8 - 1 - 1,
FileLenCRLF = byte_size(Line) - 2 - 16 - 1 - 8 - 1 - 2,
CSumFileLenLF = byte_size(Line) - 2 - 1, CSumFileLenLF = byte_size(Line) - 2 - 1,
CSumFileLenCRLF = byte_size(Line) - 2 - 2, CSumFileLenCRLF = byte_size(Line) - 2 - 2,
WriteFileLenLF = byte_size(Line) - 7 - 16 - 1 - 8 - 1 - 1, WriteFileLenLF = byte_size(Line) - 7 - 16 - 1 - 8 - 1 - 1,
DelFileLenLF = byte_size(Line) - 14 - 1, DelFileLenLF = byte_size(Line) - 14 - 1,
case Line of case Line of
%% For normal use %% For normal use
<<"A ", LenHex:8/binary, " ",
Prefix:PrefixLenLF/binary, "\n">> ->
do_net_server_append(RegName, Sock, LenHex, Prefix);
%% BEGIN epoch-id hack
<<"A ", <<"A ",
_EpochIDRaw:(?EpochIDSpace)/binary, _EpochIDRaw:(?EpochIDSpace)/binary,
LenHex:8/binary, LenHex:8/binary,
Prefix:PrefixLenLF_E/binary, "\n">> -> Prefix:PrefixLenLF_E/binary, "\n">> ->
do_net_server_append(RegName, Sock, LenHex, Prefix); do_net_server_append(RegName, Sock, LenHex, Prefix);
%% END epoch-id hack <<"R ",
<<"A ", LenHex:8/binary, " ", _EpochIDRaw:(?EpochIDSpace)/binary,
Prefix:PrefixLenCRLF/binary, "\r\n">> -> OffsetHex:16/binary, LenHex:8/binary,
do_net_server_append(RegName, Sock, LenHex, Prefix); File:FileLenLF_E/binary, "\n">> ->
<<"R ", OffsetHex:16/binary, " ", LenHex:8/binary, " ",
File:FileLenLF/binary, "\n">> ->
do_net_server_read(Sock, OffsetHex, LenHex, File, DataDir);
<<"R ", OffsetHex:16/binary, " ", LenHex:8/binary, " ",
File:FileLenCRLF/binary, "\r\n">> ->
do_net_server_read(Sock, OffsetHex, LenHex, File, DataDir); do_net_server_read(Sock, OffsetHex, LenHex, File, DataDir);
<<"L\n">> -> <<"L\n">> ->
do_net_server_listing(Sock, DataDir); do_net_server_listing(Sock, DataDir);

View file

@ -24,7 +24,7 @@
-export([ -export([
append_chunk/4, append_chunk/5, append_chunk/4, append_chunk/5,
read_chunk/4, read_chunk/5, read_chunk/5, read_chunk/6,
checksum_list/2, checksum_list/3, checksum_list/2, checksum_list/3,
list_files/1, list_files/2, list_files/1, list_files/2,
quit/1 quit/1
@ -77,21 +77,22 @@ append_chunk(Host, TcpPort, EpochID, Prefix, Chunk) ->
%% @doc Read a chunk of data of size `Size' from `File' at `Offset'. %% @doc Read a chunk of data of size `Size' from `File' at `Offset'.
-spec read_chunk(port(), file_name(), file_offset(), chunk_size()) -> -spec read_chunk(port(), epoch_id(), file_name(), file_offset(), chunk_size()) ->
{ok, chunk_s()} | {error, term()}. {ok, chunk_s()} | {error, term()}.
read_chunk(Sock, File, Offset, Size) read_chunk(Sock, EpochID, File, Offset, Size)
when Offset >= ?MINIMUM_OFFSET, Size >= 0 -> when Offset >= ?MINIMUM_OFFSET, Size >= 0 ->
read_chunk2(Sock, File, Offset, Size). read_chunk2(Sock, EpochID, File, Offset, Size).
%% @doc Read a chunk of data of size `Size' from `File' at `Offset'. %% @doc Read a chunk of data of size `Size' from `File' at `Offset'.
-spec read_chunk(inet_host(), inet_port(), file_name(), file_offset(), chunk_size()) -> -spec read_chunk(inet_host(), inet_port(), epoch_id(),
file_name(), file_offset(), chunk_size()) ->
{ok, chunk_s()} | {error, term()}. {ok, chunk_s()} | {error, term()}.
read_chunk(Host, TcpPort, File, Offset, Size) read_chunk(Host, TcpPort, EpochID, File, Offset, Size)
when Offset >= ?MINIMUM_OFFSET, Size >= 0 -> when Offset >= ?MINIMUM_OFFSET, Size >= 0 ->
Sock = machi_util:connect(Host, TcpPort), Sock = machi_util:connect(Host, TcpPort),
try try
read_chunk2(Sock, File, Offset, Size) read_chunk2(Sock, EpochID, File, Offset, Size)
after after
catch gen_tcp:close(Sock) catch gen_tcp:close(Sock)
end. end.
@ -244,11 +245,13 @@ append_chunk2(Sock, EpochID, Prefix0, Chunk0) ->
{error, {badmatch, BadMatch, erlang:get_stacktrace()}} {error, {badmatch, BadMatch, erlang:get_stacktrace()}}
end. end.
read_chunk2(Sock, File0, Offset, Size) -> read_chunk2(Sock, EpochID, File0, Offset, Size) ->
{EpochNum, EpochCSum} = EpochID,
EpochIDRaw = <<EpochNum:(4*8)/big, EpochCSum/binary>>,
File = machi_util:make_binary(File0), File = machi_util:make_binary(File0),
PrefixHex = machi_util:int_to_hexbin(Offset, 64), PrefixHex = machi_util:int_to_hexbin(Offset, 64),
SizeHex = machi_util:int_to_hexbin(Size, 32), SizeHex = machi_util:int_to_hexbin(Size, 32),
CmdLF = [$R, 32, PrefixHex, 32, SizeHex, 32, File, 10], CmdLF = [$R, 32, EpochIDRaw, PrefixHex, SizeHex, File, 10],
ok = gen_tcp:send(Sock, CmdLF), ok = gen_tcp:send(Sock, CmdLF),
case gen_tcp:recv(Sock, 3) of case gen_tcp:recv(Sock, 3) of
{ok, <<"OK\n">>} -> {ok, <<"OK\n">>} ->

View file

@ -58,7 +58,8 @@ flu_smoke_test() ->
{ok, {Off1,Len1,File1}} = ?FLU_C:append_chunk(Host, TcpPort, {ok, {Off1,Len1,File1}} = ?FLU_C:append_chunk(Host, TcpPort,
?DUMMY_PV1_EPOCH, ?DUMMY_PV1_EPOCH,
Prefix, Chunk1), Prefix, Chunk1),
{ok, Chunk1} = ?FLU_C:read_chunk(Host, TcpPort, File1, Off1, Len1), {ok, Chunk1} = ?FLU_C:read_chunk(Host, TcpPort, ?DUMMY_PV1_EPOCH,
File1, Off1, Len1),
{ok, [{_,_,_}]} = ?FLU_C:checksum_list(Host, TcpPort, File1), {ok, [{_,_,_}]} = ?FLU_C:checksum_list(Host, TcpPort, File1),
{error, bad_arg} = ?FLU_C:append_chunk(Host, TcpPort, {error, bad_arg} = ?FLU_C:append_chunk(Host, TcpPort,
?DUMMY_PV1_EPOCH, ?DUMMY_PV1_EPOCH,
@ -66,8 +67,10 @@ flu_smoke_test() ->
{ok, [{_,File1}]} = ?FLU_C:list_files(Host, TcpPort), {ok, [{_,File1}]} = ?FLU_C:list_files(Host, TcpPort),
Len1 = size(Chunk1), Len1 = size(Chunk1),
{error, no_such_file} = ?FLU_C:read_chunk(Host, TcpPort, {error, no_such_file} = ?FLU_C:read_chunk(Host, TcpPort,
?DUMMY_PV1_EPOCH,
File1, Off1*983, Len1), File1, Off1*983, Len1),
{error, partial_read} = ?FLU_C:read_chunk(Host, TcpPort, {error, partial_read} = ?FLU_C:read_chunk(Host, TcpPort,
?DUMMY_PV1_EPOCH,
File1, Off1, Len1*984), File1, Off1, Len1*984),
Chunk2 = <<"yo yo">>, Chunk2 = <<"yo yo">>,
@ -77,10 +80,13 @@ flu_smoke_test() ->
ok = ?FLU_C:write_chunk(Host, TcpPort, File2, Off2, Chunk2), ok = ?FLU_C:write_chunk(Host, TcpPort, File2, Off2, Chunk2),
{error, bad_arg} = ?FLU_C:write_chunk(Host, TcpPort, {error, bad_arg} = ?FLU_C:write_chunk(Host, TcpPort,
BadFile, Off2, Chunk2), BadFile, Off2, Chunk2),
{ok, Chunk2} = ?FLU_C:read_chunk(Host, TcpPort, File2, Off2, Len2), {ok, Chunk2} = ?FLU_C:read_chunk(Host, TcpPort, ?DUMMY_PV1_EPOCH,
File2, Off2, Len2),
{error, no_such_file} = ?FLU_C:read_chunk(Host, TcpPort, {error, no_such_file} = ?FLU_C:read_chunk(Host, TcpPort,
?DUMMY_PV1_EPOCH,
"no!!", Off2, Len2), "no!!", Off2, Len2),
{error, bad_arg} = ?FLU_C:read_chunk(Host, TcpPort, {error, bad_arg} = ?FLU_C:read_chunk(Host, TcpPort,
?DUMMY_PV1_EPOCH,
BadFile, Off2, Len2), BadFile, Off2, Len2),
%% We know that File1 still exists. Pretend that we've done a %% We know that File1 still exists. Pretend that we've done a