Re: Atomic Operations?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 16-12-23 22:14, Kent Borg wrote:
Hello, a newbie here!

Doing some playing with Python and librados, and it is mostly easy to use, but I am confused about atomic operations. The documentation isn't clear to me, and Google isn't giving me obvious answers either...

I would like to do some locking. The data structures I am playing with in RADOS should work great if I don't accidentally fire up more than one instance of myself. So I would like to drop an attribute on a central object saying it's mine, all mine--but only if another copy of myself hasn't done so already.

Is there some sample code to show me the safe way to do this?

I was using this approach when I needed to ensure that only one instance of script is running. It would grab a lock and do what it needs to do, you'll need to adjust it to work with your case:

def break_lock():
    try:
ceph_ioctx.lock_exclusive(key=lock_obj_name + '_breaker', name=lock_obj_name + '_lock', cookie='cookie1', duration=60)
        ceph_ioctx.remove_object(key=lock_obj_name)
ceph_ioctx.unlock(key=lock_obj_name + '_breaker', name=lock_obj_name + '_lock', cookie='cookie1')
        ceph_ioctx.remove_object(key=lock_obj_name + '_breaker')
        logger.info('Broke lock')
    except:
        logger.error('Failed to break lock')
        return False
    return True


def try_locking():
    try:
ceph_ioctx.lock_exclusive(key=lock_obj_name, name=lock_obj_name + '_lock', cookie='cookie1', duration=72000)
        ceph_ioctx.write_full(lock_obj_name, str(date_now))
        got_lock = True
        logger.info('Locked')
    except:
        got_lock = False
        logger.warning("Didn't lock")
    return got_lock


def aquire_lock():
    # Let's aquire lock and update objects timestamp
    if not try_locking():
        logger.warning('Failed to lock, preping for retry')
        lock_date = ceph_ioctx.read(lock_obj_name)
lock_age = int((date_now - dateutil.parser.parse(lock_date)).total_seconds() / 60 / 60)
        logger.debug('Lock age: %i' % (lock_age))

        if lock_age > 20 and break_lock():
            logger.warning('Retrying locking')
            try_locking()
        else:
            logger.error("Failed lock breaking, bailing out")
            return False
    return True


def release_lock():
    logger.info('Releasing lock')
ceph_ioctx.unlock(key=lock_obj_name, name=lock_obj_name + '_lock', cookie='cookie1')
    ceph_ioctx.remove_object(key=lock_obj_name)


Thanks,

-kb, the Kent who is reluctant to just play around with locking to see what works...because it might look like it is working, yet still have a race susceptibility.

_______________________________________________
ceph-users mailing list
ceph-users@xxxxxxxxxxxxxx
http://lists.ceph.com/listinfo.cgi/ceph-users-ceph.com


_______________________________________________
ceph-users mailing list
ceph-users@xxxxxxxxxxxxxx
http://lists.ceph.com/listinfo.cgi/ceph-users-ceph.com



[Index of Archives]     [Information on CEPH]     [Linux Filesystem Development]     [Ceph Development]     [Ceph Large]     [Ceph Dev]     [Linux USB Development]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [xfs]


  Powered by Linux