blob: 904d242884a695edb7091f9b428d3e2e8c38fe09 [file] [log] [blame]
maruel@chromium.orgc6f90062012-11-07 18:32:22 +00001#!/usr/bin/env python
Marc-Antoine Ruel8add1242013-11-05 17:28:27 -05002# Copyright 2013 The Swarming Authors. All rights reserved.
Marc-Antoine Ruele98b1122013-11-05 20:27:57 -05003# Use of this source code is governed under the Apache License, Version 2.0 that
4# can be found in the LICENSE file.
maruel@chromium.orgc6f90062012-11-07 18:32:22 +00005
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -04006"""Archives a set of files or directories to an Isolate Server."""
maruel@chromium.orgc6f90062012-11-07 18:32:22 +00007
Cory Massarocc19c8c2015-03-10 13:35:11 -07008__version__ = '0.4.3'
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00009
Cory Massarocc19c8c2015-03-10 13:35:11 -070010import base64
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +000011import functools
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000012import logging
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -040013import optparse
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000014import os
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +000015import re
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +040016import signal
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000017import sys
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -050018import tempfile
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +000019import threading
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000020import time
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -050021import types
maruel@chromium.orge82112e2013-04-24 14:41:55 +000022import urllib
Marc-Antoine Ruel1687b5e2014-02-06 17:47:53 -050023import urlparse
csharp@chromium.org59c7bcf2012-11-21 21:13:18 +000024import zlib
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000025
maruel@chromium.orgfb78d432013-08-28 21:22:40 +000026from third_party import colorama
27from third_party.depot_tools import fix_encoding
28from third_party.depot_tools import subcommand
29
Marc-Antoine Ruel37989932013-11-19 16:28:08 -050030from utils import file_path
Marc-Antoine Ruelf74cffe2015-07-15 15:21:34 -040031from utils import logging_utils
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040032from utils import lru
vadimsh@chromium.org6b706212013-08-28 15:03:46 +000033from utils import net
Marc-Antoine Ruelcfb60852014-07-02 15:22:00 -040034from utils import on_error
vadimsh@chromium.orgb074b162013-08-22 17:55:46 +000035from utils import threading_utils
vadimsh@chromium.orga4326472013-08-24 02:05:41 +000036from utils import tools
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000037
Vadim Shtayurae34e13a2014-02-02 11:23:26 -080038import auth
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -040039import isolated_format
Vadim Shtayurae34e13a2014-02-02 11:23:26 -080040
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000041
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +000042# Version of isolate protocol passed to the server in /handshake request.
43ISOLATE_PROTOCOL_VERSION = '1.0'
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000044
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +000045
Vadim Shtayura3148e072014-09-02 18:51:52 -070046# The file size to be used when we don't know the correct file size,
47# generally used for .isolated files.
48UNKNOWN_FILE_SIZE = None
49
50
51# Maximum expected delay (in seconds) between successive file fetches or uploads
52# in Storage. If it takes longer than that, a deadlock might be happening
53# and all stack frames for all threads are dumped to log.
54DEADLOCK_TIMEOUT = 5 * 60
55
56
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +000057# The number of files to check the isolate server per /pre-upload query.
vadimsh@chromium.orgeea52422013-08-21 19:35:54 +000058# All files are sorted by likelihood of a change in the file content
59# (currently file size is used to estimate this: larger the file -> larger the
60# possibility it has changed). Then first ITEMS_PER_CONTAINS_QUERIES[0] files
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +000061# are taken and send to '/pre-upload', then next ITEMS_PER_CONTAINS_QUERIES[1],
vadimsh@chromium.orgeea52422013-08-21 19:35:54 +000062# and so on. Numbers here is a trade-off; the more per request, the lower the
63# effect of HTTP round trip latency and TCP-level chattiness. On the other hand,
64# larger values cause longer lookups, increasing the initial latency to start
65# uploading, which is especially an issue for large files. This value is
66# optimized for the "few thousands files to look up with minimal number of large
67# files missing" case.
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -040068ITEMS_PER_CONTAINS_QUERIES = (20, 20, 50, 50, 50, 100)
csharp@chromium.org07fa7592013-01-11 18:19:30 +000069
maruel@chromium.org9958e4a2013-09-17 00:01:48 +000070
csharp@chromium.org59c7bcf2012-11-21 21:13:18 +000071# A list of already compressed extension types that should not receive any
72# compression before being uploaded.
73ALREADY_COMPRESSED_TYPES = [
Marc-Antoine Ruel7f234c82014-08-06 21:55:18 -040074 '7z', 'avi', 'cur', 'gif', 'h264', 'jar', 'jpeg', 'jpg', 'mp4', 'pdf',
75 'png', 'wav', 'zip',
csharp@chromium.org59c7bcf2012-11-21 21:13:18 +000076]
77
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000078
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +000079# Chunk size to use when reading from network stream.
80NET_IO_FILE_CHUNK = 16 * 1024
81
maruel@chromium.org8750e4b2013-09-18 02:37:57 +000082
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +000083# Read timeout in seconds for downloads from isolate storage. If there's no
84# response from the server within this timeout whole download will be aborted.
85DOWNLOAD_READ_TIMEOUT = 60
86
87
maruel@chromium.org41601642013-09-18 19:40:46 +000088# The delay (in seconds) to wait between logging statements when retrieving
89# the required files. This is intended to let the user (or buildbot) know that
90# the program is still running.
91DELAY_BETWEEN_UPDATES_IN_SECS = 30
92
93
Marc-Antoine Ruelac54cb42013-11-18 14:05:35 -050094DEFAULT_BLACKLIST = (
95 # Temporary vim or python files.
96 r'^.+\.(?:pyc|swp)$',
97 # .git or .svn directory.
98 r'^(?:.+' + re.escape(os.path.sep) + r'|)\.(?:git|svn)$',
99)
100
101
Vadim Shtayura8623c272014-12-01 11:45:27 -0800102# A class to use to communicate with the server by default. Can be changed by
103# 'set_storage_api_class'. Default is IsolateServer.
104_storage_api_cls = None
105
106
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -0500107class Error(Exception):
108 """Generic runtime error."""
109 pass
110
111
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400112class Aborted(Error):
113 """Operation aborted."""
114 pass
115
116
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000117def stream_read(stream, chunk_size):
118 """Reads chunks from |stream| and yields them."""
119 while True:
120 data = stream.read(chunk_size)
121 if not data:
122 break
123 yield data
124
125
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -0400126def file_read(filepath, chunk_size=isolated_format.DISK_FILE_CHUNK, offset=0):
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -0800127 """Yields file content in chunks of |chunk_size| starting from |offset|."""
maruel@chromium.org8750e4b2013-09-18 02:37:57 +0000128 with open(filepath, 'rb') as f:
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -0800129 if offset:
130 f.seek(offset)
maruel@chromium.org8750e4b2013-09-18 02:37:57 +0000131 while True:
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000132 data = f.read(chunk_size)
maruel@chromium.org8750e4b2013-09-18 02:37:57 +0000133 if not data:
134 break
135 yield data
136
137
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000138def file_write(filepath, content_generator):
139 """Writes file content as generated by content_generator.
140
maruel@chromium.org8750e4b2013-09-18 02:37:57 +0000141 Creates the intermediary directory as needed.
142
143 Returns the number of bytes written.
144
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000145 Meant to be mocked out in unit tests.
146 """
147 filedir = os.path.dirname(filepath)
148 if not os.path.isdir(filedir):
149 os.makedirs(filedir)
maruel@chromium.org8750e4b2013-09-18 02:37:57 +0000150 total = 0
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000151 with open(filepath, 'wb') as f:
152 for d in content_generator:
maruel@chromium.org8750e4b2013-09-18 02:37:57 +0000153 total += len(d)
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000154 f.write(d)
maruel@chromium.org8750e4b2013-09-18 02:37:57 +0000155 return total
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000156
157
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000158def zip_compress(content_generator, level=7):
159 """Reads chunks from |content_generator| and yields zip compressed chunks."""
160 compressor = zlib.compressobj(level)
161 for chunk in content_generator:
162 compressed = compressor.compress(chunk)
163 if compressed:
164 yield compressed
165 tail = compressor.flush(zlib.Z_FINISH)
166 if tail:
167 yield tail
168
169
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -0400170def zip_decompress(
171 content_generator, chunk_size=isolated_format.DISK_FILE_CHUNK):
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000172 """Reads zipped data from |content_generator| and yields decompressed data.
173
174 Decompresses data in small chunks (no larger than |chunk_size|) so that
175 zip bomb file doesn't cause zlib to preallocate huge amount of memory.
176
177 Raises IOError if data is corrupted or incomplete.
178 """
179 decompressor = zlib.decompressobj()
180 compressed_size = 0
181 try:
182 for chunk in content_generator:
183 compressed_size += len(chunk)
184 data = decompressor.decompress(chunk, chunk_size)
185 if data:
186 yield data
187 while decompressor.unconsumed_tail:
188 data = decompressor.decompress(decompressor.unconsumed_tail, chunk_size)
189 if data:
190 yield data
191 tail = decompressor.flush()
192 if tail:
193 yield tail
194 except zlib.error as e:
195 raise IOError(
196 'Corrupted zip stream (read %d bytes) - %s' % (compressed_size, e))
197 # Ensure all data was read and decompressed.
198 if decompressor.unused_data or decompressor.unconsumed_tail:
199 raise IOError('Not all data was decompressed')
200
201
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000202def get_zip_compression_level(filename):
203 """Given a filename calculates the ideal zip compression level to use."""
204 file_ext = os.path.splitext(filename)[1].lower()
205 # TODO(csharp): Profile to find what compression level works best.
206 return 0 if file_ext in ALREADY_COMPRESSED_TYPES else 7
207
208
maruel@chromium.orgaf254852013-09-17 17:48:14 +0000209def create_directories(base_directory, files):
210 """Creates the directory structure needed by the given list of files."""
211 logging.debug('create_directories(%s, %d)', base_directory, len(files))
212 # Creates the tree of directories to create.
213 directories = set(os.path.dirname(f) for f in files)
214 for item in list(directories):
215 while item:
216 directories.add(item)
217 item = os.path.dirname(item)
218 for d in sorted(directories):
219 if d:
220 os.mkdir(os.path.join(base_directory, d))
221
222
Marc-Antoine Ruelccafe0e2013-11-08 16:15:36 -0500223def create_symlinks(base_directory, files):
224 """Creates any symlinks needed by the given set of files."""
maruel@chromium.orgaf254852013-09-17 17:48:14 +0000225 for filepath, properties in files:
226 if 'l' not in properties:
227 continue
228 if sys.platform == 'win32':
Marc-Antoine Ruelccafe0e2013-11-08 16:15:36 -0500229 # TODO(maruel): Create symlink via the win32 api.
maruel@chromium.orgaf254852013-09-17 17:48:14 +0000230 logging.warning('Ignoring symlink %s', filepath)
231 continue
232 outfile = os.path.join(base_directory, filepath)
Marc-Antoine Ruelccafe0e2013-11-08 16:15:36 -0500233 # os.symlink() doesn't exist on Windows.
maruel@chromium.orgaf254852013-09-17 17:48:14 +0000234 os.symlink(properties['l'], outfile) # pylint: disable=E1101
maruel@chromium.orgaf254852013-09-17 17:48:14 +0000235
236
maruel@chromium.orge45728d2013-09-16 23:23:22 +0000237def is_valid_file(filepath, size):
maruel@chromium.orgdedbf492013-09-12 20:42:11 +0000238 """Determines if the given files appears valid.
239
240 Currently it just checks the file's size.
241 """
Vadim Shtayura3148e072014-09-02 18:51:52 -0700242 if size == UNKNOWN_FILE_SIZE:
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000243 return os.path.isfile(filepath)
maruel@chromium.orgdedbf492013-09-12 20:42:11 +0000244 actual_size = os.stat(filepath).st_size
245 if size != actual_size:
246 logging.warning(
247 'Found invalid item %s; %d != %d',
248 os.path.basename(filepath), actual_size, size)
249 return False
250 return True
251
252
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000253class Item(object):
254 """An item to push to Storage.
255
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800256 Its digest and size may be provided in advance, if known. Otherwise they will
257 be derived from content(). If digest is provided, it MUST correspond to
258 hash algorithm used by Storage.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000259
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800260 When used with Storage, Item starts its life in a main thread, travels
261 to 'contains' thread, then to 'push' thread and then finally back to
262 the main thread. It is never used concurrently from multiple threads.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000263 """
264
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800265 def __init__(self, digest=None, size=None, high_priority=False):
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000266 self.digest = digest
267 self.size = size
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800268 self.high_priority = high_priority
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000269 self.compression_level = 6
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000270
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800271 def content(self):
272 """Iterable with content of this item as byte string (str) chunks."""
273 raise NotImplementedError()
274
275 def prepare(self, hash_algo):
276 """Ensures self.digest and self.size are set.
277
278 Uses content() as a source of data to calculate them. Does nothing if digest
279 and size is already known.
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000280
281 Arguments:
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800282 hash_algo: hash algorithm to use to calculate digest.
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000283 """
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800284 if self.digest is None or self.size is None:
285 digest = hash_algo()
286 total = 0
287 for chunk in self.content():
288 digest.update(chunk)
289 total += len(chunk)
290 self.digest = digest.hexdigest()
291 self.size = total
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000292
293
294class FileItem(Item):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800295 """A file to push to Storage.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000296
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800297 Its digest and size may be provided in advance, if known. Otherwise they will
298 be derived from the file content.
299 """
300
301 def __init__(self, path, digest=None, size=None, high_priority=False):
302 super(FileItem, self).__init__(
303 digest,
304 size if size is not None else os.stat(path).st_size,
305 high_priority)
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000306 self.path = path
307 self.compression_level = get_zip_compression_level(path)
308
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800309 def content(self):
310 return file_read(self.path)
maruel@chromium.orgc6f90062012-11-07 18:32:22 +0000311
312
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000313class BufferItem(Item):
314 """A byte buffer to push to Storage."""
315
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800316 def __init__(self, buf, high_priority=False):
317 super(BufferItem, self).__init__(None, len(buf), high_priority)
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000318 self.buffer = buf
319
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800320 def content(self):
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000321 return [self.buffer]
322
323
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000324class Storage(object):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800325 """Efficiently downloads or uploads large set of files via StorageApi.
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000326
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800327 Implements compression support, parallel 'contains' checks, parallel uploads
328 and more.
329
330 Works only within single namespace (and thus hashing algorithm and compression
331 scheme are fixed).
332
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400333 Spawns multiple internal threads. Thread safe, but not fork safe. Modifies
334 signal handlers table to handle Ctrl+C.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800335 """
336
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700337 def __init__(self, storage_api):
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000338 self._storage_api = storage_api
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -0400339 self._use_zip = isolated_format.is_namespace_with_compression(
340 storage_api.namespace)
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -0400341 self._hash_algo = isolated_format.get_hash_algo(storage_api.namespace)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000342 self._cpu_thread_pool = None
343 self._net_thread_pool = None
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400344 self._aborted = False
345 self._prev_sig_handlers = {}
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000346
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000347 @property
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700348 def hash_algo(self):
349 """Hashing algorithm used to name files in storage based on their content.
350
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -0400351 Defined by |namespace|. See also isolated_format.get_hash_algo().
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700352 """
353 return self._hash_algo
354
355 @property
356 def location(self):
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -0500357 """URL of the backing store that this class is using."""
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700358 return self._storage_api.location
359
360 @property
361 def namespace(self):
362 """Isolate namespace used by this storage.
363
364 Indirectly defines hashing scheme and compression method used.
365 """
366 return self._storage_api.namespace
367
368 @property
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000369 def cpu_thread_pool(self):
370 """ThreadPool for CPU-bound tasks like zipping."""
371 if self._cpu_thread_pool is None:
Marc-Antoine Ruelbdad1182015-02-06 16:04:35 -0500372 threads = max(threading_utils.num_processors(), 2)
373 if sys.maxsize <= 2L**32:
374 # On 32 bits userland, do not try to use more than 16 threads.
375 threads = min(threads, 16)
376 self._cpu_thread_pool = threading_utils.ThreadPool(2, threads, 0, 'zip')
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000377 return self._cpu_thread_pool
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000378
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000379 @property
380 def net_thread_pool(self):
381 """AutoRetryThreadPool for IO-bound tasks, retries IOError."""
382 if self._net_thread_pool is None:
Vadim Shtayura3148e072014-09-02 18:51:52 -0700383 self._net_thread_pool = threading_utils.IOAutoRetryThreadPool()
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000384 return self._net_thread_pool
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000385
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000386 def close(self):
387 """Waits for all pending tasks to finish."""
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400388 logging.info('Waiting for all threads to die...')
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000389 if self._cpu_thread_pool:
390 self._cpu_thread_pool.join()
391 self._cpu_thread_pool.close()
392 self._cpu_thread_pool = None
393 if self._net_thread_pool:
394 self._net_thread_pool.join()
395 self._net_thread_pool.close()
396 self._net_thread_pool = None
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400397 logging.info('Done.')
398
399 def abort(self):
400 """Cancels any pending or future operations."""
401 # This is not strictly theadsafe, but in the worst case the logging message
402 # will be printed twice. Not a big deal. In other places it is assumed that
403 # unprotected reads and writes to _aborted are serializable (it is true
404 # for python) and thus no locking is used.
405 if not self._aborted:
406 logging.warning('Aborting... It can take a while.')
407 self._aborted = True
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000408
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000409 def __enter__(self):
410 """Context manager interface."""
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400411 assert not self._prev_sig_handlers, self._prev_sig_handlers
412 for s in (signal.SIGINT, signal.SIGTERM):
413 self._prev_sig_handlers[s] = signal.signal(s, lambda *_args: self.abort())
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000414 return self
415
416 def __exit__(self, _exc_type, _exc_value, _traceback):
417 """Context manager interface."""
418 self.close()
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400419 while self._prev_sig_handlers:
420 s, h = self._prev_sig_handlers.popitem()
421 signal.signal(s, h)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000422 return False
423
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000424 def upload_items(self, items):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800425 """Uploads a bunch of items to the isolate server.
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000426
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800427 It figures out what items are missing from the server and uploads only them.
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000428
429 Arguments:
430 items: list of Item instances that represents data to upload.
431
432 Returns:
433 List of items that were uploaded. All other items are already there.
434 """
Vadim Shtayuraea38c572014-10-06 16:57:16 -0700435 logging.info('upload_items(items=%d)', len(items))
436
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800437 # Ensure all digests are calculated.
438 for item in items:
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700439 item.prepare(self._hash_algo)
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800440
vadimsh@chromium.org672cd2b2013-10-08 17:49:33 +0000441 # For each digest keep only first Item that matches it. All other items
442 # are just indistinguishable copies from the point of view of isolate
443 # server (it doesn't care about paths at all, only content and digests).
444 seen = {}
445 duplicates = 0
446 for item in items:
447 if seen.setdefault(item.digest, item) is not item:
448 duplicates += 1
449 items = seen.values()
450 if duplicates:
Vadim Shtayuraea38c572014-10-06 16:57:16 -0700451 logging.info('Skipped %d files with duplicated content', duplicates)
vadimsh@chromium.org672cd2b2013-10-08 17:49:33 +0000452
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000453 # Enqueue all upload tasks.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000454 missing = set()
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000455 uploaded = []
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800456 channel = threading_utils.TaskChannel()
457 for missing_item, push_state in self.get_missing_items(items):
458 missing.add(missing_item)
459 self.async_push(channel, missing_item, push_state)
460
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000461 # No need to spawn deadlock detector thread if there's nothing to upload.
462 if missing:
Vadim Shtayura3148e072014-09-02 18:51:52 -0700463 with threading_utils.DeadlockDetector(DEADLOCK_TIMEOUT) as detector:
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000464 # Wait for all started uploads to finish.
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000465 while len(uploaded) != len(missing):
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000466 detector.ping()
467 item = channel.pull()
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000468 uploaded.append(item)
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000469 logging.debug(
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000470 'Uploaded %d / %d: %s', len(uploaded), len(missing), item.digest)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000471 logging.info('All files are uploaded')
472
473 # Print stats.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000474 total = len(items)
475 total_size = sum(f.size for f in items)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000476 logging.info(
477 'Total: %6d, %9.1fkb',
478 total,
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000479 total_size / 1024.)
480 cache_hit = set(items) - missing
481 cache_hit_size = sum(f.size for f in cache_hit)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000482 logging.info(
483 'cache hit: %6d, %9.1fkb, %6.2f%% files, %6.2f%% size',
484 len(cache_hit),
485 cache_hit_size / 1024.,
486 len(cache_hit) * 100. / total,
487 cache_hit_size * 100. / total_size if total_size else 0)
488 cache_miss = missing
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000489 cache_miss_size = sum(f.size for f in cache_miss)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000490 logging.info(
491 'cache miss: %6d, %9.1fkb, %6.2f%% files, %6.2f%% size',
492 len(cache_miss),
493 cache_miss_size / 1024.,
494 len(cache_miss) * 100. / total,
495 cache_miss_size * 100. / total_size if total_size else 0)
496
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000497 return uploaded
498
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800499 def get_fetch_url(self, item):
500 """Returns an URL that can be used to fetch given item once it's uploaded.
501
502 Note that if namespace uses compression, data at given URL is compressed.
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000503
504 Arguments:
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800505 item: Item to get fetch URL for.
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000506
507 Returns:
508 An URL or None if underlying protocol doesn't support this.
509 """
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700510 item.prepare(self._hash_algo)
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800511 return self._storage_api.get_fetch_url(item.digest)
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000512
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800513 def async_push(self, channel, item, push_state):
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000514 """Starts asynchronous push to the server in a parallel thread.
515
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800516 Can be used only after |item| was checked for presence on a server with
517 'get_missing_items' call. 'get_missing_items' returns |push_state| object
518 that contains storage specific information describing how to upload
519 the item (for example in case of cloud storage, it is signed upload URLs).
520
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000521 Arguments:
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000522 channel: TaskChannel that receives back |item| when upload ends.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000523 item: item to upload as instance of Item class.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800524 push_state: push state returned by 'get_missing_items' call for |item|.
525
526 Returns:
527 None, but |channel| later receives back |item| when upload ends.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000528 """
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800529 # Thread pool task priority.
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -0400530 priority = (
Vadim Shtayura3148e072014-09-02 18:51:52 -0700531 threading_utils.PRIORITY_HIGH if item.high_priority
532 else threading_utils.PRIORITY_MED)
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800533
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000534 def push(content):
Marc-Antoine Ruel095a8be2014-03-21 14:58:19 -0400535 """Pushes an Item and returns it to |channel|."""
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400536 if self._aborted:
537 raise Aborted()
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700538 item.prepare(self._hash_algo)
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800539 self._storage_api.push(item, push_state, content)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000540 return item
541
542 # If zipping is not required, just start a push task.
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700543 if not self._use_zip:
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800544 self.net_thread_pool.add_task_with_channel(
545 channel, priority, push, item.content())
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000546 return
547
548 # If zipping is enabled, zip in a separate thread.
549 def zip_and_push():
550 # TODO(vadimsh): Implement streaming uploads. Before it's done, assemble
551 # content right here. It will block until all file is zipped.
552 try:
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400553 if self._aborted:
554 raise Aborted()
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800555 stream = zip_compress(item.content(), item.compression_level)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000556 data = ''.join(stream)
557 except Exception as exc:
558 logging.error('Failed to zip \'%s\': %s', item, exc)
Vadim Shtayura0ffc4092013-11-20 17:49:52 -0800559 channel.send_exception()
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000560 return
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000561 self.net_thread_pool.add_task_with_channel(
562 channel, priority, push, [data])
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000563 self.cpu_thread_pool.add_task(priority, zip_and_push)
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000564
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800565 def push(self, item, push_state):
566 """Synchronously pushes a single item to the server.
567
568 If you need to push many items at once, consider using 'upload_items' or
569 'async_push' with instance of TaskChannel.
570
571 Arguments:
572 item: item to upload as instance of Item class.
573 push_state: push state returned by 'get_missing_items' call for |item|.
574
575 Returns:
576 Pushed item (same object as |item|).
577 """
578 channel = threading_utils.TaskChannel()
Vadim Shtayura3148e072014-09-02 18:51:52 -0700579 with threading_utils.DeadlockDetector(DEADLOCK_TIMEOUT):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800580 self.async_push(channel, item, push_state)
581 pushed = channel.pull()
582 assert pushed is item
583 return item
584
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000585 def async_fetch(self, channel, priority, digest, size, sink):
586 """Starts asynchronous fetch from the server in a parallel thread.
587
588 Arguments:
589 channel: TaskChannel that receives back |digest| when download ends.
590 priority: thread pool task priority for the fetch.
591 digest: hex digest of an item to download.
592 size: expected size of the item (after decompression).
593 sink: function that will be called as sink(generator).
594 """
595 def fetch():
596 try:
597 # Prepare reading pipeline.
598 stream = self._storage_api.fetch(digest)
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700599 if self._use_zip:
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -0400600 stream = zip_decompress(stream, isolated_format.DISK_FILE_CHUNK)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000601 # Run |stream| through verifier that will assert its size.
602 verifier = FetchStreamVerifier(stream, size)
603 # Verified stream goes to |sink|.
604 sink(verifier.run())
605 except Exception as err:
Vadim Shtayura0ffc4092013-11-20 17:49:52 -0800606 logging.error('Failed to fetch %s: %s', digest, err)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000607 raise
608 return digest
609
610 # Don't bother with zip_thread_pool for decompression. Decompression is
611 # really fast and most probably IO bound anyway.
612 self.net_thread_pool.add_task_with_channel(channel, priority, fetch)
613
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000614 def get_missing_items(self, items):
615 """Yields items that are missing from the server.
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000616
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000617 Issues multiple parallel queries via StorageApi's 'contains' method.
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000618
619 Arguments:
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000620 items: a list of Item objects to check.
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000621
622 Yields:
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800623 For each missing item it yields a pair (item, push_state), where:
624 * item - Item object that is missing (one of |items|).
625 * push_state - opaque object that contains storage specific information
626 describing how to upload the item (for example in case of cloud
627 storage, it is signed upload URLs). It can later be passed to
628 'async_push'.
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000629 """
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000630 channel = threading_utils.TaskChannel()
631 pending = 0
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800632
633 # Ensure all digests are calculated.
634 for item in items:
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700635 item.prepare(self._hash_algo)
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800636
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400637 def contains(batch):
638 if self._aborted:
639 raise Aborted()
640 return self._storage_api.contains(batch)
641
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000642 # Enqueue all requests.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800643 for batch in batch_items_for_check(items):
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -0400644 self.net_thread_pool.add_task_with_channel(
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400645 channel, threading_utils.PRIORITY_HIGH, contains, batch)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000646 pending += 1
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800647
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000648 # Yield results as they come in.
649 for _ in xrange(pending):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800650 for missing_item, push_state in channel.pull().iteritems():
651 yield missing_item, push_state
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000652
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000653
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800654def batch_items_for_check(items):
655 """Splits list of items to check for existence on the server into batches.
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000656
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800657 Each batch corresponds to a single 'exists?' query to the server via a call
658 to StorageApi's 'contains' method.
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000659
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800660 Arguments:
661 items: a list of Item objects.
662
663 Yields:
664 Batches of items to query for existence in a single operation,
665 each batch is a list of Item objects.
666 """
667 batch_count = 0
668 batch_size_limit = ITEMS_PER_CONTAINS_QUERIES[0]
669 next_queries = []
670 for item in sorted(items, key=lambda x: x.size, reverse=True):
671 next_queries.append(item)
672 if len(next_queries) == batch_size_limit:
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000673 yield next_queries
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800674 next_queries = []
675 batch_count += 1
676 batch_size_limit = ITEMS_PER_CONTAINS_QUERIES[
677 min(batch_count, len(ITEMS_PER_CONTAINS_QUERIES) - 1)]
678 if next_queries:
679 yield next_queries
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000680
681
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000682class FetchQueue(object):
683 """Fetches items from Storage and places them into LocalCache.
684
685 It manages multiple concurrent fetch operations. Acts as a bridge between
686 Storage and LocalCache so that Storage and LocalCache don't depend on each
687 other at all.
688 """
689
690 def __init__(self, storage, cache):
691 self.storage = storage
692 self.cache = cache
693 self._channel = threading_utils.TaskChannel()
694 self._pending = set()
695 self._accessed = set()
696 self._fetched = cache.cached_set()
697
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -0400698 def add(
Vadim Shtayura3148e072014-09-02 18:51:52 -0700699 self,
700 digest,
701 size=UNKNOWN_FILE_SIZE,
702 priority=threading_utils.PRIORITY_MED):
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000703 """Starts asynchronous fetch of item |digest|."""
704 # Fetching it now?
705 if digest in self._pending:
706 return
707
708 # Mark this file as in use, verify_all_cached will later ensure it is still
709 # in cache.
710 self._accessed.add(digest)
711
712 # Already fetched? Notify cache to update item's LRU position.
713 if digest in self._fetched:
714 # 'touch' returns True if item is in cache and not corrupted.
715 if self.cache.touch(digest, size):
716 return
717 # Item is corrupted, remove it from cache and fetch it again.
718 self._fetched.remove(digest)
719 self.cache.evict(digest)
720
721 # TODO(maruel): It should look at the free disk space, the current cache
722 # size and the size of the new item on every new item:
723 # - Trim the cache as more entries are listed when free disk space is low,
724 # otherwise if the amount of data downloaded during the run > free disk
725 # space, it'll crash.
726 # - Make sure there's enough free disk space to fit all dependencies of
727 # this run! If not, abort early.
728
729 # Start fetching.
730 self._pending.add(digest)
731 self.storage.async_fetch(
732 self._channel, priority, digest, size,
733 functools.partial(self.cache.write, digest))
734
735 def wait(self, digests):
736 """Starts a loop that waits for at least one of |digests| to be retrieved.
737
738 Returns the first digest retrieved.
739 """
740 # Flush any already fetched items.
741 for digest in digests:
742 if digest in self._fetched:
743 return digest
744
745 # Ensure all requested items are being fetched now.
746 assert all(digest in self._pending for digest in digests), (
747 digests, self._pending)
748
749 # Wait for some requested item to finish fetching.
750 while self._pending:
751 digest = self._channel.pull()
752 self._pending.remove(digest)
753 self._fetched.add(digest)
754 if digest in digests:
755 return digest
756
757 # Should never reach this point due to assert above.
758 raise RuntimeError('Impossible state')
759
760 def inject_local_file(self, path, algo):
761 """Adds local file to the cache as if it was fetched from storage."""
762 with open(path, 'rb') as f:
763 data = f.read()
764 digest = algo(data).hexdigest()
765 self.cache.write(digest, [data])
766 self._fetched.add(digest)
767 return digest
768
769 @property
770 def pending_count(self):
771 """Returns number of items to be fetched."""
772 return len(self._pending)
773
774 def verify_all_cached(self):
775 """True if all accessed items are in cache."""
776 return self._accessed.issubset(self.cache.cached_set())
777
778
779class FetchStreamVerifier(object):
780 """Verifies that fetched file is valid before passing it to the LocalCache."""
781
782 def __init__(self, stream, expected_size):
Marc-Antoine Rueldf4976d2015-04-15 19:56:21 -0400783 assert stream is not None
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000784 self.stream = stream
785 self.expected_size = expected_size
786 self.current_size = 0
787
788 def run(self):
789 """Generator that yields same items as |stream|.
790
791 Verifies |stream| is complete before yielding a last chunk to consumer.
792
793 Also wraps IOError produced by consumer into MappingError exceptions since
794 otherwise Storage will retry fetch on unrelated local cache errors.
795 """
796 # Read one chunk ahead, keep it in |stored|.
797 # That way a complete stream can be verified before pushing last chunk
798 # to consumer.
799 stored = None
800 for chunk in self.stream:
801 assert chunk is not None
802 if stored is not None:
803 self._inspect_chunk(stored, is_last=False)
804 try:
805 yield stored
806 except IOError as exc:
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -0400807 raise isolated_format.MappingError(
808 'Failed to store an item in cache: %s' % exc)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000809 stored = chunk
810 if stored is not None:
811 self._inspect_chunk(stored, is_last=True)
812 try:
813 yield stored
814 except IOError as exc:
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -0400815 raise isolated_format.MappingError(
816 'Failed to store an item in cache: %s' % exc)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000817
818 def _inspect_chunk(self, chunk, is_last):
819 """Called for each fetched chunk before passing it to consumer."""
820 self.current_size += len(chunk)
Marc-Antoine Ruel1e7658c2014-08-28 19:46:39 -0400821 if (is_last and
Vadim Shtayura3148e072014-09-02 18:51:52 -0700822 (self.expected_size != UNKNOWN_FILE_SIZE) and
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000823 (self.expected_size != self.current_size)):
824 raise IOError('Incorrect file size: expected %d, got %d' % (
825 self.expected_size, self.current_size))
826
827
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000828class StorageApi(object):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800829 """Interface for classes that implement low-level storage operations.
830
831 StorageApi is oblivious of compression and hashing scheme used. This details
832 are handled in higher level Storage class.
833
834 Clients should generally not use StorageApi directly. Storage class is
835 preferred since it implements compression and upload optimizations.
836 """
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000837
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700838 @property
839 def location(self):
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -0500840 """URL of the backing store that this class is using."""
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700841 raise NotImplementedError()
842
843 @property
844 def namespace(self):
845 """Isolate namespace used by this storage.
846
847 Indirectly defines hashing scheme and compression method used.
848 """
849 raise NotImplementedError()
850
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000851 def get_fetch_url(self, digest):
852 """Returns an URL that can be used to fetch an item with given digest.
853
854 Arguments:
855 digest: hex digest of item to fetch.
856
857 Returns:
858 An URL or None if the protocol doesn't support this.
859 """
860 raise NotImplementedError()
861
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -0800862 def fetch(self, digest, offset=0):
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000863 """Fetches an object and yields its content.
864
865 Arguments:
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000866 digest: hash digest of item to download.
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -0800867 offset: offset (in bytes) from the start of the file to resume fetch from.
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000868
869 Yields:
870 Chunks of downloaded item (as str objects).
871 """
872 raise NotImplementedError()
873
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800874 def push(self, item, push_state, content=None):
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000875 """Uploads an |item| with content generated by |content| generator.
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000876
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800877 |item| MUST go through 'contains' call to get |push_state| before it can
878 be pushed to the storage.
879
880 To be clear, here is one possible usage:
881 all_items = [... all items to push as Item subclasses ...]
882 for missing_item, push_state in storage_api.contains(all_items).items():
883 storage_api.push(missing_item, push_state)
884
885 When pushing to a namespace with compression, data that should be pushed
886 and data provided by the item is not the same. In that case |content| is
887 not None and it yields chunks of compressed data (using item.content() as
888 a source of original uncompressed data). This is implemented by Storage
889 class.
890
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000891 Arguments:
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000892 item: Item object that holds information about an item being pushed.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800893 push_state: push state object as returned by 'contains' call.
894 content: a generator that yields chunks to push, item.content() if None.
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000895
896 Returns:
897 None.
898 """
899 raise NotImplementedError()
900
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000901 def contains(self, items):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800902 """Checks for |items| on the server, prepares missing ones for upload.
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000903
904 Arguments:
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800905 items: list of Item objects to check for presence.
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000906
907 Returns:
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800908 A dict missing Item -> opaque push state object to be passed to 'push'.
909 See doc string for 'push'.
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000910 """
911 raise NotImplementedError()
912
913
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800914class _IsolateServerPushState(object):
915 """Per-item state passed from IsolateServer.contains to IsolateServer.push.
Mike Frysinger27f03da2014-02-12 16:47:01 -0500916
917 Note this needs to be a global class to support pickling.
918 """
919
Cory Massarocc19c8c2015-03-10 13:35:11 -0700920 def __init__(self, preupload_status, size):
921 self.preupload_status = preupload_status
922 gs_upload_url = preupload_status.get('gs_upload_url') or None
923 if gs_upload_url:
924 self.upload_url = gs_upload_url
925 self.finalize_url = '_ah/api/isolateservice/v1/finalize_gs_upload'
926 else:
927 self.upload_url = '_ah/api/isolateservice/v1/store_inline'
928 self.finalize_url = None
Mike Frysinger27f03da2014-02-12 16:47:01 -0500929 self.uploaded = False
930 self.finalized = False
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -0500931 self.size = size
Mike Frysinger27f03da2014-02-12 16:47:01 -0500932
933
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000934class IsolateServer(StorageApi):
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000935 """StorageApi implementation that downloads and uploads to Isolate Server.
936
937 It uploads and downloads directly from Google Storage whenever appropriate.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800938 Works only within single namespace.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000939 """
940
maruel@chromium.org3e42ce82013-09-12 18:36:59 +0000941 def __init__(self, base_url, namespace):
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000942 super(IsolateServer, self).__init__()
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -0500943 assert file_path.is_url(base_url), base_url
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700944 self._base_url = base_url.rstrip('/')
945 self._namespace = namespace
Cory Massarocc19c8c2015-03-10 13:35:11 -0700946 self._namespace_dict = {
947 'compression': 'flate' if namespace.endswith(
948 ('-gzip', '-flate')) else '',
949 'digest_hash': 'sha-1',
950 'namespace': namespace,
951 }
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000952 self._lock = threading.Lock()
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000953 self._server_caps = None
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -0500954 self._memory_use = 0
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000955
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000956 @property
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000957 def _server_capabilities(self):
Cory Massarocc19c8c2015-03-10 13:35:11 -0700958 """Gets server details.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000959
960 Returns:
Cory Massarocc19c8c2015-03-10 13:35:11 -0700961 Server capabilities dictionary as returned by /server_details endpoint.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000962 """
maruel@chromium.org3e42ce82013-09-12 18:36:59 +0000963 # TODO(maruel): Make this request much earlier asynchronously while the
964 # files are being enumerated.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800965
966 # TODO(vadimsh): Put |namespace| in the URL so that server can apply
967 # namespace-level ACLs to this call.
Cory Massarocc19c8c2015-03-10 13:35:11 -0700968
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000969 with self._lock:
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000970 if self._server_caps is None:
Cory Massarocc19c8c2015-03-10 13:35:11 -0700971 self._server_caps = net.url_read_json(
972 url='%s/_ah/api/isolateservice/v1/server_details' % self._base_url,
973 data={})
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000974 return self._server_caps
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000975
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700976 @property
977 def location(self):
978 return self._base_url
979
980 @property
981 def namespace(self):
982 return self._namespace
983
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000984 def get_fetch_url(self, digest):
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000985 assert isinstance(digest, basestring)
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000986 return '%s/content-gs/retrieve/%s/%s' % (
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700987 self._base_url, self._namespace, digest)
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000988
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -0800989 def fetch(self, digest, offset=0):
Cory Massarocc19c8c2015-03-10 13:35:11 -0700990 assert offset >= 0
991 source_url = '%s/_ah/api/isolateservice/v1/retrieve' % (
992 self._base_url)
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -0800993 logging.debug('download_file(%s, %d)', source_url, offset)
Cory Massarocc19c8c2015-03-10 13:35:11 -0700994 response = self.do_fetch(source_url, digest, offset)
maruel@chromium.orge45728d2013-09-16 23:23:22 +0000995
Cory Massarocc19c8c2015-03-10 13:35:11 -0700996 if not response:
997 raise IOError('Attempted to fetch from %s; no data exist.' % source_url)
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -0800998
Cory Massarocc19c8c2015-03-10 13:35:11 -0700999 # for DB uploads
1000 content = response.get('content')
1001 if content is not None:
1002 return base64.b64decode(content)
1003
1004 # for GS entities
1005 connection = net.url_open(response['url'])
1006
1007 # If |offset|, verify server respects it by checking Content-Range.
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -08001008 if offset:
1009 content_range = connection.get_header('Content-Range')
1010 if not content_range:
1011 raise IOError('Missing Content-Range header')
1012
1013 # 'Content-Range' format is 'bytes <offset>-<last_byte_index>/<size>'.
1014 # According to a spec, <size> can be '*' meaning "Total size of the file
1015 # is not known in advance".
1016 try:
1017 match = re.match(r'bytes (\d+)-(\d+)/(\d+|\*)', content_range)
1018 if not match:
1019 raise ValueError()
1020 content_offset = int(match.group(1))
1021 last_byte_index = int(match.group(2))
1022 size = None if match.group(3) == '*' else int(match.group(3))
1023 except ValueError:
1024 raise IOError('Invalid Content-Range header: %s' % content_range)
1025
1026 # Ensure returned offset equals requested one.
1027 if offset != content_offset:
1028 raise IOError('Expecting offset %d, got %d (Content-Range is %s)' % (
1029 offset, content_offset, content_range))
1030
1031 # Ensure entire tail of the file is returned.
1032 if size is not None and last_byte_index + 1 != size:
1033 raise IOError('Incomplete response. Content-Range: %s' % content_range)
1034
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001035 return stream_read(connection, NET_IO_FILE_CHUNK)
maruel@chromium.orge45728d2013-09-16 23:23:22 +00001036
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001037 def push(self, item, push_state, content=None):
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001038 assert isinstance(item, Item)
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001039 assert item.digest is not None
1040 assert item.size is not None
1041 assert isinstance(push_state, _IsolateServerPushState)
1042 assert not push_state.finalized
1043
1044 # Default to item.content().
1045 content = item.content() if content is None else content
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -05001046 logging.info('Push state size: %d', push_state.size)
1047 if isinstance(content, (basestring, list)):
1048 # Memory is already used, too late.
1049 with self._lock:
1050 self._memory_use += push_state.size
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +00001051 else:
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -05001052 # TODO(vadimsh): Do not read from |content| generator when retrying push.
1053 # If |content| is indeed a generator, it can not be re-winded back to the
1054 # beginning of the stream. A retry will find it exhausted. A possible
1055 # solution is to wrap |content| generator with some sort of caching
1056 # restartable generator. It should be done alongside streaming support
1057 # implementation.
1058 #
1059 # In theory, we should keep the generator, so that it is not serialized in
1060 # memory. Sadly net.HttpService.request() requires the body to be
1061 # serialized.
1062 assert isinstance(content, types.GeneratorType), repr(content)
1063 slept = False
1064 # HACK HACK HACK. Please forgive me for my sins but OMG, it works!
Marc-Antoine Ruele6677c82015-02-05 14:54:22 -05001065 # One byte less than 512mb. This is to cope with incompressible content.
1066 max_size = int(sys.maxsize * 0.25)
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -05001067 while True:
1068 with self._lock:
1069 # This is due to 32 bits python when uploading very large files. The
1070 # problem is that it's comparing uncompressed sizes, while we care
1071 # about compressed sizes since it's what is serialized in memory.
1072 # The first check assumes large files are compressible and that by
1073 # throttling one upload at once, we can survive. Otherwise, kaboom.
1074 memory_use = self._memory_use
1075 if ((push_state.size >= max_size and not memory_use) or
1076 (memory_use + push_state.size <= max_size)):
1077 self._memory_use += push_state.size
1078 memory_use = self._memory_use
1079 break
1080 time.sleep(0.1)
1081 slept = True
1082 if slept:
1083 logging.info('Unblocked: %d %d', memory_use, push_state.size)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +00001084
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -05001085 try:
1086 # This push operation may be a retry after failed finalization call below,
1087 # no need to reupload contents in that case.
1088 if not push_state.uploaded:
1089 # PUT file to |upload_url|.
Cory Massarocc19c8c2015-03-10 13:35:11 -07001090 success = self.do_push(push_state, content)
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -05001091 if not success:
Cory Massarocc19c8c2015-03-10 13:35:11 -07001092 raise IOError('Failed to upload file with hash %s to URL %s' % (
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -05001093 item.digest, push_state.upload_url))
1094 push_state.uploaded = True
1095 else:
1096 logging.info(
1097 'A file %s already uploaded, retrying finalization only',
1098 item.digest)
1099
1100 # Optionally notify the server that it's done.
1101 if push_state.finalize_url:
1102 # TODO(vadimsh): Calculate MD5 or CRC32C sum while uploading a file and
1103 # send it to isolated server. That way isolate server can verify that
1104 # the data safely reached Google Storage (GS provides MD5 and CRC32C of
1105 # stored files).
1106 # TODO(maruel): Fix the server to accept properly data={} so
1107 # url_read_json() can be used.
Cory Massarocc19c8c2015-03-10 13:35:11 -07001108 response = net.url_read_json(
1109 url='%s/%s' % (self._base_url, push_state.finalize_url),
1110 data={
1111 'upload_ticket': push_state.preupload_status['upload_ticket'],
1112 })
1113 if not response or not response['ok']:
1114 raise IOError('Failed to finalize file with hash %s.' % item.digest)
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -05001115 push_state.finalized = True
1116 finally:
1117 with self._lock:
1118 self._memory_use -= push_state.size
maruel@chromium.orgd1e20c92013-09-17 20:54:26 +00001119
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001120 def contains(self, items):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001121 # Ensure all items were initialized with 'prepare' call. Storage does that.
1122 assert all(i.digest is not None and i.size is not None for i in items)
1123
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001124 # Request body is a json encoded list of dicts.
Cory Massarocc19c8c2015-03-10 13:35:11 -07001125 body = {
1126 'items': [
1127 {
1128 'digest': item.digest,
1129 'is_isolated': bool(item.high_priority),
1130 'size': item.size,
1131 } for item in items
1132 ],
1133 'namespace': self._namespace_dict,
1134 }
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001135
Cory Massarocc19c8c2015-03-10 13:35:11 -07001136 query_url = '%s/_ah/api/isolateservice/v1/preupload' % self._base_url
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001137
1138 # Response body is a list of push_urls (or null if file is already present).
Marc-Antoine Ruel0a620612014-08-13 15:47:07 -04001139 response = None
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001140 try:
Marc-Antoine Ruel0a620612014-08-13 15:47:07 -04001141 response = net.url_read_json(url=query_url, data=body)
1142 if response is None:
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -04001143 raise isolated_format.MappingError(
Cory Massarocc19c8c2015-03-10 13:35:11 -07001144 'Failed to execute preupload query')
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001145 except ValueError as err:
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -04001146 raise isolated_format.MappingError(
Marc-Antoine Ruel0a620612014-08-13 15:47:07 -04001147 'Invalid response from server: %s, body is %s' % (err, response))
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001148
1149 # Pick Items that are missing, attach _PushState to them.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001150 missing_items = {}
Cory Massarocc19c8c2015-03-10 13:35:11 -07001151 for preupload_status in response.get('items', []):
1152 assert 'upload_ticket' in preupload_status, (
1153 preupload_status, '/preupload did not generate an upload ticket')
1154 index = int(preupload_status['index'])
1155 missing_items[items[index]] = _IsolateServerPushState(
1156 preupload_status, items[index].size)
vadimsh@chromium.org35122be2013-09-19 02:48:00 +00001157 logging.info('Queried %d files, %d cache hit',
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001158 len(items), len(items) - len(missing_items))
1159 return missing_items
maruel@chromium.orgc6f90062012-11-07 18:32:22 +00001160
Cory Massarocc19c8c2015-03-10 13:35:11 -07001161 def do_fetch(self, url, digest, offset):
Vadim Shtayura8623c272014-12-01 11:45:27 -08001162 """Fetches isolated data from the URL.
1163
1164 Used only for fetching files, not for API calls. Can be overridden in
1165 subclasses.
1166
1167 Args:
1168 url: URL to fetch the data from, can possibly return http redirect.
1169 offset: byte offset inside the file to start fetching from.
1170
1171 Returns:
1172 net.HttpResponse compatible object, with 'read' and 'get_header' calls.
1173 """
Cory Massarocc19c8c2015-03-10 13:35:11 -07001174 assert isinstance(offset, int)
1175 data = {
1176 'digest': digest.encode('utf-8'),
1177 'namespace': self._namespace_dict,
1178 'offset': offset,
1179 }
1180 return net.url_read_json(
1181 url=url,
1182 data=data,
1183 read_timeout=DOWNLOAD_READ_TIMEOUT)
Vadim Shtayura8623c272014-12-01 11:45:27 -08001184
Cory Massarocc19c8c2015-03-10 13:35:11 -07001185 def do_push(self, push_state, content):
Vadim Shtayura8623c272014-12-01 11:45:27 -08001186 """Uploads isolated file to the URL.
1187
1188 Used only for storing files, not for API calls. Can be overridden in
1189 subclasses.
1190
1191 Args:
1192 url: URL to upload the data to.
Cory Massarocc19c8c2015-03-10 13:35:11 -07001193 push_state: an _IsolateServicePushState instance
1194 item: the original Item to be uploaded
Vadim Shtayura8623c272014-12-01 11:45:27 -08001195 content: an iterable that yields 'str' chunks.
Vadim Shtayura8623c272014-12-01 11:45:27 -08001196 """
1197 # A cheezy way to avoid memcpy of (possibly huge) file, until streaming
1198 # upload support is implemented.
1199 if isinstance(content, list) and len(content) == 1:
1200 content = content[0]
1201 else:
1202 content = ''.join(content)
Cory Massarocc19c8c2015-03-10 13:35:11 -07001203
1204 # DB upload
1205 if not push_state.finalize_url:
1206 url = '%s/%s' % (self._base_url, push_state.upload_url)
1207 content = base64.b64encode(content)
1208 data = {
1209 'upload_ticket': push_state.preupload_status['upload_ticket'],
1210 'content': content,
1211 }
1212 response = net.url_read_json(url=url, data=data)
1213 return response is not None and response['ok']
1214
1215 # upload to GS
1216 url = push_state.upload_url
Vadim Shtayura8623c272014-12-01 11:45:27 -08001217 response = net.url_read(
Cory Massarocc19c8c2015-03-10 13:35:11 -07001218 content_type='application/octet-stream',
1219 data=content,
1220 method='PUT',
1221 url=url)
Vadim Shtayura8623c272014-12-01 11:45:27 -08001222 return response is not None
1223
maruel@chromium.orgc6f90062012-11-07 18:32:22 +00001224
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001225class LocalCache(object):
1226 """Local cache that stores objects fetched via Storage.
1227
1228 It can be accessed concurrently from multiple threads, so it should protect
1229 its internal state with some lock.
1230 """
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -05001231 cache_dir = None
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001232
1233 def __enter__(self):
1234 """Context manager interface."""
1235 return self
1236
1237 def __exit__(self, _exc_type, _exec_value, _traceback):
1238 """Context manager interface."""
1239 return False
1240
1241 def cached_set(self):
1242 """Returns a set of all cached digests (always a new object)."""
1243 raise NotImplementedError()
1244
1245 def touch(self, digest, size):
1246 """Ensures item is not corrupted and updates its LRU position.
1247
1248 Arguments:
1249 digest: hash digest of item to check.
1250 size: expected size of this item.
1251
1252 Returns:
1253 True if item is in cache and not corrupted.
1254 """
1255 raise NotImplementedError()
1256
1257 def evict(self, digest):
1258 """Removes item from cache if it's there."""
1259 raise NotImplementedError()
1260
1261 def read(self, digest):
1262 """Returns contents of the cached item as a single str."""
1263 raise NotImplementedError()
1264
1265 def write(self, digest, content):
1266 """Reads data from |content| generator and stores it in cache."""
1267 raise NotImplementedError()
1268
Marc-Antoine Ruelfb199cf2013-11-12 15:38:12 -05001269 def hardlink(self, digest, dest, file_mode):
1270 """Ensures file at |dest| has same content as cached |digest|.
1271
1272 If file_mode is provided, it is used to set the executable bit if
1273 applicable.
1274 """
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001275 raise NotImplementedError()
1276
1277
1278class MemoryCache(LocalCache):
1279 """LocalCache implementation that stores everything in memory."""
1280
Vadim Shtayurae3fbd102014-04-29 17:05:21 -07001281 def __init__(self, file_mode_mask=0500):
1282 """Args:
1283 file_mode_mask: bit mask to AND file mode with. Default value will make
1284 all mapped files to be read only.
1285 """
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001286 super(MemoryCache, self).__init__()
Vadim Shtayurae3fbd102014-04-29 17:05:21 -07001287 self._file_mode_mask = file_mode_mask
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001288 # Let's not assume dict is thread safe.
1289 self._lock = threading.Lock()
1290 self._contents = {}
1291
1292 def cached_set(self):
1293 with self._lock:
1294 return set(self._contents)
1295
1296 def touch(self, digest, size):
1297 with self._lock:
1298 return digest in self._contents
1299
1300 def evict(self, digest):
1301 with self._lock:
1302 self._contents.pop(digest, None)
1303
1304 def read(self, digest):
1305 with self._lock:
1306 return self._contents[digest]
1307
1308 def write(self, digest, content):
1309 # Assemble whole stream before taking the lock.
1310 data = ''.join(content)
1311 with self._lock:
1312 self._contents[digest] = data
1313
Marc-Antoine Ruelfb199cf2013-11-12 15:38:12 -05001314 def hardlink(self, digest, dest, file_mode):
1315 """Since data is kept in memory, there is no filenode to hardlink."""
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001316 file_write(dest, [self.read(digest)])
Marc-Antoine Ruelfb199cf2013-11-12 15:38:12 -05001317 if file_mode is not None:
Vadim Shtayurae3fbd102014-04-29 17:05:21 -07001318 os.chmod(dest, file_mode & self._file_mode_mask)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001319
1320
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001321class CachePolicies(object):
1322 def __init__(self, max_cache_size, min_free_space, max_items):
1323 """
1324 Arguments:
1325 - max_cache_size: Trim if the cache gets larger than this value. If 0, the
1326 cache is effectively a leak.
1327 - min_free_space: Trim if disk free space becomes lower than this value. If
1328 0, it unconditionally fill the disk.
1329 - max_items: Maximum number of items to keep in the cache. If 0, do not
1330 enforce a limit.
1331 """
1332 self.max_cache_size = max_cache_size
1333 self.min_free_space = min_free_space
1334 self.max_items = max_items
1335
1336
1337class DiskCache(LocalCache):
1338 """Stateful LRU cache in a flat hash table in a directory.
1339
1340 Saves its state as json file.
1341 """
1342 STATE_FILE = 'state.json'
1343
1344 def __init__(self, cache_dir, policies, hash_algo):
1345 """
1346 Arguments:
1347 cache_dir: directory where to place the cache.
1348 policies: cache retention policies.
1349 algo: hashing algorithm used.
1350 """
1351 super(DiskCache, self).__init__()
1352 self.cache_dir = cache_dir
1353 self.policies = policies
1354 self.hash_algo = hash_algo
1355 self.state_file = os.path.join(cache_dir, self.STATE_FILE)
1356
1357 # All protected methods (starting with '_') except _path should be called
1358 # with this lock locked.
1359 self._lock = threading_utils.LockWithAssert()
1360 self._lru = lru.LRUDict()
1361
1362 # Profiling values.
1363 self._added = []
1364 self._removed = []
1365 self._free_disk = 0
1366
1367 with tools.Profiler('Setup'):
1368 with self._lock:
1369 self._load()
1370
1371 def __enter__(self):
1372 return self
1373
1374 def __exit__(self, _exc_type, _exec_value, _traceback):
1375 with tools.Profiler('CleanupTrimming'):
1376 with self._lock:
1377 self._trim()
1378
1379 logging.info(
1380 '%5d (%8dkb) added',
1381 len(self._added), sum(self._added) / 1024)
1382 logging.info(
1383 '%5d (%8dkb) current',
1384 len(self._lru),
1385 sum(self._lru.itervalues()) / 1024)
1386 logging.info(
1387 '%5d (%8dkb) removed',
1388 len(self._removed), sum(self._removed) / 1024)
1389 logging.info(
1390 ' %8dkb free',
1391 self._free_disk / 1024)
1392 return False
1393
1394 def cached_set(self):
1395 with self._lock:
1396 return self._lru.keys_set()
1397
1398 def touch(self, digest, size):
1399 """Verifies an actual file is valid.
1400
1401 Note that is doesn't compute the hash so it could still be corrupted if the
1402 file size didn't change.
1403
1404 TODO(maruel): More stringent verification while keeping the check fast.
1405 """
1406 # Do the check outside the lock.
1407 if not is_valid_file(self._path(digest), size):
1408 return False
1409
1410 # Update it's LRU position.
1411 with self._lock:
1412 if digest not in self._lru:
1413 return False
1414 self._lru.touch(digest)
1415 return True
1416
1417 def evict(self, digest):
1418 with self._lock:
1419 self._lru.pop(digest)
1420 self._delete_file(digest, UNKNOWN_FILE_SIZE)
1421
1422 def read(self, digest):
1423 with open(self._path(digest), 'rb') as f:
1424 return f.read()
1425
1426 def write(self, digest, content):
Marc-Antoine Rueldf4976d2015-04-15 19:56:21 -04001427 assert content is not None
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001428 path = self._path(digest)
1429 # A stale broken file may remain. It is possible for the file to have write
1430 # access bit removed which would cause the file_write() call to fail to open
1431 # in write mode. Take no chance here.
1432 file_path.try_remove(path)
1433 try:
1434 size = file_write(path, content)
1435 except:
1436 # There are two possible places were an exception can occur:
1437 # 1) Inside |content| generator in case of network or unzipping errors.
1438 # 2) Inside file_write itself in case of disk IO errors.
1439 # In any case delete an incomplete file and propagate the exception to
1440 # caller, it will be logged there.
1441 file_path.try_remove(path)
1442 raise
1443 # Make the file read-only in the cache. This has a few side-effects since
1444 # the file node is modified, so every directory entries to this file becomes
1445 # read-only. It's fine here because it is a new file.
1446 file_path.set_read_only(path, True)
1447 with self._lock:
1448 self._add(digest, size)
1449
1450 def hardlink(self, digest, dest, file_mode):
1451 """Hardlinks the file to |dest|.
1452
1453 Note that the file permission bits are on the file node, not the directory
1454 entry, so changing the access bit on any of the directory entries for the
1455 file node will affect them all.
1456 """
1457 path = self._path(digest)
1458 # TODO(maruel): file_path.HARDLINK_WITH_FALLBACK ?
1459 file_path.hardlink(path, dest)
1460 if file_mode is not None:
1461 # Ignores all other bits.
1462 os.chmod(dest, file_mode & 0500)
1463
1464 def _load(self):
1465 """Loads state of the cache from json file."""
1466 self._lock.assert_locked()
1467
1468 if not os.path.isdir(self.cache_dir):
1469 os.makedirs(self.cache_dir)
1470 else:
1471 # Make sure the cache is read-only.
1472 # TODO(maruel): Calculate the cost and optimize the performance
1473 # accordingly.
1474 file_path.make_tree_read_only(self.cache_dir)
1475
1476 # Load state of the cache.
1477 if os.path.isfile(self.state_file):
1478 try:
1479 self._lru = lru.LRUDict.load(self.state_file)
1480 except ValueError as err:
1481 logging.error('Failed to load cache state: %s' % (err,))
1482 # Don't want to keep broken state file.
1483 file_path.try_remove(self.state_file)
1484
1485 # Ensure that all files listed in the state still exist and add new ones.
1486 previous = self._lru.keys_set()
1487 unknown = []
1488 for filename in os.listdir(self.cache_dir):
1489 if filename == self.STATE_FILE:
1490 continue
1491 if filename in previous:
1492 previous.remove(filename)
1493 continue
1494 # An untracked file.
1495 if not isolated_format.is_valid_hash(filename, self.hash_algo):
1496 logging.warning('Removing unknown file %s from cache', filename)
Marc-Antoine Ruel8cd33372015-02-09 12:54:43 -05001497 p = self._path(filename)
1498 if os.path.isdir(p):
1499 try:
1500 file_path.rmtree(p)
1501 except OSError:
1502 pass
1503 else:
1504 file_path.try_remove(p)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001505 continue
1506 # File that's not referenced in 'state.json'.
1507 # TODO(vadimsh): Verify its SHA1 matches file name.
1508 logging.warning('Adding unknown file %s to cache', filename)
1509 unknown.append(filename)
1510
1511 if unknown:
1512 # Add as oldest files. They will be deleted eventually if not accessed.
1513 self._add_oldest_list(unknown)
1514 logging.warning('Added back %d unknown files', len(unknown))
1515
1516 if previous:
1517 # Filter out entries that were not found.
1518 logging.warning('Removed %d lost files', len(previous))
1519 for filename in previous:
1520 self._lru.pop(filename)
1521 self._trim()
1522
1523 def _save(self):
1524 """Saves the LRU ordering."""
1525 self._lock.assert_locked()
1526 if sys.platform != 'win32':
1527 d = os.path.dirname(self.state_file)
1528 if os.path.isdir(d):
1529 # Necessary otherwise the file can't be created.
1530 file_path.set_read_only(d, False)
1531 if os.path.isfile(self.state_file):
1532 file_path.set_read_only(self.state_file, False)
1533 self._lru.save(self.state_file)
1534
1535 def _trim(self):
1536 """Trims anything we don't know, make sure enough free space exists."""
1537 self._lock.assert_locked()
1538
1539 # Ensure maximum cache size.
1540 if self.policies.max_cache_size:
1541 total_size = sum(self._lru.itervalues())
1542 while total_size > self.policies.max_cache_size:
1543 total_size -= self._remove_lru_file()
1544
1545 # Ensure maximum number of items in the cache.
1546 if self.policies.max_items and len(self._lru) > self.policies.max_items:
1547 for _ in xrange(len(self._lru) - self.policies.max_items):
1548 self._remove_lru_file()
1549
1550 # Ensure enough free space.
1551 self._free_disk = file_path.get_free_space(self.cache_dir)
1552 trimmed_due_to_space = False
1553 while (
1554 self.policies.min_free_space and
1555 self._lru and
1556 self._free_disk < self.policies.min_free_space):
1557 trimmed_due_to_space = True
1558 self._remove_lru_file()
1559 self._free_disk = file_path.get_free_space(self.cache_dir)
1560 if trimmed_due_to_space:
1561 total_usage = sum(self._lru.itervalues())
1562 usage_percent = 0.
1563 if total_usage:
1564 usage_percent = 100. * self.policies.max_cache_size / float(total_usage)
1565 logging.warning(
1566 'Trimmed due to not enough free disk space: %.1fkb free, %.1fkb '
1567 'cache (%.1f%% of its maximum capacity)',
1568 self._free_disk / 1024.,
1569 total_usage / 1024.,
1570 usage_percent)
1571 self._save()
1572
1573 def _path(self, digest):
1574 """Returns the path to one item."""
1575 return os.path.join(self.cache_dir, digest)
1576
1577 def _remove_lru_file(self):
1578 """Removes the last recently used file and returns its size."""
1579 self._lock.assert_locked()
1580 digest, size = self._lru.pop_oldest()
1581 self._delete_file(digest, size)
1582 return size
1583
1584 def _add(self, digest, size=UNKNOWN_FILE_SIZE):
1585 """Adds an item into LRU cache marking it as a newest one."""
1586 self._lock.assert_locked()
1587 if size == UNKNOWN_FILE_SIZE:
1588 size = os.stat(self._path(digest)).st_size
1589 self._added.append(size)
1590 self._lru.add(digest, size)
1591
1592 def _add_oldest_list(self, digests):
1593 """Adds a bunch of items into LRU cache marking them as oldest ones."""
1594 self._lock.assert_locked()
1595 pairs = []
1596 for digest in digests:
1597 size = os.stat(self._path(digest)).st_size
1598 self._added.append(size)
1599 pairs.append((digest, size))
1600 self._lru.batch_insert_oldest(pairs)
1601
1602 def _delete_file(self, digest, size=UNKNOWN_FILE_SIZE):
1603 """Deletes cache file from the file system."""
1604 self._lock.assert_locked()
1605 try:
1606 if size == UNKNOWN_FILE_SIZE:
1607 size = os.stat(self._path(digest)).st_size
1608 file_path.try_remove(self._path(digest))
1609 self._removed.append(size)
1610 except OSError as e:
1611 logging.error('Error attempting to delete a file %s:\n%s' % (digest, e))
1612
1613
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001614class IsolatedBundle(object):
1615 """Fetched and parsed .isolated file with all dependencies."""
1616
Vadim Shtayura3148e072014-09-02 18:51:52 -07001617 def __init__(self):
1618 self.command = []
1619 self.files = {}
1620 self.read_only = None
1621 self.relative_cwd = None
1622 # The main .isolated file, a IsolatedFile instance.
1623 self.root = None
1624
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001625 def fetch(self, fetch_queue, root_isolated_hash, algo):
1626 """Fetches the .isolated and all the included .isolated.
Vadim Shtayura3148e072014-09-02 18:51:52 -07001627
1628 It enables support for "included" .isolated files. They are processed in
1629 strict order but fetched asynchronously from the cache. This is important so
1630 that a file in an included .isolated file that is overridden by an embedding
1631 .isolated file is not fetched needlessly. The includes are fetched in one
1632 pass and the files are fetched as soon as all the ones on the left-side
1633 of the tree were fetched.
1634
1635 The prioritization is very important here for nested .isolated files.
1636 'includes' have the highest priority and the algorithm is optimized for both
1637 deep and wide trees. A deep one is a long link of .isolated files referenced
1638 one at a time by one item in 'includes'. A wide one has a large number of
1639 'includes' in a single .isolated file. 'left' is defined as an included
1640 .isolated file earlier in the 'includes' list. So the order of the elements
1641 in 'includes' is important.
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001642
1643 As a side effect this method starts asynchronous fetch of all data files
1644 by adding them to |fetch_queue|. It doesn't wait for data files to finish
1645 fetching though.
Vadim Shtayura3148e072014-09-02 18:51:52 -07001646 """
1647 self.root = isolated_format.IsolatedFile(root_isolated_hash, algo)
1648
1649 # Isolated files being retrieved now: hash -> IsolatedFile instance.
1650 pending = {}
1651 # Set of hashes of already retrieved items to refuse recursive includes.
1652 seen = set()
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001653 # Set of IsolatedFile's whose data files have already being fetched.
1654 processed = set()
Vadim Shtayura3148e072014-09-02 18:51:52 -07001655
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001656 def retrieve_async(isolated_file):
Vadim Shtayura3148e072014-09-02 18:51:52 -07001657 h = isolated_file.obj_hash
1658 if h in seen:
1659 raise isolated_format.IsolatedError(
1660 'IsolatedFile %s is retrieved recursively' % h)
1661 assert h not in pending
1662 seen.add(h)
1663 pending[h] = isolated_file
1664 fetch_queue.add(h, priority=threading_utils.PRIORITY_HIGH)
1665
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001666 # Start fetching root *.isolated file (single file, not the whole bundle).
1667 retrieve_async(self.root)
Vadim Shtayura3148e072014-09-02 18:51:52 -07001668
1669 while pending:
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001670 # Wait until some *.isolated file is fetched, parse it.
Vadim Shtayura3148e072014-09-02 18:51:52 -07001671 item_hash = fetch_queue.wait(pending)
1672 item = pending.pop(item_hash)
1673 item.load(fetch_queue.cache.read(item_hash))
Vadim Shtayura3148e072014-09-02 18:51:52 -07001674
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001675 # Start fetching included *.isolated files.
Vadim Shtayura3148e072014-09-02 18:51:52 -07001676 for new_child in item.children:
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001677 retrieve_async(new_child)
Vadim Shtayura3148e072014-09-02 18:51:52 -07001678
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001679 # Always fetch *.isolated files in traversal order, waiting if necessary
1680 # until next to-be-processed node loads. "Waiting" is done by yielding
1681 # back to the outer loop, that waits until some *.isolated is loaded.
1682 for node in isolated_format.walk_includes(self.root):
1683 if node not in processed:
1684 # Not visited, and not yet loaded -> wait for it to load.
1685 if not node.is_loaded:
1686 break
1687 # Not visited and loaded -> process it and continue the traversal.
1688 self._start_fetching_files(node, fetch_queue)
1689 processed.add(node)
Vadim Shtayura3148e072014-09-02 18:51:52 -07001690
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001691 # All *.isolated files should be processed by now and only them.
1692 all_isolateds = set(isolated_format.walk_includes(self.root))
1693 assert all_isolateds == processed, (all_isolateds, processed)
Vadim Shtayura3148e072014-09-02 18:51:52 -07001694
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001695 # Extract 'command' and other bundle properties.
1696 for node in isolated_format.walk_includes(self.root):
1697 self._update_self(node)
Vadim Shtayura3148e072014-09-02 18:51:52 -07001698 self.relative_cwd = self.relative_cwd or ''
1699
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001700 def _start_fetching_files(self, isolated, fetch_queue):
1701 """Starts fetching files from |isolated| that are not yet being fetched.
Vadim Shtayura3148e072014-09-02 18:51:52 -07001702
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001703 Modifies self.files.
1704 """
1705 logging.debug('fetch_files(%s)', isolated.obj_hash)
1706 for filepath, properties in isolated.data.get('files', {}).iteritems():
1707 # Root isolated has priority on the files being mapped. In particular,
1708 # overridden files must not be fetched.
1709 if filepath not in self.files:
1710 self.files[filepath] = properties
1711 if 'h' in properties:
1712 # Preemptively request files.
1713 logging.debug('fetching %s', filepath)
1714 fetch_queue.add(
1715 properties['h'], properties['s'], threading_utils.PRIORITY_MED)
1716
1717 def _update_self(self, node):
1718 """Extracts bundle global parameters from loaded *.isolated file.
1719
1720 Will be called with each loaded *.isolated file in order of traversal of
1721 isolated include graph (see isolated_format.walk_includes).
1722 """
Vadim Shtayura3148e072014-09-02 18:51:52 -07001723 # Grabs properties.
1724 if not self.command and node.data.get('command'):
1725 # Ensure paths are correctly separated on windows.
1726 self.command = node.data['command']
1727 if self.command:
1728 self.command[0] = self.command[0].replace('/', os.path.sep)
1729 self.command = tools.fix_python_path(self.command)
1730 if self.read_only is None and node.data.get('read_only') is not None:
1731 self.read_only = node.data['read_only']
1732 if (self.relative_cwd is None and
1733 node.data.get('relative_cwd') is not None):
1734 self.relative_cwd = node.data['relative_cwd']
1735
1736
Vadim Shtayura8623c272014-12-01 11:45:27 -08001737def set_storage_api_class(cls):
1738 """Replaces StorageApi implementation used by default."""
1739 global _storage_api_cls
1740 assert _storage_api_cls is None
1741 assert issubclass(cls, StorageApi)
1742 _storage_api_cls = cls
1743
1744
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -05001745def get_storage_api(url, namespace):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001746 """Returns an object that implements low-level StorageApi interface.
1747
1748 It is used by Storage to work with single isolate |namespace|. It should
1749 rarely be used directly by clients, see 'get_storage' for
1750 a better alternative.
1751
1752 Arguments:
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -05001753 url: URL of isolate service to use shared cloud based storage.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001754 namespace: isolate namespace to operate in, also defines hashing and
1755 compression scheme used, i.e. namespace names that end with '-gzip'
1756 store compressed data.
1757
1758 Returns:
1759 Instance of StorageApi subclass.
1760 """
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -05001761 cls = _storage_api_cls or IsolateServer
1762 return cls(url, namespace)
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00001763
1764
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -05001765def get_storage(url, namespace):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001766 """Returns Storage class that can upload and download from |namespace|.
1767
1768 Arguments:
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -05001769 url: URL of isolate service to use shared cloud based storage.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001770 namespace: isolate namespace to operate in, also defines hashing and
1771 compression scheme used, i.e. namespace names that end with '-gzip'
1772 store compressed data.
1773
1774 Returns:
1775 Instance of Storage.
1776 """
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -05001777 return Storage(get_storage_api(url, namespace))
maruel@chromium.orgdedbf492013-09-12 20:42:11 +00001778
maruel@chromium.orgdedbf492013-09-12 20:42:11 +00001779
Vadim Shtayuraea38c572014-10-06 16:57:16 -07001780def upload_tree(base_url, infiles, namespace):
maruel@chromium.orgc6f90062012-11-07 18:32:22 +00001781 """Uploads the given tree to the given url.
1782
1783 Arguments:
Vadim Shtayuraea38c572014-10-06 16:57:16 -07001784 base_url: The url of the isolate server to upload to.
1785 infiles: iterable of pairs (absolute path, metadata dict) of files.
csharp@chromium.org59c7bcf2012-11-21 21:13:18 +00001786 namespace: The namespace to use on the server.
maruel@chromium.orgc6f90062012-11-07 18:32:22 +00001787 """
Vadim Shtayuraea38c572014-10-06 16:57:16 -07001788 # Convert |infiles| into a list of FileItem objects, skip duplicates.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001789 # Filter out symlinks, since they are not represented by items on isolate
1790 # server side.
Vadim Shtayuraea38c572014-10-06 16:57:16 -07001791 items = []
1792 seen = set()
1793 skipped = 0
1794 for filepath, metadata in infiles:
1795 if 'l' not in metadata and filepath not in seen:
1796 seen.add(filepath)
1797 item = FileItem(
1798 path=filepath,
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001799 digest=metadata['h'],
1800 size=metadata['s'],
1801 high_priority=metadata.get('priority') == '0')
Vadim Shtayuraea38c572014-10-06 16:57:16 -07001802 items.append(item)
1803 else:
1804 skipped += 1
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001805
Vadim Shtayuraea38c572014-10-06 16:57:16 -07001806 logging.info('Skipped %d duplicated entries', skipped)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001807 with get_storage(base_url, namespace) as storage:
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001808 storage.upload_items(items)
Vadim Shtayura3148e072014-09-02 18:51:52 -07001809
1810
Vadim Shtayurae0ab1902014-04-29 10:55:27 -07001811def fetch_isolated(isolated_hash, storage, cache, outdir, require_command):
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00001812 """Aggressively downloads the .isolated file(s), then download all the files.
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00001813
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001814 Arguments:
1815 isolated_hash: hash of the root *.isolated file.
1816 storage: Storage class that communicates with isolate storage.
1817 cache: LocalCache class that knows how to store and map files locally.
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001818 outdir: Output directory to map file tree to.
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001819 require_command: Ensure *.isolated specifies a command to run.
1820
1821 Returns:
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001822 IsolatedBundle object that holds details about loaded *.isolated file.
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001823 """
Marc-Antoine Ruel4e8cd182014-06-18 13:27:17 -04001824 logging.debug(
1825 'fetch_isolated(%s, %s, %s, %s, %s)',
1826 isolated_hash, storage, cache, outdir, require_command)
Vadim Shtayurae0ab1902014-04-29 10:55:27 -07001827 # Hash algorithm to use, defined by namespace |storage| is using.
1828 algo = storage.hash_algo
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001829 with cache:
1830 fetch_queue = FetchQueue(storage, cache)
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001831 bundle = IsolatedBundle()
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001832
1833 with tools.Profiler('GetIsolateds'):
1834 # Optionally support local files by manually adding them to cache.
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -04001835 if not isolated_format.is_valid_hash(isolated_hash, algo):
Marc-Antoine Ruel4e8cd182014-06-18 13:27:17 -04001836 logging.debug('%s is not a valid hash, assuming a file', isolated_hash)
1837 try:
1838 isolated_hash = fetch_queue.inject_local_file(isolated_hash, algo)
1839 except IOError:
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -04001840 raise isolated_format.MappingError(
Marc-Antoine Ruel4e8cd182014-06-18 13:27:17 -04001841 '%s doesn\'t seem to be a valid file. Did you intent to pass a '
1842 'valid hash?' % isolated_hash)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001843
1844 # Load all *.isolated and start loading rest of the files.
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001845 bundle.fetch(fetch_queue, isolated_hash, algo)
1846 if require_command and not bundle.command:
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001847 # TODO(vadimsh): All fetch operations are already enqueue and there's no
1848 # easy way to cancel them.
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -04001849 raise isolated_format.IsolatedError('No command to run')
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00001850
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001851 with tools.Profiler('GetRest'):
1852 # Create file system hierarchy.
1853 if not os.path.isdir(outdir):
1854 os.makedirs(outdir)
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001855 create_directories(outdir, bundle.files)
1856 create_symlinks(outdir, bundle.files.iteritems())
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00001857
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001858 # Ensure working directory exists.
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001859 cwd = os.path.normpath(os.path.join(outdir, bundle.relative_cwd))
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001860 if not os.path.isdir(cwd):
1861 os.makedirs(cwd)
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00001862
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001863 # Multimap: digest -> list of pairs (path, props).
1864 remaining = {}
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001865 for filepath, props in bundle.files.iteritems():
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001866 if 'h' in props:
1867 remaining.setdefault(props['h'], []).append((filepath, props))
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00001868
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001869 # Now block on the remaining files to be downloaded and mapped.
1870 logging.info('Retrieving remaining files (%d of them)...',
1871 fetch_queue.pending_count)
1872 last_update = time.time()
Vadim Shtayura3148e072014-09-02 18:51:52 -07001873 with threading_utils.DeadlockDetector(DEADLOCK_TIMEOUT) as detector:
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001874 while remaining:
1875 detector.ping()
1876
1877 # Wait for any item to finish fetching to cache.
1878 digest = fetch_queue.wait(remaining)
1879
1880 # Link corresponding files to a fetched item in cache.
1881 for filepath, props in remaining.pop(digest):
Marc-Antoine Ruelfb199cf2013-11-12 15:38:12 -05001882 cache.hardlink(
1883 digest, os.path.join(outdir, filepath), props.get('m'))
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001884
1885 # Report progress.
1886 duration = time.time() - last_update
1887 if duration > DELAY_BETWEEN_UPDATES_IN_SECS:
1888 msg = '%d files remaining...' % len(remaining)
1889 print msg
1890 logging.info(msg)
1891 last_update = time.time()
1892
1893 # Cache could evict some items we just tried to fetch, it's a fatal error.
1894 if not fetch_queue.verify_all_cached():
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -04001895 raise isolated_format.MappingError(
1896 'Cache is too small to hold all requested files')
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001897 return bundle
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00001898
1899
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05001900def directory_to_metadata(root, algo, blacklist):
1901 """Returns the FileItem list and .isolated metadata for a directory."""
1902 root = file_path.get_native_path_case(root)
Marc-Antoine Ruel92257792014-08-28 20:51:08 -04001903 paths = isolated_format.expand_directory_and_symlink(
Vadim Shtayura439d3fc2014-05-07 16:05:12 -07001904 root, '.' + os.path.sep, blacklist, sys.platform != 'win32')
Marc-Antoine Ruel92257792014-08-28 20:51:08 -04001905 metadata = {
1906 relpath: isolated_format.file_to_metadata(
Marc-Antoine Ruelf1d827c2014-11-24 15:22:25 -05001907 os.path.join(root, relpath), {}, 0, algo)
Marc-Antoine Ruel92257792014-08-28 20:51:08 -04001908 for relpath in paths
1909 }
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05001910 for v in metadata.itervalues():
1911 v.pop('t')
1912 items = [
1913 FileItem(
1914 path=os.path.join(root, relpath),
1915 digest=meta['h'],
1916 size=meta['s'],
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001917 high_priority=relpath.endswith('.isolated'))
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05001918 for relpath, meta in metadata.iteritems() if 'h' in meta
1919 ]
1920 return items, metadata
1921
1922
Vadim Shtayurae0ab1902014-04-29 10:55:27 -07001923def archive_files_to_storage(storage, files, blacklist):
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -05001924 """Stores every entries and returns the relevant data.
1925
1926 Arguments:
1927 storage: a Storage object that communicates with the remote object store.
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -05001928 files: list of file paths to upload. If a directory is specified, a
1929 .isolated file is created and its hash is returned.
1930 blacklist: function that returns True if a file should be omitted.
1931 """
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05001932 assert all(isinstance(i, unicode) for i in files), files
1933 if len(files) != len(set(map(os.path.abspath, files))):
1934 raise Error('Duplicate entries found.')
1935
1936 results = []
1937 # The temporary directory is only created as needed.
1938 tempdir = None
1939 try:
1940 # TODO(maruel): Yield the files to a worker thread.
1941 items_to_upload = []
1942 for f in files:
1943 try:
1944 filepath = os.path.abspath(f)
1945 if os.path.isdir(filepath):
1946 # Uploading a whole directory.
Vadim Shtayurae0ab1902014-04-29 10:55:27 -07001947 items, metadata = directory_to_metadata(
1948 filepath, storage.hash_algo, blacklist)
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05001949
1950 # Create the .isolated file.
1951 if not tempdir:
Marc-Antoine Ruel3c979cb2015-03-11 13:43:28 -04001952 tempdir = tempfile.mkdtemp(prefix=u'isolateserver')
1953 handle, isolated = tempfile.mkstemp(dir=tempdir, suffix=u'.isolated')
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05001954 os.close(handle)
1955 data = {
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -04001956 'algo':
1957 isolated_format.SUPPORTED_ALGOS_REVERSE[storage.hash_algo],
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05001958 'files': metadata,
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -04001959 'version': isolated_format.ISOLATED_FILE_VERSION,
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05001960 }
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -04001961 isolated_format.save_isolated(isolated, data)
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -04001962 h = isolated_format.hash_file(isolated, storage.hash_algo)
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05001963 items_to_upload.extend(items)
1964 items_to_upload.append(
1965 FileItem(
1966 path=isolated,
1967 digest=h,
1968 size=os.stat(isolated).st_size,
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001969 high_priority=True))
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05001970 results.append((h, f))
1971
1972 elif os.path.isfile(filepath):
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -04001973 h = isolated_format.hash_file(filepath, storage.hash_algo)
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05001974 items_to_upload.append(
1975 FileItem(
1976 path=filepath,
1977 digest=h,
1978 size=os.stat(filepath).st_size,
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001979 high_priority=f.endswith('.isolated')))
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05001980 results.append((h, f))
1981 else:
1982 raise Error('%s is neither a file or directory.' % f)
1983 except OSError:
1984 raise Error('Failed to process %s.' % f)
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -05001985 # Technically we would care about which files were uploaded but we don't
1986 # much in practice.
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05001987 _uploaded_files = storage.upload_items(items_to_upload)
1988 return results
1989 finally:
Marc-Antoine Ruel1b7bfec2015-02-11 15:35:42 -05001990 if tempdir and os.path.isdir(tempdir):
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001991 file_path.rmtree(tempdir)
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05001992
1993
Marc-Antoine Ruel488ce8f2014-02-09 11:25:04 -05001994def archive(out, namespace, files, blacklist):
1995 if files == ['-']:
1996 files = sys.stdin.readlines()
1997
1998 if not files:
1999 raise Error('Nothing to upload')
2000
2001 files = [f.decode('utf-8') for f in files]
Marc-Antoine Ruel488ce8f2014-02-09 11:25:04 -05002002 blacklist = tools.gen_blacklist(blacklist)
2003 with get_storage(out, namespace) as storage:
Vadim Shtayurae0ab1902014-04-29 10:55:27 -07002004 results = archive_files_to_storage(storage, files, blacklist)
Marc-Antoine Ruel488ce8f2014-02-09 11:25:04 -05002005 print('\n'.join('%s %s' % (r[0], r[1]) for r in results))
2006
2007
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002008@subcommand.usage('<file1..fileN> or - to read from stdin')
2009def CMDarchive(parser, args):
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002010 """Archives data to the server.
2011
2012 If a directory is specified, a .isolated file is created the whole directory
2013 is uploaded. Then this .isolated file can be included in another one to run
2014 commands.
2015
2016 The commands output each file that was processed with its content hash. For
2017 directories, the .isolated generated for the directory is listed as the
2018 directory entry itself.
2019 """
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05002020 add_isolate_server_options(parser)
Marc-Antoine Ruel1f8ba352014-11-04 15:55:03 -05002021 add_archive_options(parser)
maruel@chromium.orgcb3c3d52013-03-14 18:55:30 +00002022 options, files = parser.parse_args(args)
Marc-Antoine Ruele290ada2014-12-10 19:48:49 -05002023 process_isolate_server_options(parser, options, True)
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002024 try:
Marc-Antoine Ruel488ce8f2014-02-09 11:25:04 -05002025 archive(options.isolate_server, options.namespace, files, options.blacklist)
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002026 except Error as e:
2027 parser.error(e.args[0])
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002028 return 0
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002029
2030
2031def CMDdownload(parser, args):
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00002032 """Download data from the server.
2033
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00002034 It can either download individual files or a complete tree from a .isolated
2035 file.
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00002036 """
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05002037 add_isolate_server_options(parser)
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00002038 parser.add_option(
Marc-Antoine Ruel185ded42015-01-28 20:49:18 -05002039 '-s', '--isolated', metavar='HASH',
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00002040 help='hash of an isolated file, .isolated file content is discarded, use '
2041 '--file if you need it')
2042 parser.add_option(
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00002043 '-f', '--file', metavar='HASH DEST', default=[], action='append', nargs=2,
2044 help='hash and destination of a file, can be used multiple times')
2045 parser.add_option(
Marc-Antoine Ruelf90861c2015-03-24 20:54:49 -04002046 '-t', '--target', metavar='DIR', default='download',
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00002047 help='destination directory')
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04002048 add_cache_options(parser)
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00002049 options, args = parser.parse_args(args)
2050 if args:
2051 parser.error('Unsupported arguments: %s' % args)
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05002052
Marc-Antoine Ruele290ada2014-12-10 19:48:49 -05002053 process_isolate_server_options(parser, options, True)
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00002054 if bool(options.isolated) == bool(options.file):
2055 parser.error('Use one of --isolated or --file, and only one.')
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00002056
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04002057 cache = process_cache_options(options)
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00002058 options.target = os.path.abspath(options.target)
Marc-Antoine Ruelf90861c2015-03-24 20:54:49 -04002059 if options.isolated:
2060 if (os.path.isfile(options.target) or
2061 (os.path.isdir(options.target) and os.listdir(options.target))):
2062 parser.error(
2063 '--target \'%s\' exists, please use another target' % options.target)
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05002064 with get_storage(options.isolate_server, options.namespace) as storage:
Vadim Shtayura3172be52013-12-03 12:49:05 -08002065 # Fetching individual files.
2066 if options.file:
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04002067 # TODO(maruel): Enable cache in this case too.
Vadim Shtayura3172be52013-12-03 12:49:05 -08002068 channel = threading_utils.TaskChannel()
2069 pending = {}
2070 for digest, dest in options.file:
2071 pending[digest] = dest
2072 storage.async_fetch(
2073 channel,
Vadim Shtayura3148e072014-09-02 18:51:52 -07002074 threading_utils.PRIORITY_MED,
Vadim Shtayura3172be52013-12-03 12:49:05 -08002075 digest,
Vadim Shtayura3148e072014-09-02 18:51:52 -07002076 UNKNOWN_FILE_SIZE,
Vadim Shtayura3172be52013-12-03 12:49:05 -08002077 functools.partial(file_write, os.path.join(options.target, dest)))
2078 while pending:
2079 fetched = channel.pull()
2080 dest = pending.pop(fetched)
2081 logging.info('%s: %s', fetched, dest)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00002082
Vadim Shtayura3172be52013-12-03 12:49:05 -08002083 # Fetching whole isolated tree.
2084 if options.isolated:
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04002085 with cache:
2086 bundle = fetch_isolated(
2087 isolated_hash=options.isolated,
2088 storage=storage,
2089 cache=cache,
2090 outdir=options.target,
2091 require_command=False)
2092 if bundle.command:
2093 rel = os.path.join(options.target, bundle.relative_cwd)
2094 print('To run this test please run from the directory %s:' %
2095 os.path.join(options.target, rel))
2096 print(' ' + ' '.join(bundle.command))
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00002097
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002098 return 0
2099
2100
Marc-Antoine Ruel1f8ba352014-11-04 15:55:03 -05002101def add_archive_options(parser):
2102 parser.add_option(
2103 '--blacklist',
2104 action='append', default=list(DEFAULT_BLACKLIST),
2105 help='List of regexp to use as blacklist filter when uploading '
2106 'directories')
2107
2108
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05002109def add_isolate_server_options(parser):
2110 """Adds --isolate-server and --namespace options to parser."""
Marc-Antoine Ruel1687b5e2014-02-06 17:47:53 -05002111 parser.add_option(
2112 '-I', '--isolate-server',
2113 metavar='URL', default=os.environ.get('ISOLATE_SERVER', ''),
Marc-Antoine Ruel8806e622014-02-12 14:15:53 -05002114 help='URL of the Isolate Server to use. Defaults to the environment '
2115 'variable ISOLATE_SERVER if set. No need to specify https://, this '
2116 'is assumed.')
Marc-Antoine Ruel1687b5e2014-02-06 17:47:53 -05002117 parser.add_option(
2118 '--namespace', default='default-gzip',
2119 help='The namespace to use on the Isolate Server, default: %default')
2120
2121
Marc-Antoine Ruele290ada2014-12-10 19:48:49 -05002122def process_isolate_server_options(parser, options, set_exception_handler):
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05002123 """Processes the --isolate-server option and aborts if not specified.
2124
2125 Returns the identity as determined by the server.
Marc-Antoine Ruel1687b5e2014-02-06 17:47:53 -05002126 """
2127 if not options.isolate_server:
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05002128 parser.error('--isolate-server is required.')
Marc-Antoine Ruel012067b2014-12-10 15:45:42 -05002129 try:
2130 options.isolate_server = net.fix_url(options.isolate_server)
2131 except ValueError as e:
2132 parser.error('--isolate-server %s' % e)
Marc-Antoine Ruele290ada2014-12-10 19:48:49 -05002133 if set_exception_handler:
2134 on_error.report_on_exception_exit(options.isolate_server)
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05002135 try:
2136 return auth.ensure_logged_in(options.isolate_server)
2137 except ValueError as e:
2138 parser.error(str(e))
Marc-Antoine Ruel8806e622014-02-12 14:15:53 -05002139
Marc-Antoine Ruel1687b5e2014-02-06 17:47:53 -05002140
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04002141def add_cache_options(parser):
2142 cache_group = optparse.OptionGroup(parser, 'Cache management')
2143 cache_group.add_option(
2144 '--cache', metavar='DIR',
2145 help='Directory to keep a local cache of the files. Accelerates download '
2146 'by reusing already downloaded files. Default=%default')
2147 cache_group.add_option(
2148 '--max-cache-size',
2149 type='int',
2150 metavar='NNN',
2151 default=20*1024*1024*1024,
2152 help='Trim if the cache gets larger than this value, default=%default')
2153 cache_group.add_option(
2154 '--min-free-space',
2155 type='int',
2156 metavar='NNN',
2157 default=2*1024*1024*1024,
2158 help='Trim if disk free space becomes lower than this value, '
2159 'default=%default')
2160 cache_group.add_option(
2161 '--max-items',
2162 type='int',
2163 metavar='NNN',
2164 default=100000,
2165 help='Trim if more than this number of items are in the cache '
2166 'default=%default')
2167 parser.add_option_group(cache_group)
2168
2169
2170def process_cache_options(options):
2171 if options.cache:
2172 policies = CachePolicies(
2173 options.max_cache_size, options.min_free_space, options.max_items)
2174
2175 # |options.cache| path may not exist until DiskCache() instance is created.
2176 return DiskCache(
Marc-Antoine Ruel3c979cb2015-03-11 13:43:28 -04002177 unicode(os.path.abspath(options.cache)),
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04002178 policies,
2179 isolated_format.get_hash_algo(options.namespace))
2180 else:
2181 return MemoryCache()
2182
2183
Marc-Antoine Ruelf74cffe2015-07-15 15:21:34 -04002184class OptionParserIsolateServer(logging_utils.OptionParserWithLogging):
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002185 def __init__(self, **kwargs):
Marc-Antoine Ruelf74cffe2015-07-15 15:21:34 -04002186 logging_utils.OptionParserWithLogging.__init__(
Marc-Antoine Ruelac54cb42013-11-18 14:05:35 -05002187 self,
2188 version=__version__,
2189 prog=os.path.basename(sys.modules[__name__].__file__),
2190 **kwargs)
Vadim Shtayurae34e13a2014-02-02 11:23:26 -08002191 auth.add_auth_options(self)
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002192
2193 def parse_args(self, *args, **kwargs):
Marc-Antoine Ruelf74cffe2015-07-15 15:21:34 -04002194 options, args = logging_utils.OptionParserWithLogging.parse_args(
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002195 self, *args, **kwargs)
Vadim Shtayura5d1efce2014-02-04 10:55:43 -08002196 auth.process_auth_options(self, options)
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002197 return options, args
2198
2199
2200def main(args):
2201 dispatcher = subcommand.CommandDispatcher(__name__)
Marc-Antoine Ruelcfb60852014-07-02 15:22:00 -04002202 return dispatcher.execute(OptionParserIsolateServer(), args)
maruel@chromium.orgc6f90062012-11-07 18:32:22 +00002203
2204
2205if __name__ == '__main__':
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002206 fix_encoding.fix_encoding()
2207 tools.disable_buffering()
2208 colorama.init()
maruel@chromium.orgcb3c3d52013-03-14 18:55:30 +00002209 sys.exit(main(sys.argv[1:]))