Ejemplo n.º 1
0
  def write_varenv(self, qs, varenv):
    """Write to the graph the specified "query"."""

    if getattr(self, 'readonly', None):
      raise GraphConnectionError(
          'Tried to write to a read-only graph',
          http_code=500,
          app_code='/mqlwrite/backend/read_only')

    dateline_in = varenv.get('write_dateline', None)

    self.write_occurred = 1

    try:
      r = self._generate_and_transmit_query(qs, varenv, WriteMode)

    except MQLDatelineInvalidError:
      # see read_varenv comment on this
      LOG.info('mqlwrite.dateline.delete',
               'got an invalid dateline, deleting from varenv',
               varenv.get('write_dateline'))
      varenv['write_dateline'] = ''

      r = self._generate_and_transmit_query(qs, varenv, WriteMode)

    dateline_out = r.dateline

    # update our write_dateline in case we do subsequent reads
    # or writes. The new 'write_dateline' is returned to the
    # user for use with subsequent mqlreads or mqlwrites they do
    varenv['write_dateline'] = dateline_out
    varenv['last_write_time'] = time.time()
    log_graph_write(varenv, dateline_in, dateline_out)

    LOG.debug(
        'graph.write_dateline.set',
        '',
        last_write_time=varenv['last_write_time'],
        write_dateline=varenv['write_dateline'])

    # Record that a write has happened and following writes should set
    # the continuation flag.
    varenv['is_write_continuation'] = True

    return r
Ejemplo n.º 2
0
  def read_varenv(self, qs, varenv):
    """Read from the graph the specified "query"."""
    try:
      # the pymql user provides a 'write_dateline', which should be a valid
      # dateline returned to said user by a previous mqlwrite query
      dateline_in = varenv.get('write_dateline', None)

      r = self._generate_and_transmit_query(qs, varenv, ReadMode)

    except MQLDatelineInvalidError:
      # Drop the datelines out of the varenv,
      # re-generate the query and try again.
      # the main use case here is when sandbox is refreshed
      # and the instance id in the dateline changes. The user's dateline
      # (usually in a cookie) is now invalid until they do a write, or a touch
      LOG.info('mqlread.dateline.delete',
               'got an invalid dateline, deleting from varenv',
               varenv.get('write_dateline'))
      varenv['write_dateline'] = ''

      r = self._generate_and_transmit_query(qs, varenv, ReadMode)

    if not r and varenv.get('graph_noisy'):
      raise EmptyResult('query %s' % qs)

    dateline_out = r.dateline

    # 'dateline' is returned to the original caller of pymql read.
    # though, in practice, it is not passed on by frapi and
    # they really should only update their dateline after doing
    # a write.
    # we do *not* update the internal 'write_dateline' varenv, here.
    # in the case of reads, the idea being the user only needs to
    # demand the level of freshness of their last write, so
    # subsequent reads in this session will use the original
    # 'write_dateline' provided by the caller of pymql read/write.
    # the 'write_dateline' is updated in the event that a write
    # occurs in this session.
    varenv['dateline'] = dateline_out
    log_graph_read(varenv, dateline_in, dateline_out)

    LOG.debug('graph.dateline.set', '', dateline=varenv['dateline'])

    return r
Ejemplo n.º 3
0
  def __init__(self,
               no_timeouts=False,
               policy_map=None,
               default_policy=None,
               custom_policy=None):

    self.reset_cost()
    self.timeout_policies = policy_map

    if default_policy:
      self.default_policy = default_policy
    else:
      self.default_policy = self.DEFAULT_POLICY_NAME

    self.no_timeouts = no_timeouts

    if custom_policy:
      LOG.info('gc.custom.timeout.policy', '', policy=str(custom_policy))
      self.timeout_policies['custom'] = custom_policy
      self.default_policy = 'custom'
Ejemplo n.º 4
0
            response['request_id'] = sq['request_id']

        # should only looking either at sq['query'] for a single query or
        # sq['queries'] for multiple queries
        for id, subq in valid_queries:
            # assuming querier is a bound method here..
            LOG.notice('Query',
                       '%s.%s' % (querier.im_class.__name__, querier.__name__),
                       subq=subq)
            try:
                results[id] = querier(subq, varenv)

                response.extend(status='200 OK')

            except EmptyResult, e:
                LOG.info('emptyresult', '%s' % e)
                response.log('empty result for query %s' % subq)
                result = None

            # exceptions should be packed into response['error']
            except ParameterizedError, e:
                if isinstance(e, MQLInternalError):
                    response.extend(status='500 Internal Server Error')
                else:
                    response.extend(status='400 Bad Request')

                tb = json_traceback(response=response, exception=e)
                response.log('parse exception: %s' % e, level='error')
                result = None
            except Exception, e:
                LOG.exception('python.exception')