public CoreScheduler( ScheduledExecutorService executorService, SyncTaskManager syncTaskManager ) { this.executorService = executorService; this.syncTaskManager = syncTaskManager; // Check for long execution timings ThreadMXBean mxBean = ManagementFactory.getThreadMXBean(); this.executorService.scheduleWithFixedDelay( () -> { long current = System.currentTimeMillis(); synchronized ( threads ) { Object2LongMap.FastEntrySet<Thread> threadSet = (Object2LongMap.FastEntrySet<Thread>) threads.object2LongEntrySet(); ObjectIterator<Object2LongMap.Entry<Thread>> threadIterator = threadSet.fastIterator(); while ( threadIterator.hasNext() ) { Object2LongMap.Entry<Thread> entry = threadIterator.next(); long diff = current - entry.getLongValue(); if ( diff > TimeUnit.SECONDS.toMillis( 10 ) ) { ThreadInfo threadInfo = mxBean.getThreadInfo( entry.getKey().getId() ); Thread.State state = threadInfo.getThreadState(); if ( !alreadyAlerted.contains( entry.getKey() ) && ( state == Thread.State.WAITING || state == Thread.State.TIMED_WAITING || state == Thread.State.BLOCKED ) ) { LOGGER.warn( "Following runnable is blocking the scheduler loops: {}", threadRunnables.get( entry.getKey() ).getClass().getName() ); threadInfo = mxBean.getThreadInfo( entry.getKey().getId(), Integer.MAX_VALUE ); for ( StackTraceElement element : threadInfo.getStackTrace() ) { LOGGER.warn( " {}", element ); } alreadyAlerted.add( entry.getKey() ); } } } } }, 10, 10, TimeUnit.MILLISECONDS ); }
/** * Iterator returns the same object every time, just with a different payload. */ public Iterator<PairOfObjectLong<K>> iterator() { return new Iterator<PairOfObjectLong<K>>() { private Iterator<Object2LongMap.Entry<K>> iter = Object2LongFrequencyDistributionFastutil.this.counts .object2LongEntrySet().iterator(); private final PairOfObjectLong<K> pair = new PairOfObjectLong<K>(); @Override public boolean hasNext() { return iter.hasNext(); } @Override public PairOfObjectLong<K> next() { if (!hasNext()) { return null; } Object2LongMap.Entry<K> entry = iter.next(); pair.set(entry.getKey(), entry.getLongValue()); return pair; } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
/** * Iterates {@linkplain FastEntrySet#fastForEach(Consumer) quickly}, if * possible, on the {@linkplain Map#entrySet() entry set} of the provided * {@code map}. * * @param map * a map on which we will try to iterate * {@linkplain FastEntrySet#fastForEach(Consumer) quickly}. * @param consumer * the consumer that will be passed to * {@link FastEntrySet#fastForEach(Consumer)}, if possible, or to * {@link Iterable#forEach(Consumer)}. * @since 8.1.0 */ @SuppressWarnings("unchecked") public static <K> void fastForEach(Object2LongMap<K> map, final Consumer<? super Object2LongMap.Entry<K>> consumer) { final ObjectSet<Object2LongMap.Entry<K>> entries = map.object2LongEntrySet(); if (entries instanceof Object2LongMap.FastEntrySet) ((Object2LongMap.FastEntrySet<K>) entries).fastForEach(consumer); else entries.forEach(consumer); } /**
/** * Returns an iterator that will be {@linkplain FastEntrySet fast}, if possible, * on the {@linkplain Map#entrySet() entry set} of the provided {@code map}. * * @param map * a map from which we will try to extract a (fast) iterator on the * entry set. * @return an iterator on the entry set of the given map that will be fast, if * possible. * @since 8.0.0 */ @SuppressWarnings("unchecked") public static <K> ObjectIterator<Object2LongMap.Entry<K>> fastIterator(Object2LongMap<K> map) { final ObjectSet<Object2LongMap.Entry<K>> entries = map.object2LongEntrySet(); return entries instanceof Object2LongMap.FastEntrySet ? ((Object2LongMap.FastEntrySet<K>) entries).fastIterator() : entries.iterator(); } /**
/** * Iterates quickly over this entry set; the iteration might happen always on * the same entry instance, suitably mutated. * * <p> * This default implementation just delegates to {@link #forEach(Consumer)}. * * @param consumer * a consumer that will by applied to the entries of this set; the * entries might be represented by the same entry instance, suitably * mutated. * @since 8.1.0 */ default void fastForEach(final Consumer<? super Object2LongMap.Entry<K>> consumer) { forEach(consumer); } }
private CoordinatorStats makeStats(int numCompactionTasks, CompactionSegmentIterator iterator) { final CoordinatorStats stats = new CoordinatorStats(); stats.addToGlobalStat(COMPACT_TASK_COUNT, numCompactionTasks); remainingSegmentSizeBytes = iterator.remainingSegmentSizeBytes(); iterator.remainingSegmentSizeBytes().object2LongEntrySet().fastForEach( entry -> { final String dataSource = entry.getKey(); final long numSegmentsWaitCompact = entry.getLongValue(); stats.addToDataSourceStat(SEGMENT_SIZE_WAIT_COMPACT, dataSource, numSegmentsWaitCompact); } ); return stats; }
private CoordinatorStats makeStats(int numCompactionTasks, CompactionSegmentIterator iterator) { final CoordinatorStats stats = new CoordinatorStats(); stats.addToGlobalStat(COMPACT_TASK_COUNT, numCompactionTasks); remainingSegmentSizeBytes = iterator.remainingSegmentSizeBytes(); iterator.remainingSegmentSizeBytes().object2LongEntrySet().fastForEach( entry -> { final String dataSource = entry.getKey(); final long numSegmentsWaitCompact = entry.getLongValue(); stats.addToDataSourceStat(SEGMENT_SIZE_WAIT_COMPACT, dataSource, numSegmentsWaitCompact); } ); return stats; }