Search in sources :

Example 36 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project lucene-solr by apache.

the class CdcrReplicationHandlerTest method testReplicationWithBufferedUpdates.

/**
   * Test the scenario where the slave is killed while the leader is still receiving updates.
   * The slave should buffer updates while in recovery, then replay them at the end of the recovery.
   * If updates were properly buffered and replayed, then the slave should have the same number of documents
   * than the leader. This checks if cdcr tlog replication interferes with buffered updates - SOLR-8263.
   */
@Test
@ShardsFixed(num = 2)
public void testReplicationWithBufferedUpdates() throws Exception {
    List<CloudJettyRunner> slaves = this.getShardToSlaveJetty(SOURCE_COLLECTION, SHARD1);
    AtomicInteger numDocs = new AtomicInteger(0);
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new DefaultSolrThreadFactory("cdcr-test-update-scheduler"));
    executor.scheduleWithFixedDelay(new UpdateThread(numDocs), 10, 10, TimeUnit.MILLISECONDS);
    // Restart the slave node to trigger Replication strategy
    this.restartServer(slaves.get(0));
    // shutdown the update thread and wait for its completion
    executor.shutdown();
    executor.awaitTermination(500, TimeUnit.MILLISECONDS);
    // check that we have the expected number of documents in the cluster
    assertNumDocs(numDocs.get(), SOURCE_COLLECTION);
    // check that we have the expected number of documents on the slave
    assertNumDocs(numDocs.get(), slaves.get(0));
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultSolrThreadFactory(org.apache.solr.util.DefaultSolrThreadFactory) Test(org.junit.Test)

Example 37 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project lucene-solr by apache.

the class LeaderElectionTest method testStressElection.

@Test
public void testStressElection() throws Exception {
    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(15, new DefaultSolrThreadFactory("stressElection"));
    final List<ClientThread> threads = Collections.synchronizedList(new ArrayList<ClientThread>());
    // start with a leader
    ClientThread thread1 = null;
    thread1 = new ClientThread("shard1", 0);
    threads.add(thread1);
    scheduler.schedule(thread1, 0, TimeUnit.MILLISECONDS);
    Thread.sleep(2000);
    Thread scheduleThread = new Thread() {

        @Override
        public void run() {
            int count = atLeast(5);
            for (int i = 1; i < count; i++) {
                int launchIn = random().nextInt(500);
                ClientThread thread = null;
                try {
                    thread = new ClientThread("shard1", i);
                } catch (Exception e) {
                //
                }
                if (thread != null) {
                    threads.add(thread);
                    scheduler.schedule(thread, launchIn, TimeUnit.MILLISECONDS);
                }
            }
        }
    };
    Thread killThread = new Thread() {

        @Override
        public void run() {
            while (!stopStress) {
                try {
                    int j;
                    try {
                        // always 1 we won't kill...
                        j = random().nextInt(threads.size() - 2);
                    } catch (IllegalArgumentException e) {
                        continue;
                    }
                    try {
                        threads.get(j).close();
                    } catch (Exception e) {
                    }
                    Thread.sleep(10);
                } catch (Exception e) {
                }
            }
        }
    };
    Thread connLossThread = new Thread() {

        @Override
        public void run() {
            while (!stopStress) {
                try {
                    Thread.sleep(50);
                    int j;
                    j = random().nextInt(threads.size());
                    try {
                        threads.get(j).es.zkClient.getSolrZooKeeper().closeCnxn();
                        if (random().nextBoolean()) {
                            long sessionId = zkClient.getSolrZooKeeper().getSessionId();
                            server.expire(sessionId);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Thread.sleep(500);
                } catch (Exception e) {
                }
            }
        }
    };
    scheduleThread.start();
    connLossThread.start();
    killThread.start();
    Thread.sleep(4000);
    stopStress = true;
    scheduleThread.interrupt();
    connLossThread.interrupt();
    killThread.interrupt();
    scheduleThread.join();
    scheduler.shutdownNow();
    connLossThread.join();
    killThread.join();
    int seq = threads.get(getLeaderThread()).getSeq();
    // cleanup any threads still running
    for (ClientThread thread : threads) {
        thread.es.zkClient.getSolrZooKeeper().close();
        thread.close();
    }
    for (Thread thread : threads) {
        thread.join();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) DefaultSolrThreadFactory(org.apache.solr.util.DefaultSolrThreadFactory) SessionExpiredException(org.apache.zookeeper.KeeperException.SessionExpiredException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) Test(org.junit.Test)

Example 38 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project lucene-solr by apache.

the class ConnectionManagerTest method testReconnectWhenZkDisappeared.

@Test
public void testReconnectWhenZkDisappeared() throws Exception {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new DefaultSolrThreadFactory("connectionManagerTest"));
    // setup a SolrZkClient to do some getBaseUrlForNodeName testing
    String zkDir = createTempDir("zkData").toFile().getAbsolutePath();
    ZkTestServer server = new ZkTestServer(zkDir);
    try {
        server.run();
        AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
        AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
        MockZkClientConnectionStrategy strat = new MockZkClientConnectionStrategy();
        SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT, strat, null);
        ConnectionManager cm = zkClient.getConnectionManager();
        try {
            assertFalse(cm.isLikelyExpired());
            assertTrue(cm.isConnected());
            // reconnect -- should no longer be likely expired
            cm.process(new WatchedEvent(EventType.None, KeeperState.Expired, ""));
            assertFalse(cm.isLikelyExpired());
            assertTrue(cm.isConnected());
            assertTrue(strat.isExceptionThrow());
        } finally {
            cm.close();
            zkClient.close();
            executor.shutdown();
        }
    } finally {
        server.shutdown();
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ConnectionManager(org.apache.solr.common.cloud.ConnectionManager) DefaultSolrThreadFactory(org.apache.solr.util.DefaultSolrThreadFactory) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) Test(org.junit.Test)

Example 39 with ScheduledExecutorService

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

the class OneTaskOnlyDecoratorJUnitTest method testExecuteOnlyOnce.

/**
   * Test to make sure we only execute the task once no matter how many times we schedule it.
   */
@Test
public void testExecuteOnlyOnce() throws Exception {
    ScheduledExecutorService ex = Executors.newScheduledThreadPool(1);
    MyConflationListener listener = new MyConflationListener();
    OneTaskOnlyExecutor decorator = new OneTaskOnlyExecutor(ex, listener);
    final CountDownLatch latch = new CountDownLatch(1);
    ex.submit(new Callable() {

        public Object call() throws Exception {
            latch.await();
            return null;
        }
    });
    final AtomicInteger counter = new AtomicInteger();
    Runnable increment = new Runnable() {

        public void run() {
            counter.incrementAndGet();
        }
    };
    for (int i = 0; i < 50; i++) {
        decorator.schedule(increment, 0, TimeUnit.SECONDS);
    }
    assertEquals(0, counter.get());
    latch.countDown();
    ex.shutdown();
    ex.awaitTermination(60, TimeUnit.SECONDS);
    assertEquals(1, counter.get());
    assertEquals(49, listener.getDropCount());
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 40 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project jackrabbit-oak by apache.

the class CompactionAndCleanupIT method offlineCompactionBinC1.

@Test
public void offlineCompactionBinC1() throws Exception {
    SegmentGCOptions gcOptions = defaultGCOptions().setOffline().withBinaryDeduplication();
    ScheduledExecutorService executor = newSingleThreadScheduledExecutor();
    FileStore fileStore = fileStoreBuilder(getFileStoreFolder()).withMaxFileSize(1).withGCOptions(gcOptions).withStatisticsProvider(new DefaultStatisticsProvider(executor)).build();
    SegmentNodeStore nodeStore = SegmentNodeStoreBuilders.builder(fileStore).build();
    try {
        NodeBuilder extra = nodeStore.getRoot().builder();
        NodeBuilder content = extra.child("content");
        int blobSize = 5 * 1024 * 1024;
        byte[] data = new byte[blobSize];
        new Random().nextBytes(data);
        NodeBuilder c1 = content.child("c1");
        Blob b1 = nodeStore.createBlob(new ByteArrayInputStream(data));
        c1.setProperty("blob1", b1);
        NodeBuilder c2 = content.child("c2");
        Blob b2 = nodeStore.createBlob(new ByteArrayInputStream(data));
        c2.setProperty("blob2", b2);
        nodeStore.merge(extra, EmptyHook.INSTANCE, CommitInfo.EMPTY);
        fileStore.flush();
        int cpNo = 4;
        Set<String> cps = new HashSet<String>();
        for (int i = 0; i < cpNo; i++) {
            cps.add(nodeStore.checkpoint(60000));
        }
        assertEquals(cpNo, cps.size());
        for (String cp : cps) {
            assertTrue(nodeStore.retrieve(cp) != null);
        }
        long size1 = fileStore.getStats().getApproximateSize();
        fileStore.compact();
        fileStore.cleanup();
        long size2 = fileStore.getStats().getApproximateSize();
        assertSize("with compacted binaries", size2, 0, size1 - blobSize);
    } finally {
        fileStore.close();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Blob(org.apache.jackrabbit.oak.api.Blob) SegmentGCOptions(org.apache.jackrabbit.oak.segment.compaction.SegmentGCOptions) DefaultStatisticsProvider(org.apache.jackrabbit.oak.stats.DefaultStatisticsProvider) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) FileStore(org.apache.jackrabbit.oak.segment.file.FileStore) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)821 Test (org.junit.Test)267 CountDownLatch (java.util.concurrent.CountDownLatch)79 ArrayList (java.util.ArrayList)72 Test (org.testng.annotations.Test)72 IOException (java.io.IOException)71 ExecutorService (java.util.concurrent.ExecutorService)70 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)65 HashMap (java.util.HashMap)57 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)53 List (java.util.List)51 Map (java.util.Map)51 TimeUnit (java.util.concurrent.TimeUnit)44 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)43 ThreadFactory (java.util.concurrent.ThreadFactory)40 CompletableFuture (java.util.concurrent.CompletableFuture)35 UUID (java.util.UUID)34 Cleanup (lombok.Cleanup)31 ExecutionException (java.util.concurrent.ExecutionException)30 HashSet (java.util.HashSet)25