c79f68549dc0a0899901558f7d3c0be71b45f524
[multicloud/framework.git] / multivimbroker / multivimbroker / pub / utils / share_lock.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Copyright (c) 2017 Wind River Systems, Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
14 import time
15
16 import redis
17
18 from multivimbroker.pub.config.config import REDIS_HOST
19 from multivimbroker.pub.config.config import REDIS_PORT
20 from multivimbroker.pub.config.config import REDIS_PASSWD
21
22
23 class SharedLock:
24     def __init__(self, lock_key, host=REDIS_HOST, port=REDIS_PORT,
25                  password=REDIS_PASSWD, db=9, lock_timeout=5 * 60):
26         self.lock_key = lock_key
27         self.lock_timeout = lock_timeout
28         self.redis = redis.Redis(host=host, port=port,
29                                  db=db, password=password)
30         self.acquire_time = -1
31
32     def acquire(self):
33         begin = now = int(time.time())
34         while (now - begin) < self.lock_timeout:
35
36             result = self.redis.setnx(self.lock_key, now +
37                                       self.lock_timeout + 1)
38             if result == 1 or result is True:
39                 self.acquire_time = now
40                 return True
41
42             current_lock_timestamp = self.redis.get(self.lock_key)
43             if not current_lock_timestamp:
44                 time.sleep(1)
45                 continue
46
47             current_lock_timestamp = int(current_lock_timestamp)
48
49             if now > current_lock_timestamp:
50                 next_lock_timestamp = self.redis.getset(self.lock_key, now +
51                                                         self.lock_timeout + 1)
52                 if not next_lock_timestamp:
53                     time.sleep(1)
54                     continue
55                 next_lock_timestamp = int(next_lock_timestamp)
56
57                 if next_lock_timestamp == current_lock_timestamp:
58                     self.acquire_time = now
59                     return True
60             else:
61                 time.sleep(1)
62                 continue
63         return False
64
65     def release(self):
66         now = int(time.time())
67         if now > self.acquire_time + self.lock_timeout:
68             # key expired, do nothing and let other clients handle it
69             return
70         self.acquire_time = None
71         self.redis.delete(self.lock_key)
72
73
74 def do_biz_with_share_lock(lock_name, callback):
75     lock = SharedLock(lock_name)
76     try:
77         if not lock.acquire():
78             raise Exception(lock_name + " timeout")
79         callback()
80     except Exception as e:
81         raise e
82     finally:
83         lock.release()