Search in sources :

Example 21 with Task

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

the class TestTransactionStore method testStopWhileCommitting.

private void testStopWhileCommitting() throws Exception {
    String fileName = getBaseDir() + "/testStopWhileCommitting.h3";
    FileUtils.delete(fileName);
    Random r = new Random(0);
    for (int i = 0; i < 10; ) {
        MVStore s;
        TransactionStore ts;
        Transaction tx;
        TransactionMap<Integer, String> m;
        s = MVStore.open(fileName);
        ts = new TransactionStore(s);
        ts.init();
        tx = ts.begin();
        s.setReuseSpace(false);
        m = tx.openMap("test");
        final String value = "x" + i;
        for (int j = 0; j < 1000; j++) {
            m.put(j, value);
        }
        final AtomicInteger state = new AtomicInteger();
        final MVStore store = s;
        final MVMap<Integer, String> other = s.openMap("other");
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                for (int i = 0; !stop; i++) {
                    state.set(i);
                    other.put(i, value);
                    store.commit();
                }
            }
        };
        task.execute();
        // wait for the task to start
        while (state.get() < 1) {
            Thread.yield();
        }
        // commit while writing in the task
        tx.commit();
        // wait for the task to stop
        task.get();
        store.close();
        s = MVStore.open(fileName);
        // roll back a bit, until we have some undo log entries
        assertTrue(s.hasMap("undoLog"));
        for (int back = 0; back < 100; back++) {
            int minus = r.nextInt(10);
            s.rollbackTo(Math.max(0, s.getCurrentVersion() - minus));
            MVMap<?, ?> undo = s.openMap("undoLog");
            if (undo.size() > 0) {
                break;
            }
        }
        // re-open the store, because we have opened
        // the undoLog map with the wrong data type
        s.close();
        s = MVStore.open(fileName);
        ts = new TransactionStore(s);
        List<Transaction> list = ts.getOpenTransactions();
        if (list.size() != 0) {
            tx = list.get(0);
            if (tx.getStatus() == Transaction.STATUS_COMMITTING) {
                i++;
            }
        }
        s.close();
        FileUtils.delete(fileName);
        assertFalse(FileUtils.exists(fileName));
    }
}
Also used : Task(org.h2.util.Task) MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionStore(org.h2.mvstore.db.TransactionStore) Random(java.util.Random) Transaction(org.h2.mvstore.db.TransactionStore.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 22 with Task

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

the class TestConcurrentUpdate method test.

@Override
public void test() throws Exception {
    deleteDb("concurrent");
    final String url = getURL("concurrent;MULTI_THREADED=TRUE", true);
    Connection conn = getConnection(url);
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name varchar)");
    Task[] tasks = new Task[THREADS];
    for (int i = 0; i < THREADS; i++) {
        final int threadId = i;
        Task t = new Task() {

            @Override
            public void call() throws Exception {
                Random r = new Random(threadId);
                Connection conn = getConnection(url);
                PreparedStatement insert = conn.prepareStatement("insert into test values(?, ?)");
                PreparedStatement update = conn.prepareStatement("update test set name = ? where id = ?");
                PreparedStatement delete = conn.prepareStatement("delete from test where id = ?");
                PreparedStatement select = conn.prepareStatement("select * from test where id = ?");
                while (!stop) {
                    try {
                        int x = r.nextInt(ROW_COUNT);
                        String data = "x" + r.nextInt(ROW_COUNT);
                        switch(r.nextInt(3)) {
                            case 0:
                                insert.setInt(1, x);
                                insert.setString(2, data);
                                insert.execute();
                                break;
                            case 1:
                                update.setString(1, data);
                                update.setInt(2, x);
                                update.execute();
                                break;
                            case 2:
                                delete.setInt(1, x);
                                delete.execute();
                                break;
                            case 4:
                                select.setInt(1, x);
                                ResultSet rs = select.executeQuery();
                                while (rs.next()) {
                                    rs.getString(2);
                                }
                                break;
                        }
                    } catch (SQLException e) {
                        handleException(e);
                    }
                }
                conn.close();
            }
        };
        tasks[i] = t;
        t.execute();
    }
    // test 2 seconds
    for (int i = 0; i < 200; i++) {
        Thread.sleep(10);
        for (Task t : tasks) {
            if (t.isFinished()) {
                i = 1000;
                break;
            }
        }
    }
    for (Task t : tasks) {
        t.get();
    }
    conn.close();
}
Also used : Task(org.h2.util.Task) Random(java.util.Random) SQLException(java.sql.SQLException) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 23 with Task

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

the class TestConcurrentLinkedList method testPerformance.

private static void testPerformance(final boolean stock) {
    System.out.print(stock ? "stock " : "custom ");
    long start = System.nanoTime();
    // final ConcurrentLinkedList<Integer> test =
    // new ConcurrentLinkedList<Integer>();
    final ConcurrentArrayList<Integer> test = new ConcurrentArrayList<>();
    final LinkedList<Integer> x = new LinkedList<>();
    final AtomicInteger counter = new AtomicInteger();
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                if (stock) {
                    synchronized (x) {
                        Integer y = x.peekFirst();
                        if (y == null) {
                            counter.incrementAndGet();
                        }
                    }
                } else {
                    Integer y = test.peekFirst();
                    if (y == null) {
                        counter.incrementAndGet();
                    }
                }
            }
        }
    };
    task.execute();
    test.add(-1);
    x.add(-1);
    for (int i = 0; i < 2000000; i++) {
        Integer value = Integer.valueOf(i & 63);
        if (stock) {
            synchronized (x) {
                Integer f = x.peekLast();
                if (f != value) {
                    x.add(i);
                }
            }
            Math.sin(i);
            synchronized (x) {
                if (x.peekFirst() != x.peekLast()) {
                    x.removeFirst();
                }
            }
        } else {
            Integer f = test.peekLast();
            if (f != value) {
                test.add(i);
            }
            Math.sin(i);
            f = test.peekFirst();
            if (f != test.peekLast()) {
                test.removeFirst(f);
            }
        }
    }
    task.get();
    System.out.println(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task) ConcurrentArrayList(org.h2.mvstore.ConcurrentArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LinkedList(java.util.LinkedList)

Example 24 with Task

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

the class TestConcurrentLinkedList method testConcurrent.

private void testConcurrent() {
    final ConcurrentArrayList<Integer> test = new ConcurrentArrayList<>();
    // final ConcurrentRing<Integer> test = new ConcurrentRing<Integer>();
    final AtomicInteger counter = new AtomicInteger();
    final AtomicInteger size = new AtomicInteger();
    Task task = new Task() {

        @Override
        public void call() {
            while (!stop) {
                Thread.yield();
                if (size.get() < 10) {
                    test.add(counter.getAndIncrement());
                    size.getAndIncrement();
                }
            }
        }
    };
    task.execute();
    for (int i = 0; i < 1000000; ) {
        Thread.yield();
        Integer x = test.peekFirst();
        if (x == null) {
            continue;
        }
        assertEquals(i, x.intValue());
        if (test.removeFirst(x)) {
            size.getAndDecrement();
            i++;
        }
    }
    task.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task) ConcurrentArrayList(org.h2.mvstore.ConcurrentArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 25 with Task

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

the class FtpServer method startTask.

/**
 * Start a task.
 *
 * @param path the name of the task file
 */
void startTask(String path) throws IOException {
    stopTask(path);
    if (path.endsWith(".zip.task")) {
        trace("expand: " + path);
        Process p = Runtime.getRuntime().exec("jar -xf " + path, null, new File(root));
        new StreamRedirect(path, p.getInputStream(), null).start();
        return;
    }
    Properties prop = SortedProperties.loadProperties(path);
    String command = prop.getProperty("command");
    String outFile = path.substring(0, path.length() - TASK_SUFFIX.length());
    String errorFile = root + "/" + prop.getProperty("error", outFile + ".err.txt");
    String outputFile = root + "/" + prop.getProperty("output", outFile + ".out.txt");
    trace("start process: " + path + " / " + command);
    Process p = Runtime.getRuntime().exec(command, null, new File(root));
    new StreamRedirect(path, p.getErrorStream(), errorFile).start();
    new StreamRedirect(path, p.getInputStream(), outputFile).start();
    tasks.put(path, p);
}
Also used : SortedProperties(org.h2.util.SortedProperties) Properties(java.util.Properties) File(java.io.File)

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