예제 #1
0
def wait_for_slave_gtid(server, gtids, timeout=0):
    """Wait until a slave executes GITDs.

    The function `SELECT WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS` is called until the
    slave catches up. If the timeout period expires prior to achieving
    the condition the :class:`~mysql.fabric.errors.TimeoutError` exception is
    raised. If any thread is stopped, the
    :class:`~mysql.fabric.errors.DatabaseError` exception is raised.

    :param server: MySQL Server.
    :param gtids: Gtid information.
    :param timeout: Timeout for waiting for slave to catch up.
    """
    # Check servers for GTID support
    if not server.gtid_enabled:
        raise _errors.ProgrammingError(
            "Global Transaction IDs are not supported.")

    res = server.exec_stmt(_GTID_WAIT, {"params": (gtids, timeout)})

    if res is None or res[0] is None or res[0][0] is None:
        raise _errors.DatabaseError("Error waiting for slave to catch up. "
                                    "GTID (%s)." % (gtids, ))
    elif res[0][0] == -1:
        raise _errors.TimeoutError("Error waiting for slave to catch up. "
                                   "GTID (%s)." % (gtids, ))

    assert (res[0][0] > -1)
예제 #2
0
def wait_for_slave(server, binlog_file, binlog_pos, timeout=0):
    """Wait for the slave to read the master's binlog up to a specified
    position.

    This methods call the MySQL function `SELECT MASTER_POS_WAIT`. If
    the timeout period expires prior to achieving the condition the
    :class:`~mysql.fabric.errors.TimeoutError` exception is raised. If any
    thread is stopped, the :class:`~mysql.fabric.errors.DatabaseError`
    exception is raised.

    :param server: MySQL Server.
    :param binlog_file: Master's binlog file.
    :param binlog_pos: Master's binlog file position.
    :param timeout: Maximum number of seconds to wait for the condition to
                    be achieved.
    """
    # Wait for slave to read the master log file
    res = server.exec_stmt(_MASTER_POS_WAIT,
                           {"params": (binlog_file, binlog_pos, timeout)})

    if res is None or res[0] is None or res[0][0] is None:
        raise _errors.DatabaseError(
            "Error waiting for slave to catch up. Binary log (%s, %s)." %
            (binlog_file, binlog_pos))
    elif res[0][0] == -1:
        raise _errors.TimeoutError(
            "Error waiting for slave to catch up. Binary log (%s, %s)." %
            (binlog_file, binlog_pos))

    assert (res[0][0] > -1)
예제 #3
0
def wait_for_slave_status_thread(server, thread, status, timeout=None):
    """Wait until a slave's thread exhibits a status.

    The status is a sub-string of the current status: Slave_IO_state or
    Slave_SQL_Running_State.

    If timeout is None, one waits indefinitely until the condition is
    achieved. If the timeout period expires prior to achieving the
    condition the exception TimeoutError is raised.

    :param server: MySQL Server.
    :param thread: Which thread should be checked.
    :type thread: `SQL_THREAD` or `IO_THREAD`.
    :status: Which status should be checked.
    :type status: string.
    :param timeout: Number of seconds one waits until the condition is
                    achieved. If it is None, one waits indefinitely.
    """
    while (timeout is None or timeout > 0) and \
           not _check_status_condition(server, thread, status):
        time.sleep(1)
        timeout = timeout - 1 if timeout is not None else None
    if not _check_status_condition(server, thread, status):
        raise _errors.TimeoutError(
            "Error waiting for slave's thread (%s) to exhibit status (%s)." %
            (thread, status))
예제 #4
0
def wait_for_slave_thread(server,
                          timeout=None,
                          wait_for_running=True,
                          threads=None):
    """Wait until slave's threads stop or start.

    If timeout is None, one waits indefinitely until the condition is
    achieved. If the timeout period expires prior to achieving the
    condition the exception TimeoutError is raised.

    :param server: MySQL Server.
    :param timeout: Number of seconds one waits until the condition is
                    achieved. If it is None, one waits indefinitely.
    :param wait_for_running: If one should check whether threads are
                             running or stopped.
    :type check_if_running: Bool
    :param threads: Which threads should be checked.
    :type threads: `SQL_THREAD` or `IO_THREAD`.
    """
    while (timeout is None or timeout > 0) and \
           not _check_condition(server, threads, wait_for_running):
        time.sleep(1)
        timeout = timeout - 1 if timeout is not None else None
    if not _check_condition(server, threads, wait_for_running):
        raise _errors.TimeoutError(
            "Error waiting for slave's thread(s) to either start or stop.")