Skip to content

Commit

Permalink
feat(api): set the default expire for redis lock
Browse files Browse the repository at this point in the history
  • Loading branch information
pycook committed Dec 9, 2024
1 parent 47ebe55 commit 95b55d2
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 18 deletions.
10 changes: 6 additions & 4 deletions cmdb-api/api/lib/cmdb/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def _auto_inc_id(attr):
db.session.commit()

value_table = TableMap(attr_name=attr.name).table
with redis_lock.Lock(rd.r, "auto_inc_id_{}".format(attr.name)):
with redis_lock.Lock(rd.r, "auto_inc_id_{}".format(attr.name), expire=10):
max_v = value_table.get_by(attr_id=attr.id, only_query=True).order_by(
getattr(value_table, 'value').desc()).first()
if max_v is not None:
Expand Down Expand Up @@ -393,7 +393,7 @@ def add(cls, ci_type_name,
ci = None
record_id = None
password_dict = {}
with redis_lock.Lock(rd.r, ci_type.name):
with redis_lock.Lock(rd.r, ci_type.name, expire=10):
db.session.commit()

if (unique_key.default and unique_key.default.get('default') == AttributeDefaultValueEnum.AUTO_INC_ID and
Expand Down Expand Up @@ -550,7 +550,7 @@ def update(self, ci_id, _is_admin=False, ticket_id=None, _sync=False, **ci_dict)
limit_attrs = self._valid_ci_for_no_read(ci) if not _is_admin else {}

record_id = None
with redis_lock.Lock(rd.r, ci_type.name):
with redis_lock.Lock(rd.r, ci_type.name, expire=10):
db.session.commit()

self._valid_unique_constraint(ci.type_id, ci_dict, ci_id)
Expand Down Expand Up @@ -1268,7 +1268,9 @@ def add(cls, first_ci_id, second_ci_id,
else:
type_relation = CITypeRelation.get_by_id(relation_type_id)

with redis_lock.Lock(rd.r, "ci_relation_add_{}_{}".format(first_ci.type_id, second_ci.type_id)):
with redis_lock.Lock(rd.r,
"ci_relation_add_{}_{}".format(first_ci.type_id, second_ci.type_id),
expire=10):

cls._check_constraint(first_ci_id, first_ci.type_id, second_ci_id, second_ci.type_id, type_relation)

Expand Down
8 changes: 4 additions & 4 deletions cmdb-api/api/lib/cmdb/dcim/rack.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def check_u_slot(self):
CIManager().update(rack['_id'], **payload)

def add_device(self, rack_id, device_id, u_start, u_count=None):
with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id))):
with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id), expire=10)):
self.calc_u_free_count(rack_id, device_id, u_start, u_count)

self.add_relation(rack_id, device_id)
Expand All @@ -139,7 +139,7 @@ def add_device(self, rack_id, device_id, u_start, u_count=None):
OperateHistoryManager().add(operate_type=OperateTypeEnum.ADD_DEVICE, rack_id=rack_id, ci_id=device_id)

def remove_device(self, rack_id, device_id):
with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id))):
with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id), expire=10)):
CIRelationManager.delete_3(rack_id, device_id, apply_async=False, valid=False)

payload = {RackBuiltinAttributes.FREE_U_COUNT: self.calc_u_free_count(rack_id)}
Expand All @@ -151,7 +151,7 @@ def remove_device(self, rack_id, device_id):
OperateHistoryManager().add(operate_type=OperateTypeEnum.REMOVE_DEVICE, rack_id=rack_id, ci_id=device_id)

def move_device(self, rack_id, device_id, to_u_start):
with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id))):
with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id), expire=10)):
payload = {RackBuiltinAttributes.FREE_U_COUNT: self.calc_u_free_count(rack_id, device_id, to_u_start)}
CIManager().update(rack_id, _sync=True, **payload)

Expand All @@ -160,7 +160,7 @@ def move_device(self, rack_id, device_id, to_u_start):
OperateHistoryManager().add(operate_type=OperateTypeEnum.MOVE_DEVICE, rack_id=rack_id, ci_id=device_id)

def migrate_device(self, rack_id, device_id, to_rack_id, to_u_start):
with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id))):
with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id), expire=10)):
self.calc_u_free_count(to_rack_id, device_id, to_u_start)

if rack_id != to_rack_id:
Expand Down
2 changes: 1 addition & 1 deletion cmdb-api/api/lib/cmdb/ipam/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def assign_ips(self, ips, subnet_id, cidr, **kwargs):
else:
return abort(400, ErrFormat.ipam_address_model_not_found)

with (redis_lock.Lock(rd.r, "IPAM_ASSIGN_ADDRESS_{}".format(subnet_id))):
with (redis_lock.Lock(rd.r, "IPAM_ASSIGN_ADDRESS_{}".format(subnet_id), expire=10)):
cis = self._get_cis(subnet_id, ips)
ip2ci = {ci[IPAddressBuiltinAttributes.IP]: ci for ci in cis}

Expand Down
6 changes: 3 additions & 3 deletions cmdb-api/api/lib/cmdb/perms.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def _can_add(self, **kwargs):

def add(self, **kwargs):
kwargs = self._can_add(**kwargs) or kwargs
with redis_lock.Lock(rd.r, 'CMDB_FILTER_{}_{}'.format(kwargs['type_id'], kwargs['rid'])):
with redis_lock.Lock(rd.r, 'CMDB_FILTER_{}_{}'.format(kwargs['type_id'], kwargs['rid']), expire=10):
request_id_filter = {}
if kwargs.get('id_filter'):
obj = self.cls.get_by(type_id=kwargs.get('type_id'),
Expand Down Expand Up @@ -232,7 +232,7 @@ def _can_delete(self, **kwargs):
pass

def delete(self, **kwargs):
with redis_lock.Lock(rd.r, 'CMDB_FILTER_{}_{}'.format(kwargs['type_id'], kwargs['rid'])):
with redis_lock.Lock(rd.r, 'CMDB_FILTER_{}_{}'.format(kwargs['type_id'], kwargs['rid']), expire=10):
obj = self.cls.get_by(type_id=kwargs.get('type_id'),
rid=kwargs.get('rid'),
id_filter=None,
Expand All @@ -249,7 +249,7 @@ def delete(self, **kwargs):

def delete2(self, **kwargs):

with redis_lock.Lock(rd.r, 'CMDB_FILTER_{}_{}'.format(kwargs['type_id'], kwargs['rid'])):
with redis_lock.Lock(rd.r, 'CMDB_FILTER_{}_{}'.format(kwargs['type_id'], kwargs['rid']), expire=10):
obj = self.cls.get_by(type_id=kwargs.get('type_id'),
rid=kwargs.get('rid'),
ci_filter=None,
Expand Down
4 changes: 2 additions & 2 deletions cmdb-api/api/lib/perm/acl/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ def get(cls, app_id):

@classmethod
def add(cls, rid, app_id):
with redis_lock.Lock(rd.r, 'HasResourceRoleCache'):
with redis_lock.Lock(rd.r, 'HasResourceRoleCache', expire=10):
c = cls.get(app_id)
c[rid] = 1
cache.set(cls.PREFIX_KEY.format(app_id), c, timeout=0)

@classmethod
def remove(cls, rid, app_id):
with redis_lock.Lock(rd.r, 'HasResourceRoleCache'):
with redis_lock.Lock(rd.r, 'HasResourceRoleCache', expire=10):
c = cls.get(app_id)
c.pop(rid, None)
cache.set(cls.PREFIX_KEY.format(app_id), c, timeout=0)
Expand Down
2 changes: 1 addition & 1 deletion cmdb-api/api/lib/perm/acl/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def get_users_by_rid(cls, rid, app_id, rid2obj=None, uid2obj=None):

@classmethod
def add(cls, role, parent_id, child_ids, app_id):
with redis_lock.Lock(rd.r, "ROLE_RELATION_ADD"):
with redis_lock.Lock(rd.r, "ROLE_RELATION_ADD", expire=10):
db.session.commit()

result = []
Expand Down
2 changes: 1 addition & 1 deletion cmdb-api/api/tasks/acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
def role_rebuild(rids, app_id):
rids = rids if isinstance(rids, list) else [rids]
for rid in rids:
with redis_lock.Lock(rd.r, "ROLE_REBUILD_{}_{}".format(rid, app_id)):
with redis_lock.Lock(rd.r, "ROLE_REBUILD_{}_{}".format(rid, app_id), expire=10):
RoleRelationCache.rebuild(rid, app_id)

current_app.logger.info("Role {0} App {1} rebuild..........".format(rids, app_id))
Expand Down
4 changes: 2 additions & 2 deletions cmdb-api/api/tasks/cmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def ci_delete_trigger(trigger, operate_type, ci_dict):
@flush_db
@reconnect_db
def ci_relation_cache(parent_id, child_id, ancestor_ids):
with redis_lock.Lock(rd.r, "CIRelation_{}".format(parent_id)):
with redis_lock.Lock(rd.r, "CIRelation_{}".format(parent_id), expire=10):
children = rd.get([parent_id], REDIS_PREFIX_CI_RELATION)[0]
children = json.loads(children) if children is not None else {}

Expand Down Expand Up @@ -223,7 +223,7 @@ def ci_relation_add(parent_dict, child_id, uid):
@celery.task(name="cmdb.ci_relation_delete", queue=CMDB_QUEUE)
@reconnect_db
def ci_relation_delete(parent_id, child_id, ancestor_ids):
with redis_lock.Lock(rd.r, "CIRelation_{}".format(parent_id)):
with redis_lock.Lock(rd.r, "CIRelation_{}".format(parent_id), expire=10):
children = rd.get([parent_id], REDIS_PREFIX_CI_RELATION)[0]
children = json.loads(children) if children is not None else {}

Expand Down

0 comments on commit 95b55d2

Please sign in to comment.