blob: 761a45d45075d2058a6541a7b453b217f32331f0 [file] [log] [blame]
maruel@chromium.orgc6f90062012-11-07 18:32:22 +00001#!/usr/bin/env python
maruelea586f32016-04-05 11:11:33 -07002# Copyright 2013 The LUCI Authors. All rights reserved.
maruelf1f5e2a2016-05-25 17:10:39 -07003# Use of this source code is governed under the Apache License, Version 2.0
4# that 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
maruel2e8d0f52016-07-16 07:51:29 -07008__version__ = '0.5.0'
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
nodir90bc8dc2016-06-15 13:35:21 -070012import errno
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000013import logging
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -040014import optparse
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000015import os
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +000016import re
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +040017import signal
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000018import sys
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -050019import tempfile
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +000020import threading
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000021import time
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -050022import types
csharp@chromium.org59c7bcf2012-11-21 21:13:18 +000023import zlib
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000024
maruel@chromium.orgfb78d432013-08-28 21:22:40 +000025from third_party import colorama
26from third_party.depot_tools import fix_encoding
27from third_party.depot_tools import subcommand
28
Marc-Antoine Ruel37989932013-11-19 16:28:08 -050029from utils import file_path
maruel12e30012015-10-09 11:55:35 -070030from utils import fs
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
maruel8e4e40c2016-05-30 06:21:07 -070035from utils import subprocess42
vadimsh@chromium.orgb074b162013-08-22 17:55:46 +000036from utils import threading_utils
vadimsh@chromium.orga4326472013-08-24 02:05:41 +000037from utils import tools
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000038
Vadim Shtayurae34e13a2014-02-02 11:23:26 -080039import auth
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -040040import isolated_format
Vadim Shtayurae34e13a2014-02-02 11:23:26 -080041
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000042
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +000043# Version of isolate protocol passed to the server in /handshake request.
44ISOLATE_PROTOCOL_VERSION = '1.0'
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000045
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +000046
Vadim Shtayura3148e072014-09-02 18:51:52 -070047# The file size to be used when we don't know the correct file size,
48# generally used for .isolated files.
49UNKNOWN_FILE_SIZE = None
50
51
52# Maximum expected delay (in seconds) between successive file fetches or uploads
53# in Storage. If it takes longer than that, a deadlock might be happening
54# and all stack frames for all threads are dumped to log.
55DEADLOCK_TIMEOUT = 5 * 60
56
57
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +000058# The number of files to check the isolate server per /pre-upload query.
vadimsh@chromium.orgeea52422013-08-21 19:35:54 +000059# All files are sorted by likelihood of a change in the file content
60# (currently file size is used to estimate this: larger the file -> larger the
61# possibility it has changed). Then first ITEMS_PER_CONTAINS_QUERIES[0] files
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +000062# are taken and send to '/pre-upload', then next ITEMS_PER_CONTAINS_QUERIES[1],
vadimsh@chromium.orgeea52422013-08-21 19:35:54 +000063# and so on. Numbers here is a trade-off; the more per request, the lower the
64# effect of HTTP round trip latency and TCP-level chattiness. On the other hand,
65# larger values cause longer lookups, increasing the initial latency to start
66# uploading, which is especially an issue for large files. This value is
67# optimized for the "few thousands files to look up with minimal number of large
68# files missing" case.
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -040069ITEMS_PER_CONTAINS_QUERIES = (20, 20, 50, 50, 50, 100)
csharp@chromium.org07fa7592013-01-11 18:19:30 +000070
maruel@chromium.org9958e4a2013-09-17 00:01:48 +000071
csharp@chromium.org59c7bcf2012-11-21 21:13:18 +000072# A list of already compressed extension types that should not receive any
73# compression before being uploaded.
74ALREADY_COMPRESSED_TYPES = [
Marc-Antoine Ruel7f234c82014-08-06 21:55:18 -040075 '7z', 'avi', 'cur', 'gif', 'h264', 'jar', 'jpeg', 'jpg', 'mp4', 'pdf',
76 'png', 'wav', 'zip',
csharp@chromium.org59c7bcf2012-11-21 21:13:18 +000077]
78
maruel@chromium.orgc6f90062012-11-07 18:32:22 +000079
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +000080# Chunk size to use when reading from network stream.
81NET_IO_FILE_CHUNK = 16 * 1024
82
maruel@chromium.org8750e4b2013-09-18 02:37:57 +000083
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +000084# Read timeout in seconds for downloads from isolate storage. If there's no
85# response from the server within this timeout whole download will be aborted.
86DOWNLOAD_READ_TIMEOUT = 60
87
88
maruel@chromium.org41601642013-09-18 19:40:46 +000089# The delay (in seconds) to wait between logging statements when retrieving
90# the required files. This is intended to let the user (or buildbot) know that
91# the program is still running.
92DELAY_BETWEEN_UPDATES_IN_SECS = 30
93
94
Marc-Antoine Ruelac54cb42013-11-18 14:05:35 -050095DEFAULT_BLACKLIST = (
96 # Temporary vim or python files.
97 r'^.+\.(?:pyc|swp)$',
98 # .git or .svn directory.
99 r'^(?:.+' + re.escape(os.path.sep) + r'|)\.(?:git|svn)$',
100)
101
102
Vadim Shtayura8623c272014-12-01 11:45:27 -0800103# A class to use to communicate with the server by default. Can be changed by
104# 'set_storage_api_class'. Default is IsolateServer.
105_storage_api_cls = None
106
107
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -0500108class Error(Exception):
109 """Generic runtime error."""
110 pass
111
112
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400113class Aborted(Error):
114 """Operation aborted."""
115 pass
116
117
nodir90bc8dc2016-06-15 13:35:21 -0700118class AlreadyExists(Error):
119 """File already exists."""
120
121
maruel12e30012015-10-09 11:55:35 -0700122def file_read(path, chunk_size=isolated_format.DISK_FILE_CHUNK, offset=0):
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -0800123 """Yields file content in chunks of |chunk_size| starting from |offset|."""
maruel12e30012015-10-09 11:55:35 -0700124 with fs.open(path, 'rb') as f:
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -0800125 if offset:
126 f.seek(offset)
maruel@chromium.org8750e4b2013-09-18 02:37:57 +0000127 while True:
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000128 data = f.read(chunk_size)
maruel@chromium.org8750e4b2013-09-18 02:37:57 +0000129 if not data:
130 break
131 yield data
132
133
maruel12e30012015-10-09 11:55:35 -0700134def file_write(path, content_generator):
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000135 """Writes file content as generated by content_generator.
136
maruel@chromium.org8750e4b2013-09-18 02:37:57 +0000137 Creates the intermediary directory as needed.
138
139 Returns the number of bytes written.
140
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000141 Meant to be mocked out in unit tests.
142 """
nodire5028a92016-04-29 14:38:21 -0700143 file_path.ensure_tree(os.path.dirname(path))
maruel@chromium.org8750e4b2013-09-18 02:37:57 +0000144 total = 0
maruel12e30012015-10-09 11:55:35 -0700145 with fs.open(path, 'wb') as f:
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000146 for d in content_generator:
maruel@chromium.org8750e4b2013-09-18 02:37:57 +0000147 total += len(d)
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000148 f.write(d)
maruel@chromium.org8750e4b2013-09-18 02:37:57 +0000149 return total
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000150
151
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000152def zip_compress(content_generator, level=7):
153 """Reads chunks from |content_generator| and yields zip compressed chunks."""
154 compressor = zlib.compressobj(level)
155 for chunk in content_generator:
156 compressed = compressor.compress(chunk)
157 if compressed:
158 yield compressed
159 tail = compressor.flush(zlib.Z_FINISH)
160 if tail:
161 yield tail
162
163
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -0400164def zip_decompress(
165 content_generator, chunk_size=isolated_format.DISK_FILE_CHUNK):
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000166 """Reads zipped data from |content_generator| and yields decompressed data.
167
168 Decompresses data in small chunks (no larger than |chunk_size|) so that
169 zip bomb file doesn't cause zlib to preallocate huge amount of memory.
170
171 Raises IOError if data is corrupted or incomplete.
172 """
173 decompressor = zlib.decompressobj()
174 compressed_size = 0
175 try:
176 for chunk in content_generator:
177 compressed_size += len(chunk)
178 data = decompressor.decompress(chunk, chunk_size)
179 if data:
180 yield data
181 while decompressor.unconsumed_tail:
182 data = decompressor.decompress(decompressor.unconsumed_tail, chunk_size)
183 if data:
184 yield data
185 tail = decompressor.flush()
186 if tail:
187 yield tail
188 except zlib.error as e:
189 raise IOError(
190 'Corrupted zip stream (read %d bytes) - %s' % (compressed_size, e))
191 # Ensure all data was read and decompressed.
192 if decompressor.unused_data or decompressor.unconsumed_tail:
193 raise IOError('Not all data was decompressed')
194
195
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000196def get_zip_compression_level(filename):
197 """Given a filename calculates the ideal zip compression level to use."""
198 file_ext = os.path.splitext(filename)[1].lower()
199 # TODO(csharp): Profile to find what compression level works best.
200 return 0 if file_ext in ALREADY_COMPRESSED_TYPES else 7
201
202
maruel@chromium.orgaf254852013-09-17 17:48:14 +0000203def create_directories(base_directory, files):
204 """Creates the directory structure needed by the given list of files."""
205 logging.debug('create_directories(%s, %d)', base_directory, len(files))
206 # Creates the tree of directories to create.
207 directories = set(os.path.dirname(f) for f in files)
208 for item in list(directories):
209 while item:
210 directories.add(item)
211 item = os.path.dirname(item)
212 for d in sorted(directories):
213 if d:
maruel12e30012015-10-09 11:55:35 -0700214 fs.mkdir(os.path.join(base_directory, d))
maruel@chromium.orgaf254852013-09-17 17:48:14 +0000215
216
Marc-Antoine Ruelccafe0e2013-11-08 16:15:36 -0500217def create_symlinks(base_directory, files):
218 """Creates any symlinks needed by the given set of files."""
maruel@chromium.orgaf254852013-09-17 17:48:14 +0000219 for filepath, properties in files:
220 if 'l' not in properties:
221 continue
222 if sys.platform == 'win32':
Marc-Antoine Ruelccafe0e2013-11-08 16:15:36 -0500223 # TODO(maruel): Create symlink via the win32 api.
maruel@chromium.orgaf254852013-09-17 17:48:14 +0000224 logging.warning('Ignoring symlink %s', filepath)
225 continue
226 outfile = os.path.join(base_directory, filepath)
nodir90bc8dc2016-06-15 13:35:21 -0700227 try:
228 os.symlink(properties['l'], outfile) # pylint: disable=E1101
229 except OSError as e:
230 if e.errno == errno.EEXIST:
231 raise AlreadyExists('File %s already exists.' % outfile)
232 raise
maruel@chromium.orgaf254852013-09-17 17:48:14 +0000233
234
maruel12e30012015-10-09 11:55:35 -0700235def is_valid_file(path, size):
maruel@chromium.orgdedbf492013-09-12 20:42:11 +0000236 """Determines if the given files appears valid.
237
238 Currently it just checks the file's size.
239 """
Vadim Shtayura3148e072014-09-02 18:51:52 -0700240 if size == UNKNOWN_FILE_SIZE:
maruel12e30012015-10-09 11:55:35 -0700241 return fs.isfile(path)
242 actual_size = fs.stat(path).st_size
maruel@chromium.orgdedbf492013-09-12 20:42:11 +0000243 if size != actual_size:
244 logging.warning(
245 'Found invalid item %s; %d != %d',
maruel12e30012015-10-09 11:55:35 -0700246 os.path.basename(path), actual_size, size)
maruel@chromium.orgdedbf492013-09-12 20:42:11 +0000247 return False
248 return True
249
250
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000251class Item(object):
252 """An item to push to Storage.
253
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800254 Its digest and size may be provided in advance, if known. Otherwise they will
255 be derived from content(). If digest is provided, it MUST correspond to
256 hash algorithm used by Storage.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000257
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800258 When used with Storage, Item starts its life in a main thread, travels
259 to 'contains' thread, then to 'push' thread and then finally back to
260 the main thread. It is never used concurrently from multiple threads.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000261 """
262
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800263 def __init__(self, digest=None, size=None, high_priority=False):
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000264 self.digest = digest
265 self.size = size
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800266 self.high_priority = high_priority
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000267 self.compression_level = 6
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000268
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800269 def content(self):
270 """Iterable with content of this item as byte string (str) chunks."""
271 raise NotImplementedError()
272
273 def prepare(self, hash_algo):
274 """Ensures self.digest and self.size are set.
275
276 Uses content() as a source of data to calculate them. Does nothing if digest
277 and size is already known.
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000278
279 Arguments:
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800280 hash_algo: hash algorithm to use to calculate digest.
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000281 """
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800282 if self.digest is None or self.size is None:
283 digest = hash_algo()
284 total = 0
285 for chunk in self.content():
286 digest.update(chunk)
287 total += len(chunk)
288 self.digest = digest.hexdigest()
289 self.size = total
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000290
291
292class FileItem(Item):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800293 """A file to push to Storage.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000294
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800295 Its digest and size may be provided in advance, if known. Otherwise they will
296 be derived from the file content.
297 """
298
299 def __init__(self, path, digest=None, size=None, high_priority=False):
300 super(FileItem, self).__init__(
301 digest,
maruel12e30012015-10-09 11:55:35 -0700302 size if size is not None else fs.stat(path).st_size,
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800303 high_priority)
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000304 self.path = path
305 self.compression_level = get_zip_compression_level(path)
306
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800307 def content(self):
308 return file_read(self.path)
maruel@chromium.orgc6f90062012-11-07 18:32:22 +0000309
310
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000311class BufferItem(Item):
312 """A byte buffer to push to Storage."""
313
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800314 def __init__(self, buf, high_priority=False):
315 super(BufferItem, self).__init__(None, len(buf), high_priority)
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000316 self.buffer = buf
317
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800318 def content(self):
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000319 return [self.buffer]
320
321
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000322class Storage(object):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800323 """Efficiently downloads or uploads large set of files via StorageApi.
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000324
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800325 Implements compression support, parallel 'contains' checks, parallel uploads
326 and more.
327
328 Works only within single namespace (and thus hashing algorithm and compression
329 scheme are fixed).
330
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400331 Spawns multiple internal threads. Thread safe, but not fork safe. Modifies
332 signal handlers table to handle Ctrl+C.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800333 """
334
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700335 def __init__(self, storage_api):
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000336 self._storage_api = storage_api
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -0400337 self._use_zip = isolated_format.is_namespace_with_compression(
338 storage_api.namespace)
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -0400339 self._hash_algo = isolated_format.get_hash_algo(storage_api.namespace)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000340 self._cpu_thread_pool = None
341 self._net_thread_pool = None
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400342 self._aborted = False
343 self._prev_sig_handlers = {}
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000344
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000345 @property
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700346 def hash_algo(self):
347 """Hashing algorithm used to name files in storage based on their content.
348
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -0400349 Defined by |namespace|. See also isolated_format.get_hash_algo().
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700350 """
351 return self._hash_algo
352
353 @property
354 def location(self):
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -0500355 """URL of the backing store that this class is using."""
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700356 return self._storage_api.location
357
358 @property
359 def namespace(self):
360 """Isolate namespace used by this storage.
361
362 Indirectly defines hashing scheme and compression method used.
363 """
364 return self._storage_api.namespace
365
366 @property
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000367 def cpu_thread_pool(self):
368 """ThreadPool for CPU-bound tasks like zipping."""
369 if self._cpu_thread_pool is None:
Marc-Antoine Ruelbdad1182015-02-06 16:04:35 -0500370 threads = max(threading_utils.num_processors(), 2)
371 if sys.maxsize <= 2L**32:
372 # On 32 bits userland, do not try to use more than 16 threads.
373 threads = min(threads, 16)
374 self._cpu_thread_pool = threading_utils.ThreadPool(2, threads, 0, 'zip')
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000375 return self._cpu_thread_pool
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000376
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000377 @property
378 def net_thread_pool(self):
379 """AutoRetryThreadPool for IO-bound tasks, retries IOError."""
380 if self._net_thread_pool is None:
Vadim Shtayura3148e072014-09-02 18:51:52 -0700381 self._net_thread_pool = threading_utils.IOAutoRetryThreadPool()
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000382 return self._net_thread_pool
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000383
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000384 def close(self):
385 """Waits for all pending tasks to finish."""
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400386 logging.info('Waiting for all threads to die...')
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000387 if self._cpu_thread_pool:
388 self._cpu_thread_pool.join()
389 self._cpu_thread_pool.close()
390 self._cpu_thread_pool = None
391 if self._net_thread_pool:
392 self._net_thread_pool.join()
393 self._net_thread_pool.close()
394 self._net_thread_pool = None
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400395 logging.info('Done.')
396
397 def abort(self):
398 """Cancels any pending or future operations."""
399 # This is not strictly theadsafe, but in the worst case the logging message
400 # will be printed twice. Not a big deal. In other places it is assumed that
401 # unprotected reads and writes to _aborted are serializable (it is true
402 # for python) and thus no locking is used.
403 if not self._aborted:
404 logging.warning('Aborting... It can take a while.')
405 self._aborted = True
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000406
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000407 def __enter__(self):
408 """Context manager interface."""
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400409 assert not self._prev_sig_handlers, self._prev_sig_handlers
410 for s in (signal.SIGINT, signal.SIGTERM):
411 self._prev_sig_handlers[s] = signal.signal(s, lambda *_args: self.abort())
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000412 return self
413
414 def __exit__(self, _exc_type, _exc_value, _traceback):
415 """Context manager interface."""
416 self.close()
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400417 while self._prev_sig_handlers:
418 s, h = self._prev_sig_handlers.popitem()
419 signal.signal(s, h)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000420 return False
421
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000422 def upload_items(self, items):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800423 """Uploads a bunch of items to the isolate server.
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000424
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800425 It figures out what items are missing from the server and uploads only them.
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000426
427 Arguments:
428 items: list of Item instances that represents data to upload.
429
430 Returns:
431 List of items that were uploaded. All other items are already there.
432 """
Vadim Shtayuraea38c572014-10-06 16:57:16 -0700433 logging.info('upload_items(items=%d)', len(items))
434
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800435 # Ensure all digests are calculated.
436 for item in items:
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700437 item.prepare(self._hash_algo)
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800438
vadimsh@chromium.org672cd2b2013-10-08 17:49:33 +0000439 # For each digest keep only first Item that matches it. All other items
440 # are just indistinguishable copies from the point of view of isolate
441 # server (it doesn't care about paths at all, only content and digests).
442 seen = {}
443 duplicates = 0
444 for item in items:
445 if seen.setdefault(item.digest, item) is not item:
446 duplicates += 1
447 items = seen.values()
448 if duplicates:
Vadim Shtayuraea38c572014-10-06 16:57:16 -0700449 logging.info('Skipped %d files with duplicated content', duplicates)
vadimsh@chromium.org672cd2b2013-10-08 17:49:33 +0000450
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000451 # Enqueue all upload tasks.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000452 missing = set()
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000453 uploaded = []
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800454 channel = threading_utils.TaskChannel()
455 for missing_item, push_state in self.get_missing_items(items):
456 missing.add(missing_item)
457 self.async_push(channel, missing_item, push_state)
458
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000459 # No need to spawn deadlock detector thread if there's nothing to upload.
460 if missing:
Vadim Shtayura3148e072014-09-02 18:51:52 -0700461 with threading_utils.DeadlockDetector(DEADLOCK_TIMEOUT) as detector:
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000462 # Wait for all started uploads to finish.
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000463 while len(uploaded) != len(missing):
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000464 detector.ping()
465 item = channel.pull()
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000466 uploaded.append(item)
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000467 logging.debug(
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000468 'Uploaded %d / %d: %s', len(uploaded), len(missing), item.digest)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000469 logging.info('All files are uploaded')
470
471 # Print stats.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000472 total = len(items)
473 total_size = sum(f.size for f in items)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000474 logging.info(
475 'Total: %6d, %9.1fkb',
476 total,
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000477 total_size / 1024.)
478 cache_hit = set(items) - missing
479 cache_hit_size = sum(f.size for f in cache_hit)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000480 logging.info(
481 'cache hit: %6d, %9.1fkb, %6.2f%% files, %6.2f%% size',
482 len(cache_hit),
483 cache_hit_size / 1024.,
484 len(cache_hit) * 100. / total,
485 cache_hit_size * 100. / total_size if total_size else 0)
486 cache_miss = missing
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000487 cache_miss_size = sum(f.size for f in cache_miss)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000488 logging.info(
489 'cache miss: %6d, %9.1fkb, %6.2f%% files, %6.2f%% size',
490 len(cache_miss),
491 cache_miss_size / 1024.,
492 len(cache_miss) * 100. / total,
493 cache_miss_size * 100. / total_size if total_size else 0)
494
vadimsh@chromium.orgf24e5c32013-10-11 21:16:21 +0000495 return uploaded
496
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800497 def async_push(self, channel, item, push_state):
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000498 """Starts asynchronous push to the server in a parallel thread.
499
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800500 Can be used only after |item| was checked for presence on a server with
501 'get_missing_items' call. 'get_missing_items' returns |push_state| object
502 that contains storage specific information describing how to upload
503 the item (for example in case of cloud storage, it is signed upload URLs).
504
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000505 Arguments:
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000506 channel: TaskChannel that receives back |item| when upload ends.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000507 item: item to upload as instance of Item class.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800508 push_state: push state returned by 'get_missing_items' call for |item|.
509
510 Returns:
511 None, but |channel| later receives back |item| when upload ends.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000512 """
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800513 # Thread pool task priority.
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -0400514 priority = (
Vadim Shtayura3148e072014-09-02 18:51:52 -0700515 threading_utils.PRIORITY_HIGH if item.high_priority
516 else threading_utils.PRIORITY_MED)
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800517
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000518 def push(content):
Marc-Antoine Ruel095a8be2014-03-21 14:58:19 -0400519 """Pushes an Item and returns it to |channel|."""
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400520 if self._aborted:
521 raise Aborted()
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700522 item.prepare(self._hash_algo)
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800523 self._storage_api.push(item, push_state, content)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000524 return item
525
526 # If zipping is not required, just start a push task.
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700527 if not self._use_zip:
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800528 self.net_thread_pool.add_task_with_channel(
529 channel, priority, push, item.content())
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000530 return
531
532 # If zipping is enabled, zip in a separate thread.
533 def zip_and_push():
534 # TODO(vadimsh): Implement streaming uploads. Before it's done, assemble
535 # content right here. It will block until all file is zipped.
536 try:
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400537 if self._aborted:
538 raise Aborted()
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800539 stream = zip_compress(item.content(), item.compression_level)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000540 data = ''.join(stream)
541 except Exception as exc:
542 logging.error('Failed to zip \'%s\': %s', item, exc)
Vadim Shtayura0ffc4092013-11-20 17:49:52 -0800543 channel.send_exception()
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000544 return
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000545 self.net_thread_pool.add_task_with_channel(
546 channel, priority, push, [data])
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000547 self.cpu_thread_pool.add_task(priority, zip_and_push)
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000548
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800549 def push(self, item, push_state):
550 """Synchronously pushes a single item to the server.
551
552 If you need to push many items at once, consider using 'upload_items' or
553 'async_push' with instance of TaskChannel.
554
555 Arguments:
556 item: item to upload as instance of Item class.
557 push_state: push state returned by 'get_missing_items' call for |item|.
558
559 Returns:
560 Pushed item (same object as |item|).
561 """
562 channel = threading_utils.TaskChannel()
Vadim Shtayura3148e072014-09-02 18:51:52 -0700563 with threading_utils.DeadlockDetector(DEADLOCK_TIMEOUT):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800564 self.async_push(channel, item, push_state)
565 pushed = channel.pull()
566 assert pushed is item
567 return item
568
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000569 def async_fetch(self, channel, priority, digest, size, sink):
570 """Starts asynchronous fetch from the server in a parallel thread.
571
572 Arguments:
573 channel: TaskChannel that receives back |digest| when download ends.
574 priority: thread pool task priority for the fetch.
575 digest: hex digest of an item to download.
576 size: expected size of the item (after decompression).
577 sink: function that will be called as sink(generator).
578 """
579 def fetch():
580 try:
581 # Prepare reading pipeline.
582 stream = self._storage_api.fetch(digest)
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700583 if self._use_zip:
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -0400584 stream = zip_decompress(stream, isolated_format.DISK_FILE_CHUNK)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000585 # Run |stream| through verifier that will assert its size.
586 verifier = FetchStreamVerifier(stream, size)
587 # Verified stream goes to |sink|.
588 sink(verifier.run())
589 except Exception as err:
Vadim Shtayura0ffc4092013-11-20 17:49:52 -0800590 logging.error('Failed to fetch %s: %s', digest, err)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000591 raise
592 return digest
593
594 # Don't bother with zip_thread_pool for decompression. Decompression is
595 # really fast and most probably IO bound anyway.
596 self.net_thread_pool.add_task_with_channel(channel, priority, fetch)
597
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000598 def get_missing_items(self, items):
599 """Yields items that are missing from the server.
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000600
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000601 Issues multiple parallel queries via StorageApi's 'contains' method.
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000602
603 Arguments:
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000604 items: a list of Item objects to check.
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000605
606 Yields:
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800607 For each missing item it yields a pair (item, push_state), where:
608 * item - Item object that is missing (one of |items|).
609 * push_state - opaque object that contains storage specific information
610 describing how to upload the item (for example in case of cloud
611 storage, it is signed upload URLs). It can later be passed to
612 'async_push'.
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000613 """
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000614 channel = threading_utils.TaskChannel()
615 pending = 0
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800616
617 # Ensure all digests are calculated.
618 for item in items:
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700619 item.prepare(self._hash_algo)
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800620
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400621 def contains(batch):
622 if self._aborted:
623 raise Aborted()
624 return self._storage_api.contains(batch)
625
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000626 # Enqueue all requests.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800627 for batch in batch_items_for_check(items):
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -0400628 self.net_thread_pool.add_task_with_channel(
Vadim Shtayuraf9e401b2014-10-15 18:19:37 +0400629 channel, threading_utils.PRIORITY_HIGH, contains, batch)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000630 pending += 1
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800631
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000632 # Yield results as they come in.
633 for _ in xrange(pending):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800634 for missing_item, push_state in channel.pull().iteritems():
635 yield missing_item, push_state
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000636
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000637
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800638def batch_items_for_check(items):
639 """Splits list of items to check for existence on the server into batches.
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000640
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800641 Each batch corresponds to a single 'exists?' query to the server via a call
642 to StorageApi's 'contains' method.
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000643
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800644 Arguments:
645 items: a list of Item objects.
646
647 Yields:
648 Batches of items to query for existence in a single operation,
649 each batch is a list of Item objects.
650 """
651 batch_count = 0
652 batch_size_limit = ITEMS_PER_CONTAINS_QUERIES[0]
653 next_queries = []
654 for item in sorted(items, key=lambda x: x.size, reverse=True):
655 next_queries.append(item)
656 if len(next_queries) == batch_size_limit:
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000657 yield next_queries
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800658 next_queries = []
659 batch_count += 1
660 batch_size_limit = ITEMS_PER_CONTAINS_QUERIES[
661 min(batch_count, len(ITEMS_PER_CONTAINS_QUERIES) - 1)]
662 if next_queries:
663 yield next_queries
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000664
665
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000666class FetchQueue(object):
667 """Fetches items from Storage and places them into LocalCache.
668
669 It manages multiple concurrent fetch operations. Acts as a bridge between
670 Storage and LocalCache so that Storage and LocalCache don't depend on each
671 other at all.
672 """
673
674 def __init__(self, storage, cache):
675 self.storage = storage
676 self.cache = cache
677 self._channel = threading_utils.TaskChannel()
678 self._pending = set()
679 self._accessed = set()
680 self._fetched = cache.cached_set()
681
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -0400682 def add(
Vadim Shtayura3148e072014-09-02 18:51:52 -0700683 self,
684 digest,
685 size=UNKNOWN_FILE_SIZE,
686 priority=threading_utils.PRIORITY_MED):
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000687 """Starts asynchronous fetch of item |digest|."""
688 # Fetching it now?
689 if digest in self._pending:
690 return
691
692 # Mark this file as in use, verify_all_cached will later ensure it is still
693 # in cache.
694 self._accessed.add(digest)
695
696 # Already fetched? Notify cache to update item's LRU position.
697 if digest in self._fetched:
698 # 'touch' returns True if item is in cache and not corrupted.
699 if self.cache.touch(digest, size):
700 return
701 # Item is corrupted, remove it from cache and fetch it again.
702 self._fetched.remove(digest)
703 self.cache.evict(digest)
704
705 # TODO(maruel): It should look at the free disk space, the current cache
706 # size and the size of the new item on every new item:
707 # - Trim the cache as more entries are listed when free disk space is low,
708 # otherwise if the amount of data downloaded during the run > free disk
709 # space, it'll crash.
710 # - Make sure there's enough free disk space to fit all dependencies of
711 # this run! If not, abort early.
712
713 # Start fetching.
714 self._pending.add(digest)
715 self.storage.async_fetch(
716 self._channel, priority, digest, size,
717 functools.partial(self.cache.write, digest))
718
719 def wait(self, digests):
720 """Starts a loop that waits for at least one of |digests| to be retrieved.
721
722 Returns the first digest retrieved.
723 """
724 # Flush any already fetched items.
725 for digest in digests:
726 if digest in self._fetched:
727 return digest
728
729 # Ensure all requested items are being fetched now.
730 assert all(digest in self._pending for digest in digests), (
731 digests, self._pending)
732
733 # Wait for some requested item to finish fetching.
734 while self._pending:
735 digest = self._channel.pull()
736 self._pending.remove(digest)
737 self._fetched.add(digest)
738 if digest in digests:
739 return digest
740
741 # Should never reach this point due to assert above.
742 raise RuntimeError('Impossible state')
743
744 def inject_local_file(self, path, algo):
745 """Adds local file to the cache as if it was fetched from storage."""
maruel12e30012015-10-09 11:55:35 -0700746 with fs.open(path, 'rb') as f:
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000747 data = f.read()
748 digest = algo(data).hexdigest()
749 self.cache.write(digest, [data])
750 self._fetched.add(digest)
751 return digest
752
753 @property
754 def pending_count(self):
755 """Returns number of items to be fetched."""
756 return len(self._pending)
757
758 def verify_all_cached(self):
759 """True if all accessed items are in cache."""
760 return self._accessed.issubset(self.cache.cached_set())
761
762
763class FetchStreamVerifier(object):
764 """Verifies that fetched file is valid before passing it to the LocalCache."""
765
766 def __init__(self, stream, expected_size):
Marc-Antoine Rueldf4976d2015-04-15 19:56:21 -0400767 assert stream is not None
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000768 self.stream = stream
769 self.expected_size = expected_size
770 self.current_size = 0
771
772 def run(self):
773 """Generator that yields same items as |stream|.
774
775 Verifies |stream| is complete before yielding a last chunk to consumer.
776
777 Also wraps IOError produced by consumer into MappingError exceptions since
778 otherwise Storage will retry fetch on unrelated local cache errors.
779 """
780 # Read one chunk ahead, keep it in |stored|.
781 # That way a complete stream can be verified before pushing last chunk
782 # to consumer.
783 stored = None
784 for chunk in self.stream:
785 assert chunk is not None
786 if stored is not None:
787 self._inspect_chunk(stored, is_last=False)
788 try:
789 yield stored
790 except IOError as exc:
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -0400791 raise isolated_format.MappingError(
792 'Failed to store an item in cache: %s' % exc)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000793 stored = chunk
794 if stored is not None:
795 self._inspect_chunk(stored, is_last=True)
796 try:
797 yield stored
798 except IOError as exc:
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -0400799 raise isolated_format.MappingError(
800 'Failed to store an item in cache: %s' % exc)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000801
802 def _inspect_chunk(self, chunk, is_last):
803 """Called for each fetched chunk before passing it to consumer."""
804 self.current_size += len(chunk)
Marc-Antoine Ruel1e7658c2014-08-28 19:46:39 -0400805 if (is_last and
Vadim Shtayura3148e072014-09-02 18:51:52 -0700806 (self.expected_size != UNKNOWN_FILE_SIZE) and
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000807 (self.expected_size != self.current_size)):
808 raise IOError('Incorrect file size: expected %d, got %d' % (
809 self.expected_size, self.current_size))
810
811
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000812class StorageApi(object):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800813 """Interface for classes that implement low-level storage operations.
814
815 StorageApi is oblivious of compression and hashing scheme used. This details
816 are handled in higher level Storage class.
817
818 Clients should generally not use StorageApi directly. Storage class is
819 preferred since it implements compression and upload optimizations.
820 """
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000821
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700822 @property
823 def location(self):
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -0500824 """URL of the backing store that this class is using."""
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700825 raise NotImplementedError()
826
827 @property
828 def namespace(self):
829 """Isolate namespace used by this storage.
830
831 Indirectly defines hashing scheme and compression method used.
832 """
833 raise NotImplementedError()
834
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -0800835 def fetch(self, digest, offset=0):
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000836 """Fetches an object and yields its content.
837
838 Arguments:
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000839 digest: hash digest of item to download.
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -0800840 offset: offset (in bytes) from the start of the file to resume fetch from.
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000841
842 Yields:
843 Chunks of downloaded item (as str objects).
844 """
845 raise NotImplementedError()
846
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800847 def push(self, item, push_state, content=None):
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000848 """Uploads an |item| with content generated by |content| generator.
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000849
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800850 |item| MUST go through 'contains' call to get |push_state| before it can
851 be pushed to the storage.
852
853 To be clear, here is one possible usage:
854 all_items = [... all items to push as Item subclasses ...]
855 for missing_item, push_state in storage_api.contains(all_items).items():
856 storage_api.push(missing_item, push_state)
857
858 When pushing to a namespace with compression, data that should be pushed
859 and data provided by the item is not the same. In that case |content| is
860 not None and it yields chunks of compressed data (using item.content() as
861 a source of original uncompressed data). This is implemented by Storage
862 class.
863
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000864 Arguments:
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000865 item: Item object that holds information about an item being pushed.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800866 push_state: push state object as returned by 'contains' call.
867 content: a generator that yields chunks to push, item.content() if None.
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000868
869 Returns:
870 None.
871 """
872 raise NotImplementedError()
873
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000874 def contains(self, items):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800875 """Checks for |items| on the server, prepares missing ones for upload.
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000876
877 Arguments:
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800878 items: list of Item objects to check for presence.
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000879
880 Returns:
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800881 A dict missing Item -> opaque push state object to be passed to 'push'.
882 See doc string for 'push'.
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +0000883 """
884 raise NotImplementedError()
885
886
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800887class _IsolateServerPushState(object):
888 """Per-item state passed from IsolateServer.contains to IsolateServer.push.
Mike Frysinger27f03da2014-02-12 16:47:01 -0500889
890 Note this needs to be a global class to support pickling.
891 """
892
Cory Massarocc19c8c2015-03-10 13:35:11 -0700893 def __init__(self, preupload_status, size):
894 self.preupload_status = preupload_status
895 gs_upload_url = preupload_status.get('gs_upload_url') or None
896 if gs_upload_url:
897 self.upload_url = gs_upload_url
898 self.finalize_url = '_ah/api/isolateservice/v1/finalize_gs_upload'
899 else:
900 self.upload_url = '_ah/api/isolateservice/v1/store_inline'
901 self.finalize_url = None
Mike Frysinger27f03da2014-02-12 16:47:01 -0500902 self.uploaded = False
903 self.finalized = False
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -0500904 self.size = size
Mike Frysinger27f03da2014-02-12 16:47:01 -0500905
906
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000907class IsolateServer(StorageApi):
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000908 """StorageApi implementation that downloads and uploads to Isolate Server.
909
910 It uploads and downloads directly from Google Storage whenever appropriate.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800911 Works only within single namespace.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000912 """
913
maruel@chromium.org3e42ce82013-09-12 18:36:59 +0000914 def __init__(self, base_url, namespace):
vadimsh@chromium.org35122be2013-09-19 02:48:00 +0000915 super(IsolateServer, self).__init__()
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -0500916 assert file_path.is_url(base_url), base_url
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700917 self._base_url = base_url.rstrip('/')
918 self._namespace = namespace
Cory Massarocc19c8c2015-03-10 13:35:11 -0700919 self._namespace_dict = {
920 'compression': 'flate' if namespace.endswith(
921 ('-gzip', '-flate')) else '',
922 'digest_hash': 'sha-1',
923 'namespace': namespace,
924 }
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000925 self._lock = threading.Lock()
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000926 self._server_caps = None
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -0500927 self._memory_use = 0
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000928
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000929 @property
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000930 def _server_capabilities(self):
Cory Massarocc19c8c2015-03-10 13:35:11 -0700931 """Gets server details.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000932
933 Returns:
Cory Massarocc19c8c2015-03-10 13:35:11 -0700934 Server capabilities dictionary as returned by /server_details endpoint.
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000935 """
maruel@chromium.org3e42ce82013-09-12 18:36:59 +0000936 # TODO(maruel): Make this request much earlier asynchronously while the
937 # files are being enumerated.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -0800938
939 # TODO(vadimsh): Put |namespace| in the URL so that server can apply
940 # namespace-level ACLs to this call.
Cory Massarocc19c8c2015-03-10 13:35:11 -0700941
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000942 with self._lock:
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000943 if self._server_caps is None:
Cory Massarocc19c8c2015-03-10 13:35:11 -0700944 self._server_caps = net.url_read_json(
945 url='%s/_ah/api/isolateservice/v1/server_details' % self._base_url,
946 data={})
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +0000947 return self._server_caps
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +0000948
Vadim Shtayurae0ab1902014-04-29 10:55:27 -0700949 @property
950 def location(self):
951 return self._base_url
952
953 @property
954 def namespace(self):
955 return self._namespace
956
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -0800957 def fetch(self, digest, offset=0):
Cory Massarocc19c8c2015-03-10 13:35:11 -0700958 assert offset >= 0
959 source_url = '%s/_ah/api/isolateservice/v1/retrieve' % (
960 self._base_url)
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -0800961 logging.debug('download_file(%s, %d)', source_url, offset)
Cory Massarocc19c8c2015-03-10 13:35:11 -0700962 response = self.do_fetch(source_url, digest, offset)
maruel@chromium.orge45728d2013-09-16 23:23:22 +0000963
Cory Massarocc19c8c2015-03-10 13:35:11 -0700964 if not response:
maruele154f9c2015-09-14 11:03:15 -0700965 raise IOError(
966 'Attempted to fetch from %s; no data exist: %s / %s.' % (
967 source_url, self._namespace, digest))
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -0800968
Cory Massarocc19c8c2015-03-10 13:35:11 -0700969 # for DB uploads
970 content = response.get('content')
971 if content is not None:
maruel863ac262016-03-17 11:00:37 -0700972 yield base64.b64decode(content)
973 return
Cory Massarocc19c8c2015-03-10 13:35:11 -0700974
975 # for GS entities
976 connection = net.url_open(response['url'])
maruelf5574752015-09-17 13:40:27 -0700977 if not connection:
978 raise IOError('Failed to download %s / %s' % (self._namespace, digest))
Cory Massarocc19c8c2015-03-10 13:35:11 -0700979
980 # If |offset|, verify server respects it by checking Content-Range.
Vadim Shtayuraf0cb97a2013-12-05 13:57:49 -0800981 if offset:
982 content_range = connection.get_header('Content-Range')
983 if not content_range:
984 raise IOError('Missing Content-Range header')
985
986 # 'Content-Range' format is 'bytes <offset>-<last_byte_index>/<size>'.
987 # According to a spec, <size> can be '*' meaning "Total size of the file
988 # is not known in advance".
989 try:
990 match = re.match(r'bytes (\d+)-(\d+)/(\d+|\*)', content_range)
991 if not match:
992 raise ValueError()
993 content_offset = int(match.group(1))
994 last_byte_index = int(match.group(2))
995 size = None if match.group(3) == '*' else int(match.group(3))
996 except ValueError:
997 raise IOError('Invalid Content-Range header: %s' % content_range)
998
999 # Ensure returned offset equals requested one.
1000 if offset != content_offset:
1001 raise IOError('Expecting offset %d, got %d (Content-Range is %s)' % (
1002 offset, content_offset, content_range))
1003
1004 # Ensure entire tail of the file is returned.
1005 if size is not None and last_byte_index + 1 != size:
1006 raise IOError('Incomplete response. Content-Range: %s' % content_range)
1007
maruel863ac262016-03-17 11:00:37 -07001008 for data in connection.iter_content(NET_IO_FILE_CHUNK):
1009 yield data
maruel@chromium.orge45728d2013-09-16 23:23:22 +00001010
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001011 def push(self, item, push_state, content=None):
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001012 assert isinstance(item, Item)
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001013 assert item.digest is not None
1014 assert item.size is not None
1015 assert isinstance(push_state, _IsolateServerPushState)
1016 assert not push_state.finalized
1017
1018 # Default to item.content().
1019 content = item.content() if content is None else content
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -05001020 logging.info('Push state size: %d', push_state.size)
1021 if isinstance(content, (basestring, list)):
1022 # Memory is already used, too late.
1023 with self._lock:
1024 self._memory_use += push_state.size
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +00001025 else:
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -05001026 # TODO(vadimsh): Do not read from |content| generator when retrying push.
1027 # If |content| is indeed a generator, it can not be re-winded back to the
1028 # beginning of the stream. A retry will find it exhausted. A possible
1029 # solution is to wrap |content| generator with some sort of caching
1030 # restartable generator. It should be done alongside streaming support
1031 # implementation.
1032 #
1033 # In theory, we should keep the generator, so that it is not serialized in
1034 # memory. Sadly net.HttpService.request() requires the body to be
1035 # serialized.
1036 assert isinstance(content, types.GeneratorType), repr(content)
1037 slept = False
1038 # HACK HACK HACK. Please forgive me for my sins but OMG, it works!
Marc-Antoine Ruele6677c82015-02-05 14:54:22 -05001039 # One byte less than 512mb. This is to cope with incompressible content.
1040 max_size = int(sys.maxsize * 0.25)
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -05001041 while True:
1042 with self._lock:
1043 # This is due to 32 bits python when uploading very large files. The
1044 # problem is that it's comparing uncompressed sizes, while we care
1045 # about compressed sizes since it's what is serialized in memory.
1046 # The first check assumes large files are compressible and that by
1047 # throttling one upload at once, we can survive. Otherwise, kaboom.
1048 memory_use = self._memory_use
1049 if ((push_state.size >= max_size and not memory_use) or
1050 (memory_use + push_state.size <= max_size)):
1051 self._memory_use += push_state.size
1052 memory_use = self._memory_use
1053 break
1054 time.sleep(0.1)
1055 slept = True
1056 if slept:
1057 logging.info('Unblocked: %d %d', memory_use, push_state.size)
vadimsh@chromium.org7cdf1c02013-09-25 00:24:16 +00001058
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -05001059 try:
1060 # This push operation may be a retry after failed finalization call below,
1061 # no need to reupload contents in that case.
1062 if not push_state.uploaded:
1063 # PUT file to |upload_url|.
Cory Massarocc19c8c2015-03-10 13:35:11 -07001064 success = self.do_push(push_state, content)
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -05001065 if not success:
Cory Massarocc19c8c2015-03-10 13:35:11 -07001066 raise IOError('Failed to upload file with hash %s to URL %s' % (
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -05001067 item.digest, push_state.upload_url))
1068 push_state.uploaded = True
1069 else:
1070 logging.info(
1071 'A file %s already uploaded, retrying finalization only',
1072 item.digest)
1073
1074 # Optionally notify the server that it's done.
1075 if push_state.finalize_url:
1076 # TODO(vadimsh): Calculate MD5 or CRC32C sum while uploading a file and
1077 # send it to isolated server. That way isolate server can verify that
1078 # the data safely reached Google Storage (GS provides MD5 and CRC32C of
1079 # stored files).
1080 # TODO(maruel): Fix the server to accept properly data={} so
1081 # url_read_json() can be used.
Cory Massarocc19c8c2015-03-10 13:35:11 -07001082 response = net.url_read_json(
1083 url='%s/%s' % (self._base_url, push_state.finalize_url),
1084 data={
1085 'upload_ticket': push_state.preupload_status['upload_ticket'],
1086 })
1087 if not response or not response['ok']:
1088 raise IOError('Failed to finalize file with hash %s.' % item.digest)
Marc-Antoine Ruele98dde92015-01-22 14:53:05 -05001089 push_state.finalized = True
1090 finally:
1091 with self._lock:
1092 self._memory_use -= push_state.size
maruel@chromium.orgd1e20c92013-09-17 20:54:26 +00001093
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001094 def contains(self, items):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001095 # Ensure all items were initialized with 'prepare' call. Storage does that.
1096 assert all(i.digest is not None and i.size is not None for i in items)
1097
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001098 # Request body is a json encoded list of dicts.
Cory Massarocc19c8c2015-03-10 13:35:11 -07001099 body = {
1100 'items': [
1101 {
1102 'digest': item.digest,
1103 'is_isolated': bool(item.high_priority),
1104 'size': item.size,
1105 } for item in items
1106 ],
1107 'namespace': self._namespace_dict,
1108 }
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001109
Cory Massarocc19c8c2015-03-10 13:35:11 -07001110 query_url = '%s/_ah/api/isolateservice/v1/preupload' % self._base_url
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001111
1112 # Response body is a list of push_urls (or null if file is already present).
Marc-Antoine Ruel0a620612014-08-13 15:47:07 -04001113 response = None
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001114 try:
Marc-Antoine Ruel0a620612014-08-13 15:47:07 -04001115 response = net.url_read_json(url=query_url, data=body)
1116 if response is None:
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -04001117 raise isolated_format.MappingError(
Cory Massarocc19c8c2015-03-10 13:35:11 -07001118 'Failed to execute preupload query')
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001119 except ValueError as err:
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -04001120 raise isolated_format.MappingError(
Marc-Antoine Ruel0a620612014-08-13 15:47:07 -04001121 'Invalid response from server: %s, body is %s' % (err, response))
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001122
1123 # Pick Items that are missing, attach _PushState to them.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001124 missing_items = {}
Cory Massarocc19c8c2015-03-10 13:35:11 -07001125 for preupload_status in response.get('items', []):
1126 assert 'upload_ticket' in preupload_status, (
1127 preupload_status, '/preupload did not generate an upload ticket')
1128 index = int(preupload_status['index'])
1129 missing_items[items[index]] = _IsolateServerPushState(
1130 preupload_status, items[index].size)
vadimsh@chromium.org35122be2013-09-19 02:48:00 +00001131 logging.info('Queried %d files, %d cache hit',
vadimsh@chromium.orgbcb966b2013-10-01 18:14:18 +00001132 len(items), len(items) - len(missing_items))
1133 return missing_items
maruel@chromium.orgc6f90062012-11-07 18:32:22 +00001134
Cory Massarocc19c8c2015-03-10 13:35:11 -07001135 def do_fetch(self, url, digest, offset):
Vadim Shtayura8623c272014-12-01 11:45:27 -08001136 """Fetches isolated data from the URL.
1137
1138 Used only for fetching files, not for API calls. Can be overridden in
1139 subclasses.
1140
1141 Args:
1142 url: URL to fetch the data from, can possibly return http redirect.
1143 offset: byte offset inside the file to start fetching from.
1144
1145 Returns:
1146 net.HttpResponse compatible object, with 'read' and 'get_header' calls.
1147 """
Cory Massarocc19c8c2015-03-10 13:35:11 -07001148 assert isinstance(offset, int)
1149 data = {
1150 'digest': digest.encode('utf-8'),
1151 'namespace': self._namespace_dict,
1152 'offset': offset,
1153 }
maruel0c25f4f2015-12-15 05:41:17 -08001154 # TODO(maruel): url + '?' + urllib.urlencode(data) once a HTTP GET endpoint
1155 # is added.
Cory Massarocc19c8c2015-03-10 13:35:11 -07001156 return net.url_read_json(
1157 url=url,
1158 data=data,
1159 read_timeout=DOWNLOAD_READ_TIMEOUT)
Vadim Shtayura8623c272014-12-01 11:45:27 -08001160
Cory Massarocc19c8c2015-03-10 13:35:11 -07001161 def do_push(self, push_state, content):
Vadim Shtayura8623c272014-12-01 11:45:27 -08001162 """Uploads isolated file to the URL.
1163
1164 Used only for storing files, not for API calls. Can be overridden in
1165 subclasses.
1166
1167 Args:
1168 url: URL to upload the data to.
Cory Massarocc19c8c2015-03-10 13:35:11 -07001169 push_state: an _IsolateServicePushState instance
1170 item: the original Item to be uploaded
Vadim Shtayura8623c272014-12-01 11:45:27 -08001171 content: an iterable that yields 'str' chunks.
Vadim Shtayura8623c272014-12-01 11:45:27 -08001172 """
1173 # A cheezy way to avoid memcpy of (possibly huge) file, until streaming
1174 # upload support is implemented.
1175 if isinstance(content, list) and len(content) == 1:
1176 content = content[0]
1177 else:
1178 content = ''.join(content)
Cory Massarocc19c8c2015-03-10 13:35:11 -07001179
1180 # DB upload
1181 if not push_state.finalize_url:
1182 url = '%s/%s' % (self._base_url, push_state.upload_url)
1183 content = base64.b64encode(content)
1184 data = {
1185 'upload_ticket': push_state.preupload_status['upload_ticket'],
1186 'content': content,
1187 }
1188 response = net.url_read_json(url=url, data=data)
1189 return response is not None and response['ok']
1190
1191 # upload to GS
1192 url = push_state.upload_url
Vadim Shtayura8623c272014-12-01 11:45:27 -08001193 response = net.url_read(
Cory Massarocc19c8c2015-03-10 13:35:11 -07001194 content_type='application/octet-stream',
1195 data=content,
1196 method='PUT',
tandriib44d54d2016-02-10 11:31:41 -08001197 headers={'Cache-Control': 'public, max-age=31536000'},
Cory Massarocc19c8c2015-03-10 13:35:11 -07001198 url=url)
Vadim Shtayura8623c272014-12-01 11:45:27 -08001199 return response is not None
1200
maruel@chromium.orgc6f90062012-11-07 18:32:22 +00001201
nodir445097b2016-06-03 22:50:26 -07001202class CacheMiss(Exception):
1203 """Raised when an item is not in cache."""
1204
1205 def __init__(self, digest):
1206 self.digest = digest
1207 super(CacheMiss, self).__init__(
1208 'Item with digest %r is not found in cache' % digest)
1209
1210
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001211class LocalCache(object):
1212 """Local cache that stores objects fetched via Storage.
1213
1214 It can be accessed concurrently from multiple threads, so it should protect
1215 its internal state with some lock.
1216 """
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -05001217 cache_dir = None
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001218
maruel064c0a32016-04-05 11:47:15 -07001219 def __init__(self):
1220 self._lock = threading_utils.LockWithAssert()
1221 # Profiling values.
1222 self._added = []
1223 self._initial_number_items = 0
1224 self._initial_size = 0
1225 self._evicted = []
1226 self._linked = []
1227
nodirbe642ff2016-06-09 15:51:51 -07001228 def __contains__(self, digest):
1229 raise NotImplementedError()
1230
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001231 def __enter__(self):
1232 """Context manager interface."""
1233 return self
1234
1235 def __exit__(self, _exc_type, _exec_value, _traceback):
1236 """Context manager interface."""
1237 return False
1238
maruel064c0a32016-04-05 11:47:15 -07001239 @property
1240 def added(self):
1241 return self._added[:]
1242
1243 @property
1244 def evicted(self):
1245 return self._evicted[:]
1246
1247 @property
1248 def initial_number_items(self):
1249 return self._initial_number_items
1250
1251 @property
1252 def initial_size(self):
1253 return self._initial_size
1254
1255 @property
1256 def linked(self):
1257 return self._linked[:]
1258
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001259 def cached_set(self):
1260 """Returns a set of all cached digests (always a new object)."""
1261 raise NotImplementedError()
1262
maruel36a963d2016-04-08 17:15:49 -07001263 def cleanup(self):
1264 """Deletes any corrupted item from the cache and trims it if necessary."""
1265 raise NotImplementedError()
1266
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001267 def touch(self, digest, size):
1268 """Ensures item is not corrupted and updates its LRU position.
1269
1270 Arguments:
1271 digest: hash digest of item to check.
1272 size: expected size of this item.
1273
1274 Returns:
1275 True if item is in cache and not corrupted.
1276 """
1277 raise NotImplementedError()
1278
1279 def evict(self, digest):
1280 """Removes item from cache if it's there."""
1281 raise NotImplementedError()
1282
1283 def read(self, digest):
1284 """Returns contents of the cached item as a single str."""
1285 raise NotImplementedError()
1286
1287 def write(self, digest, content):
maruel083fa552016-04-08 14:38:01 -07001288 """Reads data from |content| generator and stores it in cache.
1289
1290 Returns digest to simplify chaining.
1291 """
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001292 raise NotImplementedError()
1293
Marc-Antoine Ruelfb199cf2013-11-12 15:38:12 -05001294 def hardlink(self, digest, dest, file_mode):
1295 """Ensures file at |dest| has same content as cached |digest|.
1296
1297 If file_mode is provided, it is used to set the executable bit if
1298 applicable.
1299 """
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001300 raise NotImplementedError()
1301
1302
1303class MemoryCache(LocalCache):
1304 """LocalCache implementation that stores everything in memory."""
1305
Vadim Shtayurae3fbd102014-04-29 17:05:21 -07001306 def __init__(self, file_mode_mask=0500):
1307 """Args:
1308 file_mode_mask: bit mask to AND file mode with. Default value will make
1309 all mapped files to be read only.
1310 """
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001311 super(MemoryCache, self).__init__()
Vadim Shtayurae3fbd102014-04-29 17:05:21 -07001312 self._file_mode_mask = file_mode_mask
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001313 self._contents = {}
1314
nodirbe642ff2016-06-09 15:51:51 -07001315 def __contains__(self, digest):
1316 with self._lock:
1317 return digest in self._contents
1318
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001319 def cached_set(self):
1320 with self._lock:
1321 return set(self._contents)
1322
maruel36a963d2016-04-08 17:15:49 -07001323 def cleanup(self):
1324 pass
1325
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001326 def touch(self, digest, size):
1327 with self._lock:
1328 return digest in self._contents
1329
1330 def evict(self, digest):
1331 with self._lock:
maruel064c0a32016-04-05 11:47:15 -07001332 v = self._contents.pop(digest, None)
1333 if v is not None:
1334 self._evicted.add(v)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001335
1336 def read(self, digest):
1337 with self._lock:
nodir445097b2016-06-03 22:50:26 -07001338 try:
1339 return self._contents[digest]
1340 except KeyError:
1341 raise CacheMiss(digest)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001342
1343 def write(self, digest, content):
1344 # Assemble whole stream before taking the lock.
1345 data = ''.join(content)
1346 with self._lock:
1347 self._contents[digest] = data
maruel064c0a32016-04-05 11:47:15 -07001348 self._added.append(len(data))
maruel083fa552016-04-08 14:38:01 -07001349 return digest
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001350
Marc-Antoine Ruelfb199cf2013-11-12 15:38:12 -05001351 def hardlink(self, digest, dest, file_mode):
1352 """Since data is kept in memory, there is no filenode to hardlink."""
maruel064c0a32016-04-05 11:47:15 -07001353 data = self.read(digest)
1354 file_write(dest, [data])
Marc-Antoine Ruelfb199cf2013-11-12 15:38:12 -05001355 if file_mode is not None:
maruel12e30012015-10-09 11:55:35 -07001356 fs.chmod(dest, file_mode & self._file_mode_mask)
maruel064c0a32016-04-05 11:47:15 -07001357 with self._lock:
1358 self._linked.append(len(data))
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001359
1360
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001361class CachePolicies(object):
1362 def __init__(self, max_cache_size, min_free_space, max_items):
1363 """
1364 Arguments:
1365 - max_cache_size: Trim if the cache gets larger than this value. If 0, the
1366 cache is effectively a leak.
1367 - min_free_space: Trim if disk free space becomes lower than this value. If
1368 0, it unconditionally fill the disk.
1369 - max_items: Maximum number of items to keep in the cache. If 0, do not
1370 enforce a limit.
1371 """
1372 self.max_cache_size = max_cache_size
1373 self.min_free_space = min_free_space
1374 self.max_items = max_items
1375
1376
1377class DiskCache(LocalCache):
1378 """Stateful LRU cache in a flat hash table in a directory.
1379
1380 Saves its state as json file.
1381 """
maruel12e30012015-10-09 11:55:35 -07001382 STATE_FILE = u'state.json'
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001383
1384 def __init__(self, cache_dir, policies, hash_algo):
1385 """
1386 Arguments:
1387 cache_dir: directory where to place the cache.
1388 policies: cache retention policies.
1389 algo: hashing algorithm used.
1390 """
maruel064c0a32016-04-05 11:47:15 -07001391 # All protected methods (starting with '_') except _path should be called
1392 # with self._lock held.
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001393 super(DiskCache, self).__init__()
1394 self.cache_dir = cache_dir
1395 self.policies = policies
1396 self.hash_algo = hash_algo
1397 self.state_file = os.path.join(cache_dir, self.STATE_FILE)
maruel083fa552016-04-08 14:38:01 -07001398 # Items in a LRU lookup dict(digest: size).
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001399 self._lru = lru.LRUDict()
maruel083fa552016-04-08 14:38:01 -07001400 # Current cached free disk space. It is updated by self._trim().
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001401 self._free_disk = 0
maruel2e8d0f52016-07-16 07:51:29 -07001402 # The first item in the LRU cache that must not be evicted during this run
1403 # since it was referenced. All items more recent that _protected in the LRU
1404 # cache are also inherently protected. It could be a set() of all items
1405 # referenced but this increases memory usage without a use case.
1406 self._protected = None
maruel36a963d2016-04-08 17:15:49 -07001407 # Cleanup operations done by self._load(), if any.
1408 self._operations = []
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001409 with tools.Profiler('Setup'):
1410 with self._lock:
maruel083fa552016-04-08 14:38:01 -07001411 # self._load() calls self._trim() which initializes self._free_disk.
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001412 self._load()
1413
nodirbe642ff2016-06-09 15:51:51 -07001414 def __contains__(self, digest):
1415 with self._lock:
1416 return digest in self._lru
1417
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001418 def __enter__(self):
1419 return self
1420
1421 def __exit__(self, _exc_type, _exec_value, _traceback):
1422 with tools.Profiler('CleanupTrimming'):
1423 with self._lock:
1424 self._trim()
1425
1426 logging.info(
1427 '%5d (%8dkb) added',
1428 len(self._added), sum(self._added) / 1024)
1429 logging.info(
1430 '%5d (%8dkb) current',
1431 len(self._lru),
1432 sum(self._lru.itervalues()) / 1024)
1433 logging.info(
maruel064c0a32016-04-05 11:47:15 -07001434 '%5d (%8dkb) evicted',
1435 len(self._evicted), sum(self._evicted) / 1024)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001436 logging.info(
1437 ' %8dkb free',
1438 self._free_disk / 1024)
1439 return False
1440
1441 def cached_set(self):
1442 with self._lock:
1443 return self._lru.keys_set()
1444
maruel36a963d2016-04-08 17:15:49 -07001445 def cleanup(self):
maruel2e8d0f52016-07-16 07:51:29 -07001446 """Cleans up the cache directory.
1447
1448 Ensures there is no unknown files in cache_dir.
1449 Ensures the read-only bits are set correctly.
1450
1451 At that point, the cache was already loaded, trimmed to respect cache
1452 policies.
1453 """
1454 fs.chmod(self.cache_dir, 0700)
1455 # Ensure that all files listed in the state still exist and add new ones.
1456 previous = self._lru.keys_set()
1457 # It'd be faster if there were a readdir() function.
1458 for filename in fs.listdir(self.cache_dir):
1459 if filename == self.STATE_FILE:
1460 fs.chmod(os.path.join(self.cache_dir, filename), 0600)
1461 continue
1462 if filename in previous:
1463 fs.chmod(os.path.join(self.cache_dir, filename), 0400)
1464 previous.remove(filename)
1465 continue
1466
1467 # An untracked file. Delete it.
1468 logging.warning('Removing unknown file %s from cache', filename)
1469 p = self._path(filename)
1470 if fs.isdir(p):
1471 try:
1472 file_path.rmtree(p)
1473 except OSError:
1474 pass
1475 else:
1476 file_path.try_remove(p)
1477 continue
1478
1479 if previous:
1480 # Filter out entries that were not found.
1481 logging.warning('Removed %d lost files', len(previous))
1482 for filename in previous:
1483 self._lru.pop(filename)
maruel36a963d2016-04-08 17:15:49 -07001484
1485 # What remains to be done is to hash every single item to
1486 # detect corruption, then save to ensure state.json is up to date.
1487 # Sadly, on a 50Gb cache with 100mib/s I/O, this is still over 8 minutes.
1488 # TODO(maruel): Let's revisit once directory metadata is stored in
1489 # state.json so only the files that had been mapped since the last cleanup()
1490 # call are manually verified.
1491 #
1492 #with self._lock:
1493 # for digest in self._lru:
1494 # if not isolated_format.is_valid_hash(
1495 # self._path(digest), self.hash_algo):
1496 # self.evict(digest)
1497 # logging.info('Deleted corrupted item: %s', digest)
1498
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001499 def touch(self, digest, size):
1500 """Verifies an actual file is valid.
1501
1502 Note that is doesn't compute the hash so it could still be corrupted if the
1503 file size didn't change.
1504
1505 TODO(maruel): More stringent verification while keeping the check fast.
1506 """
1507 # Do the check outside the lock.
1508 if not is_valid_file(self._path(digest), size):
1509 return False
1510
1511 # Update it's LRU position.
1512 with self._lock:
1513 if digest not in self._lru:
1514 return False
1515 self._lru.touch(digest)
maruel2e8d0f52016-07-16 07:51:29 -07001516 self._protected = self._protected or digest
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001517 return True
1518
1519 def evict(self, digest):
1520 with self._lock:
maruel2e8d0f52016-07-16 07:51:29 -07001521 # Do not check for 'digest == self._protected' since it could be because
maruel083fa552016-04-08 14:38:01 -07001522 # the object is corrupted.
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001523 self._lru.pop(digest)
1524 self._delete_file(digest, UNKNOWN_FILE_SIZE)
1525
1526 def read(self, digest):
nodir445097b2016-06-03 22:50:26 -07001527 try:
1528 with fs.open(self._path(digest), 'rb') as f:
1529 return f.read()
1530 except IOError:
1531 raise CacheMiss(digest)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001532
1533 def write(self, digest, content):
Marc-Antoine Rueldf4976d2015-04-15 19:56:21 -04001534 assert content is not None
maruel083fa552016-04-08 14:38:01 -07001535 with self._lock:
maruel2e8d0f52016-07-16 07:51:29 -07001536 self._protected = self._protected or digest
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001537 path = self._path(digest)
1538 # A stale broken file may remain. It is possible for the file to have write
1539 # access bit removed which would cause the file_write() call to fail to open
1540 # in write mode. Take no chance here.
1541 file_path.try_remove(path)
1542 try:
1543 size = file_write(path, content)
1544 except:
1545 # There are two possible places were an exception can occur:
1546 # 1) Inside |content| generator in case of network or unzipping errors.
1547 # 2) Inside file_write itself in case of disk IO errors.
1548 # In any case delete an incomplete file and propagate the exception to
1549 # caller, it will be logged there.
1550 file_path.try_remove(path)
1551 raise
1552 # Make the file read-only in the cache. This has a few side-effects since
1553 # the file node is modified, so every directory entries to this file becomes
1554 # read-only. It's fine here because it is a new file.
1555 file_path.set_read_only(path, True)
1556 with self._lock:
1557 self._add(digest, size)
maruel083fa552016-04-08 14:38:01 -07001558 return digest
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001559
1560 def hardlink(self, digest, dest, file_mode):
1561 """Hardlinks the file to |dest|.
1562
1563 Note that the file permission bits are on the file node, not the directory
1564 entry, so changing the access bit on any of the directory entries for the
1565 file node will affect them all.
1566 """
1567 path = self._path(digest)
maruel1f7e8162015-09-16 10:35:43 -07001568 if not file_path.link_file(dest, path, file_path.HARDLINK_WITH_FALLBACK):
1569 # Report to the server that it failed with more details. We'll want to
1570 # squash them all.
1571 on_error.report('Failed to hardlink\n%s -> %s' % (path, dest))
1572
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001573 if file_mode is not None:
1574 # Ignores all other bits.
maruel12e30012015-10-09 11:55:35 -07001575 fs.chmod(dest, file_mode & 0500)
maruel064c0a32016-04-05 11:47:15 -07001576 with self._lock:
1577 self._linked.append(self._lru[digest])
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001578
1579 def _load(self):
maruel2e8d0f52016-07-16 07:51:29 -07001580 """Loads state of the cache from json file.
1581
1582 If cache_dir does not exist on disk, it is created.
1583 """
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001584 self._lock.assert_locked()
1585
maruel2e8d0f52016-07-16 07:51:29 -07001586 if not fs.isfile(self.state_file):
1587 if not os.path.isdir(self.cache_dir):
1588 fs.makedirs(self.cache_dir)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001589 else:
maruel2e8d0f52016-07-16 07:51:29 -07001590 # Load state of the cache.
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001591 try:
1592 self._lru = lru.LRUDict.load(self.state_file)
1593 except ValueError as err:
1594 logging.error('Failed to load cache state: %s' % (err,))
1595 # Don't want to keep broken state file.
1596 file_path.try_remove(self.state_file)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001597 self._trim()
maruel2e8d0f52016-07-16 07:51:29 -07001598 # We want the initial cache size after trimming, i.e. what is readily
1599 # avaiable.
1600 self._initial_number_items = len(self._lru)
1601 self._initial_size = sum(self._lru.itervalues())
1602 if self._evicted:
1603 logging.info(
1604 'Trimming evicted items with the following sizes: %s',
1605 sorted(self._evicted))
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001606
1607 def _save(self):
1608 """Saves the LRU ordering."""
1609 self._lock.assert_locked()
1610 if sys.platform != 'win32':
1611 d = os.path.dirname(self.state_file)
maruel12e30012015-10-09 11:55:35 -07001612 if fs.isdir(d):
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001613 # Necessary otherwise the file can't be created.
1614 file_path.set_read_only(d, False)
maruel12e30012015-10-09 11:55:35 -07001615 if fs.isfile(self.state_file):
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001616 file_path.set_read_only(self.state_file, False)
1617 self._lru.save(self.state_file)
1618
1619 def _trim(self):
1620 """Trims anything we don't know, make sure enough free space exists."""
1621 self._lock.assert_locked()
1622
1623 # Ensure maximum cache size.
1624 if self.policies.max_cache_size:
1625 total_size = sum(self._lru.itervalues())
1626 while total_size > self.policies.max_cache_size:
maruel2e8d0f52016-07-16 07:51:29 -07001627 total_size -= self._remove_lru_file(True)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001628
1629 # Ensure maximum number of items in the cache.
1630 if self.policies.max_items and len(self._lru) > self.policies.max_items:
1631 for _ in xrange(len(self._lru) - self.policies.max_items):
maruel2e8d0f52016-07-16 07:51:29 -07001632 self._remove_lru_file(True)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001633
1634 # Ensure enough free space.
1635 self._free_disk = file_path.get_free_space(self.cache_dir)
kjlubickea9abf02016-06-01 09:34:33 -07001636 trimmed_due_to_space = 0
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001637 while (
1638 self.policies.min_free_space and
1639 self._lru and
1640 self._free_disk < self.policies.min_free_space):
kjlubickea9abf02016-06-01 09:34:33 -07001641 trimmed_due_to_space += 1
maruel2e8d0f52016-07-16 07:51:29 -07001642 self._remove_lru_file(True)
kjlubickea9abf02016-06-01 09:34:33 -07001643
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001644 if trimmed_due_to_space:
1645 total_usage = sum(self._lru.itervalues())
1646 usage_percent = 0.
1647 if total_usage:
kjlubickea9abf02016-06-01 09:34:33 -07001648 usage_percent = 100. * float(total_usage) / self.policies.max_cache_size
1649
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001650 logging.warning(
kjlubickea9abf02016-06-01 09:34:33 -07001651 'Trimmed %s file(s) due to not enough free disk space: %.1fkb free,'
1652 ' %.1fkb cache (%.1f%% of its maximum capacity of %.1fkb)',
1653 trimmed_due_to_space,
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001654 self._free_disk / 1024.,
1655 total_usage / 1024.,
kjlubickea9abf02016-06-01 09:34:33 -07001656 usage_percent,
1657 self.policies.max_cache_size / 1024.)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001658 self._save()
1659
1660 def _path(self, digest):
1661 """Returns the path to one item."""
1662 return os.path.join(self.cache_dir, digest)
1663
maruel2e8d0f52016-07-16 07:51:29 -07001664 def _remove_lru_file(self, allow_protected):
1665 """Removes the lastest recently used file and returns its size."""
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001666 self._lock.assert_locked()
maruel083fa552016-04-08 14:38:01 -07001667 try:
1668 digest, size = self._lru.get_oldest()
maruel2e8d0f52016-07-16 07:51:29 -07001669 if not allow_protected and digest == self._protected:
1670 raise Error('Not enough space to map the whole isolated tree')
maruel083fa552016-04-08 14:38:01 -07001671 except KeyError:
1672 raise Error('Nothing to remove')
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001673 digest, size = self._lru.pop_oldest()
kjlubickea9abf02016-06-01 09:34:33 -07001674 logging.debug("Removing LRU file %s", digest)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001675 self._delete_file(digest, size)
1676 return size
1677
1678 def _add(self, digest, size=UNKNOWN_FILE_SIZE):
1679 """Adds an item into LRU cache marking it as a newest one."""
1680 self._lock.assert_locked()
1681 if size == UNKNOWN_FILE_SIZE:
maruel12e30012015-10-09 11:55:35 -07001682 size = fs.stat(self._path(digest)).st_size
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001683 self._added.append(size)
1684 self._lru.add(digest, size)
maruel083fa552016-04-08 14:38:01 -07001685 self._free_disk -= size
1686 # Do a quicker version of self._trim(). It only enforces free disk space,
1687 # not cache size limits. It doesn't actually look at real free disk space,
1688 # only uses its cache values. self._trim() will be called later to enforce
1689 # real trimming but doing this quick version here makes it possible to map
1690 # an isolated that is larger than the current amount of free disk space when
1691 # the cache size is already large.
1692 while (
1693 self.policies.min_free_space and
1694 self._lru and
1695 self._free_disk < self.policies.min_free_space):
maruel2e8d0f52016-07-16 07:51:29 -07001696 self._remove_lru_file(False)
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001697
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001698 def _delete_file(self, digest, size=UNKNOWN_FILE_SIZE):
1699 """Deletes cache file from the file system."""
1700 self._lock.assert_locked()
1701 try:
1702 if size == UNKNOWN_FILE_SIZE:
maruel12e30012015-10-09 11:55:35 -07001703 size = fs.stat(self._path(digest)).st_size
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001704 file_path.try_remove(self._path(digest))
maruel064c0a32016-04-05 11:47:15 -07001705 self._evicted.append(size)
maruel083fa552016-04-08 14:38:01 -07001706 self._free_disk += size
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04001707 except OSError as e:
1708 logging.error('Error attempting to delete a file %s:\n%s' % (digest, e))
1709
1710
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001711class IsolatedBundle(object):
1712 """Fetched and parsed .isolated file with all dependencies."""
1713
Vadim Shtayura3148e072014-09-02 18:51:52 -07001714 def __init__(self):
1715 self.command = []
1716 self.files = {}
1717 self.read_only = None
1718 self.relative_cwd = None
1719 # The main .isolated file, a IsolatedFile instance.
1720 self.root = None
1721
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001722 def fetch(self, fetch_queue, root_isolated_hash, algo):
1723 """Fetches the .isolated and all the included .isolated.
Vadim Shtayura3148e072014-09-02 18:51:52 -07001724
1725 It enables support for "included" .isolated files. They are processed in
1726 strict order but fetched asynchronously from the cache. This is important so
1727 that a file in an included .isolated file that is overridden by an embedding
1728 .isolated file is not fetched needlessly. The includes are fetched in one
1729 pass and the files are fetched as soon as all the ones on the left-side
1730 of the tree were fetched.
1731
1732 The prioritization is very important here for nested .isolated files.
1733 'includes' have the highest priority and the algorithm is optimized for both
1734 deep and wide trees. A deep one is a long link of .isolated files referenced
1735 one at a time by one item in 'includes'. A wide one has a large number of
1736 'includes' in a single .isolated file. 'left' is defined as an included
1737 .isolated file earlier in the 'includes' list. So the order of the elements
1738 in 'includes' is important.
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001739
1740 As a side effect this method starts asynchronous fetch of all data files
1741 by adding them to |fetch_queue|. It doesn't wait for data files to finish
1742 fetching though.
Vadim Shtayura3148e072014-09-02 18:51:52 -07001743 """
1744 self.root = isolated_format.IsolatedFile(root_isolated_hash, algo)
1745
1746 # Isolated files being retrieved now: hash -> IsolatedFile instance.
1747 pending = {}
1748 # Set of hashes of already retrieved items to refuse recursive includes.
1749 seen = set()
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001750 # Set of IsolatedFile's whose data files have already being fetched.
1751 processed = set()
Vadim Shtayura3148e072014-09-02 18:51:52 -07001752
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001753 def retrieve_async(isolated_file):
Vadim Shtayura3148e072014-09-02 18:51:52 -07001754 h = isolated_file.obj_hash
1755 if h in seen:
1756 raise isolated_format.IsolatedError(
1757 'IsolatedFile %s is retrieved recursively' % h)
1758 assert h not in pending
1759 seen.add(h)
1760 pending[h] = isolated_file
1761 fetch_queue.add(h, priority=threading_utils.PRIORITY_HIGH)
1762
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001763 # Start fetching root *.isolated file (single file, not the whole bundle).
1764 retrieve_async(self.root)
Vadim Shtayura3148e072014-09-02 18:51:52 -07001765
1766 while pending:
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001767 # Wait until some *.isolated file is fetched, parse it.
Vadim Shtayura3148e072014-09-02 18:51:52 -07001768 item_hash = fetch_queue.wait(pending)
1769 item = pending.pop(item_hash)
1770 item.load(fetch_queue.cache.read(item_hash))
Vadim Shtayura3148e072014-09-02 18:51:52 -07001771
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001772 # Start fetching included *.isolated files.
Vadim Shtayura3148e072014-09-02 18:51:52 -07001773 for new_child in item.children:
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001774 retrieve_async(new_child)
Vadim Shtayura3148e072014-09-02 18:51:52 -07001775
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001776 # Always fetch *.isolated files in traversal order, waiting if necessary
1777 # until next to-be-processed node loads. "Waiting" is done by yielding
1778 # back to the outer loop, that waits until some *.isolated is loaded.
1779 for node in isolated_format.walk_includes(self.root):
1780 if node not in processed:
1781 # Not visited, and not yet loaded -> wait for it to load.
1782 if not node.is_loaded:
1783 break
1784 # Not visited and loaded -> process it and continue the traversal.
1785 self._start_fetching_files(node, fetch_queue)
1786 processed.add(node)
Vadim Shtayura3148e072014-09-02 18:51:52 -07001787
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001788 # All *.isolated files should be processed by now and only them.
1789 all_isolateds = set(isolated_format.walk_includes(self.root))
1790 assert all_isolateds == processed, (all_isolateds, processed)
Vadim Shtayura3148e072014-09-02 18:51:52 -07001791
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001792 # Extract 'command' and other bundle properties.
1793 for node in isolated_format.walk_includes(self.root):
1794 self._update_self(node)
Vadim Shtayura3148e072014-09-02 18:51:52 -07001795 self.relative_cwd = self.relative_cwd or ''
1796
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001797 def _start_fetching_files(self, isolated, fetch_queue):
1798 """Starts fetching files from |isolated| that are not yet being fetched.
Vadim Shtayura3148e072014-09-02 18:51:52 -07001799
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001800 Modifies self.files.
1801 """
1802 logging.debug('fetch_files(%s)', isolated.obj_hash)
1803 for filepath, properties in isolated.data.get('files', {}).iteritems():
1804 # Root isolated has priority on the files being mapped. In particular,
1805 # overridden files must not be fetched.
1806 if filepath not in self.files:
1807 self.files[filepath] = properties
1808 if 'h' in properties:
1809 # Preemptively request files.
1810 logging.debug('fetching %s', filepath)
1811 fetch_queue.add(
1812 properties['h'], properties['s'], threading_utils.PRIORITY_MED)
1813
1814 def _update_self(self, node):
1815 """Extracts bundle global parameters from loaded *.isolated file.
1816
1817 Will be called with each loaded *.isolated file in order of traversal of
1818 isolated include graph (see isolated_format.walk_includes).
1819 """
Vadim Shtayura3148e072014-09-02 18:51:52 -07001820 # Grabs properties.
1821 if not self.command and node.data.get('command'):
1822 # Ensure paths are correctly separated on windows.
1823 self.command = node.data['command']
1824 if self.command:
1825 self.command[0] = self.command[0].replace('/', os.path.sep)
1826 self.command = tools.fix_python_path(self.command)
1827 if self.read_only is None and node.data.get('read_only') is not None:
1828 self.read_only = node.data['read_only']
1829 if (self.relative_cwd is None and
1830 node.data.get('relative_cwd') is not None):
1831 self.relative_cwd = node.data['relative_cwd']
1832
1833
Vadim Shtayura8623c272014-12-01 11:45:27 -08001834def set_storage_api_class(cls):
1835 """Replaces StorageApi implementation used by default."""
1836 global _storage_api_cls
1837 assert _storage_api_cls is None
1838 assert issubclass(cls, StorageApi)
1839 _storage_api_cls = cls
1840
1841
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -05001842def get_storage_api(url, namespace):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001843 """Returns an object that implements low-level StorageApi interface.
1844
1845 It is used by Storage to work with single isolate |namespace|. It should
1846 rarely be used directly by clients, see 'get_storage' for
1847 a better alternative.
1848
1849 Arguments:
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -05001850 url: URL of isolate service to use shared cloud based storage.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001851 namespace: isolate namespace to operate in, also defines hashing and
1852 compression scheme used, i.e. namespace names that end with '-gzip'
1853 store compressed data.
1854
1855 Returns:
1856 Instance of StorageApi subclass.
1857 """
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -05001858 cls = _storage_api_cls or IsolateServer
1859 return cls(url, namespace)
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00001860
1861
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -05001862def get_storage(url, namespace):
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001863 """Returns Storage class that can upload and download from |namespace|.
1864
1865 Arguments:
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -05001866 url: URL of isolate service to use shared cloud based storage.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001867 namespace: isolate namespace to operate in, also defines hashing and
1868 compression scheme used, i.e. namespace names that end with '-gzip'
1869 store compressed data.
1870
1871 Returns:
1872 Instance of Storage.
1873 """
Marc-Antoine Ruelb10edf22014-12-11 13:33:57 -05001874 return Storage(get_storage_api(url, namespace))
maruel@chromium.orgdedbf492013-09-12 20:42:11 +00001875
maruel@chromium.orgdedbf492013-09-12 20:42:11 +00001876
Vadim Shtayuraea38c572014-10-06 16:57:16 -07001877def upload_tree(base_url, infiles, namespace):
maruel@chromium.orgc6f90062012-11-07 18:32:22 +00001878 """Uploads the given tree to the given url.
1879
1880 Arguments:
Vadim Shtayuraea38c572014-10-06 16:57:16 -07001881 base_url: The url of the isolate server to upload to.
1882 infiles: iterable of pairs (absolute path, metadata dict) of files.
csharp@chromium.org59c7bcf2012-11-21 21:13:18 +00001883 namespace: The namespace to use on the server.
maruel@chromium.orgc6f90062012-11-07 18:32:22 +00001884 """
Vadim Shtayuraea38c572014-10-06 16:57:16 -07001885 # Convert |infiles| into a list of FileItem objects, skip duplicates.
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001886 # Filter out symlinks, since they are not represented by items on isolate
1887 # server side.
Vadim Shtayuraea38c572014-10-06 16:57:16 -07001888 items = []
1889 seen = set()
1890 skipped = 0
1891 for filepath, metadata in infiles:
maruel12e30012015-10-09 11:55:35 -07001892 assert isinstance(filepath, unicode), filepath
Vadim Shtayuraea38c572014-10-06 16:57:16 -07001893 if 'l' not in metadata and filepath not in seen:
1894 seen.add(filepath)
1895 item = FileItem(
1896 path=filepath,
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001897 digest=metadata['h'],
1898 size=metadata['s'],
1899 high_priority=metadata.get('priority') == '0')
Vadim Shtayuraea38c572014-10-06 16:57:16 -07001900 items.append(item)
1901 else:
1902 skipped += 1
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08001903
Vadim Shtayuraea38c572014-10-06 16:57:16 -07001904 logging.info('Skipped %d duplicated entries', skipped)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001905 with get_storage(base_url, namespace) as storage:
maruel064c0a32016-04-05 11:47:15 -07001906 return storage.upload_items(items)
Vadim Shtayura3148e072014-09-02 18:51:52 -07001907
1908
maruelb8d88d12016-04-08 12:54:01 -07001909def fetch_isolated(isolated_hash, storage, cache, outdir):
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00001910 """Aggressively downloads the .isolated file(s), then download all the files.
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00001911
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001912 Arguments:
1913 isolated_hash: hash of the root *.isolated file.
1914 storage: Storage class that communicates with isolate storage.
1915 cache: LocalCache class that knows how to store and map files locally.
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001916 outdir: Output directory to map file tree to.
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001917
1918 Returns:
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001919 IsolatedBundle object that holds details about loaded *.isolated file.
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001920 """
Marc-Antoine Ruel4e8cd182014-06-18 13:27:17 -04001921 logging.debug(
maruelb8d88d12016-04-08 12:54:01 -07001922 'fetch_isolated(%s, %s, %s, %s)', isolated_hash, storage, cache, outdir)
Vadim Shtayurae0ab1902014-04-29 10:55:27 -07001923 # Hash algorithm to use, defined by namespace |storage| is using.
1924 algo = storage.hash_algo
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001925 with cache:
1926 fetch_queue = FetchQueue(storage, cache)
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001927 bundle = IsolatedBundle()
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001928
1929 with tools.Profiler('GetIsolateds'):
1930 # Optionally support local files by manually adding them to cache.
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -04001931 if not isolated_format.is_valid_hash(isolated_hash, algo):
Marc-Antoine Ruel4e8cd182014-06-18 13:27:17 -04001932 logging.debug('%s is not a valid hash, assuming a file', isolated_hash)
maruel1ceb3872015-10-14 06:10:44 -07001933 path = unicode(os.path.abspath(isolated_hash))
Marc-Antoine Ruel4e8cd182014-06-18 13:27:17 -04001934 try:
maruel1ceb3872015-10-14 06:10:44 -07001935 isolated_hash = fetch_queue.inject_local_file(path, algo)
Marc-Antoine Ruel4e8cd182014-06-18 13:27:17 -04001936 except IOError:
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -04001937 raise isolated_format.MappingError(
Marc-Antoine Ruel4e8cd182014-06-18 13:27:17 -04001938 '%s doesn\'t seem to be a valid file. Did you intent to pass a '
1939 'valid hash?' % isolated_hash)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001940
1941 # Load all *.isolated and start loading rest of the files.
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001942 bundle.fetch(fetch_queue, isolated_hash, algo)
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00001943
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001944 with tools.Profiler('GetRest'):
1945 # Create file system hierarchy.
nodire5028a92016-04-29 14:38:21 -07001946 file_path.ensure_tree(outdir)
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001947 create_directories(outdir, bundle.files)
1948 create_symlinks(outdir, bundle.files.iteritems())
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00001949
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001950 # Ensure working directory exists.
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001951 cwd = os.path.normpath(os.path.join(outdir, bundle.relative_cwd))
nodire5028a92016-04-29 14:38:21 -07001952 file_path.ensure_tree(cwd)
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00001953
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001954 # Multimap: digest -> list of pairs (path, props).
1955 remaining = {}
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001956 for filepath, props in bundle.files.iteritems():
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001957 if 'h' in props:
1958 remaining.setdefault(props['h'], []).append((filepath, props))
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00001959
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001960 # Now block on the remaining files to be downloaded and mapped.
1961 logging.info('Retrieving remaining files (%d of them)...',
1962 fetch_queue.pending_count)
1963 last_update = time.time()
Vadim Shtayura3148e072014-09-02 18:51:52 -07001964 with threading_utils.DeadlockDetector(DEADLOCK_TIMEOUT) as detector:
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001965 while remaining:
1966 detector.ping()
1967
1968 # Wait for any item to finish fetching to cache.
1969 digest = fetch_queue.wait(remaining)
1970
1971 # Link corresponding files to a fetched item in cache.
1972 for filepath, props in remaining.pop(digest):
nodir90bc8dc2016-06-15 13:35:21 -07001973 dest = os.path.join(outdir, filepath)
1974 if os.path.exists(dest):
1975 raise AlreadyExists('File %s already exists' % dest)
1976 cache.hardlink(digest, dest, props.get('m'))
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00001977
1978 # Report progress.
1979 duration = time.time() - last_update
1980 if duration > DELAY_BETWEEN_UPDATES_IN_SECS:
1981 msg = '%d files remaining...' % len(remaining)
1982 print msg
1983 logging.info(msg)
1984 last_update = time.time()
1985
1986 # Cache could evict some items we just tried to fetch, it's a fatal error.
1987 if not fetch_queue.verify_all_cached():
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -04001988 raise isolated_format.MappingError(
1989 'Cache is too small to hold all requested files')
Vadim Shtayura7f7459c2014-09-04 13:25:10 -07001990 return bundle
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00001991
1992
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05001993def directory_to_metadata(root, algo, blacklist):
1994 """Returns the FileItem list and .isolated metadata for a directory."""
1995 root = file_path.get_native_path_case(root)
Marc-Antoine Ruel92257792014-08-28 20:51:08 -04001996 paths = isolated_format.expand_directory_and_symlink(
Vadim Shtayura439d3fc2014-05-07 16:05:12 -07001997 root, '.' + os.path.sep, blacklist, sys.platform != 'win32')
Marc-Antoine Ruel92257792014-08-28 20:51:08 -04001998 metadata = {
1999 relpath: isolated_format.file_to_metadata(
Marc-Antoine Ruelf1d827c2014-11-24 15:22:25 -05002000 os.path.join(root, relpath), {}, 0, algo)
Marc-Antoine Ruel92257792014-08-28 20:51:08 -04002001 for relpath in paths
2002 }
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002003 for v in metadata.itervalues():
2004 v.pop('t')
2005 items = [
2006 FileItem(
2007 path=os.path.join(root, relpath),
2008 digest=meta['h'],
2009 size=meta['s'],
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08002010 high_priority=relpath.endswith('.isolated'))
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002011 for relpath, meta in metadata.iteritems() if 'h' in meta
2012 ]
2013 return items, metadata
2014
2015
Vadim Shtayurae0ab1902014-04-29 10:55:27 -07002016def archive_files_to_storage(storage, files, blacklist):
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -05002017 """Stores every entries and returns the relevant data.
2018
2019 Arguments:
2020 storage: a Storage object that communicates with the remote object store.
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -05002021 files: list of file paths to upload. If a directory is specified, a
2022 .isolated file is created and its hash is returned.
2023 blacklist: function that returns True if a file should be omitted.
maruel064c0a32016-04-05 11:47:15 -07002024
2025 Returns:
2026 tuple(list(tuple(hash, path)), list(FileItem cold), list(FileItem hot)).
2027 The first file in the first item is always the isolated file.
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -05002028 """
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002029 assert all(isinstance(i, unicode) for i in files), files
2030 if len(files) != len(set(map(os.path.abspath, files))):
2031 raise Error('Duplicate entries found.')
2032
maruel064c0a32016-04-05 11:47:15 -07002033 # List of tuple(hash, path).
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002034 results = []
2035 # The temporary directory is only created as needed.
2036 tempdir = None
2037 try:
2038 # TODO(maruel): Yield the files to a worker thread.
2039 items_to_upload = []
2040 for f in files:
2041 try:
2042 filepath = os.path.abspath(f)
maruel12e30012015-10-09 11:55:35 -07002043 if fs.isdir(filepath):
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002044 # Uploading a whole directory.
Vadim Shtayurae0ab1902014-04-29 10:55:27 -07002045 items, metadata = directory_to_metadata(
2046 filepath, storage.hash_algo, blacklist)
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002047
2048 # Create the .isolated file.
2049 if not tempdir:
Marc-Antoine Ruel3c979cb2015-03-11 13:43:28 -04002050 tempdir = tempfile.mkdtemp(prefix=u'isolateserver')
2051 handle, isolated = tempfile.mkstemp(dir=tempdir, suffix=u'.isolated')
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002052 os.close(handle)
2053 data = {
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -04002054 'algo':
2055 isolated_format.SUPPORTED_ALGOS_REVERSE[storage.hash_algo],
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002056 'files': metadata,
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -04002057 'version': isolated_format.ISOLATED_FILE_VERSION,
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002058 }
Marc-Antoine Ruel52436aa2014-08-28 21:57:57 -04002059 isolated_format.save_isolated(isolated, data)
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -04002060 h = isolated_format.hash_file(isolated, storage.hash_algo)
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002061 items_to_upload.extend(items)
2062 items_to_upload.append(
2063 FileItem(
2064 path=isolated,
2065 digest=h,
maruel12e30012015-10-09 11:55:35 -07002066 size=fs.stat(isolated).st_size,
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08002067 high_priority=True))
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002068 results.append((h, f))
2069
maruel12e30012015-10-09 11:55:35 -07002070 elif fs.isfile(filepath):
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -04002071 h = isolated_format.hash_file(filepath, storage.hash_algo)
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002072 items_to_upload.append(
2073 FileItem(
2074 path=filepath,
2075 digest=h,
maruel12e30012015-10-09 11:55:35 -07002076 size=fs.stat(filepath).st_size,
Vadim Shtayurabcff74f2014-02-27 16:19:34 -08002077 high_priority=f.endswith('.isolated')))
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002078 results.append((h, f))
2079 else:
2080 raise Error('%s is neither a file or directory.' % f)
2081 except OSError:
2082 raise Error('Failed to process %s.' % f)
maruel064c0a32016-04-05 11:47:15 -07002083 uploaded = storage.upload_items(items_to_upload)
2084 cold = [i for i in items_to_upload if i in uploaded]
2085 hot = [i for i in items_to_upload if i not in uploaded]
2086 return results, cold, hot
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002087 finally:
maruel12e30012015-10-09 11:55:35 -07002088 if tempdir and fs.isdir(tempdir):
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -04002089 file_path.rmtree(tempdir)
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002090
2091
Marc-Antoine Ruel488ce8f2014-02-09 11:25:04 -05002092def archive(out, namespace, files, blacklist):
2093 if files == ['-']:
2094 files = sys.stdin.readlines()
2095
2096 if not files:
2097 raise Error('Nothing to upload')
2098
2099 files = [f.decode('utf-8') for f in files]
Marc-Antoine Ruel488ce8f2014-02-09 11:25:04 -05002100 blacklist = tools.gen_blacklist(blacklist)
2101 with get_storage(out, namespace) as storage:
maruel064c0a32016-04-05 11:47:15 -07002102 # Ignore stats.
2103 results = archive_files_to_storage(storage, files, blacklist)[0]
Marc-Antoine Ruel488ce8f2014-02-09 11:25:04 -05002104 print('\n'.join('%s %s' % (r[0], r[1]) for r in results))
2105
2106
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002107@subcommand.usage('<file1..fileN> or - to read from stdin')
2108def CMDarchive(parser, args):
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002109 """Archives data to the server.
2110
2111 If a directory is specified, a .isolated file is created the whole directory
2112 is uploaded. Then this .isolated file can be included in another one to run
2113 commands.
2114
2115 The commands output each file that was processed with its content hash. For
2116 directories, the .isolated generated for the directory is listed as the
2117 directory entry itself.
2118 """
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05002119 add_isolate_server_options(parser)
Marc-Antoine Ruel1f8ba352014-11-04 15:55:03 -05002120 add_archive_options(parser)
maruel@chromium.orgcb3c3d52013-03-14 18:55:30 +00002121 options, files = parser.parse_args(args)
nodir55be77b2016-05-03 09:39:57 -07002122 process_isolate_server_options(parser, options, True, True)
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002123 try:
Marc-Antoine Ruel488ce8f2014-02-09 11:25:04 -05002124 archive(options.isolate_server, options.namespace, files, options.blacklist)
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002125 except Error as e:
2126 parser.error(e.args[0])
Marc-Antoine Ruelfcc3cd82013-11-19 16:31:38 -05002127 return 0
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002128
2129
2130def CMDdownload(parser, args):
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00002131 """Download data from the server.
2132
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00002133 It can either download individual files or a complete tree from a .isolated
2134 file.
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00002135 """
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05002136 add_isolate_server_options(parser)
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00002137 parser.add_option(
Marc-Antoine Ruel185ded42015-01-28 20:49:18 -05002138 '-s', '--isolated', metavar='HASH',
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00002139 help='hash of an isolated file, .isolated file content is discarded, use '
2140 '--file if you need it')
2141 parser.add_option(
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00002142 '-f', '--file', metavar='HASH DEST', default=[], action='append', nargs=2,
2143 help='hash and destination of a file, can be used multiple times')
2144 parser.add_option(
Marc-Antoine Ruelf90861c2015-03-24 20:54:49 -04002145 '-t', '--target', metavar='DIR', default='download',
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00002146 help='destination directory')
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04002147 add_cache_options(parser)
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00002148 options, args = parser.parse_args(args)
2149 if args:
2150 parser.error('Unsupported arguments: %s' % args)
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05002151
nodir55be77b2016-05-03 09:39:57 -07002152 process_isolate_server_options(parser, options, True, True)
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +00002153 if bool(options.isolated) == bool(options.file):
2154 parser.error('Use one of --isolated or --file, and only one.')
maruel@chromium.orgb7e79a22013-09-13 01:24:56 +00002155
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04002156 cache = process_cache_options(options)
maruel2e8d0f52016-07-16 07:51:29 -07002157 cache.cleanup()
maruel12e30012015-10-09 11:55:35 -07002158 options.target = unicode(os.path.abspath(options.target))
Marc-Antoine Ruelf90861c2015-03-24 20:54:49 -04002159 if options.isolated:
maruel12e30012015-10-09 11:55:35 -07002160 if (fs.isfile(options.target) or
2161 (fs.isdir(options.target) and fs.listdir(options.target))):
Marc-Antoine Ruelf90861c2015-03-24 20:54:49 -04002162 parser.error(
2163 '--target \'%s\' exists, please use another target' % options.target)
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05002164 with get_storage(options.isolate_server, options.namespace) as storage:
Vadim Shtayura3172be52013-12-03 12:49:05 -08002165 # Fetching individual files.
2166 if options.file:
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04002167 # TODO(maruel): Enable cache in this case too.
Vadim Shtayura3172be52013-12-03 12:49:05 -08002168 channel = threading_utils.TaskChannel()
2169 pending = {}
2170 for digest, dest in options.file:
2171 pending[digest] = dest
2172 storage.async_fetch(
2173 channel,
Vadim Shtayura3148e072014-09-02 18:51:52 -07002174 threading_utils.PRIORITY_MED,
Vadim Shtayura3172be52013-12-03 12:49:05 -08002175 digest,
Vadim Shtayura3148e072014-09-02 18:51:52 -07002176 UNKNOWN_FILE_SIZE,
Vadim Shtayura3172be52013-12-03 12:49:05 -08002177 functools.partial(file_write, os.path.join(options.target, dest)))
2178 while pending:
2179 fetched = channel.pull()
2180 dest = pending.pop(fetched)
2181 logging.info('%s: %s', fetched, dest)
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00002182
Vadim Shtayura3172be52013-12-03 12:49:05 -08002183 # Fetching whole isolated tree.
2184 if options.isolated:
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04002185 with cache:
2186 bundle = fetch_isolated(
2187 isolated_hash=options.isolated,
2188 storage=storage,
2189 cache=cache,
maruelb8d88d12016-04-08 12:54:01 -07002190 outdir=options.target)
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04002191 if bundle.command:
2192 rel = os.path.join(options.target, bundle.relative_cwd)
2193 print('To run this test please run from the directory %s:' %
2194 os.path.join(options.target, rel))
2195 print(' ' + ' '.join(bundle.command))
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +00002196
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002197 return 0
2198
2199
Marc-Antoine Ruel1f8ba352014-11-04 15:55:03 -05002200def add_archive_options(parser):
2201 parser.add_option(
2202 '--blacklist',
2203 action='append', default=list(DEFAULT_BLACKLIST),
2204 help='List of regexp to use as blacklist filter when uploading '
2205 'directories')
2206
2207
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05002208def add_isolate_server_options(parser):
2209 """Adds --isolate-server and --namespace options to parser."""
Marc-Antoine Ruel1687b5e2014-02-06 17:47:53 -05002210 parser.add_option(
2211 '-I', '--isolate-server',
2212 metavar='URL', default=os.environ.get('ISOLATE_SERVER', ''),
Marc-Antoine Ruel8806e622014-02-12 14:15:53 -05002213 help='URL of the Isolate Server to use. Defaults to the environment '
2214 'variable ISOLATE_SERVER if set. No need to specify https://, this '
2215 'is assumed.')
Marc-Antoine Ruel1687b5e2014-02-06 17:47:53 -05002216 parser.add_option(
2217 '--namespace', default='default-gzip',
2218 help='The namespace to use on the Isolate Server, default: %default')
2219
2220
nodir55be77b2016-05-03 09:39:57 -07002221def process_isolate_server_options(
2222 parser, options, set_exception_handler, required):
2223 """Processes the --isolate-server option.
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05002224
2225 Returns the identity as determined by the server.
Marc-Antoine Ruel1687b5e2014-02-06 17:47:53 -05002226 """
2227 if not options.isolate_server:
nodir55be77b2016-05-03 09:39:57 -07002228 if required:
2229 parser.error('--isolate-server is required.')
2230 return
2231
Marc-Antoine Ruel012067b2014-12-10 15:45:42 -05002232 try:
2233 options.isolate_server = net.fix_url(options.isolate_server)
2234 except ValueError as e:
2235 parser.error('--isolate-server %s' % e)
Marc-Antoine Ruele290ada2014-12-10 19:48:49 -05002236 if set_exception_handler:
2237 on_error.report_on_exception_exit(options.isolate_server)
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05002238 try:
2239 return auth.ensure_logged_in(options.isolate_server)
2240 except ValueError as e:
2241 parser.error(str(e))
Marc-Antoine Ruel8806e622014-02-12 14:15:53 -05002242
Marc-Antoine Ruel1687b5e2014-02-06 17:47:53 -05002243
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04002244def add_cache_options(parser):
2245 cache_group = optparse.OptionGroup(parser, 'Cache management')
2246 cache_group.add_option(
2247 '--cache', metavar='DIR',
2248 help='Directory to keep a local cache of the files. Accelerates download '
2249 'by reusing already downloaded files. Default=%default')
2250 cache_group.add_option(
2251 '--max-cache-size',
2252 type='int',
2253 metavar='NNN',
maruel71586102016-01-29 11:44:09 -08002254 default=50*1024*1024*1024,
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04002255 help='Trim if the cache gets larger than this value, default=%default')
2256 cache_group.add_option(
2257 '--min-free-space',
2258 type='int',
2259 metavar='NNN',
2260 default=2*1024*1024*1024,
2261 help='Trim if disk free space becomes lower than this value, '
2262 'default=%default')
2263 cache_group.add_option(
2264 '--max-items',
2265 type='int',
2266 metavar='NNN',
2267 default=100000,
2268 help='Trim if more than this number of items are in the cache '
2269 'default=%default')
2270 parser.add_option_group(cache_group)
2271
2272
2273def process_cache_options(options):
2274 if options.cache:
2275 policies = CachePolicies(
2276 options.max_cache_size, options.min_free_space, options.max_items)
2277
2278 # |options.cache| path may not exist until DiskCache() instance is created.
2279 return DiskCache(
Marc-Antoine Ruel3c979cb2015-03-11 13:43:28 -04002280 unicode(os.path.abspath(options.cache)),
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04002281 policies,
2282 isolated_format.get_hash_algo(options.namespace))
2283 else:
2284 return MemoryCache()
2285
2286
Marc-Antoine Ruelf74cffe2015-07-15 15:21:34 -04002287class OptionParserIsolateServer(logging_utils.OptionParserWithLogging):
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002288 def __init__(self, **kwargs):
Marc-Antoine Ruelf74cffe2015-07-15 15:21:34 -04002289 logging_utils.OptionParserWithLogging.__init__(
Marc-Antoine Ruelac54cb42013-11-18 14:05:35 -05002290 self,
2291 version=__version__,
2292 prog=os.path.basename(sys.modules[__name__].__file__),
2293 **kwargs)
Vadim Shtayurae34e13a2014-02-02 11:23:26 -08002294 auth.add_auth_options(self)
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002295
2296 def parse_args(self, *args, **kwargs):
Marc-Antoine Ruelf74cffe2015-07-15 15:21:34 -04002297 options, args = logging_utils.OptionParserWithLogging.parse_args(
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002298 self, *args, **kwargs)
Vadim Shtayura5d1efce2014-02-04 10:55:43 -08002299 auth.process_auth_options(self, options)
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002300 return options, args
2301
2302
2303def main(args):
2304 dispatcher = subcommand.CommandDispatcher(__name__)
Marc-Antoine Ruelcfb60852014-07-02 15:22:00 -04002305 return dispatcher.execute(OptionParserIsolateServer(), args)
maruel@chromium.orgc6f90062012-11-07 18:32:22 +00002306
2307
2308if __name__ == '__main__':
maruel8e4e40c2016-05-30 06:21:07 -07002309 subprocess42.inhibit_os_error_reporting()
maruel@chromium.orgfb78d432013-08-28 21:22:40 +00002310 fix_encoding.fix_encoding()
2311 tools.disable_buffering()
2312 colorama.init()
maruel@chromium.orgcb3c3d52013-03-14 18:55:30 +00002313 sys.exit(main(sys.argv[1:]))