/** Stop managing a location. Use with great care (testing only). */ public static synchronized void expel(Location location, boolean force) { StoreConnection sConn = cache.get(location) ; if (sConn == null) return ; if (!force && sConn.transactionManager.activeTransactions()) throw new TDBTransactionException("Can't expel: Active transactions for location: " + location) ; // No transactions at this point (or we don't care and are clearing up forcefully.) sConn.transactionManager.closedown() ; sConn.baseDSG.close() ; sConn.isValid = false ; cache.remove(location) ; ChannelManager.release(sConn.transactionManager.getJournal().getFilename()) ; // Release the lock if (SystemTDB.DiskLocationMultiJvmUsagePrevention) { if (location.getLock().isOwned()) { location.getLock().release(); } else if (location.getLock().canLock()) { SystemTDB.errlog.warn("Location " + location.getDirectoryPath() + " was not locked, if another JVM accessed this location simultaneously data corruption may have occurred"); } } }
LocationLock lock = location.getLock(); if (lock.canLock()) { if (!lock.canObtain()) {
@Test public void location_lock_dir_error_03() throws IOException { Assume.assumeTrue(negativePidsTreatedAsAlive); Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Write a TDB2 format lock file try (BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) { // TDB2 format lock file, this writes a new line to the end of the lock file writer.write(Integer.toString(-1234)); writer.write('\n'); } // Trying to get the owner should error accordingly try { lock.canObtain(); Assert.fail("Expected the lock file to be considered invalid"); } catch (FileException e) { String errMsg = e.getMessage(); Assert.assertNotNull(errMsg); Assert.assertTrue(errMsg.contains("appear to be for a TDB2 database")); } } }
@Test public void location_lock_dir_02() throws IOException { Assume.assumeTrue(negativePidsTreatedAsAlive); Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Write a fake PID to the lock file try (BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) { writer.write(Integer.toString(-1234)); // Fake PID that would never // be valid } Assert.assertTrue(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertFalse(lock.canObtain()); }
@Test public void location_lock_dir_02() throws IOException { Assume.assumeTrue(negativePidsTreatedAsAlive); Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Write a fake PID to the lock file try(BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) { writer.write(Integer.toString(-1234)); // Fake PID that would never be valid } Assert.assertTrue(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertFalse(lock.canObtain()); }
@Test(expected = TDBException.class) public void location_lock_store_connection_02() throws IOException { Assume.assumeTrue(negativePidsTreatedAsAlive); Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Write a fake PID to the lock file try(BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) { // Fake PID that would never be valid writer.write(Integer.toString(-1234)); } Assert.assertTrue(lock.isLocked()); Assert.assertFalse(lock.isOwned()); // Attempting to create a connection on this location should error StoreConnection.make(dir); } }
@Test(expected = TDBException.class) public void location_lock_store_connection_02() throws IOException { Assume.assumeTrue(negativePidsTreatedAsAlive); Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Write a fake PID to the lock file try(BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) { // Fake PID that would never be valid writer.write(Integer.toString(-1234)); } Assert.assertTrue(lock.isLocked()); Assert.assertFalse(lock.isOwned()); // Attempting to create a connection on this location should error StoreConnection.make(dir); } }
@Test(expected = TDBException.class) public void location_lock_dir_error_01() throws IOException { Assume.assumeTrue(negativePidsTreatedAsAlive); Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Write a fake PID to the lock file try(BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) { // Fake PID that would never be valid writer.write(Integer.toString(-1234)); } Assert.assertTrue(lock.isLocked()); Assert.assertFalse(lock.isOwned()); // Attempting to obtain the lock should now error Assert.assertFalse(lock.canObtain()); lock.obtain(); }
@Test(expected = TDBException.class) public void location_lock_dir_error_01() throws IOException { Assume.assumeTrue(negativePidsTreatedAsAlive); Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Write a fake PID to the lock file try (BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) { // Fake PID that would never be valid writer.write(Integer.toString(-1234)); } Assert.assertTrue(lock.isLocked()); Assert.assertFalse(lock.isOwned()); // Attempting to obtain the lock should now error Assert.assertFalse(lock.canObtain()); lock.obtain(); }
@Test(expected = TDBException.class) public void location_lock_dir_error_02() throws IOException { Assume.assumeTrue(negativePidsTreatedAsAlive); Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Write a fake PID to the lock file try (BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) { // Fake PID that would never be valid writer.write(Integer.toString(-1234)); } Assert.assertTrue(lock.isLocked()); Assert.assertFalse(lock.isOwned()); // Attempting to release a lock we don't own should error Assert.assertFalse(lock.canObtain()); lock.release(); }
@Test(expected = TDBException.class) public void location_lock_dir_error_02() throws IOException { Assume.assumeTrue(negativePidsTreatedAsAlive); Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Write a fake PID to the lock file try(BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) { // Fake PID that would never be valid writer.write(Integer.toString(-1234)); } Assert.assertTrue(lock.isLocked()); Assert.assertFalse(lock.isOwned()); // Attempting to release a lock we don't own should error Assert.assertFalse(lock.canObtain()); lock.release(); } }
@Test public void location_lock_mem() { Location mem = Location.mem(); LocationLock lock = mem.getLock(); Assert.assertFalse(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertFalse(lock.canObtain()); }
@Test public void location_lock_mem() { Location mem = Location.mem(); LocationLock lock = mem.getLock(); Assert.assertFalse(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertFalse(lock.canObtain()); }
@Test public void location_lock_dir_01() { Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Try to obtain the lock lock.obtain(); Assert.assertTrue(lock.isLocked()); Assert.assertTrue(lock.isOwned()); // Release the lock lock.release(); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); }
@Test public void location_lock_dir_01() { Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Try to obtain the lock lock.obtain(); Assert.assertTrue(lock.isLocked()); Assert.assertTrue(lock.isOwned()); // Release the lock lock.release(); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); }
@Test public void location_lock_store_connection_01() { Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Creating a StoreConnection on the location will obtain the lock StoreConnection.make(dir); Assert.assertTrue(lock.isLocked()); Assert.assertTrue(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Releasing the connection releases the lock StoreConnection.release(dir); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); }
@Test public void location_lock_store_connection_01() { Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Creating a StoreConnection on the location will obtain the lock StoreConnection.make(dir); Assert.assertTrue(lock.isLocked()); Assert.assertTrue(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Releasing the connection releases the lock StoreConnection.release(dir); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); }