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

The following examples show how to use org.apache.lucene.util.Constants. 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: Elasticsearch   Source File: Environment.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the path is writable.
 * Acts just like {@link Files#isWritable(Path)}, except won't
 * falsely return false for paths on SUBST'd drive letters
 * See https://bugs.openjdk.java.net/browse/JDK-8034057
 * Note this will set the file modification time (to its already-set value)
 * to test access.
 */
@SuppressForbidden(reason = "works around https://bugs.openjdk.java.net/browse/JDK-8034057")
public static boolean isWritable(Path path) throws IOException {
    boolean v = Files.isWritable(path);
    if (v || Constants.WINDOWS == false) {
        return v;
    }

    // isWritable returned false on windows, the hack begins!!!!!!
    // resetting the modification time is the least destructive/simplest
    // way to check for both files and directories, and fails early just
    // in getting the current value if file doesn't exist, etc
    try {
        Files.setLastModifiedTime(path, Files.getLastModifiedTime(path));
        return true;
    } catch (Throwable e) {
        return false;
    }
}
 
Example 2
Source Project: Elasticsearch   Source File: ESFileStore.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressForbidden(reason = "tries to determine if disk is spinning")
// TODO: move PathUtils to be package-private here instead of 
// public+forbidden api!
ESFileStore(FileStore in) {
    this.in = in;
    Boolean spins;
    // Lucene's IOUtils.spins only works on Linux today:
    if (Constants.LINUX) {
        try {
            spins = IOUtils.spins(PathUtils.get(getMountPointLinux(in)));
        } catch (Exception e) {
            spins = null;
        }
    } else {
        spins = null;
    }
    this.spins = spins;
}
 
Example 3
Source Project: Elasticsearch   Source File: FsDirectoryService.java    License: Apache License 2.0 6 votes vote down vote up
protected Directory newFSDirectory(Path location, LockFactory lockFactory) throws IOException {
    final String storeType = indexSettings.get(IndexStoreModule.STORE_TYPE, IndexStoreModule.Type.DEFAULT.getSettingsKey());
    if (IndexStoreModule.Type.FS.match(storeType) || IndexStoreModule.Type.DEFAULT.match(storeType)) {
        final FSDirectory open = FSDirectory.open(location, lockFactory); // use lucene defaults
        if (open instanceof MMapDirectory && Constants.WINDOWS == false) {
            return newDefaultDir(location, (MMapDirectory) open, lockFactory);
        }
        return open;
    } else if (IndexStoreModule.Type.SIMPLEFS.match(storeType)) {
        return new SimpleFSDirectory(location, lockFactory);
    } else if (IndexStoreModule.Type.NIOFS.match(storeType)) {
        return new NIOFSDirectory(location, lockFactory);
    } else if (IndexStoreModule.Type.MMAPFS.match(storeType)) {
        return new MMapDirectory(location, lockFactory);
    }
    throw new IllegalArgumentException("No directory found for type [" + storeType + "]");
}
 
Example 4
Source Project: Elasticsearch   Source File: Seccomp.java    License: Apache License 2.0 6 votes vote down vote up
static void solarisImpl() {
    // first be defensive: we can give nice errors this way, at the very least.
    boolean supported = Constants.SUN_OS;
    if (supported == false) {
        throw new IllegalStateException("bug: should not be trying to initialize priv_set for an unsupported OS");
    }

    // we couldn't link methods, could be some really ancient Solaris or some bug
    if (libc_solaris == null) {
        throw new UnsupportedOperationException("priv_set unavailable: could not link methods. requires Solaris 10+");
    }

    // drop a null-terminated list of privileges 
    if (libc_solaris.priv_set(PRIV_OFF, PRIV_ALLSETS, PRIV_PROC_FORK, PRIV_PROC_EXEC, null) != 0) {
        throw new UnsupportedOperationException("priv_set unavailable: priv_set(): " + JNACLibrary.strerror(Native.getLastError()));
    }

    logger.debug("Solaris priv_set initialization successful");
}
 
Example 5
Source Project: Elasticsearch   Source File: Seccomp.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Attempt to drop the capability to execute for the process.
 * <p>
 * This is best effort and OS and architecture dependent. It may throw any Throwable.
 * @return 0 if we can do this for application threads, 1 for the entire process
 */
static int init(Path tmpFile) throws Throwable {
    if (Constants.LINUX) {
        return linuxImpl();
    } else if (Constants.MAC_OS_X) {
        // try to enable both mechanisms if possible
        bsdImpl();
        macImpl(tmpFile);
        return 1;
    } else if (Constants.SUN_OS) {
        solarisImpl();
        return 1;
    } else if (Constants.FREE_BSD || OPENBSD) {
        bsdImpl();
        return 1;
    } else if (Constants.WINDOWS) {
        windowsImpl();
        return 1;
    } else {
        throw new UnsupportedOperationException("syscall filtering not supported for OS: '" + Constants.OS_NAME + "'");
    }
}
 
Example 6
Source Project: lucene-solr   Source File: MockFileSystemTestCase.java    License: Apache License 2.0 6 votes vote down vote up
private void implTestURI(String fileName) throws IOException {
  assumeFalse("broken on J9: see https://issues.apache.org/jira/browse/LUCENE-6517", Constants.JAVA_VENDOR.startsWith("IBM"));
  Path dir = wrap(createTempDir());

  Path f1 = null;
  try {
    f1 = dir.resolve(fileName);
  } catch (InvalidPathException ipe) {
    assumeNoException("couldn't resolve '"+fileName+"'", ipe);
  }

  URI uri = f1.toUri();
  Path f2 = dir.getFileSystem().provider().getPath(uri);
  assertEquals(f1, f2);

  dir.getFileSystem().close();
}
 
Example 7
Source Project: lucene-solr   Source File: SolrTestCaseJ4.java    License: Apache License 2.0 6 votes vote down vote up
private static SSLTestConfig buildSSLConfig() {

    SSLRandomizer sslRandomizer =
      SSLRandomizer.getSSLRandomizerForClass(RandomizedContext.current().getTargetClass());
    
    if (Constants.MAC_OS_X) {
      // see SOLR-9039
      // If a solution is found to remove this, please make sure to also update
      // TestMiniSolrCloudClusterSSL.testSslAndClientAuth as well.
      sslRandomizer = new SSLRandomizer(sslRandomizer.ssl, 0.0D, (sslRandomizer.debug + " w/ MAC_OS_X supressed clientAuth"));
    }

    SSLTestConfig result = sslRandomizer.createSSLTestConfig();
    if (log.isInfoEnabled()) {
      log.info("Randomized ssl ({}) and clientAuth ({}) via: {}",
          result.isSSLMode(), result.isClientAuthMode(), sslRandomizer.debug);
    }
    return result;
  }
 
Example 8
Source Project: lucene-solr   Source File: ImpersonationUtil.java    License: Apache License 2.0 6 votes vote down vote up
static String getUsersFirstGroup() throws Exception {
  String group = "*"; // accept any group if a group can't be found
  if (!Constants.WINDOWS) { // does not work on Windows!
    org.apache.hadoop.security.Groups hGroups =
        new org.apache.hadoop.security.Groups(new Configuration());
    try {
      List<String> g = hGroups.getGroups(System.getProperty("user.name"));
      if (g != null && g.size() > 0) {
        group = g.get(0);
      }
    } catch (NullPointerException npe) {
      // if user/group doesn't exist on test box
    }
  }
  return group;
}
 
Example 9
Source Project: lucene-solr   Source File: SoftAutoCommitTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void createMonitor() throws Exception {
  assumeFalse("This test is not working on Windows (or maybe machines with only 2 CPUs)",
    Constants.WINDOWS);

  SolrCore core = h.getCore();

  updater = (DirectUpdateHandler2) core.getUpdateHandler();
  updater.setCommitWithinSoftCommit(true); // foce to default, let tests change as needed
  monitor = new MockEventListener();

  core.registerNewSearcherListener(monitor);
  updater.registerSoftCommitCallback(monitor);
  updater.registerCommitCallback(monitor);

  // isolate searcher getting ready from this test
  monitor.searcher.poll(5000, MILLISECONDS);
}
 
Example 10
Source Project: lucene-solr   Source File: TestSolrDeletionPolicy1.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCommitAge() throws InterruptedException {
  assumeFalse("This test is not working on Windows (or maybe machines with only 2 CPUs)",
    Constants.WINDOWS);

  IndexDeletionPolicyWrapper delPolicy = h.getCore().getDeletionPolicy();
  addDocs();
  Map<Long, IndexCommit> commits = delPolicy.getCommits();
  IndexCommit ic = delPolicy.getLatestCommit();
  String agestr = ((SolrDeletionPolicy) (delPolicy.getWrappedDeletionPolicy())).getMaxCommitAge().replaceAll("[a-zA-Z]", "").replaceAll("-", "");
  long age = Long.parseLong(agestr);
  Thread.sleep(age);

  assertU(adoc("id", String.valueOf(6),
          "name", "name" + String.valueOf(6)));
  assertU(optimize());
  assertQ("return all docs",
          req("id:[0 TO 6]"),
          "*[count(//doc)=6]"
  );

  commits = delPolicy.getCommits();
  assertTrue(!commits.containsKey(ic.getGeneration()));
}
 
Example 11
Source Project: crate   Source File: Node.java    License: Apache License 2.0 6 votes vote down vote up
private void logVersion(Logger logger, JvmInfo jvmInfo) {
    logger.info(
        "version[{}], pid[{}], build[{}/{}], OS[{}/{}/{}], JVM[{}/{}/{}/{}]",
        Version.displayVersion(Version.CURRENT, Version.CURRENT.isSnapshot()),
        jvmInfo.pid(),
        Build.CURRENT.hashShort(),
        Build.CURRENT.timestamp(),
        Constants.OS_NAME,
        Constants.OS_VERSION,
        Constants.OS_ARCH,
        Constants.JVM_VENDOR,
        Constants.JVM_NAME,
        Constants.JAVA_VERSION,
        Constants.JVM_VERSION);
    warnIfPreRelease(Version.CURRENT, Version.CURRENT.isSnapshot(), logger);
}
 
Example 12
Source Project: crate   Source File: JNANatives.java    License: Apache License 2.0 6 votes vote down vote up
static void trySetMaxNumberOfThreads() {
    if (Constants.LINUX) {
        // this is only valid on Linux and the value *is* different on OS X
        // see /usr/include/sys/resource.h on OS X
        // on Linux the resource RLIMIT_NPROC means *the number of threads*
        // this is in opposition to BSD-derived OSes
        final int rlimit_nproc = 6;

        final JNACLibrary.Rlimit rlimit = new JNACLibrary.Rlimit();
        if (JNACLibrary.getrlimit(rlimit_nproc, rlimit) == 0) {
            MAX_NUMBER_OF_THREADS = rlimit.rlim_cur.longValue();
        } else {
            LOGGER.warn("unable to retrieve max number of threads [" + JNACLibrary.strerror(Native.getLastError()) + "]");
        }
    }
}
 
Example 13
Source Project: crate   Source File: ReproduceInfoPrinter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void testFailure(Failure failure) throws Exception {
    // Ignore assumptions.
    if (failure.getException() instanceof AssumptionViolatedException) {
        return;
    }

    final String gradlew = Constants.WINDOWS ? "gradlew" : "./gradlew";
    final StringBuilder b = new StringBuilder("REPRODUCE WITH: " + gradlew + " ");
    String task = System.getProperty("tests.task");
    // TODO: enforce (intellij still runs the runner?) or use default "test" but that won't work for integ
    b.append(task);

    GradleMessageBuilder gradleMessageBuilder = new GradleMessageBuilder(b);
    gradleMessageBuilder.appendAllOpts(failure.getDescription());

    printToErr(b.toString());
}
 
Example 14
Source Project: crate   Source File: NodeStatsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSysNodsOsInfo() throws Exception {
    SQLResponse response = execute("select os_info from sys.nodes limit 1");
    Map results = (Map) response.rows()[0][0];
    assertThat(response.rowCount(), is(1L));

    assertThat((Integer) results.get("available_processors"), greaterThan(0));
    assertEquals(Constants.OS_NAME, results.get("name"));
    assertEquals(Constants.OS_ARCH, results.get("arch"));
    assertEquals(Constants.OS_VERSION, results.get("version"));

    Map<String, Object> jvmObj = new HashMap<>(4);
    jvmObj.put("version", Constants.JAVA_VERSION);
    jvmObj.put("vm_name", Constants.JVM_NAME);
    jvmObj.put("vm_vendor", Constants.JVM_VENDOR);
    jvmObj.put("vm_version", Constants.JVM_VERSION);
    assertEquals(jvmObj, results.get("jvm"));
}
 
Example 15
@Test
public void testArithmeticFunctions() throws Exception {
    execute("select ((2 * 4 - 2 + 1) / 2) % 3 from sys.cluster");
    assertThat(response.cols()[0], is("0"));
    assertThat(response.rows()[0][0], is(0));

    execute("select ((2 * 4.0 - 2 + 1) / 2) % 3 from sys.cluster");
    assertThat(response.rows()[0][0], is(0.5));

    execute("select ? + 2 from sys.cluster", $(1));
    assertThat(response.rows()[0][0], is(3));

    if (!Constants.WINDOWS) {
        response = execute("select load['1'] + load['5'], load['1'], load['5'] from sys.nodes limit 1");
        assertEquals(response.rows()[0][0], (Double) response.rows()[0][1] + (Double) response.rows()[0][2]);
    }
}
 
Example 16
Source Project: Elasticsearch   Source File: OsProbe.java    License: Apache License 2.0 5 votes vote down vote up
public OsInfo osInfo() {
    OsInfo info = new OsInfo();
    info.availableProcessors = Runtime.getRuntime().availableProcessors();
    info.name = Constants.OS_NAME;
    info.arch = Constants.OS_ARCH;
    info.version = Constants.OS_VERSION;
    return info;
}
 
Example 17
Source Project: Elasticsearch   Source File: JNANatives.java    License: Apache License 2.0 5 votes vote down vote up
/** Returns true if user is root, false if not, or if we don't know */
static boolean definitelyRunningAsRoot() {
    if (Constants.WINDOWS) {
        return false; // don't know
    }
    try {
        return JNACLibrary.geteuid() == 0;
    } catch (UnsatisfiedLinkError e) {
        // this will have already been logged by Kernel32Library, no need to repeat it
        return false;
    }
}
 
Example 18
Source Project: Elasticsearch   Source File: JNANatives.java    License: Apache License 2.0 5 votes vote down vote up
static void addConsoleCtrlHandler(ConsoleCtrlHandler handler) {
    // The console Ctrl handler is necessary on Windows platforms only.
    if (Constants.WINDOWS) {
        try {
            boolean result = JNAKernel32Library.getInstance().addConsoleCtrlHandler(handler);
            if (result) {
                logger.debug("console ctrl handler correctly set");
            } else {
                logger.warn("unknown error " + Native.getLastError() + " when adding console ctrl handler:");
            }
        } catch (UnsatisfiedLinkError e) {
            // this will have already been logged by Kernel32Library, no need to repeat it
        }
    }
}
 
Example 19
Source Project: Elasticsearch   Source File: JVMCheck.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Checks that the current JVM is "ok". This means it doesn't have severe bugs that cause data corruption.
 */
static void check() {
    if (Boolean.parseBoolean(System.getProperty(JVM_BYPASS))) {
        Loggers.getLogger(JVMCheck.class).warn("bypassing jvm version check for version [{}], this can result in data corruption!", fullVersion());
    } else if ("Oracle Corporation".equals(Constants.JVM_VENDOR)) {
        HotspotBug bug = JVM_BROKEN_HOTSPOT_VERSIONS.get(Constants.JVM_VERSION);
        if (bug != null) {
            if (bug.workAround != null && ManagementFactory.getRuntimeMXBean().getInputArguments().contains(bug.workAround)) {
                Loggers.getLogger(JVMCheck.class).warn(bug.getWarningMessage());
            } else {
                throw new RuntimeException(bug.getErrorMessage());
            }
        }
    } else if ("IBM Corporation".equals(Constants.JVM_VENDOR)) {
        // currently some old JVM versions from IBM will easily result in index corruption.
        // 2.8+ seems ok for ES from testing.
        float version = Float.POSITIVE_INFINITY;
        try {
            version = Float.parseFloat(Constants.JVM_VERSION);
        } catch (NumberFormatException ignored) {
            // this is just a simple best-effort to detect old runtimes,
            // if we cannot parse it, we don't fail.
        }
        if (version < 2.8f) {
            StringBuilder sb = new StringBuilder();
            sb.append("IBM J9 runtimes < 2.8 suffer from several bugs which can cause data corruption.");
            sb.append(System.lineSeparator());
            sb.append("Your version: " + fullVersion());
            sb.append(System.lineSeparator());
            sb.append("Please upgrade the JVM to a recent IBM JDK");
            throw new RuntimeException(sb.toString());
        }
    }
}
 
Example 20
Source Project: Elasticsearch   Source File: JVMCheck.java    License: Apache License 2.0 5 votes vote down vote up
/** 
 * Returns java + jvm version, looks like this:
 * {@code Oracle Corporation 1.8.0_45 [Java HotSpot(TM) 64-Bit Server VM 25.45-b02]}
 */
static String fullVersion() {
    StringBuilder sb = new StringBuilder();
    sb.append(Constants.JAVA_VENDOR);
    sb.append(" ");
    sb.append(Constants.JAVA_VERSION);
    sb.append(" [");
    sb.append(Constants.JVM_NAME);
    sb.append(" ");
    sb.append(Constants.JVM_VERSION);
    sb.append("]");
    return sb.toString();
}
 
Example 21
Source Project: Elasticsearch   Source File: Seccomp.java    License: Apache License 2.0 5 votes vote down vote up
/** try to install our custom rule profile into sandbox_init() to block execution */
private static void macImpl(Path tmpFile) throws IOException {
    // first be defensive: we can give nice errors this way, at the very least.
    boolean supported = Constants.MAC_OS_X;
    if (supported == false) {
        throw new IllegalStateException("bug: should not be trying to initialize seatbelt for an unsupported OS");
    }

    // we couldn't link methods, could be some really ancient OS X (< Leopard) or some bug
    if (libc_mac == null) {
        throw new UnsupportedOperationException("seatbelt unavailable: could not link methods. requires Leopard or above.");
    }

    // write rules to a temporary file, which will be passed to sandbox_init()
    Path rules = Files.createTempFile(tmpFile, "es", "sb");
    Files.write(rules, Collections.singleton(SANDBOX_RULES), StandardCharsets.UTF_8);

    boolean success = false;
    try {
        PointerByReference errorRef = new PointerByReference();
        int ret = libc_mac.sandbox_init(rules.toAbsolutePath().toString(), SANDBOX_NAMED, errorRef);
        // if sandbox_init() fails, add the message from the OS (e.g. syntax error) and free the buffer
        if (ret != 0) {
            Pointer errorBuf = errorRef.getValue();
            RuntimeException e = new UnsupportedOperationException("sandbox_init(): " + errorBuf.getString(0));
            libc_mac.sandbox_free_error(errorBuf);
            throw e;
        }
        logger.debug("OS X seatbelt initialization successful");
        success = true;
    } finally {
        if (success) {
            Files.delete(rules);
        } else {
            IOUtils.deleteFilesIgnoringExceptions(rules);
        }
    }
}
 
Example 22
Source Project: Elasticsearch   Source File: Seccomp.java    License: Apache License 2.0 5 votes vote down vote up
static void bsdImpl() {
    boolean supported = Constants.FREE_BSD || OPENBSD || Constants.MAC_OS_X;
    if (supported == false) {
        throw new IllegalStateException("bug: should not be trying to initialize RLIMIT_NPROC for an unsupported OS");
    }

    JNACLibrary.Rlimit limit = new JNACLibrary.Rlimit();
    limit.rlim_cur.setValue(0);
    limit.rlim_max.setValue(0);
    if (JNACLibrary.setrlimit(RLIMIT_NPROC, limit) != 0) {
        throw new UnsupportedOperationException("RLIMIT_NPROC unavailable: " + JNACLibrary.strerror(Native.getLastError()));
    }

    logger.debug("BSD RLIMIT_NPROC initialization successful");
}
 
Example 23
Source Project: Elasticsearch   Source File: Seccomp.java    License: Apache License 2.0 5 votes vote down vote up
static void windowsImpl() {
    if (!Constants.WINDOWS) {
        throw new IllegalStateException("bug: should not be trying to initialize ActiveProcessLimit for an unsupported OS");
    }

    JNAKernel32Library lib = JNAKernel32Library.getInstance();

    // create a new Job
    Pointer job = lib.CreateJobObjectW(null, null);
    if (job == null) {
        throw new UnsupportedOperationException("CreateJobObject: " + Native.getLastError());
    }

    try {
        // retrieve the current basic limits of the job
        int clazz = JNAKernel32Library.JOBOBJECT_BASIC_LIMIT_INFORMATION_CLASS;
        JNAKernel32Library.JOBOBJECT_BASIC_LIMIT_INFORMATION limits = new JNAKernel32Library.JOBOBJECT_BASIC_LIMIT_INFORMATION();
        limits.write();
        if (!lib.QueryInformationJobObject(job, clazz, limits.getPointer(), limits.size(), null)) {
            throw new UnsupportedOperationException("QueryInformationJobObject: " + Native.getLastError());
        }
        limits.read();
        // modify the number of active processes to be 1 (exactly the one process we will add to the job).
        limits.ActiveProcessLimit = 1;
        limits.LimitFlags = JNAKernel32Library.JOB_OBJECT_LIMIT_ACTIVE_PROCESS;
        limits.write();
        if (!lib.SetInformationJobObject(job, clazz, limits.getPointer(), limits.size())) {
            throw new UnsupportedOperationException("SetInformationJobObject: " + Native.getLastError());
        }
        // assign ourselves to the job
        if (!lib.AssignProcessToJobObject(job, lib.GetCurrentProcess())) {
            throw new UnsupportedOperationException("AssignProcessToJobObject: " + Native.getLastError());
        }
    } finally {
        lib.CloseHandle(job);
    }

    logger.debug("Windows ActiveProcessLimit initialization successful");
}
 
Example 24
public void testRenameWithHardLink() throws Exception {
  // irony: currently we don't emulate windows well enough to work on windows!
  assumeFalse("windows is not supported", Constants.WINDOWS);
  Path path = createTempDir();
  FileSystem fs = new WindowsFS(path.getFileSystem()).getFileSystem(URI.create("file:///"));
  Directory dir1 = new NIOFSDirectory(new FilterPath(path, fs));
  Directory dir2 = new NIOFSDirectory(new FilterPath(path.resolve("link"), fs));

  IndexOutput target = dir1.createOutput("target.txt", IOContext.DEFAULT);
  target.writeInt(1);
  target.close();

  HardlinkCopyDirectoryWrapper wrapper = new HardlinkCopyDirectoryWrapper(dir2);
  wrapper.copyFrom(dir1, "target.txt", "link.txt", IOContext.DEFAULT);

  IndexOutput source = dir1.createOutput("source.txt", IOContext.DEFAULT);
  source.writeInt(2);
  source.close();

  IndexInput link = dir2.openInput("link.txt", IOContext.DEFAULT);
  // Rename while opening a hard-link file
  dir1.rename("source.txt", "target.txt");
  link.close();

  IndexInput in = dir1.openInput("target.txt", IOContext.DEFAULT);
  assertEquals(2, in.readInt());
  in.close();

  IOUtils.close(dir1, dir2);
}
 
Example 25
Source Project: lucene-solr   Source File: IndexFileDeleter.java    License: Apache License 2.0 5 votes vote down vote up
private void deleteFile(String fileName) throws IOException {
  try {
    directory.deleteFile(fileName);
  } catch (NoSuchFileException | FileNotFoundException e) {
    if (Constants.WINDOWS) {
      // TODO: can we remove this OS-specific hacky logic?  If windows deleteFile is buggy, we should instead contain this workaround in
      // a WindowsFSDirectory ...
      // LUCENE-6684: we suppress this assert for Windows, since a file could be in a confusing "pending delete" state, where we already
      // deleted it once, yet it still shows up in directory listings, and if you try to delete it again you'll hit NSFE/FNFE:
    } else {
      throw e;
    }
  }
}
 
Example 26
Source Project: lucene-solr   Source File: FSDirectory.java    License: Apache License 2.0 5 votes vote down vote up
/** Just like {@link #open(Path)}, but allows you to
 *  also specify a custom {@link LockFactory}. */
public static FSDirectory open(Path path, LockFactory lockFactory) throws IOException {
  if (Constants.JRE_IS_64BIT && MMapDirectory.UNMAP_SUPPORTED) {
    return new MMapDirectory(path, lockFactory);
  } else {
    return new NIOFSDirectory(path, lockFactory);
  }
}
 
Example 27
Source Project: lucene-solr   Source File: FSDirectory.java    License: Apache License 2.0 5 votes vote down vote up
private void privateDeleteFile(String name, boolean isPendingDelete) throws IOException {
  try {
    Files.delete(directory.resolve(name));
    pendingDeletes.remove(name);
  } catch (NoSuchFileException | FileNotFoundException e) {
    // We were asked to delete a non-existent file:
    pendingDeletes.remove(name);
    if (isPendingDelete && Constants.WINDOWS) {
      // TODO: can we remove this OS-specific hacky logic?  If windows deleteFile is buggy, we should instead contain this workaround in
      // a WindowsFSDirectory ...
      // LUCENE-6684: we suppress this check for Windows, since a file could be in a confusing "pending delete" state, failing the first
      // delete attempt with access denied and then apparently falsely failing here when we try ot delete it again, with NSFE/FNFE
    } else {
      throw e;
    }
  } catch (IOException ioe) {
    // On windows, a file delete can fail because there's still an open
    // file handle against it.  We record this in pendingDeletes and
    // try again later.

    // TODO: this is hacky/lenient (we don't know which IOException this is), and
    // it should only happen on filesystems that can do this, so really we should
    // move this logic to WindowsDirectory or something

    // TODO: can/should we do if (Constants.WINDOWS) here, else throw the exc?
    // but what about a Linux box with a CIFS mount?
    pendingDeletes.add(name);
  }
}
 
Example 28
Source Project: lucene-solr   Source File: MMapDirectory.java    License: Apache License 2.0 5 votes vote down vote up
private IOException convertMapFailedIOException(IOException ioe, String resourceDescription, int bufSize) {
  final String originalMessage;
  final Throwable originalCause;
  if (ioe.getCause() instanceof OutOfMemoryError) {
    // nested OOM confuses users, because it's "incorrect", just print a plain message:
    originalMessage = "Map failed";
    originalCause = null;
  } else {
    originalMessage = ioe.getMessage();
    originalCause = ioe.getCause();
  }
  final String moreInfo;
  if (!Constants.JRE_IS_64BIT) {
    moreInfo = "MMapDirectory should only be used on 64bit platforms, because the address space on 32bit operating systems is too small. ";
  } else if (Constants.WINDOWS) {
    moreInfo = "Windows is unfortunately very limited on virtual address space. If your index size is several hundred Gigabytes, consider changing to Linux. ";
  } else if (Constants.LINUX) {
    moreInfo = "Please review 'ulimit -v', 'ulimit -m' (both should return 'unlimited'), and 'sysctl vm.max_map_count'. ";
  } else {
    moreInfo = "Please review 'ulimit -v', 'ulimit -m' (both should return 'unlimited'). ";
  }
  final IOException newIoe = new IOException(String.format(Locale.ENGLISH,
      "%s: %s [this may be caused by lack of enough unfragmented virtual address space "+
      "or too restrictive virtual memory limits enforced by the operating system, "+
      "preventing us to map a chunk of %d bytes. %sMore information: "+
      "http://blog.thetaphi.de/2012/07/use-lucenes-mmapdirectory-on-64bit.html]",
      originalMessage, resourceDescription, bufSize, moreInfo), originalCause);
  newIoe.setStackTrace(ioe.getStackTrace());
  return newIoe;
}
 
Example 29
Source Project: lucene-solr   Source File: TestNRTReaderCleanup.java    License: Apache License 2.0 5 votes vote down vote up
public void testClosingNRTReaderDoesNotCorruptYourIndex() throws IOException {

    // Windows disallows deleting & overwriting files still
    // open for reading:
    assumeFalse("this test can't run on Windows", Constants.WINDOWS);

    MockDirectoryWrapper dir = newMockDirectory();
    
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    LogMergePolicy lmp = new LogDocMergePolicy();
    lmp.setMergeFactor(2);
    iwc.setMergePolicy(lmp);

    RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
    Document doc = new Document();
    doc.add(new TextField("a", "foo", Field.Store.NO));
    w.addDocument(doc);
    w.commit();
    w.addDocument(doc);

    // Get a new reader, but this also sets off a merge:
    IndexReader r = w.getReader();
    w.close();

    // Blow away index and make a new writer:
    for(String name : dir.listAll()) {
      dir.deleteFile(name);
    }

    w = new RandomIndexWriter(random(), dir);
    w.addDocument(doc);
    w.close();
    r.close();
    dir.close();
  }
 
Example 30
Source Project: lucene-solr   Source File: TestIndexWriter.java    License: Apache License 2.0 5 votes vote down vote up
public void testPendingDeletionsRollbackWithReader() throws IOException {
  // irony: currently we don't emulate windows well enough to work on windows!
  assumeFalse("windows is not supported", Constants.WINDOWS);

  Path path = createTempDir();

  // Use WindowsFS to prevent open files from being deleted:
  FileSystem fs = new WindowsFS(path.getFileSystem()).getFileSystem(URI.create("file:///"));
  Path root = new FilterPath(path, fs);
  try (FSDirectory _dir = new NIOFSDirectory(root)) {
    Directory dir = new FilterDirectory(_dir) {};

    IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    IndexWriter w = new IndexWriter(dir, iwc);
    Document d = new Document();
    d.add(new StringField("id", "1", Field.Store.YES));
    d.add(new NumericDocValuesField("numval", 1));
    w.addDocument(d);
    w.commit();
    w.addDocument(d);
    w.flush();
    DirectoryReader reader = DirectoryReader.open(w);
    w.rollback();

    // try-delete superfluous files (some will fail due to windows-fs)
    IndexWriterConfig iwc2 = new IndexWriterConfig(new MockAnalyzer(random()));
    new IndexWriter(dir, iwc2).close();

    // test that we can index on top of pending deletions
    IndexWriterConfig iwc3 = new IndexWriterConfig(new MockAnalyzer(random()));
    w = new IndexWriter(dir, iwc3);
    w.addDocument(d);
    w.commit();

    reader.close();
    w.close();
  }
}