/** * @return BucketRange */ protected function _claimNextFreeRange() { $range = false; $db = BucketRange::conn(); EventManager::trigger(Events::CLAIM_RANGE_START); // Check for an already-flagged range $coll = new RecordCollection(new BucketRange()); $coll->loadWhere(['processing' => 1, 'hostname' => $this->_hostname, 'instanceName' => $this->_instanceName])->limit(1); if ($coll->count() > 0) { $range = $coll->first(); } else { $res = $db->query(ParseQuery::parse($db, "UPDATE %T SET processing=1, hostname=%s, instanceName=%s " . "WHERE processing=0 AND processed=0 ORDER BY randomKey LIMIT 1", BucketRange::tableName(), $this->_hostname, $this->_instanceName)); if ($res) { $range = BucketRange::loadWhere(['processing' => 1, 'hostname' => $this->_hostname, 'instanceName' => $this->_instanceName]); } } EventManager::trigger(Events::CLAIM_RANGE_END); return $range; }
/** * @return TokenRange */ public function claimNextFreeRange() { EventManager::trigger(Events::CLAIM_RANGE_START); $range = false; $db = TokenRange::conn(); // Check for an already-flagged range $coll = new RecordCollection(new TokenRange()); $coll->loadWhere(['processing' => 1, 'hostname' => $this->_hostname])->limit(1); if ($coll->count() > 0) { $range = $coll->first(); } else { $startKey = $this->_lastStartKey; $res = false; while (true) { $endKey = $startKey + 100; $res = $db->query(ParseQuery::parse($db, "UPDATE %T SET processing=1, hostname=%s " . "WHERE processing=0 AND processed=0 AND " . "randomKey BETWEEN %d AND %d " . "ORDER BY randomKey LIMIT 1", (new TokenRange())->getTableName(), $this->_hostname, $startKey, $endKey)); if ($res && $db->affectedRows() > 0 || $startKey > 10000) { $this->_lastStartKey = $startKey; break; } $startKey += 100; } if ($res) { $range = TokenRange::loadWhere(['processing' => 1, 'hostname' => $this->_hostname]); } } EventManager::trigger(Events::CLAIM_RANGE_END); return $range; }