-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shard MySQL for connect four context (#164)
Applies schema-based sharding for the connect four database #119. The application itself only knows about logical shards and relies on a proxy, such as ProxySQL, to forward queries to physical shards. * The env variable APP_CONNECT_FOUR_DOCTRINE_DBAL_SHARDS defines a comma-separated list of active shards. As games can be sharded across multiple physical databases, the transactional scope (technical-wise) is moved to the repository layer, and removed from the application layer. The changes to the repository, which is now very persistence oriented, aim to make this explicit. The shard selection happens based on the game id. * The env variable APP_CONNECT_FOUR_DOCTRINE_DBAL_DATABASE is introduced to allow selecting a shard for database creation and migration, as doctrine commands currently don't allow passing the database name via cli arguments. * Besides the repository, the event store access must also be aware of sharding, since this is read when the query model is not yet populated. For that matter, to query the events from the write model, the repository is used, which encapsulates the EventStore. * Remove query model's GameNotFoundException This is an aside refactoring, not related. This indirection wasn't needed in the past. Additionally, an instance of GameId is required to find a game, not a loose string. * The current implementation doesn't take resharding into consideration.
- Loading branch information
Showing
26 changed files
with
218 additions
and
241 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
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
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gaming\Common\Sharding\Exception; | ||
|
||
use Exception; | ||
|
||
final class ShardingException extends Exception | ||
{ | ||
} |
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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gaming\Common\Sharding\Integration; | ||
|
||
use Gaming\Common\Sharding\Exception\ShardingException; | ||
use Gaming\Common\Sharding\Shards; | ||
|
||
/** | ||
* This implementation causes problems in the event of re-sharding, | ||
* because it's very likely that values will be reassigned to another shard. | ||
* This can lead to more network calls until everything is back in place. | ||
* It's therefore not suitable for every use case and should be used with caution. | ||
* A better alternative would be to use a consistent hashing algorithm, | ||
* which would reduce the likelihood of values being reassigned to another shard. | ||
*/ | ||
final class Crc32ModShards implements Shards | ||
{ | ||
/** | ||
* @param string[] $shards | ||
* | ||
* @throws ShardingException | ||
*/ | ||
public function __construct( | ||
private readonly array $shards | ||
) { | ||
if (count($this->shards) === 0) { | ||
throw new ShardingException('At least one shard must be specified.'); | ||
} | ||
} | ||
|
||
public function lookup(string $value): string | ||
{ | ||
return $this->shards[crc32($value) % count($this->shards)]; | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gaming\Common\Sharding; | ||
|
||
use Gaming\Common\Sharding\Exception\ShardingException; | ||
|
||
interface Shards | ||
{ | ||
/** | ||
* @throws ShardingException | ||
*/ | ||
public function lookup(string $value): string; | ||
} |
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
11 changes: 0 additions & 11 deletions
11
src/ConnectFour/Application/Game/Query/Exception/GameNotFoundException.php
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.