Repository navigation
feat(db): add db:mysql-optimize (beta), to replace db:size --cleanup #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f46ee5
feat(db): prototype db:mysql-optimize, to replace db:size --cleanup
claude 9128d6e
feat(db): estimate reclaimable space from a sample of rows
pjcdawkins b06e5a0
fix(db): address review of db:mysql-optimize
pjcdawkins 6e0062f
fix(db): include free extents in the table size of db:mysql-optimize
pjcdawkins 037d752
fix(db): exclude a shared tablespace's free space from table sizes
pjcdawkins 95c5213
fix(db): rebuild the smallest tables first in db:mysql-optimize
pjcdawkins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,86 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Platformsh\Cli\Model\Mysql; | ||
|
|
||
| use Platformsh\Cli\Command\Db\DbMysqlOptimizeCommand; | ||
| use Platformsh\Cli\Service\Observability; | ||
|
|
||
| /** | ||
| * The disk and /tmp usage of a database service, from the metrics API. | ||
| */ | ||
| readonly class DiskHeadroom | ||
| { | ||
| public function __construct( | ||
| public float $diskUsed, | ||
| public float $diskLimit, | ||
| public ?float $tmpUsed = null, | ||
| public ?float $tmpLimit = null, | ||
| ) {} | ||
|
|
||
| /** | ||
| * Reads the usage from a service's metrics data point. | ||
| * | ||
| * @param array<mixed> $point | ||
| */ | ||
| public static function fromMetrics(array $point): ?self | ||
| { | ||
| $value = function (string $mountpoint, string $metric, string $agg) use ($point): ?float { | ||
| $v = Observability::nested($point, 'mountpoints', $mountpoint, $metric, $agg); | ||
| return is_numeric($v) ? (float) $v : null; | ||
| }; | ||
| $diskUsed = $value('/mnt', 'disk_used', 'avg'); | ||
| $diskLimit = $value('/mnt', 'disk_limit', 'max'); | ||
| if ($diskUsed === null || !$diskLimit) { | ||
| return null; | ||
| } | ||
| $tmpUsed = $value('/tmp', 'disk_used', 'avg'); | ||
| $tmpLimit = $value('/tmp', 'disk_limit', 'max'); | ||
| if ($tmpUsed === null || !$tmpLimit) { | ||
| $tmpUsed = $tmpLimit = null; | ||
| } | ||
|
|
||
| return new self($diskUsed, $diskLimit, $tmpUsed, $tmpLimit); | ||
| } | ||
|
|
||
| public function diskFree(): float | ||
| { | ||
| return max(0, $this->diskLimit - $this->diskUsed); | ||
| } | ||
|
|
||
| public function hasTmp(): bool | ||
| { | ||
| return $this->tmpLimit !== null; | ||
| } | ||
|
|
||
| public function tmpFree(): ?float | ||
| { | ||
| return $this->tmpLimit !== null ? max(0, $this->tmpLimit - (float) $this->tmpUsed) : null; | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether the disk has room for a rebuild, keeping a reserve free. | ||
| */ | ||
| public function diskFits(int $needed): bool | ||
| { | ||
| return self::fits($this->diskUsed, $this->diskLimit, $needed); | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether /tmp has room for a rebuild, or null if its usage is unknown. | ||
| */ | ||
| public function tmpFits(int $needed): ?bool | ||
| { | ||
| if ($this->tmpLimit === null) { | ||
| return null; | ||
| } | ||
|
|
||
| return self::fits((float) $this->tmpUsed, $this->tmpLimit, $needed); | ||
| } | ||
|
|
||
| private static function fits(float $used, float $limit, int $needed): bool | ||
| { | ||
| return $used + $needed <= $limit * (100 - DbMysqlOptimizeCommand::RESERVE_PERCENT) / 100; | ||
| } | ||
| } |
This file contains hidden or 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,57 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Platformsh\Cli\Model\Mysql; | ||
|
|
||
| /** | ||
| * Settings of a MySQL or MariaDB server, read from its global variables and status. | ||
| */ | ||
| readonly class MysqlServerInfo | ||
| { | ||
| public string $version; | ||
| public bool $isMariaDB; | ||
| /** Whether innodb_file_per_table is ON, or null if it is unknown. */ | ||
| public ?bool $filePerTable; | ||
| public bool $wsrepOn; | ||
| public int $clusterSize; | ||
| /** Whether information_schema_stats_expiry exists (MySQL 8+). */ | ||
| public bool $hasStatsExpiry; | ||
| private ?string $tmpdir; | ||
|
|
||
| /** | ||
| * @param array<string, string> $vars Variables and status values, by lowercase name. | ||
| */ | ||
| public function __construct(array $vars) | ||
| { | ||
| $this->version = $vars['version'] ?? ''; | ||
| $this->isMariaDB = stripos($this->version . ' ' . ($vars['version_comment'] ?? ''), 'mariadb') !== false; | ||
| $this->filePerTable = isset($vars['innodb_file_per_table']) ? self::bool($vars['innodb_file_per_table']) : null; | ||
| $this->wsrepOn = isset($vars['wsrep_on']) && self::bool($vars['wsrep_on']); | ||
| $this->clusterSize = (int) ($vars['wsrep_cluster_size'] ?? 0); | ||
| $this->hasStatsExpiry = isset($vars['information_schema_stats_expiry']); | ||
| $tmpdir = ($vars['innodb_tmpdir'] ?? '') !== '' ? $vars['innodb_tmpdir'] : ($vars['tmpdir'] ?? ''); | ||
| $this->tmpdir = $tmpdir !== '' ? $tmpdir : null; | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether the server is a node of a Galera cluster with more than one node. | ||
| */ | ||
| public function isGaleraCluster(): bool | ||
| { | ||
| return $this->wsrepOn && $this->clusterSize > 1; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the directory for temporary files of online DDL (innodb_tmpdir, or tmpdir). | ||
| */ | ||
| public function tmpdir(): ?string | ||
| { | ||
| return $this->tmpdir; | ||
| } | ||
|
|
||
| private static function bool(string $value): bool | ||
| { | ||
| return \in_array(strtoupper($value), ['ON', '1', 'TRUE', 'YES'], true); | ||
| } | ||
| } |
This file contains hidden or 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,165 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Platformsh\Cli\Model\Mysql; | ||
|
|
||
| use Platformsh\Cli\Command\Db\DbMysqlOptimizeCommand; | ||
|
|
||
| /** | ||
| * An InnoDB table with reclaimable space, from information_schema. | ||
| */ | ||
| readonly class TableCandidate | ||
| { | ||
| /** The ratio of a rebuilt table's data size to the bytes in its rows (for headers and free space in pages). */ | ||
| public const PAGE_OVERHEAD = 1.5; | ||
|
|
||
| /** The disk space needed for a rebuild, beyond the estimated size, as a percentage and in bytes (for the file's free extents). */ | ||
| public const DISK_MARGIN_PERCENT = 10; | ||
| public const DISK_MARGIN_BYTES = 8 * 1024 * 1024; | ||
|
|
||
| public function __construct( | ||
| public string $schema, | ||
| public string $name, | ||
| public int $dataLength, | ||
| public int $indexLength, | ||
| public int $dataFree, | ||
| /** The estimated number of rows (TABLE_ROWS). */ | ||
| public int $rows = 0, | ||
| /** The number of FULLTEXT or SPATIAL indexes, which prevent an online rebuild. */ | ||
| public int $offlineIndexes = 0, | ||
| /** The number of other tables reporting the same free space (a sign of a shared tablespace). */ | ||
| public int $sameFree = 0, | ||
| /** The average bytes per row, from a sample of rows, or null if unknown. */ | ||
| public ?float $avgRowBytes = null, | ||
| /** Whether the sample is the first rows only, rather than spread across the table. */ | ||
| public bool $sampledFirstRows = false, | ||
| ) {} | ||
|
|
||
| /** | ||
| * @param string[] $row | ||
| */ | ||
| public static function fromRow(array $row): self | ||
| { | ||
| return new self($row[0], $row[1], (int) $row[2], (int) $row[3], (int) $row[4], (int) $row[5], (int) $row[6], (int) $row[7]); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a copy with the result of a sample of rows. | ||
| */ | ||
| public function withSample(float $avgRowBytes, bool $firstRows): self | ||
| { | ||
| return new self($this->schema, $this->name, $this->dataLength, $this->indexLength, $this->dataFree, $this->rows, $this->offlineIndexes, $this->sameFree, $avgRowBytes, $firstRows); | ||
| } | ||
|
|
||
| /** | ||
| * The size of the table's data, indexes and free extents (about the size of its file). | ||
| * | ||
| * In a shared tablespace, the free extents are the tablespace's, so they are not counted. | ||
| */ | ||
| public function size(MysqlServerInfo $server): int | ||
| { | ||
| if ($this->inSharedTablespace($server)) { | ||
| return $this->dataLength + $this->indexLength; | ||
| } | ||
|
|
||
| return $this->dataLength + $this->indexLength + $this->dataFree; | ||
| } | ||
|
|
||
| /** | ||
| * The estimated size of the table's data and indexes after a rebuild, or null if unknown. | ||
| * | ||
| * Secondary indexes are assumed to shrink in the same ratio as the data. | ||
| */ | ||
| public function rebuiltSize(): ?int | ||
| { | ||
| if ($this->avgRowBytes === null) { | ||
| return null; | ||
| } | ||
| $data = (int) ceil($this->avgRowBytes * $this->rows * self::PAGE_OVERHEAD); | ||
|
pjcdawkins marked this conversation as resolved.
Resolved
|
||
| $index = $this->dataLength > 0 ? (int) round($this->indexLength * min(1, $data / $this->dataLength)) : $this->indexLength; | ||
|
|
||
| return $data + $index; | ||
| } | ||
|
|
||
| /** | ||
| * The estimated disk space that a rebuild would return. | ||
| * | ||
| * Free extents (DATA_FREE) are always reclaimable. Free space inside | ||
| * pages is only counted if rows were sampled. | ||
| */ | ||
| public function reclaimable(): int | ||
| { | ||
| $rebuilt = $this->rebuiltSize(); | ||
| if ($rebuilt === null) { | ||
| return $this->dataFree; | ||
| } | ||
|
|
||
| return max($this->dataFree, $this->dataLength + $this->indexLength + $this->dataFree - $rebuilt); | ||
| } | ||
|
|
||
| /** | ||
| * The disk space needed for the rebuilt copy of the table. | ||
| */ | ||
| public function diskNeeded(): int | ||
| { | ||
| $rebuilt = $this->rebuiltSize(); | ||
| if ($rebuilt === null) { | ||
| return $this->dataLength + $this->indexLength; | ||
| } | ||
|
|
||
| return $rebuilt + intdiv($rebuilt * self::DISK_MARGIN_PERCENT, 100) + self::DISK_MARGIN_BYTES; | ||
| } | ||
|
|
||
| /** | ||
| * The temporary space that may be needed, to sort the secondary indexes. | ||
| * | ||
| * Concurrent writes during the rebuild are also logged to a temporary | ||
| * file (up to innodb_online_alter_log_max_size), which is not counted. | ||
| */ | ||
| public function tmpNeeded(): int | ||
| { | ||
| return $this->indexLength; | ||
| } | ||
|
|
||
| public function displayName(): string | ||
| { | ||
| return $this->schema . '.' . $this->name; | ||
| } | ||
|
|
||
| public function quotedName(): string | ||
| { | ||
| return DbMysqlOptimizeCommand::quoteIdentifier($this->schema) . '.' . DbMysqlOptimizeCommand::quoteIdentifier($this->name); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the query that rebuilds the table online, or fails. | ||
| */ | ||
| public function rebuildQuery(): string | ||
| { | ||
| return 'ALTER TABLE ' . $this->quotedName() . ' FORCE, ALGORITHM=INPLACE, LOCK=NONE;'; | ||
| } | ||
|
|
||
| /** | ||
| * Whether the table is probably in a shared tablespace, which a rebuild does not shrink. | ||
| */ | ||
| public function inSharedTablespace(MysqlServerInfo $server): bool | ||
| { | ||
| return $this->sameFree > 0 || $server->filePerTable === false; | ||
| } | ||
|
|
||
| /** | ||
| * Returns why the table would not be rebuilt, or null. | ||
| */ | ||
| public function skipReason(MysqlServerInfo $server): ?string | ||
| { | ||
| if ($this->inSharedTablespace($server)) { | ||
| return 'probably in a shared tablespace: a rebuild would not reclaim space'; | ||
| } | ||
| if ($this->offlineIndexes > 0) { | ||
| return 'has a FULLTEXT or SPATIAL index: it cannot be rebuilt without blocking writes'; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.