~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/protocol.py

  • Committer: Robert Collins
  • Date: 2009-02-20 10:48:51 UTC
  • mfrom: (4027 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4028.
  • Revision ID: robertc@robertcollins.net-20090220104851-m0s7qwe6jzqkgj1f
Merge bzr.dev (avoids criss-cross for PQM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Wire-level encoding and decoding of requests and responses for the smart
18
18
client and server.
22
22
from cStringIO import StringIO
23
23
import struct
24
24
import sys
25
 
import thread
26
 
import threading
27
25
import time
28
26
 
29
27
import bzrlib
30
 
from bzrlib import (
31
 
    debug,
32
 
    errors,
33
 
    osutils,
34
 
    )
 
28
from bzrlib import debug
 
29
from bzrlib import errors
35
30
from bzrlib.smart import message, request
36
31
from bzrlib.trace import log_exception_quietly, mutter
37
 
from bzrlib.bencode import bdecode_as_tuple, bencode
 
32
from bzrlib.util.bencode import bdecode_as_tuple, bencode
38
33
 
39
34
 
40
35
# Protocol version strings.  These are sent as prefixes of bzr requests and
62
57
 
63
58
def _encode_tuple(args):
64
59
    """Encode the tuple args to a bytestream."""
65
 
    joined = '\x01'.join(args) + '\n'
66
 
    if type(joined) is unicode:
67
 
        # XXX: We should fix things so this never happens!  -AJB, 20100304
68
 
        mutter('response args contain unicode, should be only bytes: %r',
69
 
               joined)
70
 
        joined = joined.encode('ascii')
71
 
    return joined
 
60
    return '\x01'.join(args) + '\n'
72
61
 
73
62
 
74
63
class Requester(object):
120
109
        for start, length in offsets:
121
110
            txt.append('%d,%d' % (start, length))
122
111
        return '\n'.join(txt)
123
 
 
 
112
        
124
113
 
125
114
class SmartServerRequestProtocolOne(SmartProtocolBase):
126
115
    """Server-side encoding and decoding logic for smart version 1."""
127
 
 
128
 
    def __init__(self, backing_transport, write_func, root_client_path='/',
129
 
            jail_root=None):
 
116
    
 
117
    def __init__(self, backing_transport, write_func, root_client_path='/'):
130
118
        self._backing_transport = backing_transport
131
119
        self._root_client_path = root_client_path
132
 
        self._jail_root = jail_root
133
120
        self.unused_data = ''
134
121
        self._finished = False
135
122
        self.in_buffer = ''
140
127
 
141
128
    def accept_bytes(self, bytes):
142
129
        """Take bytes, and advance the internal state machine appropriately.
143
 
 
 
130
        
144
131
        :param bytes: must be a byte string
145
132
        """
146
133
        if not isinstance(bytes, str):
157
144
                req_args = _decode_tuple(first_line)
158
145
                self.request = request.SmartServerRequestHandler(
159
146
                    self._backing_transport, commands=request.request_handlers,
160
 
                    root_client_path=self._root_client_path,
161
 
                    jail_root=self._jail_root)
162
 
                self.request.args_received(req_args)
 
147
                    root_client_path=self._root_client_path)
 
148
                self.request.dispatch_command(req_args[0], req_args[1:])
163
149
                if self.request.finished_reading:
164
150
                    # trivial request
165
151
                    self.unused_data = self.in_buffer
183
169
 
184
170
        if self._has_dispatched:
185
171
            if self._finished:
186
 
                # nothing to do.XXX: this routine should be a single state
 
172
                # nothing to do.XXX: this routine should be a single state 
187
173
                # machine too.
188
174
                self.unused_data += self.in_buffer
189
175
                self.in_buffer = ''
225
211
 
226
212
    def _write_protocol_version(self):
227
213
        """Write any prefixes this protocol requires.
228
 
 
 
214
        
229
215
        Version one doesn't send protocol versions.
230
216
        """
231
217
 
248
234
 
249
235
class SmartServerRequestProtocolTwo(SmartServerRequestProtocolOne):
250
236
    r"""Version two of the server side of the smart protocol.
251
 
 
 
237
   
252
238
    This prefixes responses with the value of RESPONSE_VERSION_TWO.
253
239
    """
254
240
 
264
250
 
265
251
    def _write_protocol_version(self):
266
252
        r"""Write any prefixes this protocol requires.
267
 
 
 
253
        
268
254
        Version two sends the value of RESPONSE_VERSION_TWO.
269
255
        """
270
256
        self._write_func(self.response_marker)
426
412
        self.chunks = collections.deque()
427
413
        self.error = False
428
414
        self.error_in_progress = None
429
 
 
 
415
    
430
416
    def next_read_size(self):
431
417
        # Note: the shortest possible chunk is 2 bytes: '0\n', and the
432
418
        # end-of-body marker is 4 bytes: 'END\n'.
520
506
                self.chunks.append(self.chunk_in_progress)
521
507
            self.chunk_in_progress = None
522
508
            self.state_accept = self._state_accept_expecting_length
523
 
 
 
509
        
524
510
    def _state_accept_reading_unused(self):
525
511
        self.unused_data += self._get_in_buffer()
526
512
        self._in_buffer_list = []
528
514
 
529
515
class LengthPrefixedBodyDecoder(_StatefulDecoder):
530
516
    """Decodes the length-prefixed bulk data."""
531
 
 
 
517
    
532
518
    def __init__(self):
533
519
        _StatefulDecoder.__init__(self)
534
520
        self.state_accept = self._state_accept_expecting_length
535
521
        self.state_read = self._state_read_no_data
536
522
        self._body = ''
537
523
        self._trailer_buffer = ''
538
 
 
 
524
    
539
525
    def next_read_size(self):
540
526
        if self.bytes_left is not None:
541
527
            # Ideally we want to read all the remainder of the body and the
551
537
        else:
552
538
            # Reading excess data.  Either way, 1 byte at a time is fine.
553
539
            return 1
554
 
 
 
540
        
555
541
    def read_pending_data(self):
556
542
        """Return any pending data that has been decoded."""
557
543
        return self.state_read()
578
564
                self._body = self._body[:self.bytes_left]
579
565
            self.bytes_left = None
580
566
            self.state_accept = self._state_accept_reading_trailer
581
 
 
 
567
        
582
568
    def _state_accept_reading_trailer(self):
583
569
        self._trailer_buffer += self._get_in_buffer()
584
570
        self._set_in_buffer(None)
588
574
            self.unused_data = self._trailer_buffer[len('done\n'):]
589
575
            self.state_accept = self._state_accept_reading_unused
590
576
            self.finished_reading = True
591
 
 
 
577
    
592
578
    def _state_accept_reading_unused(self):
593
579
        self.unused_data += self._get_in_buffer()
594
580
        self._set_in_buffer(None)
626
612
            mutter('hpss call:   %s', repr(args)[1:-1])
627
613
            if getattr(self._request._medium, 'base', None) is not None:
628
614
                mutter('             (to %s)', self._request._medium.base)
629
 
            self._request_start_time = osutils.timer_func()
 
615
            self._request_start_time = time.time()
630
616
        self._write_args(args)
631
617
        self._request.finished_writing()
632
618
        self._last_verb = args[0]
641
627
            if getattr(self._request._medium, '_path', None) is not None:
642
628
                mutter('                  (to %s)', self._request._medium._path)
643
629
            mutter('              %d bytes', len(body))
644
 
            self._request_start_time = osutils.timer_func()
 
630
            self._request_start_time = time.time()
645
631
            if 'hpssdetail' in debug.debug_flags:
646
632
                mutter('hpss body content: %s', body)
647
633
        self._write_args(args)
660
646
            mutter('hpss call w/readv: %s', repr(args)[1:-1])
661
647
            if getattr(self._request._medium, '_path', None) is not None:
662
648
                mutter('                  (to %s)', self._request._medium._path)
663
 
            self._request_start_time = osutils.timer_func()
 
649
            self._request_start_time = time.time()
664
650
        self._write_args(args)
665
651
        readv_bytes = self._serialise_offsets(body)
666
652
        bytes = self._encode_bulk_data(readv_bytes)
669
655
        if 'hpss' in debug.debug_flags:
670
656
            mutter('              %d bytes in readv request', len(readv_bytes))
671
657
        self._last_verb = args[0]
672
 
 
 
658
    
673
659
    def call_with_body_stream(self, args, stream):
674
660
        # Protocols v1 and v2 don't support body streams.  So it's safe to
675
661
        # assume that a v1/v2 server doesn't support whatever method we're
692
678
        if 'hpss' in debug.debug_flags:
693
679
            if self._request_start_time is not None:
694
680
                mutter('   result:   %6.3fs  %s',
695
 
                       osutils.timer_func() - self._request_start_time,
 
681
                       time.time() - self._request_start_time,
696
682
                       repr(result)[1:-1])
697
683
                self._request_start_time = None
698
684
            else:
743
729
    def _response_is_unknown_method(self, result_tuple):
744
730
        """Raise UnexpectedSmartServerResponse if the response is an 'unknonwn
745
731
        method' response to the request.
746
 
 
 
732
        
747
733
        :param response: The response from a smart client call_expecting_body
748
734
            call.
749
735
        :param verb: The verb used in that call.
756
742
            # The response will have no body, so we've finished reading.
757
743
            self._request.finished_reading()
758
744
            raise errors.UnknownSmartMethod(self._last_verb)
759
 
 
 
745
        
760
746
    def read_body_bytes(self, count=-1):
761
747
        """Read bytes from the body, decoding into a byte stream.
762
 
 
763
 
        We read all bytes at once to ensure we've checked the trailer for
 
748
        
 
749
        We read all bytes at once to ensure we've checked the trailer for 
764
750
        errors, and then feed the buffer back as read_body_bytes is called.
765
751
        """
766
752
        if self._body_buffer is not None:
804
790
 
805
791
    def _write_protocol_version(self):
806
792
        """Write any prefixes this protocol requires.
807
 
 
 
793
        
808
794
        Version one doesn't send protocol versions.
809
795
        """
810
796
 
811
797
 
812
798
class SmartClientRequestProtocolTwo(SmartClientRequestProtocolOne):
813
799
    """Version two of the client side of the smart protocol.
814
 
 
 
800
    
815
801
    This prefixes the request with the value of REQUEST_VERSION_TWO.
816
802
    """
817
803
 
845
831
 
846
832
    def _write_protocol_version(self):
847
833
        """Write any prefixes this protocol requires.
848
 
 
 
834
        
849
835
        Version two sends the value of REQUEST_VERSION_TWO.
850
836
        """
851
837
        self._request.accept_bytes(self.request_marker)
872
858
 
873
859
 
874
860
def build_server_protocol_three(backing_transport, write_func,
875
 
                                root_client_path, jail_root=None):
 
861
                                root_client_path):
876
862
    request_handler = request.SmartServerRequestHandler(
877
863
        backing_transport, commands=request.request_handlers,
878
 
        root_client_path=root_client_path, jail_root=jail_root)
 
864
        root_client_path=root_client_path)
879
865
    responder = ProtocolThreeResponder(write_func)
880
866
    message_handler = message.ConventionalRequestHandler(request_handler, responder)
881
867
    return ProtocolThreeDecoder(message_handler)
911
897
            # We do *not* set self.decoding_failed here.  The message handler
912
898
            # has raised an error, but the decoder is still able to parse bytes
913
899
            # and determine when this message ends.
914
 
            if not isinstance(exception.exc_value, errors.UnknownSmartMethod):
915
 
                log_exception_quietly()
 
900
            log_exception_quietly()
916
901
            self.message_handler.protocol_error(exception.exc_value)
917
902
            # The state machine is ready to continue decoding, but the
918
903
            # exception has interrupted the loop that runs the state machine.
1000
985
            self.message_handler.headers_received(decoded)
1001
986
        except:
1002
987
            raise errors.SmartMessageHandlerError(sys.exc_info())
1003
 
 
 
988
    
1004
989
    def _state_accept_expecting_message_part(self):
1005
990
        message_part_kind = self._extract_single_byte()
1006
991
        if message_part_kind == 'o':
1051
1036
            raise errors.SmartMessageHandlerError(sys.exc_info())
1052
1037
 
1053
1038
    def _state_accept_reading_unused(self):
1054
 
        self.unused_data += self._get_in_buffer()
 
1039
        self.unused_data = self._get_in_buffer()
1055
1040
        self._set_in_buffer(None)
1056
1041
 
1057
1042
    def next_read_size(self):
1073
1058
class _ProtocolThreeEncoder(object):
1074
1059
 
1075
1060
    response_marker = request_marker = MESSAGE_VERSION_THREE
1076
 
    BUFFER_SIZE = 1024*1024 # 1 MiB buffer before flushing
1077
1061
 
1078
1062
    def __init__(self, write_func):
1079
 
        self._buf = []
1080
 
        self._buf_len = 0
 
1063
        self._buf = ''
1081
1064
        self._real_write_func = write_func
1082
1065
 
1083
1066
    def _write_func(self, bytes):
1084
 
        # TODO: It is probably more appropriate to use sum(map(len, _buf))
1085
 
        #       for total number of bytes to write, rather than buffer based on
1086
 
        #       the number of write() calls
1087
 
        # TODO: Another possibility would be to turn this into an async model.
1088
 
        #       Where we let another thread know that we have some bytes if
1089
 
        #       they want it, but we don't actually block for it
1090
 
        #       Note that osutils.send_all always sends 64kB chunks anyway, so
1091
 
        #       we might just push out smaller bits at a time?
1092
 
        self._buf.append(bytes)
1093
 
        self._buf_len += len(bytes)
1094
 
        if self._buf_len > self.BUFFER_SIZE:
1095
 
            self.flush()
 
1067
        self._buf += bytes
1096
1068
 
1097
1069
    def flush(self):
1098
1070
        if self._buf:
1099
 
            self._real_write_func(''.join(self._buf))
1100
 
            del self._buf[:]
1101
 
            self._buf_len = 0
 
1071
            self._real_write_func(self._buf)
 
1072
            self._buf = ''
1102
1073
 
1103
1074
    def _serialise_offsets(self, offsets):
1104
1075
        """Serialise a readv offset list."""
1106
1077
        for start, length in offsets:
1107
1078
            txt.append('%d,%d' % (start, length))
1108
1079
        return '\n'.join(txt)
1109
 
 
 
1080
        
1110
1081
    def _write_protocol_version(self):
1111
1082
        self._write_func(MESSAGE_VERSION_THREE)
1112
1083
 
1153
1124
        _ProtocolThreeEncoder.__init__(self, write_func)
1154
1125
        self.response_sent = False
1155
1126
        self._headers = {'Software version': bzrlib.__version__}
1156
 
        if 'hpss' in debug.debug_flags:
1157
 
            self._thread_id = thread.get_ident()
1158
 
            self._response_start_time = None
1159
 
 
1160
 
    def _trace(self, action, message, extra_bytes=None, include_time=False):
1161
 
        if self._response_start_time is None:
1162
 
            self._response_start_time = osutils.timer_func()
1163
 
        if include_time:
1164
 
            t = '%5.3fs ' % (time.clock() - self._response_start_time)
1165
 
        else:
1166
 
            t = ''
1167
 
        if extra_bytes is None:
1168
 
            extra = ''
1169
 
        else:
1170
 
            extra = ' ' + repr(extra_bytes[:40])
1171
 
            if len(extra) > 33:
1172
 
                extra = extra[:29] + extra[-1] + '...'
1173
 
        mutter('%12s: [%s] %s%s%s'
1174
 
               % (action, self._thread_id, t, message, extra))
1175
1127
 
1176
1128
    def send_error(self, exception):
1177
1129
        if self.response_sent:
1183
1135
                ('UnknownMethod', exception.verb))
1184
1136
            self.send_response(failure)
1185
1137
            return
1186
 
        if 'hpss' in debug.debug_flags:
1187
 
            self._trace('error', str(exception))
1188
1138
        self.response_sent = True
1189
1139
        self._write_protocol_version()
1190
1140
        self._write_headers(self._headers)
1204
1154
            self._write_success_status()
1205
1155
        else:
1206
1156
            self._write_error_status()
1207
 
        if 'hpss' in debug.debug_flags:
1208
 
            self._trace('response', repr(response.args))
1209
1157
        self._write_structure(response.args)
1210
1158
        if response.body is not None:
1211
1159
            self._write_prefixed_body(response.body)
1212
 
            if 'hpss' in debug.debug_flags:
1213
 
                self._trace('body', '%d bytes' % (len(response.body),),
1214
 
                            response.body, include_time=True)
1215
1160
        elif response.body_stream is not None:
1216
 
            count = num_bytes = 0
1217
 
            first_chunk = None
1218
 
            for exc_info, chunk in _iter_with_errors(response.body_stream):
1219
 
                count += 1
1220
 
                if exc_info is not None:
1221
 
                    self._write_error_status()
1222
 
                    error_struct = request._translate_error(exc_info[1])
1223
 
                    self._write_structure(error_struct)
1224
 
                    break
1225
 
                else:
1226
 
                    if isinstance(chunk, request.FailedSmartServerResponse):
1227
 
                        self._write_error_status()
1228
 
                        self._write_structure(chunk.args)
1229
 
                        break
1230
 
                    num_bytes += len(chunk)
1231
 
                    if first_chunk is None:
1232
 
                        first_chunk = chunk
1233
 
                    self._write_prefixed_body(chunk)
1234
 
                    if 'hpssdetail' in debug.debug_flags:
1235
 
                        # Not worth timing separately, as _write_func is
1236
 
                        # actually buffered
1237
 
                        self._trace('body chunk',
1238
 
                                    '%d bytes' % (len(chunk),),
1239
 
                                    chunk, suppress_time=True)
1240
 
            if 'hpss' in debug.debug_flags:
1241
 
                self._trace('body stream',
1242
 
                            '%d bytes %d chunks' % (num_bytes, count),
1243
 
                            first_chunk)
 
1161
            for chunk in response.body_stream:
 
1162
                self._write_prefixed_body(chunk)
 
1163
                self.flush()
1244
1164
        self._write_end()
1245
 
        if 'hpss' in debug.debug_flags:
1246
 
            self._trace('response end', '', include_time=True)
1247
 
 
1248
 
 
1249
 
def _iter_with_errors(iterable):
1250
 
    """Handle errors from iterable.next().
1251
 
 
1252
 
    Use like::
1253
 
 
1254
 
        for exc_info, value in _iter_with_errors(iterable):
1255
 
            ...
1256
 
 
1257
 
    This is a safer alternative to::
1258
 
 
1259
 
        try:
1260
 
            for value in iterable:
1261
 
               ...
1262
 
        except:
1263
 
            ...
1264
 
 
1265
 
    Because the latter will catch errors from the for-loop body, not just
1266
 
    iterable.next()
1267
 
 
1268
 
    If an error occurs, exc_info will be a exc_info tuple, and the generator
1269
 
    will terminate.  Otherwise exc_info will be None, and value will be the
1270
 
    value from iterable.next().  Note that KeyboardInterrupt and SystemExit
1271
 
    will not be itercepted.
1272
 
    """
1273
 
    iterator = iter(iterable)
1274
 
    while True:
1275
 
        try:
1276
 
            yield None, iterator.next()
1277
 
        except StopIteration:
1278
 
            return
1279
 
        except (KeyboardInterrupt, SystemExit):
1280
 
            raise
1281
 
        except Exception:
1282
 
            mutter('_iter_with_errors caught error')
1283
 
            log_exception_quietly()
1284
 
            yield sys.exc_info(), None
1285
 
            return
1286
 
 
 
1165
        
1287
1166
 
1288
1167
class ProtocolThreeRequester(_ProtocolThreeEncoder, Requester):
1289
1168
 
1294
1173
 
1295
1174
    def set_headers(self, headers):
1296
1175
        self._headers = headers.copy()
1297
 
 
 
1176
        
1298
1177
    def call(self, *args):
1299
1178
        if 'hpss' in debug.debug_flags:
1300
1179
            mutter('hpss call:   %s', repr(args)[1:-1])
1301
1180
            base = getattr(self._medium_request._medium, 'base', None)
1302
1181
            if base is not None:
1303
1182
                mutter('             (to %s)', base)
1304
 
            self._request_start_time = osutils.timer_func()
 
1183
            self._request_start_time = time.time()
1305
1184
        self._write_protocol_version()
1306
1185
        self._write_headers(self._headers)
1307
1186
        self._write_structure(args)
1319
1198
            if path is not None:
1320
1199
                mutter('                  (to %s)', path)
1321
1200
            mutter('              %d bytes', len(body))
1322
 
            self._request_start_time = osutils.timer_func()
 
1201
            self._request_start_time = time.time()
1323
1202
        self._write_protocol_version()
1324
1203
        self._write_headers(self._headers)
1325
1204
        self._write_structure(args)
1338
1217
            path = getattr(self._medium_request._medium, '_path', None)
1339
1218
            if path is not None:
1340
1219
                mutter('                  (to %s)', path)
1341
 
            self._request_start_time = osutils.timer_func()
 
1220
            self._request_start_time = time.time()
1342
1221
        self._write_protocol_version()
1343
1222
        self._write_headers(self._headers)
1344
1223
        self._write_structure(args)
1355
1234
            path = getattr(self._medium_request._medium, '_path', None)
1356
1235
            if path is not None:
1357
1236
                mutter('                  (to %s)', path)
1358
 
            self._request_start_time = osutils.timer_func()
 
1237
            self._request_start_time = time.time()
1359
1238
        self._write_protocol_version()
1360
1239
        self._write_headers(self._headers)
1361
1240
        self._write_structure(args)
1363
1242
        #       have finished sending the stream.  We would notice at the end
1364
1243
        #       anyway, but if the medium can deliver it early then it's good
1365
1244
        #       to short-circuit the whole request...
1366
 
        for exc_info, part in _iter_with_errors(stream):
1367
 
            if exc_info is not None:
1368
 
                # Iterating the stream failed.  Cleanly abort the request.
1369
 
                self._write_error_status()
1370
 
                # Currently the client unconditionally sends ('error',) as the
1371
 
                # error args.
1372
 
                self._write_structure(('error',))
1373
 
                self._write_end()
1374
 
                self._medium_request.finished_writing()
1375
 
                raise exc_info[0], exc_info[1], exc_info[2]
1376
 
            else:
 
1245
        try:
 
1246
            for part in stream:
1377
1247
                self._write_prefixed_body(part)
1378
1248
                self.flush()
 
1249
        except Exception:
 
1250
            exc_info = sys.exc_info()
 
1251
            # Iterating the stream failed.  Cleanly abort the request.
 
1252
            self._write_error_status()
 
1253
            # Currently the client unconditionally sends ('error',) as the
 
1254
            # error args.
 
1255
            self._write_structure(('error',))
 
1256
            raise exc_info[0], exc_info[1], exc_info[2]
1379
1257
        self._write_end()
1380
1258
        self._medium_request.finished_writing()
1381
1259