Search in sources :

Example 16 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestConcurrent method testConcurrentAutoCommitAndChange.

private void testConcurrentAutoCommitAndChange() throws InterruptedException {
    String fileName = "memFS:" + getTestName();
    FileUtils.delete(fileName);
    final MVStore s = new MVStore.Builder().fileName(fileName).pageSplitSize(1000).open();
    try {
        s.setRetentionTime(1000);
        s.setAutoCommitDelay(1);
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                while (!stop) {
                    s.compact(100, 1024 * 1024);
                }
            }
        };
        final MVMap<Integer, Integer> dataMap = s.openMap("data");
        final MVMap<Integer, Integer> dataSmallMap = s.openMap("dataSmall");
        s.openMap("emptyMap");
        final AtomicInteger counter = new AtomicInteger();
        Task task2 = new Task() {

            @Override
            public void call() throws Exception {
                while (!stop) {
                    int i = counter.getAndIncrement();
                    dataMap.put(i, i * 10);
                    dataSmallMap.put(i % 100, i * 10);
                    if (i % 100 == 0) {
                        dataSmallMap.clear();
                    }
                }
            }
        };
        task.execute();
        task2.execute();
        Thread.sleep(1);
        for (int i = 0; !task.isFinished() && !task2.isFinished() && i < 1000; i++) {
            MVMap<Integer, Integer> map = s.openMap("d" + (i % 3));
            map.put(0, i);
            s.commit();
        }
        task.get();
        task2.get();
        for (int i = 0; i < counter.get(); i++) {
            assertEquals(10 * i, dataMap.get(i).intValue());
        }
    } finally {
        s.close();
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 17 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestConcurrent method testConcurrentReplaceAndRead.

private void testConcurrentReplaceAndRead() throws InterruptedException {
    final MVStore s = new MVStore.Builder().open();
    final MVMap<Integer, Integer> map = s.openMap("data");
    for (int i = 0; i < 100; i++) {
        map.put(i, i % 100);
    }
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            int i = 0;
            while (!stop) {
                map.put(i % 100, i % 100);
                i++;
                if (i % 1000 == 0) {
                    s.commit();
                }
            }
        }
    };
    task.execute();
    try {
        Thread.sleep(1);
        for (int i = 0; !task.isFinished() && i < 1000000; i++) {
            assertEquals(i % 100, map.get(i % 100).intValue());
        }
    } finally {
        task.get();
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task)

Example 18 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestConcurrent method testConcurrentChangeAndCompact.

private void testConcurrentChangeAndCompact() throws InterruptedException {
    String fileName = "memFS:" + getTestName();
    FileUtils.delete(fileName);
    final MVStore s = new MVStore.Builder().fileName(fileName).pageSplitSize(10).autoCommitDisabled().open();
    s.setRetentionTime(10000);
    try {
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                while (!stop) {
                    s.compact(100, 1024 * 1024);
                }
            }
        };
        task.execute();
        Task task2 = new Task() {

            @Override
            public void call() throws Exception {
                while (!stop) {
                    s.compact(100, 1024 * 1024);
                }
            }
        };
        task2.execute();
        Thread.sleep(1);
        for (int i = 0; !task.isFinished() && !task2.isFinished() && i < 1000; i++) {
            MVMap<Integer, Integer> map = s.openMap("d" + (i % 3));
            // MVMap<Integer, Integer> map = s.openMap("d" + (i % 3),
            // new MVMapConcurrent.Builder<Integer, Integer>());
            map.put(0, i);
            map.get(0);
            s.commit();
        }
        task.get();
        task2.get();
    } finally {
        s.close();
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task)

Example 19 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestConcurrent method testConcurrentFree.

private void testConcurrentFree() throws InterruptedException {
    String fileName = "memFS:" + getTestName();
    for (int test = 0; test < 10; test++) {
        FileUtils.delete(fileName);
        final MVStore s1 = new MVStore.Builder().fileName(fileName).autoCommitDisabled().open();
        s1.setRetentionTime(0);
        final int count = 200;
        for (int i = 0; i < count; i++) {
            MVMap<Integer, Integer> m = s1.openMap("d" + i);
            m.put(1, 1);
            if (i % 2 == 0) {
                s1.commit();
            }
        }
        s1.close();
        final MVStore s = new MVStore.Builder().fileName(fileName).autoCommitDisabled().open();
        try {
            s.setRetentionTime(0);
            final ArrayList<MVMap<Integer, Integer>> list = New.arrayList();
            for (int i = 0; i < count; i++) {
                MVMap<Integer, Integer> m = s.openMap("d" + i);
                list.add(m);
            }
            final AtomicInteger counter = new AtomicInteger();
            Task task = new Task() {

                @Override
                public void call() throws Exception {
                    while (!stop) {
                        int x = counter.getAndIncrement();
                        if (x >= count) {
                            break;
                        }
                        MVMap<Integer, Integer> m = list.get(x);
                        m.clear();
                        s.removeMap(m);
                    }
                }
            };
            task.execute();
            Thread.sleep(1);
            while (true) {
                int x = counter.getAndIncrement();
                if (x >= count) {
                    break;
                }
                MVMap<Integer, Integer> m = list.get(x);
                m.clear();
                s.removeMap(m);
                if (x % 5 == 0) {
                    s.commit();
                }
            }
            task.get();
            // this will mark old chunks as unused,
            // but not remove (and overwrite) them yet
            s.commit();
            // this will remove them, so we end up with
            // one unused one, and one active one
            MVMap<Integer, Integer> m = s.openMap("dummy");
            m.put(1, 1);
            s.commit();
            m.put(2, 2);
            s.commit();
            MVMap<String, String> meta = s.getMetaMap();
            int chunkCount = 0;
            for (String k : meta.keyList()) {
                if (k.startsWith("chunk.")) {
                    chunkCount++;
                }
            }
            assertTrue("" + chunkCount, chunkCount < 3);
        } finally {
            s.close();
        }
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MVMap(org.h2.mvstore.MVMap)

Example 20 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestMVTableEngine method testLocking.

private void testLocking() throws Exception {
    deleteDb(getTestName());
    String dbName = getTestName() + ";MV_STORE=TRUE;MVCC=FALSE";
    Connection conn = getConnection(dbName);
    Statement stat = conn.createStatement();
    stat.execute("set lock_timeout 1000");
    stat.execute("create table a(id int primary key, name varchar)");
    stat.execute("create table b(id int primary key, name varchar)");
    Connection conn1 = getConnection(dbName);
    final Statement stat1 = conn1.createStatement();
    stat1.execute("set lock_timeout 1000");
    conn.setAutoCommit(false);
    conn1.setAutoCommit(false);
    stat.execute("insert into a values(1, 'Hello')");
    stat1.execute("insert into b values(1, 'Hello')");
    Task t = new Task() {

        @Override
        public void call() throws Exception {
            stat1.execute("insert into a values(2, 'World')");
        }
    };
    t.execute();
    try {
        stat.execute("insert into b values(2, 'World')");
        throw t.getException();
    } catch (SQLException e) {
        assertEquals(e.toString(), ErrorCode.DEADLOCK_1, e.getErrorCode());
    }
    conn1.close();
    conn.close();
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Aggregations

Task (org.h2.util.Task)71 Connection (java.sql.Connection)33 Statement (java.sql.Statement)27 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 PreparedStatement (java.sql.PreparedStatement)22 SQLException (java.sql.SQLException)22 MVStore (org.h2.mvstore.MVStore)20 Random (java.util.Random)18 ResultSet (java.sql.ResultSet)14 JdbcConnection (org.h2.jdbc.JdbcConnection)7 IOException (java.io.IOException)5 ServerSocket (java.net.ServerSocket)5 Socket (java.net.Socket)5 ConcurrentModificationException (java.util.ConcurrentModificationException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 SSLServerSocket (javax.net.ssl.SSLServerSocket)4 SSLSocket (javax.net.ssl.SSLSocket)4 OutputStream (java.io.OutputStream)3 PipedInputStream (java.io.PipedInputStream)3 PipedOutputStream (java.io.PipedOutputStream)3