锁定表会导致django出现保存点问题

chy5wohz  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(328)

我试图在django应用程序的mariadb数据库中构建一个有向无环图(dag)。因为这是非循环的,所以我需要验证任何添加的元素(顶点/边)都不会在图中创建循环。
许多客户机将尝试在一天中同时添加元素,但是这些循环检查需要是原子的,因此我推断在添加/更新元素时需要使用一些锁。django似乎没有提供这样的东西,所以我尝试使用一个原始的 LOCK TABLES / UNLOCK TABLES 查询。这是我用来做的代码。。。

def lock_tables():                                                                    
    cursor = get_connection(DEFAULT_DB_ALIAS).cursor()                            

    tables = [                                                                    
        'vertex',                                                  
        'edge'                                                                                             
    ]                                                                             

    lock_query = ', '.join(                                                
        "{} {}".format(table, 'WRITE') for table in tables                        
    )                                                                             

    query = 'LOCK TABLES {}'.format(lock_query)                            
    cursor.execute(query)                                                         

def unlock_tables():                                                                  
    cursor = get_connection(DEFAULT_DB_ALIAS).cursor()                            
    cursor.execute('UNLOCK TABLES')

然后在我的模式下 save 方法。。。

@transaction.atomic()
def save(self, *args,**kwargs):

    print("---INSIDE MODEL SAVE")

    try:
        print("---LOCKING TABLES")
        lock_tables()
        print("---LOCKED TABLES")

        super().save(*args,**kwargs)

        # TODO: Add Cycle check here
    except Exception as ex:
        print("---EXCEPTION THROWN INSIDE SAVE: {}".format(ex))
        raise
    finally:
        print("---UNLOCKING TABLES")
        unlock_tables()
        print("---UNLOCKED TABLES")

但是,有关锁定和解锁这些表的某些内容会影响使用创建的保存点 django.db.transaction.atomic ... 在django试图退出 atomic 上下文中,它尝试回滚到已发布的保存点。
下面是一些日志,我试图捕捉问题, Executing Query 线路来自 django.db.backends.mysql.base , STARTING/EXITING ATOMIC 线路来自
django.db.transactions.atomic __enter__ / __exit__ 方法及注意事项 #### 是我在事后加上的评论,试图解释我的想法。

---STARTING ATOMIC  #### Atomic context wrapping my serializer's create method
Executing query: 'SAVEPOINT `s139667621889792_x1`' - args: None

---STARTING ATOMIC  #### Atomic context wrapping my model's save method
Executing query: 'SAVEPOINT `s139667621889792_x2`' - args: None

---INSIDE MODEL SAVE
---LOCKING TABLES
Executing query: 'LOCK TABLES vertex WRITE, edge WRITE
---LOCKED TABLES

---STARTING ATOMIC  #### I think Django must wrap some queries in an atomic block, but this doesnt even create a savepoint
Executing query: 'INSERT INTO `edge`...
---EXITING ATOMIC

#### WHERE MY CYCLE CHECK CODE WOULD RUN - not implemented yet

---UNLOCKING TABLES
Executing query: 'UNLOCK TABLES' - args: None
---UNLOCKED TABLES

---EXITING ATOMIC
Executing query: 'RELEASE SAVEPOINT `s139667621889792_x2`' - args: None
Executing query: 'ROLLBACK TO SAVEPOINT `s139667621889792_x2`' - args: None   ### WHAT I BELIEVE TO BE THE OFFENDING QUERY

---EXITING ATOMIC
Executing query: 'ROLLBACK TO SAVEPOINT `s139667621889792_x1`' - args: None

Traceback (most recent call last):
  File ".../site-packages/django/db/backends/utils.py", line 83, in _execute
    return self.cursor.execute(sql)
  File ".../site-packages/django/db/backends/mysql/base.py", line 72, in execute
    return self.cursor.execute(query, args)
  File ".../site-packages/pymysql/cursors.py", line 170, in execute
    result = self._query(query)
  File ".../site-packages/pymysql/cursors.py", line 328, in _query
    conn.query(q)
  File ".../site-packages/pymysql/connections.py", line 516, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File ".../site-packages/pymysql/connections.py", line 727, in _read_query_result
    result.read()
  File ".../site-packages/pymysql/connections.py", line 1066, in read
    first_packet = self.connection._read_packet()
  File ".../site-packages/pymysql/connections.py", line 683, in _read_packet
    packet.check_error()
  File ".../site-packages/pymysql/protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File ".../site-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.InternalError: (1305, 'SAVEPOINT s139667621889792_x2 does not exist')

如上所示,django试图回滚到它已经发布的保存点。如果我删除了对lock/unlock表的调用,那么这段代码可以完美地工作,但是我不能再保证我的周期检查是原子的。
以前有没有人遇到过这个问题,或者有没有什么技巧可以让你更深入地了解原因?
编辑:我读得越多,我越觉得我想要的行为是不可能的。根据mysql关于锁的文档,当您在表上获得锁时,事务似乎被提交。这打破了我的用例,因为我希望在循环检查失败时回滚事务。

rqmkfv5c

rqmkfv5c1#

任何反循环算法都取决于在执行检查时表没有改变。对的?执行周期检查需要多长时间?你每天需要多少支票?
假设你有足够的时间做所有这些工作,那么考虑一下:

SELECT GET_LOCK('cycle_check');  -- (you may want timeout)
BEGIN;
INSERT new item in graph
perform cycle check
if ... COMMIT else ROLLBACK
SELECT RELEASE_LOCK('cycle_check');

请注意,此锁定机制与 LOCK TABLES 毫无用处。
要防止在循环检查期间读取,还需要:

SELECT GET_LOCK('cycle_check');
SELECT ...;
SELECT RELEASE_LOCK('cycle_check');

(旁注:这是极为罕见的 GET_LOCK 以“正确”的方式进行锁定。请不要将此扩展到任意其他情况。)

相关问题