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); } } }
/** * Opens an input stream to this location. * * <p>Please note that it will try to obtain a stream from every storage until a storage returns * one. * * @return The input-stream to this location. */ @Override public InputStream getInputStream() throws IOException { // Sine we are going to suppress all unhandles IOExceptions and only throw them // if we were unable to get a stream, we will access the location. List<IOException> exceptionList = new ArrayList<IOException>(); // Try all locations but try the write-storage first. for (ConfigurationStorage cs : ArrayUtils.add(this.storage.getChildStorage(), 0, this.storage.getWriteStorage())) { // Try to access the storage location. InputStream is = null; try { is = cs.getInputStream(this.module, this.configuration); } catch (IOException e) { // Just suppress IOExceptions. exceptionList.add(e); } // If we were able to access the location, return the stream. if (is != null) return is; } if (exceptionList.size() == 0) return null; else if (exceptionList.size() == 1) { // Just rethrow the only one that occured. throw exceptionList.get(0); } else { // Throw all suppressed exceptions. IOException exception = new IOException( "Failed to obtain the stream. The following exceptions have been suppressed."); for (IOException e : exceptionList) exception.addSuppressed(e); throw exception; } }
/** * Ensures configured directory {@code path} exists. * * @throws IOException if {@code path} exists, but is not a directory, not accessible, or broken * symbolic link. */ static void ensureDirectoryExists(Path path) throws IOException { // this isn't atomic, but neither is createDirectories. if (Files.isDirectory(path)) { // verify access, following links (throws exception if something is wrong) // we only check READ as a sanity test path.getFileSystem().provider().checkAccess(path.toRealPath(), AccessMode.READ); } else { // doesn't exist, or not a directory try { Files.createDirectories(path); } catch (FileAlreadyExistsException e) { // convert optional specific exception so the context is clear IOException e2 = new NotDirectoryException(path.toString()); e2.addSuppressed(e); throw e2; } } }
@Override public void close() throws IOException { IOException e = null; for (InputStream stream : opened) { try { stream.close(); } catch (IOException ex) { if (e == null) { e = ex; } else { e.addSuppressed(ex); } } } if (e != null) { throw e; } }
@Override public DatagramChannel create() throws ObjectCreationException { DatagramChannel datagramChannel; try { datagramChannel = DatagramChannel.open(); } catch (IOException ex) { throw new ObjectCreationException(ex); } try { datagramChannel.connect(address); return datagramChannel; } catch (IOException ex) { try { datagramChannel.close(); } catch (IOException ex1) { ex1.addSuppressed(ex); throw new ObjectCreationException(ex1); } throw new ObjectCreationException(ex); } }
@Override public void close() throws IOException { IOException cause = null; for (DataStoreTransaction transaction : transactions.values()) { try { transaction.close(); } catch (IOException | Error | RuntimeException e) { if (cause != null) { cause.addSuppressed(e); } else if (e instanceof IOException) { cause = (IOException) e; } else { cause = new IOException(e); } } } transactions.clear(); if (cause != null) { throw cause; } }