All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.ce.taskprocessor.EnabledCeWorkerControllerImpl Maven / Gradle / Ivy

The newest version!
/*
 * SonarQube
 * Copyright (C) 2009-2018 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.ce.taskprocessor;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.ce.configuration.CeConfiguration;

public class EnabledCeWorkerControllerImpl implements EnabledCeWorkerController {
  private final ConcurrentHashMap map = new ConcurrentHashMap<>();
  private final CeConfiguration ceConfiguration;
  private final AtomicInteger workerCount;

  enum Status {
    PROCESSING, PAUSED
  }

  public EnabledCeWorkerControllerImpl(CeConfiguration ceConfiguration) {
    this.ceConfiguration = ceConfiguration;
    this.workerCount = new AtomicInteger(ceConfiguration.getWorkerCount());
    logEnabledWorkerCount();
  }

  private void logEnabledWorkerCount() {
    if (workerCount.get() > 1) {
      Loggers.get(EnabledCeWorkerController.class).info("Compute Engine will use {} concurrent workers to process tasks", this.workerCount);
    }
  }

  @Override
  public void refresh() {
    ceConfiguration.refresh();
    this.workerCount.set(ceConfiguration.getWorkerCount());
    logEnabledWorkerCount();
  }

  @Override
  public ProcessingRecorderHook registerProcessingFor(CeWorker ceWorker) {
    return new ProcessingRecorderHookImpl(ceWorker);
  }

  @Override
  public boolean hasAtLeastOneProcessingWorker() {
    return map.entrySet().stream().anyMatch(e -> e.getValue() == Status.PROCESSING);
  }

  /**
   * Returns {@code true} if {@link CeWorker#getOrdinal() worker ordinal} is strictly less than
   * {@link CeConfiguration#getWorkerCount()}.
   *
   * This method does not fail if ordinal is invalid (ie. < 0).
   */
  @Override
  public boolean isEnabled(CeWorker ceWorker) {
    return ceWorker.getOrdinal() < workerCount.get();
  }

  private class ProcessingRecorderHookImpl implements ProcessingRecorderHook {
    private final CeWorker ceWorker;

    private ProcessingRecorderHookImpl(CeWorker ceWorker) {
      this.ceWorker = ceWorker;
      map.put(this.ceWorker, Status.PROCESSING);
    }

    @Override
    public void close() {
      map.put(ceWorker, Status.PAUSED);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy