Yenta/Kibitz/Store/Server.pm untested

filter: Statementstmt 0.0% 0 / 185 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 300 CC 34 SCAR 43.5
CC
34
Cov
0%
api_get
132.0
api_distrib
132.0
api_check
56.0
CRAP 77.5
1 # -*- perl -*-
2  
3 # Copyright (c) 2009 AdCopy
4 # Author: Jeff Weisberg
5 # Created: 2009-Apr-01 11:06 (EDT)
6 # Function: server side api of storage system
7 #
8 # $Id$
9  
10 package AC::Yenta::Kibitz::Store::Server;
11 use AC::Yenta::Debug 'store_server';
12 use AC::Yenta::Store;
13 use AC::Yenta::Config;
14 use AC::Dumper;
15 use JSON;
16 use Digest::SHA 'sha1_base64';
17 require 'AC/protobuf/yenta_getset.pl';
18 require 'AC/protobuf/yenta_check.pl';
19 use strict;
20  
21 my $TIMEOUT = 1;
22  
23 sub api_get {
24 my $io = shift;
25 my $proto = shift;
26 my $gpb = shift;
27 my $content = shift; # not used
28  
29 unless( $proto->{want_reply} ){
30 $io->shut();
31 return;
32 }
33  
34 # decode request
35 my $req;
36 eval {
37 $req = ACPYentaGetSet->decode( $gpb );
38 };
39 if(my $e = $@){
40 problem("cannot decode request: $e");
41 $io->shut();
42 return;
43 }
44  
45 # process requests
46 my @res;
47 my $rescont;
48 for my $r (@{ $req->{data} }){
49 debug("get request: $r->{map}, $r->{key}, $r->{version}");
50 my($data, $ver, $file, $meta) = store_get( $r->{map}, $r->{key}, $r->{version} );
51 my $res = {
52 map => $r->{map},
53 key => $r->{key},
54 };
55  
56 if( $meta && $file ){
57 unless( _check_content( $meta, $file ) ){
58 problem("content SHA1 check failed: $r->{map}, $r->{key}, $ver - removing");
59 # QQQ - remove from system, (and let AE get a new copy)?
60 store_remove($r->{map}, $r->{key}, $ver);
61 # tell caller it was not found
62 $ver = undef;
63 }
64 }
65  
66 if( defined $ver ){
67 $res->{version} = $ver;
68 $res->{value} = $data;
69 $res->{meta} = $meta if defined $meta;
70 if( $file ){
71 # if one file to send, send it as content
72 if( @{$req->{data}} == 1 ){
73 $rescont = $file;
74 }else{
75 $res->{file} = $$file;
76 }
77 }
78 }
79 push @res, $res;
80 }
81  
82 # encode results
83 my $ect = '';
84 my $yp = AC::Yenta::Protocol->new( secret => conf_value('secret') );
85 $ect = $proto->{data_encrypted} ? $yp->encrypt(undef, $$rescont) : $$rescont if $rescont;
86 my $response = $yp->encode_reply( {
87 type => 'yenta_get',
88 msgid => $proto->{msgid},
89 is_reply => 1,
90 data_encrypted => $proto->{data_encrypted},
91 content_encrypted => $proto->{data_encrypted},
92 }, { data => \@res }, \$ect );
93  
94 debug("sending get reply");
95 $io->timeout_rel($TIMEOUT);
96 $io->{writebuf_timeout} = $TIMEOUT;
97 $io->write_and_shut( $response . $ect );
98  
99 }
100  
101 sub api_check {
102 my $io = shift;
103 my $proto = shift;
104 my $gpb = shift;
105 my $content = shift; # not used
106  
107 unless( $proto->{want_reply} ){
108 $io->shut();
109 return;
110 }
111  
112 # decode request
113 my $req;
114 eval {
115 $req = ACPYentaCheckRequest->decode( $gpb );
116 };
117 if(my $e = $@){
118 problem("cannot decode request: $e");
119 $io->shut();
120 return;
121 }
122  
123 debug("check request: $req->{map}, $req->{version}, $req->{level}");
124  
125 my @res;
126 my @todo = { version => $req->{version}, shard => $req->{shard} };
127  
128 # the top of the tree will be fairly sparse,
129 # return up to several levels if they are sparse
130 for my $l (0 .. 32){
131 my $cl = $req->{level} + $l;
132 my @lres;
133 my $nexttot;
134 for my $do (@todo){
135 my @r = _get_check( $req->{map}, $do->{shard}, $do->{version}, $cl );
136 push @lres, @r;
137 for (@r){
138 $nexttot += $_->{key} ? ($_->{count} / 8) : $_->{count};
139 }
140 }
141 push @res, @lres;
142 @todo = ();
143 last unless @lres; # reached the bottom of the tree
144 last if @res > 64; # got enough results
145 last if (@lres > 2) && ($nexttot > @lres + 2); # less sparse region
146 # get the next level also
147 @todo = @lres;
148 }
149  
150 # encode results
151 my $yp = AC::Yenta::Protocol->new( secret => conf_value('secret') );
152 my $response = $yp->encode_reply( {
153 type => 'yenta_check',
154 msgid => $proto->{msgid},
155 is_reply => 1,
156 data_encrypted => $proto->{data_encrypted},
157 }, { check => \@res } );
158  
159 debug("sending check reply");
160 $io->timeout_rel($TIMEOUT);
161 $io->{writebuf_timeout} = $TIMEOUT;
162 $io->write_and_shut( $response );
163  
164 }
165  
166 # get + process merkle data
167 sub _get_check {
168 my $map = shift;
169 my $shard = shift;
170 my $ver = shift;
171 my $lev = shift;
172  
173 my $res = store_get_merkle($map, $shard, $ver, $lev);
174 return unless $res;
175 for my $r (@$res) {
176 $r->{map} = $map;
177 }
178  
179 return @$res;
180 }
181  
182 sub api_distrib {
183 my $io = shift;
184 my $proto = shift;
185 my $gpb = shift;
186 my $content = shift; # reference
187  
188 # decode request
189 my $req;
190 eval {
191 $req = ACPYentaDistRequest->decode( $gpb );
192 die "invalid k/v for put request\n" unless $req->{datum}{key} && $req->{datum}{version};
193 };
194 if(my $e = $@){
195 my $enc = $proto->{data_encrypted} ? ' (encrypted)' : '';
196 problem("cannot decode request: peer: $io->{peerip} $enc, $e");
197 $io->shut();
198 return;
199 }
200  
201 unless( conf_map( $req->{datum}{map} ) ){
202 problem("distribute request for unknown map '$req->{datum}{map}' - $io->{info}");
203 _reply_error($io, $proto, 404, 'Map Not Found');
204 return;
205 }
206  
207 my $v = $req->{datum};
208  
209 # do we already have ?
210 my $want = store_want( $v->{map}, $v->{shard}, $v->{key}, $v->{version} );
211  
212 if( $want ){
213 # put
214  
215 debug("put request from $io->{peerip}: $v->{map}, $v->{key}, $v->{version}");
216  
217 # file content is passed by reference, to avoid large copies
218 $content ||= \ $v->{file} if $v->{file};
219  
220 # check
221 if( $v->{meta} && $content ){
222 unless( _check_content( $v->{meta}, $content ) ){
223 problem("content SHA1 check failed: $req->{datum}{map}, $req->{datum}{key}, $req->{datum}{version}");
224 $io->shut();
225 return;
226 }
227 }
228  
229 $want = store_put( $v->{map}, $v->{shard}, $v->{key}, $v->{version}, $v->{value},
230 $content, $v->{meta} );
231  
232 # distribute to other systems
233 AC::Yenta::Store::Distrib->new( $req, $content ) if $want;
234 }else{
235 debug("put from $io->{peerip} unwanted: $v->{map}, $v->{shard}, $v->{key}, $v->{version}");
236 }
237  
238  
239 unless( $proto->{want_reply} ){
240 $io->shut();
241 return;
242 }
243  
244 # encode results
245 my $yp = AC::Yenta::Protocol->new( secret => conf_value('secret') );
246 my $response = $yp->encode_reply( {
247 type => 'yenta_distrib',
248 msgid => $proto->{msgid},
249 is_reply => 1,
250 data_encrypted => $proto->{data_encrypted},
251 }, { status_code => 200, status_message => 'OK', haveit => !$want } );
252  
253 debug("sending distrib reply");
254 $io->timeout_rel($TIMEOUT);
255 $io->write_and_shut( $response );
256  
257 }
258  
259 sub _check_content {
260 my $meta = shift;
261 my $cont = shift;
262  
263 return 1 unless $meta && $meta =~ /^\{/;
264  
265 eval {
266 $meta = decode_json($meta);
267 };
268 return 1 if $@;
269  
270 if( $meta->{sha1} ){
271 my $chk = sha1_base64( $$cont );
272 return unless $chk eq $meta->{sha1};
273 }
274 if( $meta->{size} ){
275 my $len = length($$cont);
276 return unless $len == $meta->{size};
277 }
278  
279 return 1;
280 }
281  
282 sub _reply_error {
283 my $io = shift;
284 my $proto = shift;
285 my $code = shift;
286 my $msg = shift;
287  
288 my $yp = AC::Yenta::Protocol->new( secret => conf_value('secret') );
289 my $response = $yp->encode_reply( {
290 type => 'yenta_distrib',
291 msgid => $proto->{msgid},
292 is_reply => 1,
293 is_error => 1,
294 data_encrypted => $proto->{data_encrypted},
295 }, {
296 status_code => $code,
297 status_message => $msg,
298 haveit => 0,
299 } );
300  
301 debug("sending distrib reply");
302 $io->write_and_shut( $response );
303 }
304  
305 1;