001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 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 org.jppf.load.balancer.AbstractLoadBalancingProfile;
022import org.jppf.utils.TypedProperties;
023
024/**
025 * Parameters profile for the "rl2" algorithm.
026 * @author Laurent Cohen
027 */
028public class RL2Profile extends AbstractLoadBalancingProfile {
029  /**
030   * Explicit serialVersionUID.
031   */
032  private static final long serialVersionUID = 1L;
033  /**
034   * The maximum size of the performance samples cache.
035   */
036  private final int performanceCacheSize;
037  /**
038   * Variation of the mean execution time that triggers a reset of the bundler.
039   */
040  private final double performanceVariationThreshold;
041  /**
042   * Number of known states from which to start computing a probablity that the next action will be chsen randomly.
043   */
044  private final int minSamples;
045  /**
046   * Number of known states from which the learning phase is considered complete.
047   */
048  private final int maxSamples;
049  /**
050   * The maximum bundle size, expressed as a fraction in the range ]0.0, 1.0], of the current job size.
051   */
052  private final double maxRelativeSize;
053
054  /**
055   * Initialize this profile with values read from the specified configuration.
056   * @param config contains a mapping of the profile parameters to their value.
057   */
058  public RL2Profile(final TypedProperties config) {
059    int intValue = config.getInt("performanceCacheSize", 2000);
060    performanceCacheSize = (intValue <= 0) ? 2000 : intValue;
061    double doubleValue = config.getDouble("performanceVariationThreshold", 0.75d);
062    performanceVariationThreshold = (doubleValue <= 0d) ? 0.75d : doubleValue;
063    intValue = config.getInt("minSamples", 10);
064    minSamples = (intValue <= 0) ? 10 : intValue;
065    intValue = config.getInt("maxSamples", minSamples);
066    maxSamples = (intValue < minSamples) ? minSamples : intValue;
067    doubleValue = config.getDouble("maxRelativeSize", 0.5d);
068    maxRelativeSize =  ((doubleValue <= 0d) || (doubleValue > 1d)) ? 0.5d : doubleValue;
069  }
070
071  /**
072   * Get the variation of the mean execution time that triggers a reset of the bundler.
073   * @return the variation threshold as a {@code double} value..
074   */
075  public double getPerformanceVariationThreshold() {
076    return performanceVariationThreshold;
077  }
078
079  /**
080   * Get the maximum size of the performance samples cache.
081   * @return the cache size as an {@code int}.
082   */
083  public int getPerformanceCacheSize() {
084    return performanceCacheSize;
085  }
086
087  /**
088   * Get the number of known states from which to start computing a probablity that the next action will be chsen randomly.
089   * @return the min samples size as an {@code int}.
090   */
091  public int getMinSamples() {
092    return minSamples;
093  }
094
095  /**
096   * Get the number of known states from which the learning phase is considered complete.
097   * @return the min samples size as an {@code int}.
098   */
099  public int getMaxSamples() {
100    return maxSamples;
101  }
102
103  /**
104   * Get the maximum bundle size, expressed as a fraction in the range ]0.0, 1.0], of the current job size.
105   * @return the max relative size as a {@code double} value between 0 and 1.
106   */
107  public double getMaxRelativeSize() {
108    return maxRelativeSize;
109  }
110
111  @Override
112  public String toString() {
113    return String.format("%s[VariationThreshold=%f, minSamples=%d, maxSamples=%d, maxRelativeSize=%f]",
114      getClass().getSimpleName(), performanceVariationThreshold, minSamples, maxSamples, maxRelativeSize);
115  }
116}