Package org.apache.helix.agent

Source Code of org.apache.helix.agent.TestHelixAgent

package org.apache.helix.agent;

/*
* 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.File;
import java.util.Date;

import org.apache.helix.ConfigAccessor;
import org.apache.helix.ScriptTestHelper;
import org.apache.helix.TestHelper;
import org.apache.helix.ZkUnitTestBase;
import org.apache.helix.manager.zk.ZNRecordSerializer;
import org.apache.helix.manager.zk.ZkClient;
import org.apache.helix.mock.controller.ClusterController;
import org.apache.helix.model.HelixConfigScope;
import org.apache.helix.model.HelixConfigScope.ConfigScopeProperty;
import org.apache.helix.model.builder.HelixConfigScopeBuilder;
import org.apache.helix.tools.ClusterSetup;
import org.apache.helix.tools.ClusterStateVerifier;
import org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestHelixAgent extends ZkUnitTestBase {
 
  // disable this test
  // @Test
  public void test() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    final String clusterName = className + "_" + methodName;
    final int n = 1;
    final String zkAddr = ZK_ADDR;

    System.out.println("START " + clusterName + " at "
        + new Date(System.currentTimeMillis()));


    TestHelper.setupCluster(clusterName,
                            zkAddr,
                            12918, // participant port
                            "localhost", // participant name prefix
                            "TestDB", // resource name prefix
                            1, // resources
                            1, // partitions per resource
                            n, // number of nodes
                            1, // replicas
                            "MasterSlave",
                            true); // do rebalance

    // set cluster config
    ZkClient client = new ZkClient(zkAddr,
                                   ZkClient.DEFAULT_SESSION_TIMEOUT,
                                   ZkClient.DEFAULT_CONNECTION_TIMEOUT,
                                   new ZNRecordSerializer());

    HelixConfigScope scope = new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER)
                                    .forCluster(clusterName)
                                    .build();
    ConfigAccessor configAccessor = new ConfigAccessor(client);
   
    // run foo_test.py during S->M
    String workingDir = ScriptTestHelper.getPrefix() + ScriptTestHelper.INTEGRATION_SCRIPT_DIR;
    String pidFile = ScriptTestHelper.getPrefix() + ScriptTestHelper.INTEGRATION_LOG_DIR + "/default/foo_{PARTITION_NAME}_pid.txt";
   
    // System.out.println("workingDir: " + workingDir);
   
    // the pid file path for the first partition
    // delete it if exists
    String pidFileFirstPartition = ScriptTestHelper.getPrefix() + ScriptTestHelper.INTEGRATION_LOG_DIR + "/default/foo_TestDB0_0_pid.txt";
    File file = new File(pidFileFirstPartition);
    if (file.exists()) {
      file.delete();
    }
   
   
    CommandConfig.Builder builder = new CommandConfig.Builder();
    CommandConfig cmdConfig = builder.setTransition("SLAVE", "MASTER")
                                     .setCommand("dds_driver.py -c foo_test -o start")
                                     .setCommandWorkingDir(workingDir)
                                     .setCommandTimeout("0")
                                     .setPidFile(pidFile)
                                     .build();
    configAccessor.set(scope, cmdConfig.toKeyValueMap());
   
    // nop for O->S, M->S, and S->O
    builder = new CommandConfig.Builder();
    cmdConfig = builder.setTransition("OFFLINE", "SLAVE")
                                     .setCommand(CommandAttribute.NOP.getName())
                                     .build();
    configAccessor.set(scope, cmdConfig.toKeyValueMap());

    builder = new CommandConfig.Builder();
    cmdConfig = builder.setTransition("MASTER", "SLAVE")
                                     .setCommand(CommandAttribute.NOP.getName())
                                     .build();
    configAccessor.set(scope, cmdConfig.toKeyValueMap());
   
    builder = new CommandConfig.Builder();
    cmdConfig = builder.setTransition("SLAVE", "OFFLINE")
                                     .setCommand(CommandAttribute.NOP.getName())
                                     .build();
    configAccessor.set(scope, cmdConfig.toKeyValueMap());
   
    builder = new CommandConfig.Builder();
    cmdConfig = builder.setTransition("OFFLINE", "DROPPED")
                                     .setCommand(CommandAttribute.NOP.getName())
                                     .build();
    configAccessor.set(scope, cmdConfig.toKeyValueMap());


    // start controller
    ClusterController controller =
        new ClusterController(clusterName, "controller_0", zkAddr);
    controller.syncStart();
   
    // start helix-agent
    for (int i = 0; i < n; i++) {
      final String instanceName = "localhost_" + (12918 + i);
      Thread agentThread = new Thread() {
        @Override
        public void run() {
          try {
            HelixAgentMain.main(new String[]{"--zkSvr", zkAddr, "--cluster", clusterName,
                "--instanceName", instanceName, "--stateModel", "MasterSlave"});
          } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
      };
      agentThread.start();
     
      // wait participant thread to start
      Thread.sleep(100);
    }
   
    boolean result =
        ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR,
                                                                                 clusterName));
    Assert.assertTrue(result);

    // read the pid file should get current process id
    String readPid = SystemUtil.getPidFromFile(new File(pidFileFirstPartition));
    Assert.assertNotNull(readPid, "readPid is the pid for foo_test.py. should NOT be null");
   
    // String name = ManagementFactory.getRuntimeMXBean().getName();
    // String currentPid = name.substring(0,name.indexOf("@"));
   
    // System.out.println("read-pid: " + readPid + ", current-pid: " + currentPid);
   
    // drop resource will trigger M->S and S->O transitions
    ClusterSetup.processCommandLineArgs(new String[]{"--zkSvr", ZK_ADDR, "--dropResource", clusterName, "TestDB0"});
    result =
        ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR,
                                                                                 clusterName));
    Assert.assertTrue(result);
   

    System.out.println("END " + clusterName + " at "
        + new Date(System.currentTimeMillis()));

  }
}
TOP

Related Classes of org.apache.helix.agent.TestHelixAgent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.