Fix ASTERIXDB-1776

The source of the issue was a deprecated method that failed when two
IODevices share a common prefix in their absolute path

Change-Id: Iba7837b433ce57f99e2c547e8bd1fb0bfc5a31df
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1489
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
BAD: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Yingyi Bu <buyingyi@gmail.com>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/exceptions/ErrorCode.java b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/exceptions/ErrorCode.java
index 963e123..d094368 100644
--- a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/exceptions/ErrorCode.java
+++ b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/exceptions/ErrorCode.java
@@ -47,6 +47,8 @@
     public static final int NO_SUCH_NODE = 10;
     public static final int CLASS_LOADING_ISSUE = 11;
     public static final int ILLEGAL_WRITE_AFTER_FLUSH_ATTEMPT = 12;
+    public static final int DUPLICATE_IODEVICE = 13;
+    public static final int NESTED_IODEVICES = 14;
 
     // Compilation error codes.
     public static final int RULECOLLECTION_NOT_INSTANCE_OF_LIST = 10001;
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 6b74b54..72f7c65 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
@@ -31,5 +31,7 @@
 10 = Node %1$s does not exist
 11 = Class loading issue: %1$s
 12 = Invalid attempt to write to a flushed append only metadata page
+13 = Duplicate IODevices are not allowed
+14 = IODevices should not be nested within each other
 
 10000 = The given rule collection %1$s is not an instance of the List class.
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/io/IOManager.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/io/IOManager.java
index 05c9f07..5ccdaa8 100644
--- a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/io/IOManager.java
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-nc/src/main/java/org/apache/hyracks/control/nc/io/IOManager.java
@@ -23,6 +23,8 @@
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -62,6 +64,7 @@
 
     public IOManager(List<IODeviceHandle> devices) throws HyracksDataException {
         this.ioDevices = Collections.unmodifiableList(devices);
+        checkDeviceValidity(devices);
         workspaces = new ArrayList<>();
         for (IODeviceHandle d : ioDevices) {
             if (d.getWorkspace() != null) {
@@ -76,6 +79,23 @@
         deviceComputer = new DefaultDeviceComputer(this);
     }
 
+    private void checkDeviceValidity(List<IODeviceHandle> devices) throws HyracksDataException {
+        for (IODeviceHandle d : devices) {
+            Path p = Paths.get(d.getMount().toURI());
+            for (IODeviceHandle e : devices) {
+                if (e != d) {
+                    Path q = Paths.get(e.getMount().toURI());
+                    if (p.equals(q)) {
+                        throw HyracksDataException.create(ErrorCode.DUPLICATE_IODEVICE);
+                    } else if (p.startsWith(q)) {
+                        throw HyracksDataException.create(ErrorCode.NESTED_IODEVICES);
+                    }
+                }
+            }
+
+        }
+    }
+
     @Override
     public void setExecutor(Executor executor) {
         this.executor = executor;
@@ -350,8 +370,9 @@
     }
 
     public IODeviceHandle getDevice(String fullPath) {
+        Path full = Paths.get(fullPath);
         for (IODeviceHandle d : ioDevices) {
-            if (fullPath.startsWith(d.getMount().getAbsolutePath())) {
+            if (full.startsWith(Paths.get(d.getMount().getAbsolutePath()))) {
                 return d;
             }
         }
diff --git a/hyracks-fullstack/hyracks/hyracks-tests/hyracks-storage-common-test/pom.xml b/hyracks-fullstack/hyracks/hyracks-tests/hyracks-storage-common-test/pom.xml
index 98a441d..1b5910b 100644
--- a/hyracks-fullstack/hyracks/hyracks-tests/hyracks-storage-common-test/pom.xml
+++ b/hyracks-fullstack/hyracks/hyracks-tests/hyracks-storage-common-test/pom.xml
@@ -64,5 +64,9 @@
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
     </dependency>
+    <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+    </dependency>
   </dependencies>
 </project>
\ No newline at end of file
diff --git a/hyracks-fullstack/hyracks/hyracks-tests/hyracks-storage-common-test/src/test/java/org/apache/hyracks/storage/common/IOManagerPathTest.java b/hyracks-fullstack/hyracks/hyracks-tests/hyracks-storage-common-test/src/test/java/org/apache/hyracks/storage/common/IOManagerPathTest.java
new file mode 100644
index 0000000..512a187
--- /dev/null
+++ b/hyracks-fullstack/hyracks/hyracks-tests/hyracks-storage-common-test/src/test/java/org/apache/hyracks/storage/common/IOManagerPathTest.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.hyracks.storage.common;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
+import org.apache.hyracks.api.io.FileReference;
+import org.apache.hyracks.api.io.IODeviceHandle;
+import org.apache.hyracks.control.nc.io.IOManager;
+import org.junit.*;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+
+public class IOManagerPathTest {
+    @Test
+    public void testPrefixNames() throws HyracksDataException {
+        IODeviceHandle shorter = new IODeviceHandle(new File("/tmp/tst/1"), "storage");
+        IODeviceHandle longer = new IODeviceHandle(new File("/tmp/tst/11"), "storage");
+        IOManager ioManager = new IOManager(Arrays.asList(new IODeviceHandle[] { shorter, longer }));
+        FileReference f = ioManager.resolveAbsolutePath("/tmp/tst/11/storage/Foo_idx_foo/my_btree");
+        Assert.assertEquals("/tmp/tst/11/storage/Foo_idx_foo/my_btree", f.getAbsolutePath());
+    }
+
+    @Test(expected = HyracksDataException.class)
+    public void testDuplicates() throws HyracksDataException {
+        IODeviceHandle first = new IODeviceHandle(new File("/tmp/tst/1"), "storage");
+        IODeviceHandle second = new IODeviceHandle(new File("/tmp/tst/1"), "storage");
+        IOManager ioManager = new IOManager(Arrays.asList(new IODeviceHandle[] { first, second }));
+    }
+
+    @After
+    @Before
+    public void cleanup() throws IOException {
+        FileUtils.deleteDirectory(new File("/tmp/tst/"));
+    }
+
+}