binlogClient.registerEventListener(eventListener); binlogClient.registerLifecycleListener(lifecycleListener); binlogClient.setBinlogFilename(currentBinlogFile); if (currentBinlogPosition != DO_NOT_SET) { binlogClient.setBinlogPosition(currentBinlogPosition); binlogClient.setServerId(serverId); connectTimeout = Long.MAX_VALUE; binlogClient.connect(connectTimeout); transitUri = "mysql://" + connectedHost.getHostString() + ":" + connectedHost.getPort(); if (!binlogClient.isConnected()) { binlogClient.disconnect(); binlogClient = null; throw new IOException("Could not connect binlog client to any of the specified hosts due to: " + lastConnectException.getMessage(), lastConnectException); binlogClient.disconnect(); binlogClient = null; throw new IOException("Error creating binlog enrichment JDBC connection to any of the specified hosts", e);
public void start() throws Exception { initDataSource(); binlogPositionManager = new BinlogPositionManager(config, dataSource); binlogPositionManager.initBeginPosition(); schema = new Schema(dataSource); schema.load(); eventListener = new EventListener(queue); binaryLogClient = new BinaryLogClient(config.mysqlAddr, config.mysqlPort, config.mysqlUsername, config.mysqlPassword); binaryLogClient.setBlocking(true); binaryLogClient.setServerId(1001); EventDeserializer eventDeserializer = new EventDeserializer(); eventDeserializer.setCompatibilityMode(EventDeserializer.CompatibilityMode.DATE_AND_TIME_AS_LONG, EventDeserializer.CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY); binaryLogClient.setEventDeserializer(eventDeserializer); binaryLogClient.registerEventListener(eventListener); binaryLogClient.setBinlogFilename(binlogPositionManager.getBinlogFilename()); binaryLogClient.setBinlogPosition(binlogPositionManager.getPosition()); binaryLogClient.connect(3000); LOGGER.info("Started."); doProcess(); }
public BinaryLogClientStatistics(BinaryLogClient binaryLogClient) { binaryLogClient.registerEventListener(this); binaryLogClient.registerLifecycleListener(this); }
@Override public void run() { try { setConnectTimeout(timeout); connect(); } catch (IOException e) { exceptionReference.set(e); countDownLatch.countDown(); // making sure we don't end up waiting whole "timeout" } } };
private void ensureReplicatorThread() throws Exception { checkCommErrors(); if ( !client.isConnected() && !stopOnEOF ) { if (this.gtidPositioning) { // When using gtid positioning, reconnecting should take us to the top // of the gtid event. We throw away any binlog position we have // (other than GTID) and bail out of getTransactionRows() LOGGER.warn("replicator stopped at position: {} -- restarting", client.getGtidSet()); client.setBinlogFilename(""); client.setBinlogPosition(4L); client.connect(5000); throw new ClientReconnectedException(); } else { // standard binlog positioning is a lot easier; we can really reconnect anywhere // we like, so we don't have to bail out of the middle of an event. LOGGER.warn("replicator stopped at position: {} -- restarting", client.getBinlogFilename() + ":" + client.getBinlogPosition()); client.connect(5000); } } }
public void start(Optional<BinlogFileOffset> binlogFileOffset, Consumer<M> eventConsumer) { client = new BinaryLogClient(host, port, dbUserName, dbPassword); client.setServerId(binlogClientUniqueId); client.setKeepAliveInterval(5 * 1000); client.setBinlogFilename(bfo.getBinlogFilename()); client.setBinlogPosition(bfo.getOffset()); client.setEventDeserializer(getEventDeserializer()); client.registerEventListener(event -> { switch (event.getHeader().getEventType()) { case TABLE_MAP: {
protected void startClient(Consumer<BinaryLogClient> preConnect) throws IOException, TimeoutException, SQLException { // Connect the bin log client ... counters = new EventQueue(DEFAULT_TIMEOUT, this::logConsumedEvent, this::logIgnoredEvent); client = new BinaryLogClient(config.getHostname(), config.getPort(), "replicator", "replpass"); client.setServerId(client.getServerId() - 1); // avoid clashes between BinaryLogClient instances client.setKeepAlive(false); client.setSSLMode(SSLMode.DISABLED); client.registerEventListener(counters); client.registerEventListener(this::recordEvent); client.registerLifecycleListener(new TraceLifecycleListener()); EventDeserializer eventDeserializer = new EventDeserializer(); eventDeserializer.setEventDataDeserializer(EventType.STOP, new StopEventDataDeserializer()); client.setEventDeserializer(eventDeserializer); if (preConnect != null) preConnect.accept(client); client.connect(DEFAULT_TIMEOUT); // does not block // Set up the table as one transaction and wait to see the events ... conn.execute("DROP TABLE IF EXISTS person", "CREATE TABLE person (" + " name VARCHAR(255) primary key," + " age INTEGER NULL DEFAULT 10," + " createdAt DATETIME NULL DEFAULT CURRENT_TIMESTAMP," + " updatedAt DATETIME NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" + ")"); counters.consume(2, EventType.QUERY); counters.reset(); }
this.client = new BinaryLogClient(mysqlConfig.host, mysqlConfig.port, mysqlConfig.user, mysqlConfig.password); this.client.setSSLMode(mysqlConfig.sslMode); String gtidStr = startBinlog.getGtidSetStr(); LOGGER.info("Setting initial gtid to: " + gtidStr); this.client.setGtidSet(gtidStr); this.gtidPositioning = true; } else { LOGGER.info("Setting initial binlog pos to: " + startBinlog.getFile() + ":" + startBinlog.getOffset()); this.client.setBinlogFilename(startBinlog.getFile()); this.client.setBinlogPosition(startBinlog.getOffset()); this.gtidPositioning = false; this.client.setKeepAlive(false); EventDeserializer.CompatibilityMode.INVALID_DATE_AND_TIME_AS_MIN_VALUE ); this.client.setEventDeserializer(eventDeserializer); this.binlogEventListener = new BinlogConnectorEventListener(client, queue, metrics, outputConfig); this.client.setBlocking(!stopOnEOF); this.client.registerEventListener(binlogEventListener); this.client.registerLifecycleListener(binlogLifecycleListener); this.client.setServerId(replicaServerID.intValue());
username = ObjectUtils.firstNonNull(username, ""); password = ObjectUtils.firstNonNull(password, ""); this.client = new BinaryLogClient(host, port, catalog, username, password); this.lifecycleListener = new MySQLBinaryLogLifecycleListener(cache); client.setServerId(RANDOM.nextLong()); client.registerLifecycleListener(lifecycleListener); client.registerEventListener(new MySQLBinaryLogEventListener(database, cache, catalog)); eventDataDeserializers.put(EventType.GTID, new GtidEventDataDeserializer()); client.setEventDeserializer( new EventDeserializer( new EventHeaderV4Deserializer(),
client = new BinaryLogClient(connectionContext.hostname(), connectionContext.port(), connectionContext.username(), connectionContext.password()); client.setThreadFactory(Threads.threadFactory(MySqlConnector.class, context.getConnectorConfig().getLogicalName(), "binlog-client", false)); client.setServerId(serverId); client.setSSLMode(sslModeFor(connectionContext.sslMode())); client.setKeepAlive(context.config().getBoolean(MySqlConnectorConfig.KEEP_ALIVE)); client.setKeepAliveInterval(context.config().getLong(MySqlConnectorConfig.KEEP_ALIVE_INTERVAL_MS)); client.registerEventListener(context.bufferSizeForBinlogReader() == 0 ? this::handleEvent : (new EventBuffer(context.bufferSizeForBinlogReader(), this))::add); client.registerLifecycleListener(new ReaderThreadLifecycleListener()); if (logger.isDebugEnabled()) client.registerEventListener(this::logEvent); new RowDeserializers.DeleteRowsDeserializer( tableMapEventByTableId).setMayContainExtraInformation(true)); client.setEventDeserializer(eventDeserializer);
@Override public void connect(Checkpoint checkpoint) { if (this.client == null || !this.client.isConnected()) { this.executor.submit(() -> { for (String hostname : this.hostname) { this.client.registerEventListener( event -> { try { this.client.setServerId(checkpoint.getServerId()); this.client.setBinlogFilename(checkpoint.getBinlog().getFilename()); this.client.setBinlogPosition(checkpoint.getBinlog().getPosition()); this.client.setGtidSet(checkpoint.getGTID().getValue()); this.client.connect();
protected void rewindBinaryLogClient(BinlogPosition position) { try { if (isRunning()) { logger.debug("Rewinding binlog to position {}", position); client.disconnect(); client.setBinlogFilename(position.getFilename()); client.setBinlogPosition(position.getPosition()); client.connect(); } } catch (IOException e) { logger.error("Unexpected error when re-connecting to the MySQL binary log reader", e); } }
private void checkConnection() throws Exception { if (!binaryLogClient.isConnected()) { BinlogPosition binlogPosition = replicator.getNextBinlogPosition(); if (binlogPosition != null) { binaryLogClient.setBinlogFilename(binlogPosition.getBinlogFilename()); binaryLogClient.setBinlogPosition(binlogPosition.getPosition()); } binaryLogClient.connect(3000); } }
client.setGtidSet(filteredGtidSetStr); source.setCompletedGtidSet(filteredGtidSetStr); gtidSet = new com.github.shyiko.mysql.binlog.GtidSet(filteredGtidSetStr); } else { client.setBinlogFilename(source.binlogFilename()); client.setBinlogPosition(source.binlogPosition()); gtidSet = new com.github.shyiko.mysql.binlog.GtidSet(""); client.setBinlogFilename(source.binlogFilename()); client.setBinlogPosition(source.binlogPosition()); try { logger.debug("Attempting to establish binlog reader connection with timeout of {} ms", timeoutInMilliseconds); client.connect(context.timeoutInMilliseconds()); } catch (TimeoutException e) {
@Override protected void beforeStop() throws Exception { this.binlogEventListener.stop(); this.client.disconnect(); }
@Override protected void doStop() { try { if (client.isConnected()) { logger.debug("Stopping binlog reader '{}', last recorded offset: {}", this.name(), lastOffset); client.disconnect(); } cleanupResources(); } catch (IOException e) { logger.error("Unexpected error when disconnecting from the MySQL binary log reader '{}'", this.name(), e); } }
public void startReplicator() throws Exception { this.client.connect(5000); replicatorStarted = true; }
try { event = eventDeserializer.nextEvent(packetLength == MAX_PACKET_LENGTH ? new ByteArrayInputStream(readPacketSplitInChunks(inputStream, packetLength - 1)) : inputStream); if (event == null) { throw e; if (isConnected()) { for (LifecycleListener lifecycleListener : lifecycleListeners) { lifecycleListener.onEventDeserializationFailure(this, e); if (isConnected()) { eventLastSeen = System.currentTimeMillis(); updateGtidSet(event); notifyEventListeners(event); updateClientBinlogFilenameAndPosition(event); if (isConnected()) { for (LifecycleListener lifecycleListener : lifecycleListeners) { lifecycleListener.onCommunicationFailure(this, e); if (isConnected()) { if (completeShutdown) { disconnect(); // initiate complete shutdown sequence (which includes keep alive thread) } else { disconnectChannel();
/** * Test case that is normally commented out since it is only useful to print out the DDL statements recorded by * the binlog during a MySQL server initialization and startup. * * @throws Exception if there are problems */ @Ignore @Test public void shouldCaptureQueryEventData() throws Exception { // Testing.Print.enable(); startClient(client -> { client.setBinlogFilename("mysql-bin.000001"); client.setBinlogPosition(4); }); counters.consumeAll(5, TimeUnit.SECONDS); List<QueryEventData> allQueryEvents = recordedEventData(QueryEventData.class, -1); allQueryEvents.forEach(event -> { String sql = event.getSql(); if (sql.equalsIgnoreCase("BEGIN") || sql.equalsIgnoreCase("COMMIT")) return; System.out.println(event.getSql()); }); }
private BinaryLogClient getClient(String hostname) { // TODO: Implement status variable parser: https://github.com/shyiko/mysql-binlog-connector-java/issues/174 BinaryLogClient client = new BinaryLogClient( hostname, this.port, this.schema, this.username, this.password ); EventDeserializer eventDeserializer = new EventDeserializer(); eventDeserializer.setCompatibilityMode(EventDeserializer.CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY); client.setEventDeserializer(eventDeserializer); return client; }