@Override protected void doStart() { executor.start(this); }
@Override protected void doStop() { executor.stop(this); } }
public static void main(final String... args) throws InterruptedException, IOException { final FibersExecutor executor = new FibersExecutor(); final Fiber helloFiber = new HelloWorldFiber(executor); helloFiber.start(); TimeUnit.SECONDS.sleep(10); helloFiber.close(); } }
@Override public final void run() { while(isStarted()) { if(fibers.size() == 0) { try { Thread.sleep(1); } catch(final InterruptedException e) { break; } } else { for(final Fiber nextFiber : fibers) { try { if(nextFiber.isStarted() || nextFiber.isShutdown()) { nextFiber.invoke(); } } catch(final RuntimeException e) { throw e; // don't catch the unchecked exceptions } catch(final Throwable t) { LOG.log(Level.WARNING, "Fiber \"" + nextFiber + "\" failed", t); } if(backgroundFlag) { LockSupport.parkNanos(1); } } } } } }
public void setThreadCount(final int threadCount) { final int newThreadCount = threadCount > 0 ? threadCount : Runtime.getRuntime().availableProcessors(); final int oldThreadCount = executor.getCorePoolSize(); if(newThreadCount != oldThreadCount) { executor.setCorePoolSize(newThreadCount); executor.setMaximumPoolSize(newThreadCount); if(newThreadCount > oldThreadCount) { for(int i = oldThreadCount; i < newThreadCount; i ++) { final FibersExecutorTask execTask = new FibersExecutorTask( fibers, backgroundFlag ); executor.submit(execTask); workers.add(execTask); execTask.start(); } } else { // less, remove some active service worker tasks try { for(int i = oldThreadCount - 1; i >= newThreadCount; i --) { workers.remove(i).close(); } } catch (final Exception e) { e.printStackTrace(System.err); } } } } }
public AsyncValueUpdatingSupplier( final FibersExecutor executor, final T initialValue, final InitCallable<T> updateAction) throws NullPointerException { super(initialValue, null); if (updateAction == null) { throw new NullPointerException("Argument should not be null"); } updateTask = new ExclusiveFiberBase(executor) { @Override protected final void invokeTimedExclusively(final long startTimeNanos) { try { lastValue = updateAction.call(); } catch (final Exception e) { LogUtil.trace( Loggers.ERR, Level.WARN, e, "Failed to execute the value update action"); } } @Override protected final void doClose() { lastValue = null; } }; try { updateTask.start(); } catch (final RemoteException ignored) { } }
@Override public void close() throws IOException { super.close(); updateTask.close(); } }
@Override public boolean await(final long timeout, final TimeUnit timeUnit) throws IllegalStateException, InterruptedException { if(isShutdown()) { final long invokeTimeMillis = System.currentTimeMillis(); final long timeOutMillis = timeUnit.toMillis(timeout); while(timeOutMillis > System.currentTimeMillis() - invokeTimeMillis) { if(itemsBuff.isEmpty()) { return true; } LockSupport.parkNanos(1); } return false; } else { return super.await(timeout, timeUnit); } }
@Override public final boolean put(final T ioTask) throws IOException { if(isStopped()) { throw new EOFException(); } final O output = selectOutput(); final CircularBuffer<T> dstBuff = buffs.get(output); final Lock dstBuffLock = buffLocks.get(output); if(dstBuff != null && dstBuffLock != null && dstBuffLock.tryLock()) { try { return dstBuff.add(ioTask); } finally { dstBuffLock.unlock(); } } else { return false; } }
public FibersExecutor(final boolean backgroundFlag) { final int svcThreadCount = Runtime.getRuntime().availableProcessors(); executor = new ThreadPoolExecutor( svcThreadCount, svcThreadCount, 0, TimeUnit.DAYS, new ArrayBlockingQueue<>(1), new ContextAwareThreadFactory("fibers-executor-", true, null) ); this.backgroundFlag = backgroundFlag; for(int i = 0; i < svcThreadCount; i ++) { final FibersExecutorTask svcWorkerTask = new FibersExecutorTask( fibers, backgroundFlag ); executor.submit(svcWorkerTask); workers.add(svcWorkerTask); svcWorkerTask.start(); } }
@Override protected final void doClose() throws IOException { super.doClose(); lineReader.close(); } }
@Override public final int put(final List<T> buffer) throws IOException { return put(buffer, 0, buffer.size()); }
@Override protected final void invokeTimed(final long startTimeNanos) { if(invocationLock.tryLock()) { try { invokeTimedExclusively(startTimeNanos); } finally { invocationLock.unlock(); } } }
@Override protected final void doStop() { super.doStop(); inputFinishFlag.set(true); Loggers.MSG.info( "Read task finish: scattered {} lines from the input file \"{}\"", lineCount, srcFileName); }
private void startIfNotStarted() { if (!isStarted()) { super.start(); Loggers.MSG.debug("Started the metrics manager fiber"); } }
/** * Decorates the invocation method with timing. */ @Override public final void invoke() { long t = System.nanoTime(); invokeTimed(t); t = System.nanoTime() - t; if(t > DEBUG_DURATION_LIMIT_NANOS) { LOG.log( t > WARN_DURATION_LIMIT_NANOS ? Level.WARNING : Level.FINE, "Fiber \"" + this + "\" invocation duration (" + TimeUnit.NANOSECONDS.toMillis(t) + "[ms]) exceeds " + "the limit (" + TimeUnit.NANOSECONDS.toMillis(SOFT_DURATION_LIMIT_NANOS) + "[ms])" ); } }
@Override protected final void doStop() throws IllegalStateException { super.doStop(); Loggers.MSG.debug( "{}: generated {}, recycled {}, output {} operations", LoadGeneratorImpl.this.toString(), builtTasksCounter.sum(), recycledOpCounter.sum(), outputOpCounter.sum()); }
new TransferFiber<>(ServiceTaskExecutor.INSTANCE, driver, this, batchSize); final long configCountLimit = opLimitConfig.longVal("count"); this.countLimit = configCountLimit > 0 ? configCountLimit : Long.MAX_VALUE;
@Override protected void doStart() throws IllegalStateException { try { resultsTransferTask.start(); } catch (final RemoteException ignored) { } try { driver.start(); } catch (final RemoteException ignored) { } catch (final IllegalStateException e) { LogUtil.exception(Level.WARN, e, "{}: failed to start the storage driver \"{}\"", id, driver); } try { generator.start(); } catch (final RemoteException ignored) { } catch (final IllegalStateException e) { LogUtil.exception( Level.WARN, e, "{}: failed to start the load generator \"{}\"", id, generator); } }
@Override protected final void doClose() throws IOException { super.doClose(); lines.clear(); linesByteBuff.close(); linesWriter.close(); } }