public function execute() { $registeredSegments = $this->cache()->getItemsOnList(SegmentCache::getCacheKeyForRegisterSegments()); $log = $this->logger(); if (is_array($registeredSegments) && !empty($registeredSegments)) { foreach ($registeredSegments as $segmentName) { $log->info(">>> Fetching data from segment: {$segmentName}"); $timeStart = Latency::startMeasuringLatency(); while (true) { $timeStartPart = Latency::startMeasuringLatency(); if (!$this->getSplitClient()->updateSegmentChanges($segmentName)) { $timeItTook = Latency::calculateLatency($timeStartPart); $log->debug("Fetching segment last part ({$segmentName}) took {$timeItTook} microseconds"); $greedyTime = Latency::calculateLatency($timeStart); $log->info("Finished fetching whole segment {$segmentName}, took {$greedyTime} microseconds"); break; } $timeItTook = Latency::calculateLatency($timeStartPart); $log->debug("Fetching segment part ({$segmentName}) took {$timeItTook} microseconds"); //Sleep 1/2 second usleep(500000); } } // Setting Segments ready mark $dateTimeUTC = new \DateTime("now", new \DateTimeZone("UTC")); $bur = new BlockUntilReadyCache(); $bur->setReadySegments($dateTimeUTC->getTimestamp()); } }
public function sendMetrics() { $metricsKeys = Di::getCache()->getKeys(MetricsCache::getCacheKeySearchLatencyPattern()); $metricsCache = new MetricsCache(); $cachedMetrics = array(); foreach ($metricsKeys as $key) { $metricName = MetricsCache::getMetricNameFromKey($key); $metricBucket = MetricsCache::getBucketFromKey($key); if (!isset($cachedMetrics[$metricName]) || !is_array($cachedMetrics[$metricName])) { //initialization latency butckets $maxBucket = Latency::getBucketForLatencyMicros(Latency::MAX_LATENCY); $cachedMetrics[$metricName] = array_fill(0, $maxBucket + 1, 0); } $cachedMetrics[$metricName][$metricBucket] = $metricsCache->getLatencyAndReset($key); } $dataset = array(); foreach ($cachedMetrics as $name => $latencies) { $dataset[] = array('name' => $name, 'latencies' => $latencies); } //Sending Metrics dataset. $response = $this->post($this->servicePath, $dataset); if ($response->isSuccessful()) { Di::getLogger()->info(count($dataset) . " Metrics sent successfuly"); } else { Di::getLogger()->error("Metrics have not been sent successfully"); Di::getLogger()->error("HTTP Code: " . $response->getStatusCode()); Di::getLogger()->error("HTTP Body: " . $response->getBody()); } }
/** * @param $timeout * @return bool */ private function blockUntilReady($timeout) { $bur = new BlockUntilReadyCache(); $startTime = Latency::startMeasuringLatency(); do { $lastreadyCheckpoint = $bur->getReadyCheckpoint(); if ($lastreadyCheckpoint > 0) { return true; } // Checkpoint in milliseconds $checkPoint = Latency::calculateLatency($startTime) / 1000; // waiting 10 milliseconds usleep(10000); } while ($checkPoint < $timeout); return false; }
/** * Returns the bucket that this latency falls into. * The latencies will not be updated. * @param latency * @return int the bucket content for the latency. */ public static function getBucketForLatencyMicros($latency) { return Latency::getBucketForLatencyMicros($latency); }