Skip to content

Commit

Permalink
Merge pull request #7 from krowinski/mariadb
Browse files Browse the repository at this point in the history
Mariadb gtid support
  • Loading branch information
krowinski authored Aug 26, 2016
2 parents bd72455 + 168bbc9 commit 643a43c
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ Based on a great work of creators:https://github.com/noplay/python-mysql-repli
Installation
=========

In you project

```sh
composer require krowinski/php-mysql-replication
```

or standalone

```sh
git clone https://github.com/krowinski/php-mysql-replication.git
```

MySQL server settings
=========

Expand All @@ -33,7 +41,7 @@ Mysql replication events explained
Configuration
=========

You can pass this params to ConfigService->makeConfigFromArray([])
You can pass this array keys to ConfigService->makeConfigFromArray([])

'user' - your mysql user (mandatory)

Expand All @@ -49,6 +57,8 @@ You can pass this params to ConfigService->makeConfigFromArray([])

'gtid' - GTID marker(s) to start from (format 9b1c8d18-2a76-11e5-a26b-000c2976f3f3:1-177592)

'mariaDbGtid' - MariaDB GTID marker(s) to start from (format 1-1-3,0-1-88)

'slaveId' - script slave id for identification

'binLogFileName' - bin log file name to start from
Expand Down
1 change: 1 addition & 0 deletions example/dump_events.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'user' => 'root',
'ip' => '127.0.0.1',
'password' => 'root',
//'mariaDbGtid' => '1-1-3,0-1-88',
//'gtid' => '9b1c8d18-2a76-11e5-a26b-000c2976f3f3:1-177592',
])
);
Expand Down
8 changes: 8 additions & 0 deletions src/MySQLReplication/BinLog/BinLogConnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ private function getBinlogStream()
$binFilePos = $this->config->getBinLogPosition();
$binFileName = $this->config->getBinLogFileName();

if ('' !== $this->config->getMariaDbGtid())
{
$this->execute("SET @mariadb_slave_capability = 4");
$this->execute("SET @slave_connect_state = '" . $this->config->getMariaDbGtid() . "'");
$this->execute("SET @slave_gtid_strict_mode = 0");
$this->execute("SET @slave_gtid_ignore_duplicates = 0");
}

if ('' === $binFilePos || '' === $binFileName)
{
$master = $this->mySQLRepository->getMasterStatus();
Expand Down
19 changes: 19 additions & 0 deletions src/MySQLReplication/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class Config
* @var array
*/
private $databasesOnly;
/**
* @var string
*/
private $mariaDbGtid;

/**
* Config constructor.
Expand All @@ -76,6 +80,7 @@ class Config
* @param string $dbName
* @param string $charset
* @param string $gtid
* @param string $mariaGtid
* @param int $slaveId
* @param string $binLogFileName
* @param $binLogPosition
Expand All @@ -92,6 +97,7 @@ public function __construct(
$dbName,
$charset,
$gtid,
$mariaGtid,
$slaveId,
$binLogFileName,
$binLogPosition,
Expand All @@ -114,6 +120,7 @@ public function __construct(
$this->eventsIgnore = $eventsIgnore;
$this->tablesOnly = $tablesOnly;
$this->databasesOnly = $databasesOnly;
$this->mariaDbGtid = $mariaGtid;
}

/**
Expand Down Expand Up @@ -167,6 +174,10 @@ public function validate()
{
throw new ConfigException(ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE);
}
if (!empty($this->mariaDbGtid) && false === is_string($this->mariaDbGtid))
{
throw new ConfigException(ConfigException::MARIADBGTID_ERROR_MESSAGE, ConfigException::MARIADBGTID_ERROR_CODE);
}
}

/**
Expand Down Expand Up @@ -225,6 +236,14 @@ public function getGtid()
return $this->gtid;
}

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

/**
* @return int
*/
Expand Down
12 changes: 12 additions & 0 deletions src/MySQLReplication/Config/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class ConfigBuilder
* @var array
*/
private $databasesOnly = [];
/**
* @var string
*/
private $mariaDbGtid;

/**
* @return Config
Expand All @@ -78,6 +82,7 @@ public function build()
$this->dbName,
$this->charset,
$this->gtid,
$this->mariaDbGtid,
$this->slaveId,
$this->binLogFileName,
$this->binLogPosition,
Expand Down Expand Up @@ -199,4 +204,11 @@ public function withDatabasesOnly(array $databasesOnly)
$this->databasesOnly = $databasesOnly;
}

/**
* @param string $mariaDbGtid
*/
public function withMariaDbGtid($mariaDbGtid)
{
$this->mariaDbGtid = $mariaDbGtid;
}
}
4 changes: 4 additions & 0 deletions src/MySQLReplication/Config/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function makeConfigFromArray(array $config)
{
$configBuilder->withDatabasesOnly($v);
}
if ('mariaDbGtid' === $k)
{
$configBuilder->withMariaDbGtid($v);
}
}

return $configBuilder->build();
Expand Down
2 changes: 2 additions & 0 deletions src/MySQLReplication/Config/Exception/ConfigException.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ class ConfigException extends MySQLReplicationException
const BIN_LOG_FILE_NAME_ERROR_CODE = 9;
const BIN_LOG_FILE_POSITION_ERROR_MESSAGE = 'Incorrect binlog position type';
const BIN_LOG_FILE_POSITION_ERROR_CODE = 10;
const MARIADBGTID_ERROR_MESSAGE = 'Maria gtid must be string';
const MARIADBGTID_ERROR_CODE = 11;
}
3 changes: 3 additions & 0 deletions src/MySQLReplication/Definitions/ConstEventType.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,7 @@ class ConstEventType
const WRITE_ROWS_EVENT_V2 = 30;
const UPDATE_ROWS_EVENT_V2 = 31;
const DELETE_ROWS_EVENT_V2 = 32;

// mariadb
const MARIA_GTID_EVENT = 162;
}
1 change: 1 addition & 0 deletions src/MySQLReplication/Definitions/ConstEventsNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ class ConstEventsNames
const DELETE = 'delete';
const UPDATE = 'update';
const WRITE = 'write';
const MARIADB_GTID = 'mariadb gtid';
}
108 changes: 108 additions & 0 deletions src/MySQLReplication/Event/DTO/MariaDbGtidLogDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Created by PhpStorm.
* User: fazi
* Date: 25.08.2016
* Time: 21:10
*/

namespace MySQLReplication\Event\DTO;

use MySQLReplication\Definitions\ConstEventsNames;
use MySQLReplication\Event\EventInfo;

/**
* Class MariaGTIDLogDTO
* @package MySQLReplication\Event\DTO
*/
class MariaDbGtidLogDTO extends EventDTO
{
/**
* @var string
*/
private $type = ConstEventsNames::MARIADB_GTID;
/**
* @var int
*/
private $flag;
/**
* @var int
*/
private $domainId;
/**
* @var int
*/
private $sequenceNumber;

public function __construct(
EventInfo $eventInfo,
$flag,
$domainId,
$sequenceNumber
) {
parent::__construct($eventInfo);

$this->flag = $flag;
$this->domainId = $domainId;
$this->sequenceNumber = $sequenceNumber;
}

/**
* @return string
*/
public function __toString()
{
return PHP_EOL .
'=== Event ' . $this->getType() . ' === ' . PHP_EOL .
'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL .
'Log position: ' . $this->eventInfo->getPos() . PHP_EOL .
'Event size: ' . $this->eventInfo->getSize() . PHP_EOL .
'Flag: ' . var_export($this->flag, true) . PHP_EOL .
'Domain Id: ' . $this->domainId . PHP_EOL .
'Sequence Number: ' . $this->sequenceNumber . PHP_EOL;
}

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

/**
* 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);
}

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

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

/**
* @return int
*/
public function getDomainId()
{
return $this->domainId;
}
}
2 changes: 2 additions & 0 deletions src/MySQLReplication/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public function consume()
return (new GtidEvent($eventInfo, $binaryDataReader))->makeGTIDLogDTO();
} else if (ConstEventType::QUERY_EVENT === $eventInfo->getType()) {
return (new QueryEvent($eventInfo, $binaryDataReader))->makeQueryDTO();
} elseif (ConstEventType::MARIA_GTID_EVENT === $eventInfo->getType()) {
return (new MariaDbGtidEvent($eventInfo, $binaryDataReader))->makeMariaDbGTIDLogDTO();
}

return null;
Expand Down
29 changes: 29 additions & 0 deletions src/MySQLReplication/Event/MariaDbGtidEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace MySQLReplication\Event;

use MySQLReplication\Event\DTO\MariaDbGtidLogDTO;

/**
* Class MariaGtidEvent
* @package MySQLReplication\Event
*/
class MariaDbGtidEvent extends EventCommon
{
/**
* @return MariaDbGtidLogDTO
*/
public function makeMariaDbGTIDLogDTO()
{
$sequenceNumber = $this->binaryDataReader->readUInt64();
$domainId = $this->binaryDataReader->readUInt32();
$flag = $this->binaryDataReader->readUInt8();

return new MariaDbGtidLogDTO(
$this->eventInfo,
$flag,
$domainId,
$sequenceNumber
);
}
}

0 comments on commit 643a43c

Please sign in to comment.