/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) Ericsson AB, 2004-2008. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.ericsson.ssa.container.overload;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.comms.api.overload.OverloadEvent;
import org.jvnet.glassfish.comms.util.LogUtil;
public class OverloadMeasurement {
private static final Logger LOGGER = LogUtil.SIP_LOGGER.getLogger();
public enum Algorithm {
CONSECUTIVE, MEDIAN;
}
/**
* The last X number of samples, used when calculating median values.
*/
private int[] samples = new int[0];
private int numberOfSamples;
private Algorithm upAlgorithm;
private Algorithm downAlgorithm;
private float consecutiveUsage;
private float medianUsage;
public void saveMeasurement(int usage, int numberOfSamples,
Algorithm upAlgorithm, Algorithm downAlgorithm) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINER, "measure: " + usage);
}
this.upAlgorithm = upAlgorithm;
this.downAlgorithm = downAlgorithm;
this.numberOfSamples = numberOfSamples;
// Make sure we have all measurements ready. Let unavailable ones be
// 0.
// How many shall we copy?
int length = Math.min(numberOfSamples - 1, samples.length);
int[] newSamples = new int[numberOfSamples];
System.arraycopy(samples, 0, newSamples, 1, length);
newSamples[0] = usage;
samples = newSamples;
// Now calculate what we need. We already have the last usage...
consecutiveUsage = usage;
int[] samplesTmp = new int[numberOfSamples];
System.arraycopy(samples, 0, samplesTmp, 0, samplesTmp.length);
Arrays.sort(samplesTmp);
// Even or odd number of samples?
if ((numberOfSamples & 1) == 1) {
// Odd. Integer division rounds down, that's what we want.
medianUsage = samplesTmp[numberOfSamples / 2];
} else {
// Take the mean of the two middle samples
medianUsage = (samplesTmp[(numberOfSamples / 2)] + samplesTmp[(numberOfSamples / 2) - 1])
/ (float) 2.0;
}
}
public class TrafficState {
private final OverloadEvent.TrafficType trafficType;
private AtomicInteger levelCounterUp = new AtomicInteger(0);
private AtomicInteger levelCounterDown = new AtomicInteger(0);
private boolean isRaised = false;
private final String detectorType;
public TrafficState(String type, OverloadEvent.TrafficType trafficType) {
super();
this.detectorType = type;
this.trafficType = trafficType;
}
public OverloadEvent createCeased(float usage) {
return new OverloadEvent(false, detectorType, trafficType, usage,
null);
}
public OverloadEvent createRaised(float usage) {
return new OverloadEvent(true, detectorType, trafficType, usage,
null);
}
/**
* If event has been raised, MEDIAN will return the fix numberOfSamples
* and CONSECUTIVE will return the number of samples since event was
* last raised. If event was ceased 0 will always be returned. NOTE:
* MEDIAN or CONSECUTIVE is only applicable for the up algorithm.
*
* @return
*/
public int retryAfter() {
return (isRaised && (upAlgorithm == Algorithm.MEDIAN)) ? numberOfSamples
: levelCounterUp.get();
}
/**
* Compares the measured load to the thresholds and adjusts counters as
* needed.
*/
public OverloadEvent compareThreshold(int threshold) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINER, "calculateThreshold: current usage(%)="
+ consecutiveUsage + " median usage(%)=" + medianUsage
+ " levelCounter=" + levelCounterUp.toString()
+ " levelCounterDown=" + levelCounterDown.toString()
+ " threshold=" + threshold + " numberOfSamples="
+ numberOfSamples + " upAlgorithm="
+ upAlgorithm.toString() + " downAlgorithm="
+ downAlgorithm.toString());
}
OverloadEvent event = null;
if (!isRaised) {
switch (upAlgorithm) {
case CONSECUTIVE:
if (consecutiveUsage >= threshold) {
if (levelCounterUp.incrementAndGet() >= numberOfSamples) {
isRaised = true;
event = createRaised(consecutiveUsage);
}
}
break;
case MEDIAN:
if (medianUsage >= threshold) {
levelCounterUp.set(1);
isRaised = true;
event = createRaised(medianUsage);
}
break;
default:
throw new RuntimeException(
"Internal error - unknown algorithm");
}
}
if (isRaised) {
switch (downAlgorithm) {
case CONSECUTIVE:
if (consecutiveUsage < threshold) {
if (levelCounterDown.decrementAndGet() <= 0) {
levelCounterUp.set(0);
isRaised = false;
event = createCeased(consecutiveUsage);
}
} else {
levelCounterDown.set(numberOfSamples);
}
break;
case MEDIAN:
if (medianUsage < threshold) {
levelCounterUp.set(0);
isRaised = false;
event = createCeased(medianUsage);
} else {
levelCounterDown.set(1);
}
break;
default:
throw new RuntimeException(
"Internal error - unknown algorithm");
}
// extra counting for retry after functionality
if (isRaised && upAlgorithm==Algorithm.CONSECUTIVE) {
levelCounterUp.incrementAndGet();
}
}
return event;
}
}
}