<commit_before>package org.apache.helix.manager.zk;

/*
 * 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.util.UUID;

import org.apache.helix.InstanceType;
import org.apache.helix.TestHelper;
import org.apache.helix.ZkHelixTestManager;
import org.apache.helix.ZkTestHelper;
import org.apache.helix.integration.ZkIntegrationTestBase;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestZkManagerFlappingDetection extends ZkIntegrationTestBase
{
  @Test
  public void testDisconnectHistory() throws Exception
  {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    final String clusterName = className + "_" + methodName;

    TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
                            "localhost", // participant name prefix
                            "TestDB", // resource name prefix
                            1, // resources
                            10, // partitions per resource
                            5, // number of nodes
                            3, // replicas
                            "MasterSlave",
                            true); // do rebalance
    
    
      String instanceName = "localhost_" + (12918 + 0);
      ZkHelixTestManager manager =
          new ZkHelixTestManager(clusterName,
                                 instanceName,
                                 InstanceType.PARTICIPANT,
                                 ZK_ADDR);
      manager.connect();
      ZkClient zkClient = manager.getZkClient();
      ZkTestHelper.expireSession(zkClient);
      for(int i = 0;i < 4; i++)
      {
        ZkTestHelper.expireSession(zkClient);
        Thread.sleep(500);
        if(i < 5)
        {
          Assert.assertTrue(manager.isConnected());
        }
      }
      ZkTestHelper.disconnectSession(zkClient);
      for(int i = 0; i < 20; i++)
      {
        Thread.sleep(500);
        if(!manager.isConnected()) break;
      }
      Assert.assertFalse(manager.isConnected());
  }
  
  @Test
  public void testDisconnectFlappingWindow() throws Exception
  {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String instanceName = "localhost_" + (12918 + 1);
    final String clusterName = className + "_" + methodName + UUID.randomUUID();

    TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
                            "localhost", // participant name prefix
                            "TestDB", // resource name prefix
                            1, // resources
                            10, // partitions per resource
                            5, // number of nodes
                            3, // replicas
                            "MasterSlave",
                            true); // do rebalance
    testDisconnectFlappingWindow2(instanceName, InstanceType.PARTICIPANT);
    testDisconnectFlappingWindow2("admin", InstanceType.ADMINISTRATOR);
  }
  
  public void testDisconnectFlappingWindow2(String instanceName, InstanceType type) throws Exception
  {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    final String clusterName = className + "_" + methodName + UUID.randomUUID();

    TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
                            "localhost", // participant name prefix
                            "TestDB", // resource name prefix
                            1, // resources
                            10, // partitions per resource
                            5, // number of nodes
                            3, // replicas
                            "MasterSlave",
                            true); // do rebalance
    
    
      // flapping time window to 5 sec
      System.setProperty("helixmanager.flappingTimeWindow", "10000");
      System.setProperty("helixmanager.maxDisconnectThreshold", "7");
      ZkHelixTestManager manager2 =
          new ZkHelixTestManager(clusterName,
                                 instanceName,
                                 type,
                                 ZK_ADDR);
      manager2.connect();
      ZkClient zkClient = manager2.getZkClient();
      for(int i = 0;i < 3; i++)
      {
        ZkTestHelper.expireSession(zkClient);
        Thread.sleep(500);
        Assert.assertTrue(manager2.isConnected());
      }
      Thread.sleep(10000);
      // Old entries should be cleaned up
      for(int i = 0;i < 7; i++)
      {
        ZkTestHelper.expireSession(zkClient);
        Thread.sleep(500);
        Assert.assertTrue(manager2.isConnected());
      }
      ZkTestHelper.disconnectSession(zkClient);
      for(int i = 0; i < 20; i++)
      {
        Thread.sleep(500);
        if(!manager2.isConnected()) break;
      }
      Assert.assertFalse(manager2.isConnected());
  }
  
  //@Test
  public void testDisconnectFlappingWindowController() throws Exception
  {
    String className = TestHelper.getTestClassName();<commit_msg>Update test paramaters for testZkManagerFlappingdetection<commit_after>package org.apache.helix.manager.zk;

/*
 * 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.util.UUID;

import org.apache.helix.InstanceType;
import org.apache.helix.TestHelper;
import org.apache.helix.ZkHelixTestManager;
import org.apache.helix.ZkTestHelper;
import org.apache.helix.integration.ZkIntegrationTestBase;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestZkManagerFlappingDetection extends ZkIntegrationTestBase
{
  @Test
  public void testDisconnectHistory() throws Exception
  {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    final String clusterName = className + "_" + methodName;

    TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
                            "localhost", // participant name prefix
                            "TestDB", // resource name prefix
                            1, // resources
                            10, // partitions per resource
                            5, // number of nodes
                            3, // replicas
                            "MasterSlave",
                            true); // do rebalance
    
    
      String instanceName = "localhost_" + (12918 + 0);
      ZkHelixTestManager manager =
          new ZkHelixTestManager(clusterName,
                                 instanceName,
                                 InstanceType.PARTICIPANT,
                                 ZK_ADDR);
      manager.connect();
      ZkClient zkClient = manager.getZkClient();
      ZkTestHelper.expireSession(zkClient);
      for(int i = 0;i < 4; i++)
      {
        ZkTestHelper.expireSession(zkClient);
        Thread.sleep(1000);
        if(i < 5)
        {
          //System.out.println(i);
          Assert.assertTrue(manager.isConnected());
        }
      }
      ZkTestHelper.disconnectSession(zkClient);
      for(int i = 0; i < 20; i++)
      {
        Thread.sleep(500);
        System.out.println(i);
        if(!manager.isConnected()) break;
      }
      Assert.assertFalse(manager.isConnected());
  }
  
  @Test
  public void testDisconnectFlappingWindow() throws Exception
  {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String instanceName = "localhost_" + (12918 + 1);
    final String clusterName = className + "_" + methodName + UUID.randomUUID();

    testDisconnectFlappingWindow2(instanceName, InstanceType.PARTICIPANT);
    testDisconnectFlappingWindow2("admin", InstanceType.ADMINISTRATOR);
  }
  
  public void testDisconnectFlappingWindow2(String instanceName, InstanceType type) throws Exception
  {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    final String clusterName = className + "_" + methodName + UUID.randomUUID();

    TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
                            "localhost", // participant name prefix
                            "TestDB", // resource name prefix
                            1, // resources
                            10, // partitions per resource
                            5, // number of nodes
                            3, // replicas
                            "MasterSlave",
                            true); // do rebalance
    
    
      // flapping time window to 5 sec
      System.setProperty("helixmanager.flappingTimeWindow", "15000");
      System.setProperty("helixmanager.maxDisconnectThreshold", "7");
      ZkHelixTestManager manager2 =
          new ZkHelixTestManager(clusterName,
                                 instanceName,
                                 type,
                                 ZK_ADDR);
      manager2.connect();
      ZkClient zkClient = manager2.getZkClient();
      for(int i = 0;i < 3; i++)
      {
        ZkTestHelper.expireSession(zkClient);
        Thread.sleep(500);
        Assert.assertTrue(manager2.isConnected());
      }
      Thread.sleep(15000);
      // Old entries should be cleaned up
      for(int i = 0;i < 7; i++)
      {
        ZkTestHelper.expireSession(zkClient);
        Thread.sleep(1000);
        Assert.assertTrue(manager2.isConnected());
      }
      ZkTestHelper.disconnectSession(zkClient);
      for(int i = 0; i < 20; i++)
      {
        Thread.sleep(500);
        if(!manager2.isConnected()) break;
      }
      Assert.assertFalse(manager2.isConnected());
  }
  
  //@Test
  public void testDisconnectFlappingWindowController() throws Exception
  {
    String className = TestHelper.getTestClassName();
{'fraction_non_alphanumeric': 0.08884571226706703, 'fraction_numerical': 0.016250110394771703, 'mean_word_length': 2.0205388103494264, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3320679', 'n_tokens_mistral': 3020, 'n_tokens_neox': 2912, 'n_words': 752}