Skip to content

Commit 46ccafe

Browse files
authored
Adding the ability to use tags servers (#15)
* adding ability to use proxy servers * added tests for changes * code style fixes * trying to fix building * another try * fix dependencies and tests * change readme * fix readme * grammar fix * remove proxy servers and add tags for servers * code style fixes * code style fix * add missing tests * fix test * add missing tests * fix method name in readme * some refactoring * refactor variables name with random server hostname
1 parent c762dbe commit 46ccafe

22 files changed

Lines changed: 396 additions & 33 deletions

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ before_script:
2323
- make && make install
2424
- cd ../../
2525

26-
script: phpunit --coverage-clover ./tests/logs/clover.xml
26+
script: vendor/bin/phpunit --coverage-clover ./tests/logs/clover.xml
2727

2828
after_script:
2929
- php vendor/bin/php-coveralls -v

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,29 @@ By default client will use random server in given list of servers or in specifie
7272
$client->using('server-2')->select('select * from table');
7373
```
7474

75+
## Server tags
76+
77+
```php
78+
$firstServerOptionsWithTag = (new \Tinderbox\Clickhouse\Common\ServerOptions())->setTag('tag');
79+
$secondServerOptionsWithAnotherTag = (new \Tinderbox\Clickhouse\Common\ServerOptions())->setTag('another-tag');
80+
81+
$server = new Tinderbox\Clickhouse\Server('127.0.0.1', '8123', 'default', 'user', 'pass', $firstServerOptionsWithTag);
82+
83+
$cluster = new Tinderbox\Clickhouse\Cluster('cluster', [
84+
new Tinderbox\Clickhouse\Server('127.0.0.2', '8123', 'default', 'user', 'pass', $secondServerOptionsWithAnotherTag)
85+
]);
86+
87+
$serverProvider = (new Tinderbox\Clickhouse\ServerProvider())->addServer($server)->addCluster($cluster);
88+
89+
$client = (new Tinderbox\Clickhouse\Client($serverProvider));
90+
```
91+
92+
To use server with tag, you should call ```usingServerWithTag``` function before execute any query.
93+
94+
```php
95+
$client->usingServerWithTag('tag');
96+
```
97+
7598
## Select queries
7699

77100
Any SELECT query will return instance of `Result`. This class implements interfaces `\ArrayAccess`, `\Countable` и `\Iterator`,

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@
1919
},
2020
"require": {
2121
"php": "~7.1",
22-
"guzzlehttp/guzzle": "~6.0"
22+
"guzzlehttp/guzzle": "~6.0",
23+
"satooshi/php-coveralls": "^2.2"
2324
},
2425
"bin": [
2526
"bin/ccat_linux",
2627
"bin/ccat_darwin"
2728
],
2829
"require-dev": {
2930
"mockery/mockery": "^0.9.9",
30-
"phpunit/php-code-coverage": "^5.2",
31-
"phpunit/phpunit": "~4.0||~5.0||~6.0"
31+
"phpunit/php-code-coverage": "^6.0",
32+
"phpunit/phpunit": "~7.0"
3233
},
3334
"scripts": {
3435
"test": "phpunit --coverage-text --colors=never"

src/Client.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,26 @@ public function usingRandomServer()
147147
return $this;
148148
}
149149

150+
/**
151+
* Client will use server with tag as server for queries.
152+
*
153+
* @var string
154+
*
155+
* @return $this
156+
*/
157+
public function usingServerWithTag(string $tag)
158+
{
159+
$this->serverHostname = function () use ($tag) {
160+
if ($this->isOnCluster()) {
161+
return $this->serverProvider->getRandomServerFromClusterByTag($this->getClusterName(), $tag);
162+
} else {
163+
return $this->serverProvider->getRandomServerWithTag($tag);
164+
}
165+
};
166+
167+
return $this;
168+
}
169+
150170
/**
151171
* Returns true if cluster selected.
152172
*

src/Cluster.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ class Cluster
2323
*/
2424
protected $servers = [];
2525

26+
/**
27+
* Servers in cluster by tags.
28+
*
29+
* @var \Tinderbox\Clickhouse\Server[][]
30+
*/
31+
protected $serversByTags = [];
32+
2633
/**
2734
* Cluster constructor.
2835
*
@@ -86,6 +93,12 @@ public function addServer(string $hostname, Server $server)
8693
}
8794

8895
$this->servers[$hostname] = $server;
96+
97+
$serverTags = $server->getOptions()->getTags();
98+
99+
foreach ($serverTags as $serverTag) {
100+
$this->serversByTags[$serverTag][$hostname] = true;
101+
}
89102
}
90103

91104
/**
@@ -98,6 +111,24 @@ public function getServers(): array
98111
return $this->servers;
99112
}
100113

114+
/**
115+
* Returns servers in cluster by tag.
116+
*
117+
* @param string $tag
118+
*
119+
* @throws ClusterException
120+
*
121+
* @return \Tinderbox\Clickhouse\Server[]
122+
*/
123+
public function getServersByTag(string $tag): array
124+
{
125+
if (!isset($this->serversByTags[$tag])) {
126+
throw ClusterException::tagNotFound($tag);
127+
}
128+
129+
return $this->serversByTags[$tag];
130+
}
131+
101132
/**
102133
* Returns server by specified hostname.
103134
*
@@ -121,7 +152,7 @@ public function getServerByHostname(string $hostname): Server
121152
*
122153
* @return string
123154
*/
124-
public function getName() : string
155+
public function getName(): string
125156
{
126157
return $this->name;
127158
}

src/Common/File.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class File extends AbstractFile implements FileInterface
1010
{
11-
public function open(bool $gzip = true) : StreamInterface
11+
public function open(bool $gzip = true): StreamInterface
1212
{
1313
$handle = fopen($this->getSource(), 'r');
1414

src/Common/FileFromString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class FileFromString extends AbstractFile implements FileInterface
1010
{
11-
public function open(bool $gzip = true) : StreamInterface
11+
public function open(bool $gzip = true): StreamInterface
1212
{
1313
$handle = fopen('php://memory', 'r+');
1414
fwrite($handle, $this->source);

src/Common/MergedFiles.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ protected function getCcatPath(): string
1414
return __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'bin'.DIRECTORY_SEPARATOR.'ccat_'.strtolower(PHP_OS);
1515
}
1616

17-
public function open(bool $gzip = true) : StreamInterface
17+
public function open(bool $gzip = true): StreamInterface
1818
{
1919
$descriptorspec = [
2020
0 => ['pipe', 'r'],

src/Common/ServerOptions.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class ServerOptions
1414
*/
1515
protected $protocol = 'http';
1616

17+
/**
18+
* Tags.
19+
*
20+
* @var array
21+
*/
22+
protected $tags = [];
23+
1724
/**
1825
* Sets protocol.
1926
*
@@ -37,4 +44,46 @@ public function getProtocol(): string
3744
{
3845
return $this->protocol;
3946
}
47+
48+
/**
49+
* Set tags.
50+
*
51+
* @param array $tags
52+
*
53+
* @return ServerOptions
54+
*/
55+
public function setTags(array $tags): self
56+
{
57+
$this->tags = [];
58+
59+
foreach ($tags as $tag) {
60+
$this->addTag($tag);
61+
}
62+
63+
return $this;
64+
}
65+
66+
/**
67+
* Adds tag.
68+
*
69+
* @param string $tag
70+
*
71+
* @return ServerOptions
72+
*/
73+
public function addTag(string $tag): self
74+
{
75+
$this->tags[$tag] = true;
76+
77+
return $this;
78+
}
79+
80+
/**
81+
* Returns tags.
82+
*
83+
* @return array
84+
*/
85+
public function getTags(): array
86+
{
87+
return array_keys($this->tags);
88+
}
4089
}

src/Common/TempTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function getFormat(): string
9494
return $this->format;
9595
}
9696

97-
public function open(bool $gzip = true) : StreamInterface
97+
public function open(bool $gzip = true): StreamInterface
9898
{
9999
return $this->source->open($gzip);
100100
}

0 commit comments

Comments
 (0)