Skip to content

Commit

Permalink
Next release (#34)
Browse files Browse the repository at this point in the history
- Removed unused (probably?) classes ConfigService, BinaryDataReaderService
- Changed Event class broke into smaller methods to be cleaner
- Added some unit test
- Added BinLogCurrent to keep current binlogFile, binlog position and gtid also added example how to resume script based on this data
- Moved to php 5.6 sorry.. the future is now ;)
  • Loading branch information
krowinski authored Mar 11, 2018
1 parent a6a24c5 commit 80ac3a9
Show file tree
Hide file tree
Showing 43 changed files with 959 additions and 371 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ log
composer.phar
composer.lock
.php_cs.cache
/example/profiler.php
/example/profiler.php
.idea/
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ dist: trusty
language: php

php:
- 5.5
- 5.6
- 7.0
- 7.1
env:
- DB=mysql57
- DB=mysql56
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

# Release Notes

## v4.0.0 (2018-03-10)
- Removed unused (probably?) classes ConfigService, BinaryDataReaderService
- Changed Event class broke into smaller methods to be cleaner
- Added some unit test
- Added BinLogCurrent to keep current binlogFile, binlog position and gtid also added example how to resume script based on this data
- Moved to php 5.6 sorry.. the future is now ;)

## v3.0.1 (2017-08-16)
- Fixed in config filter_var validation if 0 given
- Changed if bin log and bin log file not given then use master otherwise given data will be send to master
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ php-mysql-replication
[![Latest Stable Version](https://poser.pugx.org/krowinski/php-mysql-replication/v/stable)](https://packagist.org/packages/krowinski/php-mysql-replication) [![Total Downloads](https://poser.pugx.org/krowinski/php-mysql-replication/downloads)](https://packagist.org/packages/krowinski/php-mysql-replication) [![Latest Unstable Version](https://poser.pugx.org/krowinski/php-mysql-replication/v/unstable)](https://packagist.org/packages/krowinski/php-mysql-replication)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/4a0e49d4-3802-41d3-bb32-0a8194d0fd4d/mini.png)](https://insight.sensiolabs.com/projects/4a0e49d4-3802-41d3-bb32-0a8194d0fd4d) [![License](https://poser.pugx.org/krowinski/php-mysql-replication/license)](https://packagist.org/packages/krowinski/php-mysql-replication)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/krowinski/php-mysql-replication/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/krowinski/php-mysql-replication/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/krowinski/php-mysql-replication/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/krowinski/php-mysql-replication/?branch=master)

Pure PHP Implementation of MySQL replication protocol. This allow you to receive event like insert, update, delete with their data and raw SQL queries.

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"type": "library",
"require": {
"php": ">=5.5.9",
"php": ">=5.6",
"ext-sockets": "*",
"doctrine/dbal": "^2.5",
"doctrine/collections": "^1.3",
Expand All @@ -22,7 +22,7 @@
"psr/simple-cache": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8|^5.7"
"phpunit/phpunit": "^5.7"
},
"license": "MIT",
"authors": [
Expand Down
4 changes: 1 addition & 3 deletions example/benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ public function run()
*/
private function consume()
{
while (1) {
$this->binLogStream->consume();
}
$this->binLogStream->run();
}

/**
Expand Down
5 changes: 1 addition & 4 deletions example/dump_events.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,4 @@ public function allEvents(EventDTO $event)
$binLogStream->registerSubscriber(new MyEventSubscribers());

// start consuming events
while (1) {
$binLogStream->consume();
}

$binLogStream->run();
112 changes: 112 additions & 0 deletions example/resuming.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace example;

error_reporting(E_ALL);
date_default_timezone_set('UTC');
include __DIR__ . '/../vendor/autoload.php';

use MySQLReplication\BinLog\BinLogCurrent;
use MySQLReplication\Config\ConfigBuilder;
use MySQLReplication\Event\DTO\EventDTO;
use MySQLReplication\Event\EventSubscribers;
use MySQLReplication\MySQLReplicationFactory;

/**
* Your db configuration @see ConfigBuilder for more options
*/
$binLogStream = new MySQLReplicationFactory(
BinLogBootstrap::startFromPosition(new ConfigBuilder())
->withUser('root')
->withHost('127.0.0.1')
->withPassword('root')
->build()
);

/**
* Class BenchmarkEventSubscribers
* @package example
*/
class MyEventSubscribers extends EventSubscribers
{
/**
* @param EventDTO $event (your own handler more in EventSubscribers class )
*/
public function allEvents(EventDTO $event)
{
// all events got __toString() implementation
echo $event;

// all events got JsonSerializable implementation
//echo json_encode($event, JSON_PRETTY_PRINT);

echo 'Memory usage ' . round(memory_get_usage() / 1048576, 2) . ' MB' . PHP_EOL;

// save event for resuming it later
BinLogBootstrap::save($event->getEventInfo()->getBinLogCurrent());
}
}

/**
* Class SaveBinLogPos
* @package example
*/
class BinLogBootstrap
{
/**
* @var string
*/
private static $fileAndPath;

/**
* @return string
*/
private static function getFileAndPath()
{
if (null === self::$fileAndPath) {
self::$fileAndPath = sys_get_temp_dir() . '/bin-log-replicator-last-position';
}
return self::$fileAndPath;
}

/**
* @param BinLogCurrent $binLogCurrent
*/
public static function save(BinLogCurrent $binLogCurrent)
{

echo 'saving file:' . $binLogCurrent->getBinFileName() . ', position:' . $binLogCurrent->getBinLogPosition() . ' bin log position' . PHP_EOL;

// can be redis/nosql/file - something fast!
// to speed up you can save every xxx time
// you can also use signal handler for ctrl + c exiting script to wait for last event
file_put_contents(self::getFileAndPath(), serialize($binLogCurrent));
}

/**
* @param ConfigBuilder $builder
* @return ConfigBuilder
*/
public static function startFromPosition(ConfigBuilder $builder)
{
if (!is_file(self::getFileAndPath())) {
return $builder;
}

/** @var BinLogCurrent $binLogCurrent */
$binLogCurrent = unserialize(file_get_contents(self::getFileAndPath()));

echo 'starting from file:' . $binLogCurrent->getBinFileName() . ', position:' . $binLogCurrent->getBinLogPosition() . ' bin log position' . PHP_EOL;

return $builder
->withBinLogFileName($binLogCurrent->getBinFileName())
->withBinLogPosition($binLogCurrent->getBinLogPosition());
}
}

// register your events handler here
$binLogStream->registerSubscriber(new MyEventSubscribers());

// start consuming events
$binLogStream->run();

7 changes: 5 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
<testsuite name="Integration Suite">
<directory>./tests/Integration</directory>
</testsuite>
<testsuite name="Unit Suite">
<directory>./tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
Expand Down
103 changes: 103 additions & 0 deletions src/MySQLReplication/BinLog/BinLogCurrent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace MySQLReplication\BinLog;

/**
* Class BinLogCurrent
* @package MySQLReplication\BinLog
*/
class BinLogCurrent implements \JsonSerializable
{
/**
* @var int
*/
private $binLogPosition;
/**
* @var string
*/
private $binFileName;
/**
* @var string
*/
private $gtid;
/**
* @var string
*/
private $mariaDbGtid;

/**
* @return int
*/
public function getBinLogPosition()
{
return $this->binLogPosition;
}

/**
* @param int $binLogPosition
*/
public function setBinLogPosition($binLogPosition)
{
$this->binLogPosition = $binLogPosition;
}

/**
* @return string
*/
public function getBinFileName()
{
return $this->binFileName;
}

/**
* @param string $binFileName
*/
public function setBinFileName($binFileName)
{
$this->binFileName = $binFileName;
}

/**
* @return string
*/
public function getGtid()
{
return $this->gtid;
}

/**
* @param string $gtid
*/
public function setGtid($gtid)
{
$this->gtid = $gtid;
}

/**
* @return string
*/
public function getMariaDbGtid()
{
return $this->mariaDbGtid;
}

/**
* @param string $mariaDbGtid
*/
public function setMariaDbGtid($mariaDbGtid)
{
$this->mariaDbGtid = $mariaDbGtid;
}

/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize()
{
return get_object_vars($this);
}
}
Loading

0 comments on commit 80ac3a9

Please sign in to comment.