Search in sources :

Example 1 with LockFailedException

use of com.zimbra.cs.mailbox.MailboxLock.LockFailedException in project zm-mailbox by Zimbra.

the class MailboxLockTest method tooManyWaiters.

@Test
public void tooManyWaiters() {
    Mailbox mbox = null;
    try {
        mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    } catch (ServiceException e) {
        Assert.fail();
    }
    int threads = LC.zimbra_mailbox_lock_max_waiting_threads.intValue();
    final AtomicBoolean done = new AtomicBoolean(false);
    final Set<Thread> waitThreads = new HashSet<Thread>();
    for (int i = 0; i < threads; i++) {
        Thread waitThread = new Thread("MailboxLockTest-Waiter") {

            @Override
            public void run() {
                Mailbox mbox;
                try {
                    mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
                    mbox.lock.lock(false);
                    while (!done.get()) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                        }
                    }
                    mbox.lock.release();
                } catch (ServiceException e) {
                }
            }
        };
        waitThread.setDaemon(true);
        waitThreads.add(waitThread);
    }
    Thread writeThread = new Thread("MailboxLockTest-Writer") {

        @Override
        public void run() {
            Mailbox mbox;
            try {
                mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
                mbox.lock.lock(true);
                for (Thread waiter : waitThreads) {
                    waiter.start();
                }
                while (!done.get()) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                    }
                }
                mbox.lock.release();
            } catch (ServiceException e) {
            }
        }
    };
    writeThread.start();
    while (mbox.lock.getQueueLength() < LC.zimbra_mailbox_lock_max_waiting_threads.intValue()) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
    }
    try {
        //one more reader...this should give too many waiters
        mbox.lock.lock(false);
        Assert.fail("expected too many waiters");
    } catch (LockFailedException e) {
        //expected
        Assert.assertTrue(e.getMessage().startsWith("too many waiters"));
        //cause writer to finish
        done.set(true);
    }
    long joinTimeout = 50000;
    joinWithTimeout(writeThread, joinTimeout);
    for (Thread t : waitThreads) {
        joinWithTimeout(t, joinTimeout);
    }
    //now do a write lock in same thread. previously this would break due to read assert not clearing
    mbox.lock.lock(true);
    mbox.lock.release();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServiceException(com.zimbra.common.service.ServiceException) LockFailedException(com.zimbra.cs.mailbox.MailboxLock.LockFailedException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with LockFailedException

use of com.zimbra.cs.mailbox.MailboxLock.LockFailedException in project zm-mailbox by Zimbra.

the class MailboxLockTest method tooManyWaitersWithMultipleReadOwners.

@Test
public void tooManyWaitersWithMultipleReadOwners() {
    Mailbox mbox = null;
    try {
        mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    } catch (ServiceException e) {
        Assert.fail();
    }
    int threads = LC.zimbra_mailbox_lock_max_waiting_threads.intValue();
    final AtomicBoolean done = new AtomicBoolean(false);
    final Set<Thread> waitThreads = new HashSet<Thread>();
    for (int i = 0; i < threads; i++) {
        Thread waitThread = new Thread("MailboxLockTest-Waiter-" + i) {

            @Override
            public void run() {
                Mailbox mbox;
                try {
                    mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
                    mbox.lock.lock(true);
                    while (!done.get()) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                        }
                    }
                    mbox.lock.release();
                } catch (ServiceException e) {
                }
            }
        };
        waitThread.setDaemon(true);
        waitThreads.add(waitThread);
    }
    int readThreadCount = 20;
    final Set<Thread> readThreads = new HashSet<Thread>();
    for (int i = 0; i < readThreadCount; i++) {
        Thread readThread = new Thread("MailboxLockTest-Reader-" + i) {

            @Override
            public void run() {
                Mailbox mbox;
                try {
                    mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
                    int holdCount = 20;
                    for (int i = 0; i < holdCount; i++) {
                        mbox.lock.lock(false);
                    }
                    while (!done.get()) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                        }
                    }
                    for (int i = 0; i < holdCount; i++) {
                        mbox.lock.release();
                    }
                } catch (ServiceException e) {
                }
            }
        };
        readThreads.add(readThread);
    }
    Thread lastReadThread = new Thread("MailboxLockTest-LastReader") {

        @Override
        public void run() {
            Mailbox mbox;
            try {
                mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
                int holdCount = 20;
                for (int i = 0; i < holdCount; i++) {
                    mbox.lock.lock(false);
                }
                //and the other readers
                for (Thread reader : readThreads) {
                    reader.start();
                }
                for (Thread waiter : waitThreads) {
                    waiter.start();
                }
                while (!done.get()) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                    }
                }
                for (int i = 0; i < holdCount; i++) {
                    mbox.lock.release();
                }
            } catch (ServiceException e) {
            }
        }
    };
    lastReadThread.start();
    while (mbox.lock.getQueueLength() < LC.zimbra_mailbox_lock_max_waiting_threads.intValue()) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
    }
    try {
        //one more reader...this should give too many waiters
        mbox.lock.lock(false);
        Assert.fail("expected too many waiters");
    } catch (LockFailedException e) {
        //expected
        Assert.assertTrue(e.getMessage().startsWith("too many waiters"));
        //cause writer to finish
        done.set(true);
    }
    long joinTimeout = 50000;
    joinWithTimeout(lastReadThread, joinTimeout);
    for (Thread t : readThreads) {
        joinWithTimeout(t, joinTimeout);
    }
    for (Thread t : waitThreads) {
        joinWithTimeout(t, joinTimeout);
    }
    //now do a write lock in same thread. previously this would break due to read assert not clearing
    mbox.lock.lock(true);
    mbox.lock.release();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServiceException(com.zimbra.common.service.ServiceException) LockFailedException(com.zimbra.cs.mailbox.MailboxLock.LockFailedException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with LockFailedException

use of com.zimbra.cs.mailbox.MailboxLock.LockFailedException in project zm-mailbox by Zimbra.

the class MailboxLockTest method tooManyWaitersWithSingleReadOwner.

@Test
public void tooManyWaitersWithSingleReadOwner() {
    Mailbox mbox = null;
    try {
        mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    } catch (ServiceException e) {
        Assert.fail();
    }
    int threads = LC.zimbra_mailbox_lock_max_waiting_threads.intValue();
    final AtomicBoolean done = new AtomicBoolean(false);
    final Set<Thread> waitThreads = new HashSet<Thread>();
    for (int i = 0; i < threads; i++) {
        Thread waitThread = new Thread("MailboxLockTest-Waiter") {

            @Override
            public void run() {
                Mailbox mbox;
                try {
                    mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
                    mbox.lock.lock(true);
                    while (!done.get()) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                        }
                    }
                    mbox.lock.release();
                } catch (ServiceException e) {
                }
            }
        };
        waitThread.setDaemon(true);
        waitThreads.add(waitThread);
    }
    Thread readThread = new Thread("MailboxLockTest-Reader") {

        @Override
        public void run() {
            Mailbox mbox;
            try {
                mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
                int holdCount = 20;
                for (int i = 0; i < holdCount; i++) {
                    mbox.lock.lock(false);
                }
                for (Thread waiter : waitThreads) {
                    waiter.start();
                }
                while (!done.get()) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                    }
                }
                for (int i = 0; i < holdCount; i++) {
                    mbox.lock.release();
                }
            } catch (ServiceException e) {
            }
        }
    };
    readThread.start();
    while (mbox.lock.getQueueLength() < LC.zimbra_mailbox_lock_max_waiting_threads.intValue()) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
    }
    try {
        //one more reader...this should give too many waiters
        mbox.lock.lock(false);
        Assert.fail("expected too many waiters");
    } catch (LockFailedException e) {
        //expected
        Assert.assertTrue(e.getMessage().startsWith("too many waiters"));
        //cause writer to finish
        done.set(true);
    }
    long joinTimeout = 50000;
    joinWithTimeout(readThread, joinTimeout);
    for (Thread t : waitThreads) {
        joinWithTimeout(t, joinTimeout);
    }
    //now do a write lock in same thread. previously this would break due to read assert not clearing
    mbox.lock.lock(true);
    mbox.lock.release();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServiceException(com.zimbra.common.service.ServiceException) LockFailedException(com.zimbra.cs.mailbox.MailboxLock.LockFailedException) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ServiceException (com.zimbra.common.service.ServiceException)3 LockFailedException (com.zimbra.cs.mailbox.MailboxLock.LockFailedException)3 HashSet (java.util.HashSet)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Test (org.junit.Test)3