Skip to content

Commit 0708267

Browse files
committed
init cluster
1 parent ef682f1 commit 0708267

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
include_once __DIR__ . '/../include.php';
4+
include_once __DIR__ . '/lib_example.php';
5+
// load production config
6+
$config = include_once __DIR__ . '/../../_clickhouse_config_product.php';
7+
8+
9+
$cl = new ClickHouseDB\Cluster($config);
10+
11+
$cl->setScanTimeOut(2.5); // 2500 ms
12+
if (!$cl->isReplicasIsOk())
13+
{
14+
throw new Exception('Replica state is bad , error='.$cl->getError());
15+
}
16+
//
17+
$cluster_name='sharovara';
18+
//
19+
echo "> $cluster_name , count shard = ".$cl->getClusterCountShard($cluster_name)." ; count replica = ".$cl->getClusterCountReplica($cluster_name)."\n";
20+
21+
22+
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
23+
$mclq=new ClickHouseDB\Cluster\Migration($cluster_name);
24+
$mclq->setAutoSplitQuerty(';;');
25+
26+
27+
28+
29+
$mclq->addSqlUpdate("
30+
31+
DROP DATABASE IF EXISTS shara
32+
33+
;;
34+
35+
CREATE DATABASE IF NOT EXISTS shara
36+
37+
;;
38+
39+
DROP TABLE IF EXISTS shara.adpreview_body_views_sharded
40+
41+
;;
42+
43+
DROP TABLE IF EXISTS shara.adpreview_body_views
44+
;;
45+
46+
CREATE TABLE IF NOT EXISTS shara.adpreview_body_views_sharded (
47+
event_date Date DEFAULT toDate(event_time),
48+
event_time DateTime DEFAULT now(),
49+
body_id Int32,
50+
site_id Int32,
51+
views Int32
52+
) ENGINE = ReplicatedSummingMergeTree('/clickhouse/tables/{sharovara_replica}/shara/adpreview_body_views_sharded', '{replica}', event_date, (event_date, event_time, body_id, site_id), 8192)
53+
;;
54+
CREATE TABLE IF NOT EXISTS
55+
shara.adpreview_body_views AS shara.adpreview_body_views_sharded
56+
ENGINE = Distributed(sharovara, shara, adpreview_body_views_sharded , rand())
57+
");
58+
59+
// откат
60+
$mclq->addSqlDowngrade('
61+
62+
DROP TABLE IF EXISTS shara.adpreview_body_views
63+
64+
;;
65+
66+
DROP TABLE IF EXISTS shara.adpreview_body_views_sharded
67+
;;
68+
DROP DATABASE IF EXISTS shara
69+
70+
');
71+
72+
echo "Start:sendMigration\n";
73+
if (!$cl->sendMigration($mclq,true))
74+
{
75+
throw new Exception('sendMigration is bad , error='.$cl->getError());
76+
}
77+
78+
79+
echo "\n----\nEND\n";
80+
// ----------------------------------------------------------------------
81+

src/Cluster.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -416,22 +416,18 @@ public function sendMigration(Cluster\Migration $migration,$showDebug=false)
416416
foreach ($undo_ip as $node=>$tmp)
417417
{
418418
foreach ($sql_down as $s_u) {
419-
if ($this->client($node)->write($s_u)->isError()) {
419+
420+
if ($showDebug)
421+
{
422+
echo "!UNDO!\nclient($node)->write(".substr($s_u,0,45).")....\n";
423+
}
424+
425+
426+
if ($this->client($node)->write($s_u)->isError()) {
420427
}
421428
}
422429
}
423430
return false;
424431

425432
}
426-
/**
427-
* @param $sql
428-
* @param array $bindings
429-
* @param bool $exception
430-
* @return Statement
431-
*/
432-
public function writeCluster($cluster,$sql, $bindings = [], $exception = true)
433-
{
434-
return $this->transport()->write($sql, $bindings, $exception);
435-
}
436-
437433
}

0 commit comments

Comments
 (0)