def get_metric(self, metric_name): """See bg_accessor.Accessor.""" super(_CassandraAccessor, self).get_metric(metric_name) metric_name = bg_accessor.encode_metric_name(metric_name) result = list(self.__session.execute( self.__select_metric_statement, (metric_name, ))) if not result: return None config = result[0][0] return bg_accessor.Metric( metric_name, bg_accessor.MetricMetadata.from_string_dict(config))
def _cache_set(self, metric_name, metric): """If metric is valid, add it to the cache. The metric is stored in the cache as follows: - its id, which is a UUID. - vertical bar (pipe) separator. - its metadata JSON representation. """ encoded_metric_name = bg_accessor.encode_metric_name(metric_name) key = encoded_metric_name value = self.__value_from_metric(metric) with self.__env.begin(self.__metric_to_metadata_db, write=True) as txn: txn.put(key, value, dupdata=False, overwrite=True)
def make_metric(name, metadata=None, **kwargs): """Create a bg_accessor.Metric with specified metadata.""" encoded_name = bg_accessor.encode_metric_name(name) id = uuid.uuid5(_UUID_NAMESPACE, encoded_name) retention = kwargs.get("retention") if isinstance(retention, basestring): kwargs["retention"] = bg_accessor.Retention.from_string(retention) if metadata: assert isinstance(metadata, bg_accessor.MetricMetadata) assert not kwargs else: metadata = bg_accessor.MetricMetadata(**kwargs) return bg_accessor.Metric(name, id, metadata)
def make_metric(name, metadata=None, accessor=None, **kwargs): """Create a bg_accessor.Metric with specified metadata.""" encoded_name = bg_accessor.encode_metric_name(name) retention = kwargs.get("retention") if isinstance(retention, basestring): kwargs["retention"] = bg_accessor.Retention.from_string(retention) if metadata: assert isinstance(metadata, bg_accessor.MetricMetadata) assert not kwargs else: metadata = bg_accessor.MetricMetadata(**kwargs) if not accessor: uid = uuid.uuid5(_UUID_NAMESPACE, encoded_name) return bg_accessor.Metric(name, uid, metadata) else: return accessor.make_metric(name, metadata)
def _cache_get(self, metric_name): """Return a Metric from a the cache, None if no such metric.""" encoded_metric_name = bg_accessor.encode_metric_name(metric_name) with self.__env.begin(self.__metric_to_metadata_db, write=False) as txn: payload = txn.get(encoded_metric_name) if payload == self._EMPTY: return None, True if not payload: # cache miss return None, False # found something in the cache split = self.__split_payload(payload) if split is None: # invalid string => evict from cache with self.__env.begin(self.__metric_to_metadata_db, write=True) as txn: txn.delete(key=encoded_metric_name) return None, False # valid value => get id and metadata string # TODO: optimization: id is a UUID (known length) id_str, metadata_str, timestamp = split try: id = uuid.UUID(id_str) except Exception as e: logging.debug(str(e)) with self.__env.begin(self.__metric_to_metadata_db, write=True) as txn: txn.delete(key=encoded_metric_name) return None, False # if the timestamp expired evict it in order to force # its recreation for the next time if self.__expired_timestamp(timestamp): with self.__env.begin(self.__metric_to_metadata_db, write=True) as txn: txn.delete(key=encoded_metric_name) metadata = self.metadata_from_str(metadata_str) return bg_accessor.Metric(metric_name, id, metadata), True
def get_metric(self, metric_name): """Return a MetricMetadata for this metric_name, None if no such metric.""" metric_name = bg_accessor.encode_metric_name(metric_name) with self.__env.begin(self.__metric_to_metadata_db, write=False) as txn: metadata_str = txn.get(metric_name) if metadata_str: # on disk cache hit self.hit_count += 1 with self.__json_cache_lock: metadata = self.__json_cache.get(metadata_str) if not metadata: metadata = bg_accessor.MetricMetadata.from_json(metric_name, metadata_str) self.__json_cache[metadata_str] = metadata return metadata else: # on disk cache miss self.miss_count += 1 with self.__accessor_lock: metadata = self.__accessor.get_metric(metric_name) self._cache(metadata) return metadata
def _cache_has(self, metric_name): """Check if metric is cached.""" encoded_metric_name = bg_accessor.encode_metric_name(metric_name) with self.__env.begin(self.__metric_to_metadata_db, write=False) as txn: payload = txn.get(encoded_metric_name) return payload is not None and payload != self._EMPTY