Skip to content

Commit 2ec0506

Browse files
committed
remove unnecessary gtid converting in BinlogHistoryGtidChildUpdater
1 parent 335f993 commit 2ec0506

File tree

5 files changed

+76
-14
lines changed

5 files changed

+76
-14
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5+
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6+
7+
## [1.0.1] - 2017-12-21
8+
9+
### Changed
10+
- remove unnecessary gtid converting in `BinlogHistoryGtidChildUpdater`
11+
- Change tab to space
12+
13+
## [1.0.0]

src/Binlog/Collector/Application/BinlogHistoryGtidUpdaterApplication.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
class BinlogHistoryGtidUpdaterApplication
1616
{
1717
const PARTITION_COUNT = 10;
18-
const CHILD_MAX_ALLOW_ERROR_COUNT = 10;
1918
const CHILD_ONCE_BINLOG_FETCH_LIMIT = 1000;
2019
const MAX_COUNT_PER_CHILD = 100000;
2120
const MIN_COUNT_PER_CHILD = 1000;

src/Binlog/Collector/BinlogHistoryGtidChildUpdater.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
namespace Binlog\Collector;
44

55
use Binlog\Collector\Interfaces\BinlogHistoryServiceInterface;
6+
use Binlog\Collector\Model\ReplicationDbModel;
67
use Monolog\Logger;
78
use MySQLReplication\Config\Config;
8-
use Binlog\Collector\Model\ReplicationDbModel;
99

1010
/**
1111
* Class BinlogHistoryGtidChildUpdater
1212
* @package Binlog\Collector
1313
*/
1414
class BinlogHistoryGtidChildUpdater
1515
{
16-
const CHILD_MAX_ALLOW_ERROR_COUNT = 10;
1716
const CHILD_ONCE_BINLOG_FETCH_LIMIT = 1000;
1817
const MAX_COUNT_PER_CHILD = 100000;
1918
const MIN_COUNT_PER_CHILD = 1000;
@@ -25,8 +24,6 @@ class BinlogHistoryGtidChildUpdater
2524
/** @var Logger */
2625
private $logger;
2726
/** @var int */
28-
private $error_count;
29-
/** @var int */
3027
private $remain_binlog_count;
3128
/** @var BinlogHistoryServiceInterface */
3229
private $binlog_history_service;
@@ -56,7 +53,6 @@ private function getFetchCount(): int
5653

5754
public function execute(int $last_binlog_id, int $max_binlog_count)
5855
{
59-
$this->error_count = 0;
6056
$this->remain_binlog_count = $max_binlog_count;
6157
$fetch_count = $this->getFetchCount();
6258
$is_first = true;
@@ -91,7 +87,9 @@ public function execute(int $last_binlog_id, int $max_binlog_count)
9187
*/
9288
private function calculateGtidAndUpdate(array $dicts): bool
9389
{
94-
foreach ($dicts as $dict) {
90+
$new_dicts = self::sortDescendingByBinlogFileNameAndGtidEndPos($dicts);
91+
92+
foreach ($new_dicts as $dict) {
9593
$id = intval($dict['id']);
9694
$binlog_file_name = $dict['binlog_filename'];
9795
$binlog_position = intval($dict['gtid_end_pos']);
@@ -102,18 +100,35 @@ private function calculateGtidAndUpdate(array $dicts): bool
102100
);
103101
$this->binlog_history_service->updateBinlogGtid($id, $binlog_offset_dto->mariadb_gtid);
104102
} catch (\Exception $exception) {
105-
$this->error_count++;
106-
if ($this->error_count >= self::CHILD_MAX_ALLOW_ERROR_COUNT) {
107-
$this->logger->info('error 개수가 ' . $this->error_count . '개가 넘음');
103+
$this->logger->info(
104+
"Gtid ({$id}:{$binlog_file_name},{$binlog_position}) " .
105+
"변환 실패(오래된 Binlog Offset인 경우 발생 할 수 있습니다.)"
106+
);
108107

109-
return false;
110-
}
108+
return false;
111109
}
112110
}
113111

114112
return true;
115113
}
116114

115+
public static function sortDescendingByBinlogFileNameAndGtidEndPos(array $dicts): array
116+
{
117+
usort(
118+
$dicts,
119+
function ($a, $b) {
120+
$ret_val = $b['binlog_filename'] <=> $a['binlog_filename'];
121+
if ($ret_val === 0) {
122+
return $b['gtid_end_pos'] <=> $a['gtid_end_pos'];
123+
}
124+
125+
return $ret_val;
126+
}
127+
);
128+
129+
return $dicts;
130+
}
131+
117132
public function close()
118133
{
119134
$this->replication_db_model->close();

tests/Binlog/BinlogCollectorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace BinLog\Tests\Utils;
3+
namespace BinLog\Tests;
44

55
use PHPUnit\Framework\TestCase;
66

@@ -9,6 +9,6 @@ class BinlogCollectorTest extends TestCase
99

1010
public function test()
1111
{
12-
12+
$this->assertTrue(true);
1313
}
1414
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace BinLog\Tests;
4+
5+
use Binlog\Collector\BinlogHistoryGtidChildUpdater;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class BinlogHistoryGtidChildUpdaterTest extends TestCase
9+
{
10+
11+
public function testSortDescendingByBinlogFileNameAndGtidEndPos()
12+
{
13+
$dicts[] = $this->makeBinlogDict(4, 'mariadb-bin.007350', 10000001, '', '2017-09-14 17:14:42');
14+
$dicts[] = $this->makeBinlogDict(2, 'mariadb-bin.007351', 20000001, '', '2017-09-14 17:14:47');
15+
$dicts[] = $this->makeBinlogDict(3, 'mariadb-bin.007350', 10000002, '', '2017-09-14 17:14:45');
16+
$dicts[] = $this->makeBinlogDict(1, 'mariadb-bin.007351', 20000002, '', '2017-09-14 17:14:48');
17+
18+
$dicts = BinlogHistoryGtidChildUpdater::sortDescendingByBinlogFileNameAndGtidEndPos($dicts);
19+
$this->assertEquals(1, $dicts[0]['id']);
20+
$this->assertEquals(2, $dicts[1]['id']);
21+
$this->assertEquals(3, $dicts[2]['id']);
22+
$this->assertEquals(4, $dicts[3]['id']);
23+
}
24+
25+
public function makeBinlogDict(int $id, string $binlog_filename, int $gtid_end_pos, string $gtid, string $reg_date)
26+
{
27+
return [
28+
'id' => $id,
29+
'binlog_filename' => $binlog_filename,
30+
'gtid_end_pos' => $gtid_end_pos,
31+
'gtid' => $gtid,
32+
'reg_date' => $reg_date,
33+
];
34+
}
35+
}

0 commit comments

Comments
 (0)