ClusterProviderPool.java 3.2 KB
Newer Older
S
sewen 已提交
1
/***********************************************************************************************************************
2
 * Copyright (C) 2010-2013 by the Stratosphere project (http://stratosphere.eu)
S
sewen 已提交
3 4 5 6 7 8 9 10 11 12 13
 *
 * 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 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.
 **********************************************************************************************************************/

S
StephanEwen 已提交
14
package eu.stratosphere.test.util.minicluster;
S
sewen 已提交
15 16 17 18 19 20 21 22

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;

23
import eu.stratosphere.configuration.Configuration;
S
StephanEwen 已提交
24
import eu.stratosphere.test.util.Constants;
S
sewen 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

/**
 */
public class ClusterProviderPool {

	protected static Hashtable<String, ClusterProvider> clusterPool = new Hashtable<String, ClusterProvider>();

	protected static Hashtable<String, Configuration> configPool = null;

	public static ClusterProvider getInstance(String clusterId) throws Exception {
		ClusterProvider instance = clusterPool.get(clusterId);
		if (instance == null) {
			// check whether config pool was initialized
			if (configPool == null) {
				loadClusterConfigs();
			}

			// get cluster config
			Configuration config = configPool.get(clusterId);
			if (config == null) {
				throw new Exception("No cluster configuration known for clusterId " + clusterId);
			}

			// bring up new cluster
			String clusterProviderType = config.getString(Constants.CLUSTER_PROVIDER_TYPE, "");
			if (clusterProviderType.equals("LocalClusterProvider")) {
				instance = new LocalClusterProvider(config);
			} else {
				throw new Exception("No or unknown cluster provider type configured");
			}
55
			instance.startFS();
S
sewen 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
			instance.startNephele();

			clusterPool.put(clusterId, instance);
		}
		return instance;
	}

	public static void removeInstance(String clusterId) throws Exception {
		clusterPool.remove(clusterId);
	}

	private static void loadClusterConfigs() throws FileNotFoundException, IOException {
		configPool = new Hashtable<String, Configuration>();

		File configDir = new File(Constants.CLUSTER_CONFIGS);

		if (configDir.isDirectory()) {
			for (File configFile : configDir.listFiles()) {
				// try to parse config file
				Configuration clusterConfig = new Configuration();
				Properties p = new Properties();
				p.load(new FileInputStream(configFile));

				for (String key : p.stringPropertyNames()) {
					clusterConfig.setString(key, p.getProperty(key));
				}

				// add config to config pool
				String clusterId = clusterConfig.getString(Constants.CLUSTER_PROVIDER_ID, "");
				if (clusterId.equals("")) {
					throw new IllegalArgumentException("Cluster Configuration has no clusterId");
				}
				configPool.put(clusterId, clusterConfig);
			}
		}
	}

}