|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +// contributor license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright ownership. |
| 4 | +// The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +// (the "License"); you may not use this file except in compliance with |
| 6 | +// the License. You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package org.apache.cloudstack.storage.allocator; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import javax.ejb.Local; |
| 23 | + |
| 24 | +import org.apache.cloudstack.engine.subsystem.api.storage.ScopeType; |
| 25 | +import org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator; |
| 26 | +import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; |
| 27 | +import org.apache.log4j.Logger; |
| 28 | + |
| 29 | +import com.cloud.deploy.DeploymentPlan; |
| 30 | +import com.cloud.deploy.DeploymentPlanner.ExcludeList; |
| 31 | +import com.cloud.storage.StoragePool; |
| 32 | +import com.cloud.vm.DiskProfile; |
| 33 | +import com.cloud.vm.VirtualMachine; |
| 34 | +import com.cloud.vm.VirtualMachineProfile; |
| 35 | + |
| 36 | +@Local(value=StoragePoolAllocator.class) |
| 37 | +public class RandomStoragePoolAllocator extends AbstractStoragePoolAllocator { |
| 38 | + private static final Logger s_logger = Logger.getLogger(RandomStoragePoolAllocator.class); |
| 39 | + |
| 40 | + @Override |
| 41 | + public List<StoragePool> select(DiskProfile dskCh, VirtualMachineProfile<? extends VirtualMachine> vmProfile, DeploymentPlan plan, ExcludeList avoid, int returnUpTo) { |
| 42 | + |
| 43 | + List<StoragePool> suitablePools = new ArrayList<StoragePool>(); |
| 44 | + |
| 45 | + long dcId = plan.getDataCenterId(); |
| 46 | + Long podId = plan.getPodId(); |
| 47 | + Long clusterId = plan.getClusterId(); |
| 48 | + s_logger.debug("Looking for pools in dc: " + dcId + " pod:" + podId + " cluster:" + clusterId); |
| 49 | + List<StoragePoolVO> pools = _storagePoolDao.listBy(dcId, podId, clusterId, ScopeType.CLUSTER); |
| 50 | + if (pools.size() == 0) { |
| 51 | + if (s_logger.isDebugEnabled()) { |
| 52 | + s_logger.debug("No storage pools available for allocation, returning"); |
| 53 | + } |
| 54 | + return suitablePools; |
| 55 | + } |
| 56 | + |
| 57 | + Collections.shuffle(pools); |
| 58 | + if (s_logger.isDebugEnabled()) { |
| 59 | + s_logger.debug("RandomStoragePoolAllocator has " + pools.size() + " pools to check for allocation"); |
| 60 | + } |
| 61 | + for (StoragePoolVO pool: pools) { |
| 62 | + if(suitablePools.size() == returnUpTo){ |
| 63 | + break; |
| 64 | + } |
| 65 | + StoragePool pol = (StoragePool)this.dataStoreMgr.getPrimaryDataStore(pool.getId()); |
| 66 | + |
| 67 | + if (filter(avoid, pol, dskCh, plan)) { |
| 68 | + suitablePools.add(pol); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (s_logger.isDebugEnabled()) { |
| 73 | + s_logger.debug("RandomStoragePoolAllocator returning "+suitablePools.size() +" suitable storage pools"); |
| 74 | + } |
| 75 | + |
| 76 | + return suitablePools; |
| 77 | + } |
| 78 | +} |
0 commit comments