1818 */
1919package com .cloud .storage ;
2020
21+ import java .util .ArrayList ;
2122import java .util .List ;
2223
2324import javax .inject .Inject ;
2728import org .apache .cloudstack .engine .subsystem .api .storage .DataStoreManager ;
2829import org .apache .cloudstack .engine .subsystem .api .storage .DataStoreProvider ;
2930import org .apache .cloudstack .engine .subsystem .api .storage .DataStoreProviderManager ;
31+ import org .apache .cloudstack .engine .subsystem .api .storage .Scope ;
3032import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
3133import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
3234import org .apache .log4j .Logger ;
3638import com .cloud .agent .api .Answer ;
3739import com .cloud .agent .api .ModifyStoragePoolCommand ;
3840import com .cloud .alert .AlertManager ;
41+ import com .cloud .host .Host ;
3942import com .cloud .host .HostVO ;
4043import com .cloud .host .Status ;
44+ import com .cloud .hypervisor .Hypervisor .HypervisorType ;
4145import com .cloud .resource .ResourceManager ;
4246import com .cloud .server .ManagementServer ;
4347import com .cloud .storage .dao .StoragePoolHostDao ;
@@ -101,17 +105,45 @@ public class StoragePoolAutomationImpl implements StoragePoolAutomation {
101105 @ Inject
102106 ManagementServer server ;
103107 @ Inject DataStoreProviderManager providerMgr ;
104-
108+
105109 @ Override
106110 public boolean maintain (DataStore store ) {
107111 Long userId = UserContext .current ().getCallerUserId ();
108112 User user = _userDao .findById (userId );
109113 Account account = UserContext .current ().getCaller ();
110114 StoragePoolVO pool = this .primaryDataStoreDao .findById (store .getId ());
111115 try {
116+ List <StoragePoolVO > spes = null ;
117+ // Handling Zone and Cluster wide storage scopes.
118+ // if the storage is ZONE wide then we pass podid and cluster id as null as they will be empty for ZWPS
119+ if (pool .getScope () == ScopeType .ZONE ) {
120+ spes = primaryDataStoreDao .listBy (
121+ pool .getDataCenterId (), null ,
122+ null , ScopeType .ZONE );
123+ }
124+ else {
125+ spes = primaryDataStoreDao .listBy (
126+ pool .getDataCenterId (), pool .getPodId (),
127+ pool .getClusterId (), ScopeType .CLUSTER );
128+ }
129+ for (StoragePoolVO sp : spes ) {
130+ if (sp .getStatus () == StoragePoolStatus .PrepareForMaintenance ) {
131+ throw new CloudRuntimeException ("Only one storage pool in a cluster can be in PrepareForMaintenance mode, " + sp .getId ()
132+ + " is already in PrepareForMaintenance mode " );
133+ }
134+ }
112135 StoragePool storagePool = (StoragePool ) store ;
113- List <HostVO > hosts = _resourceMgr .listHostsInClusterByStatus (
114- pool .getClusterId (), Status .Up );
136+
137+ //Handeling the Zone wide and cluster wide primay storage
138+ List <HostVO > hosts = new ArrayList <HostVO >();
139+ // if the storage scope is ZONE wide, then get all the hosts for which hypervisor ZWSP created to send Modifystoragepoolcommand
140+ if (pool .getScope ().equals (ScopeType .ZONE )) {
141+ hosts = _resourceMgr .listAllUpAndEnabledHostsInOneZoneByHypervisor (pool .getHypervisor () , pool .getDataCenterId ());
142+ } else {
143+ hosts = _resourceMgr .listHostsInClusterByStatus (
144+ pool .getClusterId (), Status .Up );
145+ }
146+
115147 if (hosts == null || hosts .size () == 0 ) {
116148 pool .setStatus (StoragePoolStatus .Maintenance );
117149 primaryDataStoreDao .update (pool .getId (), pool );
@@ -130,11 +162,11 @@ public boolean maintain(DataStore store) {
130162 if (s_logger .isDebugEnabled ()) {
131163 s_logger .debug ("ModifyStoragePool false failed due to "
132164 + ((answer == null ) ? "answer null" : answer
133- .getDetails ()));
165+ .getDetails ()));
134166 }
135167 } else {
136168 if (s_logger .isDebugEnabled ()) {
137- s_logger .debug ("ModifyStoragePool false secceeded " );
169+ s_logger .debug ("ModifyStoragePool false succeeded " );
138170 }
139171 }
140172 }
@@ -314,7 +346,6 @@ public boolean maintain(DataStore store) {
314346 }
315347 }
316348 }
317-
318349 } catch (Exception e ) {
319350 s_logger .error (
320351 "Exception in enabling primary storage maintenance:" , e );
@@ -334,7 +365,7 @@ public boolean cancelMaintain(DataStore store) {
334365 StoragePoolVO poolVO = this .primaryDataStoreDao
335366 .findById (store .getId ());
336367 StoragePool pool = (StoragePool )store ;
337-
368+
338369 List <HostVO > hosts = _resourceMgr .listHostsInClusterByStatus (
339370 pool .getClusterId (), Status .Up );
340371 if (hosts == null || hosts .size () == 0 ) {
@@ -349,7 +380,7 @@ public boolean cancelMaintain(DataStore store) {
349380 if (s_logger .isDebugEnabled ()) {
350381 s_logger .debug ("ModifyStoragePool add failed due to "
351382 + ((answer == null ) ? "answer null" : answer
352- .getDetails ()));
383+ .getDetails ()));
353384 }
354385 } else {
355386 if (s_logger .isDebugEnabled ()) {
0 commit comments