001/*
002 * JPPF.
003 * Copyright (C) 2005-2016 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.load.balancer.impl;
020
021import java.util.concurrent.atomic.AtomicReference;
022
023import org.jppf.load.balancer.*;
024import org.jppf.utils.*;
025import org.slf4j.*;
026
027/**
028 * Parameters profile for a proportional bundler.
029 * @author Laurent Cohen
030 */
031public class ProportionalProfile extends AbstractLoadBalancingProfile {
032  /**
033   * Logger for this class.
034   */
035  private static Logger log = LoggerFactory.getLogger(ProportionalProfile.class);
036  /**
037   * Determines whether debugging level is set for logging.
038   */
039  private static boolean debugEnabled = LoggingUtils.isDebugEnabled(log);
040  /**
041   * A default profile with default parameter values.
042   */
043  private static AtomicReference<ProportionalProfile> defaultProfile = new AtomicReference<>(new ProportionalProfile());
044  /**
045   * The maximum size of the performance samples cache.
046   */
047  private int performanceCacheSize = 2000;
048  /**
049   * The proportionality factor.
050   */
051  private int proportionalityFactor = 1;
052  /**
053   * The initial bundle size to use when the performance cache is empty, to bootstrap the algorithm.
054   */
055  private int initialSize = 10;
056  /**
057   * The initial value of the mean execution time, used to bootstrap the algorithm.
058   */
059  private double initialMeanTime = 1e9d;
060  /**
061   * The name of this profile.
062   */
063  private String name = "unknown";
064
065  /**
066   * Initialize this profile with default parameters.
067   */
068  public ProportionalProfile() {
069    if (debugEnabled) log.debug("in default constructor");
070  }
071
072  /**
073   * Initialize this profile with values read from the configuration file.
074   * @param config contains a mapping of the profile parameters to their value.
075   */
076  public ProportionalProfile(final TypedProperties config) {
077    if (debugEnabled) log.debug("in constructor without profile name");
078    performanceCacheSize = config.getInt("performanceCacheSize", 2000);
079    proportionalityFactor = config.getInt("proportionalityFactor", 1);
080    initialSize = config.getInt("initialSize", 10);
081    initialMeanTime = config.getDouble("initialMeanTime", 1e9d);
082    if (initialMeanTime < Double.MIN_VALUE) initialMeanTime = Double.MIN_VALUE;
083  }
084
085  /**
086   * Get the maximum size of the performance samples cache.
087   * @return the cache size as an int.
088   */
089  public int getPerformanceCacheSize() {
090    return performanceCacheSize;
091  }
092
093  /**
094   * Set the maximum size of the performance samples cache.
095   * @param performanceCacheSize the cache size as an int.
096   */
097  public void setPerformanceCacheSize(final int performanceCacheSize) {
098    this.performanceCacheSize = performanceCacheSize;
099  }
100
101  /**
102   * Get the proportionality factor.
103   * @return the factor as an int.
104   */
105  public int getProportionalityFactor() {
106    return proportionalityFactor;
107  }
108
109  /**
110   * Set the proportionality factor.
111   * @param proportionalityFactor the factor as an int.
112   */
113  public void setProportionalityFactor(final int proportionalityFactor) {
114    this.proportionalityFactor = proportionalityFactor;
115  }
116
117  /**
118   * Get the default profile with default parameter values.
119   * @return a <code>ProportionalTuneProfile</code> singleton instance.
120   */
121  public static ProportionalProfile getDefaultProfile() {
122    return defaultProfile.get();
123  }
124
125  /**
126   * Get the initial bundle size to use when the performance cache is empty.
127   * @return the initial size as an int.
128   */
129  public int getInitialSize() {
130    return initialSize;
131  }
132
133  /**
134   * Set the initial bundle size to use when the performance cache is empty.
135   * @param initialSize the initial size as an int.
136   */
137  public void setInitialSize(final int initialSize) {
138    this.initialSize = initialSize;
139  }
140
141  /**
142   * Get the initial value of the mean execution time, used to bootstrap the algorithm.
143   * @return the initial mean time as a double.
144   */
145  public double getInitialMeanTime() {
146    return initialMeanTime;
147  }
148
149  /**
150   * Set the initial value of the mean execution time, used to bootstrap the algorithm.
151   * @param initialMeanTime the initial mean time as a double.
152   */
153  public void setInitialMeanTime(final double initialMeanTime) {
154    this.initialMeanTime = initialMeanTime;
155  }
156
157  /**
158   * Return a string representation of this profile.
159   * @return this profile represented as a string value.
160   */
161  @Override
162  public String toString() {
163    StringBuilder sb = new StringBuilder();
164    sb.append("profileName=").append(name);
165    sb.append(", performanceCacheSize=").append(performanceCacheSize);
166    sb.append(", proportionalityFactor=").append(proportionalityFactor);
167    sb.append(", initialSize=").append(initialSize);
168    sb.append(", initialMeanTime=").append(initialMeanTime);
169    return sb.toString();
170  }
171}