/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/smart/protocol.py

  • Committer: John Arbash Meinel
  • Date: 2009-12-16 17:27:49 UTC
  • mto: This revision was merged to the branch mainline in revision 4903.
  • Revision ID: john@arbash-meinel.com-20091216172749-sgr2ehcbi9efmauv
Change the test that expected 100 writes to flush, to make it based on content size.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2008, 2009 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
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
32
from bzrlib.bencode import bdecode_as_tuple, bencode
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):
626
615
            mutter('hpss call:   %s', repr(args)[1:-1])
627
616
            if getattr(self._request._medium, 'base', None) is not None:
628
617
                mutter('             (to %s)', self._request._medium.base)
629
 
            self._request_start_time = osutils.timer_func()
 
618
            self._request_start_time = time.time()
630
619
        self._write_args(args)
631
620
        self._request.finished_writing()
632
621
        self._last_verb = args[0]
641
630
            if getattr(self._request._medium, '_path', None) is not None:
642
631
                mutter('                  (to %s)', self._request._medium._path)
643
632
            mutter('              %d bytes', len(body))
644
 
            self._request_start_time = osutils.timer_func()
 
633
            self._request_start_time = time.time()
645
634
            if 'hpssdetail' in debug.debug_flags:
646
635
                mutter('hpss body content: %s', body)
647
636
        self._write_args(args)
660
649
            mutter('hpss call w/readv: %s', repr(args)[1:-1])
661
650
            if getattr(self._request._medium, '_path', None) is not None:
662
651
                mutter('                  (to %s)', self._request._medium._path)
663
 
            self._request_start_time = osutils.timer_func()
 
652
            self._request_start_time = time.time()
664
653
        self._write_args(args)
665
654
        readv_bytes = self._serialise_offsets(body)
666
655
        bytes = self._encode_bulk_data(readv_bytes)
692
681
        if 'hpss' in debug.debug_flags:
693
682
            if self._request_start_time is not None:
694
683
                mutter('   result:   %6.3fs  %s',
695
 
                       osutils.timer_func() - self._request_start_time,
 
684
                       time.time() - self._request_start_time,
696
685
                       repr(result)[1:-1])
697
686
                self._request_start_time = None
698
687
            else:
1081
1070
        self._real_write_func = write_func
1082
1071
 
1083
1072
    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
1073
        self._buf.append(bytes)
1093
1074
        self._buf_len += len(bytes)
1094
1075
        if self._buf_len > self.BUFFER_SIZE:
1153
1134
        _ProtocolThreeEncoder.__init__(self, write_func)
1154
1135
        self.response_sent = False
1155
1136
        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
1137
 
1176
1138
    def send_error(self, exception):
1177
1139
        if self.response_sent:
1183
1145
                ('UnknownMethod', exception.verb))
1184
1146
            self.send_response(failure)
1185
1147
            return
1186
 
        if 'hpss' in debug.debug_flags:
1187
 
            self._trace('error', str(exception))
1188
1148
        self.response_sent = True
1189
1149
        self._write_protocol_version()
1190
1150
        self._write_headers(self._headers)
1204
1164
            self._write_success_status()
1205
1165
        else:
1206
1166
            self._write_error_status()
1207
 
        if 'hpss' in debug.debug_flags:
1208
 
            self._trace('response', repr(response.args))
1209
1167
        self._write_structure(response.args)
1210
1168
        if response.body is not None:
1211
1169
            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
1170
        elif response.body_stream is not None:
1216
 
            count = num_bytes = 0
1217
 
            first_chunk = None
1218
1171
            for exc_info, chunk in _iter_with_errors(response.body_stream):
1219
 
                count += 1
1220
1172
                if exc_info is not None:
1221
1173
                    self._write_error_status()
1222
1174
                    error_struct = request._translate_error(exc_info[1])
1227
1179
                        self._write_error_status()
1228
1180
                        self._write_structure(chunk.args)
1229
1181
                        break
1230
 
                    num_bytes += len(chunk)
1231
 
                    if first_chunk is None:
1232
 
                        first_chunk = chunk
1233
1182
                    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)
1244
1183
        self._write_end()
1245
 
        if 'hpss' in debug.debug_flags:
1246
 
            self._trace('response end', '', include_time=True)
1247
1184
 
1248
1185
 
1249
1186
def _iter_with_errors(iterable):
1301
1238
            base = getattr(self._medium_request._medium, 'base', None)
1302
1239
            if base is not None:
1303
1240
                mutter('             (to %s)', base)
1304
 
            self._request_start_time = osutils.timer_func()
 
1241
            self._request_start_time = time.time()
1305
1242
        self._write_protocol_version()
1306
1243
        self._write_headers(self._headers)
1307
1244
        self._write_structure(args)
1319
1256
            if path is not None:
1320
1257
                mutter('                  (to %s)', path)
1321
1258
            mutter('              %d bytes', len(body))
1322
 
            self._request_start_time = osutils.timer_func()
 
1259
            self._request_start_time = time.time()
1323
1260
        self._write_protocol_version()
1324
1261
        self._write_headers(self._headers)
1325
1262
        self._write_structure(args)
1338
1275
            path = getattr(self._medium_request._medium, '_path', None)
1339
1276
            if path is not None:
1340
1277
                mutter('                  (to %s)', path)
1341
 
            self._request_start_time = osutils.timer_func()
 
1278
            self._request_start_time = time.time()
1342
1279
        self._write_protocol_version()
1343
1280
        self._write_headers(self._headers)
1344
1281
        self._write_structure(args)
1355
1292
            path = getattr(self._medium_request._medium, '_path', None)
1356
1293
            if path is not None:
1357
1294
                mutter('                  (to %s)', path)
1358
 
            self._request_start_time = osutils.timer_func()
 
1295
            self._request_start_time = time.time()
1359
1296
        self._write_protocol_version()
1360
1297
        self._write_headers(self._headers)
1361
1298
        self._write_structure(args)