cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ahu...@apache.org
Subject [19/36] Commit to try something on removing getZone
Date Fri, 06 Sep 2013 22:38:49 GMT
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/435e74e9/engine/orchestration/src/com/cloud/cluster/ClusteredAgentRebalanceService.java
----------------------------------------------------------------------
diff --git a/engine/orchestration/src/com/cloud/cluster/ClusteredAgentRebalanceService.java
b/engine/orchestration/src/com/cloud/cluster/ClusteredAgentRebalanceService.java
new file mode 100644
index 0000000..ed52eb1
--- /dev/null
+++ b/engine/orchestration/src/com/cloud/cluster/ClusteredAgentRebalanceService.java
@@ -0,0 +1,30 @@
+// 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 com.cloud.cluster;
+
+import com.cloud.exception.AgentUnavailableException;
+import com.cloud.exception.OperationTimedoutException;
+import com.cloud.host.Status.Event;
+
+public interface ClusteredAgentRebalanceService {
+    public static final int DEFAULT_TRANSFER_CHECK_INTERVAL = 10000;
+
+    void scheduleRebalanceAgents();
+
+    boolean executeRebalanceRequest(long agentId, long currentOwnerId, long futureOwnerId,
Event event) throws AgentUnavailableException, OperationTimedoutException;
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/435e74e9/engine/orchestration/src/com/cloud/cluster/agentlb/AgentLoadBalancerPlanner.java
----------------------------------------------------------------------
diff --git a/engine/orchestration/src/com/cloud/cluster/agentlb/AgentLoadBalancerPlanner.java
b/engine/orchestration/src/com/cloud/cluster/agentlb/AgentLoadBalancerPlanner.java
new file mode 100644
index 0000000..5a65250
--- /dev/null
+++ b/engine/orchestration/src/com/cloud/cluster/agentlb/AgentLoadBalancerPlanner.java
@@ -0,0 +1,29 @@
+// 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 com.cloud.cluster.agentlb;
+
+import java.util.List;
+
+import com.cloud.host.HostVO;
+import com.cloud.utils.component.Adapter;
+
+
+public interface AgentLoadBalancerPlanner extends Adapter{
+    
+    List<HostVO> getHostsToRebalance(long msId, int avLoad);
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/435e74e9/engine/orchestration/src/com/cloud/cluster/agentlb/ClusterBasedAgentLoadBalancerPlanner.java
----------------------------------------------------------------------
diff --git a/engine/orchestration/src/com/cloud/cluster/agentlb/ClusterBasedAgentLoadBalancerPlanner.java
b/engine/orchestration/src/com/cloud/cluster/agentlb/ClusterBasedAgentLoadBalancerPlanner.java
new file mode 100755
index 0000000..535ba07
--- /dev/null
+++ b/engine/orchestration/src/com/cloud/cluster/agentlb/ClusterBasedAgentLoadBalancerPlanner.java
@@ -0,0 +1,150 @@
+// 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 com.cloud.cluster.agentlb;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.ejb.Local;
+import javax.inject.Inject;
+import javax.naming.ConfigurationException;
+
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Component;
+
+import com.cloud.host.Host;
+import com.cloud.host.HostVO;
+import com.cloud.host.Status;
+import com.cloud.host.dao.HostDao;
+import com.cloud.utils.component.AdapterBase;
+import com.cloud.utils.db.SearchCriteria2;
+import com.cloud.utils.db.SearchCriteria.Op;
+import com.cloud.utils.db.SearchCriteriaService;
+
+@Component
+@Local(value=AgentLoadBalancerPlanner.class)
+public class ClusterBasedAgentLoadBalancerPlanner extends AdapterBase implements AgentLoadBalancerPlanner{
+    private static final Logger s_logger = Logger.getLogger(AgentLoadBalancerPlanner.class);
+    
+    @Inject HostDao _hostDao = null;
+    
+    @Override
+    public List<HostVO> getHostsToRebalance(long msId, int avLoad) {
+    	SearchCriteriaService<HostVO, HostVO> sc = SearchCriteria2.create(HostVO.class);
+    	sc.addAnd(sc.getEntity().getType(), Op.EQ, Host.Type.Routing);
+    	sc.addAnd(sc.getEntity().getManagementServerId(), Op.EQ, msId);
+        List<HostVO> allHosts = sc.list();
+ 
+        if (allHosts.size() <= avLoad) {
+            s_logger.debug("Agent load = " + allHosts.size() + " for management server "
+ msId + " doesn't exceed average system agent load = " + avLoad + "; so it doesn't participate
in agent rebalancing process");
+            return null;
+        }
+        
+        sc = SearchCriteria2.create(HostVO.class);
+        sc.addAnd(sc.getEntity().getManagementServerId(), Op.EQ, msId);
+        sc.addAnd(sc.getEntity().getStatus(), Op.EQ, Status.Up);
+        List<HostVO> directHosts = sc.list();
+        
+        if (directHosts.isEmpty()) {
+            s_logger.debug("No direct agents in status " + Status.Up + " exist for the management
server " + msId + "; so it doesn't participate in agent rebalancing process");
+            return null;
+        } 
+        
+       
+        Map<Long, List<HostVO>> hostToClusterMap = new HashMap<Long, List<HostVO>>();
+        
+        for (HostVO directHost : directHosts) {
+            Long clusterId = directHost.getClusterId();
+            List<HostVO> directHostsPerCluster = null;
+            if (!hostToClusterMap.containsKey(clusterId)) {
+                directHostsPerCluster = new ArrayList<HostVO>();
+            } else {
+                directHostsPerCluster = hostToClusterMap.get(clusterId);
+            }
+            directHostsPerCluster.add(directHost);
+            hostToClusterMap.put(clusterId, directHostsPerCluster);
+        }
+        
+        hostToClusterMap = sortByClusterSize(hostToClusterMap);
+        
+        int hostsToGive = allHosts.size() - avLoad;
+        int hostsLeftToGive = hostsToGive;
+        int hostsLeft = directHosts.size();
+        List<HostVO> hostsToReturn = new ArrayList<HostVO>();
+        
+        s_logger.debug("Management server " + msId + " can give away " + hostsToGive + "
as it currently owns " + allHosts.size() + 
+                " and the average agent load in the system is " + avLoad + "; finalyzing
list of hosts to give away...");
+        for (Long cluster : hostToClusterMap.keySet()) {
+            List<HostVO> hostsInCluster = hostToClusterMap.get(cluster);
+            hostsLeft = hostsLeft - hostsInCluster.size();
+            if (hostsToReturn.size() < hostsToGive) {
+                s_logger.debug("Trying cluster id=" + cluster);
+                
+                if (hostsInCluster.size() > hostsLeftToGive) {
+                    s_logger.debug("Skipping cluster id=" + cluster + " as it has more hosts
than we need: " + hostsInCluster.size() + " vs " + hostsLeftToGive);
+                    if (hostsLeft >= hostsLeftToGive) {
+                        continue;
+                    } else {
+                        break;
+                    }  
+                } else {
+                    s_logger.debug("Taking all " + hostsInCluster.size() + " hosts: " + hostsInCluster
+ " from cluster id=" + cluster);
+                    hostsToReturn.addAll(hostsInCluster);
+                    hostsLeftToGive = hostsLeftToGive - hostsInCluster.size();
+                }
+            } else {
+                break;
+            }
+        }
+        
+        s_logger.debug("Management server " + msId + " is ready to give away " + hostsToReturn.size()
+ " hosts");
+        return hostsToReturn;
+    }
+    
+    public static LinkedHashMap<Long, List<HostVO>> sortByClusterSize(final Map<Long,
List<HostVO>> hostToClusterMap) {
+        List<Long> keys = new ArrayList<Long>();
+        keys.addAll(hostToClusterMap.keySet());
+        Collections.sort(keys, new Comparator<Long>() {
+            @Override
+            public int compare(Long o1, Long o2) {
+                List<HostVO> v1 = hostToClusterMap.get(o1);
+                List<HostVO> v2 = hostToClusterMap.get(o2);
+                if (v1 == null) {
+                    return (v2 == null) ? 0 : 1;
+                }
+                
+                if (v1.size() < v2.size()) {
+                    return 1;
+                } else {
+                    return 0;
+                }
+            }
+        });
+        
+        LinkedHashMap<Long, List<HostVO>> sortedMap = new LinkedHashMap<Long,
List<HostVO>>();
+        for (Long key : keys) {
+            sortedMap.put(key, hostToClusterMap.get(key));
+        }
+        return sortedMap;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/435e74e9/engine/orchestration/src/com/cloud/network/NetworkStateListener.java
----------------------------------------------------------------------
diff --git a/engine/orchestration/src/com/cloud/network/NetworkStateListener.java b/engine/orchestration/src/com/cloud/network/NetworkStateListener.java
new file mode 100644
index 0000000..5a3ba2b
--- /dev/null
+++ b/engine/orchestration/src/com/cloud/network/NetworkStateListener.java
@@ -0,0 +1,107 @@
+// 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 com.cloud.network;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.inject.Inject;
+
+import org.apache.log4j.Logger;
+import org.springframework.beans.factory.NoSuchBeanDefinitionException;
+
+import org.apache.cloudstack.framework.events.EventBus;
+import org.apache.cloudstack.framework.events.EventBusException;
+
+import com.cloud.event.EventCategory;
+import com.cloud.event.dao.UsageEventDao;
+import com.cloud.network.Network.Event;
+import com.cloud.network.Network.State;
+import com.cloud.network.dao.NetworkDao;
+import com.cloud.utils.component.ComponentContext;
+import com.cloud.utils.fsm.StateListener;
+
+public class NetworkStateListener implements StateListener<State, Event, Network> {
+
+    @Inject protected UsageEventDao _usageEventDao;
+    @Inject protected NetworkDao _networkDao;
+
+    protected static EventBus _eventBus = null;
+
+    private static final Logger s_logger = Logger.getLogger(NetworkStateListener.class);
+
+    public NetworkStateListener(UsageEventDao usageEventDao, NetworkDao networkDao) {
+        _usageEventDao = usageEventDao;
+        _networkDao = networkDao;
+    }
+
+    @Override
+    public boolean preStateTransitionEvent(State oldState, Event event, State newState, Network
vo, boolean status, Object opaque) {
+        pubishOnEventBus(event.name(), "preStateTransitionEvent", vo, oldState, newState);
+        return true;
+    }
+
+    @Override
+    public boolean postStateTransitionEvent(State oldState, Event event, State newState,
Network vo, boolean status, Object opaque) {
+        pubishOnEventBus(event.name(), "postStateTransitionEvent", vo, oldState, newState);
+        return true;
+    }
+
+    private void pubishOnEventBus(String event, String status, Network vo, State oldState,
State newState) {
+
+        try {
+            _eventBus = ComponentContext.getComponent(EventBus.class);
+        } catch(NoSuchBeanDefinitionException nbe) {
+            return; // no provider is configured to provide events bus, so just return
+        }
+
+        String resourceName = getEntityFromClassName(Network.class.getName());
+        org.apache.cloudstack.framework.events.Event eventMsg =  new org.apache.cloudstack.framework.events.Event(
+"management-server",
+                EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(),
+                event,
+                resourceName,
+                vo.getUuid());
+        Map<String, String> eventDescription = new HashMap<String, String>();
+        eventDescription.put("resource", resourceName);
+        eventDescription.put("id", vo.getUuid());
+        eventDescription.put("old-state", oldState.name());
+        eventDescription.put("new-state", newState.name());
+
+        String eventDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").format(new Date());
+        eventDescription.put("eventDateTime", eventDate);
+
+        eventMsg.setDescription(eventDescription);
+        try {
+            _eventBus.publish(eventMsg);
+        } catch (EventBusException e) {
+            s_logger.warn("Failed to publish state change event on the the event bus.");
+        }
+    }
+
+    private String getEntityFromClassName(String entityClassName) {
+        int index = entityClassName.lastIndexOf(".");
+        String entityName = entityClassName;
+        if (index != -1) {
+            entityName = entityClassName.substring(index+1);
+        }
+        return entityName;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/435e74e9/engine/orchestration/src/com/cloud/network/rules/StaticNatRuleImpl.java
----------------------------------------------------------------------
diff --git a/engine/orchestration/src/com/cloud/network/rules/StaticNatRuleImpl.java b/engine/orchestration/src/com/cloud/network/rules/StaticNatRuleImpl.java
new file mode 100644
index 0000000..6103689
--- /dev/null
+++ b/engine/orchestration/src/com/cloud/network/rules/StaticNatRuleImpl.java
@@ -0,0 +1,147 @@
+// 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 com.cloud.network.rules;
+
+import java.util.List;
+
+
+public class StaticNatRuleImpl implements StaticNatRule {
+    long id;
+    String xid;
+    String uuid;
+    String protocol;
+    int portStart;
+    int portEnd;
+    State state;
+    long accountId;
+    long domainId;
+    long networkId;
+    long sourceIpAddressId;
+    String destIpAddress;
+
+    public StaticNatRuleImpl(FirewallRuleVO rule, String dstIp) {
+        this.id = rule.getId();
+        this.xid = rule.getXid();
+        this.uuid = rule.getUuid();
+        this.protocol = rule.getProtocol();
+        this.portStart = rule.getSourcePortStart();
+        this.portEnd = rule.getSourcePortEnd();
+        this.state = rule.getState();
+        this.accountId = rule.getAccountId();
+        this.domainId = rule.getDomainId();
+        this.networkId = rule.getNetworkId();
+        this.sourceIpAddressId = rule.getSourceIpAddressId();
+        this.destIpAddress = dstIp;
+    }
+
+    @Override
+    public String getProtocol() {
+        return protocol;
+    }
+
+    @Override
+    public Integer getSourcePortEnd() {
+        return portEnd;
+    }
+
+    @Override
+    public Purpose getPurpose() {
+        return Purpose.StaticNat;
+    }
+
+    @Override
+    public State getState() {
+        return state;
+    }
+
+    @Override
+    public long getAccountId() {
+        return accountId;
+    }
+
+    @Override
+    public long getDomainId() {
+        return domainId;
+    }
+
+    @Override
+    public long getNetworkId() {
+        return networkId;
+    }
+
+    @Override
+    public long getId() {
+        return id;
+    }
+
+    @Override
+    public Integer getSourcePortStart() {
+        return portStart;
+    }
+
+    @Override
+    public Long getSourceIpAddressId() {
+        return sourceIpAddressId;
+    }
+
+    @Override
+    public String getDestIpAddress() {
+        return destIpAddress;
+    }
+
+    @Override
+    public String getXid() {
+        return xid;
+    }
+
+
+    @Override
+    public String getUuid() {
+        return uuid;
+    }
+
+    @Override
+    public Integer getIcmpCode() {
+        return null;
+    }
+
+    @Override
+    public Integer getIcmpType() {
+        return null;
+    }
+
+    @Override
+    public List<String> getSourceCidrList() {
+        return null;
+    }
+
+    @Override
+    public Long getRelated() {
+        return null;
+    }
+
+	@Override
+	public FirewallRuleType getType() {
+		return FirewallRuleType.User;
+	}
+
+    @Override
+    public TrafficType getTrafficType() {
+        return null;
+    }
+
+}


Mime
View raw message