Search in sources :

Example 56 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project pulsar by yahoo.

the class ManagedCursorConcurrencyTest method testConcurrentIndividualDeletes.

@Test(timeOut = 30000)
public void testConcurrentIndividualDeletes() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(100));
    final ManagedCursor cursor = ledger.openCursor("c1");
    final int N = 1000;
    final List<Position> addedEntries = Lists.newArrayListWithExpectedSize(N);
    for (int i = 0; i < N; i++) {
        Position pos = ledger.addEntry("entry".getBytes());
        addedEntries.add(pos);
    }
    final int Threads = 10;
    final CyclicBarrier barrier = new CyclicBarrier(Threads);
    final CountDownLatch counter = new CountDownLatch(Threads);
    final AtomicBoolean gotException = new AtomicBoolean(false);
    for (int thread = 0; thread < Threads; thread++) {
        final int myThread = thread;
        cachedExecutor.execute(() -> {
            try {
                barrier.await();
                for (int i = 0; i < N; i++) {
                    int threadId = i % Threads;
                    if (threadId == myThread) {
                        cursor.delete(addedEntries.get(i));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        });
    }
    counter.await();
    assertEquals(gotException.get(), false);
    assertEquals(cursor.getMarkDeletedPosition(), addedEntries.get(addedEntries.size() - 1));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Example 57 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project pulsar by yahoo.

the class ManagedCursorConcurrencyTest method testCloseAndRead.

@Test
public void testCloseAndRead() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger_test_close_and_read", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    final ManagedCursor cursor = ledger.openCursor("c1");
    final CompletableFuture<String> closeFuture = new CompletableFuture<>();
    final String CLOSED = "closed";
    final List<Position> addedEntries = Lists.newArrayList();
    for (int i = 0; i < 1000; i++) {
        Position pos = ledger.addEntry("entry".getBytes());
        addedEntries.add(pos);
    }
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch counter = new CountDownLatch(2);
    final AtomicBoolean gotException = new AtomicBoolean(false);
    Thread deleter = new Thread() {

        public void run() {
            try {
                barrier.await();
                for (Position position : addedEntries) {
                    cursor.markDelete(position);
                    Thread.sleep(1);
                }
            } catch (ManagedLedgerException e) {
                if (!e.getMessage().equals("Cursor was already closed")) {
                    gotException.set(true);
                }
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    Thread reader = new Thread() {

        public void run() {
            try {
                barrier.await();
                for (int i = 0; i < 1000; i++) {
                    cursor.readEntries(1).forEach(e -> e.release());
                    // Thread.sleep(2,200);
                    Thread.sleep(2, 195);
                }
                cursor.asyncClose(new AsyncCallbacks.CloseCallback() {

                    @Override
                    public void closeComplete(Object ctx) {
                        log.info("Successfully closed cursor ledger");
                        closeFuture.complete(CLOSED);
                    }

                    @Override
                    public void closeFailed(ManagedLedgerException exception, Object ctx) {
                        log.error("Error closing cursor: ", exception);
                        closeFuture.completeExceptionally(new Exception(exception));
                    }
                }, null);
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    deleter.start();
    reader.start();
    counter.await();
    assertEquals(gotException.get(), false);
    assertEquals(closeFuture.get(), CLOSED);
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 58 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project voltdb by VoltDB.

the class VoltClient4 method scan.

@Override
public Status scan(String keyspace, String lowerBound, int recordCount, Set<String> columns, Vector<HashMap<String, ByteIterator>> result) {
    try {
        byte[] ks = keyspace.getBytes(UTF8);
        ClientResponse response = m_client.callProcedure("Scan", ks, lowerBound, lowerBound.getBytes(UTF8), recordCount);
        if (response.getStatus() != ClientResponse.SUCCESS) {
            return Status.ERROR;
        }
        int nFound = 0;
        String partKey = lowerBound;
        CyclicBarrier barrier = new CyclicBarrier(2);
        result.ensureCapacity(recordCount);
        ScanCallback callback = null;
        boolean proceed = true;
        while (proceed) {
            if (response.getStatus() != ClientResponse.SUCCESS) {
                return Status.ERROR;
            }
            VoltTable table = response.getResults()[0];
            nFound += table.getRowCount();
            proceed = nFound < recordCount && (partKey = nextPartitionKey(partKey)) != null;
            if (proceed) {
                barrier.reset();
                callback = new ScanCallback(barrier);
                m_client.callProcedure(callback, "Scan", ks, partKey, null, recordCount - nFound);
            }
            while (table.advanceRow()) {
                result.add(unpackRowData(table, columns));
            }
            if (proceed) {
                barrier.await();
                response = callback.response;
            }
        }
        return Status.OK;
    } catch (Exception e) {
        e.printStackTrace();
        return Status.ERROR;
    }
}
Also used : ClientResponse(org.voltdb.client.ClientResponse) VoltTable(org.voltdb.VoltTable) DBException(com.yahoo.ycsb.DBException) IOException(java.io.IOException) ProcCallException(org.voltdb.client.ProcCallException) NoConnectionsException(org.voltdb.client.NoConnectionsException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 59 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project voltdb by VoltDB.

the class ProcedureCallMicrobench method main.

public static void main(String[] args) throws Exception {
    int siteCount = 1;
    TPCCProjectBuilder pb = new TPCCProjectBuilder();
    pb.addDefaultSchema();
    pb.addDefaultPartitioning();
    pb.addProcedures(EmptyProcedure.class, MultivariateEmptyProcedure.class);
    pb.compile("procedureCallMicrobench.jar", siteCount, 0);
    ServerThread server = new ServerThread("procedureCallMicrobench.jar", BackendTarget.NATIVE_EE_JNI);
    server.start();
    server.waitForInitialization();
    int[] clientCounts = new int[] {};
    if (args.length >= 1 && !args[0].equals("${clients}")) {
        String[] clientCountString = args[0].split("\\s+");
        clientCounts = new int[clientCountString.length];
        for (int i = 0; i < clientCountString.length; i++) {
            clientCounts[i] = Integer.parseInt(clientCountString[i]);
        }
    }
    for (int clientCount : clientCounts) {
        for (int varmode = 0; varmode < 2; varmode++) {
            final Date date = new Date();
            final String name = varmode == 0 ? "EmptyProcedure" : "MultivariateEmptyProcedure";
            final Runner runner = varmode == 0 ? new Runner() {

                @Override
                public void run(Client client) throws Exception {
                    client.callProcedure(name, 0L);
                }
            } : new Runner() {

                @Override
                public void run(Client client) throws Exception {
                    client.callProcedure(name, 0L, 0L, 0L, "String c_first", "String c_middle", "String c_last", "String c_street_1", "String c_street_2", "String d_city", "String d_state", "String d_zip", "String c_phone", date, "String c_credit", 0.0, 0.0, 0.0, 0.0, 0L, 0L, "String c_data");
                }
            };
            // trigger classloading a couple times
            {
                ClientConfig config = new ClientConfig("program", "none");
                Client client = ClientFactory.createClient(config);
                client.createConnection("localhost");
                for (int i = 0; i < 10000; i++) client.callProcedure("EmptyProcedure", 0L);
            }
            ExecutorService executor = Executors.newFixedThreadPool(clientCount);
            ArrayList<Future<Integer>> futures = new ArrayList<Future<Integer>>(clientCount);
            final CyclicBarrier barrier = new CyclicBarrier(clientCount + 1);
            final long stopTime = System.currentTimeMillis() + 2000;
            for (int i = 0; i < clientCount; i++) {
                futures.add(executor.submit(new Callable<Integer>() {

                    public Integer call() {
                        try {
                            ClientConfig config = new ClientConfig("program", "none");
                            Client client = ClientFactory.createClient(config);
                            client.createConnection("localhost");
                            int count = 0;
                            barrier.await();
                            for (count = 0; count % 10 != 0 || System.currentTimeMillis() < stopTime; count++) {
                                runner.run(client);
                            }
                            return count;
                        } catch (Exception ex) {
                            ex.printStackTrace();
                            throw new RuntimeException(ex);
                        }
                    }
                }));
            }
            barrier.await();
            final long startTime = System.currentTimeMillis();
            int count = 0;
            for (Future<Integer> future : futures) {
                count += future.get();
            }
            double time = stopTime - startTime;
            System.out.println(name + " with " + clientCount + " clients: " + count + " xacts in " + time + " ms => " + (time / count) + " ms/xact => " + (count / time) * 1000 + "tps");
        }
    }
    System.exit(0);
}
Also used : ArrayList(java.util.ArrayList) Date(java.util.Date) Callable(java.util.concurrent.Callable) CyclicBarrier(java.util.concurrent.CyclicBarrier) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Client(org.voltdb.client.Client) ClientConfig(org.voltdb.client.ClientConfig) TPCCProjectBuilder(org.voltdb.benchmark.tpcc.TPCCProjectBuilder)

Example 60 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project voltdb by VoltDB.

the class SnapshotSiteProcessor method readySnapshotSetupBarriers.

public static void readySnapshotSetupBarriers(int numSites) {
    synchronized (SnapshotSiteProcessor.m_snapshotCreateLock) {
        if (SnapshotSiteProcessor.m_snapshotCreateSetupBarrier == null) {
            SnapshotSiteProcessor.m_snapshotCreateFinishBarrier = new CyclicBarrier(numSites);
            SnapshotSiteProcessor.m_snapshotCreateSetupBarrier = new CyclicBarrier(numSites, SnapshotSiteProcessor.m_snapshotCreateSetupBarrierAction);
        } else if (SnapshotSiteProcessor.m_snapshotCreateSetupBarrier.isBroken()) {
            SnapshotSiteProcessor.m_snapshotCreateSetupBarrier.reset();
            SnapshotSiteProcessor.m_snapshotCreateFinishBarrier.reset();
        }
    }
}
Also used : CyclicBarrier(java.util.concurrent.CyclicBarrier)

Aggregations

CyclicBarrier (java.util.concurrent.CyclicBarrier)650 Test (org.junit.Test)315 CountDownLatch (java.util.concurrent.CountDownLatch)169 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)142 ArrayList (java.util.ArrayList)126 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)124 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)121 IOException (java.io.IOException)97 ExecutorService (java.util.concurrent.ExecutorService)84 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)67 AtomicReference (java.util.concurrent.atomic.AtomicReference)66 Ignite (org.apache.ignite.Ignite)64 List (java.util.List)53 Test (org.testng.annotations.Test)52 TimeoutException (java.util.concurrent.TimeoutException)48 Transaction (org.apache.ignite.transactions.Transaction)48 IgniteException (org.apache.ignite.IgniteException)47 ExecutionException (java.util.concurrent.ExecutionException)40 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)40 IgniteCache (org.apache.ignite.IgniteCache)37