<commit_before>package org.apache.solr.cloud.rule;

/*
 * 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.
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.cloud.rule.ReplicaAssigner.Position;
import org.apache.solr.common.cloud.ZkStateReader;
import org.junit.Test;

import static org.apache.solr.cloud.rule.Rule.parseRule;
import static org.apache.solr.common.cloud.ZkNodeProps.makeMap;

public class RuleEngineTest extends SolrTestCaseJ4{
  @Test
  public void testPlacement2(){


    String s = "{" +
        "  '127.0.0.1:49961_':{" +
        "    'node':'127.0.0.1:49961_'," +
        "    'disk':992," +
        "    'cores':1}," +
        "  '127.0.0.1:49955_':{" +
        "    'node':'127.0.0.1:49955_'," +
        "    'disk':992," +
        "    'cores':1}," +
        "  '127.0.0.1:49952_':{" +
        "    'node':'127.0.0.1:49952_'," +
        "    'disk':992," +
        "    'cores':1}," +
        "  '127.0.0.1:49947_':{" +
        "    'node':'127.0.0.1:49947_'," +
        "    'disk':992," +
        "    'cores':1}," +
        "  '127.0.0.1:49958_':{" +
        "    'node':'127.0.0.1:49958_'," +
        "    'disk':992," +
        "    'cores':1}}";
    MockSnitch.nodeVsTags = (Map) ZkStateReader.fromJSON(s.getBytes());
    Map shardVsReplicaCount = makeMap("shard1", 2, "shard2", 2);

    List<Rule> rules = parseRules("[{'cores':'<4'}, {" +
            "'replica':'1',shard:'*','node':'*'}," +
            " {'disk':'>1'}]");

    Map<Position, String> mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null ).getNodeMappings();
    assertNotNull(mapping);

    mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null ).getNodeMappings();
    assertNotNull(mapping);



  }

  public void testPlacement3(){
    String s = "{" +
        "  '127.0.0.1:49961_':{" +
        "    'node':'127.0.0.1:49961_'," +
        "    'disk':992," +
        "    'cores':1}," +
        "  '127.0.0.2:49955_':{" +
        "    'node':'127.0.0.1:49955_'," +
        "    'disk':995," +
        "    'cores':1}," +
        "  '127.0.0.3:49952_':{" +
        "    'node':'127.0.0.1:49952_'," +
        "    'disk':990," +
        "    'cores':1}," +
        "  '127.0.0.1:49947_':{" +
        "    'node':'127.0.0.1:49947_'," +
        "    'disk':980," +
        "    'cores':1}," +
        "  '127.0.0.2:49958_':{" +
        "    'node':'127.0.0.1:49958_'," +
        "    'disk':970," +
        "    'cores':1}}";
    MockSnitch.nodeVsTags = (Map) ZkStateReader.fromJSON(s.getBytes());
    //test not
    List<Rule> rules = parseRules(
         "[{cores:'<4'}, " +
            "{replica:'1',shard:'*',node:'*'}," +
            "{node:'!127.0.0.1:49947_'}," +
            "{disk:'>1'}]");
    Map shardVsReplicaCount = makeMap("shard1", 2, "shard2", 2);
    Map<Position, String> mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null).getNodeMappings();
    assertNotNull(mapping);
    assertFalse(mapping.containsValue("127.0.0.1:49947_"));

    rules = parseRules(
         "[{cores:'<4'}, " +
            "{replica:'1',node:'*'}," +
            "{disk:'>980'}]");
    shardVsReplicaCount = makeMap("shard1", 2, "shard2", 2);
    mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null).getNodeMappings();
    assertNull(mapping);


    rules = parseRules(
        "[{cores:'<4'}, " +
            "{replica:'1',node:'*'}," +
            "{disk:'>980~'}]");
    shardVsReplicaCount = makeMap("shard1", 2, "shard2", 2);
    mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null).getNodeMappings();
    assertNotNull(mapping);
    assertFalse(mapping.containsValue("127.0.0.2:49958_"));

    rules = parseRules(
        "[{cores:'<4'}, " +
            "{replica:'1',shard:'*',host:'*'}]"
            );
    shardVsReplicaCount = makeMap("shard1", 2, "shard2", 2);
    mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null).getNodeMappings();
    assertNotNull(mapping);

    rules = parseRules(
        "[{cores:'<4'}, " +
            "{replica:'1',shard:'**',host:'*'}]"
            );
    shardVsReplicaCount = makeMap("shard1", 2, "shard2", 2);
    mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null).getNodeMappings();
    assertNull(mapping);

    rules = parseRules(
        "[{cores:'<4'}, " +
            "{replica:'1~',shard:'**',host:'*'}]"
            );
    shardVsReplicaCount = makeMap("shard1", 2, "shard2", 2);
    mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null).getNodeMappings();
    assertNotNull(mapping);


  }

  private List<Rule> parseRules(String s) {

    List maps = (List) ZkStateReader.fromJSON(s.getBytes());

    List<Rule> rules = new ArrayList<>();
    for (Object map : maps) rules.add(new Rule((Map) map));
    return rules;
  }

  @Test
  public void testPlacement() throws Exception {
    String rulesStr = "rack:*,replica:<2";
    List<Rule> rules = parse(Arrays.asList(rulesStr));
    Map shardVsReplicaCount = makeMap("shard1", 3, "shard2", 3);
    Map nodeVsTags = makeMap(
        "node1:80", makeMap("rack", "178"),
        "node2:80", makeMap("rack", "179"),
        "node3:80", makeMap("rack", "180"),
        "node4:80", makeMap("rack", "181"),
        "node5:80", makeMap("rack", "182")
    );
    MockSnitch.nodeVsTags = nodeVsTags;
    Map<Position, String> mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null ,null).getNodeMappings();
    assertNull(mapping);<commit_msg>SOLR-6220: Fixes forbidden method invocation String#getBytes() in RuleEngineTest<commit_after>package org.apache.solr.cloud.rule;

/*
 * 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.
 */

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.cloud.rule.ReplicaAssigner.Position;
import org.apache.solr.common.cloud.ZkStateReader;
import org.junit.Test;

import static org.apache.solr.cloud.rule.Rule.parseRule;
import static org.apache.solr.common.cloud.ZkNodeProps.makeMap;

public class RuleEngineTest extends SolrTestCaseJ4{
  @Test
  public void testPlacement2(){


    String s = "{" +
        "  '127.0.0.1:49961_':{" +
        "    'node':'127.0.0.1:49961_'," +
        "    'disk':992," +
        "    'cores':1}," +
        "  '127.0.0.1:49955_':{" +
        "    'node':'127.0.0.1:49955_'," +
        "    'disk':992," +
        "    'cores':1}," +
        "  '127.0.0.1:49952_':{" +
        "    'node':'127.0.0.1:49952_'," +
        "    'disk':992," +
        "    'cores':1}," +
        "  '127.0.0.1:49947_':{" +
        "    'node':'127.0.0.1:49947_'," +
        "    'disk':992," +
        "    'cores':1}," +
        "  '127.0.0.1:49958_':{" +
        "    'node':'127.0.0.1:49958_'," +
        "    'disk':992," +
        "    'cores':1}}";
    MockSnitch.nodeVsTags = (Map) ZkStateReader.fromJSON(s.getBytes(StandardCharsets.UTF_8));
    Map shardVsReplicaCount = makeMap("shard1", 2, "shard2", 2);

    List<Rule> rules = parseRules("[{'cores':'<4'}, {" +
            "'replica':'1',shard:'*','node':'*'}," +
            " {'disk':'>1'}]");

    Map<Position, String> mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null ).getNodeMappings();
    assertNotNull(mapping);

    mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null ).getNodeMappings();
    assertNotNull(mapping);



  }

  public void testPlacement3(){
    String s = "{" +
        "  '127.0.0.1:49961_':{" +
        "    'node':'127.0.0.1:49961_'," +
        "    'disk':992," +
        "    'cores':1}," +
        "  '127.0.0.2:49955_':{" +
        "    'node':'127.0.0.1:49955_'," +
        "    'disk':995," +
        "    'cores':1}," +
        "  '127.0.0.3:49952_':{" +
        "    'node':'127.0.0.1:49952_'," +
        "    'disk':990," +
        "    'cores':1}," +
        "  '127.0.0.1:49947_':{" +
        "    'node':'127.0.0.1:49947_'," +
        "    'disk':980," +
        "    'cores':1}," +
        "  '127.0.0.2:49958_':{" +
        "    'node':'127.0.0.1:49958_'," +
        "    'disk':970," +
        "    'cores':1}}";
    MockSnitch.nodeVsTags = (Map) ZkStateReader.fromJSON(s.getBytes(StandardCharsets.UTF_8));
    //test not
    List<Rule> rules = parseRules(
         "[{cores:'<4'}, " +
            "{replica:'1',shard:'*',node:'*'}," +
            "{node:'!127.0.0.1:49947_'}," +
            "{disk:'>1'}]");
    Map shardVsReplicaCount = makeMap("shard1", 2, "shard2", 2);
    Map<Position, String> mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null).getNodeMappings();
    assertNotNull(mapping);
    assertFalse(mapping.containsValue("127.0.0.1:49947_"));

    rules = parseRules(
         "[{cores:'<4'}, " +
            "{replica:'1',node:'*'}," +
            "{disk:'>980'}]");
    shardVsReplicaCount = makeMap("shard1", 2, "shard2", 2);
    mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null).getNodeMappings();
    assertNull(mapping);


    rules = parseRules(
        "[{cores:'<4'}, " +
            "{replica:'1',node:'*'}," +
            "{disk:'>980~'}]");
    shardVsReplicaCount = makeMap("shard1", 2, "shard2", 2);
    mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null).getNodeMappings();
    assertNotNull(mapping);
    assertFalse(mapping.containsValue("127.0.0.2:49958_"));

    rules = parseRules(
        "[{cores:'<4'}, " +
            "{replica:'1',shard:'*',host:'*'}]"
            );
    shardVsReplicaCount = makeMap("shard1", 2, "shard2", 2);
    mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null).getNodeMappings();
    assertNotNull(mapping);

    rules = parseRules(
        "[{cores:'<4'}, " +
            "{replica:'1',shard:'**',host:'*'}]"
            );
    shardVsReplicaCount = makeMap("shard1", 2, "shard2", 2);
    mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null).getNodeMappings();
    assertNull(mapping);

    rules = parseRules(
        "[{cores:'<4'}, " +
            "{replica:'1~',shard:'**',host:'*'}]"
            );
    shardVsReplicaCount = makeMap("shard1", 2, "shard2", 2);
    mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null, null).getNodeMappings();
    assertNotNull(mapping);


  }

  private List<Rule> parseRules(String s) {

    List maps = (List) ZkStateReader.fromJSON(s.getBytes(StandardCharsets.UTF_8));

    List<Rule> rules = new ArrayList<>();
    for (Object map : maps) rules.add(new Rule((Map) map));
    return rules;
  }

  @Test
  public void testPlacement() throws Exception {
    String rulesStr = "rack:*,replica:<2";
    List<Rule> rules = parse(Arrays.asList(rulesStr));
    Map shardVsReplicaCount = makeMap("shard1", 3, "shard2", 3);
    Map nodeVsTags = makeMap(
        "node1:80", makeMap("rack", "178"),
        "node2:80", makeMap("rack", "179"),
        "node3:80", makeMap("rack", "180"),
        "node4:80", makeMap("rack", "181"),
        "node5:80", makeMap("rack", "182")
    );
    MockSnitch.nodeVsTags = nodeVsTags;
    Map<Position, String> mapping = new ReplicaAssigner(
        rules,
        shardVsReplicaCount, Collections.singletonList(MockSnitch.class.getName()),
        new HashMap(), new ArrayList<>(MockSnitch.nodeVsTags.keySet()), null ,null).getNodeMappings();
    assertNull(mapping);
{'fraction_non_alphanumeric': 0.1772119506109654, 'fraction_numerical': 0.04983686264474442, 'mean_word_length': 2.9011729473421513, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 46, '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': '3435948', 'n_tokens_mistral': 5688, 'n_tokens_neox': 5291, 'n_words': 938}