Author: pferraro
Date: 2008-12-02 15:08:49 -0500 (Tue, 02 Dec 2008)
New Revision: 2085
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/impl/DynamicLoadBalanceFactorProvider.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/impl/DynamicLoadBalanceFactorProviderMBean.java
Log:
Consolidate source->metric and metric->load-queue into one map.
Add getMetrics() method.
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/impl/DynamicLoadBalanceFactorProvider.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/impl/DynamicLoadBalanceFactorProvider.java 2008-12-02
19:24:44 UTC (rev 2084)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/impl/DynamicLoadBalanceFactorProvider.java 2008-12-02
20:08:49 UTC (rev 2085)
@@ -23,8 +23,9 @@
import java.util.ArrayList;
import java.util.Collection;
-import java.util.HashMap;
+import java.util.Collections;
import java.util.LinkedHashMap;
+import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -33,6 +34,7 @@
import org.jboss.modcluster.load.LoadBalanceFactorProvider;
import org.jboss.modcluster.load.metric.LoadContext;
import org.jboss.modcluster.load.metric.LoadMetric;
+import org.jboss.modcluster.load.metric.LoadMetricMBean;
import org.jboss.modcluster.load.metric.LoadMetricSource;
/**
@@ -44,8 +46,7 @@
{
private final Logger log = Logger.getLogger(this.getClass());
- private final Map<LoadMetricSource<LoadContext>,
Collection<LoadMetric<LoadContext>>> metrics = new
LinkedHashMap<LoadMetricSource<LoadContext>,
Collection<LoadMetric<LoadContext>>>();
- private final Map<LoadMetric<LoadContext>, List<Double>> loadHistory
= new HashMap<LoadMetric<LoadContext>, List<Double>>();
+ private final Map<LoadMetricSource<LoadContext>,
Map<LoadMetric<LoadContext>, List<Double>>> loadHistory = new
LinkedHashMap<LoadMetricSource<LoadContext>,
Map<LoadMetric<LoadContext>, List<Double>>>();
private volatile int decayFactor = 2;
private volatile int history = 9;
@@ -55,20 +56,40 @@
for (LoadMetric<LoadContext> metric: metrics)
{
LoadMetricSource<LoadContext> source = metric.getSource();
- Collection<LoadMetric<LoadContext>> sourceMetrics =
this.metrics.get(source);
+ Map<LoadMetric<LoadContext>, List<Double>> sourceLoadHistory =
this.loadHistory.get(source);
- if (sourceMetrics == null)
+ if (sourceLoadHistory == null)
{
- sourceMetrics = new ArrayList<LoadMetric<LoadContext>>();
- this.metrics.put(source, sourceMetrics);
+ sourceLoadHistory = new LinkedHashMap<LoadMetric<LoadContext>,
List<Double>>();
+
+ this.loadHistory.put(source, sourceLoadHistory);
}
-
- sourceMetrics.add(metric);
+
+ sourceLoadHistory.put(metric, new ArrayList<Double>(this.history + 1));
}
}
/**
* @{inheritDoc}
+ * @see
org.jboss.modcluster.load.impl.DynamicLoadBalanceFactorProviderMBean#getMetrics()
+ */
+ public Collection<LoadMetricMBean> getMetrics()
+ {
+ Collection<LoadMetricMBean> metrics = new
LinkedList<LoadMetricMBean>();
+
+ for (Map<LoadMetric<LoadContext>, List<Double>>
sourceLoadHistory: this.loadHistory.values())
+ {
+ for (LoadMetric<LoadContext> metric: sourceLoadHistory.keySet())
+ {
+ metrics.add(metric);
+ }
+ }
+
+ return Collections.unmodifiableCollection(metrics);
+ }
+
+ /**
+ * @{inheritDoc}
* @see org.jboss.modcluster.load.LoadBalanceFactorProvider#getLoadBalanceFactor()
*/
public synchronized int getLoadBalanceFactor()
@@ -76,15 +97,15 @@
int totalWeight = 0;
double totalWeightedLoad = 0;
- for (Map.Entry<LoadMetricSource<LoadContext>,
Collection<LoadMetric<LoadContext>>> entry: this.metrics.entrySet())
+ for (Map.Entry<LoadMetricSource<LoadContext>,
Map<LoadMetric<LoadContext>, List<Double>>> loadHistoryEntry:
this.loadHistory.entrySet())
{
- LoadMetricSource<LoadContext> source = entry.getKey();
- Collection<LoadMetric<LoadContext>> metrics = entry.getValue();
+ LoadMetricSource<LoadContext> source = loadHistoryEntry.getKey();
+ Map<LoadMetric<LoadContext>, List<Double>> sourceLoadHistory =
loadHistoryEntry.getValue();
boolean skip = true;
// Skip this source if all weights are 0
- for (LoadMetric<LoadContext> metric: metrics)
+ for (LoadMetric<LoadContext> metric: sourceLoadHistory.keySet())
{
skip = skip && (metric.getWeight() <= 0);
}
@@ -95,19 +116,23 @@
try
{
- for (LoadMetric<LoadContext> metric: metrics)
+ for (Map.Entry<LoadMetric<LoadContext>, List<Double>>
entry: sourceLoadHistory.entrySet())
{
+ LoadMetric<LoadContext> metric = entry.getKey();
+
int weight = metric.getWeight();
if (weight > 0)
{
+ List<Double> metricLoadHistory = entry.getValue();
+
try
{
// Normalize load with respect to capacity
- List<Double> queue = this.recordLoad(metric,
metric.getLoad(context) / metric.getCapacity());
+ this.recordLoad(metricLoadHistory, metric.getLoad(context) /
metric.getCapacity());
totalWeight += weight;
- totalWeightedLoad += this.average(queue) * weight;
+ totalWeightedLoad += this.average(metricLoadHistory) * weight;
}
catch (Exception e)
{
@@ -130,18 +155,10 @@
return 100 - Math.max(0, Math.min(load, 100));
}
- private List<Double> recordLoad(LoadMetric<LoadContext> metric, double
load)
+ private void recordLoad(List<Double> queue, double load)
{
- List<Double> queue = this.loadHistory.get(metric);
-
- if (queue == null)
+ if (!queue.isEmpty())
{
- queue = new ArrayList<Double>(this.history + 1);
-
- this.loadHistory.put(metric, queue);
- }
- else
- {
// History could have changed, so prune queue accordingly
for (int i = (queue.size() - 1); i >= this.history; --i)
{
@@ -149,10 +166,8 @@
}
}
- // Add new load to the front
+ // Add new load to the head
queue.add(0, new Double(load));
-
- return queue;
}
/**
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/impl/DynamicLoadBalanceFactorProviderMBean.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/impl/DynamicLoadBalanceFactorProviderMBean.java 2008-12-02
19:24:44 UTC (rev 2084)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/impl/DynamicLoadBalanceFactorProviderMBean.java 2008-12-02
20:08:49 UTC (rev 2085)
@@ -21,7 +21,10 @@
*/
package org.jboss.modcluster.load.impl;
+import java.util.Collection;
+import org.jboss.modcluster.load.metric.LoadMetricMBean;
+
/**
* @author Paul Ferraro
*
@@ -51,4 +54,10 @@
* @param decayFactor the new decay factor
*/
void setDecayFactor(int decayFactor);
+
+ /**
+ * Returns the load metrics registered with this provider
+ * @return a collection of load metrics
+ */
+ Collection<LoadMetricMBean> getMetrics();
}