<commit_before>import logging

from math import ceil
from time import mktime, sleep

from DB import DB
from ShardingHandler import ShardingHandler


class ReplsetHandler:
    def __init__(self, host, port, user, password, authdb, max_lag_secs, retries=5):
        self.host         = host
        self.port         = port
        self.user         = user
        self.password     = password
        self.authdb       = authdb
        self.max_lag_secs = max_lag_secs
        self.retries      = retries

        try:
            self.connection = DB(self.host, self.port, self.user, self.password, self.authdb).connection()
        except Exception, e:
            logging.fatal("Could not get DB connection! Error: %s" % e)
            raise e

    def close(self):
        return self.connection.close()

    def get_rs_status(self):
        tries  = 0
        status = None
        while not status and tries < self.retries:
            try:
                status = self.connection['admin'].command("replSetGetStatus")
                if not status:
                    raise e
            except Exception, e:
                logging.debug("Error running command 'replSetGetStatus': %s" % e)
                tries = tries + 1
                sleep(1)
        if not status:
            raise Exception, "Could not get output from command: 'replSetGetStatus' after %i retries!" % self.retries, None
        return status

    def find_desirable_secondary(self):
        rs_status    = self.get_rs_status()
        rs_name      = rs_status['set']
        quorum_count = ceil(len(rs_status['members']) / 2.0)
        secondary    = None
        primary      = None
        for member in rs_status['members']:
            if 'health' in member and member['health'] > 0:
                logging.debug("Found %s: %s/%s with optime %s" % (<commit_msg>debug->error log level<commit_after>import logging

from math import ceil
from time import mktime, sleep

from DB import DB
from ShardingHandler import ShardingHandler


class ReplsetHandler:
    def __init__(self, host, port, user, password, authdb, max_lag_secs, retries=5):
        self.host         = host
        self.port         = port
        self.user         = user
        self.password     = password
        self.authdb       = authdb
        self.max_lag_secs = max_lag_secs
        self.retries      = retries

        try:
            self.connection = DB(self.host, self.port, self.user, self.password, self.authdb).connection()
        except Exception, e:
            logging.fatal("Could not get DB connection! Error: %s" % e)
            raise e

    def close(self):
        return self.connection.close()

    def get_rs_status(self):
        tries  = 0
        status = None
        while not status and tries < self.retries:
            try:
                status = self.connection['admin'].command("replSetGetStatus")
                if not status:
                    raise e
            except Exception, e:
                logging.error("Error running command 'replSetGetStatus': %s" % e)
                tries = tries + 1
                sleep(1)
        if not status:
            raise Exception, "Could not get output from command: 'replSetGetStatus' after %i retries!" % self.retries, None
        return status

    def find_desirable_secondary(self):
        rs_status    = self.get_rs_status()
        rs_name      = rs_status['set']
        quorum_count = ceil(len(rs_status['members']) / 2.0)
        secondary    = None
        primary      = None
        for member in rs_status['members']:
            if 'health' in member and member['health'] > 0:
                logging.debug("Found %s: %s/%s with optime %s" % (
{'fraction_non_alphanumeric': 0.08897276894041521, 'fraction_numerical': 0.0037746023186842814, 'mean_word_length': 1.8450920245398772, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2107980', 'n_tokens_mistral': 1053, 'n_tokens_neox': 1017, 'n_words': 302}