def test_exception_safe_to_retry(self): x = ElasticsearchBackend(app=self.app) assert not x.exception_safe_to_retry(Exception("failed")) assert not x.exception_safe_to_retry(BaseException("failed")) assert x.exception_safe_to_retry(exceptions.ConflictError(409, "concurrent update", {})) assert x.exception_safe_to_retry(exceptions.ConnectionError(503, "service unavailable", {})) assert x.exception_safe_to_retry(exceptions.TransportError(429, "too many requests", {})) assert not x.exception_safe_to_retry(exceptions.NotFoundError(404, "not found", {}))
def put_template(self, *args, **kwargs): assert 'name' in kwargs and 'body' in kwargs name = kwargs['name'] if self.name is None: self.name = name else: assert self.name == name body = kwargs['body'] if self.body is None: self.body = body else: assert self.body == body if self.behavior: behavior = self.behavior.pop(0) if behavior == "exception": raise MockException() elif behavior == "ce": raise es_excs.ConnectionError(None, "fake ce", Exception()) elif behavior in ("500", "501", "502", "503", "504"): raise es_excs.TransportError( int(behavior), "fake 50x", Exception() ) return None
def create_db(): """Create the Elasticsearch index for Annotations and Documents.""" # Check for required plugin(s) _ensure_es_plugins(es.conn) models = [Annotation, Document] mappings = {} analysis = {} # Collect the mappings and analysis settings for model in models: mappings.update(model.get_mapping()) for section, items in model.get_analysis().items(): existing_items = analysis.setdefault(section, {}) for name in items: if name in existing_items: fmt = "Duplicate definition of 'index.analysis.{}.{}'." msg = fmt.format(section, name) raise RuntimeError(msg) existing_items.update(items) # Create the index try: # Pylint issue #258: https://bitbucket.org/logilab/pylint/issue/258 # # pylint: disable=unexpected-keyword-arg response = es.conn.indices.create(es.index, ignore=400, body={ 'mappings': mappings, 'settings': { 'analysis': analysis }, }) except elasticsearch_exceptions.ConnectionError as e: msg = ('Can not access Elasticsearch at {0}! ' 'Check to ensure it is running.').format(es.host) raise elasticsearch_exceptions.ConnectionError('N/A', msg, e) # Bad request (400) is ignored above, to prevent warnings in the log, but # the failure could be for reasons other than that the index exists. If so, # raise the error here. if 'error' in response and 'IndexAlreadyExists' not in response['error']: raise elasticsearch_exceptions.RequestError(400, response['error']) # Update analysis settings settings = es.conn.indices.get_settings(index=es.index) existing = settings[es.index]['settings']['index'].get('analysis', {}) if existing != analysis: try: es.conn.indices.close(index=es.index) es.conn.indices.put_settings(index=es.index, body={'analysis': analysis}) finally: es.conn.indices.open(index=es.index) # Update mappings try: for doc_type, body in mappings.items(): es.conn.indices.put_mapping(index=es.index, doc_type=doc_type, body=body) except elasticsearch_exceptions.RequestError as e: if e.error.startswith('MergeMappingException'): date = time.strftime('%Y-%m-%d') message = ("Elasticsearch index mapping is incorrect! Please " "reindex it. For example, run: " "./bin/hypothesis reindex {0} {1} {1}-{2}".format( 'yourconfig.ini', es.index, date)) log.critical(message) raise RuntimeError(message) raise