예제 #1
0
파일: session.py 프로젝트: msimonin/rome
 def __init__(self):
     self.session_id = uuid.uuid1()
     self.session_objects_add = []
     self.session_objects_delete = []
     self.session_timeout = current_milli_time() + Session.max_duration
     self.dlm = ClusterLock()
     self.acquired_locks = []
     self.already_saved = []
예제 #2
0
파일: test_lock.py 프로젝트: Gayout/rome
 def work1():
     dlm = ClusterLock()
     while not dlm.lock("toto", 2000):
         print("1> waiting for acquiring lock")
         time.sleep(0.1)
     print("1> acquired lock")
     time.sleep(3)
     dlm.unlock("toto")
예제 #3
0
 def work1():
     dlm = ClusterLock()
     while not dlm.lock("toto", 2000):
         print("1> waiting for acquiring lock")
         time.sleep(0.1)
     print("1> acquired lock")
     time.sleep(3)
     dlm.unlock("toto")
예제 #4
0
파일: session.py 프로젝트: Gayout/rome
 def __init__(self):
     self.session_id = uuid.uuid1()
     self.session_objects_add = []
     self.session_objects_delete = []
     self.session_timeout = current_milli_time() + Session.max_duration
     self.dlm = ClusterLock()
     self.acquired_locks = []
예제 #5
0
파일: session.py 프로젝트: Gayout/rome
class Session(object):

    max_duration = 300

    def __init__(self):
        self.session_id = uuid.uuid1()
        self.session_objects_add = []
        self.session_objects_delete = []
        self.session_timeout = current_milli_time() + Session.max_duration
        self.dlm = ClusterLock()
        self.acquired_locks = []

    def add(self, *objs):
        for obj in objs:
            if obj not in self.session_objects_add:
                self.session_objects_add += [obj]

    def delete(self, *objs):
        for obj in objs:
            if obj not in self.session_objects_delete:
                self.session_objects_delete += [obj]

    def query(self, *entities, **kwargs):
        from lib.rome.core.orm.query import Query
        return Query(*entities, **merge_dicts(kwargs, {"session": self}))

    def begin(self, *args, **kwargs):
        return SessionControlledExecution(session=self)

    def flush(self, *args, **kwargs):
        logging.info("flushing session %s" % (self.session_id))
        if self.can_commit_request():
            logging.info("committing session %s" % (self.session_id))
            self.commit()

    def can_be_used(self, obj):
        if getattr(obj, "session", None) is None:
            return True
        else:
            if obj.session["session_id"] == self.session_id:
                return True
            if current_milli_time >= obj.session["session_timeout"]:
                return True
        logging.error("session %s cannot use object %s" % (self.session_id, obj))
        return False

    def can_commit_request(self):
        locks = []
        success = True
        for obj in self.session_objects_add + self.session_objects_delete:
            if obj.id is not None:
                lock_name = "session_lock_%s_%s" % (obj.__tablename__, obj.id)
                if self.dlm.lock(lock_name, 100):
                    locks += [lock_name]
                else:
                    success = False
                    break
        if not success:
            logging.error("session %s encountered a conflict, aborting commit" % (self.session_id))
            for lock in locks:
                self.dlm.unlock(lock)
            raise DBDeadlock()
            # raise SessionDeadlock()
        else:
            self.acquired_locks = locks
        return success

    def commit(self):
        logging.info("session %s will start commit" % (self.session_id))
        for obj in self.session_objects_add:
            obj.save(force=True)
        for obj in self.session_objects_delete:
            obj.soft_delete()
        logging.info("session %s committed" % (self.session_id))
        for lock in self.acquired_locks:
            self.dlm.unlock(lock)
            self.acquired_locks.remove(lock)
        self.session_objects_add = []
        self.session_objects_delete = []
예제 #6
0
    return RomeSession()
    # return OldRomeSession()


def model_query(context, *args, **kwargs):
    # base_model = kwargs["base_model"]
    # models = args
    return RomeQuery(*args, **kwargs)


import random
from nova.openstack.common import uuidutils

from lib.rome.driver.redis.lock import ClusterLock as ClusterLock

dlm = ClusterLock()


def acquire_lock(lockname):
    global dlm
    try_to_lock = True
    while try_to_lock:
        if dlm.lock(lockname, 100):
            fo = open("/opt/logs/db_api.log", "a")
            fo.write("[NET] acquired lock: %s\n" % (lockname))
            fo.close()
            return True


def release_lock(lockname):
    global dlm
예제 #7
0
파일: session.py 프로젝트: msimonin/rome
class Session(object):

    max_duration = 300

    def __init__(self):
        self.session_id = uuid.uuid1()
        self.session_objects_add = []
        self.session_objects_delete = []
        self.session_timeout = current_milli_time() + Session.max_duration
        self.dlm = ClusterLock()
        self.acquired_locks = []
        self.already_saved = []

    def already_in(self, obj, objects):
        if obj in objects:
            return True
        obj_signature = "%s" % (obj)
        existing_signature = map(lambda x: "%s" % (x), objects)
        return obj_signature in existing_signature

    def add(self, *objs):
        for obj in objs:
            if hasattr(obj, "is_loaded"):
                if obj.is_loaded:
                    obj = obj.data
                else:
                    continue
            if not self.already_in(obj, self.session_objects_add):
                self.session_objects_add += [obj]

    def update(self, obj):
        if self.already_in(obj, self.session_objects_add):
            filtered = filter(lambda x: ("%s" % (x)) != "%s" % (obj),
                              self.session_objects_add)
            self.session_objects_add = filtered
        if not self.already_in(obj, self.session_objects_add):
            self.session_objects_add += [obj]

    def delete(self, *objs):
        for obj in objs:
            if hasattr(obj, "is_loaded"):
                if obj.is_loaded:
                    obj = obj.data
                else:
                    continue
            if not self.already_in(obj, self.session_objects_delete):
                self.session_objects_delete += [obj]

    def query(self, *entities, **kwargs):
        from lib.rome.core.orm.query import Query
        return Query(*entities, **merge_dicts(kwargs, {"session": self}))

    def begin(self, *args, **kwargs):
        return SessionControlledExecution(session=self)

    def flush(self, *args, **kwargs):
        logging.info("flushing session %s" % (self.session_id))
        if self.can_commit_request():
            logging.info("committing session %s" % (self.session_id))
            self.commit()

    def can_be_used(self, obj):
        if getattr(obj, "_session", None) is None:
            return True
        else:
            if obj.session["session_id"] == self.session_id:
                return True
            if current_milli_time >= obj.session["session_timeout"]:
                return True
        logging.error("session %s cannot use object %s" %
                      (self.session_id, obj))
        return False

    def can_commit_request(self):
        locks = []
        success = True
        for obj in self.session_objects_add + self.session_objects_delete:
            if obj.id is not None:
                lock_name = "session_lock_%s_%s" % (obj.__tablename__, obj.id)
                if self.dlm.lock(lock_name, 100):
                    locks += [lock_name]
                else:
                    success = False
                    break
        if not success:
            logging.error(
                "session %s encountered a conflict, aborting commit" %
                (self.session_id))
            for lock in locks:
                self.dlm.unlock(lock)
            raise DBDeadlock()
            # raise SessionDeadlock()
        else:
            self.acquired_locks = locks
        return success

    def commit(self):
        logging.info("session %s will start commit" % (self.session_id))
        for obj in self.session_objects_add:
            obj.save(force=True, session_saving=self)
        for obj in self.session_objects_delete:
            obj.soft_delete()
        logging.info("session %s committed" % (self.session_id))
        for lock in self.acquired_locks:
            self.dlm.unlock(lock)
            self.acquired_locks.remove(lock)
        self.session_objects_add = []
        self.session_objects_delete = []