Search in sources :

Example 76 with Callable

use of java.util.concurrent.Callable in project graylog2-server by Graylog2.

the class AbstractTcpTransportTest method getBaseChannelHandlersGeneratesSelfSignedCertificates.

@Test
public void getBaseChannelHandlersGeneratesSelfSignedCertificates() {
    final Configuration configuration = new Configuration(ImmutableMap.of("bind_address", "localhost", "port", 12345, "tls_enable", true));
    final AbstractTcpTransport transport = new AbstractTcpTransport(configuration, throughputCounter, localRegistry, bossPool, workerPool, connectionCounter) {

        @Override
        protected Bootstrap getBootstrap() {
            return super.getBootstrap();
        }

        @Override
        protected LinkedHashMap<String, Callable<? extends ChannelHandler>> getBaseChannelHandlers(MessageInput input) {
            return super.getBaseChannelHandlers(input);
        }
    };
    final MessageInput input = mock(MessageInput.class);
    assertThat(transport.getBaseChannelHandlers(input)).containsKey("tls");
}
Also used : Configuration(org.graylog2.plugin.configuration.Configuration) MessageInput(org.graylog2.plugin.inputs.MessageInput) ChannelHandler(org.jboss.netty.channel.ChannelHandler) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 77 with Callable

use of java.util.concurrent.Callable in project graylog2-server by Graylog2.

the class AbstractTcpTransportTest method getBaseChannelHandlersFailsIfTempDirIsNoDirectory.

@Test
public void getBaseChannelHandlersFailsIfTempDirIsNoDirectory() throws IOException {
    final File file = temporaryFolder.newFile();
    assumeTrue(file.isFile());
    System.setProperty("java.io.tmpdir", file.getAbsolutePath());
    final Configuration configuration = new Configuration(ImmutableMap.of("bind_address", "localhost", "port", 12345, "tls_enable", true));
    final AbstractTcpTransport transport = new AbstractTcpTransport(configuration, throughputCounter, localRegistry, bossPool, workerPool, connectionCounter) {

        @Override
        protected Bootstrap getBootstrap() {
            return super.getBootstrap();
        }

        @Override
        protected LinkedHashMap<String, Callable<? extends ChannelHandler>> getBaseChannelHandlers(MessageInput input) {
            return super.getBaseChannelHandlers(input);
        }
    };
    expectedException.expect(IllegalStateException.class);
    expectedException.expectMessage("Couldn't write to temporary directory: " + file.getAbsolutePath());
    transport.getBaseChannelHandlers(input);
}
Also used : Configuration(org.graylog2.plugin.configuration.Configuration) MessageInput(org.graylog2.plugin.inputs.MessageInput) ChannelHandler(org.jboss.netty.channel.ChannelHandler) File(java.io.File) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 78 with Callable

use of java.util.concurrent.Callable in project alluxio by Alluxio.

the class FileSystemMaster method startupCheckConsistency.

/**
   * Checks the consistency of the root in a multi-threaded and incremental fashion. This method
   * will only READ lock the directories and files actively being checked and release them after the
   * check on the file / directory is complete.
   *
   * @return a list of paths in Alluxio which are not consistent with the under storage
   * @throws InterruptedException if the thread is interrupted during execution
   * @throws IOException if an error occurs interacting with the under storage
   */
private List<AlluxioURI> startupCheckConsistency(final ExecutorService service) throws InterruptedException, IOException {
    /** A marker {@link StartupConsistencyChecker}s add to the queue to signal completion */
    final long completionMarker = -1;
    /** A shared queue of directories which have yet to be checked */
    final BlockingQueue<Long> dirsToCheck = new LinkedBlockingQueue<>();
    /**
     * A {@link Callable} which checks the consistency of a directory.
     */
    final class StartupConsistencyChecker implements Callable<List<AlluxioURI>> {

        /** The path to check, guaranteed to be a directory in Alluxio. */
        private final Long mFileId;

        /**
       * Creates a new callable which checks the consistency of a directory.
       * @param fileId the path to check
       */
        private StartupConsistencyChecker(Long fileId) {
            mFileId = fileId;
        }

        /**
       * Checks the consistency of the directory and all immediate children which are files. All
       * immediate children which are directories are added to the shared queue of directories to
       * check. The parent directory is READ locked during the entire call while the children are
       * READ locked only during the consistency check of the children files.
       *
       * @return a list of inconsistent uris
       * @throws IOException if an error occurs interacting with the under storage
       */
        @Override
        public List<AlluxioURI> call() throws IOException {
            List<AlluxioURI> inconsistentUris = new ArrayList<>();
            try (LockedInodePath dir = mInodeTree.lockFullInodePath(mFileId, InodeTree.LockMode.READ)) {
                Inode parentInode = dir.getInode();
                AlluxioURI parentUri = dir.getUri();
                if (!checkConsistencyInternal(parentInode, parentUri)) {
                    inconsistentUris.add(parentUri);
                }
                for (Inode childInode : ((InodeDirectory) parentInode).getChildren()) {
                    try {
                        childInode.lockReadAndCheckParent(parentInode);
                    } catch (InvalidPathException e) {
                        // This should be safe, continue.
                        LOG.debug("Error during startup check consistency, ignoring and continuing.", e);
                        continue;
                    }
                    try {
                        AlluxioURI childUri = parentUri.join(childInode.getName());
                        if (childInode.isDirectory()) {
                            dirsToCheck.add(childInode.getId());
                        } else {
                            if (!checkConsistencyInternal(childInode, childUri)) {
                                inconsistentUris.add(childUri);
                            }
                        }
                    } finally {
                        childInode.unlockRead();
                    }
                }
            } catch (FileDoesNotExistException e) {
                // This should be safe, continue.
                LOG.debug("A file scheduled for consistency check was deleted before the check.");
            } catch (InvalidPathException e) {
                // This should not happen.
                LOG.error("An invalid path was discovered during the consistency check, skipping.", e);
            }
            dirsToCheck.add(completionMarker);
            return inconsistentUris;
        }
    }
    // Add the root to the directories to check.
    dirsToCheck.add(mInodeTree.getRoot().getId());
    List<Future<List<AlluxioURI>>> results = new ArrayList<>();
    // Tracks how many checkers have been started.
    long started = 0;
    // Tracks how many checkers have completed.
    long completed = 0;
    do {
        Long fileId = dirsToCheck.take();
        if (fileId == completionMarker) {
            // A thread signaled completion.
            completed++;
        } else {
            // A new directory needs to be checked.
            StartupConsistencyChecker checker = new StartupConsistencyChecker(fileId);
            results.add(service.submit(checker));
            started++;
        }
    } while (started != completed);
    // Return the total set of inconsistent paths discovered.
    List<AlluxioURI> inconsistentUris = new ArrayList<>();
    for (Future<List<AlluxioURI>> result : results) {
        try {
            inconsistentUris.addAll(result.get());
        } catch (Exception e) {
            // This shouldn't happen, all futures should be complete.
            Throwables.propagate(e);
        }
    }
    service.shutdown();
    return inconsistentUris;
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) ArrayList(java.util.ArrayList) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) InvalidPathException(alluxio.exception.InvalidPathException) Callable(java.util.concurrent.Callable) AlluxioException(alluxio.exception.AlluxioException) BlockInfoException(alluxio.exception.BlockInfoException) FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) IOException(java.io.IOException) InvalidPathException(alluxio.exception.InvalidPathException) AccessControlException(alluxio.exception.AccessControlException) InvalidFileSizeException(alluxio.exception.InvalidFileSizeException) FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) FileAlreadyCompletedException(alluxio.exception.FileAlreadyCompletedException) DirectoryNotEmptyException(alluxio.exception.DirectoryNotEmptyException) UnexpectedAlluxioException(alluxio.exception.UnexpectedAlluxioException) LockedInodePath(alluxio.master.file.meta.LockedInodePath) InodeDirectory(alluxio.master.file.meta.InodeDirectory) Inode(alluxio.master.file.meta.Inode) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) InodeLockList(alluxio.master.file.meta.InodeLockList) List(java.util.List) TtlBucketList(alluxio.master.file.meta.TtlBucketList) PrefixList(alluxio.collections.PrefixList) AlluxioURI(alluxio.AlluxioURI)

Example 79 with Callable

use of java.util.concurrent.Callable in project nhin-d by DirectProject.

the class NHINDSecurityAndTrustMailet_functionalTest method testProcessOutgoingMessageEndToEnd_multipleProcessThreads.

public void testProcessOutgoingMessageEndToEnd_multipleProcessThreads() throws Exception {
    new TestPlan() {

        protected String getMessageToProcess() throws Exception {
            return TestUtils.readMessageResource("PlainOutgoingMessage.txt");
        }

        final class MessageEncrypter implements Callable<MimeMessage> {

            private final Mailet theMailet;

            public MessageEncrypter(Mailet theMailet) {
                this.theMailet = theMailet;
            }

            public MimeMessage call() throws Exception {
                // encrypt
                String originalMessage = getMessageToProcess();
                MimeMessage msg = EntitySerializer.Default.deserialize(originalMessage);
                // add an MDN request
                msg.setHeader(MDNStandard.Headers.DispositionNotificationTo, msg.getHeader(MailStandard.Headers.From, ","));
                MockMail theMessage = new MockMail(msg);
                theMailet.service(theMessage);
                assertNotNull(theMessage);
                assertNotNull(theMessage.getMessage());
                msg = theMessage.getMessage();
                return msg;
            }
        }

        protected void performInner() throws Exception {
            ExecutorService execService = Executors.newFixedThreadPool(10);
            String originalMessage = getMessageToProcess();
            // execute 100 times
            GatewayState.getInstance().setSettingsUpdateInterval(1);
            Mailet theMailet = getMailet("ValidConfig.xml");
            List<Future<MimeMessage>> futures = new ArrayList<Future<MimeMessage>>();
            for (int i = 0; i < 100; ++i) {
                futures.add(execService.submit(new MessageEncrypter(theMailet)));
            }
            assertEquals(100, futures.size());
            GatewayState.getInstance().setSettingsUpdateInterval(300);
            for (Future<MimeMessage> future : futures) {
                // decrypt
                theMailet = getMailet("ValidConfigStateLine.txt");
                MockMail theMessage = new MockMail(future.get());
                theMailet.service(theMessage);
                assertNotNull(theMessage);
                assertNotNull(theMessage.getMessage());
                MimeMessage msg = theMessage.getMessage();
                assertFalse(SMIMEStandard.isEncrypted(msg));
                assertEquals(theMessage.getState(), Mail.TRANSPORT);
                Message compareMessage = new Message(theMessage.getMessage());
                // remove the MDN before comparison				
                compareMessage.removeHeader(MDNStandard.Headers.DispositionNotificationTo);
                assertEquals(originalMessage, compareMessage.toString());
            }
        }
    }.perform();
}
Also used : Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) BaseTestPlan(org.nhindirect.gateway.testutils.BaseTestPlan) NHINDSecurityAndTrustMailet(org.nhindirect.gateway.smtp.james.mailet.NHINDSecurityAndTrustMailet) Mailet(org.apache.mailet.Mailet) Callable(java.util.concurrent.Callable) MimeMessage(javax.mail.internet.MimeMessage) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) List(java.util.List)

Example 80 with Callable

use of java.util.concurrent.Callable in project opennms by OpenNMS.

the class Jsr160ConnectionFactory method getMBeanServerConnection.

public static JmxServerConnectionWrapper getMBeanServerConnection(Map<String, String> propertiesMap, InetAddress address) throws IOException {
    final long timeout = DEFAULT_TIMEOUT;
    propertiesMap.putIfAbsent("factory", "STANDARD");
    propertiesMap.putIfAbsent("port", "1099");
    propertiesMap.putIfAbsent("protocol", "rmi");
    propertiesMap.putIfAbsent("urlPath", "/jmxrmi");
    propertiesMap.putIfAbsent("timeout", Long.toString(timeout));
    final Callable<JmxServerConnectionWrapper> task = new Callable<JmxServerConnectionWrapper>() {

        @Override
        public JmxServerConnectionWrapper call() throws Exception {
            return new DefaultJmxConnector().createConnection(address, propertiesMap);
        }
    };
    final Future<JmxServerConnectionWrapper> future = executor.submit(task);
    try {
        final JmxServerConnectionWrapper connectionWrapper = future.get(timeout, TimeUnit.MILLISECONDS);
        return connectionWrapper;
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        final String url = JmxConnectionConfigBuilder.buildFrom(address, propertiesMap).build().getUrl();
        LOG.info("Exception connecting JMXConnectorFactory url {} , Error: {}", url, e.getMessage());
        if (!future.isDone()) {
            future.cancel(true);
            LOG.info(" the task {}", future.isCancelled() ? "was cancelled" : "could not be cancelled");
        }
        throw new ConnectException("Error connecting JMXConnectionFactory  " + url);
    }
}
Also used : JmxServerConnectionWrapper(org.opennms.netmgt.jmx.connection.JmxServerConnectionWrapper) ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable) TimeoutException(java.util.concurrent.TimeoutException) ConnectException(java.net.ConnectException)

Aggregations

Callable (java.util.concurrent.Callable)1946 ArrayList (java.util.ArrayList)664 ExecutorService (java.util.concurrent.ExecutorService)630 Test (org.junit.Test)598 Future (java.util.concurrent.Future)482 IOException (java.io.IOException)255 ExecutionException (java.util.concurrent.ExecutionException)247 List (java.util.List)167 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)158 CountDownLatch (java.util.concurrent.CountDownLatch)157 HashMap (java.util.HashMap)120 Map (java.util.Map)117 File (java.io.File)112 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)105 Ignite (org.apache.ignite.Ignite)87 HashSet (java.util.HashSet)80 Set (java.util.Set)55 TimeoutException (java.util.concurrent.TimeoutException)54 Collectors (java.util.stream.Collectors)53 Transaction (org.apache.ignite.transactions.Transaction)52