<commit_before>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */
package org.apache.ambari.server.state.configgroup;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.ReadWriteLock;

import org.apache.ambari.server.AmbariException;
import org.apache.ambari.server.DuplicateResourceException;
import org.apache.ambari.server.controller.ConfigGroupResponse;
import org.apache.ambari.server.controller.internal.ConfigurationResourceProvider;
import org.apache.ambari.server.logging.LockFactory;
import org.apache.ambari.server.orm.dao.ClusterDAO;
import org.apache.ambari.server.orm.dao.ConfigGroupConfigMappingDAO;
import org.apache.ambari.server.orm.dao.ConfigGroupDAO;
import org.apache.ambari.server.orm.dao.ConfigGroupHostMappingDAO;
import org.apache.ambari.server.orm.dao.HostDAO;
import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
import org.apache.ambari.server.orm.entities.ClusterEntity;
import org.apache.ambari.server.orm.entities.ConfigGroupConfigMappingEntity;
import org.apache.ambari.server.orm.entities.ConfigGroupEntity;
import org.apache.ambari.server.orm.entities.ConfigGroupHostMappingEntity;
import org.apache.ambari.server.orm.entities.ConfigGroupHostMappingEntityPK;
import org.apache.ambari.server.orm.entities.HostEntity;
import org.apache.ambari.server.state.Cluster;
import org.apache.ambari.server.state.Clusters;
import org.apache.ambari.server.state.Config;
import org.apache.ambari.server.state.ConfigFactory;
import org.apache.ambari.server.state.Host;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import com.google.inject.persist.Transactional;

public class ConfigGroupImpl implements ConfigGroup {
  private static final Logger LOG = LoggerFactory.getLogger(ConfigGroupImpl.class);

  private Cluster cluster;
  private ConcurrentMap<Long, Host> m_hosts;
  private ConcurrentMap<String, Config> m_configurations;
  private String configGroupName;
  private long configGroupId;

  /**
   * This lock is required to prevent inconsistencies in internal state between
   * {@link #m_hosts} and the entities stored by the {@link ConfigGroupEntity}.
   */
  private final ReadWriteLock hostLock;

  /**
   * A label for {@link #hostLock} to use with the {@link LockFactory}.
   */
  private static final String hostLockLabel = "configurationGroupHostLock";

  private final ConfigGroupDAO configGroupDAO;

  private final ConfigGroupConfigMappingDAO configGroupConfigMappingDAO;

  private final ConfigGroupHostMappingDAO configGroupHostMappingDAO;

  private final HostDAO hostDAO;

  private final ClusterDAO clusterDAO;

  private final ConfigFactory configFactory;

  @AssistedInject
  public ConfigGroupImpl(@Assisted("cluster") Cluster cluster, @Assisted("name") String name,
      @Assisted("tag") String tag, @Assisted("description") String description,
      @Assisted("configs") Map<String, Config> configurations,
      @Assisted("hosts") Map<Long, Host> hosts, Clusters clusters, ConfigFactory configFactory,
      ClusterDAO clusterDAO, HostDAO hostDAO, ConfigGroupDAO configGroupDAO,
      ConfigGroupConfigMappingDAO configGroupConfigMappingDAO,
      ConfigGroupHostMappingDAO configGroupHostMappingDAO, LockFactory lockFactory) {

    this.configFactory = configFactory;
    this.clusterDAO = clusterDAO;
    this.hostDAO = hostDAO;
    this.configGroupDAO = configGroupDAO;
    this.configGroupConfigMappingDAO = configGroupConfigMappingDAO;
    this.configGroupHostMappingDAO = configGroupHostMappingDAO;

    hostLock = lockFactory.newReadWriteLock(hostLockLabel);

    this.cluster = cluster;
    configGroupName = name;

    ConfigGroupEntity configGroupEntity = new ConfigGroupEntity();
    configGroupEntity.setClusterId(cluster.getClusterId());
    configGroupEntity.setGroupName(name);
    configGroupEntity.setTag(tag);
    configGroupEntity.setDescription(description);

    m_hosts = hosts == null ? new ConcurrentHashMap<Long, Host>()
        : new ConcurrentHashMap<>(hosts);

    m_configurations = configurations == null ? new ConcurrentHashMap<String, Config>()
        : new ConcurrentHashMap<>(configurations);

    // save the entity and grab the ID
    persist(configGroupEntity);
    configGroupId = configGroupEntity.getGroupId();
  }

  @AssistedInject
  public ConfigGroupImpl(@Assisted Cluster cluster, @Assisted ConfigGroupEntity configGroupEntity,
      Clusters clusters, ConfigFactory configFactory,
      ClusterDAO clusterDAO, HostDAO hostDAO, ConfigGroupDAO configGroupDAO,
      ConfigGroupConfigMappingDAO configGroupConfigMappingDAO,
      ConfigGroupHostMappingDAO configGroupHostMappingDAO, LockFactory lockFactory) {

    this.configFactory = configFactory;
    this.clusterDAO = clusterDAO;
    this.hostDAO = hostDAO;
    this.configGroupDAO = configGroupDAO;
    this.configGroupConfigMappingDAO = configGroupConfigMappingDAO;
    this.configGroupHostMappingDAO = configGroupHostMappingDAO;

    hostLock = lockFactory.newReadWriteLock(hostLockLabel);

    this.cluster = cluster;
    configGroupId = configGroupEntity.getGroupId();
    configGroupName = configGroupEntity.getGroupName();

    m_configurations = new ConcurrentHashMap<String, Config>();
    m_hosts = new ConcurrentHashMap<Long, Host>();

    // Populate configs
    for (ConfigGroupConfigMappingEntity configMappingEntity : configGroupEntity.getConfigGroupConfigMappingEntities()) {
      Config config = cluster.getConfig(configMappingEntity.getConfigType(),
        configMappingEntity.getVersionTag());

      if (config != null) {
        m_configurations.put(config.getType(), config);
      } else {
        LOG.warn("Unable to find config mapping {}/{} for config group in cluster {}",
            configMappingEntity.getConfigType(), configMappingEntity.getVersionTag(),
            cluster.getClusterName());
      }
    }

    // Populate Hosts
    for (ConfigGroupHostMappingEntity hostMappingEntity : configGroupEntity.getConfigGroupHostMappingEntities()) {
      try {
        Host host = clusters.getHost(hostMappingEntity.getHostname());
        HostEntity hostEntity = hostMappingEntity.getHostEntity();
        if (host != null && hostEntity != null) {
          m_hosts.put(hostEntity.getHostId(), host);
        }
      } catch (AmbariException e) {
        LOG.warn("Host seems to be deleted but Config group mapping still exists !");
        LOG.debug("Host seems to be deleted but Config group mapping still exists !", e);
      }
    }
  }

  @Override
  public Long getId() {
    return configGroupId;
  }

  @Override
  public String getName() {
    return configGroupName;
  }<commit_msg>AMBARI-19679. Dangling config-group hostmapping entries result in partial loading of config groups. (swagle)<commit_after>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */
package org.apache.ambari.server.state.configgroup;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.ReadWriteLock;

import org.apache.ambari.server.AmbariException;
import org.apache.ambari.server.DuplicateResourceException;
import org.apache.ambari.server.controller.ConfigGroupResponse;
import org.apache.ambari.server.controller.internal.ConfigurationResourceProvider;
import org.apache.ambari.server.logging.LockFactory;
import org.apache.ambari.server.orm.dao.ClusterDAO;
import org.apache.ambari.server.orm.dao.ConfigGroupConfigMappingDAO;
import org.apache.ambari.server.orm.dao.ConfigGroupDAO;
import org.apache.ambari.server.orm.dao.ConfigGroupHostMappingDAO;
import org.apache.ambari.server.orm.dao.HostDAO;
import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
import org.apache.ambari.server.orm.entities.ClusterEntity;
import org.apache.ambari.server.orm.entities.ConfigGroupConfigMappingEntity;
import org.apache.ambari.server.orm.entities.ConfigGroupEntity;
import org.apache.ambari.server.orm.entities.ConfigGroupHostMappingEntity;
import org.apache.ambari.server.orm.entities.ConfigGroupHostMappingEntityPK;
import org.apache.ambari.server.orm.entities.HostEntity;
import org.apache.ambari.server.state.Cluster;
import org.apache.ambari.server.state.Clusters;
import org.apache.ambari.server.state.Config;
import org.apache.ambari.server.state.ConfigFactory;
import org.apache.ambari.server.state.Host;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import com.google.inject.persist.Transactional;

public class ConfigGroupImpl implements ConfigGroup {
  private static final Logger LOG = LoggerFactory.getLogger(ConfigGroupImpl.class);

  private Cluster cluster;
  private ConcurrentMap<Long, Host> m_hosts;
  private ConcurrentMap<String, Config> m_configurations;
  private String configGroupName;
  private long configGroupId;

  /**
   * This lock is required to prevent inconsistencies in internal state between
   * {@link #m_hosts} and the entities stored by the {@link ConfigGroupEntity}.
   */
  private final ReadWriteLock hostLock;

  /**
   * A label for {@link #hostLock} to use with the {@link LockFactory}.
   */
  private static final String hostLockLabel = "configurationGroupHostLock";

  private final ConfigGroupDAO configGroupDAO;

  private final ConfigGroupConfigMappingDAO configGroupConfigMappingDAO;

  private final ConfigGroupHostMappingDAO configGroupHostMappingDAO;

  private final HostDAO hostDAO;

  private final ClusterDAO clusterDAO;

  private final ConfigFactory configFactory;

  @AssistedInject
  public ConfigGroupImpl(@Assisted("cluster") Cluster cluster, @Assisted("name") String name,
      @Assisted("tag") String tag, @Assisted("description") String description,
      @Assisted("configs") Map<String, Config> configurations,
      @Assisted("hosts") Map<Long, Host> hosts, Clusters clusters, ConfigFactory configFactory,
      ClusterDAO clusterDAO, HostDAO hostDAO, ConfigGroupDAO configGroupDAO,
      ConfigGroupConfigMappingDAO configGroupConfigMappingDAO,
      ConfigGroupHostMappingDAO configGroupHostMappingDAO, LockFactory lockFactory) {

    this.configFactory = configFactory;
    this.clusterDAO = clusterDAO;
    this.hostDAO = hostDAO;
    this.configGroupDAO = configGroupDAO;
    this.configGroupConfigMappingDAO = configGroupConfigMappingDAO;
    this.configGroupHostMappingDAO = configGroupHostMappingDAO;

    hostLock = lockFactory.newReadWriteLock(hostLockLabel);

    this.cluster = cluster;
    configGroupName = name;

    ConfigGroupEntity configGroupEntity = new ConfigGroupEntity();
    configGroupEntity.setClusterId(cluster.getClusterId());
    configGroupEntity.setGroupName(name);
    configGroupEntity.setTag(tag);
    configGroupEntity.setDescription(description);

    m_hosts = hosts == null ? new ConcurrentHashMap<Long, Host>()
        : new ConcurrentHashMap<>(hosts);

    m_configurations = configurations == null ? new ConcurrentHashMap<String, Config>()
        : new ConcurrentHashMap<>(configurations);

    // save the entity and grab the ID
    persist(configGroupEntity);
    configGroupId = configGroupEntity.getGroupId();
  }

  @AssistedInject
  public ConfigGroupImpl(@Assisted Cluster cluster, @Assisted ConfigGroupEntity configGroupEntity,
      Clusters clusters, ConfigFactory configFactory,
      ClusterDAO clusterDAO, HostDAO hostDAO, ConfigGroupDAO configGroupDAO,
      ConfigGroupConfigMappingDAO configGroupConfigMappingDAO,
      ConfigGroupHostMappingDAO configGroupHostMappingDAO, LockFactory lockFactory) {

    this.configFactory = configFactory;
    this.clusterDAO = clusterDAO;
    this.hostDAO = hostDAO;
    this.configGroupDAO = configGroupDAO;
    this.configGroupConfigMappingDAO = configGroupConfigMappingDAO;
    this.configGroupHostMappingDAO = configGroupHostMappingDAO;

    hostLock = lockFactory.newReadWriteLock(hostLockLabel);

    this.cluster = cluster;
    configGroupId = configGroupEntity.getGroupId();
    configGroupName = configGroupEntity.getGroupName();

    m_configurations = new ConcurrentHashMap<String, Config>();
    m_hosts = new ConcurrentHashMap<Long, Host>();

    // Populate configs
    for (ConfigGroupConfigMappingEntity configMappingEntity : configGroupEntity.getConfigGroupConfigMappingEntities()) {
      Config config = cluster.getConfig(configMappingEntity.getConfigType(),
        configMappingEntity.getVersionTag());

      if (config != null) {
        m_configurations.put(config.getType(), config);
      } else {
        LOG.warn("Unable to find config mapping {}/{} for config group in cluster {}",
            configMappingEntity.getConfigType(), configMappingEntity.getVersionTag(),
            cluster.getClusterName());
      }
    }

    // Populate Hosts
    for (ConfigGroupHostMappingEntity hostMappingEntity : configGroupEntity.getConfigGroupHostMappingEntities()) {
      try {
        Host host = clusters.getHost(hostMappingEntity.getHostname());
        HostEntity hostEntity = hostMappingEntity.getHostEntity();
        if (host != null && hostEntity != null) {
          m_hosts.put(hostEntity.getHostId(), host);
        }
      } catch (Exception e) {
        LOG.warn("Host {} seems to be deleted but Config group {} mapping " +
          "still exists !", hostMappingEntity.getHostname(), configGroupName);
        LOG.debug("Host seems to be deleted but Config group mapping still exists !", e);
      }
    }
  }

  @Override
  public Long getId() {
    return configGroupId;
  }

  @Override
  public String getName() {
    return configGroupName;
  }
{'fraction_non_alphanumeric': 0.08604770453962554, 'fraction_numerical': 0.0010900230828417544, 'mean_word_length': 4.7006578947368425, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '1468931', 'n_tokens_mistral': 4284, 'n_tokens_neox': 4238, 'n_words': 1038}