<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.hadoop.hbase.master.balancer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.TreeMap;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.ClusterMetrics;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.RegionMetrics;
import org.apache.hadoop.hbase.ServerMetrics;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.Size;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.RegionInfo;
import org.apache.hadoop.hbase.master.MockNoopMasterServices;
import org.apache.hadoop.hbase.master.RegionPlan;
import org.apache.hadoop.hbase.master.balancer.BaseLoadBalancer.Cluster;
import org.apache.hadoop.hbase.master.balancer.StochasticLoadBalancer.ServerLocalityCostFunction;
import org.apache.hadoop.hbase.testclassification.MasterTests;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category({ MasterTests.class, MediumTests.class })
public class TestStochasticLoadBalancer extends BalancerTestBase {

  @ClassRule
  public static final HBaseClassTestRule CLASS_RULE =
      HBaseClassTestRule.forClass(TestStochasticLoadBalancer.class);

  private static final String REGION_KEY = "testRegion";

  // Mapping of locality test -> expected locality
  private float[] expectedLocalities = {1.0f, 0.0f, 0.50f, 0.25f, 1.0f};

  /**
   * Data set for testLocalityCost:
   * [test][0][0] = mapping of server to number of regions it hosts
   * [test][region + 1][0] = server that region is hosted on
   * [test][region + 1][server + 1] = locality for region on server
   */

  private int[][][] clusterRegionLocationMocks = new int[][][]{

      // Test 1: each region is entirely on server that hosts it
      new int[][]{
          new int[]{2, 1, 1},
          new int[]{2, 0, 0, 100},   // region 0 is hosted and entirely local on server 2
          new int[]{0, 100, 0, 0},   // region 1 is hosted and entirely on server 0
          new int[]{0, 100, 0, 0},   // region 2 is hosted and entirely on server 0
          new int[]{1, 0, 100, 0},   // region 1 is hosted and entirely on server 1
      },

      // Test 2: each region is 0% local on the server that hosts it
      new int[][]{
          new int[]{1, 2, 1},
          new int[]{0, 0, 0, 100},   // region 0 is hosted and entirely local on server 2
          new int[]{1, 100, 0, 0},   // region 1 is hosted and entirely on server 0
          new int[]{1, 100, 0, 0},   // region 2 is hosted and entirely on server 0
          new int[]{2, 0, 100, 0},   // region 1 is hosted and entirely on server 1
      },

      // Test 3: each region is 25% local on the server that hosts it (and 50% locality is possible)
      new int[][]{
          new int[]{1, 2, 1},
          new int[]{0, 25, 0, 50},   // region 0 is hosted and entirely local on server 2
          new int[]{1, 50, 25, 0},   // region 1 is hosted and entirely on server 0
          new int[]{1, 50, 25, 0},   // region 2 is hosted and entirely on server 0
          new int[]{2, 0, 50, 25},   // region 1 is hosted and entirely on server 1
      },

      // Test 4: each region is 25% local on the server that hosts it (and 100% locality is possible)
      new int[][]{
          new int[]{1, 2, 1},
          new int[]{0, 25, 0, 100},   // region 0 is hosted and entirely local on server 2
          new int[]{1, 100, 25, 0},   // region 1 is hosted and entirely on server 0
          new int[]{1, 100, 25, 0},   // region 2 is hosted and entirely on server 0
          new int[]{2, 0, 100, 25},   // region 1 is hosted and entirely on server 1
      },

      // Test 5: each region is 75% local on the server that hosts it (and 75% locality is possible everywhere)
      new int[][]{
          new int[]{1, 2, 1},
          new int[]{0, 75, 75, 75},   // region 0 is hosted and entirely local on server 2
          new int[]{1, 75, 75, 75},   // region 1 is hosted and entirely on server 0
          new int[]{1, 75, 75, 75},   // region 2 is hosted and entirely on server 0
          new int[]{2, 75, 75, 75},   // region 1 is hosted and entirely on server 1
      },
  };

  private ServerMetrics mockServerMetricsWithCpRequests(ServerName server,
                                                        List<RegionInfo> regionsOnServer,
                                                        long cpRequestCount) {
    ServerMetrics serverMetrics = mock(ServerMetrics.class);
    Map<byte[], RegionMetrics> regionLoadMap = new TreeMap<>(Bytes.BYTES_COMPARATOR);<commit_msg>HBASE-24407 Correct the comment of clusterRegionLocationMocks in TestStochasticLoadBalancer (#1750)<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.hadoop.hbase.master.balancer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.TreeMap;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.ClusterMetrics;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.RegionMetrics;
import org.apache.hadoop.hbase.ServerMetrics;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.Size;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.RegionInfo;
import org.apache.hadoop.hbase.master.MockNoopMasterServices;
import org.apache.hadoop.hbase.master.RegionPlan;
import org.apache.hadoop.hbase.master.balancer.BaseLoadBalancer.Cluster;
import org.apache.hadoop.hbase.master.balancer.StochasticLoadBalancer.ServerLocalityCostFunction;
import org.apache.hadoop.hbase.testclassification.MasterTests;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category({ MasterTests.class, MediumTests.class })
public class TestStochasticLoadBalancer extends BalancerTestBase {

  @ClassRule
  public static final HBaseClassTestRule CLASS_RULE =
      HBaseClassTestRule.forClass(TestStochasticLoadBalancer.class);

  private static final String REGION_KEY = "testRegion";

  // Mapping of locality test -> expected locality
  private float[] expectedLocalities = {1.0f, 0.0f, 0.50f, 0.25f, 1.0f};

  /**
   * Data set for testLocalityCost:
   * [test][0][0] = mapping of server to number of regions it hosts
   * [test][region + 1][0] = server that region is hosted on
   * [test][region + 1][server + 1] = locality for region on server
   */

  private int[][][] clusterRegionLocationMocks = new int[][][]{

      // Test 1: each region is entirely on server that hosts it
      new int[][]{
          new int[]{2, 1, 1},
          new int[]{2, 0, 0, 100},   // region 0 is hosted and entirely local on server 2
          new int[]{0, 100, 0, 0},   // region 1 is hosted and entirely on server 0
          new int[]{0, 100, 0, 0},   // region 2 is hosted and entirely on server 0
          new int[]{1, 0, 100, 0},   // region 3 is hosted and entirely on server 1
      },

      // Test 2: each region is 0% local on the server that hosts it
      new int[][]{
          new int[]{1, 2, 1},
          new int[]{0, 0, 0, 100},   // region 0 is hosted and entirely local on server 2
          new int[]{1, 100, 0, 0},   // region 1 is hosted and entirely on server 0
          new int[]{1, 100, 0, 0},   // region 2 is hosted and entirely on server 0
          new int[]{2, 0, 100, 0},   // region 3 is hosted and entirely on server 1
      },

      // Test 3: each region is 25% local on the server that hosts it (and 50% locality is possible)
      new int[][]{
          new int[]{1, 2, 1},
          new int[]{0, 25, 0, 50},   // region 0 is hosted and entirely local on server 2
          new int[]{1, 50, 25, 0},   // region 1 is hosted and entirely on server 0
          new int[]{1, 50, 25, 0},   // region 2 is hosted and entirely on server 0
          new int[]{2, 0, 50, 25},   // region 3 is hosted and entirely on server 1
      },

      // Test 4: each region is 25% local on the server that hosts it (and 100% locality is possible)
      new int[][]{
          new int[]{1, 2, 1},
          new int[]{0, 25, 0, 100},   // region 0 is hosted and entirely local on server 2
          new int[]{1, 100, 25, 0},   // region 1 is hosted and entirely on server 0
          new int[]{1, 100, 25, 0},   // region 2 is hosted and entirely on server 0
          new int[]{2, 0, 100, 25},   // region 3 is hosted and entirely on server 1
      },

      // Test 5: each region is 75% local on the server that hosts it (and 75% locality is possible everywhere)
      new int[][]{
          new int[]{1, 2, 1},
          new int[]{0, 75, 75, 75},   // region 0 is hosted and entirely local on server 2
          new int[]{1, 75, 75, 75},   // region 1 is hosted and entirely on server 0
          new int[]{1, 75, 75, 75},   // region 2 is hosted and entirely on server 0
          new int[]{2, 75, 75, 75},   // region 3 is hosted and entirely on server 1
      },
  };

  private ServerMetrics mockServerMetricsWithCpRequests(ServerName server,
                                                        List<RegionInfo> regionsOnServer,
                                                        long cpRequestCount) {
    ServerMetrics serverMetrics = mock(ServerMetrics.class);
    Map<byte[], RegionMetrics> regionLoadMap = new TreeMap<>(Bytes.BYTES_COMPARATOR);
{'fraction_non_alphanumeric': 0.1044137022397892, 'fraction_numerical': 0.03763175230566535, 'mean_word_length': 2.945743989603639, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6351365', 'n_tokens_mistral': 3818, 'n_tokens_neox': 3394, 'n_words': 1353}