Search in sources :

Example 11 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project ignite by apache.

the class IgniteCacheClientQueryReplicatedNodeRestartSelfTest method testRestarts.

/**
 * @throws Exception If failed.
 */
public void testRestarts() throws Exception {
    fail("https://issues.apache.org/jira/browse/IGNITE-7946");
    int duration = 90 * 1000;
    int qryThreadNum = 5;
    // 2 of 4 data nodes
    int restartThreadsNum = 2;
    final int nodeLifeTime = 2 * 1000;
    final int logFreq = 10;
    startGridsMultiThreaded(GRID_CNT);
    // The last is client only.
    final AtomicIntegerArray locks = new AtomicIntegerArray(GRID_CNT - 1);
    fillCaches();
    final List<List<?>> pRes = grid(0).cache("pu").query(new SqlFieldsQuery(QRY)).getAll();
    Thread.sleep(3000);
    assertEquals(pRes, grid(0).cache("pu").query(new SqlFieldsQuery(QRY)).getAll());
    assertFalse(pRes.isEmpty());
    final AtomicInteger qryCnt = new AtomicInteger();
    final AtomicBoolean qrysDone = new AtomicBoolean();
    final List<Integer> cacheSize = new ArrayList<>(4);
    for (int i = 0; i < GRID_CNT - 1; i++) {
        int j = 0;
        for (String cacheName : F.asList("co", "pr", "pe", "pu")) {
            IgniteCache<?, ?> cache = grid(i).cache(cacheName);
            assertClient(cache, false);
            if (i == 0)
                cacheSize.add(cache.size());
            else
                assertEquals(cacheSize.get(j++).intValue(), cache.size());
        }
    }
    int j = 0;
    for (String cacheName : F.asList("co", "pr", "pe", "pu")) {
        IgniteCache<?, ?> cache = grid(GRID_CNT - 1).cache(cacheName);
        assertClient(cache, true);
        assertEquals(cacheSize.get(j++).intValue(), cache.size());
    }
    final IgniteCache<?, ?> clientCache = grid(GRID_CNT - 1).cache("pu");
    IgniteInternalFuture<?> fut1 = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            GridRandom rnd = new GridRandom();
            while (!qrysDone.get()) {
                SqlFieldsQuery qry = new SqlFieldsQuery(QRY);
                boolean smallPageSize = rnd.nextBoolean();
                if (smallPageSize)
                    qry.setPageSize(3);
                List<List<?>> res;
                try {
                    res = clientCache.query(qry).getAll();
                } catch (CacheException e) {
                    assertTrue("On large page size must retry.", smallPageSize);
                    boolean failedOnRemoteFetch = false;
                    for (Throwable th = e; th != null; th = th.getCause()) {
                        if (!(th instanceof CacheException))
                            continue;
                        if (th.getMessage() != null && th.getMessage().startsWith("Failed to fetch data from node:")) {
                            failedOnRemoteFetch = true;
                            break;
                        }
                    }
                    if (!failedOnRemoteFetch) {
                        e.printStackTrace();
                        fail("Must fail inside of GridResultPage.fetchNextPage or subclass.");
                    }
                    res = FAKE;
                }
                if (res != FAKE && !res.equals(pRes)) {
                    int j = 0;
                    // Check for data loss.
                    for (String cacheName : F.asList("co", "pr", "pe", "pu")) {
                        assertEquals(cacheName, cacheSize.get(j++).intValue(), grid(GRID_CNT - 1).cache(cacheName).size());
                    }
                    // Fail with nice message.
                    assertEquals(pRes, res);
                }
                int c = qryCnt.incrementAndGet();
                if (c % logFreq == 0)
                    info("Executed queries: " + c);
            }
        }
    }, qryThreadNum, "query-thread");
    final AtomicInteger restartCnt = new AtomicInteger();
    final AtomicBoolean restartsDone = new AtomicBoolean();
    IgniteInternalFuture<?> fut2 = multithreadedAsync(new Callable<Object>() {

        @SuppressWarnings({ "BusyWait" })
        @Override
        public Object call() throws Exception {
            GridRandom rnd = new GridRandom();
            while (!restartsDone.get()) {
                int g;
                do {
                    g = rnd.nextInt(locks.length());
                } while (!locks.compareAndSet(g, 0, -1));
                log.info("Stop node: " + g);
                stopGrid(g);
                Thread.sleep(rnd.nextInt(nodeLifeTime));
                log.info("Start node: " + g);
                startGrid(g);
                Thread.sleep(rnd.nextInt(nodeLifeTime));
                locks.set(g, 0);
                int c = restartCnt.incrementAndGet();
                if (c % logFreq == 0)
                    info("Node restarts: " + c);
            }
            return true;
        }
    }, restartThreadsNum, "restart-thread");
    Thread.sleep(duration);
    info("Stopping..");
    restartsDone.set(true);
    fut2.get();
    info("Restarts stopped.");
    qrysDone.set(true);
    fut1.get();
    info("Queries stopped.");
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) CacheException(javax.cache.CacheException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) CAX(org.apache.ignite.internal.util.typedef.CAX)

Example 12 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_browser_Browser method test_evaluate_array_numbers.

/**
 * Test the evaluate() api that returns an array of numbers. Functionality based on Snippet308.
 * Only wait till success. Otherwise timeout after 3 seconds.
 */
@Test
public void test_evaluate_array_numbers() {
    // Bug 509411
    assumeFalse(webkit1SkipMsg(), isWebkit1);
    // Small note:
    // evaluate() returns 'Double' type. Java doesn't have AtomicDouble
    // for convienience we simply convert double to int as we're dealing with integers anyway.
    final AtomicIntegerArray atomicIntArray = new AtomicIntegerArray(3);
    atomicIntArray.set(0, -1);
    browser.addProgressListener(completedAdapter(event -> {
        Object[] evalResult = (Object[]) browser.evaluate("return new Array(1,2,3)");
        atomicIntArray.set(0, ((Double) evalResult[0]).intValue());
        atomicIntArray.set(1, ((Double) evalResult[1]).intValue());
        atomicIntArray.set(2, ((Double) evalResult[2]).intValue());
        if (debug_verbose_output)
            System.out.println("Node value: " + evalResult);
    }));
    browser.setText("<html><body><p id='myid'>HelloWorld</p></body></html>");
    shell.open();
    AtomicReference<String> additionalErrorInfo = new AtomicReference<>("");
    boolean passed = waitForPassCondition(() -> {
        if (atomicIntArray.get(0) != -1) {
            if (atomicIntArray.get(0) == 1 && atomicIntArray.get(1) == 2 && atomicIntArray.get(2) == 3) {
                return true;
            } else {
                additionalErrorInfo.set("Resulting numbers in the array are not as expected");
            }
        }
        return false;
    });
    String message = "".equals(additionalErrorInfo.get()) ? "Javascript did not call java" : "Javasscript called java, but passed wrong values: " + additionalErrorInfo.get();
    assertTrue(message, passed);
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) HttpURLConnection(java.net.HttpURLConnection) StatusTextListener(org.eclipse.swt.browser.StatusTextListener) URL(java.net.URL) Assume.assumeFalse(org.junit.Assume.assumeFalse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LocationAdapter(org.eclipse.swt.browser.LocationAdapter) CloseWindowListener(org.eclipse.swt.browser.CloseWindowListener) ProgressListener(org.eclipse.swt.browser.ProgressListener) TitleListener(org.eclipse.swt.browser.TitleListener) VisibilityWindowAdapter(org.eclipse.swt.browser.VisibilityWindowAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Point(org.eclipse.swt.graphics.Point) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) BrowserFunction(org.eclipse.swt.browser.BrowserFunction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) TestName(org.junit.rules.TestName) LocationListener.changedAdapter(org.eclipse.swt.browser.LocationListener.changedAdapter) VisibilityWindowListener.showAdapter(org.eclipse.swt.browser.VisibilityWindowListener.showAdapter) WindowEvent(org.eclipse.swt.browser.WindowEvent) LocationListener(org.eclipse.swt.browser.LocationListener) ProgressListener.completedAdapter(org.eclipse.swt.browser.ProgressListener.completedAdapter) Assert.fail(org.junit.Assert.fail) SWTException(org.eclipse.swt.SWTException) VisibilityWindowListener(org.eclipse.swt.browser.VisibilityWindowListener) FillLayout(org.eclipse.swt.layout.FillLayout) AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) Before(org.junit.Before) OpenWindowListener(org.eclipse.swt.browser.OpenWindowListener) ProgressAdapter(org.eclipse.swt.browser.ProgressAdapter) Browser(org.eclipse.swt.browser.Browser) Shell(org.eclipse.swt.widgets.Shell) MalformedURLException(java.net.MalformedURLException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Display(org.eclipse.swt.widgets.Display) Instant(java.time.Instant) LocationListener.changingAdapter(org.eclipse.swt.browser.LocationListener.changingAdapter) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) Assert.assertFalse(org.junit.Assert.assertFalse) SWT(org.eclipse.swt.SWT) LocationEvent(org.eclipse.swt.browser.LocationEvent) Assume.assumeTrue(org.junit.Assume.assumeTrue) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 13 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project athenz by yahoo.

the class ZpeMetric method increment.

// to increment a metric counter by 1
public void increment(String metricName, String domainName) {
    if (statsEnabled) {
        if (!counter.containsKey(domainName)) {
            counter.putIfAbsent(domainName, new AtomicIntegerArray(DomainMetricType.LOAD_DOMAIN_GOOD.ordinal() + 1));
        }
        Integer index = com.yahoo.athenz.zts.DomainMetricType.valueOf(metricName).ordinal();
        counter.get(domainName).incrementAndGet(index);
    }
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray)

Aggregations

AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 List (java.util.List)3 CacheException (javax.cache.CacheException)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)3 GridRandom (org.apache.ignite.internal.util.GridRandom)3 CAX (org.apache.ignite.internal.util.typedef.CAX)3 ArrayList (java.util.ArrayList)2 AtomicLongArray (java.util.concurrent.atomic.AtomicLongArray)2 IgniteCache (org.apache.ignite.IgniteCache)2 IgniteFutureTimeoutCheckedException (org.apache.ignite.internal.IgniteFutureTimeoutCheckedException)2 JSONArray (com.alibaba.fastjson.JSONArray)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Instant (java.time.Instant)1 LinkedList (java.util.LinkedList)1