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.locks.ReentrantLock;
022
023import org.jppf.load.balancer.*;
024import org.slf4j.*;
025
026/**
027 * Instances of this bundler delegate their operations to a singleton instance of a
028 * {@link org.jppf.load.balancer.impl.AutoTunedBundler AutoTunedBundler}.
029 * @author Laurent Cohen
030 */
031public class AutotunedDelegatingBundler extends AbstractBundler<AnnealingTuneProfile> {
032  /**
033   * Logger for this class.
034   */
035  private static Logger log = LoggerFactory.getLogger(AutotunedDelegatingBundler.class);
036  /**
037   * The global bundler to which bundle size calculations are delegated.
038   */
039  private static AutoTunedBundler simpleBundler = null;
040  /**
041   * Used to synchronize multiple threads when creating the simple bundler.
042   */
043  private final static ReentrantLock lock = new ReentrantLock();
044
045  /**
046   * Creates a new instance with the initial size of bundle as the start size.
047   * @param profile the parameters of the auto-tuning algorithm grouped as a performance analysis profile.
048   */
049  public AutotunedDelegatingBundler(final AnnealingTuneProfile profile) {
050    super(profile);
051    log.info("Bundler#" + bundlerNumber + ": Using Auto-Tuned bundle size");
052    //log.info("Bundler#" + bundlerNumber + ": The initial size is " + bundleSize);
053    lock.lock();
054    try {
055      if (simpleBundler == null) simpleBundler = new AutoTunedBundler(profile);
056    } finally {
057      lock.unlock();
058    }
059  }
060
061  /**
062   * Get the current size of bundle.
063   * @return the bundle size as an int value.
064   */
065  @Override
066  public int getBundleSize() {
067    return simpleBundler.getBundleSize();
068  }
069
070  /**
071   * This method delegates the bundle size calculation to the singleton instance of <code>SimpleBundler</code>.
072   * @param bundleSize the number of tasks executed.
073   * @param totalTime the time in nanoseconds it took to execute the tasks.
074   */
075  @Override
076  public void feedback(final int bundleSize, final double totalTime) {
077    simpleBundler.feedback(bundleSize, totalTime);
078  }
079
080  /**
081   * Get the max bundle size that can be used for this bundler.
082   * @return the bundle size as an int.
083   */
084  @Override
085  public int maxSize() {
086    int max = 0;
087    synchronized (simpleBundler) {
088      max = simpleBundler.maxSize();
089    }
090    return max;
091  }
092}