Skip to content

Commit

Permalink
Merge branch 'master' of github.com:aternosorg/php-etcd into if-custo…
Browse files Browse the repository at this point in the history
…m-compare

� Conflicts:
�	src/ClientInterface.php
�	src/ShardedClient.php
  • Loading branch information
007hacky007 committed May 1, 2020
2 parents 9ab9e1c + c96bcf9 commit 6decc1d
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 17 deletions.
96 changes: 93 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
use Etcdserverpb\DeleteRangeRequest;
use Etcdserverpb\DeleteRangeResponse;
use Etcdserverpb\KVClient;
use Etcdserverpb\LeaseClient;
use Etcdserverpb\LeaseGrantRequest;
use Etcdserverpb\LeaseGrantResponse;
use Etcdserverpb\LeaseKeepAliveRequest;
use Etcdserverpb\LeaseKeepAliveResponse;
use Etcdserverpb\LeaseRevokeRequest;
use Etcdserverpb\PutRequest;
use Etcdserverpb\PutResponse;
use Etcdserverpb\RangeRequest;
Expand All @@ -21,6 +27,7 @@
use Etcdserverpb\ResponseOp;
use Etcdserverpb\TxnRequest;
use Etcdserverpb\TxnResponse;
use Exception;
use Grpc\ChannelCredentials;

/**
Expand Down Expand Up @@ -63,6 +70,11 @@ class Client implements ClientInterface
*/
protected $authClient;

/**
* @var LeaseClient
*/
protected $leaseClient;

/**
* Client constructor.
*
Expand Down Expand Up @@ -94,14 +106,14 @@ public function getHostname(?string $key = null): string
* @param string $key
* @param mixed $value
* @param bool $prevKv Get the previous key value in the response
* @param int $lease
* @param int $leaseID
* @param bool $ignoreLease Ignore the current lease
* @param bool $ignoreValue Updates the key using its current value
*
* @return string|null Returns previous value if $prevKv is set to true
* @throws InvalidResponseStatusCodeException
*/
public function put(string $key, $value, bool $prevKv = false, int $lease = 0, bool $ignoreLease = false, bool $ignoreValue = false)
public function put(string $key, $value, bool $prevKv = false, int $leaseID = 0, bool $ignoreLease = false, bool $ignoreValue = false)
{
$client = $this->getKvClient();

Expand All @@ -112,7 +124,7 @@ public function put(string $key, $value, bool $prevKv = false, int $lease = 0, b
$request->setPrevKv($prevKv);
$request->setIgnoreLease($ignoreLease);
$request->setIgnoreValue($ignoreValue);
$request->setLease($lease);
$request->setLease($leaseID);

/** @var PutResponse $response */
list($response, $status) = $client->Put($request, $this->getMetaData(), $this->getOptions())->wait();
Expand Down Expand Up @@ -216,6 +228,68 @@ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail
return $this->getIfResponse($returnNewValueOnFail, $response);
}

/**
* Get leaseID which can be used with etcd's put
*
* @param int $ttl time-to-live in seconds
* @return int
* @throws InvalidResponseStatusCodeException
*/
public function getLeaseID(int $ttl)
{
$lease = $this->getLeaseClient();
$leaseRequest = new LeaseGrantRequest();
$leaseRequest->setTTL($ttl);

/** @var LeaseGrantResponse $response */
list($response, $status) = $lease->LeaseGrant($leaseRequest, $this->getMetaData())->wait();
$this->validateStatus($status);

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

/**
* Revoke existing leaseID
*
* @param int $leaseID
* @throws InvalidResponseStatusCodeException
*/
public function revokeLeaseID(int $leaseID)
{
$lease = $this->getLeaseClient();
$leaseRequest = new LeaseRevokeRequest();
$leaseRequest->setID($leaseID);

list(, $status) = $lease->LeaseRevoke($leaseRequest, $this->getMetaData())->wait();
$this->validateStatus($status);
}

/**
* Refresh chosen leaseID
*
* @param int $leaseID
* @return int lease TTL
* @throws InvalidResponseStatusCodeException
* @throws Exception
*/
public function refreshLease(int $leaseID)
{
$lease = $this->getLeaseClient();
$leaseBidi = $lease->LeaseKeepAlive($this->getMetaData());
$leaseKeepAlive = new LeaseKeepAliveRequest();
$leaseKeepAlive->setID($leaseID);
/** @noinspection PhpParamsInspection */
$leaseBidi->write($leaseKeepAlive);
$leaseBidi->writesDone();
/** @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);

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

/**
* Execute $requestOperation if $key value matches $previous otherwise $returnNewValueOnFail
*
Expand Down Expand Up @@ -319,6 +393,22 @@ public function getCompare(string $key, string $value, int $result, int $target)
return $compare;
}

/**
* Get an instance of LeaseClient
*
* @return LeaseClient
*/
protected function getLeaseClient(): LeaseClient
{
if (!$this->leaseClient) {
$this->leaseClient = new LeaseClient($this->hostname, [
'credentials' => ChannelCredentials::createInsecure()
]);
}

return $this->leaseClient;
}

/**
* Get an instance of KVClient
*
Expand Down
32 changes: 30 additions & 2 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Etcdserverpb\Compare;
use Etcdserverpb\RequestOp;
use Etcdserverpb\TxnResponse;
use Exception;

/**
* Interface ClientInterface
Expand All @@ -26,13 +27,13 @@ public function getHostname(?string $key = null): string;
* @param string $key
* @param mixed $value
* @param bool $prevKv Get the previous key value in the response
* @param int $lease
* @param int $leaseID
* @param bool $ignoreLease Ignore the current lease
* @param bool $ignoreValue Updates the key using its current value
* @return string|null Returns previous value if $prevKv is set to true
* @throws InvalidResponseStatusCodeException
*/
public function put(string $key, $value, bool $prevKv = false, int $lease = 0, bool $ignoreLease = false, bool $ignoreValue = false);
public function put(string $key, $value, bool $prevKv = false, int $leaseID = 0, bool $ignoreLease = false, bool $ignoreValue = false);

/**
* Get a key value
Expand Down Expand Up @@ -124,4 +125,31 @@ public function getPutOperation(string $key, string $value, int $leaseId = 0): R
* @return RequestOp
*/
public function getDeleteOperation(string $key): RequestOp;

/**
* Get leaseID which can be used with etcd's put
*
* @param int $ttl time-to-live in seconds
* @return int
* @throws InvalidResponseStatusCodeException
*/
public function getLeaseID(int $ttl);

/**
* Revoke existing leaseID
*
* @param int $leaseID
* @throws InvalidResponseStatusCodeException
*/
public function revokeLeaseID(int $leaseID);

/**
* Refresh chosen leaseID
*
* @param int $leaseID
* @return int lease TTL
* @throws InvalidResponseStatusCodeException
* @throws Exception
*/
public function refreshLease(int $leaseID);
}
60 changes: 48 additions & 12 deletions src/ShardedClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Etcdserverpb\Compare;
use Etcdserverpb\RequestOp;
use Etcdserverpb\TxnResponse;
use Flexihash\Exception;
use Flexihash\Flexihash;

/**
Expand Down Expand Up @@ -52,7 +53,7 @@ public function __construct(array $clients)
*
* @param string $key
* @return ClientInterface
* @throws \Flexihash\Exception
* @throws Exception
*/
protected function getClientFromKey(string $key): ClientInterface
{
Expand All @@ -72,9 +73,20 @@ protected function getClientFromKey(string $key): ClientInterface
return $this->keyCache[$key];
}

/**
* Get random client
*
* @return ClientInterface
*/
protected function getRandomClient(): ClientInterface
{
$rndIndex = array_rand($this->clients);
return $this->clients[$rndIndex];
}

/**
* @inheritDoc
* @throws \Flexihash\Exception
* @throws Exception
*/
public function getHostname(?string $key = null): string
{
Expand All @@ -86,7 +98,7 @@ public function getHostname(?string $key = null): string

/**
* @inheritDoc
* @throws \Flexihash\Exception
* @throws Exception
*/
public function put(string $key, $value, bool $prevKv = false, int $lease = 0, bool $ignoreLease = false, bool $ignoreValue = false)
{
Expand All @@ -95,7 +107,7 @@ public function put(string $key, $value, bool $prevKv = false, int $lease = 0, b

/**
* @inheritDoc
* @throws \Flexihash\Exception
* @throws Exception
*/
public function get(string $key)
{
Expand All @@ -104,7 +116,7 @@ public function get(string $key)

/**
* @inheritDoc
* @throws \Flexihash\Exception
* @throws Exception
*/
public function delete(string $key)
{
Expand All @@ -113,7 +125,7 @@ public function delete(string $key)

/**
* @inheritDoc
* @throws \Flexihash\Exception
* @throws Exception
*/
public function putIf(string $key, string $value, $compareValue, bool $returnNewValueOnFail = false)
{
Expand All @@ -122,7 +134,7 @@ public function putIf(string $key, string $value, $compareValue, bool $returnNew

/**
* @inheritDoc
* @throws \Flexihash\Exception
* @throws Exception
*/
public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail = false)
{
Expand All @@ -131,7 +143,7 @@ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail

/**
* @inheritDoc
* @throws \Flexihash\Exception
* @throws Exception
*/
public function txnRequest(string $key, array $requestOperations, ?array $failureOperations, array $compare): TxnResponse
{
Expand All @@ -140,7 +152,7 @@ public function txnRequest(string $key, array $requestOperations, ?array $failur

/**
* @inheritDoc
* @throws \Flexihash\Exception
* @throws Exception
*/
public function getCompare(string $key, string $value, int $result, int $target): Compare
{
Expand All @@ -149,7 +161,7 @@ public function getCompare(string $key, string $value, int $result, int $target)

/**
* @inheritDoc
* @throws \Flexihash\Exception
* @throws Exception
*/
public function getGetOperation(string $key): RequestOp
{
Expand All @@ -158,7 +170,7 @@ public function getGetOperation(string $key): RequestOp

/**
* @inheritDoc
* @throws \Flexihash\Exception
* @throws Exception
*/
public function getPutOperation(string $key, string $value, int $leaseId = 0): RequestOp
{
Expand All @@ -167,10 +179,34 @@ public function getPutOperation(string $key, string $value, int $leaseId = 0): R

/**
* @inheritDoc
* @throws \Flexihash\Exception
* @throws Exception
*/
public function getDeleteOperation(string $key): RequestOp
{
return $this->getClientFromKey($key)->getDeleteOperation($key);
}

/**
* @inheritDoc
*/
public function getLeaseID(int $ttl)
{
return $this->getRandomClient()->getLeaseID($ttl);
}

/**
* @inheritDoc
*/
public function revokeLeaseID(int $leaseID)
{
return $this->getRandomClient()->revokeLeaseID($leaseID);
}

/**
* @inheritDoc
*/
public function refreshLease(int $leaseID)
{
return $this->getRandomClient()->refreshLease($leaseID);
}
}

0 comments on commit 6decc1d

Please sign in to comment.