Skip to content

Commit

Permalink
attempt to support es6 with es7 client
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerry committed Aug 26, 2021
1 parent 9aa0cd6 commit 4e10c03
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 39 deletions.
50 changes: 13 additions & 37 deletions biothings/web/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,28 @@
import elasticsearch
import elasticsearch_dsl
import requests
from biothings.utils.common import run_once
from elasticsearch import AIOHttpConnection
from elasticsearch import RequestsHttpConnection as _Conn
from requests_aws4auth import AWS4Auth
from tornado.ioloop import IOLoop

logger = logging.getLogger(__name__)

_should_log = run_once()

class ESPackageInfo():

def __init__(self):
self.es_ver = elasticsearch.__version__
self.es_dsl_ver = elasticsearch_dsl.__version__
logger.info("Python Elasticsearch Version: %s",
'.'.join(map(str, self.es_dsl_ver)))
logger.info("Python Elasticsearch DSL Version: %s",
'.'.join(map(str, self.es_dsl_ver)))

if self.es_ver[0] != self.es_dsl_ver[0]:
logger.error("ES Pacakge Version Mismatch with ES-DSL.")

def is_compatible(self, version):
assert isinstance(version, str)
major_version = version.split('.')[0]
assert major_version.isdigit()
return int(major_version) == self.es_ver[0]


es_local = None

def _log_pkg():
es_ver = elasticsearch.__version__
es_dsl_ver = elasticsearch_dsl.__version__
logger.info("Elasticsearch Package Version: %s",
'.'.join(map(str, es_ver)))
logger.info("Elasticsearch DSL Package Version: %s",
'.'.join(map(str, es_dsl_ver)))

def _log_db(client, uri):
logger.info(client)


def _log_es(client, hosts):
_log_db(client, hosts)

Expand All @@ -59,24 +46,13 @@ async def log_cluster(async_client):
# there could be a number of es tasks scheduled before
# this call and would take the cluster a while to respond

if _should_log():
_log_pkg()

cluster_name = cluster['cluster_name']
version = cluster['version']['number']

global es_local
if not es_local:
es_local = ESPackageInfo()

if es_local.is_compatible(version):
level = logging.INFO
suffix = ""
else: # package version mismatch
level = logging.WARNING
suffix = "(Incompatible)"

logger.log(
level, 'Elasticsearch [%s] %s: %s %s',
hosts, cluster_name, version, suffix
)
logger.info('%s: %s %s', hosts, cluster_name, version)
IOLoop.current().add_callback(log_cluster, client)


Expand Down
4 changes: 2 additions & 2 deletions biothings/web/services/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ class _ESIndexMappings:
"""

def __init__(self, mapping):
# assume package version corresponds to that of es
if mapping and elasticsearch.__version__[0] < 7:
# for elasticsearch version 6.x
if len(mapping) == 1 and next(iter(mapping)) != "properties":
# remove doc_type, support 1 type per index
mapping = next(iter(mapping.values()))
self.enabled = mapping.pop('enabled', True)
Expand Down

0 comments on commit 4e10c03

Please sign in to comment.