Package org.apache.lucene.store

Examples of org.apache.lucene.store.Lock


   */
  public void run() {
    LOGGER.info("Optimization run started...");
    RequestContext context = null;
    IndexWriter writer = null;
    Lock backgroundLock = null;
    long tStartMillis = System.currentTimeMillis();
    try {
     
      // initialize
      context = RequestContext.extract(null);
      LuceneIndexAdapter adapter = new LuceneIndexAdapter(context);
      adapter.touch(); // ensures that a proper directory structure exists
      if (this.checkInterrupted()) return;
     
      // obtain the background thread lock,
      // sleep for 10 minutes if busy then try again
      try {
        backgroundLock = adapter.obtainBackgroundLock();
      } catch (LockObtainFailedException lofe) {
        if (this.checkInterrupted()) return;
        try {
          Thread.sleep(10 * 1000);
        } catch (InterruptedException e) {
          throw new IOException(e.toString());
        }
        if (this.checkInterrupted()) return;
        backgroundLock = adapter.obtainBackgroundLock();
      }
     
      // optimize the index
      writer = adapter.newWriter();
      if (this.checkInterrupted()) return;
      writer.optimize();
      adapter.closeWriter(writer);
      writer = null;
     
      // log the summary message
      double dSec = (System.currentTimeMillis() - tStartMillis) / 1000.0;
      StringBuffer msg = new StringBuffer();
      msg.append("Optimization run completed.");
      msg.append(", runtime: ");
      msg.append(Math.round(dSec / 60.0 * 100.0) / 100.0).append(" minutes");
      if (dSec <= 600) {
        msg.append(", ").append(Math.round(dSec * 100.0) / 100.0).append(" seconds");
      }
      LOGGER.info(msg.toString());
     
    } catch (LockObtainFailedException e) {
      LOGGER.log(Level.INFO,"Optimization run aborted, reason: "+e.getMessage());
    } catch (Throwable t) {
      LOGGER.log(Level.SEVERE,"Error optimizing index.",t);
    } finally {
      if (writer != null) {
        try {
          writer.close();
        } catch (Throwable t) {
          LOGGER.log(Level.SEVERE,"Error closing IndexWriter.",t);
        }
      }
      if (backgroundLock != null) {
        try {
          backgroundLock.release();
        } catch (Throwable t) {
          LOGGER.log(Level.WARNING,"Error releasing lock.",t);
        }
      }
      if (context != null) {
View Full Code Here


          boolean wasReleased = false;
          try {
            wlFld.setAccessible(true);
            Object wlObj = wlFld.get(writer);
            if ((wlObj != null) && (wlObj instanceof Lock)) {
              Lock wlLock = (Lock)wlObj;
              wasReleased = !wlLock.isLocked();
              if (!wasReleased) {
                wlLock.release();
                wasReleased = !wlLock.isLocked();
              }
            }
          } catch (Throwable t2) {
            getLogger().log(Level.WARNING,"Unable to forcibly release an abandoned write lock.",t2);
          } finally {
View Full Code Here

   * <br/>The lock must be closed when the background process is complete.
   * @return the obtained lock
   * @throws LockObtainFailedException if the lock wwas not obtained
   */
  public synchronized Lock obtainBackgroundLock() throws LockObtainFailedException {
    Lock lock = null;
    String pfx = "Unable to obtain background lock.";
    if (this.luceneConfig.getUseNativeFSLockFactory()) {
      try {
        NativeFSLockFactory nativeLockFactory = this.getNativeLockFactory();
        lock = nativeLockFactory.makeLock(BACKGROUND_LOCKNAME);
      } catch (IOException e) {
        String msg = pfx+" "+e.getMessage();
        getLogger().log(Level.WARNING,pfx,e);
        throw new LockObtainFailedException(msg);
      }
    } else {
      try {
        File fDir = new File(this.luceneConfig.getIndexLocation());
        SimpleFSLockFactory factory = new SimpleFSLockFactory(fDir);
        factory.setLockPrefix("lucene-simple");
        lock = factory.makeLock(BACKGROUND_LOCKNAME);
      } catch (IOException e) {
        String msg = pfx+" "+e.getMessage();
        getLogger().log(Level.WARNING,pfx,e);
        throw new LockObtainFailedException(msg);
      }
    }
    try {
      boolean wasObtained = lock.obtain();
      if (!wasObtained) {
        String msg = "Unable to obtain background lock for: "+lock.toString();
        throw new LockObtainFailedException(msg);
      }
      return lock;
    } catch (LockObtainFailedException e) {
      throw e;
View Full Code Here

   * Run the synchronization process.
   */
  public void run() {
    LOGGER.info("AGSSynchronizer run started...");
    RequestContext rContext = null;
    Lock backgroundLock = null;
    long tStartMillis = System.currentTimeMillis();
    try {
     
      // initialize
      String restUrl = "";
      String soapUrl = "";
      PublicationRecord template = new PublicationRecord();
      if (this.parameters != null) {
        restUrl = Val.chkStr(this.parameters.getValue("restUrl"));
        soapUrl = Val.chkStr(this.parameters.getValue("soapUrl"));
        template.setAutoApprove(
            Val.chkStr(this.parameters.getValue("autoApprove")).equalsIgnoreCase("true"));
        template.setUpdateOnlyIfXmlHasChanged(
            Val.chkStr(this.parameters.getValue("updateOnlyIfXmlHasChanged")).equalsIgnoreCase("true"));
      }
      if (restUrl.length() == 0) {
        LOGGER.log(Level.SEVERE,"AGSSynchronizer run aborted: the restUrl parameter was empty.");
        return;
      }
      if (soapUrl.length() == 0) {
        LOGGER.log(Level.SEVERE,"AGSSynchronizer run aborted: the soapUrl parameter was empty.");
        return;
      }
     
      // obtain the background thread lock,
      // sleep for 10 minutes if busy then try again
      rContext = RequestContext.extract(null);
      LuceneIndexAdapter adapter = new LuceneIndexAdapter(rContext);
      adapter.touch(); // ensures that a proper directory structure exists
      try {
        backgroundLock = adapter.obtainBackgroundLock();
      } catch (LockObtainFailedException lofe) {
        if (Thread.currentThread().isInterrupted()) return;
        try {
          Thread.sleep(10 * 1000);
        } catch (InterruptedException e) {
          throw new IOException(e.toString());
        }
        if (Thread.currentThread().isInterrupted()) return;
        backgroundLock = adapter.obtainBackgroundLock();
      }
      if (Thread.currentThread().isInterrupted()) return;
     
      // process services on the ArcGIS server
      StringBuilder sbSummary = new StringBuilder();
      Publisher publisher = Publisher.makeSystemAdministrator(rContext);
      HttpClientRequest httpClient = HttpClientRequest.newRequest();
     
      ProcessingContext pContext = new ProcessingContext(rContext,publisher,httpClient,template,false);
      AGSProcessor ags = new AGSProcessor(pContext);
      ags.getTarget().setRestUrl(restUrl);
      ags.getTarget().setSoapUrl(soapUrl);
      ags.getTarget().setTargetUrl(restUrl);
      ags.getTarget().setTargetType(AGSTarget.TargetType.ROOT);
      if (!Thread.currentThread().isInterrupted()) {
        ags.process();
      }
      sbSummary.append("\n numCreated=").append(pContext.getNumberCreated());
      sbSummary.append(", numReplaced=").append(pContext.getNumberReplaced());
      sbSummary.append(", numUnchanged=").append(pContext.getNumberUnchanged());
      sbSummary.append(", numDeleted=").append(pContext.getNumberDeleted());
      sbSummary.append(", numFailed=").append(pContext.getNumberFailed());
           
      // log a summary message
      double dSec = (System.currentTimeMillis() - tStartMillis) / 1000.0;
      StringBuilder msg = new StringBuilder();
      msg.append("AGSSynchronizer run completed.");
      msg.append("\n restUrl=").append(restUrl);
      msg.append("\n soapUrl=").append(soapUrl);
      msg.append(sbSummary.toString());
      msg.append("\n wasInterrupted=").append(Thread.currentThread().isInterrupted());
      msg.append(", runtime: ");
      msg.append(Math.round(dSec / 60.0 * 100.0) / 100.0).append(" minutes");
      if (dSec <= 600) {
        msg.append(", ").append(Math.round(dSec * 100.0) / 100.0).append(" seconds");
      }
      LOGGER.info(msg.toString());
     
    } catch (ImsServiceException e) {
      LOGGER.log(Level.SEVERE,"Deletion error.",e);
    } catch (CatalogIndexException e) {
      LOGGER.log(Level.SEVERE,"Catalog index error.",e);
    } catch (SQLException e) {
      LOGGER.log(Level.SEVERE,"Database error.",e);
    } catch (Exception e) {
      LOGGER.log(Level.SEVERE,"Unknown error.",e);
     
    } finally {
      if (backgroundLock != null) {
        try {
          backgroundLock.release();
        } catch (Throwable t) {
          LOGGER.log(Level.WARNING,"Error releasing lock.",t);
        }
      }
      if (rContext != null) {
View Full Code Here

  }

  @Override
  public Lock makeLock(String lockName) {
    final Path lockPath = new Path(_dir, lockName);
    return new Lock() {
      private boolean _set;

      @Override
      public boolean obtain() throws IOException {
        if (_set) {
View Full Code Here

      ensureOpen();
      if (stale)
        throw new StaleReaderException("IndexReader out of date and no longer valid for delete, undelete, or setNorm operations");

      if (writeLock == null) {
        Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME);
        if (!writeLock.obtain(IndexWriterConfig.WRITE_LOCK_TIMEOUT)) // obtain write lock
          throw new LockObtainFailedException("Index locked for write: " + writeLock);
        this.writeLock = writeLock;

        // we have to check whether index has changed since this reader was opened.
        // if so, this reader is no longer valid for
View Full Code Here

                    FileSystemUtils.mkdirs(dir);
                }
                logger.trace("obtaining node lock on {} ...", dir.getAbsolutePath());
                try {
                    NativeFSLockFactory lockFactory = new NativeFSLockFactory(dir.toPath());
                    Lock tmpLock = lockFactory.makeLock("node.lock");
                    boolean obtained = tmpLock.obtain();
                    if (obtained) {
                        locks[dirIndex] = tmpLock;
                        nodesFiles[dirIndex] = dir;
                        localNodeId = possibleLockId;
                    } else {
View Full Code Here

    if (create) {
      // Clear the write lock in case it's leftover:
      directory.clearLock(WRITE_LOCK_NAME);
    }

    Lock writeLock = directory.makeLock(WRITE_LOCK_NAME);
    if (!writeLock.obtain(writeLockTimeout)) // obtain write lock
      throw new LockObtainFailedException("Index locked for write: " + writeLock);
    this.writeLock = writeLock;                   // save it

    try {
      if (create) {
View Full Code Here

      ensureOpen();
      if (stale)
        throw new StaleReaderException("IndexReader out of date and no longer valid for delete, undelete, or setNorm operations");

      if (writeLock == null) {
        Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME);
        if (!writeLock.obtain(IndexWriter.WRITE_LOCK_TIMEOUT)) // obtain write lock
          throw new LockObtainFailedException("Index locked for write: " + writeLock);
        this.writeLock = writeLock;

        // we have to check whether index has changed since this reader was opened.
        // if so, this reader is no longer valid for deletion
View Full Code Here

    method will result in an error.  The presence of this document may still be
    reflected in the {@link #docFreq} statistic, though
    this will be corrected eventually as the index is further modified.  */
  public synchronized final void delete(int docNum) throws IOException {
    if (writeLock == null) {
      Lock writeLock = directory.makeLock("write.lock");
      if (!writeLock.obtain())        // obtain write lock
        throw new IOException("Index locked for write: " + writeLock);
      this.writeLock = writeLock;
    }
    doDelete(docNum);
  }
View Full Code Here

TOP

Related Classes of org.apache.lucene.store.Lock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.