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