Search in sources :

Example 11 with Task

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

the class TestConcurrent method testConcurrentChangeAndGetVersion.

private static void testConcurrentChangeAndGetVersion() throws InterruptedException {
    for (int test = 0; test < 10; test++) {
        final MVStore s = new MVStore.Builder().autoCommitDisabled().open();
        try {
            s.setVersionsToKeep(10);
            final MVMap<Integer, Integer> m = s.openMap("data");
            m.put(1, 1);
            Task task = new Task() {

                @Override
                public void call() throws Exception {
                    while (!stop) {
                        m.put(1, 1);
                        s.commit();
                    }
                }
            };
            task.execute();
            Thread.sleep(1);
            for (int i = 0; i < 10000; i++) {
                if (task.isFinished()) {
                    break;
                }
                for (int j = 0; j < 20; j++) {
                    m.put(1, 1);
                    s.commit();
                }
                s.setVersionsToKeep(15);
                long version = s.getCurrentVersion() - 1;
                try {
                    m.openVersion(version);
                } catch (IllegalArgumentException e) {
                // ignore
                }
                s.setVersionsToKeep(20);
            }
            task.get();
            s.commit();
        } finally {
            s.close();
        }
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task)

Example 12 with Task

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

the class TestConcurrent method testInterruptReopen.

private void testInterruptReopen() throws Exception {
    String fileName = "retry:nio:" + getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    final MVStore s = new MVStore.Builder().fileName(fileName).cacheSize(0).open();
    final Thread mainThread = Thread.currentThread();
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                mainThread.interrupt();
                Thread.sleep(10);
            }
        }
    };
    try {
        MVMap<Integer, byte[]> map = s.openMap("data");
        task.execute();
        for (int i = 0; i < 1000 && !task.isFinished(); i++) {
            map.get(i % 1000);
            map.put(i % 1000, new byte[1024]);
            s.commit();
        }
    } finally {
        task.get();
        s.close();
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task)

Example 13 with Task

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

the class TestConcurrent method testConcurrentIterate.

private static void testConcurrentIterate() {
    MVStore s = new MVStore.Builder().pageSplitSize(3).open();
    s.setVersionsToKeep(100);
    final MVMap<Integer, Integer> map = s.openMap("test");
    final int len = 10;
    final Random r = new Random();
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                int x = r.nextInt(len);
                if (r.nextBoolean()) {
                    map.remove(x);
                } else {
                    map.put(x, r.nextInt(100));
                }
            }
        }
    };
    task.execute();
    try {
        for (int k = 0; k < 10000; k++) {
            Iterator<Integer> it = map.keyIterator(r.nextInt(len));
            long old = s.getCurrentVersion();
            s.commit();
            while (map.getVersion() == old) {
                Thread.yield();
            }
            while (it.hasNext()) {
                it.next();
            }
        }
    } finally {
        task.get();
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task) Random(java.util.Random)

Example 14 with Task

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

the class TestConcurrent method testConcurrentOnlineBackup.

private void testConcurrentOnlineBackup() throws Exception {
    String fileName = getBaseDir() + "/" + getTestName();
    String fileNameRestore = getBaseDir() + "/" + getTestName() + "2";
    final MVStore s = openStore(fileName);
    final MVMap<Integer, byte[]> map = s.openMap("test");
    final Random r = new Random();
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                for (int i = 0; i < 10; i++) {
                    map.put(i, new byte[100 * r.nextInt(100)]);
                }
                s.commit();
                map.clear();
                s.commit();
                long len = s.getFileStore().size();
                if (len > 1024 * 1024) {
                    // slow down writing a lot
                    Thread.sleep(200);
                } else if (len > 20 * 1024) {
                    // slow down writing
                    Thread.sleep(20);
                }
            }
        }
    };
    task.execute();
    try {
        for (int i = 0; i < 10; i++) {
            // System.out.println("test " + i);
            s.setReuseSpace(false);
            OutputStream out = new BufferedOutputStream(new FileOutputStream(fileNameRestore));
            long len = s.getFileStore().size();
            copyFileSlowly(s.getFileStore().getFile(), len, out);
            out.close();
            s.setReuseSpace(true);
            MVStore s2 = openStore(fileNameRestore);
            MVMap<Integer, byte[]> test = s2.openMap("test");
            for (Integer k : test.keySet()) {
                test.get(k);
            }
            s2.close();
            // let it compact
            Thread.sleep(10);
        }
    } finally {
        task.get();
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task) Random(java.util.Random) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 15 with Task

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

the class TestConcurrent method testConcurrentSaveCompact.

private void testConcurrentSaveCompact() throws Exception {
    String fileName = "memFS:" + getTestName();
    FileUtils.delete(fileName);
    final MVStore s = new MVStore.Builder().fileName(fileName).cacheSize(0).open();
    try {
        s.setRetentionTime(0);
        final MVMap<Integer, Integer> dataMap = s.openMap("data");
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                int i = 0;
                while (!stop) {
                    s.compact(100, 1024 * 1024);
                    dataMap.put(i % 1000, i * 10);
                    s.commit();
                    i++;
                }
            }
        };
        task.execute();
        for (int i = 0; i < 1000 && !task.isFinished(); i++) {
            s.compact(100, 1024 * 1024);
            dataMap.put(i % 1000, i * 10);
            s.commit();
        }
        task.get();
    } finally {
        s.close();
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task)

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