本文整理汇总了Python中pymongo.errors.OperationFailure方法的典型用法代码示例。如果您正苦于以下问题:Python errors.OperationFailure方法的具体用法?Python errors.OperationFailure怎么用?Python errors.OperationFailure使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymongo.errors
的用法示例。
在下文中一共展示了errors.OperationFailure方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import OperationFailure [as 别名]
def get(self, key, newer_than_secs=None):
"""
:param key: Key for the dataset. eg. list_libraries.
:param newer_than_secs: None to indicate use cache if available. Used to indicate what level of staleness
in seconds is tolerable.
:return: None unless if there is non stale data present in the cache.
"""
try:
if not self._cachecol:
# Collection not created or no permissions to read from it.
return None
cached_data = self._cachecol.find_one({"type": key})
# Check that there is data in cache and it's not stale.
if cached_data and self._is_not_expired(cached_data, newer_than_secs):
return cached_data['data']
except OperationFailure as op:
logging.warning("Could not read from cache due to: %s. Ask your admin to give read permissions on %s:%s",
op, CACHE_DB, CACHE_COLL)
return None
示例2: _ensure_index
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import OperationFailure [as 别名]
def _ensure_index(collection):
try:
collection.create_index([('symbol', pymongo.HASHED)], background=True)
# We keep it only for its uniqueness
collection.create_index([('symbol', pymongo.ASCENDING),
('sha', pymongo.ASCENDING)], unique=True, background=True)
# TODO: When/if we remove the segments->versions pointers implementation and keep only the forward pointers,
# we can remove the 'parent' from the index.
collection.create_index([('symbol', pymongo.ASCENDING),
('parent', pymongo.ASCENDING),
('segment', pymongo.ASCENDING)], unique=True, background=True)
# Used for efficient SHA-based read queries that have index ranges
collection.create_index([('symbol', pymongo.ASCENDING),
('sha', pymongo.ASCENDING),
('segment', pymongo.ASCENDING)], unique=True, background=True)
except OperationFailure as e:
if "can't use unique indexes" in str(e):
return
raise
示例3: initialize_library
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import OperationFailure [as 别名]
def initialize_library(cls, arctic_lib, hashed=True, **kwargs):
c = arctic_lib.get_top_level_collection()
if 'strict_write_handler' in kwargs:
arctic_lib.set_library_metadata('STRICT_WRITE_HANDLER_MATCH',
bool(kwargs.pop('strict_write_handler')))
for th in _TYPE_HANDLERS:
th.initialize_library(arctic_lib, **kwargs)
VersionStore._bson_handler.initialize_library(arctic_lib, **kwargs)
VersionStore(arctic_lib)._ensure_index()
logger.info("Trying to enable sharding...")
try:
enable_sharding(arctic_lib.arctic, arctic_lib.get_name(), hashed=hashed)
except OperationFailure as e:
logger.warning("Library created, but couldn't enable sharding: %s. This is OK if you're not 'admin'" % str(e))
示例4: _test_query_falls_back_to_primary
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import OperationFailure [as 别名]
def _test_query_falls_back_to_primary(library_secondary, library_name, fw_pointers_cfg):
with FwPointersCtx(fw_pointers_cfg):
allow_secondary = [True]
def _query(options, *args, **kwargs):
# If we're allowing secondary read and an error occurs when reading a chunk.
# We should attempt a call to primary only subsequently.
# In newer MongoDBs we query <database>.$cmd rather than <database>.<collection>
if args[0].startswith('arctic_{}.'.format(library_name.split('.')[0])) and \
bool(options & _QUERY_OPTIONS['slave_okay']) == True:
allow_secondary[0] = False
raise OperationFailure("some_error")
return __query(options, *args, **kwargs)
library_secondary.write(symbol, ts1)
with patch('pymongo.message.query', side_effect=_query), \
patch('pymongo.server_description.ServerDescription.server_type', SERVER_TYPE.Mongos):
assert library_secondary.read(symbol) is not None
# We raised at least once on a secondary read
assert allow_secondary[0] == False
示例5: test_prune_previous_versions_retries_on_cleanup_error
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import OperationFailure [as 别名]
def test_prune_previous_versions_retries_on_cleanup_error(library, fw_pointers_cfg):
with FwPointersCtx(fw_pointers_cfg):
original_cleanup = _version_store_utils.cleanup
def _cleanup(*args, **kwargs):
if _cleanup.first_try:
_cleanup.first_try = False
raise OperationFailure(0)
else:
return original_cleanup(*args, **kwargs)
_cleanup.first_try = True
library.write(symbol, ts1)
library.write(symbol, ts2)
with patch("arctic.store.version_store.cleanup", side_effect=_cleanup) as cleanup:
cleanup.__name__ = "cleanup" # required by functools.wraps
library._prune_previous_versions(symbol, keep_mins=0)
assert len(list(library._arctic_lib.get_top_level_collection().find({'symbol': symbol}))) == 1
示例6: test_mongo_retry
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import OperationFailure [as 别名]
def test_mongo_retry():
retries = [2]
self = MagicMock()
self._arctic_lib.arctic.mongo_host = sentinel.host
self._collection.database.client.nodes = set([('a', 12)])
self._arctic_lib.get_name.return_value = sentinel.lib_name
with patch('arctic.decorators._handle_error', autospec=True) as he:
@mongo_retry
def foo(self):
if retries[0] == 2:
retries[0] -= 1
raise OperationFailure('error')
elif retries[0] == 1:
retries[0] -= 1
raise AutoReconnect('error')
return "success"
foo(self)
assert he.call_count == 2
assert isinstance(he.call_args_list[0][0][1], OperationFailure)
assert he.call_args_list[0][0][2] == 1
assert he.call_args_list[0][1] == {'mnodes': ['a:12'],
'mhost': 'sentinel.host',
'l': sentinel.lib_name}
assert isinstance(he.call_args_list[1][0][1], AutoReconnect)
assert he.call_args_list[1][0][2] == 2
示例7: test_mongo_retry_hook_changes
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import OperationFailure [as 别名]
def test_mongo_retry_hook_changes():
retries = [2]
self = MagicMock()
hook1 = Mock()
register_log_exception_hook(hook1)
hook2 = Mock()
@mongo_retry
def foo(self):
if retries[0] == 2:
retries[0] -= 1
raise OperationFailure('error')
elif retries[0] == 1:
register_log_exception_hook(hook2)
retries[0] -= 1
raise AutoReconnect('error')
return "success"
foo(self)
assert hook1.call_count == 1
assert hook2.call_count == 1
示例8: test_retry_nested
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import OperationFailure [as 别名]
def test_retry_nested():
error = OperationFailure('error')
with patch('arctic.decorators._log_exception', autospec=True) as le:
@mongo_retry
def foo():
@mongo_retry
def bar():
raise error
try:
bar()
except:
raise error
with pytest.raises(OperationFailure):
foo()
assert le.call_count == 15
assert le.call_args[0][0] == 'bar'
assert le.call_args[0][1] == error
示例9: test_read_handles_operation_failure
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import OperationFailure [as 别名]
def test_read_handles_operation_failure():
self = Mock(spec=VersionStore)
self._read_preference.return_value = sentinel.read_preference
self._collection = create_autospec(Collection)
self._read_metadata.side_effect = [sentinel.meta1, sentinel.meta2]
self._read_metadata.__name__ = 'name'
self._do_read.__name__ = 'name' # feh: mongo_retry decorator cares about this
self._do_read.side_effect = [OperationFailure('error'), sentinel.read]
VersionStore.read(self, sentinel.symbol, sentinel.as_of,
from_version=sentinel.from_version,
date_range=sentinel.date_range,
other_kwarg=sentinel.other_kwarg)
# Assert that, for the two read calls, the second uses the new metadata
assert self._do_read.call_args_list == [call(sentinel.symbol, sentinel.meta1,
sentinel.from_version,
date_range=sentinel.date_range,
other_kwarg=sentinel.other_kwarg,
read_preference=sentinel.read_preference)]
assert self._do_read_retry.call_args_list == [call(sentinel.symbol, sentinel.meta2,
sentinel.from_version,
date_range=sentinel.date_range,
other_kwarg=sentinel.other_kwarg,
read_preference=ReadPreference.PRIMARY)]
示例10: test_write_error_clean_retry
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import OperationFailure [as 别名]
def test_write_error_clean_retry():
write_handler = Mock(write=Mock(__name__=""))
write_handler.write.side_effect = [OperationFailure("mongo failure"), None]
vs = create_autospec(VersionStore, instance=True,
_collection=Mock(),
_version_nums=Mock(find_one_and_update=Mock(return_value={'version': 1})),
_versions=Mock(insert_one=Mock(__name__="insert_one"), find_one=Mock(__name__="find_one")),
_arctic_lib=create_autospec(ArcticLibraryBinding,
arctic=create_autospec(Arctic, mongo_host='some_host')))
vs._insert_version = lambda version: VersionStore._insert_version(vs, version)
vs._collection.database.connection.nodes = []
vs._write_handler.return_value = write_handler
VersionStore.write(vs, 'sym', sentinel.data, prune_previous_version=False)
assert vs._version_nums.find_one_and_update.call_count == 2
assert vs._versions.find_one.call_count == 2
assert write_handler.write.call_count == 2
assert vs._versions.insert_one.call_count == 1
示例11: test_append_error_clean_retry
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import OperationFailure [as 别名]
def test_append_error_clean_retry():
read_handler = Mock(append=Mock(__name__=""))
read_handler.append.side_effect = [OperationFailure("mongo failure"), None]
previous_version = TPL_VERSION.copy()
previous_version['version'] = 1
vs = create_autospec(VersionStore, instance=True,
_collection=Mock(),
_version_nums=Mock(find_one_and_update=Mock(return_value={'version': previous_version['version']+1})),
_versions=Mock(insert_one=Mock(__name__="insert_one"), find_one=Mock(__name__="find_one", return_value=previous_version)),
_arctic_lib=create_autospec(ArcticLibraryBinding,
arctic=create_autospec(Arctic, mongo_host='some_host')))
vs._insert_version = lambda version: VersionStore._insert_version(vs, version)
vs._collection.database.connection.nodes = []
vs._read_handler.return_value = read_handler
VersionStore.append(vs, 'sym', [1, 2, 3], prune_previous_version=False, upsert=False)
assert vs._version_nums.find_one_and_update.call_count == 2
assert vs._versions.find_one.call_count == 2
assert read_handler.append.call_count == 2
assert vs._versions.insert_one.call_count == 1
示例12: test_append_insert_version_operror
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import OperationFailure [as 别名]
def test_append_insert_version_operror():
read_handler = Mock(append=Mock(__name__=""))
previous_version = TPL_VERSION.copy()
previous_version['version'] = 1
vs = create_autospec(VersionStore, instance=True,
_collection=Mock(),
_version_nums=Mock(find_one_and_update=Mock(return_value={'version': previous_version['version']+1})),
_versions=Mock(insert_one=Mock(__name__="insert_one"), find_one=Mock(__name__="find_one", return_value=previous_version)),
_arctic_lib=create_autospec(ArcticLibraryBinding,
arctic=create_autospec(Arctic, mongo_host='some_host')))
vs._insert_version = lambda version: VersionStore._insert_version(vs, version)
vs._versions.insert_one.side_effect = [OperationFailure("mongo op error"), None]
vs._collection.database.connection.nodes = []
vs._read_handler.return_value = read_handler
VersionStore.append(vs, 'sym', [1, 2, 3], prune_previous_version=False, upsert=False)
assert vs._version_nums.find_one_and_update.call_count == 1
assert vs._versions.find_one.call_count == 1
assert read_handler.append.call_count == 1
assert vs._versions.insert_one.call_count == 2
示例13: test_ArcticTransaction_writes_if_base_data_corrupted
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import OperationFailure [as 别名]
def test_ArcticTransaction_writes_if_base_data_corrupted():
vs = Mock(spec=VersionStore)
ts1 = pd.DataFrame(index=[1, 2], data={'a': [1.0, 2.0]})
vs.read.side_effect = OperationFailure('some failure')
vs.write.return_value = VersionedItem(symbol=sentinel.symbol, library=sentinel.library, version=2,
metadata=None, data=None, host=sentinel.host)
vs.read_metadata.return_value = VersionedItem(symbol=sentinel.symbol, library=sentinel.library, version=1,
metadata=None, data=None, host=sentinel.host)
vs.list_versions.return_value = [{'version': 2}, {'version': 1}]
with ArcticTransaction(vs, sentinel.symbol, sentinel.user, sentinel.log) as cwb:
cwb.write(sentinel.symbol, ts1, metadata={1: 2})
vs.write.assert_called_once_with(sentinel.symbol, ANY, prune_previous_version=True, metadata={1: 2})
assert vs.list_versions.call_args_list == [call(sentinel.symbol)]
示例14: write_command
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import OperationFailure [as 别名]
def write_command(self, request_id, msg, docs):
"""A proxy for SocketInfo.write_command that handles event publishing.
"""
if self.publish:
duration = datetime.datetime.now() - self.start_time
self._start(request_id, docs)
start = datetime.datetime.now()
try:
reply = self.sock_info.write_command(request_id, msg)
if self.publish:
duration = (datetime.datetime.now() - start) + duration
self._succeed(request_id, reply, duration)
except OperationFailure as exc:
if self.publish:
duration = (datetime.datetime.now() - start) + duration
self._fail(request_id, exc.details, duration)
raise
finally:
self.start_time = datetime.datetime.now()
return reply
示例15: _refresh
# 需要导入模块: from pymongo import errors [as 别名]
# 或者: from pymongo.errors import OperationFailure [as 别名]
def _refresh(self):
"""Refreshes the cursor with more data from the server.
Returns the length of self.__data after refresh. Will exit early if
self.__data is already non-empty. Raises OperationFailure when the
cursor cannot be refreshed due to an error on the query.
"""
if len(self.__data) or self.__killed:
return len(self.__data)
if self.__id: # Get More
dbname, collname = self.__ns.split('.', 1)
self.__send_message(
_GetMore(dbname,
collname,
self.__batch_size,
self.__id,
self.__collection.codec_options))
else: # Cursor id is zero nothing else to return
self.__killed = True
return len(self.__data)