Commit 3d0fab20 authored by Julien Muchembled's avatar Julien Muchembled

logger: retry instead of raise if the log is locked by another process

parent 0e3c2712
...@@ -27,7 +27,7 @@ from logging import getLogger, Formatter, Logger, LogRecord, StreamHandler, \ ...@@ -27,7 +27,7 @@ from logging import getLogger, Formatter, Logger, LogRecord, StreamHandler, \
DEBUG, WARNING DEBUG, WARNING
from time import time from time import time
from traceback import format_exception from traceback import format_exception
import bz2, inspect, neo, os, signal, sqlite3, threading import bz2, inspect, neo, os, signal, sqlite3, sys, threading
# Stats for storage node of matrix test (py2.7:SQLite) # Stats for storage node of matrix test (py2.7:SQLite)
RECORD_SIZE = ( 234360832 # extra memory used RECORD_SIZE = ( 234360832 # extra memory used
...@@ -101,15 +101,30 @@ class NEOLogger(Logger): ...@@ -101,15 +101,30 @@ class NEOLogger(Logger):
if self._db is None: if self._db is None:
return return
try: try:
self._db.execute("BEGIN")
for r in self._record_queue: for r in self._record_queue:
self._emit(r) self._emit(r)
finally: finally:
# Always commit, to not lose any record that we could emit. # Always commit, to not lose any record that we could emit.
self._db.commit() self.commit()
self._record_queue.clear() self._record_queue.clear()
self._record_size = 0 self._record_size = 0
def commit(self):
try:
self._db.commit()
except sqlite3.OperationalError, e:
x = e.args[0]
if x == 'database is locked':
sys.stderr.write('%s: retrying to emit log...' % x)
while e.args[0] == x:
try:
self._db.commit()
except sqlite3.OperationalError, e:
continue
sys.stderr.write(' ok\n')
return
raise
def backlog(self, max_size=1<<24): def backlog(self, max_size=1<<24):
with self: with self:
self._max_size = max_size self._max_size = max_size
...@@ -133,8 +148,7 @@ class NEOLogger(Logger): ...@@ -133,8 +148,7 @@ class NEOLogger(Logger):
self._record_size = 0 self._record_size = 0
return return
if filename: if filename:
self._db = sqlite3.connect(filename, isolation_level=None, self._db = sqlite3.connect(filename, check_same_thread=False)
check_same_thread=False)
q = self._db.execute q = self._db.execute
if reset: if reset:
for t in 'log', 'packet': for t in 'log', 'packet':
...@@ -169,6 +183,7 @@ class NEOLogger(Logger): ...@@ -169,6 +183,7 @@ class NEOLogger(Logger):
if p == t: if p == t:
break break
else: else:
with self._db:
q("INSERT INTO protocol VALUES (?,?)", (time(), p)) q("INSERT INTO protocol VALUES (?,?)", (time(), p))
__del__ = setup __del__ = setup
...@@ -193,6 +208,7 @@ class NEOLogger(Logger): ...@@ -193,6 +208,7 @@ class NEOLogger(Logger):
try: try:
if self._max_size is None: if self._max_size is None:
self._emit(record) self._emit(record)
self.commit()
else: else:
self._record_size += RECORD_SIZE + len(record.msg) self._record_size += RECORD_SIZE + len(record.msg)
q = self._record_queue q = self._record_queue
......
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