Skip to content
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

avoid master/slave terminology #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# syntax = docker/dockerfile:experimental
ARG PHP_VERSION=7.3.10
ARG COMPOSER_VERSION=1.9.0
ARG COMPOSER_VERSION=1.10.10

# PHPの土台をつくるステージ
FROM php:${PHP_VERSION}-cli AS base
Expand All @@ -14,9 +13,6 @@ RUN docker-php-source extract \
&& docker-php-ext-enable redis xdebug \
&& docker-php-source delete

# composerの準備をするステージ
FROM base as composer

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
git \
ssh \
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ $manager->getSlave()->get("key"); // master-host, use master
## Test

```php
docker-compose up -d --scale redis-slave1=4 redis-slave1
docker-compose up -d --scale redis-replica1=4 redis-replica1
docker-compose run --rm phpunit-full
```

Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "wfs/master-slave-redis",
"name": "wfs/primary-replica-redis",
"version": "1.0.0",
"description": "",
"type": "library",
Expand All @@ -22,8 +22,13 @@
},
"autoload": {
"psr-4": {
"Wfs\\MasterSlaveRedis\\": "src/",
"Wfs\\MasterSlaveRedis\\Tests\\": "tests/"
"Wfs\\PrimaryReplicaRedis\\": "src/",
"Wfs\\PrimaryReplicaRedis\\Tests\\": "tests/"
}
},
"scripts": {
"phpcs": "phpcs --standard=PSR12 src",
"phpcbf": "phpcbf --standard=PSR12 src",
"phan": "phan"
}
}
27 changes: 17 additions & 10 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
version: '3.4'

services:
redis-master:
redis-primary:
image: redis:5.0.6-alpine
redis-slave1:
redis-replica1:
image: redis:5.0.6-alpine
depends_on:
- redis-master
command: redis-server --slaveof redis-master 6379
redis-slave2:
- redis-primary
command: redis-server --replicaof redis-primary 6379
redis-replica2:
image: redis:5.0.6-alpine
depends_on:
- redis-master
command: redis-server --slaveof redis-master 6379
- redis-primary
command: redis-server --replicaof redis-primary 6379
phpunit-full:
image: phpunit
build: .
volumes:
- .:/var/www/html
depends_on:
- redis-master
- redis-slave1
- redis-slave2
- redis-primary
- redis-replica1
- redis-replica2
command: ./vendor/bin/phpunit --testsuite Medium --coverage-text
phpunit:
image: phpunit
build: .
volumes:
- .:/var/www/html
command: ./vendor/bin/phpunit --testsuite Small --coverage-text
composer:
image: phpunit
build: .
volumes:
- .:/var/www/html
entrypoint: composer

50 changes: 27 additions & 23 deletions src/ConfigManipulator.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php declare(strict_types=1);
<?php

namespace Wfs\MasterSlaveRedis;
declare(strict_types=1);

namespace Wfs\PrimaryReplicaRedis;

final class ConfigManipulator
{
const PRIMARY_KEY = 'primary';
const REPLICA_KEY = 'replica';
const DEFAULT_PORT = 6379;
const DEFAULT_TIMEOUT = 0;

Expand Down Expand Up @@ -32,12 +36,12 @@ private static function fillHostDefaultValue(array $assocHostConfig): array

/**
* @param array $assocConfig = [
* "master" => [
* "primary" => [
* "host" => "host-name",
* "port" => 6379,
* "timeout" => 3,
* ],
* "slave" => [
* "replica" => [
* [
* "host" => "host-name",
* "port" => 6379,
Expand All @@ -51,20 +55,20 @@ private static function fillHostDefaultValue(array $assocHostConfig): array
* "timeout" => 3,
* ]
*/
public static function pickupMasterConfig(array $assocConfig): array
public static function pickupPrimaryConfig(array $assocConfig): array
{
self::throwIfInvalidConfig($assocConfig);
return self::fillHostDefaultValue($assocConfig['master']);
return self::fillHostDefaultValue($assocConfig[self::PRIMARY_KEY]);
}

/**
* @param array $assocConfig = [
* "master" => [
* "primary" => [
* "host" => "host-name",
* "port" => 6379,
* "timeout" => 3,
* ],
* "slave" => [
* "replica" => [
* [
* "host" => "host-name",
* "port" => 6379,
Expand All @@ -78,18 +82,18 @@ public static function pickupMasterConfig(array $assocConfig): array
* "timeout" => 3,
* ]
*/
public static function pickupSlaveConfig(array $assocConfig): array
public static function pickupReplicaConfig(array $assocConfig): array
{
self::throwIfInvalidConfig($assocConfig);

// 空ならmasterを使う
if (! isset($assocConfig['slave']) || count($assocConfig['slave']) === 0) {
return self::pickupMasterConfig($assocConfig);
// 空ならprimaryを使う
if (! isset($assocConfig[self::REPLICA_KEY]) || count($assocConfig[self::REPLICA_KEY]) === 0) {
return self::pickupPrimaryConfig($assocConfig);
}

// 乱択
$index = array_rand($assocConfig['slave']);
return self::fillHostDefaultValue($assocConfig['slave'][$index]);
$index = array_rand($assocConfig[self::REPLICA_KEY]);
return self::fillHostDefaultValue($assocConfig[self::REPLICA_KEY][$index]);
}

private static function isValidHostConfig(array $assocHostConfig, bool $checkOptionalKeys = false): bool
Expand Down Expand Up @@ -127,25 +131,25 @@ private static function isValidHostConfig(array $assocHostConfig, bool $checkOpt

private static function isValidConfig(array $assocConfig, bool $checkOptionalKeys = false): bool
{
// masterは必須。中身も正しく
if (! array_key_exists('master', $assocConfig)) {
// primaryは必須。中身も正しく
if (! array_key_exists(self::PRIMARY_KEY, $assocConfig)) {
return false;
}
if (! self::isValidHostConfig($assocConfig['master'], $checkOptionalKeys)) {
if (! self::isValidHostConfig($assocConfig[self::PRIMARY_KEY], $checkOptionalKeys)) {
return false;
}

// slaveも必須だが、空でもよい
if (! array_key_exists('slave', $assocConfig)) {
// replicaも必須だが、空でもよい
if (! array_key_exists(self::REPLICA_KEY, $assocConfig)) {
return false;
}
if (! is_iterable($assocConfig['slave'])) {
if (! is_iterable($assocConfig[self::REPLICA_KEY])) {
return false;
}

// 存在しているslave設定は正しくなければならない
foreach ($assocConfig['slave'] as $assocSlaveConfig) {
if (! self::isValidHostConfig($assocSlaveConfig, $checkOptionalKeys)) {
// 存在しているreplica設定は正しくなければならない
foreach ($assocConfig[self::REPLICA_KEY] as $assocReplicaConfig) {
if (! self::isValidHostConfig($assocReplicaConfig, $checkOptionalKeys)) {
return false;
}
}
Expand Down
28 changes: 16 additions & 12 deletions src/RedisManager.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php declare(strict_types=1);
<?php

namespace Wfs\MasterSlaveRedis;
declare(strict_types=1);

namespace Wfs\PrimaryReplicaRedis;

class RedisManager
{
Expand Down Expand Up @@ -57,11 +59,12 @@ private function connect(
* @return \Redis
* @throws RedisManagerException
*/
public function getMaster(int $retry = self::DEFAULT_RETRY_NUM,
int $retry_interval = self::DEFAULT_RETRY_INTERVAL): \Redis
{
$master = ConfigManipulator::pickupMasterConfig($this->config);
return $this->connect($master, $retry, $retry_interval);
public function getPrimary(
int $retry = self::DEFAULT_RETRY_NUM,
int $retry_interval = self::DEFAULT_RETRY_INTERVAL
): \Redis {
$primary = ConfigManipulator::pickupPrimaryConfig($this->config);
return $this->connect($primary, $retry, $retry_interval);
}

/**
Expand All @@ -70,10 +73,11 @@ public function getMaster(int $retry = self::DEFAULT_RETRY_NUM,
* @return \Redis
* @throws RedisManagerException
*/
public function getSlave(int $retry = self::DEFAULT_RETRY_NUM,
int $retry_interval = self::DEFAULT_RETRY_INTERVAL): \Redis
{
$slave = ConfigManipulator::pickupSlaveConfig($this->config);
return $this->connect($slave, $retry, $retry_interval);
public function getReplica(
int $retry = self::DEFAULT_RETRY_NUM,
int $retry_interval = self::DEFAULT_RETRY_INTERVAL
): \Redis {
$replica = ConfigManipulator::pickupReplicaConfig($this->config);
return $this->connect($replica, $retry, $retry_interval);
}
}
6 changes: 4 additions & 2 deletions src/RedisManagerException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php declare(strict_types=1);
<?php

namespace Wfs\MasterSlaveRedis;
declare(strict_types=1);

namespace Wfs\PrimaryReplicaRedis;

final class RedisManagerException extends \Exception
{
Expand Down
Loading