<commit_before>package com.linkedin.clustermanager.controller.stages;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.linkedin.clustermanager.ClusterDataAccessor;
import com.linkedin.clustermanager.ClusterDataAccessor.ClusterPropertyType;
import com.linkedin.clustermanager.ClusterDataAccessor.IdealStateConfigProperty;
import com.linkedin.clustermanager.ClusterManager;
import com.linkedin.clustermanager.ZNRecord;
import com.linkedin.clustermanager.model.IdealState;
import com.linkedin.clustermanager.model.ResourceGroup;
import com.linkedin.clustermanager.model.ResourceKey;
import com.linkedin.clustermanager.model.StateModelDefinition;
import com.linkedin.clustermanager.pipeline.AbstractBaseStage;
import com.linkedin.clustermanager.pipeline.StageException;
import com.linkedin.clustermanager.util.ZNRecordUtil;
/**
 * For resourceKey
 *   compute best possible (instance,state) pair based on IdealState,StateModel,LiveInstance
 * @author kgopalak
 *
 */
public class BestPossibleStateCalcStage extends AbstractBaseStage
{
  private static final Logger logger = Logger
      .getLogger(BestPossibleStateCalcStage.class.getName());

  @Override
  public void process(ClusterEvent event) throws Exception
  {
    ClusterManager manager = event.getAttribute("clustermanager");
    if (manager == null)
    {
      throw new StageException("ClusterManager attribute value is null");
    }
    ClusterDataAccessor dataAccessor = manager.getDataAccessor();
    List<ZNRecord> liveInstances;
    liveInstances = dataAccessor
        .getClusterPropertyList(ClusterPropertyType.LIVEINSTANCES);
    List<ZNRecord> idealStates = dataAccessor
        .getClusterPropertyList(ClusterPropertyType.IDEALSTATES);
    List<ZNRecord> stateModelDefs = dataAccessor
        .getClusterPropertyList(ClusterPropertyType.STATEMODELDEFS);
    CurrentStateOutput currentStateOutput = event
        .getAttribute(AttributeName.CURRENT_STATE.toString());
    Map<String, ResourceGroup> resourceGroupMap = event
        .getAttribute(AttributeName.RESOURCE_GROUPS.toString());
    BestPossibleStateOutput bestPossibleStateOutput = compute(resourceGroupMap,
        liveInstances, idealStates, stateModelDefs, currentStateOutput);
    event.addAttribute(AttributeName.BEST_POSSIBLE_STATE.toString(),
        bestPossibleStateOutput);
  }

  protected BestPossibleStateOutput compute(
      Map<String, ResourceGroup> resourceGroupMap,
      List<ZNRecord> liveInstances, List<ZNRecord> idealStates,
      List<ZNRecord> stateModelDefs, CurrentStateOutput currentStateOutput)
  {
    // for each ideal state
    // read the state model def
    // for each resource
    // get the preference list
    // for each instanceName check if its alive then assign a state
    Map<String, ZNRecord> liveInstancesMap = ZNRecordUtil
        .convertListToMap(liveInstances);

    Map<String, ZNRecord> idealStatesMap = ZNRecordUtil
        .convertListToMap(idealStates);
    BestPossibleStateOutput output = new BestPossibleStateOutput();

    for (String resourceGroupName : resourceGroupMap.keySet())
    {
      logger.info("Processing resourceGroup:" + resourceGroupName);

      ResourceGroup resourceGroup = resourceGroupMap.get(resourceGroupName);
      // Ideal state may be gone. In that case we need to get the state model name 
      // from the current state
      boolean idealStateExists = idealStatesMap.containsKey(resourceGroupName);
      ZNRecord idealStateRec = idealStatesMap.get(resourceGroupName);
      IdealState idealState = new IdealState(idealStateRec);
      
      String stateModelDefName = idealStateExists ? 
          idealState.getStateModelDefRef() : currentStateOutput.getResourceGroupStateModelDef(resourceGroupName);
      
      StateModelDefinition stateModelDef = lookupStateModel(
          stateModelDefName, stateModelDefs);
      for (ResourceKey resource : resourceGroup.getResourceKeys())
      {
        List<String> instancePreferenceList = getPreferenceList(resource,
            idealState, liveInstances, stateModelDef);

        Map<String, String> currentStateMap = currentStateOutput
            .getCurrentStateMap(resourceGroupName, resource);
        
        Map<String, String> bestStateForResource;
        if (idealState.getIdealStateMode() == IdealStateConfigProperty.CUSTOMIZED)
        {
          // TODO add computerBestStateForResourceInCustomizedMode()<commit_msg>Fix test drop resource testcase<commit_after>package com.linkedin.clustermanager.controller.stages;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.linkedin.clustermanager.ClusterDataAccessor;
import com.linkedin.clustermanager.ClusterDataAccessor.ClusterPropertyType;
import com.linkedin.clustermanager.ClusterDataAccessor.IdealStateConfigProperty;
import com.linkedin.clustermanager.ClusterManager;
import com.linkedin.clustermanager.ZNRecord;
import com.linkedin.clustermanager.model.IdealState;
import com.linkedin.clustermanager.model.ResourceGroup;
import com.linkedin.clustermanager.model.ResourceKey;
import com.linkedin.clustermanager.model.StateModelDefinition;
import com.linkedin.clustermanager.pipeline.AbstractBaseStage;
import com.linkedin.clustermanager.pipeline.StageException;
import com.linkedin.clustermanager.util.ZNRecordUtil;
/**
 * For resourceKey
 *   compute best possible (instance,state) pair based on IdealState,StateModel,LiveInstance
 * @author kgopalak
 *
 */
public class BestPossibleStateCalcStage extends AbstractBaseStage
{
  private static final Logger logger = Logger
      .getLogger(BestPossibleStateCalcStage.class.getName());

  @Override
  public void process(ClusterEvent event) throws Exception
  {
    ClusterManager manager = event.getAttribute("clustermanager");
    if (manager == null)
    {
      throw new StageException("ClusterManager attribute value is null");
    }
    ClusterDataAccessor dataAccessor = manager.getDataAccessor();
    List<ZNRecord> liveInstances;
    liveInstances = dataAccessor
        .getClusterPropertyList(ClusterPropertyType.LIVEINSTANCES);
    List<ZNRecord> idealStates = dataAccessor
        .getClusterPropertyList(ClusterPropertyType.IDEALSTATES);
    List<ZNRecord> stateModelDefs = dataAccessor
        .getClusterPropertyList(ClusterPropertyType.STATEMODELDEFS);
    CurrentStateOutput currentStateOutput = event
        .getAttribute(AttributeName.CURRENT_STATE.toString());
    Map<String, ResourceGroup> resourceGroupMap = event
        .getAttribute(AttributeName.RESOURCE_GROUPS.toString());
    BestPossibleStateOutput bestPossibleStateOutput = compute(resourceGroupMap,
        liveInstances, idealStates, stateModelDefs, currentStateOutput);
    event.addAttribute(AttributeName.BEST_POSSIBLE_STATE.toString(),
        bestPossibleStateOutput);
  }

  protected BestPossibleStateOutput compute(
      Map<String, ResourceGroup> resourceGroupMap,
      List<ZNRecord> liveInstances, List<ZNRecord> idealStates,
      List<ZNRecord> stateModelDefs, CurrentStateOutput currentStateOutput)
  {
    // for each ideal state
    // read the state model def
    // for each resource
    // get the preference list
    // for each instanceName check if its alive then assign a state
    Map<String, ZNRecord> liveInstancesMap = ZNRecordUtil
        .convertListToMap(liveInstances);

    Map<String, ZNRecord> idealStatesMap = ZNRecordUtil
        .convertListToMap(idealStates);
    BestPossibleStateOutput output = new BestPossibleStateOutput();

    for (String resourceGroupName : resourceGroupMap.keySet())
    {
      logger.info("Processing resourceGroup:" + resourceGroupName);

      ResourceGroup resourceGroup = resourceGroupMap.get(resourceGroupName);
      // Ideal state may be gone. In that case we need to get the state model name 
      // from the current state
      IdealState idealState;
      boolean idealStateExists = idealStatesMap.containsKey(resourceGroupName);
      if(!idealStateExists)
      {
        logger.info(" resourceGroup:" + resourceGroupName + " does not exist anymore");
        idealState = new IdealState(new ZNRecord(resourceGroupName));
      }
      else
      {
        // if ideal state is deleted, use a empty ZnRecord 
        idealState = new IdealState(idealStatesMap.get(resourceGroupName));
      }
      
      String stateModelDefName = idealStateExists ? 
          idealState.getStateModelDefRef() : currentStateOutput.getResourceGroupStateModelDef(resourceGroupName);
      
      StateModelDefinition stateModelDef = lookupStateModel(
          stateModelDefName, stateModelDefs);
      for (ResourceKey resource : resourceGroup.getResourceKeys())
      {
        List<String> instancePreferenceList = getPreferenceList(resource,
            idealState, liveInstances, stateModelDef);

        Map<String, String> currentStateMap = currentStateOutput
            .getCurrentStateMap(resourceGroupName, resource);
        
        Map<String, String> bestStateForResource;
        if (idealState.getIdealStateMode() == IdealStateConfigProperty.CUSTOMIZED)
        {
          // TODO add computerBestStateForResourceInCustomizedMode()
{'fraction_non_alphanumeric': 0.07587024463843087, 'fraction_numerical': 0.00021554046772281495, 'mean_word_length': 4.373480023161552, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6083373', 'n_tokens_mistral': 2495, 'n_tokens_neox': 2457, 'n_words': 511}