Skip to content

Commit 7fe00f3

Browse files
committed
proto
1 parent cf6a9c2 commit 7fe00f3

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

example/cluster_01_start.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,28 @@
1919

2020
// ----------------------------------------------------------------------
2121
$cluster_name='ads';
22-
$cl1 = new ClickHouseDB\Cluster($config);
23-
$cl2= new ClickHouseDB\Cluster($config);
24-
$cl2->setScanTimeOut(0.45); // 200 ms
22+
$cl = new ClickHouseDB\Cluster($config);
23+
$cl->setScanTimeOut(0.45); // 200 ms
2524

26-
27-
for ($z=0;$z<100;$z++)
25+
if (!$cl->isReplicasIsOk())
2826
{
29-
$cl1->rescan();
30-
$cl2->rescan();
31-
print_r( $cl1->getBadIps() );
32-
print_r( $cl2->getBadIps() );
33-
27+
throw new Exception('Replica state is bad');
3428
}
3529

30+
$sql=[
31+
'up'=>['CREATE DATABASE IF NOT EXISTS ttests'],
32+
'down'=>['DROP DATABASE IF EXISTS ttests ']
33+
];
34+
echo "getClusterList:\n";
35+
print_r($cl->getClusterList());
36+
37+
echo "getClusterHosts:model:\n";
38+
print_r($cl->getClusterHosts('model'));
3639

3740

3841

39-
echo "END\n";
40-
//print_r( $db->getAllHostsIps() );
4142

42-
//print_r( );
4343

44-
//print_r($db->getListHostInCluser('ads')); // like $db->getClustersTable()
44+
echo "\n----\nEND\n";
4545

4646
// ----------------------------------------------------------------------

src/Cluster.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class Cluster
2727
private $badIps=[];
2828

2929

30+
/**
31+
* @var array
32+
*/
33+
private $resultScan=[];
3034
/**
3135
* @var bool
3236
*/
@@ -105,6 +109,11 @@ public function connect()
105109
}
106110
return $this;
107111
}
112+
113+
/**
114+
* @param $replicas
115+
* @return bool
116+
*/
108117
private function isReplicasWork($replicas)
109118
{
110119
$ok=true;
@@ -127,6 +136,10 @@ private function isReplicasWork($replicas)
127136
}
128137
return $ok;
129138
}
139+
140+
/**
141+
* @return $this
142+
*/
130143
public function rescan()
131144
{
132145
/*
@@ -139,6 +152,7 @@ public function rescan()
139152
$statementsReplicas=[];
140153
$statementsClusters=[];
141154
$result=[];
155+
142156
$badIps=[];
143157
$replicasIsOk=true;
144158

@@ -168,7 +182,13 @@ public function rescan()
168182
// ---------------------------------------------------------------------------------------------------
169183
try
170184
{
171-
$result['clusters'][$ip] =$statementsClusters[$ip]->rowsAsTree('cluster.host_address');
185+
$c=$statementsClusters[$ip]->rows();
186+
$result['clusters'][$ip] = $c;
187+
foreach ($c as $row)
188+
{
189+
$result['cluster.list'][$row['cluster']][$row['host_address']][$row['shard_num']][$row['replica_num']]=['shard_weight'=>$row['shard_weight'],'is_local'=>$row['is_local']];
190+
}
191+
172192
}
173193
catch (\Exception $E)
174194
{
@@ -207,9 +227,18 @@ public function rescan()
207227
$this->replicasIsOk=false;
208228
}
209229

230+
$this->resultScan=$result;
210231
// @todo Мы подключаемся ко всем в списке DNS, нужно пререить что запросы вернули все хосты к которым мы подключались
232+
return $this;
211233
}
212234

235+
/**
236+
* @return boolean
237+
*/
238+
public function isReplicasIsOk()
239+
{
240+
return $this->connect()->replicasIsOk;
241+
}
213242
/**
214243
* @return Client
215244
*/
@@ -225,7 +254,17 @@ public function activeClient()
225254
{
226255
return $this->client($this->ips[0]);
227256
}
228-
257+
public function getClusterHosts($cluster)
258+
{
259+
$this->connect();
260+
if (empty($this->resultScan['cluster.list'][$cluster])) throw new QueryException('Cluster not find:'.$cluster);
261+
return array_keys($this->resultScan['cluster.list'][$cluster]);
262+
}
263+
public function getClusterList()
264+
{
265+
$this->connect();
266+
return array_keys($this->resultScan['cluster.list']);
267+
}
229268
/**
230269
* @param $sql
231270
* @param array $bindings

0 commit comments

Comments
 (0)