<commit_before>import logging
import MySQLdb
import hiredis
from redistrib.exceptions import RedisStatusError

import db
import errors
import models.cluster as clu

COL_ID = 0
COL_HOST = 1
COL_PORT = 2
COL_MEM = 3
COL_STAT = 4
COL_CLUSTER_ID = 5
COL_OCCUPIER_ID = 5
STATUS_ONLINE = 0
STATUS_MISSING = -1
STATUS_BROKEN = -2


def list_all_nodes(client):
    client.execute('''SELECT * FROM `redis_node`''')
    return client.fetchall()


def create_instance(client, host, port, max_mem):
    client.execute('''INSERT INTO `redis_node`
        (`host`, `port`, `max_mem`, `status`, `assignee_id`)
        VALUES (%s, %s, %s, 0, null)''', (host, port, max_mem))


def delete_free_instance(client, host, port):
    client.execute('''DELETE FROM `redis_node` WHERE
        `host`=%s AND `port`=%s AND
        ISNULL(`assignee_id`) AND ISNULL(`occupier_id`)''', (host, port))


def flag_instance(instance_id, status):
    with db.update() as client:
        client.execute('''UPDATE `redis_node` SET `status`=%s
                       WHERE `id`=%s''', (status, instance_id))
    logging.info('Instance %d flaged as %s', instance_id, status)


def _update_status(client, instance_id, max_mem):
    client.execute('''UPDATE `redis_node`
        SET `status`=0, `max_mem`=%s WHERE `id`=%s''', (max_mem, instance_id))


def _remove(client, instance_id):
    client.execute('''DELETE FROM `redis_node` WHERE `id`=%s''',
                   (instance_id,))


def _pick_by_cluster(client, cluster_id):
    client.execute('''SELECT * FROM `redis_node`
        WHERE `assignee_id`=%s LIMIT 1''', cluster_id)
    return client.fetchone()


def pick_by(client, host, port):
    client.execute('''SELECT * FROM `redis_node`
        WHERE `host`=%s AND `port`=%s LIMIT 1''', (host, port))
    return client.fetchone()


def lock_instance(instance_id, cluster_id):
    try:
        with db.update() as client:
            client.execute('''UPDATE `redis_node` SET `occupier_id`=%s
                WHERE `id`=%s AND
                ISNULL(`occupier_id`)''', (cluster_id, instance_id))
        logging.info('Application %d locked instance %d',
                     cluster_id, instance_id)
    except MySQLdb.IntegrityError:
        logging.info('Application %d occupying, raise', cluster_id)
        raise errors.AppMutexError()

    r = True
    try:
        with db.query() as client:
            client.execute('''SELECT `id` FROM `redis_node`
                WHERE `id`=%s AND `occupier_id`=%s''',
                (instance_id, cluster_id))
            r = client.fetchone() is not None
    except:
        unlock_instance(instance_id)
        raise

    if not r:
        raise errors.AppMutexError()


def unlock_instance(instance_id):
    with db.update() as client:
        client.execute('''UPDATE `redis_node` SET `occupier_id`=NULL
            WHERE `id`=%s''', (instance_id,))
    logging.info('Instance released: %d', instance_id)


def distribute_free_to(client, instance_id, cluster_id):
    client.execute(
        '''UPDATE `redis_node` SET `assignee_id`=%s
        WHERE `id`=%s AND ISNULL(`assignee_id`)''',
        (cluster_id, instance_id))


def _distribute_to(instance_id, cluster_id):
    with db.update() as client:
        distribute_free_to(client, instance_id, cluster_id)


def _free_instance(instance_id, cluster_id):
    with db.update() as client:
        client.execute('''UPDATE `redis_node` SET `assignee_id`=NULL
            WHERE `id`=%s AND `assignee_id`=%s''', (instance_id, cluster_id))


def _get_id_from_app_or_none(client, app_name):
    client.execute('''SELECT `id` FROM `cluster`
        WHERE `app_name`=%s LIMIT 1''', app_name)
    r = client.fetchone()
    return None if r is None else r[0]


def _get_id_from_app(client, app_name):
    r = _get_id_from_app_or_none(client, app_name)
    if r is not None:
        return r

    client.execute('''INSERT INTO `cluster` (`app_name`) VALUES (%s)''',
                   (app_name,))
    return client.lastrowid


def pick_and_launch(host, port, cluster_id, start_cluster):
    logging.info('Launching cluster for [ %d ]', cluster_id)
    with db.update() as client:
        instance = pick_by(client, host, port)

    if instance is None:
        raise ValueError('No such node')

    if instance[COL_CLUSTER_ID] is not None:
        raise errors.AppMutexError()

    lock_instance(instance[COL_ID], cluster_id)<commit_msg>only arg to tuple<commit_after>import logging
import MySQLdb
import hiredis
from redistrib.exceptions import RedisStatusError

import db
import errors
import models.cluster as clu

COL_ID = 0
COL_HOST = 1
COL_PORT = 2
COL_MEM = 3
COL_STAT = 4
COL_CLUSTER_ID = 5
COL_OCCUPIER_ID = 5
STATUS_ONLINE = 0
STATUS_MISSING = -1
STATUS_BROKEN = -2


def list_all_nodes(client):
    client.execute('''SELECT * FROM `redis_node`''')
    return client.fetchall()


def create_instance(client, host, port, max_mem):
    client.execute('''INSERT INTO `redis_node`
        (`host`, `port`, `max_mem`, `status`, `assignee_id`)
        VALUES (%s, %s, %s, 0, null)''', (host, port, max_mem))


def delete_free_instance(client, host, port):
    client.execute('''DELETE FROM `redis_node` WHERE
        `host`=%s AND `port`=%s AND
        ISNULL(`assignee_id`) AND ISNULL(`occupier_id`)''', (host, port))


def flag_instance(instance_id, status):
    with db.update() as client:
        client.execute('''UPDATE `redis_node` SET `status`=%s
                       WHERE `id`=%s''', (status, instance_id))
    logging.info('Instance %d flaged as %s', instance_id, status)


def _update_status(client, instance_id, max_mem):
    client.execute('''UPDATE `redis_node`
        SET `status`=0, `max_mem`=%s WHERE `id`=%s''', (max_mem, instance_id))


def _remove(client, instance_id):
    client.execute('''DELETE FROM `redis_node` WHERE `id`=%s''',
                   (instance_id,))


def _pick_by_cluster(client, cluster_id):
    client.execute('''SELECT * FROM `redis_node`
        WHERE `assignee_id`=%s LIMIT 1''', (cluster_id,))
    return client.fetchone()


def pick_by(client, host, port):
    client.execute('''SELECT * FROM `redis_node`
        WHERE `host`=%s AND `port`=%s LIMIT 1''', (host, port))
    return client.fetchone()


def lock_instance(instance_id, cluster_id):
    try:
        with db.update() as client:
            client.execute('''UPDATE `redis_node` SET `occupier_id`=%s
                WHERE `id`=%s AND
                ISNULL(`occupier_id`)''', (cluster_id, instance_id))
        logging.info('Application %d locked instance %d',
                     cluster_id, instance_id)
    except MySQLdb.IntegrityError:
        logging.info('Application %d occupying, raise', cluster_id)
        raise errors.AppMutexError()

    r = True
    try:
        with db.query() as client:
            client.execute('''SELECT `id` FROM `redis_node`
                WHERE `id`=%s AND `occupier_id`=%s''',
                (instance_id, cluster_id))
            r = client.fetchone() is not None
    except:
        unlock_instance(instance_id)
        raise

    if not r:
        raise errors.AppMutexError()


def unlock_instance(instance_id):
    with db.update() as client:
        client.execute('''UPDATE `redis_node` SET `occupier_id`=NULL
            WHERE `id`=%s''', (instance_id,))
    logging.info('Instance released: %d', instance_id)


def distribute_free_to(client, instance_id, cluster_id):
    client.execute(
        '''UPDATE `redis_node` SET `assignee_id`=%s
        WHERE `id`=%s AND ISNULL(`assignee_id`)''',
        (cluster_id, instance_id))


def _distribute_to(instance_id, cluster_id):
    with db.update() as client:
        distribute_free_to(client, instance_id, cluster_id)


def _free_instance(instance_id, cluster_id):
    with db.update() as client:
        client.execute('''UPDATE `redis_node` SET `assignee_id`=NULL
            WHERE `id`=%s AND `assignee_id`=%s''', (instance_id, cluster_id))


def _get_id_from_app_or_none(client, app_name):
    client.execute('''SELECT `id` FROM `cluster`
        WHERE `app_name`=%s LIMIT 1''', (app_name,))
    r = client.fetchone()
    return None if r is None else r[0]


def _get_id_from_app(client, app_name):
    r = _get_id_from_app_or_none(client, app_name)
    if r is not None:
        return r

    client.execute('''INSERT INTO `cluster` (`app_name`) VALUES (%s)''',
                   (app_name,))
    return client.lastrowid


def pick_and_launch(host, port, cluster_id, start_cluster):
    logging.info('Launching cluster for [ %d ]', cluster_id)
    with db.update() as client:
        instance = pick_by(client, host, port)

    if instance is None:
        raise ValueError('No such node')

    if instance[COL_CLUSTER_ID] is not None:
        raise errors.AppMutexError()

    lock_instance(instance[COL_ID], cluster_id)
{'fraction_non_alphanumeric': 0.13331819214172155, 'fraction_numerical': 0.0036338859868271633, 'mean_word_length': 3.30029296875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7507084', 'n_tokens_mistral': 3000, 'n_tokens_neox': 2894, 'n_words': 722}