Fixed bug with very long dns query lifetime

This commit is contained in:
Denis Zheleztsov 2016-11-11 21:13:40 +03:00
parent 7550c5ebb4
commit c33073bdf5

View File

@ -1,37 +1,26 @@
import dns.resolver import dns.resolver
import dns.query import dns.query
from dns.exception import DNSException from dns.exception import DNSException
import logging from .logger import info, warning, error, debug
import sys import sys
# Logger configuration
# This need to be moved
def get_logger():
# Configure logging
FORMAT = '%(asctime) %(message)s'
logging.basicConfig(format=FORMAT)
logger = logging.getLogger(__name__)
return logger
# Resolve service from mesos-dns SRV record # Resolve service from mesos-dns SRV record
# return dict {"servicename": [{"name": "service.f.q.d.n.", "port": 9999}]} # return dict {"servicename": [{"name": "service.f.q.d.n.", "port": 9999}]}
def resolve(app, conf): def resolve(app, conf):
hosts = {} hosts = {}
services = app['services'] services = app['services']
domain = conf['domain'] domain = conf['domain']
logger = get_logger()
for service in services: for service in services:
hosts[service['name']] = {} hosts[service['name']] = {}
group = get_group(service, app) group = get_group(service, app)
if group is False: if group is False:
logger.error('Group is not defined in config, SUROK_DISCOVERY_GROUP and MARATHON_APP_ID') error('Group is not defined in config, SUROK_DISCOVERY_GROUP and MARATHON_APP_ID')
logger.error('Not in Mesos launch?') error('Not in Mesos launch?')
sys.exit(2) sys.exit(2)
# Port name from app config # Port name from app config
ports = None ports = None
try: try:
@ -46,7 +35,7 @@ def resolve(app, conf):
for port_name in ports: for port_name in ports:
fqdn = '_' + port_name + '.' + '_' + service['name'] + '.' + group + '._tcp.' + domain fqdn = '_' + port_name + '.' + '_' + service['name'] + '.' + group + '._tcp.' + domain
hosts[service['name']][port_name] = do_query(fqdn, conf['loglevel']) hosts[service['name']][port_name] = do_query(fqdn, conf['loglevel'])
else: else:
fqdn = '_' + service['name'] + '.' + group + '._tcp.' + domain fqdn = '_' + service['name'] + '.' + group + '._tcp.' + domain
hosts[service['name']] = do_query(fqdn, conf['loglevel']) hosts[service['name']] = do_query(fqdn, conf['loglevel'])
@ -56,11 +45,12 @@ def resolve(app, conf):
# Do SRV queries # Do SRV queries
# Return array: [{"name": "f.q.d.n", "port": 8876}] # Return array: [{"name": "f.q.d.n", "port": 8876}]
def do_query(fqdn, loglevel): def do_query(fqdn, loglevel):
logger = get_logger()
servers = [] servers = []
try: try:
query = dns.resolver.query(fqdn, 'SRV') resolver = dns.resolver.Resolver()
query.lifetime = 1.0 resolver.lifetime = 1
resolver.timeout = 1
query = resolver.query(fqdn, 'SRV')
for rdata in query: for rdata in query:
info = str(rdata).split() info = str(rdata).split()
@ -68,7 +58,7 @@ def do_query(fqdn, loglevel):
servers.append(server) servers.append(server)
except DNSException as e: except DNSException as e:
if loglevel != 'info': if loglevel != 'info':
logger.error("Could not resolve " + fqdn + ': ' + str(e)) error("Could not resolve " + fqdn + ': ' + str(e))
return servers return servers
@ -101,6 +91,6 @@ def parse_marathon_app_id(marathon_app_id):
group = group + marathon_app_id[counter] group = group + marathon_app_id[counter]
if counter != i + 1: if counter != i + 1:
group += '.' group += '.'
counter -= 1 counter -= 1
return group return group