<commit_before>////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2014-2018 ArangoDB GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author <NAME>
/// @author <NAME>
////////////////////////////////////////////////////////////////////////////////

#include "MaintenanceFeature.h"

#include "ApplicationFeatures/ApplicationServer.h"
#include "Basics/ConditionLocker.h"
#include "Basics/ReadLocker.h"
#include "Basics/WriteLocker.h"
#include "Basics/MutexLocker.h"
#include "Cluster/ActionDescription.h"
#include "Cluster/CreateDatabase.h"
#include "Cluster/Action.h"
#include "Cluster/MaintenanceWorker.h"
#include "Cluster/ServerState.h"

using namespace arangodb;
using namespace arangodb::application_features;
using namespace arangodb::options;
using namespace arangodb::maintenance;

const uint32_t MaintenanceFeature::minThreadLimit = 2;
const uint32_t MaintenanceFeature::maxThreadLimit = 64;

MaintenanceFeature::MaintenanceFeature(application_features::ApplicationServer& server)
  : ApplicationFeature(server, "Maintenance"),
    _forceActivation(false),
    _maintenanceThreadsMax(2) {
  // the number of threads will be adjusted later. it's just that we want to initialize all members properly

  // this feature has to know the role of this server in its `start`. The role
  // is determined by `ClusterFeature::validateOptions`, hence the following line
  // of code is not required. For philosophical reasons we added it to the
  // ClusterPhase and let it start after `Cluster`.
  startsAfter("Cluster");

  init();
} // MaintenanceFeature::MaintenanceFeature

void MaintenanceFeature::init() {
  _isShuttingDown = false;
  _nextActionId = 1;

  setOptional(true);
  requiresElevatedPrivileges(false); // ??? this mean admin priv?

  // these parameters might be updated by config and/or command line options

  _maintenanceThreadsMax = (std::max)(static_cast<uint32_t>(minThreadLimit),
    static_cast<uint32_t>(TRI_numberProcessors() / 4 + 1));
  _secondsActionsBlock = 2;
  _secondsActionsLinger = 3600;
} // MaintenanceFeature::init


void MaintenanceFeature::collectOptions(std::shared_ptr<ProgramOptions> options) {
  options->addSection("server", "Server features");

  options->addHiddenOption(
    "--server.maintenance-threads",
    "maximum number of threads available for maintenance actions",
    new UInt32Parameter(&_maintenanceThreadsMax));

  options->addHiddenOption(
    "--server.maintenance-actions-block",
    "minimum number of seconds finished Actions block duplicates",
    new Int32Parameter(&_secondsActionsBlock));

  options->addHiddenOption(
    "--server.maintenance-actions-linger",
    "minimum number of seconds finished Actions remain in deque",
    new Int32Parameter(&_secondsActionsLinger));

} // MaintenanceFeature::collectOptions

void MaintenanceFeature::validateOptions(std::shared_ptr<ProgramOptions> options) {

  if (_maintenanceThreadsMax < minThreadLimit) {
    LOG_TOPIC(WARN, Logger::MAINTENANCE)
      << "Need at least" << minThreadLimit << "maintenance-threads";
    _maintenanceThreadsMax = minThreadLimit;
  } else if (_maintenanceThreadsMax >= maxThreadLimit) {
    LOG_TOPIC(WARN, Logger::MAINTENANCE)
      << "maintenance-threads limited to " << minThreadLimit;
    _maintenanceThreadsMax = minThreadLimit;
  }
}

/// do not start threads in prepare
void MaintenanceFeature::prepare() {
} // MaintenanceFeature::prepare


void MaintenanceFeature::start() {
  auto serverState = ServerState::instance();

  // _forceActivation is set by the catch tests
  if (!_forceActivation &&
      (serverState->isAgent() || serverState->isSingleServer())) {<commit_msg>fixed typo in limiting the thread num (#6347)<commit_after>////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2014-2018 ArangoDB GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author <NAME>
/// @author <NAME>
////////////////////////////////////////////////////////////////////////////////

#include "MaintenanceFeature.h"

#include "ApplicationFeatures/ApplicationServer.h"
#include "Basics/ConditionLocker.h"
#include "Basics/ReadLocker.h"
#include "Basics/WriteLocker.h"
#include "Basics/MutexLocker.h"
#include "Cluster/ActionDescription.h"
#include "Cluster/CreateDatabase.h"
#include "Cluster/Action.h"
#include "Cluster/MaintenanceWorker.h"
#include "Cluster/ServerState.h"

using namespace arangodb;
using namespace arangodb::application_features;
using namespace arangodb::options;
using namespace arangodb::maintenance;

const uint32_t MaintenanceFeature::minThreadLimit = 2;
const uint32_t MaintenanceFeature::maxThreadLimit = 64;

MaintenanceFeature::MaintenanceFeature(application_features::ApplicationServer& server)
  : ApplicationFeature(server, "Maintenance"),
    _forceActivation(false),
    _maintenanceThreadsMax(2) {
  // the number of threads will be adjusted later. it's just that we want to initialize all members properly

  // this feature has to know the role of this server in its `start`. The role
  // is determined by `ClusterFeature::validateOptions`, hence the following line
  // of code is not required. For philosophical reasons we added it to the
  // ClusterPhase and let it start after `Cluster`.
  startsAfter("Cluster");

  init();
} // MaintenanceFeature::MaintenanceFeature

void MaintenanceFeature::init() {
  _isShuttingDown = false;
  _nextActionId = 1;

  setOptional(true);
  requiresElevatedPrivileges(false); // ??? this mean admin priv?

  // these parameters might be updated by config and/or command line options

  _maintenanceThreadsMax = (std::max)(static_cast<uint32_t>(minThreadLimit),
    static_cast<uint32_t>(TRI_numberProcessors() / 4 + 1));
  _secondsActionsBlock = 2;
  _secondsActionsLinger = 3600;
} // MaintenanceFeature::init


void MaintenanceFeature::collectOptions(std::shared_ptr<ProgramOptions> options) {
  options->addSection("server", "Server features");

  options->addHiddenOption(
    "--server.maintenance-threads",
    "maximum number of threads available for maintenance actions",
    new UInt32Parameter(&_maintenanceThreadsMax));

  options->addHiddenOption(
    "--server.maintenance-actions-block",
    "minimum number of seconds finished Actions block duplicates",
    new Int32Parameter(&_secondsActionsBlock));

  options->addHiddenOption(
    "--server.maintenance-actions-linger",
    "minimum number of seconds finished Actions remain in deque",
    new Int32Parameter(&_secondsActionsLinger));

} // MaintenanceFeature::collectOptions

void MaintenanceFeature::validateOptions(std::shared_ptr<ProgramOptions> options) {

  if (_maintenanceThreadsMax < minThreadLimit) {
    LOG_TOPIC(WARN, Logger::MAINTENANCE)
      << "Need at least" << minThreadLimit << "maintenance-threads";
    _maintenanceThreadsMax = minThreadLimit;
  } else if (_maintenanceThreadsMax >= maxThreadLimit) {
    LOG_TOPIC(WARN, Logger::MAINTENANCE)
      << "maintenance-threads limited to " << minThreadLimit;
    _maintenanceThreadsMax = maxThreadLimit;
  }
}

/// do not start threads in prepare
void MaintenanceFeature::prepare() {
} // MaintenanceFeature::prepare


void MaintenanceFeature::start() {
  auto serverState = ServerState::instance();

  // _forceActivation is set by the catch tests
  if (!_forceActivation &&
      (serverState->isAgent() || serverState->isSingleServer())) {
{'fraction_non_alphanumeric': 0.14373265494912119, 'fraction_numerical': 0.009250693802035153, 'mean_word_length': 3.886440677966102, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3384011', 'n_tokens_mistral': 2397, 'n_tokens_neox': 2258, 'n_words': 683}