Search in sources :

Example 21 with BrokenBarrierException

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

the class UpdateLogging method run.

/**
     * Change the operational log configuration.
     * @param ctx       Internal parameter. Not user-accessible.
     * @param xmlConfig New configuration XML document.
     * @return          Standard STATUS table.
     */
@SuppressWarnings("deprecation")
public VoltTable[] run(SystemProcedureExecutionContext ctx, String username, String remoteHost, String xmlConfig) {
    long oldLevels = 0;
    if (ctx.isLowestSiteId()) {
        // Logger level is a global property, pick the site with lowest id to do it.
        hostLog.info(String.format("%s from %s changed the log4j settings", username, remoteHost));
        hostLog.info(xmlConfig);
        oldLevels = hostLog.getLogLevels(loggers);
    }
    try {
        // Mimic the multi-fragment semantics as scatter-gather pattern is an overkill for this simple task.
        // There are chances that some sites being interrupted and update the logging before old logger level
        // being read, but the reasons we don't care because 1) it is rare and 2) it only effects when HOST
        // logger being changed from higher than INFO level to INFO or lower level.
        barrier.await();
    } catch (InterruptedException | BrokenBarrierException dontcare) {
    }
    VoltDB.instance().logUpdate(xmlConfig, DeprecatedProcedureAPIAccess.getVoltPrivateRealTransactionId(this), ctx.getPaths().getVoltDBRoot());
    ctx.updateBackendLogLevels();
    if (ctx.isLowestSiteId()) {
        long newLevels = hostLog.getLogLevels(loggers);
        if (newLevels != oldLevels) {
            // If HOST logger wasn't able to log before and now it can, logs the setting change event.
            int index = (int) ((oldLevels >> 3) & 7);
            Level before = Level.values()[index];
            index = (int) ((newLevels >> 3) & 7);
            Level after = Level.values()[index];
            if (before.ordinal() > Level.INFO.ordinal() && after.ordinal() <= Level.INFO.ordinal()) {
                hostLog.info(String.format("%s from %s changed the log4j settings", username, remoteHost));
                hostLog.info(xmlConfig);
            }
        }
        barrier.reset();
    }
    VoltTable t = new VoltTable(VoltSystemProcedure.STATUS_SCHEMA);
    t.addRow(VoltSystemProcedure.STATUS_OK);
    return (new VoltTable[] { t });
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Level(org.voltcore.logging.Level) VoltTable(org.voltdb.VoltTable)

Example 22 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project geode by apache.

the class AtomicStatsJUnitTest method testConcurrentGets.

/**
   * Test for bug 41340. Do two gets at the same time of a dirty stat, and make sure we get the
   * correct value for the stat.
   * 
   * @throws Throwable
   */
@Test
public void testConcurrentGets() throws Throwable {
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    // props.setProperty("statistic-sample-rate", "60000");
    props.setProperty(STATISTIC_SAMPLING_ENABLED, "false");
    DistributedSystem ds = DistributedSystem.connect(props);
    String statName = "TestStats";
    String statDescription = "Tests stats";
    final String statDesc = "blah blah blah";
    StatisticsTypeFactory f = StatisticsTypeFactoryImpl.singleton();
    StatisticsType type = f.createType(statName, statDescription, new StatisticDescriptor[] { f.createIntGauge("stat", statDesc, "bottles of beer on the wall") });
    final int statId = type.nameToId("stat");
    try {
        final AtomicReference<Statistics> statsRef = new AtomicReference<Statistics>();
        final CyclicBarrier beforeIncrement = new CyclicBarrier(3);
        final CyclicBarrier afterIncrement = new CyclicBarrier(3);
        Thread thread1 = new Thread("thread1") {

            public void run() {
                try {
                    while (true) {
                        beforeIncrement.await();
                        statsRef.get().incInt(statId, 1);
                        afterIncrement.await();
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        Thread thread3 = new Thread("thread1") {

            public void run() {
                try {
                    while (true) {
                        beforeIncrement.await();
                        afterIncrement.await();
                        statsRef.get().getInt(statId);
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        thread1.start();
        thread3.start();
        for (int i = 0; i < 5000; i++) {
            Statistics stats = ds.createAtomicStatistics(type, "stats");
            statsRef.set(stats);
            beforeIncrement.await();
            afterIncrement.await();
            assertEquals("On loop " + i, 1, stats.getInt(statId));
            stats.close();
        }
    } finally {
        ds.disconnect();
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) StatisticsType(org.apache.geode.StatisticsType) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) DistributedSystem(org.apache.geode.distributed.DistributedSystem) Statistics(org.apache.geode.Statistics) CyclicBarrier(java.util.concurrent.CyclicBarrier) StatisticsTypeFactory(org.apache.geode.StatisticsTypeFactory) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 23 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project ignite by apache.

the class GridCacheMultithreadedFailoverAbstractTest method test.

/**
     * Actual test.
     *
     * @throws Exception If failed.
     */
public void test() throws Exception {
    startUp();
    final CyclicBarrier startBarrier = new CyclicBarrier(putThreads());
    final Map<Integer, Integer> expVals = new ConcurrentHashMap<>();
    final int keysPerThread = keyRange() / putThreads();
    final AtomicLong ctr = new AtomicLong();
    final AtomicLong errCtr = new AtomicLong();
    final AtomicBoolean stop = new AtomicBoolean();
    assert keysPerThread > 0;
    Thread[] putThreads = new Thread[putThreads()];
    for (int i = 0; i < putThreads(); i++) {
        final int idx = i;
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    startBarrier.await();
                } catch (InterruptedException | BrokenBarrierException ignore) {
                    return;
                }
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                Ignite ignite = G.ignite(nodeName(0));
                IgniteCache<Integer, Integer> cache = ignite.cache(CACHE_NAME);
                int startKey = keysPerThread * idx;
                int endKey = keysPerThread * (idx + 1);
                Map<Integer, Integer> putMap = new HashMap<>();
                Set<Integer> rmvSet = new HashSet<>();
                while (!stop.get()) {
                    for (int i = 0; i < 100; i++) {
                        int key = rnd.nextInt(startKey, endKey);
                        if (rnd.nextInt(0, 10) > 0) {
                            putMap.put(key, i);
                            rmvSet.remove(key);
                        } else {
                            rmvSet.add(key);
                            putMap.remove(key);
                        }
                    }
                    try {
                        Transaction tx = atomicityMode() == TRANSACTIONAL ? ignite.transactions().txStart() : null;
                        try {
                            cache.putAll(putMap);
                            cache.removeAll(rmvSet);
                            if (tx != null)
                                tx.commit();
                        } finally {
                            if (tx != null)
                                tx.close();
                        }
                        expVals.putAll(putMap);
                        for (Integer key : rmvSet) expVals.remove(key);
                    } catch (Exception e) {
                        log.error("Cache update failed [putMap=" + putMap + ", rmvSet=" + rmvSet + ']', e);
                        errCtr.incrementAndGet();
                    }
                    ctr.addAndGet(putMap.size() + rmvSet.size());
                    try {
                        if (cmp) {
                            cmpLatch.countDown();
                            lock.lock();
                            try {
                                while (cmp) putCond.await();
                            } finally {
                                lock.unlock();
                            }
                        }
                    } catch (InterruptedException ignore) {
                        return;
                    }
                }
            }
        });
        thread.setName("put-thread-" + i);
        thread.start();
        putThreads[i] = thread;
    }
    IgniteInternalFuture<?> killNodeFut = null;
    if (nodeKillProbability() > 0) {
        killNodeFut = GridTestUtils.runAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                while (!stop.get()) {
                    U.sleep(ThreadLocalRandom.current().nextLong(killDelay().get1(), killDelay().get2()));
                    killLock.lock();
                    try {
                        int idx = ThreadLocalRandom.current().nextInt(1, dataNodes());
                        String igniteInstanceName = nodeName(idx);
                        if (stop.get())
                            return null;
                        log.info("Killing node [igniteInstanceName=" + igniteInstanceName + ']');
                        stopGrid(igniteInstanceName);
                        U.sleep(ThreadLocalRandom.current().nextLong(restartDelay().get1(), restartDelay().get2()));
                        if (stop.get())
                            return null;
                        log.info("Restarting node [igniteInstanceName=" + igniteInstanceName + ']');
                        G.start(configuration(idx));
                    } finally {
                        killLock.unlock();
                    }
                }
                return null;
            }
        });
    }
    boolean failed = false;
    try {
        long stopTime = U.currentTimeMillis() + duration();
        long nextCmp = U.currentTimeMillis() + cacheComparisonFrequency();
        while (!failed && U.currentTimeMillis() < stopTime) {
            long start = System.nanoTime();
            long ops = ctr.longValue();
            U.sleep(1000);
            long diff = ctr.longValue() - ops;
            double time = (System.nanoTime() - start) / 1_000_000_000d;
            long opsPerSecond = (long) (diff / time);
            log.info("Operations/second: " + opsPerSecond);
            if (U.currentTimeMillis() >= nextCmp) {
                failed = !compare(expVals);
                nextCmp = System.currentTimeMillis() + cacheComparisonFrequency();
            }
        }
    } finally {
        stop.set(true);
    }
    if (killNodeFut != null)
        killNodeFut.get();
    for (Thread thread : putThreads) U.join(thread);
    log.info("Test finished. Put errors: " + errCtr.get());
    assertFalse("Test failed", failed);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Callable(java.util.concurrent.Callable) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IgniteCache(org.apache.ignite.IgniteCache) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) Transaction(org.apache.ignite.transactions.Transaction) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 24 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project ignite by apache.

the class GridTreeBenchmark method doTestAtomicInt.

/**
     * @throws BrokenBarrierException If failed.
     * @throws InterruptedException If failed.
     */
private static void doTestAtomicInt() throws BrokenBarrierException, InterruptedException {
    final AtomicInteger[] cnts = new AtomicInteger[8];
    for (int i = 0; i < cnts.length; i++) cnts[i] = new AtomicInteger();
    final Thread[] ths = new Thread[THREADS];
    final CyclicBarrier barrier = new CyclicBarrier(THREADS + 1);
    final AtomicInteger cnt = new AtomicInteger();
    for (int i = 0; i < ths.length; i++) {
        ths[i] = new Thread(new Runnable() {

            @Override
            public void run() {
                int idx = cnt.getAndIncrement();
                AtomicInteger x = cnts[idx % cnts.length];
                try {
                    barrier.await();
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
                for (int i = 0; i < ITERATIONS; i++) x.incrementAndGet();
            }
        });
        ths[i].start();
    }
    barrier.await();
    long start = System.currentTimeMillis();
    for (Thread t : ths) t.join();
    long time = System.currentTimeMillis() - start;
    System.out.println(time);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 25 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project ignite by apache.

the class IgniteCacheOffheapEvictQueryTest method testEvictAndRemove.

/**
     * @throws Exception If failed.
     */
public void testEvictAndRemove() throws Exception {
    final int KEYS_CNT = 3000;
    final int THREADS_CNT = 250;
    final IgniteCache<Integer, Integer> c = startGrid().cache(DEFAULT_CACHE_NAME);
    for (int i = 0; i < KEYS_CNT; i++) {
        c.put(i, i);
        if ((i & 1) == 0)
            c.localEvict(F.asList(i));
    }
    X.println("___ Cache loaded...");
    final CyclicBarrier b = new CyclicBarrier(THREADS_CNT, new Runnable() {

        @Override
        public void run() {
            X.println("___ go!");
        }
    });
    final AtomicInteger keys = new AtomicInteger(KEYS_CNT);
    IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {

        @Override
        public void run() {
            Random rnd = new GridRandom();
            try {
                b.await();
            } catch (InterruptedException e) {
                throw new IgniteInterruptedException(e);
            } catch (BrokenBarrierException e) {
                throw new IllegalStateException(e);
            }
            while (keys.get() > 0) {
                int k = rnd.nextInt(KEYS_CNT);
                try {
                    switch(rnd.nextInt(4)) {
                        case 0:
                            c.localEvict(F.asList(k));
                            break;
                        case 1:
                            c.get(k);
                            break;
                        case 2:
                            if (c.remove(k))
                                keys.decrementAndGet();
                            break;
                        case 3:
                            c.query(new SqlFieldsQuery("select _val from Integer where _key between ? and ?").setArgs(k, k + 20).setLocal(true)).getAll();
                            break;
                    }
                } catch (CacheException e) {
                    String msgStart = "Failed to get value for key:";
                    for (Throwable th = e; th != null; th = th.getCause()) {
                        String msg = th.getMessage();
                        if (msg != null && msg.startsWith(msgStart)) {
                            int dot = msg.indexOf('.', msgStart.length());
                            assertTrue(dot != -1);
                            final Integer failedKey = Integer.parseInt(msg.substring(msgStart.length(), dot).trim());
                            X.println("___ failed key: " + failedKey);
                            break;
                        }
                    }
                    LT.warn(log, e.getMessage());
                    return;
                }
            }
        }
    }, THREADS_CNT);
    try {
        fut.get(60_000);
        if (c.size(CachePeekMode.ALL) != 0)
            fail("Not all keys removed.");
        X.println("___ all keys removed");
    } catch (IgniteFutureTimeoutCheckedException ignored) {
        X.println("___ timeout");
        X.println("___ keys: " + keys.get());
        keys.set(0);
        fut.get();
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CacheException(javax.cache.CacheException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException)

Aggregations

BrokenBarrierException (java.util.concurrent.BrokenBarrierException)62 CyclicBarrier (java.util.concurrent.CyclicBarrier)53 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 IOException (java.io.IOException)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 CountDownLatch (java.util.concurrent.CountDownLatch)12 ExecutionException (java.util.concurrent.ExecutionException)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 List (java.util.List)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 TimeoutException (java.util.concurrent.TimeoutException)8 Random (java.util.Random)7 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)7 Set (java.util.Set)6 TimeUnit (java.util.concurrent.TimeUnit)6