Skip to content

Commit

Permalink
Improve error message trying to use TDB2 location with TDB (JENA-1658)
Browse files Browse the repository at this point in the history
Since the lock files have slightly different formats TDB can detect when
a lock file appears to be TDB2 and issue an appropriate error message to
suggest users try using TDB2 instead.
  • Loading branch information
rvesse committed Jan 18, 2019
1 parent f77d743 commit 141d663
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public int getOwner() {
// Can read lock owner from the file
try {
String rawLockInfo = IO.readWholeFileAsUTF8(lockFile.getAbsolutePath());
if (rawLockInfo.endsWith("\n")) {
// This is most likely down to trying to open a TDB2 database with TDB1
throw new FileException("Unable to check TDB lock owner, the lock file contents appear to be for a TDB2 database. Please try loading this location as a TDB2 database.");
}

int owner = Integer.parseInt(rawLockInfo);
return owner;
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import java.io.FileWriter;
import java.io.IOException;

import org.apache.jena.tdb.TDBException ;
import org.apache.jena.tdb.base.file.Location ;
import org.apache.jena.tdb.base.file.LocationLock ;
import org.apache.jena.tdb.sys.ProcessUtils ;
import org.apache.jena.tdb.TDBException;
import org.apache.jena.tdb.base.file.Location;
import org.apache.jena.tdb.base.file.LocationLock;
import org.apache.jena.tdb.sys.ProcessUtils;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.BeforeClass;
Expand All @@ -39,10 +39,10 @@
public class TestLocationLock {

private static boolean negativePidsTreatedAsAlive = false;

@Rule
public TemporaryFolder tempDir = new TemporaryFolder();

@BeforeClass
public static void setup() {
negativePidsTreatedAsAlive = ProcessUtils.negativePidsTreatedAsAlive();
Expand Down Expand Up @@ -90,8 +90,9 @@ public void location_lock_dir_02() throws IOException {
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
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());
Expand All @@ -110,9 +111,9 @@ public void location_lock_dir_error_01() throws IOException {
Assert.assertTrue(lock.canObtain());

// Write a fake PID to the lock file
try(BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) {
// Fake PID that would never be valid
writer.write(Integer.toString(-1234));
writer.write(Integer.toString(-1234));
}
Assert.assertTrue(lock.isLocked());
Assert.assertFalse(lock.isOwned());
Expand All @@ -134,10 +135,9 @@ public void location_lock_dir_error_02() throws IOException {
Assert.assertTrue(lock.canObtain());

// Write a fake PID to the lock file
try(BufferedWriter writer =
new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) {
// Fake PID that would never be valid
writer.write(Integer.toString(-1234));
writer.write(Integer.toString(-1234));
}
Assert.assertTrue(lock.isLocked());
Assert.assertFalse(lock.isOwned());
Expand All @@ -146,4 +146,33 @@ public void location_lock_dir_error_02() throws IOException {
Assert.assertFalse(lock.canObtain());
lock.release();
}

@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"));
}
}
}

0 comments on commit 141d663

Please sign in to comment.