add lifecycle and pool manager

This commit is contained in:
Edison Su 2012-08-27 18:15:41 -07:00
parent 60758785b7
commit b65f9ec414
3 changed files with 291 additions and 0 deletions

View File

@ -0,0 +1,123 @@
package org.apache.cloudstack.storage.lifecycle;
import java.util.ArrayList;
import java.util.List;
import org.apache.cloudstack.platform.subsystem.api.storage.DataStore;
import org.apache.cloudstack.platform.subsystem.api.storage.DataStoreDriver;
import org.apache.cloudstack.platform.subsystem.api.storage.DataStoreEndPoint;
import org.apache.cloudstack.platform.subsystem.api.storage.DataStoreEndPointSelector;
import org.apache.cloudstack.platform.subsystem.api.storage.DataStoreLifeCycle;
import org.apache.log4j.Logger;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.CreateStoragePoolCommand;
import com.cloud.agent.api.ModifyStoragePoolAnswer;
import com.cloud.agent.api.ModifyStoragePoolCommand;
import com.cloud.alert.AlertManager;
import com.cloud.exception.StorageUnavailableException;
import com.cloud.host.HostVO;
import com.cloud.storage.StoragePoolHostVO;
import com.cloud.storage.StoragePoolVO;
import com.cloud.storage.Storage.StoragePoolType;
import com.cloud.storage.dao.StoragePoolDao;
import com.cloud.storage.dao.StoragePoolHostDao;
import com.cloud.utils.component.Inject;
import com.cloud.utils.exception.CloudRuntimeException;
public class DefaultPrimaryDataStoreLifeCycle implements DataStoreLifeCycle {
private static final Logger s_logger = Logger.getLogger(DataStoreLifeCycle.class);
private DataStore _ds;
@Inject
StoragePoolDao _storagePoolDao;
@Inject
StoragePoolHostDao _poolHostDao;
public DefaultPrimaryDataStoreLifeCycle(DataStore ds) {
this._ds = ds;
}
protected boolean createStoragePool(DataStoreEndPoint ep, StoragePoolVO pool) {
DataStoreDriver dsDriver = _ds.getDataStoreDriver();
CreateStoragePoolCommand cmd = new CreateStoragePoolCommand(true, pool);
final Answer answer = dsDriver.sendMessage(ep, cmd);
if (answer != null && answer.getResult()) {
return true;
} else {
throw new CloudRuntimeException(answer.getDetails());
}
}
protected void connectHostToSharedPool(DataStoreEndPoint ep, StoragePoolVO pool) throws StorageUnavailableException {
DataStoreDriver dsDriver = _ds.getDataStoreDriver();
long hostId = ep.getHostId();
ModifyStoragePoolCommand cmd = new ModifyStoragePoolCommand(true, pool);
final Answer answer = dsDriver.sendMessage(ep, cmd);
if (answer == null) {
throw new StorageUnavailableException("Unable to get an answer to the modify storage pool command", pool.getId());
}
if (!answer.getResult()) {
throw new StorageUnavailableException("Unable establish connection from storage head to storage pool " + pool.getId() + " due to " + answer.getDetails(), pool.getId());
}
assert (answer instanceof ModifyStoragePoolAnswer) : "Well, now why won't you actually return the ModifyStoragePoolAnswer when it's ModifyStoragePoolCommand? Pool=" + pool.getId();
ModifyStoragePoolAnswer mspAnswer = (ModifyStoragePoolAnswer) answer;
StoragePoolHostVO poolHost = _poolHostDao.findByPoolHost(pool.getId(), hostId);
if (poolHost == null) {
poolHost = new StoragePoolHostVO(pool.getId(), hostId, mspAnswer.getPoolInfo().getLocalPath().replaceAll("//", "/"));
_poolHostDao.persist(poolHost);
} else {
poolHost.setLocalPath(mspAnswer.getPoolInfo().getLocalPath().replaceAll("//", "/"));
}
pool.setAvailableBytes(mspAnswer.getPoolInfo().getAvailableBytes());
pool.setCapacityBytes(mspAnswer.getPoolInfo().getCapacityBytes());
_storagePoolDao.update(pool.getId(), pool);
}
public void add() {
DataStoreEndPointSelector dseps = _ds.getEndPointSelector();
List<DataStoreEndPoint> dsep = dseps.getEndPoints();
boolean success = false;
StoragePoolVO spool = _storagePoolDao.findById(_ds.getId());
for (DataStoreEndPoint ep : dsep) {
success = createStoragePool(ep, spool);
if (success) {
break;
}
}
List<DataStoreEndPoint> poolHosts = new ArrayList<DataStoreEndPoint>();
for (DataStoreEndPoint ep : dsep) {
try {
connectHostToSharedPool(ep, spool);
poolHosts.add(ep);
} catch (Exception e) {
s_logger.debug("Failed to add storage on this ep: " + ep.getHostId());
}
}
}
public void delete() {
// TODO Auto-generated method stub
}
public void enable() {
// TODO Auto-generated method stub
}
public void disable() {
// TODO Auto-generated method stub
}
public void processEvent(DataStoreEvent event, Object... objs) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,129 @@
/*
* 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.cloudstack.storage.manager;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.cloudstack.platform.subsystem.api.storage.DataStore;
import org.apache.cloudstack.platform.subsystem.api.storage.DataStore.StoreType;
import org.apache.cloudstack.platform.subsystem.api.storage.DataStoreLifeCycle;
import org.apache.cloudstack.platform.subsystem.api.storage.StorageProvider;
import com.cloud.dc.ClusterVO;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.dao.ClusterDao;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.dc.dao.HostPodDao;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.storage.StoragePool;
import com.cloud.storage.StoragePoolStatus;
import com.cloud.storage.StoragePoolVO;
import com.cloud.storage.dao.StoragePoolDao;
import com.cloud.utils.component.Adapters;
import com.cloud.utils.component.Inject;
import com.cloud.utils.exception.CloudRuntimeException;
public class StoragePoolManagerImpl implements StoragePoolService {
@Inject(adapter = StorageProvider.class)
protected Adapters<StorageProvider> _storageProviders;
@Inject
protected DataCenterDao _dcDao;
@Inject
protected HostPodDao _podDao;
@Inject
protected ClusterDao _clusterDao;
@Inject
protected StoragePoolDao _storagePoolDao;
public void deleteStoragePool(long poolId) {
StoragePool spool = _storagePoolDao.findById(poolId);
StorageProvider sp = findStorageProvider(spool.getStorageProvider());
DataStore ds = sp.getDataStore(spool);
DataStoreLifeCycle dslc = ds.getLifeCycle();
dslc.delete();
}
public void enableStoragePool(long poolId) {
// TODO Auto-generated method stub
}
public void disableStoragePool(long poolId) {
// TODO Auto-generated method stub
}
public Map<String, List<String>> getSupportedPrimaryStorages(long zoneId, HypervisorType hypervisor) {
// TODO Auto-generated method stub
return null;
}
public Map<String, List<String>> getSupportedSecondaryStorages(long zoneId) {
// TODO Auto-generated method stub
return null;
}
protected StorageProvider findStorageProvider(String name) {
Iterator<StorageProvider> spIter = _storageProviders.iterator();
StorageProvider sp = null;
while (spIter.hasNext()) {
sp = spIter.next();
if (sp.getProviderName().equalsIgnoreCase(name)) {
break;
}
}
return sp;
}
public StoragePool addStoragePool(long zoneId, long podId, long clusterId, long hostId, String URI, String storageType, String poolName, String storageProviderName, Map<String, String> params) {
StoragePoolVO spool = new StoragePoolVO();
long poolId = _storagePoolDao.getNextInSequence(Long.class, "id");
spool.setId(poolId);
spool.setDataCenterId(zoneId);
spool.setPodId(podId);
spool.setName(poolName);
spool.setClusterId(clusterId);
spool.setStorageProvider(storageProviderName);
spool.setStorageType(storageType);
spool.setStatus(StoragePoolStatus.Creating);
spool = _storagePoolDao.persist(spool);
StorageProvider sp = findStorageProvider(storageProviderName);
DataStore ds = sp.addDataStore((StoragePool)spool, URI, params);
DataStoreLifeCycle dslc = ds.getLifeCycle();
try {
dslc.add();
} catch (CloudRuntimeException e) {
_storagePoolDao.remove(spool.getId());
throw e;
}
spool.setPath(ds.getURI());
spool.setUuid(ds.getUUID());
spool.setStatus(StoragePoolStatus.Up);
_storagePoolDao.update(spool.getId(), spool);
spool = _storagePoolDao.findById(spool.getId());
return spool;
}
}

View File

@ -0,0 +1,39 @@
/*
* 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.cloudstack.storage.manager;
import java.util.List;
import java.util.Map;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.storage.StoragePool;
public interface StoragePoolService {
StoragePool addStoragePool(long zoneId, long podId, long clusterId, long hostId,
String URI,
String storageType,
String poolName,
String storageProviderName,
Map<String, String> params);
void deleteStoragePool(long poolId);
void enableStoragePool(long poolId);
void disableStoragePool(long poolId);
Map<String, List<String>> getSupportedPrimaryStorages(long zoneId, HypervisorType hypervisor);
Map<String, List<String>> getSupportedSecondaryStorages(long zoneId);
}