public void stop() {

    try {
      flush();
    } catch (IOException e) {
      LOGGER.error("Error flushing", e);
    }
    try {
      close();
    } catch (IOException e) {
      LOGGER.error("Error closing", e);
    }
    try {
      backgroundFlushTask.shutdown();
      // Wait a while for existing tasks to terminate
      if (!backgroundFlushTask.awaitTermination(15, TimeUnit.SECONDS)) {
        backgroundFlushTask.shutdownNow(); // Cancel currently executing tasks
        // Wait a while for tasks to respond to being cancelled
        if (!backgroundFlushTask.awaitTermination(15, TimeUnit.SECONDS)) {
          LOGGER.error("Stream did not terminate");
        }
      }
    } catch (InterruptedException ie) {
      // (Re-)Cancel if current thread also interrupted
      backgroundFlushTask.shutdownNow();
      // Preserve interrupt status
      Thread.currentThread().interrupt();
    }
  }
  /**
   * Test to make sure we reschedule the task for execution if the new requested execution is
   * earlier than the previous one
   */
  public void testRescheduleForEarlierTime() throws InterruptedException {
    ScheduledExecutorService ex = Executors.newScheduledThreadPool(1);
    MyConflationListener listener = new MyConflationListener();
    OneTaskOnlyExecutor decorator = new OneTaskOnlyExecutor(ex, listener);

    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger counter = new AtomicInteger();

    Runnable increment =
        new Runnable() {

          public void run() {
            counter.incrementAndGet();
          }
        };

    decorator.schedule(increment, 120, TimeUnit.SECONDS);
    decorator.schedule(increment, 10, TimeUnit.MILLISECONDS);

    long start = System.nanoTime();

    ex.shutdown();
    ex.awaitTermination(60, TimeUnit.SECONDS);
    long elapsed = System.nanoTime() - start;
    assertEquals(1, counter.get());
    assertEquals(1, listener.getDropCount());
    assertTrue(elapsed < TimeUnit.SECONDS.toNanos(120));
  }
Ejemplo n.º 3
0
 /** Stop the decommission monitor thread, waiting briefly for it to terminate. */
 void close() {
   executor.shutdownNow();
   try {
     executor.awaitTermination(3000, TimeUnit.MILLISECONDS);
   } catch (InterruptedException e) {
   }
 }
Ejemplo n.º 4
0
  public void close() throws IOException {
    flush();

    if (!commitExecutor.isShutdown()) {
      commitExecutor.shutdown();
      try {
        if (!commitExecutor.awaitTermination(5, TimeUnit.MINUTES))
          throw new OException("Background data flush task can not be stopped.");
      } catch (InterruptedException e) {
        OLogManager.instance().error(this, "Data flush thread was interrupted");

        Thread.interrupted();
        throw new OException("Data flush thread was interrupted", e);
      }
    }

    synchronized (syncObject) {
      for (OFileClassic fileClassic : files.values()) {
        if (fileClassic.isOpen()) fileClassic.close();
      }

      if (nameIdMapHolder != null) {
        nameIdMapHolder.setLength(0);
        for (Map.Entry<String, Long> entry : nameIdMap.entrySet()) {
          writeNameIdEntry(new NameFileIdEntry(entry.getKey(), entry.getValue()), false);
        }
        nameIdMapHolder.getFD().sync();
        nameIdMapHolder.close();
      }
    }
  }
Ejemplo n.º 5
0
  public void delete() throws IOException {
    synchronized (syncObject) {
      for (long fileId : files.keySet()) doDeleteFile(fileId);

      if (nameIdMapHolderFile != null) {
        nameIdMapHolder.close();

        if (!nameIdMapHolderFile.delete())
          throw new OStorageException(
              "Can not delete disk cache file which contains name-id mapping.");
      }
    }

    if (!commitExecutor.isShutdown()) {
      commitExecutor.shutdown();
      try {
        if (!commitExecutor.awaitTermination(5, TimeUnit.MINUTES))
          throw new OException("Background data flush task can not be stopped.");
      } catch (InterruptedException e) {
        OLogManager.instance().error(this, "Data flush thread was interrupted");

        Thread.interrupted();
        throw new OException("Data flush thread was interrupted", e);
      }
    }
  }
Ejemplo n.º 6
0
 private void stop() {
   log.info("ExecutorManager stopping...");
   scheduler.shutdown();
   try {
     scheduler.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.MILLISECONDS);
   } catch (Exception e) {
     log.error("scheduler await termination error", e);
   }
   canceller.shutdown();
   try {
     canceller.awaitTermination(0, TimeUnit.MILLISECONDS);
   } catch (InterruptedException e) {
     log.error("canceller await termination error", e);
   }
   nearRealTimeOrderedTasksMap.clear();
 }
Ejemplo n.º 7
0
  public void testBlockingTake() throws InterruptedException {
    final CoalescedPipe<String> coalescedPipe = new CoalescedPipe<String>();

    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

    scheduledExecutorService.schedule(
        new Runnable() {

          public void run() {
            try {
              coalescedPipe.put("test1");
            } catch (InterruptedException ie) {
              fail(ie.getMessage());
            }
          }
        },
        500,
        TimeUnit.MILLISECONDS);

    long startTime = System.currentTimeMillis();

    assertEquals("test1", coalescedPipe.take());
    assertTrue((System.currentTimeMillis() - startTime) > 250L);

    scheduledExecutorService.shutdownNow();
    scheduledExecutorService.awaitTermination(120, TimeUnit.SECONDS);
  }
Ejemplo n.º 8
0
  public static synchronized void clearThreadPools() {

    if (globalThreadPool != null) {
      globalThreadPool.shutdown();
      try {
        if (!globalThreadPool.awaitTermination(10, TimeUnit.SECONDS)) {
          throw new IllegalStateException("Couldn't finish the globalThreadPool");
        }
      } catch (InterruptedException e) {
      } finally {
        globalThreadPool = null;
      }
    }

    if (globalScheduledThreadPool != null) {
      globalScheduledThreadPool.shutdown();
      try {
        if (!globalScheduledThreadPool.awaitTermination(10, TimeUnit.SECONDS)) {
          throw new IllegalStateException("Couldn't finish the globalScheduledThreadPool");
        }
      } catch (InterruptedException e) {
      } finally {
        globalScheduledThreadPool = null;
      }
    }
  }
  @Test
  public void shouldNotExhaustThreads() throws Exception {
    final ScheduledExecutorService executorService =
        Executors.newScheduledThreadPool(2, testingThreadFactory);
    final GremlinExecutor gremlinExecutor =
        GremlinExecutor.build()
            .executorService(executorService)
            .scheduledExecutorService(executorService)
            .create();

    final AtomicInteger count = new AtomicInteger(0);
    assertTrue(
        IntStream.range(0, 1000)
            .mapToObj(i -> gremlinExecutor.eval("1+1"))
            .allMatch(
                f -> {
                  try {
                    return (Integer) f.get() == 2;
                  } catch (Exception ex) {
                    throw new RuntimeException(ex);
                  } finally {
                    count.incrementAndGet();
                  }
                }));

    assertEquals(1000, count.intValue());

    executorService.shutdown();
    executorService.awaitTermination(30000, TimeUnit.MILLISECONDS);
  }
Ejemplo n.º 10
0
 public void shutdownBlocking() {
   service.shutdown();
   try {
     service.awaitTermination(defaultExpiration * 2, TimeUnit.MILLISECONDS);
   } catch (InterruptedException e) {
     throw new AssertionError(e);
   }
 }
Ejemplo n.º 11
0
 synchronized void shutdown() {
   stop();
   executor.shutdown();
   try {
     executor.awaitTermination(3, TimeUnit.SECONDS);
   } catch (InterruptedException e) {
     executor.shutdownNow();
   }
 }
Ejemplo n.º 12
0
  void stopTrashExecutor() {
    trashExecutor.shutdown();

    try {
      trashExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
      trashExecutor = null;
    } catch (InterruptedException e) {
      logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
  }
Ejemplo n.º 13
0
    public void passivate() throws Exception {
      circuitBreaker.removeVetoableChangeListener(this);

      if (receiverExecutor != null) {
        receiverExecutor.shutdown();
        receiverExecutor.awaitTermination(30, TimeUnit.SECONDS);
      }

      logger.info("Mail service shutdown");
    }
Ejemplo n.º 14
0
  @Test
  public void testDeadlock() throws Exception {
    LuceneTestCase.assumeFalse(
        "This test fails on UNIX with Turkish default locale (https://issues.apache.org/jira/browse/LUCENE-6036)",
        new Locale("tr").getLanguage().equals(Locale.getDefault().getLanguage()));

    // pick random codec names for stress test in separate process:
    final Random rnd = RandomizedContext.current().getRandom();
    Set<String> avail;
    final String codecName =
        new ArrayList<>(avail = Codec.availableCodecs()).get(rnd.nextInt(avail.size()));
    final String pfName =
        new ArrayList<>(avail = PostingsFormat.availablePostingsFormats())
            .get(rnd.nextInt(avail.size()));
    final String dvfName =
        new ArrayList<>(avail = DocValuesFormat.availableDocValuesFormats())
            .get(rnd.nextInt(avail.size()));

    // spawn separate JVM:
    final Process p =
        new ProcessBuilder(
                Paths.get(System.getProperty("java.home"), "bin", "java").toString(),
                "-cp",
                System.getProperty("java.class.path"),
                getClass().getName(),
                codecName,
                pfName,
                dvfName)
            .inheritIO()
            .start();
    final ScheduledExecutorService scheduler =
        Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("processKiller"));
    final ScheduledFuture<?> f =
        scheduler.schedule(
            new Runnable() {
              @Override
              public void run() {
                p.destroy();
              }
            },
            30,
            TimeUnit.SECONDS);
    try {
      final int exitCode = p.waitFor();
      if (f.cancel(false)) {
        assertEquals("Process died abnormally", 0, exitCode);
      } else {
        fail("Process did not exit after 30 secs -> classloader deadlock?");
      }
    } finally {
      scheduler.shutdown();
      while (!scheduler.awaitTermination(1, TimeUnit.MINUTES)) ;
    }
  }
  @Test
  public void shouldStopSchedulingJobPoller() throws Exception {
    // given
    when(executorService.awaitTermination(1, TimeUnit.MINUTES)).thenReturn(true);

    // when
    jobWorkerDaemon.stop();

    // then
    verify(executorService).shutdown();
  }
Ejemplo n.º 16
0
  @Override
  public boolean stop() {
    _hostScanScheduler.shutdownNow();
    try {
      _hostScanScheduler.awaitTermination(3000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
    }

    shutdownCleanup();
    return true;
  }
  @Override
  public synchronized void shutdown() throws Throwable {
    modeSwitcherExecutor.shutdown();

    modeSwitcherExecutor.awaitTermination(60, TimeUnit.SECONDS);

    haCommunicationLife.shutdown();

    switchToMaster.close();
    switchToMaster = null;
    switchToSlave = null;
  }
Ejemplo n.º 18
0
 public void stop() {
   executor.shutdownNow();
   try {
     if (executor.awaitTermination(1, TimeUnit.SECONDS)) {
       logger.info("metric reporters stopped.");
     } else {
       logger.info("metric reporters can't stopped in 1 seconds, force stopped");
     }
   } catch (InterruptedException ignored) {
     // do nothing
   }
 }
  @Test
  public void shouldForceStoppingSchedulingJobPollerAfterOneMinute() throws Exception {
    // given
    when(executorService.awaitTermination(1, TimeUnit.MINUTES)).thenReturn(false).thenReturn(true);

    // when
    jobWorkerDaemon.stop();

    // then
    verify(executorService).shutdown();
    verify(executorService).shutdownNow();
  }
Ejemplo n.º 20
0
  public void stop() {
    /** Shutdown the scheduler and clear out any remaining tasks */
    try {
      mScheduler.shutdown();

      mScheduler.awaitTermination(100, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
      /* Do nothing ... we're shutting down */
      //	        mLog.error( "DFTProcessor - exception while awaiting shutdown of "
      //	        		+ "calculation scheduler for reset", e );
    }
  }
  @Test
  public void shouldNotShutdownExecutorServicesSuppliedToGremlinExecutor() throws Exception {
    final ScheduledExecutorService service =
        Executors.newScheduledThreadPool(4, testingThreadFactory);
    final GremlinExecutor gremlinExecutor =
        GremlinExecutor.build().executorService(service).scheduledExecutorService(service).create();

    gremlinExecutor.close();
    assertFalse(service.isShutdown());
    service.shutdown();
    service.awaitTermination(30000, TimeUnit.MILLISECONDS);
  }
 @Override
 public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
   boolean result = true;
   long now = System.nanoTime();
   long timeRemaining = unit.toNanos(timeout);
   for (ScheduledExecutorService w : workers) {
     result = result && w.awaitTermination(timeRemaining, TimeUnit.NANOSECONDS);
     long elapsed = System.nanoTime() - now;
     timeRemaining = Math.min(1, timeout - elapsed);
   }
   return result;
 }
Ejemplo n.º 23
0
  private void shutdownExecutor() {
    if (executor != null) {
      executor.shutdown();

      try {
        if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
          executor.shutdownNow();
        }
      } catch (InterruptedException e) {
        executor.shutdownNow();
      }
    }
  }
 private synchronized void stopScheduledExecutor() {
   if (scheduledExecutorService == null) {
     return;
   }
   try {
     scheduledExecutorService.shutdown();
     scheduledExecutorService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
     running.set(false);
     scheduledExecutorService = null;
   } catch (InterruptedException e) {
     log.timedOutWaitingShutdown(indexName);
   }
 }
 /** Close this {@link Publisher}. This will also close the underlying {@link Pubsub} client. */
 @Override
 public void close() {
   // TODO (dano): fail outstanding futures
   scheduler.shutdownNow();
   try {
     scheduler.awaitTermination(30, SECONDS);
   } catch (InterruptedException e) {
     Thread.currentThread().interrupt();
   }
   pubsub.close();
   closeFuture.complete(null);
   listener.publisherClosed(Publisher.this);
 }
Ejemplo n.º 26
0
  /** Called by APAM when an instance of this implementation is removed */
  public void deleteInst() {
    logger.info("A user instance desapeared");

    instanciationService.shutdown();
    try {
      instanciationService.awaitTermination(5, TimeUnit.SECONDS);
      logger.debug("instanciation acocunt service terminate.");
    } catch (InterruptedException e) {
      // instanciationService has probably terminated, but some problem
      // happened.
      logger.debug("Account instanciation service thread crash at termination");
    }
  }
  @Test
  public void shouldForceStoppingSchedulingJobPollerWhenInterruptedExceptionIsThrow()
      throws Exception {
    // given
    when(executorService.awaitTermination(1, TimeUnit.MINUTES))
        .thenThrow(new InterruptedException());

    // when
    jobWorkerDaemon.stop();

    // then
    verify(executorService).shutdown();
    verify(executorService).shutdownNow();
  }
Ejemplo n.º 28
0
  @Override
  public synchronized void stop() {
    executor.shutdown();
    try {
      executor.awaitTermination(10L, TimeUnit.SECONDS);
    } catch (InterruptedException ex) {
      logger.info("Interrupted while awaiting termination", ex);
    }
    executor.shutdownNow();

    super.stop();
    sourceCounter.stop();
    logger.info("SpoolDir source {} stopped. Metrics: {}", getName(), sourceCounter);
  }
 public void close() {
   closed = true;
   executor.shutdown();
   try {
     executor.awaitTermination(2, TimeUnit.SECONDS);
   } catch (InterruptedException e) {
     Thread.currentThread().interrupt();
   }
   if (activityStorage != null) {
     activityStorage.close();
   }
   for (ActivityValues activityIntValues : valuesMap.values()) {
     activityIntValues.close();
   }
 }
Ejemplo n.º 30
0
  @Test
  public void testStartStop() throws Exception {
    BufferHandler handler =
        new BufferHandler() {

          public void bufferReceived(final Object connectionID, final HornetQBuffer buffer) {}
        };

    Map<String, Object> params = new HashMap<String, Object>();
    ConnectionLifeCycleListener listener =
        new ConnectionLifeCycleListener() {

          public void connectionException(final Object connectionID, final HornetQException me) {}

          public void connectionDestroyed(final Object connectionID) {}

          public void connectionCreated(
              final HornetQComponent component,
              final Connection connection,
              final String protocol) {}

          public void connectionReadyForWrites(Object connectionID, boolean ready) {}
        };
    pool1 = Executors.newCachedThreadPool();
    pool2 =
        Executors.newScheduledThreadPool(
            HornetQDefaultConfiguration.getDefaultScheduledThreadPoolMaxSize());
    NettyAcceptor acceptor = new NettyAcceptor(params, handler, listener, pool1, pool2, null);

    addHornetQComponent(acceptor);
    acceptor.start();
    Assert.assertTrue(acceptor.isStarted());
    acceptor.stop();
    Assert.assertFalse(acceptor.isStarted());
    UnitTestCase.checkFreePort(TransportConstants.DEFAULT_PORT);

    acceptor.start();
    Assert.assertTrue(acceptor.isStarted());
    acceptor.stop();
    Assert.assertFalse(acceptor.isStarted());
    UnitTestCase.checkFreePort(TransportConstants.DEFAULT_PORT);

    pool1.shutdown();
    pool2.shutdown();

    pool1.awaitTermination(1, TimeUnit.SECONDS);
    pool2.awaitTermination(1, TimeUnit.SECONDS);
  }