Search in sources :

Example 1 with DaemonThreadFactory

use of hudson.util.DaemonThreadFactory in project support-core-plugin by jenkinsci.

the class SlaveLogs method addContents.

@Override
public void addContents(@NonNull Container container) {
    // expensive remote computation are pooled together and executed later concurrently across all the agents
    List<java.util.concurrent.Callable<List<FileContent>>> tasks = Lists.newArrayList();
    // id is awkward because of backward compatibility
    SmartLogFetcher logFetcher = new SmartLogFetcher("cache", new LogFilenameFilter());
    SmartLogFetcher winswLogFetcher = new SmartLogFetcher("winsw", new WinswLogfileFilter());
    final boolean needHack = SlaveLogFetcher.isRequired();
    for (final Node node : Jenkins.getInstance().getNodes()) {
        if (node.toComputer() instanceof SlaveComputer) {
            container.add(new PrintedContent("nodes/slave/" + node.getNodeName() + "/jenkins.log") {

                @Override
                protected void printTo(PrintWriter out) throws IOException {
                    Computer computer = node.toComputer();
                    if (computer == null) {
                        out.println("N/A");
                    } else {
                        try {
                            List<LogRecord> records = null;
                            if (needHack) {
                                VirtualChannel channel = computer.getChannel();
                                if (channel != null) {
                                    hudson.remoting.Future<List<LogRecord>> future = SlaveLogFetcher.getLogRecords(channel);
                                    records = future.get(REMOTE_OPERATION_TIMEOUT_MS, TimeUnit.MILLISECONDS);
                                }
                            }
                            if (records == null) {
                                records = computer.getLogRecords();
                            }
                            for (ListIterator<LogRecord> iterator = records.listIterator(records.size()); iterator.hasPrevious(); ) {
                                LogRecord logRecord = iterator.previous();
                                out.print(LOG_FORMATTER.format(logRecord));
                            }
                        } catch (Throwable e) {
                            out.println();
                            SupportLogFormatter.printStackTrace(e, out);
                        }
                    }
                    out.flush();
                }
            });
        }
        addSlaveJulLogRecords(container, tasks, node, logFetcher);
        addWinsStdoutStderrLog(tasks, node, winswLogFetcher);
    }
    // execute all the expensive computations in parallel to speed up the time
    if (!tasks.isEmpty()) {
        ExecutorService service = Executors.newFixedThreadPool(Math.max(1, Math.min(Runtime.getRuntime().availableProcessors() * 2, tasks.size())), new ExceptionCatchingThreadFactory(new DaemonThreadFactory()));
        try {
            long expiresNanoTime = System.nanoTime() + TimeUnit.SECONDS.toNanos(SupportPlugin.REMOTE_OPERATION_CACHE_TIMEOUT_SEC);
            for (java.util.concurrent.Future<List<FileContent>> r : service.invokeAll(tasks, SupportPlugin.REMOTE_OPERATION_CACHE_TIMEOUT_SEC, TimeUnit.SECONDS)) {
                try {
                    for (FileContent c : r.get(Math.max(1, expiresNanoTime - System.nanoTime()), TimeUnit.NANOSECONDS)) {
                        container.add(c);
                    }
                } catch (ExecutionException e) {
                    LOGGER.log(Level.WARNING, "Could not retrieve some of the remote node extra logs", e);
                } catch (TimeoutException e) {
                    LOGGER.log(Level.WARNING, "Could not retrieve some of the remote node extra logs", e);
                    r.cancel(false);
                }
            }
        } catch (InterruptedException e) {
            LOGGER.log(Level.WARNING, "Could not retrieve some of the remote node extra logs", e);
        } finally {
            service.shutdown();
        }
    }
}
Also used : DaemonThreadFactory(hudson.util.DaemonThreadFactory) Node(hudson.model.Node) MasterToSlaveCallable(jenkins.security.MasterToSlaveCallable) LogRecord(java.util.logging.LogRecord) PrintedContent(com.cloudbees.jenkins.support.api.PrintedContent) Computer(hudson.model.Computer) SlaveComputer(hudson.slaves.SlaveComputer) ArrayList(java.util.ArrayList) List(java.util.List) ExceptionCatchingThreadFactory(hudson.util.ExceptionCatchingThreadFactory) ExecutionException(java.util.concurrent.ExecutionException) PrintWriter(java.io.PrintWriter) TimeoutException(java.util.concurrent.TimeoutException) SlaveComputer(hudson.slaves.SlaveComputer) VirtualChannel(hudson.remoting.VirtualChannel) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ListIterator(java.util.ListIterator) FileContent(com.cloudbees.jenkins.support.api.FileContent) ExecutorService(java.util.concurrent.ExecutorService)

Example 2 with DaemonThreadFactory

use of hudson.util.DaemonThreadFactory in project hudson-2.x by hudson.

the class Hudson method executeReactor.

/**
 * Executes a reactor.
 *
 * @param is
 *      If non-null, this can be consulted for ignoring some tasks. Only used during the initialization of Hudson.
 */
private void executeReactor(final InitStrategy is, TaskBuilder... builders) throws IOException, InterruptedException, ReactorException {
    Reactor reactor = new Reactor(builders) {

        /**
         * Sets the thread name to the task for better diagnostics.
         */
        @Override
        protected void runTask(Task task) throws Exception {
            if (is != null && is.skipInitTask(task)) {
                return;
            }
            // full access in the initialization thread
            SecurityContextHolder.getContext().setAuthentication(ACL.SYSTEM);
            String taskName = task.getDisplayName();
            Thread t = Thread.currentThread();
            String name = t.getName();
            if (taskName != null) {
                t.setName(taskName);
            }
            try {
                long start = System.currentTimeMillis();
                super.runTask(task);
                if (LOG_STARTUP_PERFORMANCE) {
                    LOGGER.info(String.format("Took %dms for %s by %s", System.currentTimeMillis() - start, taskName, name));
                }
            } finally {
                t.setName(name);
                SecurityContextHolder.clearContext();
            }
        }
    };
    ExecutorService es;
    if (PARALLEL_LOAD) {
        es = new ThreadPoolExecutor(TWICE_CPU_NUM, TWICE_CPU_NUM, 5L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new DaemonThreadFactory());
    } else {
        es = Executors.newSingleThreadExecutor(new DaemonThreadFactory());
    }
    try {
        reactor.execute(es, buildReactorListener());
    } finally {
        // upon a successful return the executor queue should be empty. Upon an exception, we want to cancel all pending tasks
        es.shutdownNow();
    }
}
Also used : Task(org.jvnet.hudson.reactor.Task) DaemonThreadFactory(hudson.util.DaemonThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Reactor(org.jvnet.hudson.reactor.Reactor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) UDPBroadcastThread(hudson.UDPBroadcastThread)

Aggregations

DaemonThreadFactory (hudson.util.DaemonThreadFactory)2 ExecutorService (java.util.concurrent.ExecutorService)2 FileContent (com.cloudbees.jenkins.support.api.FileContent)1 PrintedContent (com.cloudbees.jenkins.support.api.PrintedContent)1 UDPBroadcastThread (hudson.UDPBroadcastThread)1 Computer (hudson.model.Computer)1 Node (hudson.model.Node)1 VirtualChannel (hudson.remoting.VirtualChannel)1 SlaveComputer (hudson.slaves.SlaveComputer)1 ExceptionCatchingThreadFactory (hudson.util.ExceptionCatchingThreadFactory)1 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 ExecutionException (java.util.concurrent.ExecutionException)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 TimeoutException (java.util.concurrent.TimeoutException)1