Merge branch 'master' into 4823-python-sdk-writable-collection-api
authorPeter Amstutz <peter.amstutz@curoverse.com>
Wed, 25 Feb 2015 14:39:36 +0000 (09:39 -0500)
committerPeter Amstutz <peter.amstutz@curoverse.com>
Wed, 25 Feb 2015 14:39:36 +0000 (09:39 -0500)
1  2 
sdk/python/arvados/keep.py
sdk/python/tests/test_keep_client.py
services/fuse/arvados_fuse/__init__.py
services/fuse/tests/test_mount.py

index ab683526e077d266ee24090543ad98cea3b200cd,71dc7ce7af863f60fb9e741b7bbc1231c22b146b..19c2252ceda827ce763096156fcba52605f20e5e
@@@ -58,9 -58,6 +58,9 @@@ class KeepLocator(object)
                               self.permission_hint()] + self.hints
              if s is not None)
  
 +    def stripped(self):
 +        return "%s+%i" % (self.md5sum, self.size)
 +
      def _make_hex_prop(name, length):
          # Build and return a new property with the given name that
          # must be a hex string of the given length.
@@@ -174,7 -171,8 +174,7 @@@ class KeepBlockCache(object)
  
      def cap_cache(self):
          '''Cap the cache size to self.cache_max'''
 -        self._cache_lock.acquire()
 -        try:
 +        with self._cache_lock:
              # Select all slots except those where ready.is_set() and content is
              # None (that means there was an error reading the block).
              self._cache = [c for c in self._cache if not (c.ready.is_set() and c.content is None)]
                          del self._cache[i]
                          break
                  sm = sum([slot.size() for slot in self._cache])
 -        finally:
 -            self._cache_lock.release()
 +
 +    def _get(self, locator):
 +        # Test if the locator is already in the cache
 +        for i in xrange(0, len(self._cache)):
 +            if self._cache[i].locator == locator:
 +                n = self._cache[i]
 +                if i != 0:
 +                    # move it to the front
 +                    del self._cache[i]
 +                    self._cache.insert(0, n)
 +                return n
 +        return None
 +
 +    def get(self, locator):
 +        with self._cache_lock:
 +            return self._get(locator)
  
      def reserve_cache(self, locator):
          '''Reserve a cache slot for the specified locator,
          or return the existing slot.'''
 -        self._cache_lock.acquire()
 -        try:
 -            # Test if the locator is already in the cache
 -            for i in xrange(0, len(self._cache)):
 -                if self._cache[i].locator == locator:
 -                    n = self._cache[i]
 -                    if i != 0:
 -                        # move it to the front
 -                        del self._cache[i]
 -                        self._cache.insert(0, n)
 -                    return n, False
 -
 -            # Add a new cache slot for the locator
 -            n = KeepBlockCache.CacheSlot(locator)
 -            self._cache.insert(0, n)
 -            return n, True
 -        finally:
 -            self._cache_lock.release()
 +        with self._cache_lock:
 +            n = self._get(locator)
 +            if n:
 +                return n, False
 +            else:
 +                # Add a new cache slot for the locator
 +                n = KeepBlockCache.CacheSlot(locator)
 +                self._cache.insert(0, n)
 +                return n, True
  
  class KeepClient(object):
  
          HTTP_ERRORS = (requests.exceptions.RequestException,
                         socket.error, ssl.SSLError)
  
 -        def __init__(self, root, **headers):
 +        def __init__(self, root, session, **headers):
              self.root = root
              self.last_result = None
              self.success_flag = None
 +            self.session = session
              self.get_headers = {'Accept': 'application/octet-stream'}
              self.get_headers.update(headers)
              self.put_headers = headers
              _logger.debug("Request: GET %s", url)
              try:
                  with timer.Timer() as t:
 -                    result = requests.get(url.encode('utf-8'),
 +                    result = self.session.get(url.encode('utf-8'),
                                            headers=self.get_headers,
                                            timeout=timeout)
              except self.HTTP_ERRORS as e:
                  content = result.content
                  _logger.info("%s response: %s bytes in %s msec (%.3f MiB/sec)",
                               self.last_status(), len(content), t.msecs,
 -                             (len(content)/(1024.0*1024))/t.secs)
 +                             (len(content)/(1024.0*1024))/t.secs if t.secs > 0 else 0)
                  if self.success_flag:
                      resp_md5 = hashlib.md5(content).hexdigest()
                      if resp_md5 == locator.md5sum:
              url = self.root + hash_s
              _logger.debug("Request: PUT %s", url)
              try:
 -                result = requests.put(url.encode('utf-8'),
 +                result = self.session.put(url.encode('utf-8'),
                                        data=body,
                                        headers=self.put_headers,
                                        timeout=timeout)
          def run_with_limiter(self, limiter):
              if self.service.finished():
                  return
 -            _logger.debug("KeepWriterThread %s proceeding %s %s",
 +            _logger.debug("KeepWriterThread %s proceeding %s+%i %s",
                            str(threading.current_thread()),
                            self.args['data_hash'],
 +                          len(self.args['data']),
                            self.args['service_root'])
              self._success = bool(self.service.put(
                  self.args['data_hash'],
              status = self.service.last_status()
              if self._success:
                  result = self.service.last_result
 -                _logger.debug("KeepWriterThread %s succeeded %s %s",
 +                _logger.debug("KeepWriterThread %s succeeded %s+%i %s",
                                str(threading.current_thread()),
                                self.args['data_hash'],
 +                              len(self.args['data']),
                                self.args['service_root'])
                  # Tick the 'done' counter for the number of replica
                  # reported stored by the server, for the case that
                      replicas_stored = int(result.headers['x-keep-replicas-stored'])
                  except (KeyError, ValueError):
                      replicas_stored = 1
-                 limiter.save_response(result.text.strip(), replicas_stored)
+                 limiter.save_response(result.content.strip(), replicas_stored)
              elif status is not None:
                  _logger.debug("Request fail: PUT %s => %s %s",
                                self.args['data_hash'], status,
-                               self.service.last_result.text)
+                               self.service.last_result.content)
  
  
      def __init__(self, api_client=None, proxy=None,
                   timeout=DEFAULT_TIMEOUT, proxy_timeout=DEFAULT_PROXY_TIMEOUT,
                   api_token=None, local_store=None, block_cache=None,
 -                 num_retries=0):
 +                 num_retries=0, session=None):
          """Initialize a new KeepClient.
  
          Arguments:
 -        * api_client: The API client to use to find Keep services.  If not
 +        :api_client:
 +          The API client to use to find Keep services.  If not
            provided, KeepClient will build one from available Arvados
            configuration.
 -        * proxy: If specified, this KeepClient will send requests to this
 -          Keep proxy.  Otherwise, KeepClient will fall back to the setting
 -          of the ARVADOS_KEEP_PROXY configuration setting.  If you want to
 -          ensure KeepClient does not use a proxy, pass in an empty string.
 -        * timeout: The timeout (in seconds) for HTTP requests to Keep
 +
 +        :proxy:
 +          If specified, this KeepClient will send requests to this Keep
 +          proxy.  Otherwise, KeepClient will fall back to the setting of the
 +          ARVADOS_KEEP_PROXY configuration setting.  If you want to ensure
 +          KeepClient does not use a proxy, pass in an empty string.
 +
 +        :timeout:
 +          The timeout (in seconds) for HTTP requests to Keep
            non-proxy servers.  A tuple of two floats is interpreted as
            (connection_timeout, read_timeout): see
            http://docs.python-requests.org/en/latest/user/advanced/#timeouts.
            Default: (2, 300).
 -        * proxy_timeout: The timeout (in seconds) for HTTP requests to
 +
 +        :proxy_timeout:
 +          The timeout (in seconds) for HTTP requests to
            Keep proxies. A tuple of two floats is interpreted as
            (connection_timeout, read_timeout). Default: (20, 300).
 -        * api_token: If you're not using an API client, but only talking
 +
 +        :api_token:
 +          If you're not using an API client, but only talking
            directly to a Keep proxy, this parameter specifies an API token
            to authenticate Keep requests.  It is an error to specify both
            api_client and api_token.  If you specify neither, KeepClient
            will use one available from the Arvados configuration.
 -        * local_store: If specified, this KeepClient will bypass Keep
 +
 +        :local_store:
 +          If specified, this KeepClient will bypass Keep
            services, and save data to the named directory.  If unspecified,
            KeepClient will fall back to the setting of the $KEEP_LOCAL_STORE
            environment variable.  If you want to ensure KeepClient does not
            use local storage, pass in an empty string.  This is primarily
            intended to mock a server for testing.
 -        * num_retries: The default number of times to retry failed requests.
 +
 +        :num_retries:
 +          The default number of times to retry failed requests.
            This will be used as the default num_retries value when get() and
            put() are called.  Default 0.
 +
 +        :session:
 +          The requests.Session object to use for get() and put() requests.
 +          Will create one if not specified.
          """
          self.lock = threading.Lock()
          if proxy is None:
              self.put = self.local_store_put
          else:
              self.num_retries = num_retries
 +            self.session = session if session is not None else requests.Session()
              if proxy:
                  if not proxy.endswith('/'):
                      proxy += '/'
          local_roots = self.weighted_service_roots(md5_s, force_rebuild)
          for root in local_roots:
              if root not in roots_map:
 -                roots_map[root] = self.KeepService(root, **headers)
 +                roots_map[root] = self.KeepService(root, self.session, **headers)
          return local_roots
  
      @staticmethod
          else:
              return None
  
 +    def get_from_cache(self, loc):
 +        """Fetch a block only if is in the cache, otherwise return None."""
 +        slot = self.block_cache.get(loc)
 +        if slot.ready.is_set():
 +            return slot.get()
 +        else:
 +            return None
 +
      @retry.retry_method
      def get(self, loc_s, num_retries=None):
          """Get data from Keep.
              return ''.join(self.get(x) for x in loc_s.split(','))
          locator = KeepLocator(loc_s)
          expect_hash = locator.md5sum
 -
          slot, first = self.block_cache.reserve_cache(expect_hash)
          if not first:
              v = slot.get()
          hint_roots = ['http://keep.{}.arvadosapi.com/'.format(hint[2:])
                        for hint in locator.hints if hint.startswith('K@')]
          # Map root URLs their KeepService objects.
 -        roots_map = {root: self.KeepService(root) for root in hint_roots}
 +        roots_map = {root: self.KeepService(root, self.session) for root in hint_roots}
          blob = None
          loop = retry.RetryLoop(num_retries, self._check_loop_result,
                                 backoff_start=2)
            exponential backoff.  The default value is set when the
            KeepClient is initialized.
          """
+         if isinstance(data, unicode):
+             data = data.encode("ascii")
+         elif not isinstance(data, str):
+             raise arvados.errors.ArgumentError("Argument 'data' to KeepClient.put must be type 'str'")
          data_hash = hashlib.md5(data).hexdigest()
          if copies < 1:
              return data_hash
              return ''
          with open(os.path.join(self.local_store, locator.md5sum), 'r') as f:
              return f.read()
 +
 +    def is_cached(self, locator):
 +        return self.block_cache.reserve_cache(expect_hash)
index 1baf1357ccf8dbfdc78960d4a63b326595e249c5,ad1d7a9c87cef02bd42812e8168328c902b09538..2ee9054d4635d154f80e0cdb40d5648aed04331c
@@@ -76,6 -76,21 +76,21 @@@ class KeepTestCase(run_test_server.Test
              '^d41d8cd98f00b204e9800998ecf8427e\+0',
              ('wrong locator from Keep.put(""): ' + blob_locator))
  
+     def test_unicode_must_be_ascii(self):
+         # If unicode type, must only consist of valid ASCII
+         foo_locator = self.keep_client.put(u'foo')
+         self.assertRegexpMatches(
+             foo_locator,
+             '^acbd18db4cc2f85cedef654fccc4a4d8\+3',
+             'wrong md5 hash from Keep.put("foo"): ' + foo_locator)
+         with self.assertRaises(UnicodeEncodeError):
+             # Error if it is not ASCII
+             self.keep_client.put(u'\xe2')
+         with self.assertRaises(arvados.errors.ArgumentError):
+             # Must be a string type
+             self.keep_client.put({})
  
  class KeepPermissionTestCase(run_test_server.TestCaseWithServers):
      MAIN_SERVER = {}
@@@ -258,57 -273,57 +273,57 @@@ class KeepClientServiceTestCase(unittes
  
      def test_get_timeout(self):
          api_client = self.mock_keep_services(count=1)
 -        keep_client = arvados.KeepClient(api_client=api_client)
          force_timeout = [socket.timeout("timed out")]
 -        with mock.patch('requests.get', side_effect=force_timeout) as mock_request:
 +        with tutil.mock_get(force_timeout) as mock_session:
 +            keep_client = arvados.KeepClient(api_client=api_client)
              with self.assertRaises(arvados.errors.KeepReadError):
                  keep_client.get('ffffffffffffffffffffffffffffffff')
 -            self.assertTrue(mock_request.called)
 +            self.assertTrue(mock_session.return_value.get.called)
              self.assertEqual(
                  arvados.KeepClient.DEFAULT_TIMEOUT,
 -                mock_request.call_args[1]['timeout'])
 +                mock_session.return_value.get.call_args[1]['timeout'])
  
      def test_put_timeout(self):
          api_client = self.mock_keep_services(count=1)
 -        keep_client = arvados.KeepClient(api_client=api_client)
          force_timeout = [socket.timeout("timed out")]
 -        with mock.patch('requests.put', side_effect=force_timeout) as mock_request:
 +        with tutil.mock_put(force_timeout) as mock_session:
 +            keep_client = arvados.KeepClient(api_client=api_client)
              with self.assertRaises(arvados.errors.KeepWriteError):
                  keep_client.put('foo')
 -            self.assertTrue(mock_request.called)
 +            self.assertTrue(mock_session.return_value.put.called)
              self.assertEqual(
                  arvados.KeepClient.DEFAULT_TIMEOUT,
 -                mock_request.call_args[1]['timeout'])
 +                mock_session.return_value.put.call_args[1]['timeout'])
  
      def test_proxy_get_timeout(self):
          # Force a timeout, verifying that the requests.get or
          # requests.put method was called with the proxy_timeout
          # setting rather than the default timeout.
          api_client = self.mock_keep_services(service_type='proxy', count=1)
 -        keep_client = arvados.KeepClient(api_client=api_client)
          force_timeout = [socket.timeout("timed out")]
 -        with mock.patch('requests.get', side_effect=force_timeout) as mock_request:
 +        with tutil.mock_get(force_timeout) as mock_session:
 +            keep_client = arvados.KeepClient(api_client=api_client)
              with self.assertRaises(arvados.errors.KeepReadError):
                  keep_client.get('ffffffffffffffffffffffffffffffff')
 -            self.assertTrue(mock_request.called)
 +            self.assertTrue(mock_session.return_value.get.called)
              self.assertEqual(
                  arvados.KeepClient.DEFAULT_PROXY_TIMEOUT,
 -                mock_request.call_args[1]['timeout'])
 +                mock_session.return_value.get.call_args[1]['timeout'])
  
      def test_proxy_put_timeout(self):
          # Force a timeout, verifying that the requests.get or
          # requests.put method was called with the proxy_timeout
          # setting rather than the default timeout.
          api_client = self.mock_keep_services(service_type='proxy', count=1)
 -        keep_client = arvados.KeepClient(api_client=api_client)
          force_timeout = [socket.timeout("timed out")]
 -        with mock.patch('requests.put', side_effect=force_timeout) as mock_request:
 +        with tutil.mock_put(force_timeout) as mock_session:
 +            keep_client = arvados.KeepClient(api_client=api_client)
              with self.assertRaises(arvados.errors.KeepWriteError):
                  keep_client.put('foo')
 -            self.assertTrue(mock_request.called)
 +            self.assertTrue(mock_session.return_value.put.called)
              self.assertEqual(
                  arvados.KeepClient.DEFAULT_PROXY_TIMEOUT,
 -                mock_request.call_args[1]['timeout'])
 +                mock_session.return_value.put.call_args[1]['timeout'])
  
      def test_probe_order_reference_set(self):
          # expected_order[i] is the probe order for
  
      def check_errors_from_last_retry(self, verb, exc_class):
          api_client = self.mock_keep_services(count=2)
 -        keep_client = arvados.KeepClient(api_client=api_client)
          req_mock = getattr(tutil, 'mock_{}_responses'.format(verb))(
              "retry error reporting test", 500, 500, 403, 403)
          with req_mock, tutil.skip_sleep, \
                  self.assertRaises(exc_class) as err_check:
 +            keep_client = arvados.KeepClient(api_client=api_client)
              getattr(keep_client, verb)('d41d8cd98f00b204e9800998ecf8427e+0',
                                         num_retries=3)
          self.assertEqual([403, 403], [
          data = 'partial failure test'
          data_loc = '{}+{}'.format(hashlib.md5(data).hexdigest(), len(data))
          api_client = self.mock_keep_services(count=3)
 -        keep_client = arvados.KeepClient(api_client=api_client)
          with tutil.mock_put_responses(data_loc, 200, 500, 500) as req_mock, \
                  self.assertRaises(arvados.errors.KeepWriteError) as exc_check:
 +            keep_client = arvados.KeepClient(api_client=api_client)
              keep_client.put(data)
          self.assertEqual(2, len(exc_check.exception.service_errors()))
  
@@@ -541,13 -556,17 +556,13 @@@ class KeepClientRetryGetTestCase(KeepCl
              self.check_success(locator=self.HINTED_LOCATOR)
  
      def test_try_next_server_after_timeout(self):
 -        side_effects = [
 -            socket.timeout("timed out"),
 -            tutil.fake_requests_response(200, self.DEFAULT_EXPECT)]
 -        with mock.patch('requests.get',
 -                        side_effect=iter(side_effects)):
 +        with tutil.mock_get([
 +                socket.timeout("timed out"),
 +                tutil.fake_requests_response(200, self.DEFAULT_EXPECT)]):
              self.check_success(locator=self.HINTED_LOCATOR)
  
      def test_retry_data_with_wrong_checksum(self):
 -        side_effects = (tutil.fake_requests_response(200, s)
 -                        for s in ['baddata', self.TEST_DATA])
 -        with mock.patch('requests.get', side_effect=side_effects):
 +        with tutil.mock_get((tutil.fake_requests_response(200, s) for s in ['baddata', self.TEST_DATA])):
              self.check_success(locator=self.HINTED_LOCATOR)
  
  
index ec43ccfd991388639bab150e28e27b50267c4ec4,5cc666058f4fa6034a12529ac318393f18cdaed9..2d4f6c9dcde55eac714f3b2a0171fa3c836b59b6
@@@ -20,6 -20,7 +20,7 @@@ import _strptim
  import calendar
  import threading
  import itertools
+ import ciso8601
  
  from arvados.util import portable_data_hash_pattern, uuid_pattern, collection_uuid_pattern, group_uuid_pattern, user_uuid_pattern, link_uuid_pattern
  
@@@ -30,12 -31,46 +31,12 @@@ _logger = logging.getLogger('arvados.ar
  # appear as underscores in the fuse mount.)
  _disallowed_filename_characters = re.compile('[\x00/]')
  
 -class SafeApi(object):
 -    """Threadsafe wrapper for API object.
 -
 -    This stores and returns a different api object per thread, because
 -    httplib2 which underlies apiclient is not threadsafe.
 -    """
 -
 -    def __init__(self, config):
 -        self.host = config.get('ARVADOS_API_HOST')
 -        self.api_token = config.get('ARVADOS_API_TOKEN')
 -        self.insecure = config.flag_is_true('ARVADOS_API_HOST_INSECURE')
 -        self.local = threading.local()
 -        self.block_cache = arvados.KeepBlockCache()
 -
 -    def localapi(self):
 -        if 'api' not in self.local.__dict__:
 -            self.local.api = arvados.api(
 -                version='v1',
 -                host=self.host, token=self.api_token, insecure=self.insecure)
 -        return self.local.api
 -
 -    def localkeep(self):
 -        if 'keep' not in self.local.__dict__:
 -            self.local.keep = arvados.KeepClient(api_client=self.localapi(), block_cache=self.block_cache)
 -        return self.local.keep
 -
 -    def __getattr__(self, name):
 -        # Proxy nonexistent attributes to the local API client.
 -        try:
 -            return getattr(self.localapi(), name)
 -        except AttributeError:
 -            return super(SafeApi, self).__getattr__(name)
 -
 -
  def convertTime(t):
      """Parse Arvados timestamp to unix time."""
      if not t:
          return 0
      try:
-         return calendar.timegm(time.strptime(t, "%Y-%m-%dT%H:%M:%SZ"))
+         return calendar.timegm(ciso8601.parse_datetime_unaware(t).timetuple())
      except (TypeError, ValueError):
          return 0
  
@@@ -315,7 -350,7 +316,7 @@@ class CollectionDirectory(Directory)
  
              with llfuse.lock_released:
                  coll_reader = arvados.CollectionReader(
 -                    self.collection_locator, self.api, self.api.localkeep(),
 +                    self.collection_locator, self.api, self.api.keep,
                      num_retries=self.num_retries)
                  new_collection_object = coll_reader.api_response() or {}
                  # If the Collection only exists in Keep, there will be no API
@@@ -900,5 -935,5 +901,5 @@@ class Operations(llfuse.Operations)
      # arv-mount.
      # The workaround is to implement it with the proper number of parameters,
      # and then everything works out.
 -    def create(self, p1, p2, p3, p4, p5):
 +    def create(self, inode_parent, name, mode, flags, ctx):
          raise llfuse.FUSEError(errno.EROFS)
index 58b3c7ee0f6ff9587f1972344facba2b14c5c7b7,379a1306d844aa2c8f60c730d34c99c307a6ebfc..f747f937b41add3cae5a60e6f5490da841628ca5
@@@ -21,7 -21,7 +21,7 @@@ class MountTestBase(unittest.TestCase)
          self.mounttmp = tempfile.mkdtemp()
          run_test_server.run()
          run_test_server.authorize_with("admin")
 -        self.api = fuse.SafeApi(arvados.config)
 +        self.api = arvados.safeapi.ThreadSafeApiCache(arvados.config.settings())
  
      def make_mount(self, root_class, **root_kwargs):
          operations = fuse.Operations(os.getuid(), os.getgid())
@@@ -259,14 -259,26 +259,26 @@@ class FuseSharedTest(MountTestBase)
  
          # Double check that we can open and read objects in this folder as a file,
          # and that its contents are what we expect.
-         with open(os.path.join(
+         pipeline_template_path = os.path.join(
                  self.mounttmp,
                  'FUSE User',
                  'FUSE Test Project',
-                 'pipeline template in FUSE project.pipelineTemplate')) as f:
+                 'pipeline template in FUSE project.pipelineTemplate')
+         with open(pipeline_template_path) as f:
              j = json.load(f)
              self.assertEqual("pipeline template in FUSE project", j['name'])
  
+         # check mtime on template
+         st = os.stat(pipeline_template_path)
+         self.assertEqual(st.st_mtime, 1397493304)
+         # check mtime on collection
+         st = os.stat(os.path.join(
+                 self.mounttmp,
+                 'FUSE User',
+                 'collection #1 owned by FUSE'))
+         self.assertEqual(st.st_mtime, 1391448174)
  
  class FuseHomeTest(MountTestBase):
      def runTest(self):