| 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: Server.pm,v 1.23 2010/12/23 19:08:17 jaw Exp $ | ||
| 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 | unless( $proto->{want_reply} ){ | ||
| 189 | $io->shut(); | ||
| 190 | return; | ||
| 191 | } | ||
| 192 | |||
| 193 | # decode request | ||
| 194 | my $req; | ||
| 195 | eval { | ||
| 196 | $req = ACPYentaDistRequest->decode( $gpb ); | ||
| 197 | die "invalid k/v for put request\n" unless $req->{datum}{key} && $req->{datum}{version}; | ||
| 198 | }; | ||
| 199 | if(my $e = $@){ | ||
| 200 | my $enc = $proto->{data_encrypted} ? ' (encrypted)' : ''; | ||
| 201 | problem("cannot decode request: peer: $io->{peerip} $enc, $e"); | ||
| 202 | $io->shut(); | ||
| 203 | return; | ||
| 204 | } | ||
| 205 | |||
| 206 | unless( conf_map( $req->{datum}{map} ) ){ | ||
| 207 | problem("distribute request for unknown map '$req->{datum}{map}' - $io->{info}"); | ||
| 208 | _reply_error($io, $proto, 404, 'Map Not Found'); | ||
| 209 | return; | ||
| 210 | } | ||
| 211 | |||
| 212 | my $v = $req->{datum}; | ||
| 213 | |||
| 214 | # do we already have ? | ||
| 215 | my $want = store_want( $v->{map}, $v->{shard}, $v->{key}, $v->{version} ); | ||
| 216 | |||
| 217 | if( $want ){ | ||
| 218 | # put | ||
| 219 | |||
| 220 | debug("put request from $io->{peerip}: $v->{map}, $v->{key}, $v->{version}"); | ||
| 221 | |||
| 222 | # file content is passed by reference, to avoid large copies | ||
| 223 | $content ||= \ $v->{file} if $v->{file}; | ||
| 224 | |||
| 225 | # check | ||
| 226 | if( $v->{meta} && $content ){ | ||
| 227 | unless( _check_content( $v->{meta}, $content ) ){ | ||
| 228 | problem("content SHA1 check failed: $req->{datum}{map}, $req->{datum}{key}, $req->{datum}{version}"); | ||
| 229 | $io->shut(); | ||
| 230 | return; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | $want = store_put( $v->{map}, $v->{shard}, $v->{key}, $v->{version}, $v->{value}, | ||
| 235 | $content, $v->{meta} ); | ||
| 236 | |||
| 237 | # distribute to other systems | ||
| 238 | AC::Yenta::Store::Distrib->new( $req, $content ) if $want; | ||
| 239 | }else{ | ||
| 240 | debug("put from $io->{peerip} unwanted: $v->{map}, $v->{shard}, $v->{key}, $v->{version}"); | ||
| 241 | } | ||
| 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; |