def testLocalAllocateIds(self): """AllocateIds request should go local or remote depending on the key.""" request = self.CreateAllocateIdsRequest('localapp') response = datastore_pb.AllocateIdsResponse() response.set_start(1) response.set_end(2) self.mock_stub.MakeSyncCall('datastore_v3', 'AllocateIds', request, response) self.mox.ReplayAll() self.stub.MakeSyncCall('datastore_v3', 'AllocateIds', request, response)
def allocate_ids_request(self, app_id, http_request_data): """ High level function for getting unique identifiers for entities. Args: app_id: Name of the application. http_request_data: Stores the protocol buffer request from the AppServer. Returns: Returns an encoded response. Raises: NotImplementedError: when requesting a max id. """ global datastore_access request = datastore_pb.AllocateIdsRequest(http_request_data) response = datastore_pb.AllocateIdsResponse() if READ_ONLY: logger.warning('Unable to allocate in read-only mode: {}'. format(request)) return (response.Encode(), datastore_pb.Error.CAPABILITY_DISABLED, 'Datastore is in read-only mode.') max_id = int(request.max()) size = int(request.size()) start = end = 0 try: start, end = datastore_access.allocate_ids(app_id, size, max_id=max_id) except zktransaction.ZKBadRequest as zkie: logger.exception('Unable to allocate IDs for {}'.format(app_id)) return (response.Encode(), datastore_pb.Error.BAD_REQUEST, "Illegal arguments for transaction. {0}".format(str(zkie))) except zktransaction.ZKInternalException: logger.exception('Unable to allocate IDs for {}'.format(app_id)) return (response.Encode(), datastore_pb.Error.INTERNAL_ERROR, "Internal error with ZooKeeper connection.") except zktransaction.ZKTransactionException: logger.exception('Unable to allocate IDs for {}'.format(app_id)) return (response.Encode(), datastore_pb.Error.CONCURRENT_TRANSACTION, "Concurrent transaction exception on allocate id request.") except dbconstants.AppScaleDBConnectionError: logger.exception('DB connection error while allocating IDs for {}'. format(app_id)) return (response.Encode(), datastore_pb.Error.INTERNAL_ERROR, "Datastore connection error on allocate id request.") response.set_start(start) response.set_end(end) return (response.Encode(), 0, "")
def allocate_ids_request(self, app_id, http_request_data): """ High level function for getting unique identifiers for entities. Args: app_id: Name of the application. http_request_data: Stores the protocol buffer request from the AppServer. Returns: Returns an encoded response. Raises: NotImplementedError: when requesting a max id. """ request = datastore_pb.AllocateIdsRequest(http_request_data) if request.has_max() and request.has_size(): raise gen.Return( ('', datastore_pb.Error.BAD_REQUEST, 'Both size and max cannot be set.')) if not (request.has_max() or request.has_size()): raise gen.Return( ('', datastore_pb.Error.BAD_REQUEST, 'Either size or max must be set.')) if not request.has_model_key(): raise gen.Return(('', datastore_pb.Error.BAD_REQUEST, 'Model key must be set')) namespace = six.text_type(request.model_key().name_space()) path_prefix = Path.flatten(request.model_key().path(), allow_partial=True)[:-1] if request.has_size(): coroutine = datastore_access.allocate_size args = (app_id, namespace, path_prefix, request.size()) else: coroutine = datastore_access.allocate_max args = (app_id, namespace, path_prefix, request.max()) try: start, end = yield coroutine(*args) except dbconstants.AppScaleBadArg as error: raise gen.Return(('', datastore_pb.Error.BAD_REQUEST, str(error))) except dbconstants.AppScaleDBConnectionError as error: raise gen.Return(('', datastore_pb.Error.INTERNAL_ERROR, str(error))) response = datastore_pb.AllocateIdsResponse() response.set_start(start) response.set_end(end) raise gen.Return((response.Encode(), 0, ''))
def allocate_ids_request(self, app_id, http_request_data): """ High level function for getting unique identifiers for entities. Args: app_id: Name of the application. http_request_data: Stores the protocol buffer request from the AppServer. Returns: Returns an encoded response. Raises: NotImplementedError: when requesting a max id. """ request = datastore_pb.AllocateIdsRequest(http_request_data) response = datastore_pb.AllocateIdsResponse() if request.has_max() and request.has_size(): return (response.Encode(), datastore_pb.Error.BAD_REQUEST, 'Both size and max cannot be set.') if not (request.has_max() or request.has_size()): return (response.Encode(), datastore_pb.Error.BAD_REQUEST, 'Either size or max must be set.') if request.has_size(): try: start, end = datastore_access.allocate_size( app_id, request.size()) except dbconstants.AppScaleBadArg as error: return response.Encode(), datastore_pb.Error.BAD_REQUEST, str( error) except dbconstants.AppScaleDBConnectionError as error: return response.Encode( ), datastore_pb.Error.INTERNAL_ERROR, str(error) else: try: start, end = datastore_access.allocate_max( app_id, request.max()) except dbconstants.AppScaleBadArg as error: return response.Encode(), datastore_pb.Error.BAD_REQUEST, str( error) except dbconstants.AppScaleDBConnectionError as error: return response.Encode( ), datastore_pb.Error.INTERNAL_ERROR, str(error) response.set_start(start) response.set_end(end) return response.Encode(), 0, ""
def testRemoteAllocateIds(self): """AllocateIds request should go local or remote depending on the key.""" request = self.CreateAllocateIdsRequest('remoteapp') response = datastore_pb.AllocateIdsResponse() response.set_start(1) response.set_end(2) expected_post = self.RemoteApiRequest('AllocateIds', request).Encode() expected_response = self.RemoteApiResponse(response).Encode() remote_api_put_stub.urlfetch.fetch( self.remote_url, expected_post, urlfetch.POST, {'X-appcfg-api-version': '1', 'auth': 'good'}, follow_redirects=False ).AndReturn(MockUrlfetchResult(200, expected_response)) self.mox.ReplayAll() self.stub.MakeSyncCall('datastore_v3', 'AllocateIds', request, response)