You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by rv...@apache.org on 2019/01/21 10:46:45 UTC

[jena] branch master updated: Improve error message trying to use TDB2 location with TDB (JENA-1658)

This is an automated email from the ASF dual-hosted git repository.

rvesse pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jena.git


The following commit(s) were added to refs/heads/master by this push:
     new 141d663  Improve error message trying to use TDB2 location with TDB (JENA-1658)
     new ca2a5cd0 Merge pull request #522 from rvesse/JENA-1658
141d663 is described below

commit 141d6638161236e1c1ba6bf58cd229e53f6082a5
Author: Rob Vesse <rv...@apache.org>
AuthorDate: Fri Jan 18 10:54:19 2019 +0000

    Improve error message trying to use TDB2 location with TDB (JENA-1658)
    
    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.
---
 .../apache/jena/tdb/base/file/LocationLock.java    |  5 ++
 .../jena/tdb/base/file/TestLocationLock.java       | 55 +++++++++++++++++-----
 2 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/LocationLock.java b/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/LocationLock.java
index c42bc41..c03ed27 100644
--- a/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/LocationLock.java
+++ b/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/LocationLock.java
@@ -103,6 +103,11 @@ public class LocationLock {
             // 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) {
diff --git a/jena-tdb/src/test/java/org/apache/jena/tdb/base/file/TestLocationLock.java b/jena-tdb/src/test/java/org/apache/jena/tdb/base/file/TestLocationLock.java
index cd8d289..cff84ec 100644
--- a/jena-tdb/src/test/java/org/apache/jena/tdb/base/file/TestLocationLock.java
+++ b/jena-tdb/src/test/java/org/apache/jena/tdb/base/file/TestLocationLock.java
@@ -22,10 +22,10 @@ import java.io.BufferedWriter;
 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;
@@ -39,10 +39,10 @@ import org.junit.rules.TemporaryFolder;
 public class TestLocationLock {
 
     private static boolean negativePidsTreatedAsAlive = false;
-    
+
     @Rule
     public TemporaryFolder tempDir = new TemporaryFolder();
-    
+
     @BeforeClass
     public static void setup() {
         negativePidsTreatedAsAlive = ProcessUtils.negativePidsTreatedAsAlive();
@@ -90,8 +90,9 @@ public class TestLocationLock {
         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());
@@ -110,9 +111,9 @@ public class TestLocationLock {
         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());
@@ -134,10 +135,9 @@ public class TestLocationLock {
         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());
@@ -146,4 +146,33 @@ public class TestLocationLock {
         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"));
+        }
+    }
 }