[NO ISSUE][STO] Log more information on File is already mapped

Change-Id: Ifcc1d56a29c67e0cfc999defc00894f456c92ca9
Reviewed-on: https://asterix-gerrit.ics.uci.edu/2791
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/nc/HaltCallback.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/nc/HaltCallback.java
index db34131..17a4f46 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/nc/HaltCallback.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/nc/HaltCallback.java
@@ -41,7 +41,7 @@
 
     @Override
     public void operationFailed(ILSMIOOperation operation, Throwable t) {
-        LOGGER.error("Operation {} has failed", t);
+        LOGGER.error("Operation {} has failed", operation, t);
         if (operation.getIOOpertionType() == LSMIOOperationType.FLUSH) {
             ExitUtil.halt(ExitUtil.EC_FLUSH_FAILED);
         }
diff --git a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/io/FileReference.java b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/io/FileReference.java
index 0375e9e..4ded855 100644
--- a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/io/FileReference.java
+++ b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/io/FileReference.java
@@ -20,6 +20,7 @@
 
 import java.io.File;
 import java.io.Serializable;
+import java.util.Date;
 
 /**
  * A device handle and a relative path.
@@ -31,6 +32,7 @@
     private final File file;
     private final IODeviceHandle dev;
     private final String path;
+    private long registrationTime = 0L;
 
     public FileReference(IODeviceHandle dev, String path) {
         file = new File(dev.getMount(), path);
@@ -90,4 +92,23 @@
     public FileReference getChild(String name) {
         return new FileReference(dev, path + File.separator + name);
     }
+
+    public void register() {
+        if (registrationTime != 0) {
+            throw new IllegalStateException(
+                    "File " + toString() + " was already registered at " + new Date(registrationTime));
+        }
+        registrationTime = System.currentTimeMillis();
+    }
+
+    public long registrationTime() {
+        return registrationTime;
+    }
+
+    public void unregister() {
+        if (registrationTime == 0) {
+            throw new IllegalStateException("File " + toString() + " wasn't registered before");
+        }
+        registrationTime = 0;
+    }
 }
diff --git a/hyracks-fullstack/hyracks/hyracks-api/src/main/resources/errormsg/en.properties b/hyracks-fullstack/hyracks/hyracks-api/src/main/resources/errormsg/en.properties
index ef07038..bf73b91 100644
--- a/hyracks-fullstack/hyracks/hyracks-api/src/main/resources/errormsg/en.properties
+++ b/hyracks-fullstack/hyracks/hyracks-api/src/main/resources/errormsg/en.properties
@@ -97,7 +97,7 @@
 78 = Failed to purge the bloom filter since it is active
 79 = Cannot bulk-load a non-empty tree
 80 = Cannot create index because it already exists
-81 = File %1$s is already mapped
+81 = File %1$s is already mapped as %2$s registered at %3$s
 82 = Failed to create the file %1$s because it already exists
 83 = No index found with resourceID %1$s
 84 = Files with overlapping non-contained timestamp intervals were found in %1$s
diff --git a/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/file/FileMapManager.java b/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/file/FileMapManager.java
index 26df884..32922d2 100644
--- a/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/file/FileMapManager.java
+++ b/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/file/FileMapManager.java
@@ -18,6 +18,7 @@
  */
 package org.apache.hyracks.storage.common.file;
 
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -67,15 +68,20 @@
             throw HyracksDataException.create(ErrorCode.NO_MAPPING_FOR_FILE_ID, fileId);
         }
         name2IdMap.remove(fileRef);
+        fileRef.unregister();
         return fileRef;
     }
 
     @Override
     public int registerFile(FileReference fileRef) throws HyracksDataException {
-        if (isMapped(fileRef)) {
-            throw HyracksDataException.create(ErrorCode.FILE_ALREADY_MAPPED, fileRef);
+        Integer existingKey = name2IdMap.get(fileRef);
+        if (existingKey != null) {
+            FileReference prevFile = id2nameMap.get(existingKey);
+            throw HyracksDataException.create(ErrorCode.FILE_ALREADY_MAPPED, fileRef, prevFile,
+                    new Date(prevFile.registrationTime()).toString());
         }
         int fileId = idCounter++;
+        fileRef.register();
         id2nameMap.put(fileId, fileRef);
         name2IdMap.put(fileRef, fileId);
         return fileId;