maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Archives a set of files to a server.""" |
| 7 | |
| 8 | import binascii |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 9 | import cStringIO |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 10 | import hashlib |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 11 | import itertools |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 12 | import logging |
| 13 | import optparse |
| 14 | import os |
| 15 | import sys |
| 16 | import time |
maruel@chromium.org | e82112e | 2013-04-24 14:41:55 +0000 | [diff] [blame] | 17 | import urllib |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 18 | import zlib |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 19 | |
| 20 | import run_isolated |
| 21 | |
| 22 | |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 23 | # The minimum size of files to upload directly to the blobstore. |
maruel@chromium.org | aef29f8 | 2012-12-12 15:00:42 +0000 | [diff] [blame] | 24 | MIN_SIZE_FOR_DIRECT_BLOBSTORE = 20 * 1024 |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 25 | |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 26 | # The number of files to check the isolate server per /contains query. The |
| 27 | # number here is a trade-off; the more per request, the lower the effect of HTTP |
| 28 | # round trip latency and TCP-level chattiness. On the other hand, larger values |
| 29 | # cause longer lookups, increasing the initial latency to start uploading, which |
| 30 | # is especially an issue for large files. This value is optimized for the "few |
| 31 | # thousands files to look up with minimal number of large files missing" case. |
| 32 | ITEMS_PER_CONTAINS_QUERY = 100 |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 33 | |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 34 | # A list of already compressed extension types that should not receive any |
| 35 | # compression before being uploaded. |
| 36 | ALREADY_COMPRESSED_TYPES = [ |
| 37 | '7z', 'avi', 'cur', 'gif', 'h264', 'jar', 'jpeg', 'jpg', 'pdf', 'png', |
| 38 | 'wav', 'zip' |
| 39 | ] |
| 40 | |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 41 | |
maruel@chromium.org | cb3c3d5 | 2013-03-14 18:55:30 +0000 | [diff] [blame] | 42 | def randomness(): |
| 43 | """Generates low-entropy randomness for MIME encoding. |
| 44 | |
| 45 | Exists so it can be mocked out in unit tests. |
| 46 | """ |
| 47 | return str(time.time()) |
| 48 | |
| 49 | |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 50 | def encode_multipart_formdata(fields, files, |
| 51 | mime_mapper=lambda _: 'application/octet-stream'): |
| 52 | """Encodes a Multipart form data object. |
| 53 | |
| 54 | Args: |
| 55 | fields: a sequence (name, value) elements for |
| 56 | regular form fields. |
| 57 | files: a sequence of (name, filename, value) elements for data to be |
| 58 | uploaded as files. |
| 59 | mime_mapper: function to return the mime type from the filename. |
| 60 | Returns: |
| 61 | content_type: for httplib.HTTP instance |
| 62 | body: for httplib.HTTP instance |
| 63 | """ |
maruel@chromium.org | cb3c3d5 | 2013-03-14 18:55:30 +0000 | [diff] [blame] | 64 | boundary = hashlib.md5(randomness()).hexdigest() |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 65 | body_list = [] |
| 66 | for (key, value) in fields: |
| 67 | if isinstance(key, unicode): |
| 68 | value = key.encode('utf-8') |
| 69 | if isinstance(value, unicode): |
| 70 | value = value.encode('utf-8') |
| 71 | body_list.append('--' + boundary) |
| 72 | body_list.append('Content-Disposition: form-data; name="%s"' % key) |
| 73 | body_list.append('') |
| 74 | body_list.append(value) |
| 75 | body_list.append('--' + boundary) |
| 76 | body_list.append('') |
| 77 | for (key, filename, value) in files: |
| 78 | if isinstance(key, unicode): |
| 79 | value = key.encode('utf-8') |
| 80 | if isinstance(filename, unicode): |
| 81 | value = filename.encode('utf-8') |
| 82 | if isinstance(value, unicode): |
| 83 | value = value.encode('utf-8') |
| 84 | body_list.append('--' + boundary) |
| 85 | body_list.append('Content-Disposition: form-data; name="%s"; ' |
| 86 | 'filename="%s"' % (key, filename)) |
| 87 | body_list.append('Content-Type: %s' % mime_mapper(filename)) |
| 88 | body_list.append('') |
| 89 | body_list.append(value) |
| 90 | body_list.append('--' + boundary) |
| 91 | body_list.append('') |
| 92 | if body_list: |
| 93 | body_list[-2] += '--' |
| 94 | body = '\r\n'.join(body_list) |
| 95 | content_type = 'multipart/form-data; boundary=%s' % boundary |
| 96 | return content_type, body |
| 97 | |
| 98 | |
maruel@chromium.org | 037758d | 2012-12-10 17:59:46 +0000 | [diff] [blame] | 99 | def sha1_file(filepath): |
| 100 | """Calculates the SHA-1 of a file without reading it all in memory at once.""" |
| 101 | digest = hashlib.sha1() |
| 102 | with open(filepath, 'rb') as f: |
| 103 | while True: |
| 104 | # Read in 1mb chunks. |
| 105 | chunk = f.read(1024*1024) |
| 106 | if not chunk: |
| 107 | break |
| 108 | digest.update(chunk) |
| 109 | return digest.hexdigest() |
| 110 | |
| 111 | |
vadimsh@chromium.org | 80f7300 | 2013-07-12 14:52:44 +0000 | [diff] [blame] | 112 | def url_read(url, **kwargs): |
| 113 | result = run_isolated.url_read(url, **kwargs) |
| 114 | if result is None: |
maruel@chromium.org | ef33312 | 2013-03-12 20:36:40 +0000 | [diff] [blame] | 115 | # If we get no response from the server, assume it is down and raise an |
| 116 | # exception. |
| 117 | raise run_isolated.MappingError('Unable to connect to server %s' % url) |
| 118 | return result |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 119 | |
| 120 | |
maruel@chromium.org | dc359e6 | 2013-03-14 13:08:55 +0000 | [diff] [blame] | 121 | def upload_hash_content_to_blobstore( |
| 122 | generate_upload_url, data, hash_key, content): |
vadimsh@chromium.org | 80f7300 | 2013-07-12 14:52:44 +0000 | [diff] [blame] | 123 | """Uploads the given hash contents directly to the blobstore via a generated |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 124 | url. |
| 125 | |
| 126 | Arguments: |
| 127 | generate_upload_url: The url to get the new upload url from. |
maruel@chromium.org | dc359e6 | 2013-03-14 13:08:55 +0000 | [diff] [blame] | 128 | data: extra POST data. |
| 129 | hash_key: sha1 of the uncompressed version of content. |
| 130 | content: The contents to upload. Must fit in memory for now. |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 131 | """ |
| 132 | logging.debug('Generating url to directly upload file to blobstore') |
maruel@chromium.org | 92a3d2e | 2012-12-20 16:22:29 +0000 | [diff] [blame] | 133 | assert isinstance(hash_key, str), hash_key |
| 134 | assert isinstance(content, str), (hash_key, content) |
maruel@chromium.org | d58bf5b | 2013-04-26 17:57:42 +0000 | [diff] [blame] | 135 | # TODO(maruel): Support large files. This would require streaming support. |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 136 | content_type, body = encode_multipart_formdata( |
maruel@chromium.org | d58bf5b | 2013-04-26 17:57:42 +0000 | [diff] [blame] | 137 | data, [('content', hash_key, content)]) |
maruel@chromium.org | 2b2139a | 2013-04-30 20:14:58 +0000 | [diff] [blame] | 138 | for attempt in xrange(run_isolated.URL_OPEN_MAX_ATTEMPTS): |
maruel@chromium.org | d58bf5b | 2013-04-26 17:57:42 +0000 | [diff] [blame] | 139 | # Retry HTTP 50x here. |
vadimsh@chromium.org | 80f7300 | 2013-07-12 14:52:44 +0000 | [diff] [blame] | 140 | upload_url = run_isolated.url_read(generate_upload_url, data=data) |
| 141 | if not upload_url: |
maruel@chromium.org | d58bf5b | 2013-04-26 17:57:42 +0000 | [diff] [blame] | 142 | raise run_isolated.MappingError( |
| 143 | 'Unable to connect to server %s' % generate_upload_url) |
maruel@chromium.org | d58bf5b | 2013-04-26 17:57:42 +0000 | [diff] [blame] | 144 | |
| 145 | # Do not retry this request on HTTP 50x. Regenerate an upload url each time |
| 146 | # since uploading "consumes" the upload url. |
vadimsh@chromium.org | 80f7300 | 2013-07-12 14:52:44 +0000 | [diff] [blame] | 147 | result = run_isolated.url_read( |
maruel@chromium.org | d58bf5b | 2013-04-26 17:57:42 +0000 | [diff] [blame] | 148 | upload_url, data=body, content_type=content_type, retry_50x=False) |
vadimsh@chromium.org | 80f7300 | 2013-07-12 14:52:44 +0000 | [diff] [blame] | 149 | if result is not None: |
| 150 | return result |
maruel@chromium.org | 2b2139a | 2013-04-30 20:14:58 +0000 | [diff] [blame] | 151 | if attempt != run_isolated.URL_OPEN_MAX_ATTEMPTS - 1: |
| 152 | run_isolated.HttpService.sleep_before_retry(attempt, None) |
maruel@chromium.org | d58bf5b | 2013-04-26 17:57:42 +0000 | [diff] [blame] | 153 | raise run_isolated.MappingError( |
| 154 | 'Unable to connect to server %s' % generate_upload_url) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 155 | |
| 156 | |
| 157 | class UploadRemote(run_isolated.Remote): |
maruel@chromium.org | 034e396 | 2013-03-13 13:34:25 +0000 | [diff] [blame] | 158 | def __init__(self, namespace, base_url, token): |
maruel@chromium.org | 21243ce | 2012-12-20 17:43:00 +0000 | [diff] [blame] | 159 | self.namespace = str(namespace) |
maruel@chromium.org | 034e396 | 2013-03-13 13:34:25 +0000 | [diff] [blame] | 160 | self._token = token |
| 161 | super(UploadRemote, self).__init__(base_url) |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 162 | |
| 163 | def get_file_handler(self, base_url): |
maruel@chromium.org | 21243ce | 2012-12-20 17:43:00 +0000 | [diff] [blame] | 164 | base_url = str(base_url) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 165 | def upload_file(content, hash_key): |
maruel@chromium.org | 034e396 | 2013-03-13 13:34:25 +0000 | [diff] [blame] | 166 | # TODO(maruel): Detect failures. |
maruel@chromium.org | 21243ce | 2012-12-20 17:43:00 +0000 | [diff] [blame] | 167 | hash_key = str(hash_key) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 168 | content_url = base_url.rstrip('/') + '/content/' |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 169 | if len(content) > MIN_SIZE_FOR_DIRECT_BLOBSTORE: |
maruel@chromium.org | dc359e6 | 2013-03-14 13:08:55 +0000 | [diff] [blame] | 170 | url = '%sgenerate_blobstore_url/%s/%s' % ( |
| 171 | content_url, self.namespace, hash_key) |
maruel@chromium.org | e82112e | 2013-04-24 14:41:55 +0000 | [diff] [blame] | 172 | # self._token is stored already quoted but it is unnecessary here, and |
| 173 | # only here. |
| 174 | data = [('token', urllib.unquote(self._token))] |
maruel@chromium.org | dc359e6 | 2013-03-14 13:08:55 +0000 | [diff] [blame] | 175 | upload_hash_content_to_blobstore(url, data, hash_key, content) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 176 | else: |
maruel@chromium.org | 034e396 | 2013-03-13 13:34:25 +0000 | [diff] [blame] | 177 | url = '%sstore/%s/%s?token=%s' % ( |
| 178 | content_url, self.namespace, hash_key, self._token) |
vadimsh@chromium.org | 80f7300 | 2013-07-12 14:52:44 +0000 | [diff] [blame] | 179 | url_read(url, data=content, content_type='application/octet-stream') |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 180 | return upload_file |
| 181 | |
| 182 | |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 183 | def check_files_exist_on_server(query_url, queries): |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 184 | """Queries the server to see which files from this batch already exist there. |
| 185 | |
| 186 | Arguments: |
| 187 | queries: The hash files to potential upload to the server. |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 188 | Returns: |
| 189 | missing_files: list of files that are missing on the server. |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 190 | """ |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 191 | logging.info('Checking existence of %d files...', len(queries)) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 192 | body = ''.join( |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 193 | (binascii.unhexlify(meta_data['h']) for (_, meta_data) in queries)) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 194 | assert (len(body) % 20) == 0, repr(body) |
| 195 | |
vadimsh@chromium.org | 80f7300 | 2013-07-12 14:52:44 +0000 | [diff] [blame] | 196 | response = url_read( |
| 197 | query_url, data=body, content_type='application/octet-stream') |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 198 | if len(queries) != len(response): |
| 199 | raise run_isolated.MappingError( |
| 200 | 'Got an incorrect number of responses from the server. Expected %d, ' |
| 201 | 'but got %d' % (len(queries), len(response))) |
| 202 | |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 203 | missing_files = [ |
| 204 | queries[i] for i, flag in enumerate(response) if flag == chr(0) |
| 205 | ] |
| 206 | logging.info('Queried %d files, %d cache hit', |
| 207 | len(queries), len(queries) - len(missing_files)) |
| 208 | return missing_files |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 209 | |
| 210 | |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 211 | def compression_level(filename): |
| 212 | """Given a filename calculates the ideal compression level to use.""" |
| 213 | file_ext = os.path.splitext(filename)[1].lower() |
| 214 | # TODO(csharp): Profile to find what compression level works best. |
| 215 | return 0 if file_ext in ALREADY_COMPRESSED_TYPES else 7 |
| 216 | |
| 217 | |
maruel@chromium.org | cb3c3d5 | 2013-03-14 18:55:30 +0000 | [diff] [blame] | 218 | def read_and_compress(filepath, level): |
| 219 | """Reads a file and returns its content gzip compressed.""" |
| 220 | compressor = zlib.compressobj(level) |
| 221 | compressed_data = cStringIO.StringIO() |
| 222 | with open(filepath, 'rb') as f: |
| 223 | while True: |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 224 | chunk = f.read(run_isolated.ZIPPED_FILE_CHUNK) |
| 225 | if not chunk: |
| 226 | break |
maruel@chromium.org | cb3c3d5 | 2013-03-14 18:55:30 +0000 | [diff] [blame] | 227 | compressed_data.write(compressor.compress(chunk)) |
| 228 | compressed_data.write(compressor.flush(zlib.Z_FINISH)) |
| 229 | value = compressed_data.getvalue() |
| 230 | compressed_data.close() |
| 231 | return value |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 232 | |
maruel@chromium.org | cb3c3d5 | 2013-03-14 18:55:30 +0000 | [diff] [blame] | 233 | |
| 234 | def zip_and_trigger_upload(infile, metadata, upload_function): |
| 235 | # TODO(csharp): Fix crbug.com/150823 and enable the touched logic again. |
| 236 | # if not metadata['T']: |
| 237 | compressed_data = read_and_compress(infile, compression_level(infile)) |
| 238 | priority = ( |
| 239 | run_isolated.Remote.HIGH if metadata.get('priority', '1') == '0' |
| 240 | else run_isolated.Remote.MED) |
| 241 | return upload_function(priority, compressed_data, metadata['h'], None) |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 242 | |
| 243 | |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 244 | def batch_files_for_check(infiles): |
| 245 | """Splits list of files to check for existence on the server into batches. |
maruel@chromium.org | 35fc0c8 | 2013-01-17 15:14:14 +0000 | [diff] [blame] | 246 | |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 247 | Each batch corresponds to a single 'exists?' query to the server. |
| 248 | |
| 249 | Yields: |
| 250 | batches: list of batches, each batch is a list of files. |
maruel@chromium.org | 35fc0c8 | 2013-01-17 15:14:14 +0000 | [diff] [blame] | 251 | """ |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 252 | # TODO(maruel): Make this adaptative, e.g. only query a few, like 10 in one |
| 253 | # request, for the largest files, since they are the ones most likely to be |
| 254 | # missing, then batch larger requests (up to 500) for the tail since they are |
| 255 | # likely to be present. |
maruel@chromium.org | 35fc0c8 | 2013-01-17 15:14:14 +0000 | [diff] [blame] | 256 | next_queries = [] |
csharp@chromium.org | 90c4581 | 2013-01-23 14:27:21 +0000 | [diff] [blame] | 257 | items = ((k, v) for k, v in infiles.iteritems() if 's' in v) |
| 258 | for relfile, metadata in sorted(items, key=lambda x: -x[1]['s']): |
maruel@chromium.org | 35fc0c8 | 2013-01-17 15:14:14 +0000 | [diff] [blame] | 259 | next_queries.append((relfile, metadata)) |
| 260 | if len(next_queries) == ITEMS_PER_CONTAINS_QUERY: |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 261 | yield next_queries |
maruel@chromium.org | 35fc0c8 | 2013-01-17 15:14:14 +0000 | [diff] [blame] | 262 | next_queries = [] |
| 263 | if next_queries: |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 264 | yield next_queries |
| 265 | |
| 266 | |
| 267 | def get_files_to_upload(contains_hash_url, infiles): |
| 268 | """Yields files that are missing on the server.""" |
| 269 | with run_isolated.ThreadPool(1, 16, 0, prefix='get_files_to_upload') as pool: |
| 270 | for files in batch_files_for_check(infiles): |
| 271 | pool.add_task(0, check_files_exist_on_server, contains_hash_url, files) |
| 272 | for missing_file in itertools.chain.from_iterable(pool.iter_results()): |
| 273 | yield missing_file |
maruel@chromium.org | 35fc0c8 | 2013-01-17 15:14:14 +0000 | [diff] [blame] | 274 | |
| 275 | |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 276 | def upload_sha1_tree(base_url, indir, infiles, namespace): |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 277 | """Uploads the given tree to the given url. |
| 278 | |
| 279 | Arguments: |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 280 | base_url: The base url, it is assume that |base_url|/has/ can be used to |
| 281 | query if an element was already uploaded, and |base_url|/store/ |
| 282 | can be used to upload a new element. |
| 283 | indir: Root directory the infiles are based in. |
maruel@chromium.org | cb3c3d5 | 2013-03-14 18:55:30 +0000 | [diff] [blame] | 284 | infiles: dict of files to upload files from |indir| to |base_url|. |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 285 | namespace: The namespace to use on the server. |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 286 | """ |
| 287 | logging.info('upload tree(base_url=%s, indir=%s, files=%d)' % |
| 288 | (base_url, indir, len(infiles))) |
maruel@chromium.org | cb3c3d5 | 2013-03-14 18:55:30 +0000 | [diff] [blame] | 289 | assert base_url.startswith('http'), base_url |
| 290 | base_url = base_url.rstrip('/') |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 291 | |
maruel@chromium.org | 034e396 | 2013-03-13 13:34:25 +0000 | [diff] [blame] | 292 | # TODO(maruel): Make this request much earlier asynchronously while the files |
| 293 | # are being enumerated. |
vadimsh@chromium.org | 80f7300 | 2013-07-12 14:52:44 +0000 | [diff] [blame] | 294 | token = urllib.quote(url_read(base_url + '/content/get_token')) |
maruel@chromium.org | 034e396 | 2013-03-13 13:34:25 +0000 | [diff] [blame] | 295 | |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 296 | # Create a pool of workers to zip and upload any files missing from |
| 297 | # the server. |
maruel@chromium.org | ea10198 | 2013-07-24 15:54:29 +0000 | [diff] [blame] | 298 | num_threads = run_isolated.num_processors() |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 299 | zipping_pool = run_isolated.ThreadPool(min(2, num_threads), |
| 300 | num_threads, 0, 'zip') |
maruel@chromium.org | 034e396 | 2013-03-13 13:34:25 +0000 | [diff] [blame] | 301 | remote_uploader = UploadRemote(namespace, base_url, token) |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 302 | |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 303 | # Starts the zip and upload process for files that are missing |
| 304 | # from the server. |
| 305 | contains_hash_url = '%s/content/contains/%s?token=%s' % ( |
| 306 | base_url, namespace, token) |
csharp@chromium.org | 20a888c | 2013-01-15 15:06:55 +0000 | [diff] [blame] | 307 | uploaded = [] |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 308 | for relfile, metadata in get_files_to_upload(contains_hash_url, infiles): |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 309 | infile = os.path.join(indir, relfile) |
maruel@chromium.org | 831958f | 2013-01-22 15:01:46 +0000 | [diff] [blame] | 310 | zipping_pool.add_task(0, zip_and_trigger_upload, infile, metadata, |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 311 | remote_uploader.add_item) |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 312 | uploaded.append((relfile, metadata)) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 313 | |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 314 | logging.info('Waiting for all files to finish zipping') |
| 315 | zipping_pool.join() |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 316 | zipping_pool.close() |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 317 | logging.info('All files zipped.') |
| 318 | |
| 319 | logging.info('Waiting for all files to finish uploading') |
maruel@chromium.org | 13eca0b | 2013-01-22 16:42:21 +0000 | [diff] [blame] | 320 | # Will raise if any exception occurred. |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 321 | remote_uploader.join() |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 322 | remote_uploader.close() |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 323 | logging.info('All files are uploaded') |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 324 | |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 325 | total = len(infiles) |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 326 | total_size = sum(metadata.get('s', 0) for metadata in infiles.itervalues()) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 327 | logging.info( |
| 328 | 'Total: %6d, %9.1fkb', |
| 329 | total, |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 330 | sum(m.get('s', 0) for m in infiles.itervalues()) / 1024.) |
csharp@chromium.org | 20a888c | 2013-01-15 15:06:55 +0000 | [diff] [blame] | 331 | cache_hit = set(infiles.iterkeys()) - set(x[0] for x in uploaded) |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 332 | cache_hit_size = sum(infiles[i].get('s', 0) for i in cache_hit) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 333 | logging.info( |
| 334 | 'cache hit: %6d, %9.1fkb, %6.2f%% files, %6.2f%% size', |
| 335 | len(cache_hit), |
| 336 | cache_hit_size / 1024., |
| 337 | len(cache_hit) * 100. / total, |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 338 | cache_hit_size * 100. / total_size if total_size else 0) |
csharp@chromium.org | 20a888c | 2013-01-15 15:06:55 +0000 | [diff] [blame] | 339 | cache_miss = uploaded |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 340 | cache_miss_size = sum(infiles[i[0]].get('s', 0) for i in cache_miss) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 341 | logging.info( |
| 342 | 'cache miss: %6d, %9.1fkb, %6.2f%% files, %6.2f%% size', |
| 343 | len(cache_miss), |
| 344 | cache_miss_size / 1024., |
| 345 | len(cache_miss) * 100. / total, |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 346 | cache_miss_size * 100. / total_size if total_size else 0) |
maruel@chromium.org | cb3c3d5 | 2013-03-14 18:55:30 +0000 | [diff] [blame] | 347 | return 0 |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 348 | |
| 349 | |
maruel@chromium.org | cb3c3d5 | 2013-03-14 18:55:30 +0000 | [diff] [blame] | 350 | def main(args): |
maruel@chromium.org | 46e61cc | 2013-03-25 19:55:34 +0000 | [diff] [blame] | 351 | run_isolated.disable_buffering() |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 352 | parser = optparse.OptionParser( |
| 353 | usage='%prog [options] <file1..fileN> or - to read from stdin', |
| 354 | description=sys.modules[__name__].__doc__) |
maruel@chromium.org | cb3c3d5 | 2013-03-14 18:55:30 +0000 | [diff] [blame] | 355 | parser.add_option('-r', '--remote', help='Remote server to archive to') |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 356 | parser.add_option( |
| 357 | '-v', '--verbose', |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 358 | action='count', default=0, |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 359 | help='Use multiple times to increase verbosity') |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 360 | parser.add_option('--namespace', default='default-gzip', |
| 361 | help='The namespace to use on the server.') |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 362 | |
maruel@chromium.org | cb3c3d5 | 2013-03-14 18:55:30 +0000 | [diff] [blame] | 363 | options, files = parser.parse_args(args) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 364 | |
| 365 | levels = [logging.ERROR, logging.INFO, logging.DEBUG] |
| 366 | logging.basicConfig( |
| 367 | level=levels[min(len(levels)-1, options.verbose)], |
vadimsh@chromium.org | 53f8d5a | 2013-06-19 13:03:55 +0000 | [diff] [blame] | 368 | format='[%(threadName)s] %(asctime)s,%(msecs)03d %(levelname)5s' |
| 369 | ' %(module)15s(%(lineno)3d): %(message)s', |
| 370 | datefmt='%H:%M:%S') |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 371 | if files == ['-']: |
| 372 | files = sys.stdin.readlines() |
| 373 | |
| 374 | if not files: |
| 375 | parser.error('Nothing to upload') |
maruel@chromium.org | cb3c3d5 | 2013-03-14 18:55:30 +0000 | [diff] [blame] | 376 | if not options.remote: |
| 377 | parser.error('Nowhere to send. Please specify --remote') |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 378 | |
| 379 | # Load the necessary metadata. This is going to be rewritten eventually to be |
| 380 | # more efficient. |
| 381 | infiles = dict( |
| 382 | ( |
| 383 | f, |
| 384 | { |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 385 | 's': os.stat(f).st_size, |
maruel@chromium.org | 037758d | 2012-12-10 17:59:46 +0000 | [diff] [blame] | 386 | 'h': sha1_file(f), |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 387 | } |
| 388 | ) |
| 389 | for f in files) |
| 390 | |
| 391 | with run_isolated.Profiler('Archive'): |
maruel@chromium.org | cb3c3d5 | 2013-03-14 18:55:30 +0000 | [diff] [blame] | 392 | return upload_sha1_tree( |
| 393 | base_url=options.remote, |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 394 | indir=os.getcwd(), |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 395 | infiles=infiles, |
| 396 | namespace=options.namespace) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 397 | |
| 398 | |
| 399 | if __name__ == '__main__': |
maruel@chromium.org | cb3c3d5 | 2013-03-14 18:55:30 +0000 | [diff] [blame] | 400 | sys.exit(main(sys.argv[1:])) |