Commit 8caeb7f4 authored by Hanno Schlichting's avatar Hanno Schlichting

Encapsulate priority map

parent fdb6a1a7
...@@ -25,8 +25,22 @@ REFRESH_RATE = 100 ...@@ -25,8 +25,22 @@ REFRESH_RATE = 100
REPORTS_LOCK = allocate_lock() REPORTS_LOCK = allocate_lock()
REPORTS = {} REPORTS = {}
PRIORITYMAP_LOCK = allocate_lock() class ThreadDict(object):
PRIORITYMAP = {}
@classmethod
def get_entry(cls, key):
return cls.value.get(key, None)
@classmethod
def set_entry(cls, key, value):
with cls.lock:
cls.value[key] = value
class PriorityMap(ThreadDict):
lock = allocate_lock()
value = {}
class ValueIndexes(object): class ValueIndexes(object):
...@@ -128,6 +142,7 @@ class CatalogPlan(object): ...@@ -128,6 +142,7 @@ class CatalogPlan(object):
self.catalog = catalog self.catalog = catalog
self.query = query self.query = query
self.key = make_key(catalog, query) self.key = make_key(catalog, query)
self.benchmark = {}
self.threshold = threshold self.threshold = threshold
self.cid = self.get_id() self.cid = self.get_id()
self.init_timer() self.init_timer()
...@@ -148,16 +163,8 @@ class CatalogPlan(object): ...@@ -148,16 +163,8 @@ class CatalogPlan(object):
self.stop_time = None self.stop_time = None
self.duration = None self.duration = None
def benchmark(self):
# holds the benchmark of each index
bm = PRIORITYMAP.get(self.key, None)
if bm is None:
with PRIORITYMAP_LOCK:
PRIORITYMAP[self.key] = {}
return bm
def plan(self): def plan(self):
benchmark = self.benchmark() benchmark = PriorityMap.get_entry(self.key)
if not benchmark: if not benchmark:
return None return None
...@@ -187,10 +194,9 @@ class CatalogPlan(object): ...@@ -187,10 +194,9 @@ class CatalogPlan(object):
name=name, duration=current - start_time, num=length)) name=name, duration=current - start_time, num=length))
# remember index's hits, search time and calls # remember index's hits, search time and calls
benchmark = self.benchmark() benchmark = self.benchmark
if name not in benchmark: if name not in benchmark:
with PRIORITYMAP_LOCK: benchmark[name] = Benchmark(num=length, duration=dt, hits=1)
benchmark[name] = Benchmark(num=length, duration=dt, hits=1)
else: else:
num, duration, hits = benchmark[name] num, duration, hits = benchmark[name]
num = int(((num * hits) + length) / float(hits + 1)) num = int(((num * hits) + length) / float(hits + 1))
...@@ -199,19 +205,12 @@ class CatalogPlan(object): ...@@ -199,19 +205,12 @@ class CatalogPlan(object):
if hits % REFRESH_RATE == 0: if hits % REFRESH_RATE == 0:
hits = 0 hits = 0
hits += 1 hits += 1
with PRIORITYMAP_LOCK: benchmark[name] = Benchmark(num, duration, hits)
benchmark[name] = Benchmark(num, duration, hits)
def stop(self): def stop(self):
self.end_time = time.time() self.end_time = time.time()
self.duration = self.end_time - self.start_time self.duration = self.end_time - self.start_time
PriorityMap.set_entry(self.key, self.benchmark)
key = self.key
benchmark = self.benchmark()
with PRIORITYMAP_LOCK:
PRIORITYMAP[key] = benchmark
self.log() self.log()
def log(self): def log(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment