Пример #1
0
 def transaction(self, callback, retry=3, entity_group=None):
     # Will invoke callback() one or more times with the default
     # context set to a new, transactional Context.  Returns a Future.
     # Callback may be a tasklet.
     if entity_group is not None:
         app = entity_group.app()
     else:
         app = ndb.key._DefaultAppId()
     yield self.flush()
     for i in range(1 + max(0, retry)):
         transaction = yield self._conn.async_begin_transaction(None, app)
         tconn = datastore_rpc.TransactionalConnection(
             adapter=self._conn.adapter, config=self._conn.config, transaction=transaction, entity_group=entity_group
         )
         tctx = self.__class__(conn=tconn, auto_batcher_class=self._auto_batcher_class)
         tctx.set_memcache_policy(lambda key: False)
         tasklets.set_context(tctx)
         try:
             try:
                 result = callback()
                 if isinstance(result, tasklets.Future):
                     result = yield result
             finally:
                 yield tctx.flush()
         except Exception, err:
             t, e, tb = sys.exc_info()
             yield tconn.async_rollback(None)  # TODO: Don't block???
             raise t, e, tb
         else:
             ok = yield tconn.async_commit(None)
             if ok:
                 # TODO: This is questionable when self is transactional.
                 self._cache.update(tctx._cache)
                 self._flush_memcache(tctx._cache)
                 raise tasklets.Return(result)
Пример #2
0
 def process_request(self, unused_request):
   """Called by Django before deciding which view to execute."""
   # Compare to the first half of toplevel() in context.py.
   tasklets._state.clear_all_pending()
   # Create and install a new context.
   ctx = tasklets.make_default_context()
   tasklets.set_context(ctx)
Пример #3
0
 def add_context_wrapper(*args):
   __ndb_debug__ = utils.func_info(func)
   tasklets.Future.clear_all_pending()
   # Reset context; a new one will be created on the first call to
   # get_context().
   tasklets.set_context(None)
   ctx = tasklets.get_context()
   try:
     return tasklets.synctasklet(func)(*args)
   finally:
     eventloop.run()  # Ensure writes are flushed, etc.
Пример #4
0
  def setUp(self):
    super(QueryTests, self).setUp()
    tasklets.set_context(context.Context())

    # Create class inside tests because kinds are cleared every test.
    global Foo
    class Foo(model.Model):
      name = model.StringProperty()
      rate = model.IntegerProperty()
      tags = model.StringProperty(repeated=True)
    self.create_entities()
Пример #5
0
    def SetupContextCache(self):
        """Set up the context cache.

            We only need cache active when testing the cache, so the default behavior
            is to disable it to avoid misleading test results. Override this when
            needed.
            """
        ctx = tasklets.make_default_context()
        tasklets.set_context(ctx)
        ctx.set_cache_policy(False)
        ctx.set_memcache_policy(False)
Пример #6
0
  def setUp(self):
    super(QueryTests, self).setUp()
    tasklets.set_context(context.Context())

    # Create class inside tests because kinds are cleared every test.
    global Foo
    class Foo(model.Model):
      name = model.StringProperty()
      rate = model.IntegerProperty()
      tags = model.StringProperty(repeated=True)
    self.create_entities()
Пример #7
0
 def transaction(self, callback, retry=3, entity_group=None, **ctx_options):
   # Will invoke callback() one or more times with the default
   # context set to a new, transactional Context.  Returns a Future.
   # Callback may be a tasklet.
   options = _make_ctx_options(ctx_options)
   if entity_group is not None:
     app = entity_group.app()
   else:
     app = ndb.key._DefaultAppId()
   yield self.flush()
   for i in range(1 + max(0, retry)):
     transaction = yield self._conn.async_begin_transaction(options, app)
     tconn = datastore_rpc.TransactionalConnection(
       adapter=self._conn.adapter,
       config=self._conn.config,
       transaction=transaction,
       entity_group=entity_group)
     tctx = self.__class__(conn=tconn,
                           auto_batcher_class=self._auto_batcher_class)
     tctx.set_memcache_policy(False)
     tasklets.set_context(tctx)
     old_ds_conn = datastore._GetConnection()
     try:
       datastore._SetConnection(tconn)  # For taskqueue coordination
       try:
         try:
           result = callback()
           if isinstance(result, tasklets.Future):
             result = yield result
         finally:
           yield tctx.flush()
       except Exception, err:
         t, e, tb = sys.exc_info()
         yield tconn.async_rollback(options)  # TODO: Don't block???
         if issubclass(t, datastore_errors.Rollback):
           return
         else:
           raise t, e, tb
       else:
         ok = yield tconn.async_commit(options)
         if ok:
           # TODO: This is questionable when self is transactional.
           self._cache.update(tctx._cache)
           self._clear_memcache(tctx._cache)
           raise tasklets.Return(result)
def cause_problem():
  tb = testbed.Testbed()
  tb.activate()
  tb.init_datastore_v3_stub()
  tb.init_memcache_stub()
  ctx = tasklets.make_default_context()
  tasklets.set_context(ctx)
  ctx.set_datastore_policy(True)
  ctx.set_cache_policy(False)
  ctx.set_memcache_policy(True)

  @tasklets.tasklet
  def problem_tasklet():
    class Foo(model.Model):
      pass
    key = yield ctx.put(Foo())
    yield ctx.get(key)  # Trigger get_tasklet that does not complete...
    yield ctx.delete(key)  # ... by the time this delete_tasklet starts.
    a = yield ctx.get(key)
    assert a is None, '%r is not None' % a

  problem_tasklet().check_success()
  print 'No problem yet...'
  tb.deactivate()
Пример #9
0
def cause_problem():
  tb = testbed.Testbed()
  tb.activate()
  tb.init_datastore_v3_stub()
  tb.init_memcache_stub()
  ctx = tasklets.make_default_context()
  tasklets.set_context(ctx)
  ctx.set_datastore_policy(True)
  ctx.set_cache_policy(False)
  ctx.set_memcache_policy(True)

  @tasklets.tasklet
  def problem_tasklet():
    class Foo(model.Model):
      pass
    key = yield ctx.put(Foo())
    yield ctx.get(key)  # Trigger get_tasklet that does not complete...
    yield ctx.delete(key)  # ... by the time this delete_tasklet starts.
    a = yield ctx.get(key)
    assert a is None, '%r is not None' % a

  problem_tasklet().check_success()
  print 'No problem yet...'
  tb.deactivate()
Пример #10
0
 def _finish():
   # Compare to the finally clause in toplevel() in context.py.
   ctx = tasklets.get_context()
   tasklets.set_context(None)
   ctx.flush().check_success()
   eventloop.run()  # Ensure writes are flushed, etc.
Пример #11
0
 def setUp(self):
   os.environ['APPLICATION_ID'] = '_'
   self.set_up_stubs()
   tasklets.set_context(context.Context())
   self.create_entities()