Develop branch update
Develop branch update
This commit is contained in:
@ -18,7 +18,7 @@
|
||||
#include <unistd.h>
|
||||
#include <binlog_common.h>
|
||||
#include <blr_constants.h>
|
||||
#include <log_manager.h>
|
||||
#include <maxscale/log_manager.h>
|
||||
|
||||
/**
|
||||
* @file binlog_common.c - Common binary log code shared between multiple modules
|
||||
|
@ -51,6 +51,8 @@
|
||||
* 11/07/2016 Massimiliano Pinto Added SSL backend support
|
||||
* 22/07/2016 Massimiliano Pinto Added semi_sync replication support
|
||||
* 24/08/2016 Massimiliano Pinto Added slave notification via CS_WAIT_DATA new state
|
||||
* 16/09/2016 Massimiliano Pinto Addition of Start Encription Event description
|
||||
* 08/11/2016 Massimiliano Pinto Added destroyInstance()
|
||||
*
|
||||
* @endverbatim
|
||||
*/
|
||||
@ -59,29 +61,26 @@
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <time.h>
|
||||
#include <service.h>
|
||||
#include <server.h>
|
||||
#include <router.h>
|
||||
#include <atomic.h>
|
||||
#include <utils.h>
|
||||
#include <secrets.h>
|
||||
#include <spinlock.h>
|
||||
#include <blr.h>
|
||||
#include <dcb.h>
|
||||
#include <spinlock.h>
|
||||
#include <housekeeper.h>
|
||||
#include <maxscale/service.h>
|
||||
#include <maxscale/server.h>
|
||||
#include <maxscale/router.h>
|
||||
#include <maxscale/atomic.h>
|
||||
#include <maxscale/utils.h>
|
||||
#include <maxscale/secrets.h>
|
||||
#include <maxscale/spinlock.h>
|
||||
#include "blr.h"
|
||||
#include <maxscale/dcb.h>
|
||||
#include <maxscale/spinlock.h>
|
||||
#include <maxscale/housekeeper.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <skygw_types.h>
|
||||
#include <skygw_utils.h>
|
||||
#include <log_manager.h>
|
||||
#include <maxscale/log_manager.h>
|
||||
|
||||
#include <mysql_client_server_protocol.h>
|
||||
#include <maxscale/protocol/mysql.h>
|
||||
#include <ini.h>
|
||||
#include <sys/stat.h>
|
||||
#include <uuid/uuid.h>
|
||||
#include <maxscale/alloc.h>
|
||||
#include <gw.h>
|
||||
|
||||
static char *version_str = "V2.1.0";
|
||||
|
||||
@ -104,15 +103,15 @@ static void errorReply(ROUTER *instance,
|
||||
error_action_t action,
|
||||
bool *succp);
|
||||
|
||||
static int getCapabilities();
|
||||
static uint64_t getCapabilities(void);
|
||||
static int blr_handler_config(void *userdata, const char *section, const char *name, const char *value);
|
||||
static int blr_handle_config_item(const char *name, const char *value, ROUTER_INSTANCE *inst);
|
||||
static int blr_set_service_mysql_user(SERVICE *service);
|
||||
static int blr_load_dbusers(const ROUTER_INSTANCE *router);
|
||||
static int blr_check_binlog(ROUTER_INSTANCE *router);
|
||||
int blr_read_events_all_events(ROUTER_INSTANCE *router, int fix, int debug);
|
||||
void blr_master_close(ROUTER_INSTANCE *);
|
||||
void blr_free_ssl_data(ROUTER_INSTANCE *inst);
|
||||
static void destroyInstance(ROUTER *instance);
|
||||
|
||||
/** The module object definition */
|
||||
static ROUTER_OBJECT MyObject =
|
||||
@ -125,7 +124,8 @@ static ROUTER_OBJECT MyObject =
|
||||
diagnostics,
|
||||
clientReply,
|
||||
errorReply,
|
||||
getCapabilities
|
||||
getCapabilities,
|
||||
destroyInstance
|
||||
};
|
||||
|
||||
static void stats_func(void *);
|
||||
@ -570,7 +570,7 @@ createInstance(SERVICE *service, char **options)
|
||||
mkdir_rval = mkdir(inst->binlogdir, 0700);
|
||||
if (mkdir_rval == -1)
|
||||
{
|
||||
char err_msg[STRERROR_BUFLEN];
|
||||
char err_msg[MXS_STRERROR_BUFLEN];
|
||||
MXS_ERROR("Service %s, Failed to create binlog directory '%s': [%d] %s",
|
||||
service->name,
|
||||
inst->binlogdir,
|
||||
@ -582,24 +582,13 @@ createInstance(SERVICE *service, char **options)
|
||||
}
|
||||
}
|
||||
|
||||
/* Allocate dbusers for this router here instead of serviceStartPort() */
|
||||
for (SERV_LISTENER *port = service->ports; port; port = port->next)
|
||||
{
|
||||
if ((port->users = mysql_users_alloc()) == NULL)
|
||||
{
|
||||
MXS_ERROR("%s: Error allocating dbusers in createInstance",
|
||||
inst->service->name);
|
||||
free_instance(inst);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Dynamically allocate master_host server struct, not written in any cnf file */
|
||||
if (service->dbref == NULL)
|
||||
{
|
||||
SERVER *server;
|
||||
SSL_LISTENER *ssl_cfg;
|
||||
server = server_alloc("_none_", "MySQLBackend", (int)3306);
|
||||
server = server_alloc("binlog_router_master_host", "_none_", 3306,
|
||||
"MySQLBackend", "MySQLBackendAuth", NULL);
|
||||
if (server == NULL)
|
||||
{
|
||||
MXS_ERROR("%s: Error for server_alloc in createInstance",
|
||||
@ -631,7 +620,6 @@ createInstance(SERVICE *service, char **options)
|
||||
server->server_ssl = ssl_cfg;
|
||||
|
||||
/* Set server unique name */
|
||||
server_set_unique_name(server, "binlog_router_master_host");
|
||||
/* Add server to service backend list */
|
||||
serviceAddBackend(inst->service, server);
|
||||
}
|
||||
@ -678,19 +666,10 @@ createInstance(SERVICE *service, char **options)
|
||||
inst->service->name, inst->binlogdir);
|
||||
}
|
||||
|
||||
/* Set service user or load db users */
|
||||
blr_set_service_mysql_user(inst->service);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
inst->master_state = BLRM_UNCONNECTED;
|
||||
|
||||
/* Try loading dbusers */
|
||||
if (inst->service->ports)
|
||||
{
|
||||
blr_load_dbusers(inst);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -940,9 +919,8 @@ static void freeSession(ROUTER* router_instance,
|
||||
{
|
||||
ROUTER_INSTANCE *router = (ROUTER_INSTANCE *)router_instance;
|
||||
ROUTER_SLAVE *slave = (ROUTER_SLAVE *)router_client_ses;
|
||||
int prev_val;
|
||||
|
||||
prev_val = atomic_add(&router->stats.n_slaves, -1);
|
||||
ss_debug(int prev_val = ) atomic_add(&router->stats.n_slaves, -1);
|
||||
ss_dassert(prev_val > 0);
|
||||
|
||||
/*
|
||||
@ -1117,7 +1095,8 @@ static char *event_names_mariadb10[] =
|
||||
/* New MariaDB 10.x event numbers */
|
||||
"Binlog Checkpoint Event",
|
||||
"GTID Event",
|
||||
"GTID List Event"
|
||||
"GTID List Event",
|
||||
"Start Encryption Event"
|
||||
};
|
||||
|
||||
/**
|
||||
@ -1628,7 +1607,7 @@ errorReply(ROUTER *instance,
|
||||
ROUTER_INSTANCE *router = (ROUTER_INSTANCE *)instance;
|
||||
int error;
|
||||
socklen_t len;
|
||||
char msg[STRERROR_BUFLEN + 1 + 5] = "";
|
||||
char msg[MXS_STRERROR_BUFLEN + 1 + 5] = "";
|
||||
char *errmsg;
|
||||
unsigned long mysql_errno;
|
||||
|
||||
@ -1688,7 +1667,7 @@ errorReply(ROUTER *instance,
|
||||
getsockopt(router->master->fd, SOL_SOCKET, SO_ERROR, &error, &len) == 0 &&
|
||||
error != 0)
|
||||
{
|
||||
char errbuf[STRERROR_BUFLEN];
|
||||
char errbuf[MXS_STRERROR_BUFLEN];
|
||||
sprintf(msg, "%s ", strerror_r(error, errbuf, sizeof(errbuf)));
|
||||
}
|
||||
else
|
||||
@ -1790,9 +1769,9 @@ static void rses_end_locked_router_action(ROUTER_SLAVE *rses)
|
||||
}
|
||||
|
||||
|
||||
static int getCapabilities()
|
||||
static uint64_t getCapabilities(void)
|
||||
{
|
||||
return (int)(RCAP_TYPE_NO_RSESSION | RCAP_TYPE_NO_USERS_INIT);
|
||||
return RCAP_TYPE_NO_RSESSION;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2136,140 +2115,6 @@ blr_handle_config_item(const char *name, const char *value, ROUTER_INSTANCE *ins
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the service user to mysql dbusers (service->users)
|
||||
* via mysql_users_alloc and add_mysql_users_with_host_ipv4
|
||||
* User is added for '%' and 'localhost' hosts
|
||||
*
|
||||
* @param service The current service
|
||||
* @return 0 on success, 1 on failure
|
||||
*/
|
||||
static int
|
||||
blr_set_service_mysql_user(SERVICE *service)
|
||||
{
|
||||
char *dpwd = NULL;
|
||||
char *newpasswd = NULL;
|
||||
char *service_user = NULL;
|
||||
char *service_passwd = NULL;
|
||||
|
||||
if (serviceGetUser(service, &service_user, &service_passwd) == 0)
|
||||
{
|
||||
MXS_ERROR("failed to get service user details for service %s",
|
||||
service->name);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
dpwd = decryptPassword(service->credentials.authdata);
|
||||
|
||||
if (!dpwd)
|
||||
{
|
||||
MXS_ERROR("decrypt password failed for service user %s, service %s",
|
||||
service_user,
|
||||
service->name);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
newpasswd = create_hex_sha1_sha1_passwd(dpwd);
|
||||
|
||||
if (!newpasswd)
|
||||
{
|
||||
MXS_ERROR("create hex_sha1_sha1_password failed for service user %s",
|
||||
service_user);
|
||||
|
||||
MXS_FREE(dpwd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/** Add the service user for % and localhost to all listeners so that
|
||||
* it can always be used. */
|
||||
for (SERV_LISTENER *port = service->ports; port; port = port->next)
|
||||
{
|
||||
add_mysql_users_with_host_ipv4(port->users, service->credentials.name,
|
||||
"%", newpasswd, "Y", "");
|
||||
add_mysql_users_with_host_ipv4(port->users, service->credentials.name,
|
||||
"localhost", newpasswd, "Y", "");
|
||||
}
|
||||
|
||||
MXS_FREE(newpasswd);
|
||||
MXS_FREE(dpwd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load mysql dbusers into (service->users)
|
||||
*
|
||||
* @param router The router instance
|
||||
* @return -1 on failure, 0 for no users found, > 0 for found users
|
||||
*/
|
||||
static int
|
||||
blr_load_dbusers(const ROUTER_INSTANCE *router)
|
||||
{
|
||||
int loaded_total = 0;
|
||||
SERVICE *service;
|
||||
char path[PATH_MAX];
|
||||
service = router->service;
|
||||
|
||||
for (SERV_LISTENER *port = service->ports; port; port = port->next)
|
||||
{
|
||||
sprintf(path, "%s/%s/%s/", router->binlogdir, BLR_DBUSERS_DIR, port->name);
|
||||
|
||||
if (mxs_mkdir_all(path, 0775))
|
||||
{
|
||||
strcat(path, BLR_DBUSERS_FILE);
|
||||
}
|
||||
|
||||
/* Try loading dbusers from configured backends */
|
||||
int loaded = load_mysql_users(port);
|
||||
|
||||
if (loaded < 0)
|
||||
{
|
||||
MXS_ERROR("Unable to load users for service %s", service->name);
|
||||
|
||||
/* Try loading authentication data from file cache */
|
||||
loaded = dbusers_load(port->users, path);
|
||||
|
||||
if (loaded != -1)
|
||||
{
|
||||
MXS_ERROR("Service %s, Listener %s, Using cached credential information file %s.",
|
||||
service->name, port->name, path);
|
||||
}
|
||||
else
|
||||
{
|
||||
MXS_ERROR("Service %s, Listener %s, Unable to read cache credential"
|
||||
" information from %s. No database user added to service users table.",
|
||||
service->name, port->name, path);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* don't update cache if no user was loaded */
|
||||
if (loaded == 0)
|
||||
{
|
||||
MXS_ERROR("Service %s, Listener %s: failed to load any user information."
|
||||
" Authentication will probably fail as a result.",
|
||||
service->name, port->name);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* update cached data */
|
||||
dbusers_save(port->users, path);
|
||||
}
|
||||
}
|
||||
loaded_total += loaded;
|
||||
}
|
||||
|
||||
/* At service start last update is set to USERS_REFRESH_TIME seconds earlier.
|
||||
* This way MaxScale could try reloading users' just after startup
|
||||
*/
|
||||
service->rate_limit.last = time(NULL) - USERS_REFRESH_TIME;
|
||||
service->rate_limit.nloads = 1;
|
||||
|
||||
return loaded_total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a numeric field from a packet of the specified number of bits
|
||||
*
|
||||
@ -2450,3 +2295,65 @@ blr_free_ssl_data(ROUTER_INSTANCE *inst)
|
||||
inst->service->dbref->server->server_ssl = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* destroy binlog server instance
|
||||
*
|
||||
* @param service The service this router instance belongs to
|
||||
*/
|
||||
static void
|
||||
destroyInstance(ROUTER *instance)
|
||||
{
|
||||
ROUTER_INSTANCE *inst = (ROUTER_INSTANCE *) instance;
|
||||
|
||||
MXS_DEBUG("Destroying instance of router %s for service %s",
|
||||
inst->service->routerModule, inst->service->name);
|
||||
|
||||
/* Check whether master connection is active */
|
||||
if (inst->master)
|
||||
{
|
||||
if (inst->master->fd != -1 && inst->master->state == DCB_STATE_POLLING)
|
||||
{
|
||||
blr_master_close(inst);
|
||||
}
|
||||
}
|
||||
|
||||
spinlock_acquire(&inst->lock);
|
||||
|
||||
if (inst->master_state != BLRM_UNCONFIGURED)
|
||||
{
|
||||
inst->master_state = BLRM_SLAVE_STOPPED;
|
||||
}
|
||||
|
||||
if (inst->client)
|
||||
{
|
||||
if (inst->client->state == DCB_STATE_POLLING)
|
||||
{
|
||||
dcb_close(inst->client);
|
||||
inst->client = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Discard the queued residual data */
|
||||
while (inst->residual)
|
||||
{
|
||||
inst->residual = gwbuf_consume(inst->residual, GWBUF_LENGTH(inst->residual));
|
||||
}
|
||||
inst->residual = NULL;
|
||||
|
||||
MXS_INFO("%s is being stopped by MaxScale shudown. Disconnecting from master %s:%d, "
|
||||
"read up to log %s, pos %lu, transaction safe pos %lu",
|
||||
inst->service->name,
|
||||
inst->service->dbref->server->name,
|
||||
inst->service->dbref->server->port,
|
||||
inst->binlog_name, inst->current_pos, inst->binlog_position);
|
||||
|
||||
if (inst->trx_safe && inst->pending_transaction)
|
||||
{
|
||||
MXS_WARNING("%s stopped by shutdown: detected mid-transaction in binlog file %s, "
|
||||
"pos %lu, incomplete transaction starts at pos %lu",
|
||||
inst->service->name, inst->binlog_name, inst->current_pos, inst->binlog_position);
|
||||
}
|
||||
|
||||
spinlock_release(&inst->lock);
|
||||
}
|
||||
|
714
server/modules/routing/binlog/blr.h
Normal file
714
server/modules/routing/binlog/blr.h
Normal file
@ -0,0 +1,714 @@
|
||||
#pragma once
|
||||
#ifndef _BLR_H
|
||||
#define _BLR_H
|
||||
/*
|
||||
* Copyright (c) 2016 MariaDB Corporation Ab
|
||||
*
|
||||
* Use of this software is governed by the Business Source License included
|
||||
* in the LICENSE.TXT file and at www.mariadb.com/bsl.
|
||||
*
|
||||
* Change Date: 2019-07-01
|
||||
*
|
||||
* On the date above, in accordance with the Business Source License, use
|
||||
* of this software will be governed by version 2 or later of the General
|
||||
* Public License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file blr.h - The binlog router header file
|
||||
*
|
||||
* @verbatim
|
||||
* Revision History
|
||||
*
|
||||
* Date Who Description
|
||||
* 02/04/14 Mark Riddoch Initial implementation
|
||||
* 25/05/15 Massimiliano Pinto Added BLRM_SLAVE_STOPPED state
|
||||
* 05/06/15 Massimiliano Pinto Addition of m_errno, m_errmsg fields
|
||||
* 08/06/15 Massimiliano Pinto Modification of MYSQL_ERROR_CODE and MYSQL_ERROR_MSG
|
||||
* 11/05/15 Massimiliano Pinto Added mariadb10_compat to master and slave structs
|
||||
* 12/06/15 Massimiliano Pinto Added mariadb10 new events
|
||||
* 23/06/15 Massimiliano Pinto Addition of MASTER_SERVER_CFG struct
|
||||
* 24/06/15 Massimiliano Pinto Added BLRM_UNCONFIGURED state
|
||||
* 05/08/15 Massimiliano Pinto Initial implementation of transaction safety
|
||||
* 23/10/15 Markus Makela Added current_safe_event
|
||||
* 26/04/16 Massimiliano Pinto Added MariaDB 10.0 and 10.1 GTID event flags detection
|
||||
* 11/07/16 Massimiliano Pinto Added SSL backend support
|
||||
* 22/07/16 Massimiliano Pinto Added Semi-Sync replication support
|
||||
* 24/08/16 Massimiliano Pinto Added slave notification state CS_WAIT_DATA.
|
||||
* State CS_UPTODATE removed.
|
||||
* 01/09/2016 Massimiliano Pinto Added support for ANNOTATE_ROWS_EVENT in COM_BINLOG_DUMP
|
||||
* 16/09/2016 Massimiliano Pinto Addition of MARIADB10_START_ENCRYPTION_EVENT 0xa4
|
||||
*
|
||||
* @endverbatim
|
||||
*/
|
||||
|
||||
#include <maxscale/cdefs.h>
|
||||
#include <maxscale/dcb.h>
|
||||
#include <maxscale/buffer.h>
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
#include <maxscale/memlog.h>
|
||||
#include <maxscale/thread.h>
|
||||
#include <zlib.h>
|
||||
#include <maxscale/protocol/mysql.h>
|
||||
#include <maxscale/secrets.h>
|
||||
|
||||
MXS_BEGIN_DECLS
|
||||
|
||||
#define BINLOG_FNAMELEN 255
|
||||
#define BLR_PROTOCOL "MySQLBackend"
|
||||
#define BINLOG_MAGIC { 0xfe, 0x62, 0x69, 0x6e }
|
||||
#define BINLOG_MAGIC_SIZE 4
|
||||
#define BINLOG_NAMEFMT "%s.%06d"
|
||||
#define BINLOG_NAME_ROOT "mysql-bin"
|
||||
|
||||
#define BINLOG_EVENT_HDR_LEN 19
|
||||
#define BINLOG_EVENT_CRC_ALGO_TYPE 1
|
||||
#define BINLOG_EVENT_CRC_SIZE 4
|
||||
/* BINLOG_EVENT_LEN_OFFSET points to event_size in event_header */
|
||||
#define BINLOG_EVENT_LEN_OFFSET 9
|
||||
#define BINLOG_ENCRYPTION_ALGORYTHM_NAME_LEN 13
|
||||
#define BINLOG_FATAL_ERROR_READING 1236
|
||||
|
||||
/**
|
||||
* Binlog event types
|
||||
*/
|
||||
#define START_EVENT_V3 0x01
|
||||
#define QUERY_EVENT 0x02
|
||||
#define STOP_EVENT 0x03
|
||||
#define ROTATE_EVENT 0x04
|
||||
#define INTVAR_EVENT 0x05
|
||||
#define LOAD_EVENT 0x06
|
||||
#define SLAVE_EVENT 0x07
|
||||
#define CREATE_FILE_EVENT 0x08
|
||||
#define APPEND_BLOCK_EVENT 0x09
|
||||
#define EXEC_LOAD_EVENT 0x0A
|
||||
#define DELETE_FILE_EVENT 0x0B
|
||||
#define NEW_LOAD_EVENT 0x0C
|
||||
#define RAND_EVENT 0x0D
|
||||
#define USER_VAR_EVENT 0x0E
|
||||
#define FORMAT_DESCRIPTION_EVENT 0x0F
|
||||
#define XID_EVENT 0x10
|
||||
#define BEGIN_LOAD_QUERY_EVENT 0x11
|
||||
#define EXECUTE_LOAD_QUERY_EVENT 0x12
|
||||
#define TABLE_MAP_EVENT 0x13
|
||||
#define WRITE_ROWS_EVENTv0 0x14
|
||||
#define UPDATE_ROWS_EVENTv0 0x15
|
||||
#define DELETE_ROWS_EVENTv0 0x16
|
||||
#define WRITE_ROWS_EVENTv1 0x17
|
||||
#define UPDATE_ROWS_EVENTv1 0x18
|
||||
#define DELETE_ROWS_EVENTv1 0x19
|
||||
#define INCIDENT_EVENT 0x1A
|
||||
#define HEARTBEAT_EVENT 0x1B
|
||||
#define IGNORABLE_EVENT 0x1C
|
||||
#define ROWS_QUERY_EVENT 0x1D
|
||||
#define WRITE_ROWS_EVENTv2 0x1E
|
||||
#define UPDATE_ROWS_EVENTv2 0x1F
|
||||
#define DELETE_ROWS_EVENTv2 0x20
|
||||
#define GTID_EVENT 0x21
|
||||
#define ANONYMOUS_GTID_EVENT 0x22
|
||||
#define PREVIOUS_GTIDS_EVENT 0x23
|
||||
|
||||
#define MAX_EVENT_TYPE 0x23
|
||||
|
||||
/* New MariaDB event numbers start from 0xa0 */
|
||||
#define MARIADB_NEW_EVENTS_BEGIN 0xa0
|
||||
#define MARIADB_ANNOTATE_ROWS_EVENT 0xa0
|
||||
/* New MariaDB 10 event numbers start from here */
|
||||
#define MARIADB10_BINLOG_CHECKPOINT_EVENT 0xa1
|
||||
#define MARIADB10_GTID_EVENT 0xa2
|
||||
#define MARIADB10_GTID_GTID_LIST_EVENT 0xa3
|
||||
#define MARIADB10_START_ENCRYPTION_EVENT 0xa4
|
||||
|
||||
#define MAX_EVENT_TYPE_MARIADB10 0xa4
|
||||
|
||||
/* Maximum event type so far */
|
||||
#define MAX_EVENT_TYPE_END MAX_EVENT_TYPE_MARIADB10
|
||||
|
||||
/**
|
||||
* Binlog event flags
|
||||
*/
|
||||
#define LOG_EVENT_BINLOG_IN_USE_F 0x0001
|
||||
#define LOG_EVENT_FORCED_ROTATE_F 0x0002
|
||||
#define LOG_EVENT_THREAD_SPECIFIC_F 0x0004
|
||||
#define LOG_EVENT_SUPPRESS_USE_F 0x0008
|
||||
#define LOG_EVENT_UPDATE_TABLE_MAP_VERSION_F 0x0010
|
||||
#define LOG_EVENT_ARTIFICIAL_F 0x0020
|
||||
#define LOG_EVENT_RELAY_LOG_F 0x0040
|
||||
#define LOG_EVENT_IGNORABLE_F 0x0080
|
||||
#define LOG_EVENT_NO_FILTER_F 0x0100
|
||||
#define LOG_EVENT_MTS_ISOLATE_F 0x0200
|
||||
|
||||
/**
|
||||
* Binlog COM_BINLOG_DUMP flags
|
||||
*/
|
||||
#define BLR_REQUEST_ANNOTATE_ROWS_EVENT 2
|
||||
|
||||
/**
|
||||
* How often to call the binlog status function (seconds)
|
||||
*/
|
||||
#define BLR_STATS_FREQ 60
|
||||
#define BLR_NSTATS_MINUTES 30
|
||||
|
||||
/**
|
||||
* High and Low water marks for the slave dcb. These values can be overriden
|
||||
* by the router options highwater and lowwater.
|
||||
*/
|
||||
#define DEF_LOW_WATER 1000
|
||||
#define DEF_HIGH_WATER 10000
|
||||
|
||||
/**
|
||||
* Default burst sizes for slave catchup
|
||||
*/
|
||||
#define DEF_SHORT_BURST 15
|
||||
#define DEF_LONG_BURST 500
|
||||
#define DEF_BURST_SIZE 1024000 /* 1 Mb */
|
||||
|
||||
/**
|
||||
* master reconnect backoff constants
|
||||
* BLR_MASTER_BACKOFF_TIME The increments of the back off time (seconds)
|
||||
* BLR_MAX_BACKOFF Maximum number of increments to backoff to
|
||||
*/
|
||||
#define BLR_MASTER_BACKOFF_TIME 10
|
||||
#define BLR_MAX_BACKOFF 60
|
||||
|
||||
/* max size for error message returned to client */
|
||||
#define BINLOG_ERROR_MSG_LEN 385
|
||||
|
||||
/* network latency extra wait tme for heartbeat check */
|
||||
#define BLR_NET_LATENCY_WAIT_TIME 1
|
||||
|
||||
/* default heartbeat interval in seconds */
|
||||
#define BLR_HEARTBEAT_DEFAULT_INTERVAL 300
|
||||
|
||||
/* strings and numbers in SQL replies */
|
||||
#define BLR_TYPE_STRING 0xf
|
||||
#define BLR_TYPE_INT 0x03
|
||||
|
||||
/* string len for COM_STATISTICS output */
|
||||
#define BLRM_COM_STATISTICS_SIZE 1000
|
||||
|
||||
/* string len for strerror_r message */
|
||||
#define BLRM_STRERROR_R_MSG_SIZE 128
|
||||
|
||||
/* string len for task message name */
|
||||
#define BLRM_TASK_NAME_LEN 80
|
||||
|
||||
/* string len for temp binlog filename */
|
||||
#define BLRM_BINLOG_NAME_STR_LEN 80
|
||||
|
||||
/* string len for temp binlog filename */
|
||||
#define BLRM_SET_HEARTBEAT_QUERY_LEN 80
|
||||
|
||||
/* string len for master registration query */
|
||||
#define BLRM_MASTER_REGITRATION_QUERY_LEN 255
|
||||
|
||||
/* Read Binlog position states */
|
||||
#define SLAVE_POS_READ_OK 0x00
|
||||
#define SLAVE_POS_READ_ERR 0xff
|
||||
#define SLAVE_POS_READ_UNSAFE 0xfe
|
||||
#define SLAVE_POS_BAD_FD 0xfd
|
||||
#define SLAVE_POS_BEYOND_EOF 0xfc
|
||||
|
||||
/* MariadDB 10 GTID event flags */
|
||||
#define MARIADB_FL_DDL 32
|
||||
#define MARIADB_FL_STANDALONE 1
|
||||
|
||||
/* Saved credential file name's tail */
|
||||
static const char BLR_DBUSERS_DIR[] = "cache/users";
|
||||
static const char BLR_DBUSERS_FILE[] = "dbusers";
|
||||
|
||||
/**
|
||||
* Some useful macros for examining the MySQL Response packets
|
||||
*/
|
||||
#define MYSQL_RESPONSE_OK(buf) (*((uint8_t *)GWBUF_DATA(buf) + 4) == 0x00)
|
||||
#define MYSQL_RESPONSE_EOF(buf) (*((uint8_t *)GWBUF_DATA(buf) + 4) == 0xfe)
|
||||
#define MYSQL_RESPONSE_ERR(buf) (*((uint8_t *)GWBUF_DATA(buf) + 4) == 0xff)
|
||||
#define MYSQL_ERROR_CODE(buf) ((uint8_t *)GWBUF_DATA(buf) + 5)
|
||||
#define MYSQL_ERROR_MSG(buf) ((uint8_t *)GWBUF_DATA(buf) + 7)
|
||||
#define MYSQL_COMMAND(buf) (*((uint8_t *)GWBUF_DATA(buf) + 4))
|
||||
|
||||
/** Possible states of an event sent by the master */
|
||||
enum blr_event_state
|
||||
{
|
||||
BLR_EVENT_DONE, /*< No event being processed */
|
||||
BLR_EVENT_STARTED, /*< The first packet of an event which spans multiple packets
|
||||
* has been received */
|
||||
BLR_EVENT_ONGOING, /*< Other packets of a multi-packet event are being processed */
|
||||
BLR_EVENT_COMPLETE /*< A multi-packet event has been successfully processed
|
||||
* but the router is not yet ready to process another one */
|
||||
};
|
||||
|
||||
/* Master Server configuration struct */
|
||||
typedef struct master_server_config
|
||||
{
|
||||
char *host;
|
||||
unsigned short port;
|
||||
char logfile[BINLOG_FNAMELEN + 1];
|
||||
uint64_t pos;
|
||||
uint64_t safe_pos;
|
||||
char *user;
|
||||
char *password;
|
||||
char *filestem;
|
||||
/* SSL options */
|
||||
char *ssl_key;
|
||||
char *ssl_cert;
|
||||
char *ssl_ca;
|
||||
int ssl_enabled;
|
||||
char *ssl_version;
|
||||
} MASTER_SERVER_CFG;
|
||||
|
||||
/* Config struct for CHANGE MASTER TO options */
|
||||
typedef struct change_master_options
|
||||
{
|
||||
char *host;
|
||||
char *port;
|
||||
char *binlog_file;
|
||||
char *binlog_pos;
|
||||
char *user;
|
||||
char *password;
|
||||
/* SSL options */
|
||||
char *ssl_key;
|
||||
char *ssl_cert;
|
||||
char *ssl_ca;
|
||||
char *ssl_enabled;
|
||||
char *ssl_version;
|
||||
} CHANGE_MASTER_OPTIONS;
|
||||
|
||||
/**
|
||||
* Packet header for replication messages
|
||||
*/
|
||||
typedef struct rep_header
|
||||
{
|
||||
int payload_len; /*< Payload length (24 bits) */
|
||||
uint8_t seqno; /*< Response sequence number */
|
||||
uint8_t ok; /*< OK Byte from packet */
|
||||
uint32_t timestamp; /*< Timestamp - start of binlog record */
|
||||
uint8_t event_type; /*< Binlog event type */
|
||||
uint32_t serverid; /*< Server id of master */
|
||||
uint32_t event_size; /*< Size of header, post-header and body */
|
||||
uint32_t next_pos; /*< Position of next event */
|
||||
uint16_t flags; /*< Event flags */
|
||||
} REP_HEADER;
|
||||
|
||||
/**
|
||||
* The binlog record structure. This contains the actual packet read from the binlog
|
||||
* file.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
unsigned long position; /*< binlog record position for this cache entry */
|
||||
GWBUF *pkt; /*< The packet received from the master */
|
||||
REP_HEADER hdr; /*< The packet header */
|
||||
} BLCACHE_RECORD;
|
||||
|
||||
/**
|
||||
* The binlog cache. A cache exists for each file that hold cached bin log records.
|
||||
* Caches will be used for all files being read by more than 1 slave.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
BLCACHE_RECORD **records; /*< The actual binlog records */
|
||||
int current; /*< The next record that will be inserted */
|
||||
int cnt; /*< The number of records in the cache */
|
||||
SPINLOCK lock; /*< The spinlock for the cache */
|
||||
} BLCACHE;
|
||||
|
||||
typedef struct blfile
|
||||
{
|
||||
char binlogname[BINLOG_FNAMELEN + 1]; /*< Name of the binlog file */
|
||||
int fd; /*< Actual file descriptor */
|
||||
int refcnt; /*< Reference count for file */
|
||||
BLCACHE *cache; /*< Record cache for this file */
|
||||
SPINLOCK lock; /*< The file lock */
|
||||
struct blfile *next; /*< Next file in list */
|
||||
} BLFILE;
|
||||
|
||||
/**
|
||||
* Slave statistics
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
int n_events; /*< Number of events sent */
|
||||
unsigned long n_bytes; /*< Number of bytes sent */
|
||||
int n_bursts; /*< Number of bursts sent */
|
||||
int n_requests; /*< Number of requests received */
|
||||
int n_flows; /*< Number of flow control restarts */
|
||||
int n_queries; /*< Number of SQL queries */
|
||||
int n_upd;
|
||||
int n_cb;
|
||||
int n_cbna;
|
||||
int n_dcb;
|
||||
int n_above;
|
||||
int n_failed_read;
|
||||
int n_overrun;
|
||||
int n_caughtup;
|
||||
int n_actions[3];
|
||||
uint64_t lastsample;
|
||||
int minno;
|
||||
int minavgs[BLR_NSTATS_MINUTES];
|
||||
} SLAVE_STATS;
|
||||
|
||||
typedef enum blr_thread_role
|
||||
{
|
||||
BLR_THREAD_ROLE_MASTER_LARGE_NOTRX,
|
||||
BLR_THREAD_ROLE_MASTER_NOTRX,
|
||||
BLR_THREAD_ROLE_MASTER_TRX,
|
||||
BLR_THREAD_ROLE_SLAVE
|
||||
} blr_thread_role_t;
|
||||
|
||||
#define ROLETOSTR(r) r == BLR_THREAD_ROLE_MASTER_LARGE_NOTRX ? "master (large event, no trx)" : \
|
||||
r == BLR_THREAD_ROLE_MASTER_NOTRX ? "master (no trx)" : \
|
||||
r == BLR_THREAD_ROLE_MASTER_TRX ? "master (trx)" : "slave"
|
||||
|
||||
/**
|
||||
* The client session structure used within this router. This represents
|
||||
* the slaves that are replicating binlogs from MaxScale.
|
||||
*/
|
||||
typedef struct router_slave
|
||||
{
|
||||
#if defined(SS_DEBUG)
|
||||
skygw_chk_t rses_chk_top;
|
||||
#endif
|
||||
DCB *dcb; /*< The slave server DCB */
|
||||
int state; /*< The state of this slave */
|
||||
uint32_t binlog_pos; /*< Binlog position for this slave */
|
||||
char binlogfile[BINLOG_FNAMELEN + 1];
|
||||
/*< Current binlog file for this slave */
|
||||
char *uuid; /*< Slave UUID */
|
||||
#ifdef BLFILE_IN_SLAVE
|
||||
BLFILE *file; /*< Currently open binlog file */
|
||||
#endif
|
||||
int serverid; /*< Server-id of the slave */
|
||||
char *hostname; /*< Hostname of the slave, if known */
|
||||
char *user; /*< Username if given */
|
||||
char *passwd; /*< Password if given */
|
||||
short port; /*< MySQL port */
|
||||
int nocrc; /*< Disable CRC */
|
||||
int overrun;
|
||||
uint32_t rank; /*< Replication rank */
|
||||
uint8_t seqno; /*< Replication dump sequence no */
|
||||
uint32_t lastEventTimestamp;/*< Last event timestamp sent */
|
||||
SPINLOCK catch_lock; /*< Event catchup lock */
|
||||
unsigned int cstate; /*< Catch up state */
|
||||
bool mariadb10_compat;/*< MariaDB 10.0 compatibility */
|
||||
SPINLOCK rses_lock; /*< Protects rses_deleted */
|
||||
pthread_t pthread;
|
||||
struct router_instance
|
||||
*router; /*< Pointer to the owning router */
|
||||
struct router_slave *next;
|
||||
SLAVE_STATS stats; /*< Slave statistics */
|
||||
time_t connect_time; /*< Connect time of slave */
|
||||
char *warning_msg; /*< Warning message */
|
||||
int heartbeat; /*< Heartbeat in seconds */
|
||||
uint8_t lastEventReceived; /*< Last event received */
|
||||
time_t lastReply; /*< Last event sent */
|
||||
// lsi: Last Sent Information
|
||||
blr_thread_role_t lsi_sender_role; /*< Master or slave code sent */
|
||||
THREAD lsi_sender_tid; /*< Who sent */
|
||||
char lsi_binlog_name[BINLOG_FNAMELEN + 1]; /*< Which binlog file */
|
||||
uint32_t lsi_binlog_pos; /*< What position */
|
||||
#if defined(SS_DEBUG)
|
||||
skygw_chk_t rses_chk_tail;
|
||||
#endif
|
||||
} ROUTER_SLAVE;
|
||||
|
||||
|
||||
/**
|
||||
* The statistics for this router instance
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
int n_slaves; /*< Number slave sessions created */
|
||||
int n_reads; /*< Number of record reads */
|
||||
uint64_t n_binlogs; /*< Number of binlog records from master */
|
||||
uint64_t n_binlogs_ses; /*< Number of binlog records from master */
|
||||
uint64_t n_binlog_errors;/*< Number of binlog records from master */
|
||||
uint64_t n_rotates; /*< Number of binlog rotate events */
|
||||
uint64_t n_cachehits; /*< Number of hits on the binlog cache */
|
||||
uint64_t n_cachemisses; /*< Number of misses on the binlog cache */
|
||||
int n_registered; /*< Number of registered slaves */
|
||||
int n_masterstarts; /*< Number of times connection restarted */
|
||||
int n_delayedreconnects;
|
||||
int n_residuals; /*< Number of times residual data was buffered */
|
||||
int n_heartbeats; /*< Number of heartbeat messages */
|
||||
time_t lastReply;
|
||||
uint64_t n_fakeevents; /*< Fake events not written to disk */
|
||||
uint64_t n_artificial; /*< Artificial events not written to disk */
|
||||
int n_badcrc; /*< No. of bad CRC's from master */
|
||||
uint64_t events[MAX_EVENT_TYPE_END + 1]; /*< Per event counters */
|
||||
uint64_t lastsample;
|
||||
int minno;
|
||||
int minavgs[BLR_NSTATS_MINUTES];
|
||||
} ROUTER_STATS;
|
||||
|
||||
/**
|
||||
* Saved responses from the master that will be forwarded to slaves
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
GWBUF *server_id; /*< Master server id */
|
||||
GWBUF *heartbeat; /*< Heartbeat period */
|
||||
GWBUF *chksum1; /*< Binlog checksum 1st response */
|
||||
GWBUF *chksum2; /*< Binlog checksum 2nd response */
|
||||
GWBUF *gtid_mode; /*< GTID Mode response */
|
||||
GWBUF *uuid; /*< Master UUID */
|
||||
GWBUF *setslaveuuid; /*< Set Slave UUID */
|
||||
GWBUF *setnames; /*< Set NAMES latin1 */
|
||||
GWBUF *utf8; /*< Set NAMES utf8 */
|
||||
GWBUF *select1; /*< select 1 */
|
||||
GWBUF *selectver; /*< select version() */
|
||||
GWBUF *selectvercom; /*< select @@version_comment */
|
||||
GWBUF *selecthostname;/*< select @@hostname */
|
||||
GWBUF *map; /*< select @@max_allowed_packet */
|
||||
GWBUF *mariadb10; /*< set @mariadb_slave_capability */
|
||||
uint8_t *fde_event; /*< Format Description Event */
|
||||
int fde_len; /*< Length of fde_event */
|
||||
} MASTER_RESPONSES;
|
||||
|
||||
/**
|
||||
* The per instance data for the router.
|
||||
*/
|
||||
typedef struct router_instance
|
||||
{
|
||||
SERVICE *service; /*< Pointer to the service using this router */
|
||||
ROUTER_SLAVE *slaves; /*< Link list of all the slave connections */
|
||||
SPINLOCK lock; /*< Spinlock for the instance data */
|
||||
char *uuid; /*< UUID for the router to use w/master */
|
||||
int masterid; /*< Set ID of the master, sent to slaves */
|
||||
int serverid; /*< ID for the router to use w/master */
|
||||
int initbinlog; /*< Initial binlog file number */
|
||||
char *user; /*< User name to use with master */
|
||||
char *password; /*< Password to use with master */
|
||||
char *fileroot; /*< Root of binlog filename */
|
||||
bool master_chksum; /*< Does the master provide checksums */
|
||||
bool mariadb10_compat; /*< MariaDB 10.0 compatibility */
|
||||
char *master_uuid; /*< Set UUID of the master, sent to slaves */
|
||||
DCB *master; /*< DCB for master connection */
|
||||
DCB *client; /*< DCB for dummy client */
|
||||
SESSION *session; /*< Fake session for master connection */
|
||||
unsigned int master_state; /*< State of the master FSM */
|
||||
uint8_t lastEventReceived; /*< Last even received */
|
||||
uint32_t lastEventTimestamp; /*< Timestamp from last event */
|
||||
GWBUF *residual; /*< Any residual binlog event */
|
||||
MASTER_RESPONSES saved_master; /*< Saved master responses */
|
||||
char *binlogdir; /*< The directory with the binlog files */
|
||||
SPINLOCK binlog_lock; /*< Lock to control update of the binlog position */
|
||||
int trx_safe; /*< Detect and handle partial transactions */
|
||||
int pending_transaction; /*< Pending transaction */
|
||||
enum blr_event_state master_event_state; /*< Packet read state */
|
||||
uint32_t stored_checksum; /*< The current value of the checksum */
|
||||
uint8_t partial_checksum[MYSQL_CHECKSUM_LEN]; /*< The partial value of the checksum
|
||||
* received from the master */
|
||||
uint8_t partial_checksum_bytes; /*< How many bytes of the checksum we have read */
|
||||
uint64_t checksum_size; /*< Data size for the checksum */
|
||||
REP_HEADER stored_header; /*< Relication header of the event the master is sending */
|
||||
uint64_t last_safe_pos; /* last committed transaction */
|
||||
char binlog_name[BINLOG_FNAMELEN + 1];
|
||||
/*< Name of the current binlog file */
|
||||
uint64_t binlog_position;
|
||||
/*< last committed transaction position */
|
||||
uint64_t current_pos;
|
||||
/*< Current binlog position */
|
||||
int binlog_fd; /*< File descriptor of the binlog
|
||||
* file being written
|
||||
*/
|
||||
uint64_t last_written; /*< Position of the last write operation */
|
||||
uint64_t last_event_pos; /*< Position of last event written */
|
||||
uint64_t current_safe_event;
|
||||
/*< Position of the latest safe event being sent to slaves */
|
||||
char prevbinlog[BINLOG_FNAMELEN + 1];
|
||||
int rotating; /*< Rotation in progress flag */
|
||||
BLFILE *files; /*< Files used by the slaves */
|
||||
SPINLOCK fileslock; /*< Lock for the files queue above */
|
||||
unsigned int low_water; /*< Low water mark for client DCB */
|
||||
unsigned int high_water; /*< High water mark for client DCB */
|
||||
unsigned int short_burst; /*< Short burst for slave catchup */
|
||||
unsigned int long_burst; /*< Long burst for slave catchup */
|
||||
unsigned long burst_size; /*< Maximum size of burst to send */
|
||||
unsigned long heartbeat; /*< Configured heartbeat value */
|
||||
ROUTER_STATS stats; /*< Statistics for this router */
|
||||
int active_logs;
|
||||
int reconnect_pending;
|
||||
int retry_backoff;
|
||||
time_t connect_time;
|
||||
int handling_threads;
|
||||
unsigned long m_errno; /*< master response mysql errno */
|
||||
char *m_errmsg; /*< master response mysql error message */
|
||||
char *set_master_version; /*< Send custom Version to slaves */
|
||||
char *set_master_hostname; /*< Send custom Hostname to slaves */
|
||||
char *set_master_uuid; /*< Send custom Master UUID to slaves */
|
||||
char *set_master_server_id; /*< Send custom Master server_id to slaves */
|
||||
int send_slave_heartbeat; /*< Enable sending heartbeat to slaves */
|
||||
bool ssl_enabled; /*< Use SSL connection to master */
|
||||
int ssl_cert_verification_depth; /*< The maximum length of the certificate
|
||||
* authority chain that will be accepted.
|
||||
*/
|
||||
char *ssl_key; /*< config Certificate Key for Master SSL connection */
|
||||
char *ssl_ca; /*< config CA Certificate for Master SSL connection */
|
||||
char *ssl_cert; /*< config Certificate for Master SSL connection */
|
||||
char *ssl_version; /*< config TLS Version for Master SSL connection */
|
||||
bool request_semi_sync; /*< Request Semi-Sync replication to master */
|
||||
int master_semi_sync; /*< Semi-Sync replication status of master server */
|
||||
struct router_instance *next;
|
||||
} ROUTER_INSTANCE;
|
||||
|
||||
/**
|
||||
* Defines and offsets for binlog encryption
|
||||
*
|
||||
* BLRM_FDE_EVENT_TYPES_OFFSET is the offset in FDE event content that points to
|
||||
* the number of events the master server supports.
|
||||
*/
|
||||
#define BLR_FDE_EVENT_BINLOG_VERSION 2
|
||||
#define BLR_FDE_EVENT_SERVER_VERSION 50
|
||||
#define BLR_FDE_EVENT_BINLOG_TIME 4
|
||||
#define BLR_FDE_EVENT_BINLOG_EVENT_HDR_LEN 1
|
||||
#define BLRM_FDE_EVENT_TYPES_OFFSET (BLR_FDE_EVENT_BINLOG_VERSION + \
|
||||
BLR_FDE_EVENT_SERVER_VERSION + \
|
||||
BLR_FDE_EVENT_BINLOG_TIME + \
|
||||
BLR_FDE_EVENT_BINLOG_EVENT_HDR_LEN)
|
||||
#define BLRM_CRYPTO_SCHEME_LENGTH 1
|
||||
#define BLRM_KEY_VERSION_LENGTH 4
|
||||
#define BLRM_IV_LENGTH AES_BLOCK_SIZE
|
||||
#define BLRM_IV_OFFS_LENGTH 4
|
||||
#define BLRM_NONCE_LENGTH (BLRM_IV_LENGTH - BLRM_IV_OFFS_LENGTH)
|
||||
|
||||
/**
|
||||
* State machine for the master to MaxScale replication
|
||||
*/
|
||||
#define BLRM_UNCONFIGURED 0x0000
|
||||
#define BLRM_UNCONNECTED 0x0001
|
||||
#define BLRM_CONNECTING 0x0002
|
||||
#define BLRM_AUTHENTICATED 0x0003
|
||||
#define BLRM_TIMESTAMP 0x0004
|
||||
#define BLRM_SERVERID 0x0005
|
||||
#define BLRM_HBPERIOD 0x0006
|
||||
#define BLRM_CHKSUM1 0x0007
|
||||
#define BLRM_CHKSUM2 0x0008
|
||||
#define BLRM_MARIADB10 0x0009
|
||||
#define BLRM_GTIDMODE 0x000A
|
||||
#define BLRM_MUUID 0x000B
|
||||
#define BLRM_SUUID 0x000C
|
||||
#define BLRM_LATIN1 0x000D
|
||||
#define BLRM_UTF8 0x000E
|
||||
#define BLRM_SELECT1 0x000F
|
||||
#define BLRM_SELECTVER 0x0010
|
||||
#define BLRM_SELECTVERCOM 0x0011
|
||||
#define BLRM_SELECTHOSTNAME 0x0012
|
||||
#define BLRM_MAP 0x0013
|
||||
#define BLRM_REGISTER 0x0014
|
||||
#define BLRM_CHECK_SEMISYNC 0x0015
|
||||
#define BLRM_REQUEST_SEMISYNC 0x0016
|
||||
#define BLRM_REQUEST_BINLOGDUMP 0x0017
|
||||
#define BLRM_BINLOGDUMP 0x0018
|
||||
#define BLRM_SLAVE_STOPPED 0x0019
|
||||
|
||||
#define BLRM_MAXSTATE 0x0019
|
||||
|
||||
static char *blrm_states[] =
|
||||
{
|
||||
"Unconfigured", "Unconnected", "Connecting", "Authenticated", "Timestamp retrieval",
|
||||
"Server ID retrieval", "HeartBeat Period setup", "binlog checksum config",
|
||||
"binlog checksum rerieval", "Set MariaDB slave capability", "GTID Mode retrieval",
|
||||
"Master UUID retrieval", "Set Slave UUID", "Set Names latin1", "Set Names utf8", "select 1",
|
||||
"select version()", "select @@version_comment", "select @@hostname",
|
||||
"select @@max_allowed_packet", "Register slave", "Semi-Sync Support retrivial",
|
||||
"Request Semi-Sync Replication", "Request Binlog Dump", "Binlog Dump", "Slave stopped"
|
||||
};
|
||||
|
||||
#define BLRS_CREATED 0x0000
|
||||
#define BLRS_UNREGISTERED 0x0001
|
||||
#define BLRS_REGISTERED 0x0002
|
||||
#define BLRS_DUMPING 0x0003
|
||||
#define BLRS_ERRORED 0x0004
|
||||
|
||||
#define BLRS_MAXSTATE 0x0004
|
||||
|
||||
static char *blrs_states[] =
|
||||
{
|
||||
"Created", "Unregistered", "Registered", "Sending binlogs", "Errored"
|
||||
};
|
||||
|
||||
/**
|
||||
* Slave catch-up status
|
||||
*/
|
||||
#define CS_UNDEFINED 0x0000
|
||||
#define CS_EXPECTCB 0x0004
|
||||
#define CS_WAIT_DATA 0x0020
|
||||
#define CS_BUSY 0x0040
|
||||
|
||||
/**
|
||||
* MySQL protocol OpCodes needed for replication
|
||||
*/
|
||||
#define COM_QUIT 0x01
|
||||
#define COM_QUERY 0x03
|
||||
#define COM_STATISTICS 0x09
|
||||
#define COM_PING 0x0e
|
||||
#define COM_REGISTER_SLAVE 0x15
|
||||
#define COM_BINLOG_DUMP 0x12
|
||||
|
||||
/**
|
||||
* Macros to extract common fields
|
||||
*/
|
||||
#define INLINE_EXTRACT 1 /* Set to 0 for debug purposes */
|
||||
|
||||
#if INLINE_EXTRACT
|
||||
#define EXTRACT16(x) (*(uint8_t *)(x) | (*((uint8_t *)(x) + 1) << 8))
|
||||
#define EXTRACT24(x) (*(uint8_t *)(x) | \
|
||||
(*((uint8_t *)(x) + 1) << 8) | \
|
||||
(*((uint8_t *)(x) + 2) << 16))
|
||||
#define EXTRACT32(x) (*(uint8_t *)(x) | \
|
||||
(*((uint8_t *)(x) + 1) << 8) | \
|
||||
(*((uint8_t *)(x) + 2) << 16) | \
|
||||
(*((uint8_t *)(x) + 3) << 24))
|
||||
#else
|
||||
#define EXTRACT16(x) extract_field((x), 16)
|
||||
#define EXTRACT24(x) extract_field((x), 24)
|
||||
#define EXTRACT32(x) extract_field((x), 32)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Externals within the router
|
||||
*/
|
||||
extern void blr_start_master(void *);
|
||||
extern void blr_master_response(ROUTER_INSTANCE *, GWBUF *);
|
||||
extern void blr_master_reconnect(ROUTER_INSTANCE *);
|
||||
extern int blr_master_connected(ROUTER_INSTANCE *);
|
||||
|
||||
extern int blr_slave_request(ROUTER_INSTANCE *, ROUTER_SLAVE *, GWBUF *);
|
||||
extern void blr_slave_rotate(ROUTER_INSTANCE *, ROUTER_SLAVE *, uint8_t *);
|
||||
extern int blr_slave_catchup(ROUTER_INSTANCE *router, ROUTER_SLAVE *slave, bool large);
|
||||
extern void blr_init_cache(ROUTER_INSTANCE *);
|
||||
|
||||
extern int blr_file_init(ROUTER_INSTANCE *);
|
||||
extern int blr_write_binlog_record(ROUTER_INSTANCE *, REP_HEADER *, uint32_t pos, uint8_t *);
|
||||
extern int blr_file_rotate(ROUTER_INSTANCE *, char *, uint64_t);
|
||||
extern void blr_file_flush(ROUTER_INSTANCE *);
|
||||
extern BLFILE *blr_open_binlog(ROUTER_INSTANCE *, char *);
|
||||
extern GWBUF *blr_read_binlog(ROUTER_INSTANCE *, BLFILE *, unsigned long, REP_HEADER *, char *);
|
||||
extern void blr_close_binlog(ROUTER_INSTANCE *, BLFILE *);
|
||||
extern unsigned long blr_file_size(BLFILE *);
|
||||
extern int blr_statistics(ROUTER_INSTANCE *, ROUTER_SLAVE *, GWBUF *);
|
||||
extern int blr_ping(ROUTER_INSTANCE *, ROUTER_SLAVE *, GWBUF *);
|
||||
extern int blr_send_custom_error(DCB *, int, int, char *, char *, unsigned int);
|
||||
extern int blr_file_next_exists(ROUTER_INSTANCE *, ROUTER_SLAVE *);
|
||||
uint32_t extract_field(uint8_t *src, int bits);
|
||||
void blr_cache_read_master_data(ROUTER_INSTANCE *router);
|
||||
int blr_read_events_all_events(ROUTER_INSTANCE *router, int fix, int debug);
|
||||
int blr_save_dbusers(const ROUTER_INSTANCE *router);
|
||||
char *blr_get_event_description(ROUTER_INSTANCE *router, uint8_t event);
|
||||
void blr_file_append(ROUTER_INSTANCE *router, char *file);
|
||||
void blr_cache_response(ROUTER_INSTANCE *router, char *response, GWBUF *buf);
|
||||
char * blr_last_event_description(ROUTER_INSTANCE *router);
|
||||
void blr_free_ssl_data(ROUTER_INSTANCE *inst);
|
||||
|
||||
extern bool blr_send_event(blr_thread_role_t role,
|
||||
const char* binlog_name,
|
||||
uint32_t binlog_pos,
|
||||
ROUTER_SLAVE *slave,
|
||||
REP_HEADER *hdr,
|
||||
uint8_t *buf);
|
||||
|
||||
MXS_END_DECLS
|
||||
|
||||
#endif
|
@ -34,18 +34,16 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <service.h>
|
||||
#include <server.h>
|
||||
#include <router.h>
|
||||
#include <atomic.h>
|
||||
#include <spinlock.h>
|
||||
#include <blr.h>
|
||||
#include <dcb.h>
|
||||
#include <spinlock.h>
|
||||
#include <maxscale/service.h>
|
||||
#include <maxscale/server.h>
|
||||
#include <maxscale/router.h>
|
||||
#include <maxscale/atomic.h>
|
||||
#include <maxscale/spinlock.h>
|
||||
#include "blr.h"
|
||||
#include <maxscale/dcb.h>
|
||||
#include <maxscale/spinlock.h>
|
||||
|
||||
#include <skygw_types.h>
|
||||
#include <skygw_utils.h>
|
||||
#include <log_manager.h>
|
||||
#include <maxscale/log_manager.h>
|
||||
|
||||
|
||||
/**
|
||||
|
@ -35,6 +35,9 @@
|
||||
* 23/10/2015 Markus Makela Added current_safe_event
|
||||
* 26/04/2016 Massimiliano Pinto Added MariaDB 10.0 and 10.1 GTID event flags detection
|
||||
* 11/07/2016 Massimiliano Pinto Added SSL backend support
|
||||
* 16/09/2016 Massimiliano Pinto Addition of IGNORABLE_EVENT in case of a missing event
|
||||
* detected from master binlog stream
|
||||
* 19/09/2016 Massimiliano Pinto START_ENCRYPTION_EVENT is detected by maxbinlocheck.
|
||||
*
|
||||
* @endverbatim
|
||||
*/
|
||||
@ -47,19 +50,18 @@
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <service.h>
|
||||
#include <server.h>
|
||||
#include <router.h>
|
||||
#include <atomic.h>
|
||||
#include <spinlock.h>
|
||||
#include <blr.h>
|
||||
#include <dcb.h>
|
||||
#include <spinlock.h>
|
||||
#include <gwdirs.h>
|
||||
#include <skygw_types.h>
|
||||
#include <skygw_utils.h>
|
||||
#include <log_manager.h>
|
||||
#include <maxscale/service.h>
|
||||
#include <maxscale/server.h>
|
||||
#include <maxscale/router.h>
|
||||
#include <maxscale/atomic.h>
|
||||
#include <maxscale/spinlock.h>
|
||||
#include "blr.h"
|
||||
#include <maxscale/dcb.h>
|
||||
#include <maxscale/spinlock.h>
|
||||
#include <maxscale/gwdirs.h>
|
||||
#include <maxscale/log_manager.h>
|
||||
#include <maxscale/alloc.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
static int blr_file_create(ROUTER_INSTANCE *router, char *file);
|
||||
static void blr_log_header(int priority, char *msg, uint8_t *ptr);
|
||||
@ -70,10 +72,11 @@ int blr_file_write_master_config(ROUTER_INSTANCE *router, char *error);
|
||||
extern uint32_t extract_field(uint8_t *src, int bits);
|
||||
static void blr_format_event_size(double *event_size, char *label);
|
||||
extern int MaxScaleUptime();
|
||||
extern void encode_value(unsigned char *data, unsigned int value, int len);
|
||||
|
||||
typedef struct binlog_event_desc
|
||||
{
|
||||
unsigned long long event_pos;
|
||||
uint64_t event_pos;
|
||||
uint8_t event_type;
|
||||
time_t event_time;
|
||||
} BINLOG_EVENT_DESC;
|
||||
@ -82,6 +85,44 @@ static void blr_print_binlog_details(ROUTER_INSTANCE *router,
|
||||
BINLOG_EVENT_DESC first_event_time,
|
||||
BINLOG_EVENT_DESC last_event_time);
|
||||
|
||||
static uint8_t *blr_create_ignorable_event(uint32_t event_size,
|
||||
REP_HEADER *hdr,
|
||||
uint32_t event_pos,
|
||||
bool do_checksum);
|
||||
static int blr_write_special_event(ROUTER_INSTANCE *router,
|
||||
uint32_t file_offset,
|
||||
uint32_t hole_size,
|
||||
REP_HEADER *hdr,
|
||||
int type);
|
||||
|
||||
/** MaxScale generated events */
|
||||
typedef enum
|
||||
{
|
||||
BLRM_IGNORABLE, /*< Ignorable event */
|
||||
BLRM_START_ENCRYPTION /*< Start Encryption event */
|
||||
} generated_event_t;
|
||||
|
||||
/**
|
||||
* MariaDB 10.1.7 Start Encryption event content
|
||||
*
|
||||
* Event header: 19 bytes
|
||||
* Content size: 17 bytes
|
||||
* crypto scheme 1 byte
|
||||
* key_version 4 bytes
|
||||
* nonce random 12 bytes
|
||||
*
|
||||
* Event size is 19 + 17 = 36 bytes
|
||||
*/
|
||||
typedef struct start_encryption_event
|
||||
{
|
||||
uint8_t header[BINLOG_EVENT_HDR_LEN]; /**< Replication event header */
|
||||
uint8_t binlog_crypto_scheme; /**< Encryption scheme */
|
||||
uint32_t binlog_key_version; /**< Encryption key version */
|
||||
uint8_t nonce[BLRM_NONCE_LENGTH]; /**< nonce (random bytes) of current binlog.
|
||||
* These bytes + the binlog event current pos
|
||||
* form the encrryption IV for the event */
|
||||
} START_ENCRYPTION_EVENT;
|
||||
|
||||
/**
|
||||
* Initialise the binlog file for this instance. MaxScale will look
|
||||
* for all the binlogs that it has on local disk, determine the next
|
||||
@ -245,7 +286,7 @@ blr_file_create(ROUTER_INSTANCE *router, char *file)
|
||||
}
|
||||
|
||||
int created = 0;
|
||||
char err_msg[STRERROR_BUFLEN];
|
||||
char err_msg[MXS_STRERROR_BUFLEN];
|
||||
|
||||
char path[PATH_MAX + 1] = "";
|
||||
|
||||
@ -364,10 +405,28 @@ blr_write_binlog_record(ROUTER_INSTANCE *router, REP_HEADER *hdr, uint32_t size,
|
||||
{
|
||||
int n;
|
||||
|
||||
uint64_t file_offset = router->current_pos;
|
||||
|
||||
/**
|
||||
* Check for possible hole looking at current pos and next pos
|
||||
* Fill the gap with a self generated ignorable event
|
||||
* Binlog file position is incremented by blr_write_special_event()
|
||||
*/
|
||||
|
||||
if (hdr->next_pos && (hdr->next_pos > (file_offset + size)))
|
||||
{
|
||||
uint64_t hole_size = hdr->next_pos - file_offset - size;
|
||||
if (!blr_write_special_event(router, file_offset, hole_size, hdr, BLRM_IGNORABLE))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Write current received event form master */
|
||||
if ((n = pwrite(router->binlog_fd, buf, size,
|
||||
router->last_written)) != size)
|
||||
{
|
||||
char err_msg[STRERROR_BUFLEN];
|
||||
char err_msg[MXS_STRERROR_BUFLEN];
|
||||
MXS_ERROR("%s: Failed to write binlog record at %lu of %s, %s. "
|
||||
"Truncating to previous record.",
|
||||
router->service->name, router->last_written,
|
||||
@ -383,6 +442,8 @@ blr_write_binlog_record(ROUTER_INSTANCE *router, REP_HEADER *hdr, uint32_t size,
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Increment offsets */
|
||||
spinlock_acquire(&router->binlog_lock);
|
||||
router->current_pos = hdr->next_pos;
|
||||
router->last_written += size;
|
||||
@ -595,7 +656,7 @@ blr_read_binlog(ROUTER_INSTANCE *router, BLFILE *file, unsigned long pos, REP_HE
|
||||
break;
|
||||
case -1:
|
||||
{
|
||||
char err_msg[STRERROR_BUFLEN];
|
||||
char err_msg[MXS_STRERROR_BUFLEN];
|
||||
snprintf(errmsg, BINLOG_ERROR_MSG_LEN, "Failed to read binlog file '%s'; (%s), event at %lu",
|
||||
file->binlogname, strerror_r(errno, err_msg, sizeof(err_msg)), pos);
|
||||
|
||||
@ -678,7 +739,7 @@ blr_read_binlog(ROUTER_INSTANCE *router, BLFILE *file, unsigned long pos, REP_HE
|
||||
break;
|
||||
case -1:
|
||||
{
|
||||
char err_msg[STRERROR_BUFLEN];
|
||||
char err_msg[MXS_STRERROR_BUFLEN];
|
||||
snprintf(errmsg, BINLOG_ERROR_MSG_LEN,
|
||||
"Failed to reread header in binlog file '%s'; (%s), event at %lu",
|
||||
file->binlogname, strerror_r(errno, err_msg, sizeof(err_msg)), pos);
|
||||
@ -738,7 +799,7 @@ blr_read_binlog(ROUTER_INSTANCE *router, BLFILE *file, unsigned long pos, REP_HE
|
||||
{
|
||||
if (n == -1)
|
||||
{
|
||||
char err_msg[STRERROR_BUFLEN];
|
||||
char err_msg[MXS_STRERROR_BUFLEN];
|
||||
snprintf(errmsg, BINLOG_ERROR_MSG_LEN,
|
||||
"Error reading the binlog event at %lu in binlog file '%s';"
|
||||
"(%s), expected %d bytes.",
|
||||
@ -1032,6 +1093,7 @@ blr_read_events_all_events(ROUTER_INSTANCE *router, int fix, int debug)
|
||||
BINLOG_EVENT_DESC last_event;
|
||||
BINLOG_EVENT_DESC fde_event;
|
||||
int fde_seen = 0;
|
||||
int start_encryption_seen = 0;
|
||||
|
||||
memset(&first_event, '\0', sizeof(first_event));
|
||||
memset(&last_event, '\0', sizeof(last_event));
|
||||
@ -1200,6 +1262,35 @@ blr_read_events_all_events(ROUTER_INSTANCE *router, int fix, int debug)
|
||||
}
|
||||
}
|
||||
|
||||
/* If binlog is encrypted just set pos = pos + event_size and continue */
|
||||
if (start_encryption_seen)
|
||||
{
|
||||
uint32_t event_size = EXTRACT32(hdbuf + BINLOG_EVENT_LEN_OFFSET);
|
||||
|
||||
/**
|
||||
* Events are encrypted.
|
||||
*
|
||||
* The routine doesn't decrypt them but follows
|
||||
* next event based on the event_size (4 bytes) that is af offset
|
||||
* of BINLOG_EVENT_LEN_OFFSET (9) and it's in clear.
|
||||
*
|
||||
*/
|
||||
MXS_DEBUG("** Encrypted Event @ %lu: size is %lu, next event at %lu\n",
|
||||
(unsigned long)pos,
|
||||
(unsigned long)event_size,
|
||||
(unsigned long)(pos + event_size));
|
||||
|
||||
/* Next event pos is ps + event size */
|
||||
pos = pos + event_size;
|
||||
|
||||
/* Update other offsets as well */
|
||||
router->binlog_position = pos;
|
||||
router->current_safe_event = pos;
|
||||
router->current_pos = pos;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
/* fill replication header struct */
|
||||
hdr.timestamp = EXTRACT32(hdbuf);
|
||||
hdr.event_type = hdbuf[4];
|
||||
@ -1395,8 +1486,6 @@ blr_read_events_all_events(ROUTER_INSTANCE *router, int fix, int debug)
|
||||
if (hdr.event_type == FORMAT_DESCRIPTION_EVENT)
|
||||
{
|
||||
int event_header_length;
|
||||
int event_header_ntypes;
|
||||
int n_events;
|
||||
int check_alg;
|
||||
uint8_t *checksum;
|
||||
char buf_t[40];
|
||||
@ -1421,29 +1510,40 @@ blr_read_events_all_events(ROUTER_INSTANCE *router, int fix, int debug)
|
||||
pos, (unsigned long)hdr.event_size, fde_event.event_time, buf_t);
|
||||
}
|
||||
|
||||
event_header_length = ptr[2 + 50 + 4];
|
||||
event_header_ntypes = hdr.event_size - event_header_length - (2 + 50 + 4 + 1);
|
||||
/* FDE is:
|
||||
*
|
||||
* 2 bytes binlog-version
|
||||
* string[50] mysql-server version
|
||||
* 4 bytes create timestamp
|
||||
* 1 event header length, 19 is the current length
|
||||
* string[p] event type header lengths:
|
||||
* an array indexed by [Binlog Event Type - 1]
|
||||
*/
|
||||
|
||||
if (event_header_ntypes == 168)
|
||||
{
|
||||
/* mariadb 10 LOG_EVENT_TYPES*/
|
||||
event_header_ntypes -= 163;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (event_header_ntypes == 165)
|
||||
{
|
||||
/* mariadb 5 LOG_EVENT_TYPES*/
|
||||
event_header_ntypes -= 160;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* mysql 5.6 LOG_EVENT_TYPES = 35 */
|
||||
event_header_ntypes -= 35;
|
||||
}
|
||||
}
|
||||
/* ptr now points to event_header_length byte.
|
||||
* This offset is just 1 byte before the number of supported events offset
|
||||
*/
|
||||
event_header_length = ptr[BLRM_FDE_EVENT_TYPES_OFFSET - 1];
|
||||
|
||||
n_events = hdr.event_size - event_header_length - (2 + 50 + 4 + 1);
|
||||
/* The number of supported events formula:
|
||||
* number_of_events = event_size - (event_header_len + BLRM_FDE_EVENT_TYPES_OFFSET)
|
||||
*/
|
||||
int n_events = hdr.event_size - event_header_length - BLRM_FDE_EVENT_TYPES_OFFSET;
|
||||
|
||||
/**
|
||||
* The FDE event also carries 5 additional bytes:
|
||||
*
|
||||
* 1 byte is the checksum_alg_type and 4 bytes are the computed crc32
|
||||
*
|
||||
* These 5 bytes are always present even if alg_type is NONE/UNDEF:
|
||||
* then the 4 crc32 bytes must not be checked, whatever the value is.
|
||||
*
|
||||
* In case of CRC32 algo_type the 4 bytes contain the event crc32.
|
||||
*/
|
||||
int fde_extra_bytes = BINLOG_EVENT_CRC_ALGO_TYPE + BINLOG_EVENT_CRC_SIZE;
|
||||
|
||||
/* Now remove from the calculated number of events the extra 5 bytes */
|
||||
n_events -= fde_extra_bytes;
|
||||
|
||||
if (debug)
|
||||
{
|
||||
@ -1452,30 +1552,74 @@ blr_read_events_all_events(ROUTER_INSTANCE *router, int fix, int debug)
|
||||
MXS_DEBUG(" FDE Header EventLength %i"
|
||||
", N. of supported MySQL/MariaDB events %i",
|
||||
event_header_length,
|
||||
(n_events - event_header_ntypes));
|
||||
n_events);
|
||||
}
|
||||
|
||||
if (event_header_ntypes < n_events)
|
||||
/* Check whether Master is sending events with CRC32 checksum */
|
||||
checksum = ptr + hdr.event_size - event_header_length - fde_extra_bytes;
|
||||
check_alg = checksum[0];
|
||||
|
||||
if (debug)
|
||||
{
|
||||
checksum = ptr + hdr.event_size - event_header_length - event_header_ntypes;
|
||||
check_alg = checksum[0];
|
||||
MXS_DEBUG(" FDE Checksum alg desc %i, alg type %s",
|
||||
check_alg,
|
||||
check_alg == 1 ?
|
||||
"BINLOG_CHECKSUM_ALG_CRC32" : "NONE or UNDEF");
|
||||
}
|
||||
if (check_alg == 1)
|
||||
{
|
||||
found_chksum = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
found_chksum = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Detect possible Start Encryption Event */
|
||||
if (hdr.event_type == MARIADB10_START_ENCRYPTION_EVENT)
|
||||
{
|
||||
START_ENCRYPTION_EVENT ste_event = {};
|
||||
char nonce_hex[BLRM_NONCE_LENGTH * 2 + 1] = "";
|
||||
/* The start encryption event data is 17 bytes long:
|
||||
* Scheme = 1
|
||||
* Key Version: 4
|
||||
* nonce = 12
|
||||
*/
|
||||
|
||||
/* Fill the event content, after the event header */
|
||||
ste_event.binlog_crypto_scheme = ptr[0];
|
||||
ste_event.binlog_key_version = extract_field(ptr + 1, 32);
|
||||
memcpy(ste_event.nonce, ptr + 1 + BLRM_KEY_VERSION_LENGTH, BLRM_NONCE_LENGTH);
|
||||
|
||||
gw_bin2hex(nonce_hex, ste_event.nonce, BLRM_NONCE_LENGTH);
|
||||
|
||||
if (debug)
|
||||
{
|
||||
MXS_DEBUG(" FDE Checksum alg desc %i, alg type %s",
|
||||
check_alg,
|
||||
check_alg == 1 ?
|
||||
"BINLOG_CHECKSUM_ALG_CRC32" : "NONE or UNDEF");
|
||||
char *cksum_format = ", crc32 0x";
|
||||
char hex_checksum[BINLOG_EVENT_CRC_SIZE * 2 + strlen(cksum_format) + 1];
|
||||
uint8_t cksum_data[BINLOG_EVENT_CRC_SIZE];
|
||||
cksum_data[3] = *(ptr + hdr.event_size - 4 - BINLOG_EVENT_HDR_LEN);
|
||||
cksum_data[2] = *(ptr + hdr.event_size - 3 - BINLOG_EVENT_HDR_LEN);
|
||||
cksum_data[1] = *(ptr + hdr.event_size - 2 - BINLOG_EVENT_HDR_LEN);
|
||||
cksum_data[0] = *(ptr + hdr.event_size - 1 - BINLOG_EVENT_HDR_LEN);
|
||||
|
||||
if (found_chksum)
|
||||
{
|
||||
strcpy(hex_checksum, cksum_format);
|
||||
gw_bin2hex(hex_checksum + strlen(cksum_format) , cksum_data, BINLOG_EVENT_CRC_SIZE);
|
||||
}
|
||||
|
||||
MXS_DEBUG("- START_ENCRYPTION event @ %llu, size %lu, next pos is @ %lu, flags %u%s",
|
||||
pos, (unsigned long)hdr.event_size, (unsigned long)hdr.next_pos, hdr.flags,
|
||||
hex_checksum);
|
||||
|
||||
MXS_DEBUG(" Encryption scheme: %u, key_version: %u,"
|
||||
" nonce: %s\n", ste_event.binlog_crypto_scheme,
|
||||
ste_event.binlog_key_version, nonce_hex);
|
||||
}
|
||||
if (check_alg == 1)
|
||||
{
|
||||
found_chksum = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
found_chksum = 0;
|
||||
}
|
||||
}
|
||||
|
||||
start_encryption_seen = 1;
|
||||
}
|
||||
|
||||
/* set last event time, pos and type */
|
||||
@ -1907,7 +2051,7 @@ blr_file_write_master_config(ROUTER_INSTANCE *router, char *error)
|
||||
|
||||
char filename[len + sizeof('/') + sizeof(MASTER_INI)]; // sizeof includes NULL
|
||||
char tmp_file[len + sizeof('/') + sizeof(MASTER_INI) + sizeof('.') + sizeof(TMP)];
|
||||
char err_msg[STRERROR_BUFLEN];
|
||||
char err_msg[MXS_STRERROR_BUFLEN];
|
||||
char *ssl_ca;
|
||||
char *ssl_cert;
|
||||
char *ssl_key;
|
||||
@ -1994,7 +2138,7 @@ blr_file_write_master_config(ROUTER_INSTANCE *router, char *error)
|
||||
|
||||
/** Print Binlog Details
|
||||
*
|
||||
* @param router The router instance
|
||||
* @param router The router instance
|
||||
* @param first_event First Event details
|
||||
* @param last_event First Event details
|
||||
*/
|
||||
@ -2019,7 +2163,7 @@ blr_print_binlog_details(ROUTER_INSTANCE *router,
|
||||
|
||||
event_desc = blr_get_event_description(router, first_event.event_type);
|
||||
|
||||
MXS_NOTICE("%lu @ %llu, %s, (%s), First EventTime",
|
||||
MXS_NOTICE("%lu @ %" PRIu64 ", %s, (%s), First EventTime",
|
||||
first_event.event_time, first_event.event_pos,
|
||||
event_desc != NULL ? event_desc : "unknown", buf_t);
|
||||
|
||||
@ -2034,8 +2178,153 @@ blr_print_binlog_details(ROUTER_INSTANCE *router,
|
||||
|
||||
event_desc = blr_get_event_description(router, last_event.event_type);
|
||||
|
||||
MXS_NOTICE("%lu @ %llu, %s, (%s), Last EventTime",
|
||||
MXS_NOTICE("%lu @ %" PRIu64 ", %s, (%s), Last EventTime",
|
||||
last_event.event_time, last_event.event_pos,
|
||||
event_desc != NULL ? event_desc : "unknown", buf_t);
|
||||
}
|
||||
|
||||
/** Create an ignorable event
|
||||
*
|
||||
* @param event_size The size of the new event being created (crc32 4 bytes could be included)
|
||||
* @param hdr Current replication event header, received from master
|
||||
* @param event_pos The position in binlog file of the new event
|
||||
* @param do_checksum Whether checksum must be calculated and stored
|
||||
* @return Returns the pointer of new event
|
||||
*/
|
||||
static uint8_t *
|
||||
blr_create_ignorable_event(uint32_t event_size,
|
||||
REP_HEADER *hdr,
|
||||
uint32_t event_pos,
|
||||
bool do_checksum)
|
||||
{
|
||||
uint8_t *new_event;
|
||||
|
||||
if (event_size < BINLOG_EVENT_HDR_LEN)
|
||||
{
|
||||
MXS_ERROR("blr_create_ignorable_event an event of %lu bytes"
|
||||
" is not valid in blr_file.c", (unsigned long)event_size);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Allocate space for event: size might contain the 4 crc32
|
||||
new_event = MXS_CALLOC(1, event_size);
|
||||
if (new_event == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Populate Event header 19 bytes for Ignorable Event
|
||||
encode_value(&new_event[0], hdr->timestamp, 32); // same timestamp as in current received event
|
||||
new_event[4] = IGNORABLE_EVENT; // type is IGNORABLE_EVENT
|
||||
encode_value(&new_event[5], hdr->serverid, 32); // same serverid as in current received event
|
||||
encode_value(&new_event[9], event_size, 32); // event size
|
||||
encode_value(&new_event[13], event_pos + event_size, 32); // next_pos
|
||||
encode_value(&new_event[17], LOG_EVENT_IGNORABLE_F, 16); // flag is LOG_EVENT_IGNORABLE_F
|
||||
|
||||
/* if checksum is required calculate the crc32 and add it in the last 4 bytes*/
|
||||
if (do_checksum)
|
||||
{
|
||||
/*
|
||||
* Now add the CRC to the Ignorable binlog event.
|
||||
*
|
||||
* The algorithm is first to compute the checksum of an empty buffer
|
||||
* and then the checksum of the real event: 4 byte less than event_size
|
||||
*/
|
||||
uint32_t chksum;
|
||||
chksum = crc32(0L, NULL, 0);
|
||||
chksum = crc32(chksum, new_event, event_size - 4);
|
||||
|
||||
// checksum is stored after current event data using 4 bytes
|
||||
encode_value(new_event + event_size - 4, chksum, 32);
|
||||
}
|
||||
return new_event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and write a special event (not received from master) into binlog file
|
||||
*
|
||||
* @param router The current router instance
|
||||
* @param file_offset Position where event will be written
|
||||
* @param event_size The size of new event (it might hold the 4 bytes crc32)
|
||||
* @param hdr Replication header of the current reived event (from Master)
|
||||
* @param type Type of special event to create and write
|
||||
* @return 1 on success, 0 on error
|
||||
*/
|
||||
static int
|
||||
blr_write_special_event(ROUTER_INSTANCE *router, uint32_t file_offset, uint32_t event_size, REP_HEADER *hdr, int type)
|
||||
{
|
||||
int n;
|
||||
uint8_t *new_event;
|
||||
char *new_event_desc;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case BLRM_IGNORABLE:
|
||||
new_event_desc = "IGNORABLE";
|
||||
MXS_INFO("Hole detected while writing in binlog '%s' @ %lu: an %s event "
|
||||
"of %lu bytes will be written at pos %lu",
|
||||
router->binlog_name,
|
||||
router->current_pos,
|
||||
new_event_desc,
|
||||
(unsigned long)event_size,
|
||||
(unsigned long)file_offset);
|
||||
|
||||
/* Create the ignorable event */
|
||||
if ((new_event = blr_create_ignorable_event(event_size,
|
||||
hdr,
|
||||
file_offset,
|
||||
router->master_chksum)) == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
new_event_desc = "UNKNOWN";
|
||||
MXS_ERROR("Cannot create special binlog event of %s type and size %lu "
|
||||
"in binlog file '%s' @ %lu",
|
||||
new_event_desc,
|
||||
(unsigned long)event_size,
|
||||
router->binlog_name,
|
||||
router->current_pos);
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// Write the event
|
||||
if ((n = pwrite(router->binlog_fd, new_event, event_size, file_offset)) != event_size)
|
||||
{
|
||||
char err_msg[MXS_STRERROR_BUFLEN];
|
||||
MXS_ERROR("%s: Failed to write %s special binlog record at %lu of %s, %s. "
|
||||
"Truncating to previous record.",
|
||||
router->service->name, new_event_desc, (unsigned long)file_offset,
|
||||
router->binlog_name,
|
||||
strerror_r(errno, err_msg, sizeof(err_msg)));
|
||||
|
||||
/* Remove any partial event that was written */
|
||||
if (ftruncate(router->binlog_fd, router->last_written))
|
||||
{
|
||||
MXS_ERROR("%s: Failed to truncate %s special binlog record at %lu of %s, %s. ",
|
||||
router->service->name, new_event_desc, (unsigned long)file_offset,
|
||||
router->binlog_name,
|
||||
strerror_r(errno, err_msg, sizeof(err_msg)));
|
||||
}
|
||||
MXS_FREE(new_event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
MXS_FREE(new_event);
|
||||
|
||||
// Increment offsets, next event will be written after this special one
|
||||
spinlock_acquire(&router->binlog_lock);
|
||||
|
||||
router->last_written += event_size;
|
||||
router->current_pos = file_offset + event_size;
|
||||
router->last_event_pos = file_offset;
|
||||
|
||||
spinlock_release(&router->binlog_lock);
|
||||
|
||||
// Force write
|
||||
fsync(router->binlog_fd);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -56,29 +56,27 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <service.h>
|
||||
#include <server.h>
|
||||
#include <router.h>
|
||||
#include <atomic.h>
|
||||
#include <session.h>
|
||||
#include <blr.h>
|
||||
#include <dcb.h>
|
||||
#include <spinlock.h>
|
||||
#include <housekeeper.h>
|
||||
#include <buffer.h>
|
||||
#include <maxscale/service.h>
|
||||
#include <maxscale/server.h>
|
||||
#include <maxscale/router.h>
|
||||
#include <maxscale/atomic.h>
|
||||
#include <maxscale/session.h>
|
||||
#include "blr.h"
|
||||
#include <maxscale/dcb.h>
|
||||
#include <maxscale/spinlock.h>
|
||||
#include <maxscale/housekeeper.h>
|
||||
#include <maxscale/buffer.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <skygw_types.h>
|
||||
#include <skygw_utils.h>
|
||||
#include <log_manager.h>
|
||||
#include <maxscale/log_manager.h>
|
||||
|
||||
#include <rdtsc.h>
|
||||
#include <thread.h>
|
||||
#include <maxscale/rdtsc.h>
|
||||
#include <maxscale/thread.h>
|
||||
|
||||
/* Temporary requirement for auth data */
|
||||
#include <mysql_client_server_protocol.h>
|
||||
#include <maxscale/protocol/mysql.h>
|
||||
#include <maxscale/alloc.h>
|
||||
|
||||
|
||||
@ -1291,7 +1289,7 @@ blr_handle_binlog_record(ROUTER_INSTANCE *router, GWBUF *pkt)
|
||||
|
||||
if (router->master_chksum)
|
||||
{
|
||||
uint32_t size = MIN(len - extra_bytes - semisync_bytes,
|
||||
uint32_t size = MXS_MIN(len - extra_bytes - semisync_bytes,
|
||||
router->checksum_size);
|
||||
|
||||
router->stored_checksum = crc32(router->stored_checksum,
|
||||
@ -1342,7 +1340,7 @@ blr_handle_binlog_record(ROUTER_INSTANCE *router, GWBUF *pkt)
|
||||
size = len - (check_packet_len + MYSQL_CHECKSUM_LEN);
|
||||
}
|
||||
|
||||
size = MIN(size, router->checksum_size);
|
||||
size = MXS_MIN(size, router->checksum_size);
|
||||
|
||||
if (router->checksum_size > 0)
|
||||
{
|
||||
@ -2092,7 +2090,7 @@ GWBUF
|
||||
break;
|
||||
case -1:
|
||||
{
|
||||
char err_msg[STRERROR_BUFLEN];
|
||||
char err_msg[MXS_STRERROR_BUFLEN];
|
||||
MXS_ERROR("Reading saved events: failed to read binlog "
|
||||
"file %s at position %llu"
|
||||
" (%s).", router->binlog_name,
|
||||
@ -2152,7 +2150,7 @@ GWBUF
|
||||
{
|
||||
if (n == -1)
|
||||
{
|
||||
char err_msg[STRERROR_BUFLEN];
|
||||
char err_msg[MXS_STRERROR_BUFLEN];
|
||||
MXS_ERROR("Reading saved events: the event at %llu in %s. "
|
||||
"%s, expected %d bytes.",
|
||||
pos, router->binlog_name,
|
||||
@ -2411,7 +2409,7 @@ blr_write_data_into_binlog(ROUTER_INSTANCE *router, uint32_t data_len, uint8_t *
|
||||
if ((n = pwrite(router->binlog_fd, buf, data_len,
|
||||
router->last_written)) != data_len)
|
||||
{
|
||||
char err_msg[STRERROR_BUFLEN];
|
||||
char err_msg[MXS_STRERROR_BUFLEN];
|
||||
MXS_ERROR("%s: Failed to write binlog record at %lu of %s, %s. "
|
||||
"Truncating to previous record.",
|
||||
router->service->name, router->last_written,
|
||||
@ -2539,7 +2537,7 @@ bool blr_send_event(blr_thread_role_t role,
|
||||
while (rval && len > 0)
|
||||
{
|
||||
uint64_t payload_len = first ? MYSQL_PACKET_LENGTH_MAX - 1 :
|
||||
MIN(MYSQL_PACKET_LENGTH_MAX, len);
|
||||
MXS_MIN(MYSQL_PACKET_LENGTH_MAX, len);
|
||||
|
||||
if (blr_send_packet(slave, buf, payload_len, first))
|
||||
{
|
||||
|
@ -61,6 +61,8 @@
|
||||
* 09/05/2016 Massimiliano Pinto Added SELECT USER()
|
||||
* 11/07/2016 Massimiliano Pinto Added SSL backend support
|
||||
* 24/08/2016 Massimiliano Pinto Added slave notification via CS_WAIT_DATA
|
||||
* 16/09/2016 Massimiliano Pinto IGNORABLE_EVENT created by MaxScale is not sent to slaves,
|
||||
* Events with LOG_EVENT_IGNORABLE_F are also skipped.
|
||||
*
|
||||
* @endverbatim
|
||||
*/
|
||||
@ -69,24 +71,21 @@
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <maxscale.h>
|
||||
#include <service.h>
|
||||
#include <server.h>
|
||||
#include <router.h>
|
||||
#include <atomic.h>
|
||||
#include <spinlock.h>
|
||||
#include <blr.h>
|
||||
#include <dcb.h>
|
||||
#include <spinlock.h>
|
||||
#include <housekeeper.h>
|
||||
#include <maxscale/maxscale.h>
|
||||
#include <maxscale/service.h>
|
||||
#include <maxscale/server.h>
|
||||
#include <maxscale/router.h>
|
||||
#include <maxscale/atomic.h>
|
||||
#include <maxscale/spinlock.h>
|
||||
#include "blr.h"
|
||||
#include <maxscale/dcb.h>
|
||||
#include <maxscale/spinlock.h>
|
||||
#include <maxscale/housekeeper.h>
|
||||
#include <sys/stat.h>
|
||||
#include <skygw_types.h>
|
||||
#include <skygw_utils.h>
|
||||
#include <log_manager.h>
|
||||
#include <version.h>
|
||||
#include <maxscale/log_manager.h>
|
||||
#include <maxscale/version.h>
|
||||
#include <zlib.h>
|
||||
#include <maxscale/alloc.h>
|
||||
#include <gw.h>
|
||||
|
||||
extern int load_mysql_users(SERV_LISTENER *listener);
|
||||
extern void blr_master_close(ROUTER_INSTANCE* router);
|
||||
@ -264,7 +263,7 @@ blr_slave_request(ROUTER_INSTANCE *router, ROUTER_SLAVE *slave, GWBUF *queue)
|
||||
case COM_QUIT:
|
||||
MXS_DEBUG("COM_QUIT received from slave with server_id %d",
|
||||
slave->serverid);
|
||||
break;
|
||||
return 1;
|
||||
default:
|
||||
blr_send_custom_error(slave->dcb, 1, 0,
|
||||
"You have an error in your SQL syntax; Check the "
|
||||
@ -436,7 +435,7 @@ blr_slave_query(ROUTER_INSTANCE *router, ROUTER_SLAVE *slave, GWBUF *queue)
|
||||
else if (strcasecmp(word, "USER()") == 0)
|
||||
{
|
||||
/* Return user@host */
|
||||
char user_host[MYSQL_USER_MAXLEN + 1 + MYSQL_HOSTNAME_MAXLEN + 1] = "";
|
||||
char user_host[MYSQL_USER_MAXLEN + 1 + MYSQL_HOST_MAXLEN + 1] = "";
|
||||
|
||||
MXS_FREE(query_text);
|
||||
snprintf(user_host, sizeof(user_host),
|
||||
@ -876,7 +875,7 @@ blr_slave_query(ROUTER_INSTANCE *router, ROUTER_SLAVE *slave, GWBUF *queue)
|
||||
|
||||
if (removed_cfg == -1)
|
||||
{
|
||||
char err_msg[STRERROR_BUFLEN];
|
||||
char err_msg[MXS_STRERROR_BUFLEN];
|
||||
snprintf(error_string, BINLOG_ERROR_MSG_LEN,
|
||||
"Error removing %s, %s, errno %u", path,
|
||||
strerror_r(errno, err_msg, sizeof(err_msg)), errno);
|
||||
@ -2304,6 +2303,15 @@ blr_slave_catchup(ROUTER_INSTANCE *router, ROUTER_SLAVE *slave, bool large)
|
||||
strcpy(binlog_name, slave->binlogfile);
|
||||
binlog_pos = slave->binlog_pos;
|
||||
|
||||
/* Don't sent special events generated by MaxScale */
|
||||
if (hdr.event_type == IGNORABLE_EVENT || (hdr.flags & LOG_EVENT_IGNORABLE_F))
|
||||
{
|
||||
slave->binlog_pos = hdr.next_pos;
|
||||
gwbuf_free(record);
|
||||
record = NULL;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hdr.event_type == ROTATE_EVENT)
|
||||
{
|
||||
unsigned long beat1 = hkheartbeat;
|
||||
@ -3337,8 +3345,6 @@ blr_stop_slave(ROUTER_INSTANCE* router, ROUTER_SLAVE* slave)
|
||||
static int
|
||||
blr_start_slave(ROUTER_INSTANCE* router, ROUTER_SLAVE* slave)
|
||||
{
|
||||
int loaded;
|
||||
|
||||
/* if unconfigured return an error */
|
||||
if (router->master_state == BLRM_UNCONFIGURED)
|
||||
{
|
||||
@ -3468,28 +3474,7 @@ blr_start_slave(ROUTER_INSTANCE* router, ROUTER_SLAVE* slave)
|
||||
router->current_pos, router->binlog_position);
|
||||
|
||||
/* Try reloading new users and update cached credentials */
|
||||
loaded = service_refresh_users(router->service);
|
||||
|
||||
if (loaded == 0)
|
||||
{
|
||||
for (SERV_LISTENER *port = router->service->ports; port; port = port->next)
|
||||
{
|
||||
char path[PATH_MAX];
|
||||
sprintf(path, "%s/%s/%s/", router->binlogdir, BLR_DBUSERS_DIR, port->name);
|
||||
|
||||
if (mxs_mkdir_all(path, 0775))
|
||||
{
|
||||
strcat(path, BLR_DBUSERS_FILE);
|
||||
dbusers_save(port->users, path);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
MXS_NOTICE("Service %s: user credentials could not be refreshed. "
|
||||
"Will use existing cached credentials (%s/%s) if possible.",
|
||||
router->service->name, router->binlogdir, BLR_DBUSERS_DIR);
|
||||
}
|
||||
service_refresh_users(router->service);
|
||||
|
||||
return blr_slave_send_ok(router, slave);
|
||||
}
|
||||
|
@ -38,8 +38,8 @@
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <maxscale/alloc.h>
|
||||
#include <log_manager.h>
|
||||
#include <blr.h>
|
||||
#include <maxscale/log_manager.h>
|
||||
#include "blr.h"
|
||||
|
||||
|
||||
static void printVersion(const char *progname);
|
||||
|
@ -26,28 +26,26 @@
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <time.h>
|
||||
#include <service.h>
|
||||
#include <server.h>
|
||||
#include <router.h>
|
||||
#include <atomic.h>
|
||||
#include <spinlock.h>
|
||||
#include <blr.h>
|
||||
#include <dcb.h>
|
||||
#include <spinlock.h>
|
||||
#include <housekeeper.h>
|
||||
#include <maxscale/service.h>
|
||||
#include <maxscale/server.h>
|
||||
#include <maxscale/router.h>
|
||||
#include <maxscale/atomic.h>
|
||||
#include <maxscale/spinlock.h>
|
||||
#include "../blr.h"
|
||||
#include <maxscale/dcb.h>
|
||||
#include <maxscale/spinlock.h>
|
||||
#include <maxscale/housekeeper.h>
|
||||
#include <time.h>
|
||||
#include <skygw_types.h>
|
||||
#include <skygw_utils.h>
|
||||
#include <log_manager.h>
|
||||
#include <gwdirs.h>
|
||||
#include <maxscale/log_manager.h>
|
||||
#include <maxscale/gwdirs.h>
|
||||
#include <maxscale/alloc.h>
|
||||
|
||||
#include <mysql_client_server_protocol.h>
|
||||
#include <maxscale/protocol/mysql.h>
|
||||
#include <ini.h>
|
||||
#include <sys/stat.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#include <version.h>
|
||||
#include <maxscale/version.h>
|
||||
|
||||
static void printVersion(const char *progname);
|
||||
static void printUsage(const char *progname);
|
||||
@ -104,13 +102,13 @@ int main(int argc, char **argv) {
|
||||
serviceAddRouterOption(service, s);
|
||||
s = strtok_r(NULL, ",", &lasts);
|
||||
}
|
||||
|
||||
server = server_alloc("_none_", "MySQLBackend", (int)3306);
|
||||
set_libdir(MXS_STRDUP_A("../../../authenticator/"));
|
||||
server = server_alloc("binlog_router_master_host", "_none_", 3306,
|
||||
"MySQLBackend", "MySQLBackendAuth", NULL);
|
||||
if (server == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
server_set_unique_name(server, "binlog_router_master_host");
|
||||
serviceAddBackend(service, server);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user