Search in sources :

Example 56 with BrokenBarrierException

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

the class TestNodeManagerResync method testContainerPreservationOnResyncImpl.

@SuppressWarnings("unchecked")
protected void testContainerPreservationOnResyncImpl(TestNodeManager1 nm, boolean isWorkPreservingRestartEnabled) throws IOException, YarnException, InterruptedException {
    int port = ServerSocketUtil.getPort(49153, 10);
    YarnConfiguration conf = createNMConfig(port);
    conf.setBoolean(YarnConfiguration.RM_WORK_PRESERVING_RECOVERY_ENABLED, isWorkPreservingRestartEnabled);
    try {
        nm.init(conf);
        nm.start();
        ContainerId cId = TestNodeManagerShutdown.createContainerId();
        TestNodeManagerShutdown.startContainer(nm, cId, localFS, tmpDir, processStartFile, port);
        nm.setExistingContainerId(cId);
        Assert.assertEquals(1, ((TestNodeManager1) nm).getNMRegistrationCount());
        nm.getNMDispatcher().getEventHandler().handle(resyncEvent);
        try {
            syncBarrier.await();
        } catch (BrokenBarrierException e) {
        }
        Assert.assertEquals(2, ((TestNodeManager1) nm).getNMRegistrationCount());
        // Only containers should be killed on resync, apps should lie around.
        // That way local resources for apps can be used beyond resync without
        // relocalization
        Assert.assertTrue(nm.getNMContext().getApplications().containsKey(cId.getApplicationAttemptId().getApplicationId()));
        Assert.assertFalse(assertionFailedInThread.get());
    } finally {
        nm.stop();
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) ContainerId(org.apache.hadoop.yarn.api.records.ContainerId)

Example 57 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project android by JetBrains.

the class RenderSecurityManagerTest method testLogger.

@Test
public void testLogger() throws Exception {
    assertNull(RenderSecurityManager.getCurrent());
    final CyclicBarrier barrier1 = new CyclicBarrier(2);
    final CyclicBarrier barrier2 = new CyclicBarrier(2);
    final CyclicBarrier barrier3 = new CyclicBarrier(2);
    Thread thread = new Thread() {

        @Override
        public void run() {
            try {
                barrier1.await();
                barrier2.await();
                System.setSecurityManager(new SecurityManager() {

                    @Override
                    public String toString() {
                        return "MyTestSecurityManager";
                    }

                    @Override
                    public void checkPermission(Permission permission) {
                    }
                });
                barrier3.await();
                assertNull(RenderSecurityManager.getCurrent());
                assertNotNull(System.getSecurityManager());
                assertEquals("MyTestSecurityManager", System.getSecurityManager().toString());
            } catch (InterruptedException e) {
                fail(e.toString());
            } catch (BrokenBarrierException e) {
                fail(e.toString());
            }
        }
    };
    thread.start();
    RenderSecurityManager manager = new RenderSecurityManager(null, null);
    RecordingLogger logger = new RecordingLogger();
    manager.setLogger(logger);
    try {
        barrier1.await();
        assertNull(RenderSecurityManager.getCurrent());
        manager.setActive(true, myCredential);
        assertSame(manager, RenderSecurityManager.getCurrent());
        barrier2.await();
        barrier3.await();
        assertNull(RenderSecurityManager.getCurrent());
        manager.setActive(false, myCredential);
        assertNull(RenderSecurityManager.getCurrent());
        assertEquals(Collections.singletonList("RenderSecurityManager being replaced by another thread"), logger.getWarningMsgs());
    } catch (InterruptedException e) {
        fail(e.toString());
    } catch (BrokenBarrierException e) {
        fail(e.toString());
    } finally {
        manager.dispose(myCredential);
        assertNull(RenderSecurityManager.getCurrent());
        assertNotNull(System.getSecurityManager());
        assertEquals("MyTestSecurityManager", System.getSecurityManager().toString());
        System.setSecurityManager(null);
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) RecordingLogger(com.android.ide.common.res2.RecordingLogger) Permission(java.security.Permission) FilePermission(java.io.FilePermission) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 58 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project gerrit by GerritCodeReview.

the class GerritServer method start.

/** Returns fully started Gerrit server */
static GerritServer start(Description desc, Config baseConfig) throws Exception {
    Config cfg = desc.buildConfig(baseConfig);
    Logger.getLogger("com.google.gerrit").setLevel(Level.DEBUG);
    CyclicBarrier serverStarted = new CyclicBarrier(2);
    Daemon daemon = new Daemon(() -> {
        try {
            serverStarted.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            throw new RuntimeException(e);
        }
    }, Paths.get(baseConfig.getString("gerrit", null, "tempSiteDir")));
    daemon.setEmailModuleForTesting(new FakeEmailSender.Module());
    daemon.setEnableSshd(SshMode.useSsh());
    final File site;
    ExecutorService daemonService = null;
    if (desc.memory()) {
        site = null;
        mergeTestConfig(cfg);
        // Set the log4j configuration to an invalid one to prevent system logs
        // from getting configured and creating log files.
        System.setProperty(SystemLog.LOG4J_CONFIGURATION, "invalidConfiguration");
        cfg.setBoolean("httpd", null, "requestLog", false);
        cfg.setBoolean("sshd", null, "requestLog", false);
        cfg.setBoolean("index", "lucene", "testInmemory", true);
        cfg.setString("gitweb", null, "cgi", "");
        daemon.setEnableHttpd(desc.httpd());
        daemon.setLuceneModule(LuceneIndexModule.singleVersionAllLatest(0));
        daemon.setDatabaseForTesting(ImmutableList.<Module>of(new InMemoryTestingDatabaseModule(cfg)));
        daemon.start();
    } else {
        site = initSite(cfg);
        daemonService = Executors.newSingleThreadExecutor();
        @SuppressWarnings("unused") Future<?> possiblyIgnoredError = daemonService.submit(() -> {
            int rc = daemon.main(new String[] { "-d", site.getPath(), "--headless", "--console-log", "--show-stack-trace" });
            if (rc != 0) {
                System.err.println("Failed to start Gerrit daemon");
                serverStarted.reset();
            }
            return null;
        });
        serverStarted.await();
        System.out.println("Gerrit Server Started");
    }
    Injector i = createTestInjector(daemon);
    return new GerritServer(desc, i, daemon, daemonService);
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Config(org.eclipse.jgit.lib.Config) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) FakeEmailSender(com.google.gerrit.testutil.FakeEmailSender) CyclicBarrier(java.util.concurrent.CyclicBarrier) Daemon(com.google.gerrit.pgm.Daemon) Injector(com.google.inject.Injector) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File)

Example 59 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project jdk8u_jdk by JetBrains.

the class ThreadSafety method testRacy_getGenericSuperclass.

@Test
public void testRacy_getGenericSuperclass() throws Exception {
    final int nThreads = 10;
    final int iterations = 30;
    final int timeout = 10;
    final CyclicBarrier newCycle = new CyclicBarrier(nThreads);
    final Callable<Void> task = new Callable<Void>() {

        public Void call() throws Exception {
            for (int i = 0; i < iterations; i++) {
                final int threadId;
                try {
                    threadId = newCycle.await(timeout, SECONDS);
                } catch (BrokenBarrierException e) {
                    return null;
                }
                for (int j = 0; j < iterations; j++) {
                    // race, for the other threads to consume.
                    if (threadId == 0) {
                        racyClass = createNewEmptyGenericSubclassClass();
                    } else {
                        racyClass.getGenericSuperclass();
                    }
                }
            }
            return null;
        }
    };
    final ExecutorService pool = Executors.newFixedThreadPool(nThreads);
    try {
        for (Future<Void> future : pool.invokeAll(Collections.nCopies(nThreads, task))) {
            try {
                future.get(iterations * timeout, SECONDS);
            } catch (ExecutionException e) {
                // ignore "collateral damage"
                if (!(e.getCause() instanceof BrokenBarrierException) && !(e.getCause() instanceof TimeoutException)) {
                    throw e;
                }
            }
        }
    } finally {
        pool.shutdownNow();
        assertTrue(pool.awaitTermination(2 * timeout, SECONDS));
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable) CyclicBarrier(java.util.concurrent.CyclicBarrier) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 60 with BrokenBarrierException

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

the class SnapshotSaveAPI method startSnapshotting.

/**
     * The only public method: do all the work to start a snapshot.
     * Assumes that a snapshot is feasible, that the caller has validated it can
     * be accomplished, that the caller knows this is a consistent or useful
     * transaction point at which to snapshot.
     *
     * @param file_path
     * @param file_nonce
     * @param format
     * @param block
     * @param txnId
     * @param data
     * @param context
     * @param hostname
     * @return VoltTable describing the results of the snapshot attempt
     */
public VoltTable startSnapshotting(final String file_path, final String pathType, final String file_nonce, final SnapshotFormat format, final byte block, final long multiPartTxnId, final long partitionTxnId, final long[] legacyPerPartitionTxnIds, final String data, final SystemProcedureExecutionContext context, final String hostname, final HashinatorSnapshotData hashinatorData, final long timestamp) {
    TRACE_LOG.trace("Creating snapshot target and handing to EEs");
    final VoltTable result = SnapshotUtil.constructNodeResultsTable();
    JSONObject jsData = null;
    if (data != null && !data.isEmpty()) {
        try {
            jsData = new JSONObject(data);
        } catch (JSONException e) {
            SNAP_LOG.error(String.format("JSON exception on snapshot data \"%s\".", data), e);
        }
    }
    final JSONObject finalJsData = jsData;
    JSONObject perSiteRemoteDataCenterDrIds;
    try {
        perSiteRemoteDataCenterDrIds = ExtensibleSnapshotDigestData.serializeSiteConsumerDrIdTrackersToJSON(context.getDrAppliedTrackers());
    } catch (JSONException e) {
        SNAP_LOG.warn("Failed to serialize the Remote DataCenter's Last applied DRIds");
        perSiteRemoteDataCenterDrIds = new JSONObject();
    }
    // number of snapshot permits.
    synchronized (SnapshotSiteProcessor.m_snapshotCreateLock) {
        SnapshotSiteProcessor.m_snapshotCreateSetupBarrierActualAction.set(new Runnable() {

            @Override
            public void run() {
                Map<Integer, Long> partitionTransactionIds = m_partitionLastSeenTransactionIds;
                SNAP_LOG.debug("Last seen partition transaction ids " + partitionTransactionIds);
                m_partitionLastSeenTransactionIds = new HashMap<Integer, Long>();
                partitionTransactionIds.put(TxnEgo.getPartitionId(multiPartTxnId), multiPartTxnId);
                Map<Integer, JSONObject> remoteDataCenterLastIds = m_remoteDataCenterLastIds;
                m_remoteDataCenterLastIds = new HashMap<Integer, JSONObject>();
                /*
                     * Do a quick sanity check that the provided IDs
                     * don't conflict with currently active partitions. If they do
                     * it isn't fatal we can just skip it.
                     */
                for (long txnId : legacyPerPartitionTxnIds) {
                    final int legacyPartition = TxnEgo.getPartitionId(txnId);
                    if (partitionTransactionIds.containsKey(legacyPartition)) {
                        SNAP_LOG.warn("While saving a snapshot and propagating legacy " + "transaction ids found an id that matches currently active partition" + partitionTransactionIds.get(legacyPartition));
                    } else {
                        partitionTransactionIds.put(legacyPartition, txnId);
                    }
                }
                m_allLocalSiteSnapshotDigestData = new ExtensibleSnapshotDigestData(SnapshotSiteProcessor.getExportSequenceNumbers(), SnapshotSiteProcessor.getDRTupleStreamStateInfo(), remoteDataCenterLastIds, finalJsData);
                createSetupIv2(file_path, pathType, file_nonce, format, multiPartTxnId, partitionTransactionIds, finalJsData, context, result, m_allLocalSiteSnapshotDigestData, context.getSiteTrackerForSnapshot(), hashinatorData, timestamp);
            }
        });
        // Create a barrier to use with the current number of sites to wait for
        // or if the barrier is already set up check if it is broken and reset if necessary
        final int numLocalSites = context.getLocalSitesCount();
        SnapshotSiteProcessor.readySnapshotSetupBarriers(numLocalSites);
        //From within this EE, record the sequence numbers as of the start of the snapshot (now)
        //so that the info can be put in the digest.
        SnapshotSiteProcessor.populateSequenceNumbersForExecutionSite(context);
        Integer partitionId = TxnEgo.getPartitionId(partitionTxnId);
        SNAP_LOG.debug("Registering transaction id " + partitionTxnId + " for " + TxnEgo.getPartitionId(partitionTxnId));
        m_partitionLastSeenTransactionIds.put(partitionId, partitionTxnId);
        m_remoteDataCenterLastIds.put(partitionId, perSiteRemoteDataCenterDrIds);
    }
    boolean runPostTasks = false;
    VoltTable earlyResultTable = null;
    try {
        SnapshotSiteProcessor.m_snapshotCreateSetupBarrier.await();
        try {
            synchronized (m_createLock) {
                SNAP_LOG.debug("Found tasks for HSIds: " + CoreUtils.hsIdCollectionToString(m_taskListsForHSIds.keySet()));
                SNAP_LOG.debug("Looking for local HSID: " + CoreUtils.hsIdToString(context.getSiteId()));
                Deque<SnapshotTableTask> taskList = m_taskListsForHSIds.remove(context.getSiteId());
                // switch to figure out what flavor of empty SnapshotSave result table to return.
                if (!m_createSuccess.get()) {
                    // There shouldn't be any work for any site if we failed
                    assert (m_taskListsForHSIds.isEmpty());
                    VoltTable finalresult = m_createResult.get();
                    if (finalresult != null) {
                        m_createResult.set(null);
                        earlyResultTable = finalresult;
                    } else {
                        // We returned a non-empty NodeResultsTable with the failures in it,
                        // every other site needs to return a NodeResultsTable as well.
                        earlyResultTable = SnapshotUtil.constructNodeResultsTable();
                    }
                } else if (taskList == null) {
                    SNAP_LOG.debug("No task for this site, block " + block);
                    // Send back an appropriate empty table based on the block flag
                    if (block != 0) {
                        runPostTasks = true;
                        earlyResultTable = SnapshotUtil.constructPartitionResultsTable();
                        earlyResultTable.addRow(context.getHostId(), hostname, CoreUtils.getSiteIdFromHSId(context.getSiteId()), "SUCCESS", "");
                    } else {
                        //If doing snapshot for only replicated table(s), earlyResultTable here
                        //may not be empty even if the taskList of this site is null.
                        //In that case, snapshot result is preserved by earlyResultTable.
                        earlyResultTable = result;
                    }
                } else {
                    context.getSiteSnapshotConnection().initiateSnapshots(format, taskList, multiPartTxnId, m_allLocalSiteSnapshotDigestData);
                }
                if (m_deferredSetupFuture != null && taskList != null) {
                    // Add a listener to the deferred setup so that it can kick off the snapshot
                    // task once the setup is done.
                    m_deferredSetupFuture.addListener(new Runnable() {

                        @Override
                        public void run() {
                            DeferredSnapshotSetup deferredSnapshotSetup = null;
                            try {
                                deferredSnapshotSetup = m_deferredSetupFuture.get();
                            } catch (Exception e) {
                            // it doesn't throw
                            }
                            assert deferredSnapshotSetup != null;
                            context.getSiteSnapshotConnection().startSnapshotWithTargets(deferredSnapshotSetup.getPlan().getSnapshotDataTargets());
                        }
                    }, CoreUtils.SAMETHREADEXECUTOR);
                }
            }
        } finally {
            SnapshotSiteProcessor.m_snapshotCreateFinishBarrier.await(120, TimeUnit.SECONDS);
        }
    } catch (TimeoutException e) {
        VoltDB.crashLocalVoltDB("Timed out waiting 120 seconds for all threads to arrive and start snapshot", true, null);
    } catch (InterruptedException e) {
        result.addRow(context.getHostId(), hostname, "", "FAILURE", CoreUtils.throwableToString(e));
        earlyResultTable = result;
    } catch (BrokenBarrierException e) {
        result.addRow(context.getHostId(), hostname, "", "FAILURE", CoreUtils.throwableToString(e));
        earlyResultTable = result;
    } catch (IllegalArgumentException e) {
        result.addRow(context.getHostId(), hostname, "", "FAILURE", CoreUtils.throwableToString(e));
        earlyResultTable = result;
    }
    // If earlyResultTable is set, return here
    if (earlyResultTable != null) {
        if (runPostTasks) {
            // Need to run post-snapshot tasks before finishing
            SnapshotSiteProcessor.runPostSnapshotTasks(context);
        }
        return earlyResultTable;
    }
    if (block != 0) {
        HashSet<Exception> failures = Sets.newHashSet();
        String status = "SUCCESS";
        String err = "";
        try {
            // For blocking snapshot, propogate the error from deferred setup back to the client
            final DeferredSnapshotSetup deferredSnapshotSetup = m_deferredSetupFuture.get();
            if (deferredSnapshotSetup != null && deferredSnapshotSetup.getError() != null) {
                status = "FAILURE";
                err = deferredSnapshotSetup.getError().toString();
                failures.add(deferredSnapshotSetup.getError());
            }
            failures.addAll(context.getSiteSnapshotConnection().completeSnapshotWork());
            SnapshotSiteProcessor.runPostSnapshotTasks(context);
        } catch (Exception e) {
            status = "FAILURE";
            err = e.toString();
            failures.add(e);
        }
        final VoltTable blockingResult = SnapshotUtil.constructPartitionResultsTable();
        if (failures.isEmpty()) {
            blockingResult.addRow(context.getHostId(), hostname, CoreUtils.getSiteIdFromHSId(context.getSiteId()), status, err);
        } else {
            status = "FAILURE";
            for (Exception e : failures) {
                err = e.toString();
            }
            blockingResult.addRow(context.getHostId(), hostname, CoreUtils.getSiteIdFromHSId(context.getSiteId()), status, err);
        }
        return blockingResult;
    }
    return result;
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) HashMap(java.util.HashMap) JSONException(org.json_voltpatches.JSONException) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.zookeeper_voltpatches.KeeperException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) JSONException(org.json_voltpatches.JSONException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JSONObject(org.json_voltpatches.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) TimeoutException(java.util.concurrent.TimeoutException)

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