Skip to content

Commit

Permalink
Merge pull request #6 from 007hacky007/etcd-node-failover
Browse files Browse the repository at this point in the history
Etcd node failover
  • Loading branch information
matthi4s authored May 15, 2020
2 parents 29da9dd + b6c9171 commit 5f99582
Show file tree
Hide file tree
Showing 6 changed files with 395 additions and 10 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,24 @@ $shardedClient = new Aternos\Etcd\ShardedClient($clients);
$shardedClient->put("key", "value");
$shardedClient->get("key");
```

### Failover client

- automatically and transparently fails-over in case etcd host fails
```php
<?php

$clients = [
new Aternos\Etcd\Client("hostA:2379"),
new Aternos\Etcd\Client("hostB:2379"),
new Aternos\Etcd\Client("hostC:2379")
];
$failoverClient = new Aternos\Etcd\FailoverClient($clients);

// set 60 seconds as a hold-off period between another connection attempt to the failing host
// default is 120 seconds
// failing host is being remembered within FailoverClient object instance
$failoverClient->setHoldoffTime(60);
$failoverClient->put("key", "value");
$failoverClient->get("key");
```
21 changes: 11 additions & 10 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Aternos\Etcd;

use Aternos\Etcd\Exception\InvalidLeaseException;
use Aternos\Etcd\Exception\NoResponseException;
use Aternos\Etcd\Exception\Status\InvalidResponseStatusCodeException;
use Aternos\Etcd\Exception\Status\ResponseStatusCodeExceptionFactory;
use Etcdserverpb\AuthClient;
Expand All @@ -27,7 +29,6 @@
use Etcdserverpb\ResponseOp;
use Etcdserverpb\TxnRequest;
use Etcdserverpb\TxnResponse;
use Exception;
use Grpc\ChannelCredentials;
use Mvccpb\KeyValue;

Expand Down Expand Up @@ -197,7 +198,6 @@ public function delete(string $key)
* @param bool $returnNewValueOnFail
* @return bool|string
* @throws InvalidResponseStatusCodeException
* @throws \Exception
*/
public function putIf(string $key, string $value, $compareValue, bool $returnNewValueOnFail = false)
{
Expand All @@ -217,7 +217,6 @@ public function putIf(string $key, string $value, $compareValue, bool $returnNew
* @param bool $returnNewValueOnFail
* @return bool|string
* @throws InvalidResponseStatusCodeException
* @throws \Exception
*/
public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail = false)
{
Expand Down Expand Up @@ -270,8 +269,7 @@ public function revokeLeaseID(int $leaseID)
*
* @param int $leaseID
* @return int lease TTL
* @throws InvalidResponseStatusCodeException
* @throws Exception
* @throws InvalidResponseStatusCodeException|NoResponseException|InvalidLeaseException
*/
public function refreshLease(int $leaseID)
{
Expand All @@ -285,18 +283,21 @@ public function refreshLease(int $leaseID)
/** @var LeaseKeepAliveResponse $response */
$response = $leaseBidi->read();
$leaseBidi->cancel();
if(empty($response->getID()) || (int)$response->getID() !== $leaseID)
throw new Exception('Could not refresh lease ID: ' . $leaseID);
if($response === null || empty($response->getID()) || (int)$response->getID() !== $leaseID)
throw new NoResponseException('Could not refresh lease ID: ' . $leaseID);

if((int)$response->getTTL() === 0)
throw new InvalidLeaseException('Invalid lease ID or expired lease');

return (int)$response->getTTL();
}

/**
* Execute $requestOperations if Compare succeeds, execute $failureOperations otherwise if defined
*
* @param array $requestOperations operations to perform on success, array of RequestOp objects
* @param array|null $failureOperations operations to perform on failure, array of RequestOp objects
* @param array $compare array of Compare objects
* @param RequestOp[] $requestOperations operations to perform on success, array of RequestOp objects
* @param RequestOp[]|null $failureOperations operations to perform on failure, array of RequestOp objects
* @param Compare[] $compare array of Compare objects
* @return TxnResponse
* @throws InvalidResponseStatusCodeException
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Exception/InvalidLeaseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Aternos\Etcd\Exception;

/**
* Class InvalidLeaseException
*
* @package Aternos\Etcd\Exception
*/
class InvalidLeaseException extends \Exception
{

}
13 changes: 13 additions & 0 deletions src/Exception/NoClientAvailableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Aternos\Etcd\Exception;

/**
* Class NoClientAvailableException
*
* @package Aternos\Etcd\Exception
*/
class NoClientAvailableException extends \Exception
{

}
13 changes: 13 additions & 0 deletions src/Exception/NoResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Aternos\Etcd\Exception;

/**
* Class NoResponseException
*
* @package Aternos\Etcd\Exception
*/
class NoResponseException extends \Exception
{

}
Loading

0 comments on commit 5f99582

Please sign in to comment.