Java Code Examples for org.apache.lucene.util.ThreadInterruptedException

The following examples show how to use org.apache.lucene.util.ThreadInterruptedException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: lucene-solr   Source File: PrimaryNode.java    License: Apache License 2.0 6 votes vote down vote up
private synchronized void waitForAllRemotesToClose() throws IOException {

    // Wait for replicas to finish or crash:
    while (true) {
      int count = copyingCount.get();
      if (count == 0) {
        return;
      }
      message("pendingCopies: " + count);

      try {
        wait(10);
      } catch (InterruptedException ie) {
        throw new ThreadInterruptedException(ie);
      }
    }
  }
 
Example 2
Source Project: lucene-solr   Source File: ReplicationClient.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Stop the update thread. If the update thread is not running, silently does
 * nothing. This method returns after the update thread has stopped.
 */
public synchronized void stopUpdateThread() {
  if (updateThread != null) {
    // this will trigger the thread to terminate if it awaits the lock.
    // otherwise, if it's in the middle of replication, we wait for it to
    // stop.
    updateThread.stop.countDown();
    try {
      updateThread.join();
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      throw new ThreadInterruptedException(e);
    }
    updateThread = null;
  }
}
 
Example 3
@Override
public synchronized void close() {
  //System.out.println("NRT: set finish");

  finish = true;

  // So thread wakes up and notices it should finish:
  reopenLock.lock();
  try {
    reopenCond.signal();
  } finally {
    reopenLock.unlock();
  }

  try {
    join();
  } catch (InterruptedException ie) {
    throw new ThreadInterruptedException(ie);
  }

  // Max it out so any waiting search threads will return:
  searchingGen = Long.MAX_VALUE;
  notifyAll();
}
 
Example 4
Source Project: lucene-solr   Source File: DocumentsWriterStallControl.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Blocks if documents writing is currently in a stalled state. 
 * 
 */
void waitIfStalled() {
  if (stalled) {
    synchronized (this) {
      if (stalled) { // react on the first wakeup call!
        // don't loop here, higher level logic will re-stall!
        try {
          incWaiters();
          // Defensive, in case we have a concurrency bug that fails to .notify/All our thread:
          // just wait for up to 1 second here, and let caller re-stall if it's still needed:
          wait(1000);
          decrWaiters();
        } catch (InterruptedException e) {
          throw new ThreadInterruptedException(e);
        }
      }
    }
  }
}
 
Example 5
@Override
public void run() {
  try {
    latch.await();
  } catch (InterruptedException e) {
    throw new ThreadInterruptedException(e);
  }
  int i = 0;
  while ((i = index.getAndIncrement()) < ids.length) {
    Term term = new Term("id", ids[i].toString());
    DocumentsWriterDeleteQueue.Node<Term> termNode = DocumentsWriterDeleteQueue.newNode(term);
    queue.add(termNode, slice);
    assertTrue(slice.isTail(termNode));
    slice.apply(deletes, BufferedUpdates.MAX_INT);
  }
}
 
Example 6
Source Project: crate   Source File: IndexShard.java    License: Apache License 2.0 6 votes vote down vote up
private void handleRefreshException(Exception e) {
    if (e instanceof AlreadyClosedException) {
        // ignore
    } else if (e instanceof RefreshFailedEngineException) {
        RefreshFailedEngineException rfee = (RefreshFailedEngineException) e;
        if (rfee.getCause() instanceof InterruptedException) {
            // ignore, we are being shutdown
        } else if (rfee.getCause() instanceof ClosedByInterruptException) {
            // ignore, we are being shutdown
        } else if (rfee.getCause() instanceof ThreadInterruptedException) {
            // ignore, we are being shutdown
        } else {
            if (state != IndexShardState.CLOSED) {
                logger.warn("Failed to perform engine refresh", e);
            }
        }
    } else {
        if (state != IndexShardState.CLOSED) {
            logger.warn("Failed to perform engine refresh", e);
        }
    }
}
 
Example 7
Source Project: Elasticsearch   Source File: CancellableThreads.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * run the Interruptable, capturing the executing thread. Concurrent calls to {@link #cancel(String)} will interrupt this thread
 * causing the call to prematurely return.
 *
 * @param interruptable code to run
 */
public void execute(Interruptable interruptable) {
    boolean wasInterrupted = add();
    RuntimeException throwable = null;
    try {
        interruptable.run();
    } catch (InterruptedException | ThreadInterruptedException e) {
        // assume this is us and ignore
    } catch (RuntimeException t) {
        throwable = t;
    } finally {
        remove();
    }
    // we are now out of threads collection so we can't be interrupted any more by this class
    // restore old flag and see if we need to fail
    if (wasInterrupted) {
        Thread.currentThread().interrupt();
    } else {
        // clear the flag interrupted flag as we are checking for failure..
        Thread.interrupted();
    }
    synchronized (this) {
        if (isCancelled()) {
            onCancel(reason, throwable);
        } else if (throwable != null) {
            // if we're not canceling, we throw the original exception
            throw throwable;
        }
    }
}
 
Example 8
Source Project: lucene-solr   Source File: EnwikiContentSource.java    License: Apache License 2.0 5 votes vote down vote up
String[] next() throws NoMoreDataException {
  if (t == null) {
    threadDone = false;
    t = new Thread(this);
    t.setDaemon(true);
    t.start();
  }
  String[] result;
  synchronized(this){
    while(tuple == null && nmde == null && !threadDone && !stopped) {
      try {
        wait();
      } catch (InterruptedException ie) {
        throw new ThreadInterruptedException(ie);
      }
    }
    if (tuple != null) {
      result = tuple;
      tuple = null;
      notify();
      return result;
    }
    if (nmde != null) {
      // Set to null so we will re-start thread in case
      // we are re-used:
      t = null;
      throw nmde;
    }
    // The thread has exited yet did not hit end of
    // data, so this means it hit an exception.  We
    // throw NoMorDataException here to force
    // benchmark to stop the current alg:
    throw new NoMoreDataException();
  }
}
 
Example 9
Source Project: lucene-solr   Source File: ReplicationClient.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("synthetic-access")
@Override
public void run() {
  while (true) {
    long time = System.currentTimeMillis();
    updateLock.lock();
    try {
      doUpdate();
    } catch (Throwable t) {
      handleUpdateException(t);
    } finally {
      updateLock.unlock();
    }
    time = System.currentTimeMillis() - time;
    
    // adjust timeout to compensate the time spent doing the replication.
    final long timeout = interval - time;
    if (timeout > 0) {
      try {
        // this will return immediately if we were ordered to stop (count=0)
        // or the timeout has elapsed. if it returns true, it means count=0,
        // so terminate.
        if (stop.await(timeout, TimeUnit.MILLISECONDS)) {
          return;
        }
      } catch (InterruptedException e) {
        // if we were interruted, somebody wants to terminate us, so just
        // throw the exception further.
        Thread.currentThread().interrupt();
        throw new ThreadInterruptedException(e);
      }
    }
  }
}
 
Example 10
Source Project: lucene-solr   Source File: SlowDirectory.java    License: Apache License 2.0 5 votes vote down vote up
void doSleep(Random random, int length) {
  int sTime = length<10 ? sleepMillis : (int) (sleepMillis * Math.log(length));
  if (random!=null) {
    sTime = random.nextInt(sTime);
  }
  try {
    Thread.sleep(sTime);
  } catch (InterruptedException e) {
    throw new ThreadInterruptedException(e);
  }
}
 
Example 11
@Override
public void close() throws IOException {
  try {
    Thread.sleep(50);
  } catch (InterruptedException ie) {
    throw new ThreadInterruptedException(ie);
  } finally {
    super.close();
  }
}
 
Example 12
public SlowOpeningMockIndexInputWrapper(MockDirectoryWrapper dir,
    String name, IndexInput delegate) throws IOException {
  super(dir, name, delegate, null);
  try {
    Thread.sleep(50);
  } catch (InterruptedException ie) {
    try {
      super.close();
    } catch (Throwable ignore) {} // we didnt open successfully
    throw new ThreadInterruptedException(ie);
  }
}
 
Example 13
Source Project: lucene-solr   Source File: TimeLimitingCollector.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  while (!stop) {
    // TODO: Use System.nanoTime() when Lucene moves to Java SE 5.
    counter.addAndGet(resolution);
    try {
      Thread.sleep( resolution );
    } catch (InterruptedException ie) {
      throw new ThreadInterruptedException(ie);
    }
  }
}
 
Example 14
Source Project: lucene-solr   Source File: DocumentsWriterFlushControl.java    License: Apache License 2.0 5 votes vote down vote up
public synchronized void waitForFlush() {
  while (flushingWriters.size() != 0) {
    try {
      this.wait();
    } catch (InterruptedException e) {
      throw new ThreadInterruptedException(e);
    }
  }
}
 
Example 15
Source Project: lucene-solr   Source File: ConcurrentMergeScheduler.java    License: Apache License 2.0 5 votes vote down vote up
/** Called from {@link #maybeStall} to pause the calling thread for a bit. */
protected synchronized void doStall() {
  try {
    // Defensively wait for only .25 seconds in case we are missing a .notify/All somewhere:
    wait(250);
  } catch (InterruptedException ie) {
    throw new ThreadInterruptedException(ie);
  }
}
 
Example 16
Source Project: lucene-solr   Source File: SleepingLockWrapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Lock obtainLock(String lockName) throws IOException {
  LockObtainFailedException failureReason = null;
  long maxSleepCount = lockWaitTimeout / pollInterval;
  long sleepCount = 0;
  
  do {
    try {
      return in.obtainLock(lockName);
    } catch (LockObtainFailedException failed) {
      if (failureReason == null) {
        failureReason = failed;
      }
    }
    try {
      Thread.sleep(pollInterval);
    } catch (InterruptedException ie) {
      throw new ThreadInterruptedException(ie);
    }
  } while (sleepCount++ < maxSleepCount || lockWaitTimeout == LOCK_OBTAIN_WAIT_FOREVER);
  
  // we failed to obtain the lock in the required time
  String reason = "Lock obtain timed out: " + this.toString();
  if (failureReason != null) {
    reason += ": " + failureReason;
  }
  throw new LockObtainFailedException(reason, failureReason);
}
 
Example 17
@Override
public long updateDocument(Term term,
    Iterable<? extends IndexableField> doc)
    throws IOException {
  long result = super.updateDocument(term, doc);
  try {
    if (waitAfterUpdate) {
      signal.countDown();
      latch.await();
    }
  } catch (InterruptedException e) {
    throw new ThreadInterruptedException(e);
  }
  return result;
}
 
Example 18
Source Project: lucene-solr   Source File: TestIndexWriterReader.java    License: Apache License 2.0 5 votes vote down vote up
void joinThreads() {
  for (int i = 0; i < numThreads; i++)
    try {
      threads[i].join();
    } catch (InterruptedException ie) {
      throw new ThreadInterruptedException(ie);
    }
}
 
Example 19
Source Project: lucene-solr   Source File: TestIndexWriter.java    License: Apache License 2.0 5 votes vote down vote up
public void testThreadInterruptDeadlock() throws Exception {
  IndexerThreadInterrupt t = new IndexerThreadInterrupt(1);
  t.setDaemon(true);
  t.start();

  // Force class loader to load ThreadInterruptedException
  // up front... else we can see a false failure if 2nd
  // interrupt arrives while class loader is trying to
  // init this class (in servicing a first interrupt):
  assertTrue(new ThreadInterruptedException(new InterruptedException()).getCause() instanceof InterruptedException);

  // issue 100 interrupts to child thread
  final int numInterrupts = atLeast(100);
  int i = 0;
  while(i < numInterrupts) {
    // TODO: would be nice to also sometimes interrupt the
    // CMS merge threads too ...
    Thread.sleep(10);
    if (t.allowInterrupt) {
      i++;
      t.interrupt();
    }
    if (!t.isAlive()) {
      break;
    }
  }
  t.finish = true;
  t.join();
  if (t.failed) {
    fail(t.bytesLog.toString("UTF-8"));
  }
}
 
Example 20
Source Project: lucene-solr   Source File: TestRateLimiter.java    License: Apache License 2.0 5 votes vote down vote up
public void testOverflowInt() throws Exception {
  Thread t = new Thread() {
      @Override
      public void run() {
        expectThrows(ThreadInterruptedException.class, () -> {
          new SimpleRateLimiter(1).pause((long) (1.5*Integer.MAX_VALUE*1024*1024/1000));
        });
      }
    };
  t.start();
  Thread.sleep(10);
  t.interrupt();
}
 
Example 21
Source Project: lucene-solr   Source File: MergeRateLimiter.java    License: Apache License 2.0 4 votes vote down vote up
/** 
 * Returns the number of nanoseconds spent in a paused state or <code>-1</code>
 * if no pause was applied. If the thread needs pausing, this method delegates 
 * to the linked {@link OneMergeProgress}. 
 */
private long maybePause(long bytes, long curNS) throws MergePolicy.MergeAbortedException {
  // Now is a good time to abort the merge:
  if (mergeProgress.isAborted()) {
    throw new MergePolicy.MergeAbortedException("Merge aborted.");
  }

  double rate = mbPerSec; // read from volatile rate once.
  double secondsToPause = (bytes/1024./1024.) / rate;

  // Time we should sleep until; this is purely instantaneous
  // rate (just adds seconds onto the last time we had paused to);
  // maybe we should also offer decayed recent history one?
  long targetNS = lastNS + (long) (1000000000 * secondsToPause);

  long curPauseNS = targetNS - curNS;

  // We don't bother with thread pausing if the pause is smaller than 2 msec.
  if (curPauseNS <= MIN_PAUSE_NS) {
    // Set to curNS, not targetNS, to enforce the instant rate, not
    // the "averaged over all history" rate:
    lastNS = curNS;
    return -1;
  }

  // Defensive: don't sleep for too long; the loop above will call us again if
  // we should keep sleeping and the rate may be adjusted in between.
  if (curPauseNS > MAX_PAUSE_NS) {
    curPauseNS = MAX_PAUSE_NS;
  }

  long start = System.nanoTime();
  try {
    mergeProgress.pauseNanos(
        curPauseNS, 
        rate == 0.0 ? PauseReason.STOPPED : PauseReason.PAUSED,
        () -> rate == mbPerSec);
  } catch (InterruptedException ie) {
    throw new ThreadInterruptedException(ie);
  }
  return System.nanoTime() - start;
}
 
Example 22
Source Project: lucene-solr   Source File: RateLimiter.java    License: Apache License 2.0 4 votes vote down vote up
/** Pauses, if necessary, to keep the instantaneous IO
 *  rate at or below the target.  Be sure to only call
 *  this method when bytes &gt; {@link #getMinPauseCheckBytes},
 *  otherwise it will pause way too long!
 *
 *  @return the pause time in nano seconds */  
@Override
public long pause(long bytes) {

  long startNS = System.nanoTime();

  double secondsToPause = (bytes/1024./1024.) / mbPerSec;

  long targetNS;

  // Sync'd to read + write lastNS:
  synchronized (this) {

    // Time we should sleep until; this is purely instantaneous
    // rate (just adds seconds onto the last time we had paused to);
    // maybe we should also offer decayed recent history one?
    targetNS = lastNS + (long) (1000000000 * secondsToPause);

    if (startNS >= targetNS) {
      // OK, current time is already beyond the target sleep time,
      // no pausing to do.

      // Set to startNS, not targetNS, to enforce the instant rate, not
      // the "averaaged over all history" rate:
      lastNS = startNS;
      return 0;
    }

    lastNS = targetNS;
  }

  long curNS = startNS;

  // While loop because Thread.sleep doesn't always sleep
  // enough:
  while (true) {
    final long pauseNS = targetNS - curNS;
    if (pauseNS > 0) {
      try {
        // NOTE: except maybe on real-time JVMs, minimum realistic sleep time
        // is 1 msec; if you pass just 1 nsec the default impl rounds
        // this up to 1 msec:
        int sleepNS;
        int sleepMS;
        if (pauseNS > 100000L * Integer.MAX_VALUE) {
          // Not really practical (sleeping for 25 days) but we shouldn't overflow int:
          sleepMS = Integer.MAX_VALUE;
          sleepNS = 0;
        } else {
          sleepMS = (int) (pauseNS/1000000);
          sleepNS = (int) (pauseNS % 1000000);
        }
        Thread.sleep(sleepMS, sleepNS);
      } catch (InterruptedException ie) {
        throw new ThreadInterruptedException(ie);
      }
      curNS = System.nanoTime();
      continue;
    }
    break;
  }

  return curNS - startNS;
}
 
Example 23
Source Project: lucene-solr   Source File: TestIndexWriter.java    License: Apache License 2.0 4 votes vote down vote up
/** Make sure that close waits for any still-running commits. */
public void testCloseDuringCommit() throws Exception {

  final CountDownLatch startCommit = new CountDownLatch(1);
  final CountDownLatch finishCommit = new CountDownLatch(1);

  Directory dir = newDirectory();
  IndexWriterConfig iwc = new IndexWriterConfig(null);
  // use an infostream that "takes a long time" to commit
  final IndexWriter iw = RandomIndexWriter.mockIndexWriter(random(), dir, iwc, new RandomIndexWriter.TestPoint() {
    @Override
    public void apply(String message) {
      if (message.equals("finishStartCommit")) {
        startCommit.countDown();
        try {
          Thread.sleep(10);
        } catch (InterruptedException ie) {
          throw new ThreadInterruptedException(ie);
        }
      }
    }
  });
  new Thread() {
    @Override
    public void run() {
      try {
        iw.commit();
        finishCommit.countDown();
      } catch (IOException ioe) {
        throw new RuntimeException(ioe);
      }
    }
  }.start();
  startCommit.await();
  try {
    iw.close();
  } catch (IllegalStateException ise) {
    // OK, but not required (depends on thread scheduling)
  }
  finishCommit.await();
  iw.close();
  dir.close();
}
 
Example 24
Source Project: lucene-solr   Source File: TestIndexWriterWithThreads.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
  try {
    syncStart.await();
  } catch (BrokenBarrierException | InterruptedException e) {
    error = e;
    throw new RuntimeException(e);
  }

  final Document doc = new Document();
  FieldType customType = new FieldType(TextField.TYPE_STORED);
  customType.setStoreTermVectors(true);
  customType.setStoreTermVectorPositions(true);
  customType.setStoreTermVectorOffsets(true);
  
  doc.add(newField("field", "aaa bbb ccc ddd eee fff ggg hhh iii jjj", customType));
  doc.add(new NumericDocValuesField("dv", 5));

  int idUpto = 0;
  int fullCount = 0;

  do {
    try {
      writer.updateDocument(new Term("id", ""+(idUpto++)), doc);
      addCount++;
    } catch (IOException ioe) {
      if (VERBOSE) {
        System.out.println("TEST: expected exc:");
        ioe.printStackTrace(System.out);
      }
      //System.out.println(Thread.currentThread().getName() + ": hit exc");
      //ioe.printStackTrace(System.out);
      if (ioe.getMessage().startsWith("fake disk full at") ||
          ioe.getMessage().equals("now failing on purpose")) {
        diskFull = true;
        try {
          Thread.sleep(1);
        } catch (InterruptedException ie) {
          throw new ThreadInterruptedException(ie);
        }
        if (fullCount++ >= 5)
          break;
      } else {
        if (noErrors) {
          System.out.println(Thread.currentThread().getName() + ": ERROR: unexpected IOException:");
          ioe.printStackTrace(System.out);
          error = ioe;
        }
        break;
      }
    } catch (AlreadyClosedException ace) {
      // OK: abort closes the writer
      break;
    } catch (Throwable t) {
      if (noErrors) {
        System.out.println(Thread.currentThread().getName() + ": ERROR: unexpected Throwable:");
        t.printStackTrace(System.out);
        error = t;
      }
      break;
    }
  } while (true);
}
 
Example 25
Source Project: lucene-solr   Source File: TestRateLimiter.java    License: Apache License 2.0 4 votes vote down vote up
public void testThreads() throws Exception {

    double targetMBPerSec = 10.0 + 20 * random().nextDouble();
    final SimpleRateLimiter limiter = new SimpleRateLimiter(targetMBPerSec);

    final CountDownLatch startingGun = new CountDownLatch(1);

    Thread[] threads = new Thread[TestUtil.nextInt(random(), 3, 6)];
    final AtomicLong totBytes = new AtomicLong();
    for(int i=0;i<threads.length;i++) {
      threads[i] = new Thread() {
          @Override
          public void run() {
            try {
              startingGun.await();
            } catch (InterruptedException ie) {
              throw new ThreadInterruptedException(ie);
            }
            long bytesSinceLastPause = 0;
            for(int i=0;i<500;i++) {
              long numBytes = TestUtil.nextInt(random(), 1000, 10000);
              totBytes.addAndGet(numBytes);
              bytesSinceLastPause += numBytes;
              if (bytesSinceLastPause > limiter.getMinPauseCheckBytes()) {
                limiter.pause(bytesSinceLastPause);
                bytesSinceLastPause = 0;
              }
            }
          }
        };
      threads[i].start();
    }

    long startNS = System.nanoTime();
    startingGun.countDown();
    for(Thread thread : threads) {
      thread.join();
    }
    long endNS = System.nanoTime();
    double actualMBPerSec = (totBytes.get()/1024/1024.)/((endNS-startNS)/1000000000.0);

    // TODO: this may false trip .... could be we can only assert that it never exceeds the max, so slow jenkins doesn't trip:
    double ratio = actualMBPerSec/targetMBPerSec;

    // Only enforce that it wasn't too fast; if machine is bogged down (can't schedule threads / sleep properly) then it may falsely be too slow:
    assumeTrue("actualMBPerSec=" + actualMBPerSec + " targetMBPerSec=" + targetMBPerSec, 0.9 <= ratio);
    assertTrue("targetMBPerSec=" + targetMBPerSec + " actualMBPerSec=" + actualMBPerSec, ratio <= 1.1);
  }