-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
Showing
43 changed files
with
959 additions
and
371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ log | |
composer.phar | ||
composer.lock | ||
.php_cs.cache | ||
/example/profiler.php | ||
/example/profiler.php | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,9 @@ dist: trusty | |
language: php | ||
|
||
php: | ||
- 5.5 | ||
- 5.6 | ||
- 7.0 | ||
- 7.1 | ||
env: | ||
- DB=mysql57 | ||
- DB=mysql56 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.