Ejemplo n.º 1
0
 public function setUp()
 {
     $conf = Config::get('sharding');
     $this->sharding = new MysqlShard($conf);
     // проверяет что установлен конфиг разработчика
     $this->sharding->checkDeveloperConfig();
     // сохранить состояние
     $strategy = new LinearStrategy(1, 'lines', $this->sharding->getConfig());
     $this->shard_id = (int) $strategy->getTabIdFromCache();
 }
Ejemplo n.º 2
0
 /**
  * Возвращает инстанс редиса
  *
  * @param int $db Номер базы данных
  * @return \Zotto\Db\Adapters\Redis
  */
 public static function redis()
 {
     if (!isset(self::$_redisConnections)) {
         $conf = Config::get('redis');
         $host = $conf['host'];
         $port = $conf['port'];
         $redis = new \Redis();
         $redis->connect($host, $port);
         self::$_redisConnections = $redis;
     }
     return self::$_redisConnections;
 }
Ejemplo n.º 3
0
 public function run()
 {
     $conf = Config::get('sharding');
     $shard = new Adapters\MysqlShard($conf);
     $strategy = new Strategy\LinearStrategy(null, self::name, $shard->getConfig());
     $name = self::name;
     $pos = strlen(self::name) + 2;
     $sql = "SELECT  substr( TABLE_NAME, {$pos}) AS num, TABLE_ROWS as `count`  \n                FROM information_schema.TABLES \n                WHERE TABLE_SCHEMA \n                    LIKE '{$name}_%' \n                    AND  TABLE_NAME REGEXP '^({$name}_)[0-9]+\$'";
     $tables = [];
     for ($cnn_id = 0; $cnn_id < $shard->getInstanceCount(); $cnn_id++) {
         try {
             $mysql = $shard->getConnectionById($cnn_id);
         } catch (Exception $e) {
             echo $e->getMessage();
         }
         $res = $mysql->query($sql);
         while (($row = $res->fetch_assoc()) != null) {
             $tables[$row['num']] = $row['count'];
         }
     }
     $sum = count($tables);
     $fillTables = 0;
     $startTabId = 0;
     foreach ($tables as $tab_id => $count) {
         if ($count > 0) {
             $fillTables++;
         }
         if ($tab_id > $startTabId) {
             $startTabId = (int) $tab_id;
         }
     }
     if ($sum - $fillTables > self::emptyTables) {
         $out = sprintf('fulling tables %d from %d %s', $fillTables, $sum, date('Y-m-d'));
         echo $out, PHP_EOL;
         exit;
     }
     $out = sprintf('generate: full tables %d from %d', $fillTables, $sum);
     $startTabId++;
     echo 'generate tables:', PHP_EOL;
     //        $shard->setNoExec();
     $table_names = '';
     for ($i = $startTabId; $i < $startTabId + self::count; $i++) {
         $table_names .= self::name . '_' . $i . PHP_EOL;
         $strategy->setId(Strategy\LinearStrategy::MAX_RECORD_COUNT * $i + 1);
         $table = Generator\LinesGenerator::getCreateTableSQL($i);
         $shard->query($table, $strategy);
         //                echo $shard->getQuery(), PHP_EOL,PHP_EOL;
     }
     echo $table_names, PHP_EOL;
 }
Ejemplo n.º 4
0
 /**
  * Конструктор класса
  * MysqlShard constructor.
  *
  * @param array $conf - конфигурационный файл
  * @throws Exception
  */
 public function __construct($conf = null)
 {
     if (!$conf) {
         $conf = Config::get('sharding');
     }
     if (!$conf || !is_array($conf)) {
         throw new Exception("Error config parameter");
     }
     $this->_conf = $conf;
     foreach ($this->_conf['instance'] as $key => $inst) {
         $this->_pool[$key] = false;
         foreach ($inst['db'] as $dbName => $db) {
             if (!isset($this->_shard_count[$dbName])) {
                 $this->_shard_count[$dbName] = 0;
             }
             foreach ($db as $shard) {
                 $this->_shard2cnn[$dbName . $shard] = $key;
                 $this->_shard_count[$dbName]++;
             }
         }
     }
 }
Ejemplo n.º 5
0
 public function setUp()
 {
     $conf = Config::get('sharding');
     $this->sharding = new MysqlShard($conf);
     $this->sharding->checkDeveloperConfig();
 }