Asterix now configures its own IOManager in AsterixRuntimeState since it should not rely on the IOManager set in Hyracks. The Asterix IO devives are configured via the Asterix .properties file.

git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix_lsm_stabilization@811 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java
index 2e0a09b..9ebb9e5 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java
@@ -73,13 +73,13 @@
 import edu.uci.ics.asterix.aql.expression.WriteStatement;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlExpressionVisitor;
 import edu.uci.ics.asterix.aql.util.FunctionUtils;
+import edu.uci.ics.asterix.common.config.AsterixProperties;
 import edu.uci.ics.asterix.common.config.DatasetConfig.DatasetType;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
 import edu.uci.ics.asterix.common.functions.FunctionConstants;
 import edu.uci.ics.asterix.formats.base.IDataFormat;
 import edu.uci.ics.asterix.metadata.MetadataException;
 import edu.uci.ics.asterix.metadata.MetadataTransactionContext;
-import edu.uci.ics.asterix.metadata.bootstrap.AsterixProperties;
 import edu.uci.ics.asterix.metadata.declared.AqlCompiledMetadataDeclarations;
 import edu.uci.ics.asterix.metadata.declared.AqlDataSource;
 import edu.uci.ics.asterix.metadata.declared.AqlLogicalPlanAndMetadataImpl;
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/MetadataDeclTranslator.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/MetadataDeclTranslator.java
index 77b8a04..bf13be0 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/MetadataDeclTranslator.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/MetadataDeclTranslator.java
@@ -16,10 +16,10 @@
 import edu.uci.ics.asterix.common.annotations.IRecordFieldDataGen;
 import edu.uci.ics.asterix.common.annotations.RecordDataGenAnnotation;
 import edu.uci.ics.asterix.common.annotations.TypeDataGen;
+import edu.uci.ics.asterix.common.config.AsterixProperties;
 import edu.uci.ics.asterix.metadata.MetadataException;
 import edu.uci.ics.asterix.metadata.MetadataManager;
 import edu.uci.ics.asterix.metadata.MetadataTransactionContext;
-import edu.uci.ics.asterix.metadata.bootstrap.AsterixProperties;
 import edu.uci.ics.asterix.metadata.declared.AqlCompiledMetadataDeclarations;
 import edu.uci.ics.asterix.metadata.entities.AsterixBuiltinTypeMap;
 import edu.uci.ics.asterix.metadata.entities.Datatype;
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/api/aqlj/server/APIClientThread.java b/asterix-app/src/main/java/edu/uci/ics/asterix/api/aqlj/server/APIClientThread.java
index 7f75e65..908e99c 100644
--- a/asterix-app/src/main/java/edu/uci/ics/asterix/api/aqlj/server/APIClientThread.java
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/api/aqlj/server/APIClientThread.java
@@ -36,10 +36,10 @@
 import edu.uci.ics.asterix.aql.expression.Query;
 import edu.uci.ics.asterix.aql.parser.AQLParser;
 import edu.uci.ics.asterix.aql.parser.ParseException;
+import edu.uci.ics.asterix.common.config.AsterixProperties;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
 import edu.uci.ics.asterix.hyracks.bootstrap.CCBootstrapImpl;
 import edu.uci.ics.asterix.metadata.MetadataManager;
-import edu.uci.ics.asterix.metadata.bootstrap.AsterixProperties;
 import edu.uci.ics.asterix.metadata.declared.AqlCompiledMetadataDeclarations;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
 import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/hyracks/bootstrap/CCBootstrapImpl.java b/asterix-app/src/main/java/edu/uci/ics/asterix/hyracks/bootstrap/CCBootstrapImpl.java
index fcd50b5..001b179 100644
--- a/asterix-app/src/main/java/edu/uci/ics/asterix/hyracks/bootstrap/CCBootstrapImpl.java
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/hyracks/bootstrap/CCBootstrapImpl.java
@@ -24,10 +24,10 @@
 import edu.uci.ics.asterix.api.aqlj.server.APIClientThreadFactory;
 import edu.uci.ics.asterix.api.aqlj.server.ThreadedServer;
 import edu.uci.ics.asterix.api.http.servlet.APIServlet;
+import edu.uci.ics.asterix.common.config.AsterixProperties;
 import edu.uci.ics.asterix.common.config.GlobalConfig;
 import edu.uci.ics.asterix.metadata.MetadataManager;
 import edu.uci.ics.asterix.metadata.api.IAsterixStateProxy;
-import edu.uci.ics.asterix.metadata.bootstrap.AsterixProperties;
 import edu.uci.ics.asterix.metadata.bootstrap.AsterixStateProxy;
 import edu.uci.ics.hyracks.api.application.ICCApplicationContext;
 import edu.uci.ics.hyracks.api.application.ICCBootstrap;
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/hyracks/bootstrap/NCBootstrapImpl.java b/asterix-app/src/main/java/edu/uci/ics/asterix/hyracks/bootstrap/NCBootstrapImpl.java
index 24cf3a8..725f68f 100644
--- a/asterix-app/src/main/java/edu/uci/ics/asterix/hyracks/bootstrap/NCBootstrapImpl.java
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/hyracks/bootstrap/NCBootstrapImpl.java
@@ -48,10 +48,8 @@
 
         runtimeContext = new AsterixAppRuntimeContext(ncApplicationContext);
         runtimeContext.initialize();
-
         ncApplicationContext.setApplicationObject(runtimeContext);
 
-        // Initialize metadata if this node is the metadata node
         IAsterixStateProxy proxy = (IAsterixStateProxy) ncApplicationContext.getDistributedState();
         isMetadataNode = nodeId.equals(proxy.getAsterixProperties().getMetadataNodeName());
         if (isMetadataNode) {
diff --git a/asterix-app/src/main/resources/asterix-metadata.properties b/asterix-app/src/main/resources/asterix-metadata.properties
index e9ccc63..309f9f4 100644
--- a/asterix-app/src/main/resources/asterix-metadata.properties
+++ b/asterix-app/src/main/resources/asterix-metadata.properties
@@ -1,4 +1,7 @@
 MetadataNode=nc1
 NewUniverse=true
-nc1.stores=/tmp/nc1data/
-nc2.stores=/tmp/nc2data/, /tmp/nc2data1/
\ No newline at end of file
+nc1.stores=nc1data
+nc2.stores=nc2data
+nc1.iodevices=/tmp/
+nc2.iodevices=/tmp/
+OutputDir=/tmp/asterix_output/
diff --git a/asterix-app/src/main/resources/test.properties b/asterix-app/src/main/resources/test.properties
index 4947dbf..309f9f4 100755
--- a/asterix-app/src/main/resources/test.properties
+++ b/asterix-app/src/main/resources/test.properties
@@ -2,4 +2,6 @@
 NewUniverse=true
 nc1.stores=nc1data
 nc2.stores=nc2data
+nc1.iodevices=/tmp/
+nc2.iodevices=/tmp/
 OutputDir=/tmp/asterix_output/
diff --git a/asterix-common/pom.xml b/asterix-common/pom.xml
index f7f3767..d6e20f9 100644
--- a/asterix-common/pom.xml
+++ b/asterix-common/pom.xml
@@ -34,6 +34,11 @@
 			<version>0.2.2-SNAPSHOT</version>
 		</dependency>
 		<dependency>
+			<groupId>edu.uci.ics.hyracks</groupId>
+			<artifactId>hyracks-control-nc</artifactId>
+			<version>0.2.2-SNAPSHOT</version>
+		</dependency>
+		<dependency>
 			<groupId>edu.uci.ics.asterix</groupId>
 			<artifactId>asterix-transactions</artifactId>
 			<version>0.0.4-SNAPSHOT</version>
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/api/IAsterixPropertiesProxy.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/api/IAsterixPropertiesProxy.java
new file mode 100644
index 0000000..5f54222
--- /dev/null
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/api/IAsterixPropertiesProxy.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2009-2010 by The Regents of the University of California
+ * Licensed 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 from
+ * 
+ *     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 edu.uci.ics.asterix.common.api;
+
+import java.io.Serializable;
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+
+import edu.uci.ics.asterix.common.config.AsterixProperties;
+
+/**
+ * Interface for setting/getting the remote Asterix properties.
+ */
+public interface IAsterixPropertiesProxy extends Remote, Serializable {
+    public void setAsterixProperties(AsterixProperties asterixProperties) throws RemoteException;
+    public AsterixProperties getAsterixProperties() throws RemoteException;
+}
diff --git a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/bootstrap/AsterixProperties.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/config/AsterixProperties.java
similarity index 84%
rename from asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/bootstrap/AsterixProperties.java
rename to asterix-common/src/main/java/edu/uci/ics/asterix/common/config/AsterixProperties.java
index 71a7de3..3fd9c79 100644
--- a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/bootstrap/AsterixProperties.java
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/config/AsterixProperties.java
@@ -13,7 +13,7 @@
  * limitations under the License.
  */
 
-package edu.uci.ics.asterix.metadata.bootstrap;
+package edu.uci.ics.asterix.common.config;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -27,7 +27,6 @@
 import java.util.Map;
 import java.util.Properties;
 
-import edu.uci.ics.asterix.common.config.GlobalConfig;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
 
 /**
@@ -41,6 +40,7 @@
     private static Boolean isNewUniverse;
     private static HashSet<String> nodeNames;
     private static Map<String, String[]> stores;
+    private static Map<String, String[]> ioDevicePaths;
     private static String outputDir;
 
     public static AsterixProperties INSTANCE = new AsterixProperties();
@@ -78,6 +78,8 @@
         }
         Enumeration<String> pNames = (Enumeration<String>) p.propertyNames();
         stores = new HashMap<String, String[]>();
+        ioDevicePaths = new HashMap<String, String[]>();
+        nodeNames = new HashSet<String>();
         boolean newUniverseChosen = false;
         String pn;
         String val;
@@ -95,6 +97,13 @@
                 outputDir = val;
             } else {
                 String ncName = pn.substring(0, pn.indexOf('.'));
+                String ncAttribute = pn.substring(pn.indexOf('.') + 1).toLowerCase();
+                Map<String, String[]> targetMap = null;
+                if (ncAttribute.equals("stores")) {
+                    targetMap = stores;
+                } else if (ncAttribute.equals("iodevices")) {
+                    targetMap = ioDevicePaths;
+                }
                 val = p.getProperty(pn);
                 String[] folderNames = val.split("\\s*,\\s*");
                 int i = 0;
@@ -110,11 +119,11 @@
                     }
                     i++;
                 }
-                stores.put(ncName, folderNames);
-                nodeNames = new HashSet<String>();
-                nodeNames.addAll(stores.keySet());
-            }
+                targetMap.put(ncName, folderNames);
+            }            
         }
+        nodeNames.addAll(stores.keySet());
+        nodeNames.addAll(ioDevicePaths.keySet());
         if (metadataNodeName == null)
             throw new AlgebricksException("You need to specify the metadata node!");
         if (!newUniverseChosen)
@@ -137,6 +146,14 @@
         return stores;
     }
 
+    public Map<String, String[]> getIODevicePaths() {
+        return ioDevicePaths;
+    }
+    
+    public String[] getIODevicePaths(String nodeId) {
+        return ioDevicePaths.get(nodeId);
+    }
+    
     public HashSet<String> getNodeNames() {
         return nodeNames;
     }
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixAppRuntimeContext.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixAppRuntimeContext.java
index 52a67d3..fd1508b 100644
--- a/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixAppRuntimeContext.java
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/AsterixAppRuntimeContext.java
@@ -1,13 +1,22 @@
 package edu.uci.ics.asterix.common.context;
 
+import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Executors;
 import java.util.logging.Level;
 
+import edu.uci.ics.asterix.common.api.IAsterixPropertiesProxy;
+import edu.uci.ics.asterix.common.config.AsterixProperties;
 import edu.uci.ics.asterix.common.config.GlobalConfig;
+import edu.uci.ics.asterix.common.exceptions.AsterixException;
 import edu.uci.ics.asterix.transaction.management.exception.ACIDException;
 import edu.uci.ics.asterix.transaction.management.service.transaction.TransactionProvider;
 import edu.uci.ics.hyracks.api.application.INCApplicationContext;
 import edu.uci.ics.hyracks.api.io.IIOManager;
+import edu.uci.ics.hyracks.api.io.IODeviceHandle;
+import edu.uci.ics.hyracks.control.nc.io.IOManager;
 import edu.uci.ics.hyracks.storage.am.common.api.IIndexLifecycleManager;
 import edu.uci.ics.hyracks.storage.am.common.dataflow.IndexLifecycleManager;
 import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMFlushController;
@@ -37,6 +46,7 @@
     private static final int DEFAULT_LIFECYCLEMANAGER_MEMORY_BUDGET = 1024 * 1024 * 1024; // 1GB
     private final INCApplicationContext ncApplicationContext;
 
+    private IIOManager ioManager;
     private IIndexLifecycleManager indexLifecycleManager;
     private IFileMapManager fileMapManager;
     private IBufferCache bufferCache;
@@ -53,15 +63,24 @@
         this.ncApplicationContext = ncApplicationContext;
     }
 
-    public void initialize() throws IOException, ACIDException {
+    public void initialize() throws AsterixException, IOException, ACIDException {
         int pageSize = getBufferCachePageSize();
         int numPages = getBufferCacheNumPages();
 
+        // Create a new IOManager based on the I/O device paths given in the Asterix .properties configuration file.
+        IAsterixPropertiesProxy proxy = (IAsterixPropertiesProxy) ncApplicationContext.getDistributedState();
+        AsterixProperties asterixProperties = proxy.getAsterixProperties();
+        String[] ioDevicePaths = asterixProperties.getIODevicePaths(ncApplicationContext.getNodeId());
+        if (ioDevicePaths == null) {
+            throw new AsterixException("I/O devices not configured for Node Controller '" + ncApplicationContext.getNodeId() + "'."); 
+        }
+        ioManager = new IOManager(getDevices(ioDevicePaths), Executors.newCachedThreadPool());
+        
         fileMapManager = new AsterixFileMapManager();
         ICacheMemoryAllocator allocator = new HeapBufferAllocator();
         IPageReplacementStrategy prs = new ClockPageReplacementStrategy();
-        IIOManager ioMgr = ncApplicationContext.getRootContext().getIOManager();
-        bufferCache = new BufferCache(ioMgr, allocator, prs, fileMapManager, pageSize, numPages, Integer.MAX_VALUE);
+        
+        bufferCache = new BufferCache(ioManager, allocator, prs, fileMapManager, pageSize, numPages, Integer.MAX_VALUE);
         indexLifecycleManager = new IndexLifecycleManager(DEFAULT_LIFECYCLEMANAGER_MEMORY_BUDGET);
         provider = new TransactionProvider(ncApplicationContext.getNodeId());
 
@@ -71,7 +90,7 @@
         opTracker = new ReferenceCountingOperationTracker();
 
         ILocalResourceRepositoryFactory persistentLocalResourceRepositoryFactory = new PersistentLocalResourceRepositoryFactory(
-                ioMgr);
+                ioManager);
         localResourceRepository = persistentLocalResourceRepositoryFactory.createRepository();
         resourceIdFactory = (new ResourceIdFactoryProvider(localResourceRepository)).createResourceIdFactory();
     }
@@ -119,10 +138,22 @@
         return numPages;
     }
 
+    private List<IODeviceHandle> getDevices(String[] ioDevicePaths) {
+        List<IODeviceHandle> devices = new ArrayList<IODeviceHandle>();
+        for (String ioDevStr : ioDevicePaths) {
+            devices.add(new IODeviceHandle(new File(ioDevStr), "."));
+        }
+        return devices;
+    }
+    
     public void deinitialize() {
         bufferCache.close();
     }
 
+    public IIOManager getIOManager() {
+        return ioManager;
+    }
+    
     public IBufferCache getBufferCache() {
         return bufferCache;
     }
diff --git a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/api/IAsterixStateProxy.java b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/api/IAsterixStateProxy.java
index 5f772c7..29a23cf 100644
--- a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/api/IAsterixStateProxy.java
+++ b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/api/IAsterixStateProxy.java
@@ -19,17 +19,13 @@
 import java.rmi.Remote;
 import java.rmi.RemoteException;
 
-import edu.uci.ics.asterix.metadata.bootstrap.AsterixProperties;
+import edu.uci.ics.asterix.common.api.IAsterixPropertiesProxy;
 
 /**
  * Interface for setting/getting distributed state of Asterix.
  */
-public interface IAsterixStateProxy extends Remote, Serializable {
+public interface IAsterixStateProxy extends Remote, Serializable, IAsterixPropertiesProxy {
     public void setMetadataNode(IMetadataNode metadataNode) throws RemoteException;
 
-    public void setAsterixProperties(AsterixProperties asterixProperties) throws RemoteException;
-
     public IMetadataNode getMetadataNode() throws RemoteException;
-
-    public AsterixProperties getAsterixProperties() throws RemoteException;
 }
diff --git a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/bootstrap/AsterixStateProxy.java b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/bootstrap/AsterixStateProxy.java
index 3946fa6..55aaf33 100644
--- a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/bootstrap/AsterixStateProxy.java
+++ b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/bootstrap/AsterixStateProxy.java
@@ -19,6 +19,7 @@
 import java.rmi.server.UnicastRemoteObject;
 import java.util.logging.Logger;
 
+import edu.uci.ics.asterix.common.config.AsterixProperties;
 import edu.uci.ics.asterix.metadata.api.IAsterixStateProxy;
 import edu.uci.ics.asterix.metadata.api.IMetadataNode;
 
diff --git a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/bootstrap/MetadataBootstrap.java b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/bootstrap/MetadataBootstrap.java
index a1a510c..a8f1116 100644
--- a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/bootstrap/MetadataBootstrap.java
+++ b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/bootstrap/MetadataBootstrap.java
@@ -23,6 +23,7 @@
 import java.util.List;
 import java.util.logging.Logger;
 
+import edu.uci.ics.asterix.common.config.AsterixProperties;
 import edu.uci.ics.asterix.common.config.DatasetConfig.DatasetType;
 import edu.uci.ics.asterix.common.config.DatasetConfig.IndexType;
 import edu.uci.ics.asterix.common.context.AsterixAppRuntimeContext;
@@ -146,7 +147,7 @@
         localResourceRepository = runtimeContext.getLocalResourceRepository();
         bufferCache = runtimeContext.getBufferCache();
         fileMapProvider = runtimeContext.getFileMapManager();
-        ioManager = ncApplicationContext.getRootContext().getIOManager();
+        ioManager = runtimeContext.getIOManager();
 
         // Begin a transaction against the metadata.
         // Lock the metadata in X mode.
diff --git a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AqlCompiledMetadataDeclarations.java b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AqlCompiledMetadataDeclarations.java
index 6973f28..f9c604b 100644
--- a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AqlCompiledMetadataDeclarations.java
+++ b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AqlCompiledMetadataDeclarations.java
@@ -22,6 +22,7 @@
 import java.util.logging.Logger;
 
 import edu.uci.ics.asterix.common.annotations.TypeDataGen;
+import edu.uci.ics.asterix.common.config.AsterixProperties;
 import edu.uci.ics.asterix.common.config.DatasetConfig.DatasetType;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
 import edu.uci.ics.asterix.formats.base.IDataFormat;
@@ -29,7 +30,6 @@
 import edu.uci.ics.asterix.metadata.MetadataManager;
 import edu.uci.ics.asterix.metadata.MetadataTransactionContext;
 import edu.uci.ics.asterix.metadata.api.IMetadataManager;
-import edu.uci.ics.asterix.metadata.bootstrap.AsterixProperties;
 import edu.uci.ics.asterix.metadata.entities.Dataset;
 import edu.uci.ics.asterix.metadata.entities.Datatype;
 import edu.uci.ics.asterix.metadata.entities.Dataverse;