<commit_before>from sqlalchemy import create_engine
from sqlalchemy.exc import OperationalError
from sqlalchemy.orm import sessionmaker

import logger
import model


class SmartClient(object):
    engines = []
    log = logger.get_logger(__file__)

    @staticmethod
    def parse_db_uri(db_uri):
        if "cockroachdb://" in db_uri and db_uri.count(",") > 1:
            return _MultipleEndpoints
        return _SingleEndpoint

    def __new__(cls, db_uri):
        o = object.__new__(cls.parse_db_uri(db_uri))
        return o

    def __init__(self, db_uri):
        self._create_engines(db_uri.split(","))

    def _create_engines(self, uri_list):
        for single_uri in uri_list:
            e = create_engine(single_uri)
            if e.url not in [k.url for k in self.engines]:
                self.log.info("%s %s" % (e.driver, e.url))
                self.engines.append(e)
        self.log.info("total: %d" % len(self.engines))

    def create_conn_with_session(self):
        conn = self.get_engine_connection()
        Session = sessionmaker(bind=conn)
        session = Session(bind=conn)
        return conn, session

    def get_engine_connection(self):
        for engine in self.engines:
            try:
                conn = engine.connect()
                return conn
            except OperationalError:
                self.log.error("could not connect to %s" % engine.url)
        raise ConnectionError(",".join(["%s" % k.url for k in self.engines]))

    def create_base(self):
        model.Base.metadata.create_all(self.get_engine_connection())


class _SingleEndpoint(SmartClient):
    pass


class _MultipleEndpoints(SmartClient):
    pass
<commit_msg>Randomize the engines to LB<commit_after>import random
import time

from sqlalchemy import create_engine
from sqlalchemy.exc import OperationalError
from sqlalchemy.orm import sessionmaker

import logger
import model


class SmartClient(object):
    engines = []
    log = logger.get_logger(__file__)

    @staticmethod
    def parse_db_uri(db_uri):
        if "cockroachdb://" in db_uri and db_uri.count(",") > 1:
            return _MultipleEndpoints
        return _SingleEndpoint

    def __new__(cls, db_uri):
        o = object.__new__(cls.parse_db_uri(db_uri))
        return o

    def __init__(self, db_uri):
        self._create_engines(db_uri.split(","))

    def _create_engines(self, uri_list):
        for single_uri in uri_list:
            e = create_engine(single_uri)
            if e.url not in [k.url for k in self.engines]:
                self.log.info("%s %s" % (e.driver, e.url))
                self.engines.append(e)
        self.log.info("total: %d" % len(self.engines))

    def create_conn_with_session(self):
        conn = self.get_engine_connection()
        Session = sessionmaker(bind=conn)
        session = Session(bind=conn)
        return conn, session

    def get_engine_connection(self):
        if time.time() % 60 == 0:
            random.shuffle(self.engines)
        for engine in self.engines:
            try:
                conn = engine.connect()
                return conn
            except OperationalError:
                self.log.error("could not connect to %s" % engine.url)
        raise ConnectionError(",".join(["%s" % k.url for k in self.engines]))

    def create_base(self):
        model.Base.metadata.create_all(self.get_engine_connection())


class _SingleEndpoint(SmartClient):
    pass


class _MultipleEndpoints(SmartClient):
    pass

{'fraction_non_alphanumeric': 0.09988551803091013, 'fraction_numerical': 0.0014310246136233543, 'mean_word_length': 2.4880239520958085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7269082', 'n_tokens_mistral': 1081, 'n_tokens_neox': 1042, 'n_words': 249}