Search in sources :

Example 36 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project elasticsearch by elastic.

the class InternalEngineTests method testConcurrentGetAndFlush.

public void testConcurrentGetAndFlush() throws Exception {
    ParsedDocument doc = testParsedDocument("1", "test", null, testDocumentWithTextField(), B_1, null);
    engine.index(indexForDoc(doc));
    final AtomicReference<Engine.GetResult> latestGetResult = new AtomicReference<>();
    latestGetResult.set(engine.get(new Engine.Get(true, newUid(doc))));
    final AtomicBoolean flushFinished = new AtomicBoolean(false);
    final CyclicBarrier barrier = new CyclicBarrier(2);
    Thread getThread = new Thread(() -> {
        try {
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            throw new RuntimeException(e);
        }
        while (flushFinished.get() == false) {
            Engine.GetResult previousGetResult = latestGetResult.get();
            if (previousGetResult != null) {
                previousGetResult.release();
            }
            latestGetResult.set(engine.get(new Engine.Get(true, newUid(doc))));
            if (latestGetResult.get().exists() == false) {
                break;
            }
        }
    });
    getThread.start();
    barrier.await();
    engine.flush();
    flushFinished.set(true);
    getThread.join();
    assertTrue(latestGetResult.get().exists());
    latestGetResult.get().release();
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument)

Example 37 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project elasticsearch by elastic.

the class IndexingMasterFailoverIT method testMasterFailoverDuringIndexingWithMappingChanges.

/**
     * Indexing operations which entail mapping changes require a blocking request to the master node to update the mapping.
     * If the master node is being disrupted or if it cannot commit cluster state changes, it needs to retry within timeout limits.
     * This retry logic is implemented in TransportMasterNodeAction and tested by the following master failover scenario.
     */
public void testMasterFailoverDuringIndexingWithMappingChanges() throws Throwable {
    logger.info("--> start 4 nodes, 3 master, 1 data");
    final Settings sharedSettings = Settings.builder().put(FaultDetection.PING_TIMEOUT_SETTING.getKey(), // for hitting simulated network failures quickly
    "1s").put(FaultDetection.PING_RETRIES_SETTING.getKey(), // for hitting simulated network failures quickly
    "1").put("discovery.zen.join_timeout", // still long to induce failures but to long so test won't time out
    "10s").put(DiscoverySettings.PUBLISH_TIMEOUT_SETTING.getKey(), // <-- for hitting simulated network failures quickly
    "1s").put(ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), 2).build();
    internalCluster().startMasterOnlyNodes(3, sharedSettings);
    String dataNode = internalCluster().startDataOnlyNode(sharedSettings);
    logger.info("--> wait for all nodes to join the cluster");
    ensureStableCluster(4);
    // We index data with mapping changes into cluster and have master failover at same time
    client().admin().indices().prepareCreate("myindex").setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)).get();
    ensureGreen("myindex");
    final CyclicBarrier barrier = new CyclicBarrier(2);
    Thread indexingThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                barrier.await();
            } catch (InterruptedException e) {
                logger.warn("Barrier interrupted", e);
                return;
            } catch (BrokenBarrierException e) {
                logger.warn("Broken barrier", e);
                return;
            }
            for (int i = 0; i < 10; i++) {
                // index data with mapping changes
                IndexResponse response = client(dataNode).prepareIndex("myindex", "mytype").setSource("field_" + i, "val").get();
                assertEquals(DocWriteResponse.Result.CREATED, response.getResult());
            }
        }
    });
    indexingThread.setName("indexingThread");
    indexingThread.start();
    barrier.await();
    // interrupt communication between master and other nodes in cluster
    String master = internalCluster().getMasterName();
    Set<String> otherNodes = new HashSet<>(Arrays.asList(internalCluster().getNodeNames()));
    otherNodes.remove(master);
    NetworkDisruption partition = new NetworkDisruption(new TwoPartitions(Collections.singleton(master), otherNodes), new NetworkDisconnect());
    internalCluster().setDisruptionScheme(partition);
    logger.info("--> disrupting network");
    partition.startDisrupting();
    logger.info("--> waiting for new master to be elected");
    ensureStableCluster(3, dataNode);
    partition.stopDisrupting();
    logger.info("--> waiting to heal");
    ensureStableCluster(4);
    indexingThread.join();
    ensureGreen("myindex");
    refresh();
    assertThat(client().prepareSearch("myindex").get().getHits().getTotalHits(), equalTo(10L));
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) TwoPartitions(org.elasticsearch.test.disruption.NetworkDisruption.TwoPartitions) NetworkDisconnect(org.elasticsearch.test.disruption.NetworkDisruption.NetworkDisconnect) CyclicBarrier(java.util.concurrent.CyclicBarrier) IndexResponse(org.elasticsearch.action.index.IndexResponse) NetworkDisruption(org.elasticsearch.test.disruption.NetworkDisruption) DiscoverySettings(org.elasticsearch.discovery.DiscoverySettings) Settings(org.elasticsearch.common.settings.Settings) HashSet(java.util.HashSet)

Example 38 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project elasticsearch by elastic.

the class CacheTests method testCachePollution.

public void testCachePollution() throws BrokenBarrierException, InterruptedException {
    int numberOfThreads = randomIntBetween(2, 32);
    final Cache<Integer, String> cache = CacheBuilder.<Integer, String>builder().build();
    CyclicBarrier barrier = new CyclicBarrier(1 + numberOfThreads);
    for (int i = 0; i < numberOfThreads; i++) {
        Thread thread = new Thread(() -> {
            try {
                barrier.await();
                Random random = new Random(random().nextLong());
                for (int j = 0; j < numberOfEntries; j++) {
                    Integer key = random.nextInt(numberOfEntries);
                    boolean first;
                    boolean second;
                    do {
                        first = random.nextBoolean();
                        second = random.nextBoolean();
                    } while (first && second);
                    if (first) {
                        try {
                            cache.computeIfAbsent(key, k -> {
                                if (random.nextBoolean()) {
                                    return Integer.toString(k);
                                } else {
                                    throw new Exception("testCachePollution");
                                }
                            });
                        } catch (ExecutionException e) {
                            assertNotNull(e.getCause());
                            assertThat(e.getCause(), instanceOf(Exception.class));
                            assertEquals(e.getCause().getMessage(), "testCachePollution");
                        }
                    } else if (second) {
                        cache.invalidate(key);
                    } else {
                        cache.get(key);
                    }
                }
                barrier.await();
            } catch (BrokenBarrierException | InterruptedException e) {
                throw new AssertionError(e);
            }
        });
        thread.start();
    }
    // wait for all threads to be ready
    barrier.await();
    // wait for all threads to finish
    barrier.await();
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ExecutionException(java.util.concurrent.ExecutionException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Random(java.util.Random) ExecutionException(java.util.concurrent.ExecutionException)

Example 39 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project elasticsearch by elastic.

the class CacheTests method testTorture.

// test that the cache is not corrupted under lots of concurrent modifications, even hitting the same key
// here be dragons: this test did catch one subtle bug during development; do not remove lightly
public void testTorture() throws BrokenBarrierException, InterruptedException {
    int numberOfThreads = randomIntBetween(2, 32);
    final Cache<Integer, String> cache = CacheBuilder.<Integer, String>builder().setMaximumWeight(1000).weigher((k, v) -> 2).build();
    CyclicBarrier barrier = new CyclicBarrier(1 + numberOfThreads);
    for (int i = 0; i < numberOfThreads; i++) {
        Thread thread = new Thread(() -> {
            try {
                barrier.await();
                Random random = new Random(random().nextLong());
                for (int j = 0; j < numberOfEntries; j++) {
                    Integer key = random.nextInt(numberOfEntries);
                    cache.put(key, Integer.toString(j));
                }
                barrier.await();
            } catch (BrokenBarrierException | InterruptedException e) {
                throw new AssertionError(e);
            }
        });
        thread.start();
    }
    // wait for all threads to be ready
    barrier.await();
    // wait for all threads to finish
    barrier.await();
    cache.refresh();
    assertEquals(500, cache.count());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Random(java.util.Random) ArrayList(java.util.ArrayList) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) HashSet(java.util.HashSet) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ESTestCase(org.elasticsearch.test.ESTestCase) ManagementFactory(java.lang.management.ManagementFactory) Before(org.junit.Before) CyclicBarrier(java.util.concurrent.CyclicBarrier) Matchers.empty(org.hamcrest.Matchers.empty) Set(java.util.Set) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ThreadMXBean(java.lang.management.ThreadMXBean) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Matchers.is(org.hamcrest.Matchers.is) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Random(java.util.Random) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 40 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project elasticsearch by elastic.

the class CacheTests method testExceptionThrownDuringConcurrentComputeIfAbsent.

public void testExceptionThrownDuringConcurrentComputeIfAbsent() throws BrokenBarrierException, InterruptedException {
    int numberOfThreads = randomIntBetween(2, 32);
    final Cache<String, String> cache = CacheBuilder.<String, String>builder().build();
    CyclicBarrier barrier = new CyclicBarrier(1 + numberOfThreads);
    final String key = randomAsciiOfLengthBetween(2, 32);
    for (int i = 0; i < numberOfThreads; i++) {
        Thread thread = new Thread(() -> {
            try {
                barrier.await();
                for (int j = 0; j < numberOfEntries; j++) {
                    try {
                        String value = cache.computeIfAbsent(key, k -> {
                            throw new RuntimeException("failed to load");
                        });
                        fail("expected exception but got: " + value);
                    } catch (ExecutionException e) {
                        assertNotNull(e.getCause());
                        assertThat(e.getCause(), instanceOf(RuntimeException.class));
                        assertEquals(e.getCause().getMessage(), "failed to load");
                    }
                }
                barrier.await();
            } catch (BrokenBarrierException | InterruptedException e) {
                throw new AssertionError(e);
            }
        });
        thread.start();
    }
    // wait for all threads to be ready
    barrier.await();
    // wait for all threads to finish
    barrier.await();
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ExecutionException(java.util.concurrent.ExecutionException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Aggregations

BrokenBarrierException (java.util.concurrent.BrokenBarrierException)70 CyclicBarrier (java.util.concurrent.CyclicBarrier)61 Test (org.junit.Test)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 ArrayList (java.util.ArrayList)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 IOException (java.io.IOException)14 CountDownLatch (java.util.concurrent.CountDownLatch)12 ExecutionException (java.util.concurrent.ExecutionException)12 List (java.util.List)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 Random (java.util.Random)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 TimeoutException (java.util.concurrent.TimeoutException)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)7 Set (java.util.Set)6