Search in sources :

Example 1 with Task

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

the class TestFileLockSerialized method testTwoWriters.

private void testTwoWriters() throws Exception {
    deleteDb("fileLockSerialized");
    String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
    final String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE";
    Connection conn = getConnection(writeUrl, "sa", "sa");
    conn.createStatement().execute("create table test(id identity) as " + "select x from system_range(1, 100)");
    conn.close();
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                Thread.sleep(10);
                Connection c = getConnection(writeUrl, "sa", "sa");
                c.createStatement().execute("select * from test");
                c.close();
            }
        }
    }.execute();
    Thread.sleep(20);
    for (int i = 0; i < 2; i++) {
        conn = getConnection(writeUrl, "sa", "sa");
        Statement stat = conn.createStatement();
        stat.execute("drop table test");
        stat.execute("create table test(id identity) as " + "select x from system_range(1, 100)");
        conn.createStatement().execute("select * from test");
        conn.close();
    }
    Thread.sleep(100);
    conn = getConnection(writeUrl, "sa", "sa");
    conn.createStatement().execute("select * from test");
    conn.close();
    task.get();
}
Also used : Task(org.h2.util.Task) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) SQLException(java.sql.SQLException)

Example 2 with Task

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

the class TestConcurrent method testConcurrentDataType.

private void testConcurrentDataType() throws InterruptedException {
    final ObjectDataType type = new ObjectDataType();
    final Object[] data = new Object[] { null, -1, 1, 10, "Hello", new Object[] { new byte[] { (byte) -1, (byte) 1 }, null }, new Object[] { new byte[] { (byte) 1, (byte) -1 }, 10 }, new Object[] { new byte[] { (byte) -1, (byte) 1 }, 20L }, new Object[] { new byte[] { (byte) 1, (byte) -1 }, 5 } };
    Arrays.sort(data, new Comparator<Object>() {

        @Override
        public int compare(Object o1, Object o2) {
            return type.compare(o1, o2);
        }
    });
    Task[] tasks = new Task[2];
    for (int i = 0; i < tasks.length; i++) {
        tasks[i] = new Task() {

            @Override
            public void call() throws Exception {
                Random r = new Random();
                WriteBuffer buff = new WriteBuffer();
                while (!stop) {
                    int a = r.nextInt(data.length);
                    int b = r.nextInt(data.length);
                    int comp;
                    if (r.nextBoolean()) {
                        comp = type.compare(a, b);
                    } else {
                        comp = -type.compare(b, a);
                    }
                    buff.clear();
                    type.write(buff, a);
                    buff.clear();
                    type.write(buff, b);
                    if (a == b) {
                        assertEquals(0, comp);
                    } else {
                        assertEquals(a > b ? 1 : -1, comp);
                    }
                }
            }
        };
        tasks[i].execute();
    }
    try {
        Thread.sleep(100);
    } finally {
        for (Task t : tasks) {
            t.get();
        }
    }
}
Also used : Task(org.h2.util.Task) WriteBuffer(org.h2.mvstore.WriteBuffer) Random(java.util.Random) ObjectDataType(org.h2.mvstore.type.ObjectDataType) ConcurrentModificationException(java.util.ConcurrentModificationException)

Example 3 with Task

use of org.gridgain.internal.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 4 with Task

use of org.gridgain.internal.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 5 with Task

use of org.gridgain.internal.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)

Aggregations

Task (org.h2.util.Task)225 Connection (java.sql.Connection)128 Statement (java.sql.Statement)104 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)103 PreparedStatement (java.sql.PreparedStatement)87 MVStore (org.h2.mvstore.MVStore)75 Random (java.util.Random)73 SQLException (java.sql.SQLException)68 Task (org.gridgain.internal.h2.util.Task)66 ResultSet (java.sql.ResultSet)60 CountDownLatch (java.util.concurrent.CountDownLatch)25 ArrayList (java.util.ArrayList)22 ServerSocket (java.net.ServerSocket)20 Socket (java.net.Socket)20 MVStore (org.gridgain.internal.h2.mvstore.MVStore)20 ConcurrentModificationException (java.util.ConcurrentModificationException)16 SSLServerSocket (javax.net.ssl.SSLServerSocket)16 SSLSocket (javax.net.ssl.SSLSocket)16 MVStoreException (org.h2.mvstore.MVStoreException)15 IOException (java.io.IOException)13