Package com.ericsson.ssa.container.overload

Source Code of com.ericsson.ssa.container.overload.OverloadCPUMemoryDetectorBase

/*
* 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.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.glassfish.comms.api.overload.OverloadEvent;
import org.glassfish.comms.api.overload.OverloadEvent.TrafficType;
import org.jvnet.glassfish.comms.util.LogUtil;

import com.ericsson.ssa.container.overload.OverloadMeasurement.Algorithm;
import com.ericsson.ssa.container.overload.OverloadMeasurement.TrafficState;
import com.ericsson.util.os.OSUtil;
import com.ericsson.util.os.OSUtilFactory;

public abstract class OverloadCPUMemoryDetectorBase extends
    OverloadDetectorBase {
  private static final Logger LOGGER = LogUtil.SIP_LOGGER.getLogger();

  protected final OSUtil _osUtil;
  private OverloadMeasurement measurement = null;
  private TrafficState sipSubsequent = null;
  private TrafficState sipInitial = null;
  private TrafficState http = null;
  private TrafficState all = null;

  public OverloadCPUMemoryDetectorBase(OverloadConfiguration config) {
    super(config);
    String allProcess = System.getProperty("AllProcess");

    // If AllProcess will be set to true default, if it not set as -D
    // attribute.
    String allProcessFlag = "true"; // non-null means set which is default

    if ("false".equals(allProcess)) {
      allProcessFlag = null; // unset the flag
    }

    if (LOGGER.isLoggable(Level.FINEST)) {
      LOGGER.log(Level.FINEST, "logging usage for all processes? "
          + ((allProcess == null) ? "false" : "true"));
    }

    _osUtil = getOsUtil(allProcessFlag);
  }

  protected OSUtil getOsUtil(String allProcessFlag) {
    OSUtil osUtil = OSUtilFactory.getOSUtil(allProcessFlag);

    if (osUtil == null) {
      LOGGER.log(Level.WARNING, getClass().getCanonicalName()
          + ".no_os_utils");
    } else {
      LOGGER.log(Level.FINEST, getClass().getCanonicalName()
          + osUtil.getInfo());
    }
    return osUtil;
  }

  public void activate() {
    measurement = new OverloadMeasurement();
    sipInitial = measurement.new TrafficState(type(),
        TrafficType.SIP_INITIAL);
    sipSubsequent = measurement.new TrafficState(type(),
        TrafficType.SIP_SUBSEQUENT);
    http = measurement.new TrafficState(type(), TrafficType.HTTP);
    all = measurement.new TrafficState(type(), TrafficType.ALL);
  }

  /**
   * NOTE: Cease events will always be sent when a detector is removed whether
   * or not raise has been sent before
   */
  public void deactivate() {
    ceaseOverload(sipInitial.createCeased(0));
    ceaseOverload(sipSubsequent.createCeased(0));
    ceaseOverload(http.createCeased(0));
    ceaseOverload(all.createCeased(0));
    measurement = null;
    sipSubsequent = null;
    sipInitial = null;
    http = null;
    all = null;
  }

  public int retryAfter(TrafficType trafficType) {
    switch (trafficType) {
    case SIP_INITIAL:
      return sipInitial.retryAfter();

    case SIP_SUBSEQUENT:
      return sipSubsequent.retryAfter();

    case HTTP:
      return http.retryAfter();

    case ALL:
      return all.retryAfter();
    }
    throw new IllegalStateException(
        "TrafficType is not supported = " + trafficType);
  }

  @Override
  protected List<OverloadEvent> detect(int sampleRate, int numberOfSamples) {
    ArrayList<OverloadEvent> events = new ArrayList<OverloadEvent>();

    measurement.saveMeasurement(usage(), numberOfSamples,
        getOverloadActivationAlgorithm(),
        getOverloadDeactivationAlgorithm());

    OverloadEvent initialSipAction = sipInitial
        .compareThreshold(getIrThreshold());
    if (initialSipAction != null) {
      events.add(initialSipAction);
    }
    OverloadEvent subsequentSipAction = sipSubsequent
        .compareThreshold(getSrThreshold());
    if (subsequentSipAction != null) {
      events.add(subsequentSipAction);
    }

    OverloadEvent httpAction = http.compareThreshold(getHttpThreshold());
    if (httpAction != null) {
      events.add(httpAction);
    }

    OverloadEvent allAction = all.compareThreshold(getMmThreshold());
    if (allAction != null) {
      events.add(allAction);
    }

    return events;
  }

  protected abstract int usage();

  public abstract Algorithm getOverloadActivationAlgorithm();

  public abstract Algorithm getOverloadDeactivationAlgorithm();

  public abstract Integer getIrThreshold();

  public abstract Integer getSrThreshold();

  public abstract Integer getHttpThreshold();

  public abstract Integer getMmThreshold();
}
TOP

Related Classes of com.ericsson.ssa.container.overload.OverloadCPUMemoryDetectorBase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.