Ejemplo n.º 1
0
 public function test_should_sharding_table_router_set_and_get_works_fine()
 {
     $table = \Myfox\App\Model\Table::instance('numsplit');
     try {
         Router::set('numsplit', array(array('field' => array('thedate' => '20110610'), 'count' => 1201)));
         $this->assertTrue(false);
     } catch (\Exception $e) {
         $this->assertTrue($e instanceof \Myfox\Lib\Exception);
         $this->assertContains('Column "cid" required for table "numsplit"', $e->getMessage());
     }
     $this->assertEquals(array('cid:1,thedate:20110610' => array(array('rows' => 1000, 'hosts' => '3,1', 'table' => 'numsplit_0.t_' . $table->get('autokid') . '_0'), array('rows' => 201, 'hosts' => '1,2', 'table' => 'numsplit_0.t_' . $table->get('autokid') . '_1')), 'cid:2,thedate:20110610' => array(array('rows' => 998, 'hosts' => '1,2', 'table' => 'numsplit_0.t_' . $table->get('autokid') . '_1'))), Router::set('numsplit', array(array('field' => array('thedate' => '2011-06-10', 'cid' => 1), 'count' => 1201), array('field' => array('thedate' => '2011-06-10', 'cid' => 2), 'count' => 998))));
     $this->assertEquals(array(), Router::get('numsplit', array('thedate' => '2011-6-10', 'cid' => 1, 'blablala' => 2)));
     Router::effect('numsplit', array('thedate' => 20110610, 'cid' => 1), 'numsplit_0.t_' . $table->get('autokid') . '_1', '1,2') && Router::flush();
     $routes = Router::get('numsplit', array('thedate' => '2011-6-10', 'cid' => 1, 'blablala' => 2));
     $result = array();
     foreach ($routes as $item) {
         unset($item['tabid'], $item['seqid']);
         $item['mtime'] = $item['mtime'] > 0 ? true : false;
         $result[] = $item;
     }
     $this->assertEquals(array(array('tbidx' => 'test_route_info_c', 'mtime' => true, 'hosts' => '1,2', 'table' => sprintf('numsplit_0.t_%d_1', $table->get('autokid')))), $result);
 }
Ejemplo n.º 2
0
 /**
  * 异步结果返回
  *
  * @access public
  * @return Integer
  */
 public function wait()
 {
     $result = array();
     foreach ((array) $this->pools as $host => $pool) {
         $failed = null;
         $mysql = Server::instance($pool['server'])->getlink();
         $this->setError($mysql->lastError());
         if (false !== $pool['handle'] && false !== $mysql->wait($pool['handle'])) {
             $failed = false;
             foreach ((array) $pool['commit'] as $query) {
                 if (false === $mysql->query($query)) {
                     $failed = true;
                     $this->setError(sprintf('[%s] %s', $pool['server'], $mysql->lastError()));
                     break;
                 }
             }
         }
         if (false === $failed) {
             $result[(int) $host] = true;
         }
     }
     if (!empty($result)) {
         $succes = implode(',', array_keys($result));
         if (false === Router::effect($this->option['table'], $this->route, $this->option['bucket'], $succes)) {
             $this->setError(sprintf('Router effect failed [%s].', $succes));
             $result = array();
         }
     }
     foreach ($this->pools as $host => $pool) {
         if (!empty($result[$host])) {
             unset($this->pools[$host]);
             continue;
         }
         foreach ((array) $pool['rollback'] as $query) {
             $mysql->query($query);
         }
     }
     $this->result = implode(',', array_keys($result));
     return empty($this->pools) ? self::SUCC : self::FAIL;
 }