<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.hdds.scm.pipeline;

import org.apache.commons.collections.CollectionUtils;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.hdds.protocol.DatanodeDetails;
import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
import org.apache.hadoop.hdds.scm.TestUtils;
import org.apache.hadoop.hdds.scm.container.MockNodeManager;
import org.apache.hadoop.hdds.scm.node.NodeManager;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Test for RatisPipelineProvider.
 */
public class TestRatisPipelineProvider {

  private NodeManager nodeManager;
  private PipelineProvider provider;
  private PipelineStateManager stateManager;

  @Before
  public void init() throws Exception {
    nodeManager = new MockNodeManager(true, 10);
    stateManager = new PipelineStateManager(new OzoneConfiguration());
    provider = new MockRatisPipelineProvider(nodeManager,
        stateManager, new OzoneConfiguration());
  }

  private void createPipelineAndAssertions(
          HddsProtos.ReplicationFactor factor) throws IOException {
    Pipeline pipeline = provider.create(factor);
    stateManager.addPipeline(pipeline);
    Assert.assertEquals(pipeline.getType(), HddsProtos.ReplicationType.RATIS);
    Assert.assertEquals(pipeline.getFactor(), factor);
    Assert.assertEquals(pipeline.getPipelineState(),
            Pipeline.PipelineState.OPEN);
    Assert.assertEquals(pipeline.getNodes().size(), factor.getNumber());
    Pipeline pipeline1 = provider.create(factor);
    stateManager.addPipeline(pipeline1);
    // New pipeline should not overlap with the previous created pipeline
    Assert.assertTrue(
        CollectionUtils.intersection(pipeline.getNodes(), pipeline1.getNodes())
            .isEmpty());
    Assert.assertEquals(pipeline1.getType(), HddsProtos.ReplicationType.RATIS);
    Assert.assertEquals(pipeline1.getFactor(), factor);
    Assert.assertEquals(pipeline1.getPipelineState(),
            Pipeline.PipelineState.OPEN);
    Assert.assertEquals(pipeline1.getNodes().size(), factor.getNumber());
  }

  @Test
  public void testCreatePipelineWithFactor() throws IOException {
    HddsProtos.ReplicationFactor factor = HddsProtos.ReplicationFactor.THREE;
    Pipeline pipeline = provider.create(factor);
    stateManager.addPipeline(pipeline);
    Assert.assertEquals(pipeline.getType(), HddsProtos.ReplicationType.RATIS);
    Assert.assertEquals(pipeline.getFactor(), factor);
    Assert.assertEquals(pipeline.getPipelineState(),
        Pipeline.PipelineState.OPEN);
    Assert.assertEquals(pipeline.getNodes().size(), factor.getNumber());

    factor = HddsProtos.ReplicationFactor.ONE;
    Pipeline pipeline1 = provider.create(factor);
    stateManager.addPipeline(pipeline1);
    // New pipeline should overlap with the previous created pipeline,
    // and one datanode should overlap between the two types.
    Assert.assertEquals(
        CollectionUtils.intersection(pipeline.getNodes(),
            pipeline1.getNodes()).size(), 1);
    Assert.assertEquals(pipeline1.getType(), HddsProtos.ReplicationType.RATIS);
    Assert.assertEquals(pipeline1.getFactor(), factor);
    Assert.assertEquals(pipeline1.getPipelineState(),
        Pipeline.PipelineState.OPEN);
    Assert.assertEquals(pipeline1.getNodes().size(), factor.getNumber());
  }

  @Test
  public void testCreatePipelineWithFactorThree() throws IOException {
    createPipelineAndAssertions(HddsProtos.ReplicationFactor.THREE);
  }

  @Test
  public void testCreatePipelineWithFactorOne() throws IOException {
    createPipelineAndAssertions(HddsProtos.ReplicationFactor.ONE);
  }

  private List<DatanodeDetails> createListOfNodes(int nodeCount) {
    List<DatanodeDetails> nodes = new ArrayList<>();
    for (int i = 0; i < nodeCount; i++) {
      nodes.add(TestUtils.randomDatanodeDetails());
    }
    return nodes;
  }

  @Test
  public void testCreatePipelineWithNodes() {
    HddsProtos.ReplicationFactor factor = HddsProtos.ReplicationFactor.THREE;
    Pipeline pipeline =
        provider.create(factor, createListOfNodes(factor.getNumber()));
    Assert.assertEquals(pipeline.getType(), HddsProtos.ReplicationType.RATIS);
    Assert.assertEquals(pipeline.getFactor(), factor);
    Assert.assertEquals(
        pipeline.getPipelineState(), Pipeline.PipelineState.OPEN);
    Assert.assertEquals(pipeline.getNodes().size(), factor.getNumber());

    factor = HddsProtos.ReplicationFactor.ONE;
    pipeline = provider.create(factor, createListOfNodes(factor.getNumber()));
    Assert.assertEquals(pipeline.getType(), HddsProtos.ReplicationType.RATIS);
    Assert.assertEquals(pipeline.getFactor(), factor);
    Assert.assertEquals(pipeline.getPipelineState(),
        Pipeline.PipelineState.OPEN);
    Assert.assertEquals(pipeline.getNodes().size(), factor.getNumber());
  }

  @Test
  public void testCreatePipelinesDnExclude() throws IOException {

    // We have 10 DNs in MockNodeManager.
    // Use up first 3 DNs for an open pipeline.
    List<DatanodeDetails> openPiplineDns = nodeManager.getAllNodes()
        .subList(0, 3);
    HddsProtos.ReplicationFactor factor = HddsProtos.ReplicationFactor.THREE;

    Pipeline openPipeline = Pipeline.newBuilder()
        .setType(HddsProtos.ReplicationType.RATIS)
        .setFactor(factor)
        .setNodes(openPiplineDns)
        .setState(Pipeline.PipelineState.OPEN)
        .setId(PipelineID.randomId())
        .build();

    stateManager.addPipeline(openPipeline);

    // Use up next 3 DNs also for an open pipeline.
    List<DatanodeDetails> moreOpenPiplineDns = nodeManager.getAllNodes()
        .subList(3, 6);
    Pipeline anotherOpenPipeline = Pipeline.newBuilder()
        .setType(HddsProtos.ReplicationType.RATIS)
        .setFactor(factor)
        .setNodes(moreOpenPiplineDns)
        .setState(Pipeline.PipelineState.OPEN)
        .setId(PipelineID.randomId())
        .build();
    stateManager.addPipeline(anotherOpenPipeline);

    // Use up next 3 DNs also for a closed pipeline.
    List<DatanodeDetails> closedPiplineDns = nodeManager.getAllNodes()
        .subList(6, 9);
    Pipeline anotherClosedPipeline = Pipeline.newBuilder()
        .setType(HddsProtos.ReplicationType.RATIS)
        .setFactor(factor)
        .setNodes(closedPiplineDns)
        .setState(Pipeline.PipelineState.CLOSED)
        .setId(PipelineID.randomId())
        .build();
    stateManager.addPipeline(anotherClosedPipeline);

    Pipeline pipeline = provider.create(factor);
    Assert.assertEquals(pipeline.getType(), HddsProtos.ReplicationType.RATIS);
    Assert.assertEquals(pipeline.getFactor(), factor);
    Assert.assertEquals(pipeline.getPipelineState(),
        Pipeline.PipelineState.OPEN);
    Assert.assertEquals(pipeline.getNodes().size(), factor.getNumber());
    List<DatanodeDetails> pipelineNodes = pipeline.getNodes();

    // Pipline nodes cannot be from open pipelines.
    Assert.assertTrue(
        pipelineNodes.parallelStream().filter(dn ->
        (openPiplineDns.contains(dn) || moreOpenPiplineDns.contains(dn)))
        .count() == 0);

    // Since we have only 10 DNs, at least 1 pipeline node should have been
    // from the closed pipeline DN list.
    Assert.assertTrue(pipelineNodes.parallelStream().filter(<commit_msg>HDDS-2081. Fix TestRatisPipelineProvider#testCreatePipelinesDnExclude. Contributed by <NAME>. (#1506)<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.hdds.scm.pipeline;

import org.apache.commons.collections.CollectionUtils;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.hdds.protocol.DatanodeDetails;
import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
import org.apache.hadoop.hdds.scm.TestUtils;
import org.apache.hadoop.hdds.scm.container.MockNodeManager;
import org.apache.hadoop.hdds.scm.node.NodeManager;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Test for RatisPipelineProvider.
 */
public class TestRatisPipelineProvider {

  private NodeManager nodeManager;
  private PipelineProvider provider;
  private PipelineStateManager stateManager;

  @Before
  public void init() throws Exception {
    nodeManager = new MockNodeManager(true, 10);
    stateManager = new PipelineStateManager(new OzoneConfiguration());
    provider = new MockRatisPipelineProvider(nodeManager,
        stateManager, new OzoneConfiguration());
  }

  private void createPipelineAndAssertions(
          HddsProtos.ReplicationFactor factor) throws IOException {
    Pipeline pipeline = provider.create(factor);
    stateManager.addPipeline(pipeline);
    Assert.assertEquals(pipeline.getType(), HddsProtos.ReplicationType.RATIS);
    Assert.assertEquals(pipeline.getFactor(), factor);
    Assert.assertEquals(pipeline.getPipelineState(),
            Pipeline.PipelineState.OPEN);
    Assert.assertEquals(pipeline.getNodes().size(), factor.getNumber());
    Pipeline pipeline1 = provider.create(factor);
    stateManager.addPipeline(pipeline1);
    // New pipeline should not overlap with the previous created pipeline
    Assert.assertTrue(
        CollectionUtils.intersection(pipeline.getNodes(), pipeline1.getNodes())
            .isEmpty());
    Assert.assertEquals(pipeline1.getType(), HddsProtos.ReplicationType.RATIS);
    Assert.assertEquals(pipeline1.getFactor(), factor);
    Assert.assertEquals(pipeline1.getPipelineState(),
            Pipeline.PipelineState.OPEN);
    Assert.assertEquals(pipeline1.getNodes().size(), factor.getNumber());
  }

  @Test
  public void testCreatePipelineWithFactor() throws IOException {
    HddsProtos.ReplicationFactor factor = HddsProtos.ReplicationFactor.THREE;
    Pipeline pipeline = provider.create(factor);
    stateManager.addPipeline(pipeline);
    Assert.assertEquals(pipeline.getType(), HddsProtos.ReplicationType.RATIS);
    Assert.assertEquals(pipeline.getFactor(), factor);
    Assert.assertEquals(pipeline.getPipelineState(),
        Pipeline.PipelineState.OPEN);
    Assert.assertEquals(pipeline.getNodes().size(), factor.getNumber());

    factor = HddsProtos.ReplicationFactor.ONE;
    Pipeline pipeline1 = provider.create(factor);
    stateManager.addPipeline(pipeline1);
    // New pipeline should overlap with the previous created pipeline,
    // and one datanode should overlap between the two types.
    Assert.assertEquals(
        CollectionUtils.intersection(pipeline.getNodes(),
            pipeline1.getNodes()).size(), 1);
    Assert.assertEquals(pipeline1.getType(), HddsProtos.ReplicationType.RATIS);
    Assert.assertEquals(pipeline1.getFactor(), factor);
    Assert.assertEquals(pipeline1.getPipelineState(),
        Pipeline.PipelineState.OPEN);
    Assert.assertEquals(pipeline1.getNodes().size(), factor.getNumber());
  }

  @Test
  public void testCreatePipelineWithFactorThree() throws IOException {
    createPipelineAndAssertions(HddsProtos.ReplicationFactor.THREE);
  }

  @Test
  public void testCreatePipelineWithFactorOne() throws IOException {
    createPipelineAndAssertions(HddsProtos.ReplicationFactor.ONE);
  }

  private List<DatanodeDetails> createListOfNodes(int nodeCount) {
    List<DatanodeDetails> nodes = new ArrayList<>();
    for (int i = 0; i < nodeCount; i++) {
      nodes.add(TestUtils.randomDatanodeDetails());
    }
    return nodes;
  }

  @Test
  public void testCreatePipelineWithNodes() {
    HddsProtos.ReplicationFactor factor = HddsProtos.ReplicationFactor.THREE;
    Pipeline pipeline =
        provider.create(factor, createListOfNodes(factor.getNumber()));
    Assert.assertEquals(pipeline.getType(), HddsProtos.ReplicationType.RATIS);
    Assert.assertEquals(pipeline.getFactor(), factor);
    Assert.assertEquals(
        pipeline.getPipelineState(), Pipeline.PipelineState.OPEN);
    Assert.assertEquals(pipeline.getNodes().size(), factor.getNumber());

    factor = HddsProtos.ReplicationFactor.ONE;
    pipeline = provider.create(factor, createListOfNodes(factor.getNumber()));
    Assert.assertEquals(pipeline.getType(), HddsProtos.ReplicationType.RATIS);
    Assert.assertEquals(pipeline.getFactor(), factor);
    Assert.assertEquals(pipeline.getPipelineState(),
        Pipeline.PipelineState.OPEN);
    Assert.assertEquals(pipeline.getNodes().size(), factor.getNumber());
  }

  @Test
  public void testCreatePipelinesDnExclude() throws IOException {

    // We need 9 Healthy DNs in MockNodeManager.
    NodeManager mockNodeManager = new MockNodeManager(true, 12);
    PipelineStateManager stateManagerMock =
        new PipelineStateManager(new OzoneConfiguration());
    PipelineProvider providerMock = new MockRatisPipelineProvider(
        mockNodeManager, stateManagerMock, new OzoneConfiguration());

    // Use up first 3 DNs for an open pipeline.
    List<DatanodeDetails> openPiplineDns = mockNodeManager.getAllNodes()
        .subList(0, 3);
    HddsProtos.ReplicationFactor factor = HddsProtos.ReplicationFactor.THREE;

    Pipeline openPipeline = Pipeline.newBuilder()
        .setType(HddsProtos.ReplicationType.RATIS)
        .setFactor(factor)
        .setNodes(openPiplineDns)
        .setState(Pipeline.PipelineState.OPEN)
        .setId(PipelineID.randomId())
        .build();

    stateManagerMock.addPipeline(openPipeline);

    // Use up next 3 DNs also for an open pipeline.
    List<DatanodeDetails> moreOpenPiplineDns = mockNodeManager.getAllNodes()
        .subList(3, 6);
    Pipeline anotherOpenPipeline = Pipeline.newBuilder()
        .setType(HddsProtos.ReplicationType.RATIS)
        .setFactor(factor)
        .setNodes(moreOpenPiplineDns)
        .setState(Pipeline.PipelineState.OPEN)
        .setId(PipelineID.randomId())
        .build();
    stateManagerMock.addPipeline(anotherOpenPipeline);

    // Use up next 3 DNs also for a closed pipeline.
    List<DatanodeDetails> closedPiplineDns = mockNodeManager.getAllNodes()
        .subList(6, 9);
    Pipeline anotherClosedPipeline = Pipeline.newBuilder()
        .setType(HddsProtos.ReplicationType.RATIS)
        .setFactor(factor)
        .setNodes(closedPiplineDns)
        .setState(Pipeline.PipelineState.CLOSED)
        .setId(PipelineID.randomId())
        .build();
    stateManagerMock.addPipeline(anotherClosedPipeline);

    Pipeline pipeline = providerMock.create(factor);
    Assert.assertEquals(pipeline.getType(), HddsProtos.ReplicationType.RATIS);
    Assert.assertEquals(pipeline.getFactor(), factor);
    Assert.assertEquals(pipeline.getPipelineState(),
        Pipeline.PipelineState.OPEN);
    Assert.assertEquals(pipeline.getNodes().size(), factor.getNumber());
    List<DatanodeDetails> pipelineNodes = pipeline.getNodes();

    // Pipline nodes cannot be from open pipelines.
    Assert.assertTrue(
        pipelineNodes.parallelStream().filter(dn ->
        (openPiplineDns.contains(dn) || moreOpenPiplineDns.contains(dn)))
        .count() == 0);

    // Since we have only 9 Healthy DNs, at least 1 pipeline node should have
    // been from the closed pipeline DN list.
    Assert.assertTrue(pipelineNodes.parallelStream().filter(
{'fraction_non_alphanumeric': 0.10238846864018107, 'fraction_numerical': 0.004884150336529871, 'mean_word_length': 4.199752245277176, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '5941834', 'n_tokens_mistral': 4662, 'n_tokens_neox': 4623, 'n_words': 987}