Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LockTableがデッドロックする問題 #18

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

yokomotod
Copy link
Owner

@yokomotod yokomotod commented Aug 18, 2024

#20 の再現コード

@yokomotod yokomotod changed the base branch from main to test-race August 18, 2024 17:44
lm := db.LogManager()
bm := db.BufferManager()

n := 10
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n := 34 だと確率的にPASSする。5ぐらいからほぼ通らなくなる

@yokomotod
Copy link
Owner Author

Javaのコードも 💀 ?

public class ManyConcurrencyTest {
   private static FileMgr fm;
   private static LogMgr lm;
   private static BufferMgr bm;

   public static void main(String[] args) {
      // initialize the database system
      SimpleDB db = new SimpleDB("concurrencytest", 400, 8);
      fm = db.fileMgr();
      lm = db.logMgr();
      bm = db.bufferMgr();
      for (int i = 0; i < 10; i++) {
         A a = new A();
         new Thread(a).start();
      }
   }

   static class A implements Runnable {
      public void run() {
         try {
            Transaction txA = new Transaction(fm, lm, bm);
            BlockId blk1 = new BlockId("testfile", 1);
            txA.pin(blk1);
            System.out.println("Tx A: request xlock 1");
            txA.setInt(blk1, 0, 0, false);
            System.out.println("Tx A: receive xlock 1");
            Thread.sleep(100);
            txA.commit();
            System.out.println("Tx A: commit");
         } catch (InterruptedException e) {
         }
      }
   }
}
Tx A: request xlock 1
Tx A: request xlock 1
Tx A: request xlock 1
Tx A: request xlock 1
Tx A: request xlock 1
Tx A: request xlock 1
Tx A: request xlock 1
Tx A: request xlock 1
Tx A: request xlock 1
Tx A: request xlock 1
Tx A: receive xlock 1
transaction 4 committed
Tx A: commit

Exception in thread "Thread-5" Exception in thread "Thread-9" Exception in thread "Thread-8" Exception in thread "Thread-6" simpledb.tx.concurrency.LockAbortException
	at simpledb.tx.concurrency.LockTable.xLock(LockTable.java:63)
	at simpledb.tx.concurrency.ConcurrencyMgr.xLock(ConcurrencyMgr.java:46)
	at simpledb.tx.Transaction.setInt(Transaction.java:149)
	at simpledb.tx.ManyConcurrencyTest$A.run(ManyConcurrencyTest.java:32)
	at java.base/java.lang.Thread.run(Thread.java:829)
Exception in thread "Thread-1" Exception in thread "Thread-2" simpledb.tx.concurrency.LockAbortException
	at simpledb.tx.concurrency.LockTable.xLock(LockTable.java:63)
	at simpledb.tx.concurrency.ConcurrencyMgr.xLock(ConcurrencyMgr.java:46)
	at simpledb.tx.Transaction.setInt(Transaction.java:149)
	at simpledb.tx.ManyConcurrencyTest$A.run(ManyConcurrencyTest.java:32)
	at java.base/java.lang.Thread.run(Thread.java:829)
Exception in thread "Thread-0" simpledb.tx.concurrency.LockAbortException
	at simpledb.tx.concurrency.LockTable.xLock(LockTable.java:63)
	at simpledb.tx.concurrency.ConcurrencyMgr.xLock(ConcurrencyMgr.java:46)
	at simpledb.tx.Transaction.setInt(Transaction.java:149)
	at simpledb.tx.ManyConcurrencyTest$A.run(ManyConcurrencyTest.java:32)
	at java.base/java.lang.Thread.run(Thread.java:829)
Exception in thread "Thread-3" simpledb.tx.concurrency.LockAbortException
	at simpledb.tx.concurrency.LockTable.xLock(LockTable.java:63)
	at simpledb.tx.concurrency.ConcurrencyMgr.xLock(ConcurrencyMgr.java:46)
	at simpledb.tx.Transaction.setInt(Transaction.java:149)
	at simpledb.tx.ManyConcurrencyTest$A.run(ManyConcurrencyTest.java:32)
	at java.base/java.lang.Thread.run(Thread.java:829)
simpledb.tx.concurrency.LockAbortException
	at simpledb.tx.concurrency.LockTable.xLock(LockTable.java:63)
	at simpledb.tx.concurrency.ConcurrencyMgr.xLock(ConcurrencyMgr.java:46)
	at simpledb.tx.Transaction.setInt(Transaction.java:149)
	at simpledb.tx.ManyConcurrencyTest$A.run(ManyConcurrencyTest.java:32)
	at java.base/java.lang.Thread.run(Thread.java:829)
simpledb.tx.concurrency.LockAbortException
	at simpledb.tx.concurrency.LockTable.xLock(LockTable.java:63)
	at simpledb.tx.concurrency.ConcurrencyMgr.xLock(ConcurrencyMgr.java:46)
	at simpledb.tx.Transaction.setInt(Transaction.java:149)
	at simpledb.tx.ManyConcurrencyTest$A.run(ManyConcurrencyTest.java:32)
	at java.base/java.lang.Thread.run(Thread.java:829)
Exception in thread "Thread-7" simpledb.tx.concurrency.LockAbortException
	at simpledb.tx.concurrency.LockTable.xLock(LockTable.java:63)
	at simpledb.tx.concurrency.ConcurrencyMgr.xLock(ConcurrencyMgr.java:46)
	at simpledb.tx.Transaction.setInt(Transaction.java:149)
	at simpledb.tx.ManyConcurrencyTest$A.run(ManyConcurrencyTest.java:32)
	at java.base/java.lang.Thread.run(Thread.java:829)
simpledb.tx.concurrency.LockAbortException
	at simpledb.tx.concurrency.LockTable.xLock(LockTable.java:63)
	at simpledb.tx.concurrency.ConcurrencyMgr.xLock(ConcurrencyMgr.java:46)
	at simpledb.tx.Transaction.setInt(Transaction.java:149)
	at simpledb.tx.ManyConcurrencyTest$A.run(ManyConcurrencyTest.java:32)
	at java.base/java.lang.Thread.run(Thread.java:829)
simpledb.tx.concurrency.LockAbortException
	at simpledb.tx.concurrency.LockTable.xLock(LockTable.java:63)
	at simpledb.tx.concurrency.ConcurrencyMgr.xLock(ConcurrencyMgr.java:46)
	at simpledb.tx.Transaction.setInt(Transaction.java:149)
	at simpledb.tx.ManyConcurrencyTest$A.run(ManyConcurrencyTest.java:32)
	at java.base/java.lang.Thread.run(Thread.java:829)

@yokomotod
Copy link
Owner Author

yokomotod commented Aug 18, 2024

concurrency.Manager.XLock() がまずslockをとってからxlockを取るところで

  • 0: 最初のスレッドがxlockまで取り終わっている、ほかたくさんのスレッドが(xlockで、まずはslockを取るところの)slockで待っている
  • 1: 最初のスレッドがxlockを解放する(commit)
  • 2: たくさんのスレッドが起き上がり、 複数のスレッドが slockを取る(複数スレッドから取れる)ところまで進んでしまう
  • 3: 次にxlockを取ろうとして、自分以外にもslockを取っているのでwaitに入ってしまう

という流れに見える…?

期待する動作は

  • 3: 一人だけがslockを取って、次にxlockに昇格、まで完了
  • 4: ほかのスレッドはslockが取れなくて再度待機

の流れ?

Base automatically changed from test-race to main August 19, 2024 02:47
@yokomotod yokomotod changed the title LockTableがUnlockを見逃す問題 LockTableがデッドロックする問題 Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant