Ejemplo n.º 1
0
  private void buildIndex(
      final List<GeoEntry> geoEntryList,
      final boolean create,
      final ProgressCallback progressCallback) {
    Directory directory;

    try {
      directory = FSDirectory.open(Paths.get(indexLocation));
    } catch (IOException e) {
      throw new GeoEntryIndexingException(
          "Couldn't open the directory for the index, " + indexLocation, e);
    }

    // Try-with-resources to ensure the IndexWriter always gets closed.
    try (final IndexWriter indexWriter = createIndexWriter(create, directory)) {
      try {
        indexGeoEntries(indexWriter, geoEntryList, progressCallback);
      } catch (IOException e) {
        // Need to roll back here before the IndexWriter is closed at the end of the try
        // block.
        indexWriter.rollback();
        throw e;
      }
    } catch (IOException e) {
      throw new GeoEntryIndexingException("Error writing to the index.", e);
    }
  }
Ejemplo n.º 2
0
 @Override
 public void close() throws ElasticSearchException {
   if (closed) {
     return;
   }
   closed = true;
   rwl.writeLock().lock();
   try {
     if (nrtResource != null) {
       this.nrtResource.forceClose();
     }
     // no need to commit in this case!, we snapshot before we close the shard, so translog and all
     // sync'ed
     if (indexWriter != null) {
       try {
         indexWriter.rollback();
       } catch (AlreadyClosedException e) {
         // ignore
       }
     }
   } catch (IOException e) {
     logger.debug("failed to rollback writer on close", e);
   } finally {
     indexWriter = null;
     rwl.writeLock().unlock();
   }
 }
Ejemplo n.º 3
0
 /**
  * Closes the engine without acquiring the write lock. This should only be called while the write
  * lock is hold or in a disaster condition ie. if the engine is failed.
  */
 @Override
 protected final void closeNoLock(String reason) {
   if (isClosed.compareAndSet(false, true)) {
     assert rwl.isWriteLockedByCurrentThread() || failEngineLock.isHeldByCurrentThread()
         : "Either the write lock must be held or the engine must be currently be failing itself";
     try {
       this.versionMap.clear();
       try {
         IOUtils.close(searcherManager);
       } catch (Throwable t) {
         logger.warn("Failed to close SearcherManager", t);
       }
       try {
         IOUtils.close(translog);
       } catch (Throwable t) {
         logger.warn("Failed to close translog", t);
       }
       // no need to commit in this case!, we snapshot before we close the shard, so translog and
       // all sync'ed
       logger.trace("rollback indexWriter");
       try {
         indexWriter.rollback();
       } catch (AlreadyClosedException e) {
         // ignore
       }
       logger.trace("rollback indexWriter done");
     } catch (Throwable e) {
       logger.warn("failed to rollback writer on close", e);
     } finally {
       store.decRef();
       logger.debug("engine closed [{}]", reason);
     }
   }
 }
Ejemplo n.º 4
0
 private SearcherManager createSearcherManager() throws EngineException {
   boolean success = false;
   SearcherManager searcherManager = null;
   try {
     try {
       final DirectoryReader directoryReader =
           ElasticsearchDirectoryReader.wrap(DirectoryReader.open(indexWriter), shardId);
       searcherManager = new SearcherManager(directoryReader, searcherFactory);
       lastCommittedSegmentInfos = readLastCommittedSegmentInfos(searcherManager, store);
       success = true;
       return searcherManager;
     } catch (IOException e) {
       maybeFailEngine("start", e);
       try {
         indexWriter.rollback();
       } catch (IOException e1) { // iw is closed below
         e.addSuppressed(e1);
       }
       throw new EngineCreationFailureException(shardId, "failed to open reader on writer", e);
     }
   } finally {
     if (success == false) { // release everything we created on a failure
       IOUtils.closeWhileHandlingException(searcherManager, indexWriter);
     }
   }
 }
Ejemplo n.º 5
0
 private void indexDoc(File doc) {
   if (doc.exists() && doc.canRead()) {
     IndexWriter indexWriter = null;
     try {
       indexWriter = new IndexWriter(this.dir, this.iwc);
       this.indexDocs(indexWriter, doc);
       indexWriter.commit();
     } catch (IOException e) {
       if (indexWriter != null) {
         try {
           indexWriter.rollback();
         } catch (IOException e2) {
           // TODO Auto-generated catch block
           e2.printStackTrace();
         }
       }
       // TODO Auto-generated catch block
       e.printStackTrace();
     } finally {
       if (indexWriter != null) {
         try {
           indexWriter.close();
         } catch (IOException e2) {
           // TODO Auto-generated catch block
           e2.printStackTrace();
         }
       }
     }
   }
 }
Ejemplo n.º 6
0
  @Override
  public void updateIndex(
      final String resource,
      final GeoEntryExtractor geoEntryExtractor,
      final boolean create,
      final ProgressCallback progressCallback) {
    Directory directory;

    try {
      directory = FSDirectory.open(Paths.get(indexLocation));
    } catch (IOException e) {
      throw new GeoEntryIndexingException(
          "Couldn't open the directory for the index, " + indexLocation, e);
    }

    // Try-with-resources to ensure the IndexWriter always gets closed.
    try (final IndexWriter indexWriter = createIndexWriter(create, directory)) {
      final ExtractionCallback extractionCallback =
          new ExtractionCallback() {
            @Override
            public void extracted(final GeoEntry newEntry) {
              try {
                addDocument(indexWriter, newEntry);
              } catch (IOException e) {
                throw new GeoEntryIndexingException("Error writing to the index.", e);
              }
            }

            @Override
            public void updateProgress(final int progress) {
              if (progressCallback != null) {
                progressCallback.updateProgress(progress);
              }
            }
          };

      try {
        geoEntryExtractor.getGeoEntriesStreaming(resource, extractionCallback);
      } catch (GeoEntryExtractionException | GeoEntryIndexingException e) {
        // Need to roll back here before the IndexWriter is closed at the end of the try
        // block.
        indexWriter.rollback();
        throw e;
      }
    } catch (IOException e) {
      throw new GeoEntryIndexingException("Error writing to the index.", e);
    }
  }
Ejemplo n.º 7
0
 @Override
 public OperationResponse rollback() {
   try {
     if (logger.isDebugEnabled()) {
       logger.debug("rollbacking...");
     }
     indexWriter.rollback();
     if (logger.isDebugEnabled()) {
       logger.debug("rollback finish.");
     }
   } catch (IOException e) {
     logger.error("rollback error", e);
     return new OperationResponse(e.getMessage(), ResultCodes.COMMON_ERROR);
   }
   return new OperationResponse();
 }
Ejemplo n.º 8
0
  @Override
  public void start() throws EngineException {
    rwl.writeLock().lock();
    try {
      if (indexWriter != null) {
        throw new EngineAlreadyStartedException(shardId);
      }
      if (logger.isDebugEnabled()) {
        logger.debug(
            "Starting engine with ram_buffer_size["
                + indexingBufferSize
                + "], refresh_interval["
                + refreshInterval
                + "]");
      }
      try {
        this.indexWriter = createWriter();
      } catch (IOException e) {
        throw new EngineCreationFailureException(shardId, "Failed to create engine", e);
      }

      try {
        translog.newTranslog(newTransactionLogId());
        this.nrtResource = buildNrtResource(indexWriter);
      } catch (IOException e) {
        try {
          indexWriter.rollback();
        } catch (IOException e1) {
          // ignore
        } finally {
          try {
            indexWriter.close();
          } catch (IOException e1) {
            // ignore
          }
        }
        throw new EngineCreationFailureException(shardId, "Failed to open reader on writer", e);
      }
    } finally {
      rwl.writeLock().unlock();
    }
  }