Skip to content

Commit b65f9ec

Browse files
committed
add lifecycle and pool manager
1 parent 6075878 commit b65f9ec

3 files changed

Lines changed: 291 additions & 0 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package org.apache.cloudstack.storage.lifecycle;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.apache.cloudstack.platform.subsystem.api.storage.DataStore;
7+
import org.apache.cloudstack.platform.subsystem.api.storage.DataStoreDriver;
8+
import org.apache.cloudstack.platform.subsystem.api.storage.DataStoreEndPoint;
9+
import org.apache.cloudstack.platform.subsystem.api.storage.DataStoreEndPointSelector;
10+
import org.apache.cloudstack.platform.subsystem.api.storage.DataStoreLifeCycle;
11+
import org.apache.log4j.Logger;
12+
13+
import com.cloud.agent.api.Answer;
14+
import com.cloud.agent.api.CreateStoragePoolCommand;
15+
import com.cloud.agent.api.ModifyStoragePoolAnswer;
16+
import com.cloud.agent.api.ModifyStoragePoolCommand;
17+
import com.cloud.alert.AlertManager;
18+
import com.cloud.exception.StorageUnavailableException;
19+
import com.cloud.host.HostVO;
20+
import com.cloud.storage.StoragePoolHostVO;
21+
import com.cloud.storage.StoragePoolVO;
22+
import com.cloud.storage.Storage.StoragePoolType;
23+
import com.cloud.storage.dao.StoragePoolDao;
24+
import com.cloud.storage.dao.StoragePoolHostDao;
25+
import com.cloud.utils.component.Inject;
26+
import com.cloud.utils.exception.CloudRuntimeException;
27+
28+
public class DefaultPrimaryDataStoreLifeCycle implements DataStoreLifeCycle {
29+
private static final Logger s_logger = Logger.getLogger(DataStoreLifeCycle.class);
30+
private DataStore _ds;
31+
@Inject
32+
StoragePoolDao _storagePoolDao;
33+
@Inject
34+
StoragePoolHostDao _poolHostDao;
35+
public DefaultPrimaryDataStoreLifeCycle(DataStore ds) {
36+
this._ds = ds;
37+
}
38+
39+
40+
protected boolean createStoragePool(DataStoreEndPoint ep, StoragePoolVO pool) {
41+
DataStoreDriver dsDriver = _ds.getDataStoreDriver();
42+
CreateStoragePoolCommand cmd = new CreateStoragePoolCommand(true, pool);
43+
final Answer answer = dsDriver.sendMessage(ep, cmd);
44+
if (answer != null && answer.getResult()) {
45+
return true;
46+
} else {
47+
throw new CloudRuntimeException(answer.getDetails());
48+
}
49+
}
50+
51+
protected void connectHostToSharedPool(DataStoreEndPoint ep, StoragePoolVO pool) throws StorageUnavailableException {
52+
DataStoreDriver dsDriver = _ds.getDataStoreDriver();
53+
long hostId = ep.getHostId();
54+
ModifyStoragePoolCommand cmd = new ModifyStoragePoolCommand(true, pool);
55+
final Answer answer = dsDriver.sendMessage(ep, cmd);
56+
57+
if (answer == null) {
58+
throw new StorageUnavailableException("Unable to get an answer to the modify storage pool command", pool.getId());
59+
}
60+
61+
if (!answer.getResult()) {
62+
throw new StorageUnavailableException("Unable establish connection from storage head to storage pool " + pool.getId() + " due to " + answer.getDetails(), pool.getId());
63+
}
64+
65+
assert (answer instanceof ModifyStoragePoolAnswer) : "Well, now why won't you actually return the ModifyStoragePoolAnswer when it's ModifyStoragePoolCommand? Pool=" + pool.getId();
66+
ModifyStoragePoolAnswer mspAnswer = (ModifyStoragePoolAnswer) answer;
67+
68+
StoragePoolHostVO poolHost = _poolHostDao.findByPoolHost(pool.getId(), hostId);
69+
if (poolHost == null) {
70+
poolHost = new StoragePoolHostVO(pool.getId(), hostId, mspAnswer.getPoolInfo().getLocalPath().replaceAll("//", "/"));
71+
_poolHostDao.persist(poolHost);
72+
} else {
73+
poolHost.setLocalPath(mspAnswer.getPoolInfo().getLocalPath().replaceAll("//", "/"));
74+
}
75+
pool.setAvailableBytes(mspAnswer.getPoolInfo().getAvailableBytes());
76+
pool.setCapacityBytes(mspAnswer.getPoolInfo().getCapacityBytes());
77+
_storagePoolDao.update(pool.getId(), pool);
78+
}
79+
80+
public void add() {
81+
DataStoreEndPointSelector dseps = _ds.getEndPointSelector();
82+
List<DataStoreEndPoint> dsep = dseps.getEndPoints();
83+
boolean success = false;
84+
StoragePoolVO spool = _storagePoolDao.findById(_ds.getId());
85+
for (DataStoreEndPoint ep : dsep) {
86+
success = createStoragePool(ep, spool);
87+
if (success) {
88+
break;
89+
}
90+
}
91+
92+
List<DataStoreEndPoint> poolHosts = new ArrayList<DataStoreEndPoint>();
93+
for (DataStoreEndPoint ep : dsep) {
94+
try {
95+
connectHostToSharedPool(ep, spool);
96+
poolHosts.add(ep);
97+
} catch (Exception e) {
98+
s_logger.debug("Failed to add storage on this ep: " + ep.getHostId());
99+
}
100+
}
101+
}
102+
103+
public void delete() {
104+
// TODO Auto-generated method stub
105+
106+
}
107+
108+
public void enable() {
109+
// TODO Auto-generated method stub
110+
111+
}
112+
113+
public void disable() {
114+
// TODO Auto-generated method stub
115+
116+
}
117+
118+
public void processEvent(DataStoreEvent event, Object... objs) {
119+
// TODO Auto-generated method stub
120+
121+
}
122+
123+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.cloudstack.storage.manager;
20+
21+
import java.util.Iterator;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
import org.apache.cloudstack.platform.subsystem.api.storage.DataStore;
26+
import org.apache.cloudstack.platform.subsystem.api.storage.DataStore.StoreType;
27+
import org.apache.cloudstack.platform.subsystem.api.storage.DataStoreLifeCycle;
28+
import org.apache.cloudstack.platform.subsystem.api.storage.StorageProvider;
29+
30+
import com.cloud.dc.ClusterVO;
31+
import com.cloud.dc.DataCenterVO;
32+
import com.cloud.dc.dao.ClusterDao;
33+
import com.cloud.dc.dao.DataCenterDao;
34+
import com.cloud.dc.dao.HostPodDao;
35+
import com.cloud.hypervisor.Hypervisor.HypervisorType;
36+
import com.cloud.storage.StoragePool;
37+
import com.cloud.storage.StoragePoolStatus;
38+
import com.cloud.storage.StoragePoolVO;
39+
import com.cloud.storage.dao.StoragePoolDao;
40+
import com.cloud.utils.component.Adapters;
41+
import com.cloud.utils.component.Inject;
42+
import com.cloud.utils.exception.CloudRuntimeException;
43+
44+
public class StoragePoolManagerImpl implements StoragePoolService {
45+
@Inject(adapter = StorageProvider.class)
46+
protected Adapters<StorageProvider> _storageProviders;
47+
@Inject
48+
protected DataCenterDao _dcDao;
49+
@Inject
50+
protected HostPodDao _podDao;
51+
@Inject
52+
protected ClusterDao _clusterDao;
53+
@Inject
54+
protected StoragePoolDao _storagePoolDao;
55+
56+
public void deleteStoragePool(long poolId) {
57+
StoragePool spool = _storagePoolDao.findById(poolId);
58+
StorageProvider sp = findStorageProvider(spool.getStorageProvider());
59+
DataStore ds = sp.getDataStore(spool);
60+
DataStoreLifeCycle dslc = ds.getLifeCycle();
61+
dslc.delete();
62+
}
63+
64+
public void enableStoragePool(long poolId) {
65+
// TODO Auto-generated method stub
66+
67+
}
68+
69+
public void disableStoragePool(long poolId) {
70+
// TODO Auto-generated method stub
71+
72+
}
73+
74+
public Map<String, List<String>> getSupportedPrimaryStorages(long zoneId, HypervisorType hypervisor) {
75+
// TODO Auto-generated method stub
76+
return null;
77+
}
78+
79+
public Map<String, List<String>> getSupportedSecondaryStorages(long zoneId) {
80+
// TODO Auto-generated method stub
81+
return null;
82+
}
83+
84+
protected StorageProvider findStorageProvider(String name) {
85+
Iterator<StorageProvider> spIter = _storageProviders.iterator();
86+
StorageProvider sp = null;
87+
while (spIter.hasNext()) {
88+
sp = spIter.next();
89+
if (sp.getProviderName().equalsIgnoreCase(name)) {
90+
break;
91+
}
92+
}
93+
94+
return sp;
95+
}
96+
97+
public StoragePool addStoragePool(long zoneId, long podId, long clusterId, long hostId, String URI, String storageType, String poolName, String storageProviderName, Map<String, String> params) {
98+
StoragePoolVO spool = new StoragePoolVO();
99+
long poolId = _storagePoolDao.getNextInSequence(Long.class, "id");
100+
spool.setId(poolId);
101+
spool.setDataCenterId(zoneId);
102+
spool.setPodId(podId);
103+
spool.setName(poolName);
104+
spool.setClusterId(clusterId);
105+
spool.setStorageProvider(storageProviderName);
106+
spool.setStorageType(storageType);
107+
spool.setStatus(StoragePoolStatus.Creating);
108+
spool = _storagePoolDao.persist(spool);
109+
110+
StorageProvider sp = findStorageProvider(storageProviderName);
111+
DataStore ds = sp.addDataStore((StoragePool)spool, URI, params);
112+
113+
DataStoreLifeCycle dslc = ds.getLifeCycle();
114+
try {
115+
dslc.add();
116+
} catch (CloudRuntimeException e) {
117+
_storagePoolDao.remove(spool.getId());
118+
throw e;
119+
}
120+
121+
spool.setPath(ds.getURI());
122+
spool.setUuid(ds.getUUID());
123+
spool.setStatus(StoragePoolStatus.Up);
124+
_storagePoolDao.update(spool.getId(), spool);
125+
spool = _storagePoolDao.findById(spool.getId());
126+
return spool;
127+
}
128+
129+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.cloudstack.storage.manager;
20+
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import com.cloud.hypervisor.Hypervisor.HypervisorType;
25+
import com.cloud.storage.StoragePool;
26+
27+
public interface StoragePoolService {
28+
StoragePool addStoragePool(long zoneId, long podId, long clusterId, long hostId,
29+
String URI,
30+
String storageType,
31+
String poolName,
32+
String storageProviderName,
33+
Map<String, String> params);
34+
void deleteStoragePool(long poolId);
35+
void enableStoragePool(long poolId);
36+
void disableStoragePool(long poolId);
37+
Map<String, List<String>> getSupportedPrimaryStorages(long zoneId, HypervisorType hypervisor);
38+
Map<String, List<String>> getSupportedSecondaryStorages(long zoneId);
39+
}

0 commit comments

Comments
 (0)