Search in sources :

Example 36 with ConfigAccessor

use of org.apache.helix.ConfigAccessor in project helix by apache.

the class ResourceAccessor method updateResourceConfig.

@POST
@Path("{resourceName}/configs")
public Response updateResourceConfig(@PathParam("clusterId") String clusterId, @PathParam("resourceName") String resourceName, String content) {
    ZNRecord record;
    try {
        record = toZNRecord(content);
    } catch (IOException e) {
        _logger.error("Failed to deserialize user's input " + content + ", Exception: " + e);
        return badRequest("Input is not a vaild ZNRecord!");
    }
    ResourceConfig resourceConfig = new ResourceConfig(record);
    ConfigAccessor configAccessor = getConfigAccessor();
    try {
        configAccessor.updateResourceConfig(clusterId, resourceName, resourceConfig);
    } catch (HelixException ex) {
        return notFound(ex.getMessage());
    } catch (Exception ex) {
        _logger.error("Failed to update cluster config, cluster " + clusterId + " new config: " + content + ", Exception: " + ex);
        return serverError(ex);
    }
    return OK();
}
Also used : HelixException(org.apache.helix.HelixException) IOException(java.io.IOException) ResourceConfig(org.apache.helix.model.ResourceConfig) ConfigAccessor(org.apache.helix.ConfigAccessor) ZNRecord(org.apache.helix.ZNRecord) HelixException(org.apache.helix.HelixException) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 37 with ConfigAccessor

use of org.apache.helix.ConfigAccessor in project helix by apache.

the class ResourceAccessor method getResource.

@GET
@Path("{resourceName}")
public Response getResource(@PathParam("clusterId") String clusterId, @PathParam("resourceName") String resourceName) {
    ConfigAccessor accessor = getConfigAccessor();
    HelixAdmin admin = getHelixAdmin();
    ResourceConfig resourceConfig = accessor.getResourceConfig(clusterId, resourceName);
    IdealState idealState = admin.getResourceIdealState(clusterId, resourceName);
    ExternalView externalView = admin.getResourceExternalView(clusterId, resourceName);
    Map<String, ZNRecord> resourceMap = new HashMap<>();
    if (idealState != null) {
        resourceMap.put(ResourceProperties.idealState.name(), idealState.getRecord());
    } else {
        return notFound();
    }
    resourceMap.put(ResourceProperties.resourceConfig.name(), null);
    resourceMap.put(ResourceProperties.externalView.name(), null);
    if (resourceConfig != null) {
        resourceMap.put(ResourceProperties.resourceConfig.name(), resourceConfig.getRecord());
    }
    if (externalView != null) {
        resourceMap.put(ResourceProperties.externalView.name(), externalView.getRecord());
    }
    return JSONRepresentation(resourceMap);
}
Also used : ExternalView(org.apache.helix.model.ExternalView) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConfigAccessor(org.apache.helix.ConfigAccessor) ResourceConfig(org.apache.helix.model.ResourceConfig) HelixAdmin(org.apache.helix.HelixAdmin) IdealState(org.apache.helix.model.IdealState) ZNRecord(org.apache.helix.ZNRecord) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 38 with ConfigAccessor

use of org.apache.helix.ConfigAccessor in project helix by apache.

the class ClusterAccessor method updateClusterConfig.

@POST
@Path("{clusterId}/configs")
public Response updateClusterConfig(@PathParam("clusterId") String clusterId, @QueryParam("command") String commandStr, String content) {
    Command command;
    try {
        command = getCommand(commandStr);
    } catch (HelixException ex) {
        return badRequest(ex.getMessage());
    }
    ZNRecord record;
    try {
        record = toZNRecord(content);
    } catch (IOException e) {
        _logger.error("Failed to deserialize user's input " + content + ", Exception: " + e);
        return badRequest("Input is not a valid ZNRecord!");
    }
    if (!record.getId().equals(clusterId)) {
        return badRequest("ID does not match the cluster name in input!");
    }
    ClusterConfig config = new ClusterConfig(record);
    ConfigAccessor configAccessor = getConfigAccessor();
    try {
        switch(command) {
            case update:
                configAccessor.updateClusterConfig(clusterId, config);
                break;
            case delete:
                {
                    HelixConfigScope clusterScope = new HelixConfigScopeBuilder(HelixConfigScope.ConfigScopeProperty.CLUSTER).forCluster(clusterId).build();
                    configAccessor.remove(clusterScope, config.getRecord());
                }
                break;
            default:
                return badRequest("Unsupported command " + commandStr);
        }
    } catch (HelixException ex) {
        return notFound(ex.getMessage());
    } catch (Exception ex) {
        _logger.error("Failed to " + command + " cluster config, cluster " + clusterId + " new config: " + content + ", Exception: " + ex);
        return serverError(ex);
    }
    return OK();
}
Also used : HelixException(org.apache.helix.HelixException) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) IOException(java.io.IOException) ConfigAccessor(org.apache.helix.ConfigAccessor) HelixConfigScope(org.apache.helix.model.HelixConfigScope) ZNRecord(org.apache.helix.ZNRecord) HelixException(org.apache.helix.HelixException) IOException(java.io.IOException) ClusterConfig(org.apache.helix.model.ClusterConfig) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 39 with ConfigAccessor

use of org.apache.helix.ConfigAccessor in project helix by apache.

the class TestHelixAgent method 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()));
    // participant port
    TestHelper.setupCluster(// participant port
    clusterName, // participant port
    zkAddr, // participant port
    12918, // participant name prefix
    "localhost", // resource name prefix
    "TestDB", // resources
    1, // partitions per resource
    1, // number of nodes
    n, // replicas
    1, "MasterSlave", // do rebalance
    true);
    // set cluster config
    HelixConfigScope scope = new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(clusterName).build();
    ConfigAccessor configAccessor = new ConfigAccessor(_gZkClient);
    // String pidFile = ScriptTestHelper.getPrefix() + ScriptTestHelper.INTEGRATION_LOG_DIR +
    // "/default/foo_{PARTITION_NAME}_pid.txt";
    // 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();
    // }
    // set commands for state-transitions
    CommandConfig.Builder builder = new CommandConfig.Builder();
    CommandConfig cmdConfig = builder.setTransition("SLAVE", "MASTER").setCommand("simpleHttpClient.py SLAVE-MASTER").setCommandWorkingDir(workingDir).setCommandTimeout("0").build();
    configAccessor.set(scope, cmdConfig.toKeyValueMap());
    builder = new CommandConfig.Builder();
    cmdConfig = builder.setTransition("OFFLINE", "SLAVE").setCommand("simpleHttpClient.py OFFLINE-SLAVE").setCommandWorkingDir(workingDir).build();
    configAccessor.set(scope, cmdConfig.toKeyValueMap());
    builder = new CommandConfig.Builder();
    cmdConfig = builder.setTransition("MASTER", "SLAVE").setCommand("simpleHttpClient.py MASTER-SLAVE").setCommandWorkingDir(workingDir).build();
    configAccessor.set(scope, cmdConfig.toKeyValueMap());
    builder = new CommandConfig.Builder();
    cmdConfig = builder.setTransition("SLAVE", "OFFLINE").setCommand("simpleHttpClient.py SLAVE-OFFLINE").setCommandWorkingDir(workingDir).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
    ClusterControllerManager controller = new ClusterControllerManager(zkAddr, clusterName, "controller_0");
    controller.syncStart();
    // start helix-agent
    Map<String, Thread> agents = new HashMap<String, Thread>();
    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) {
                    LOG.error("Exception start helix-agent", e);
                }
            }
        };
        agents.put(instanceName, agentThread);
        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);
    // clean up
    controller.syncStop();
    for (Thread agentThread : agents.values()) {
        agentThread.interrupt();
    }
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : HashMap(java.util.HashMap) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) ConfigAccessor(org.apache.helix.ConfigAccessor) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) HelixConfigScope(org.apache.helix.model.HelixConfigScope) Test(org.testng.annotations.Test)

Example 40 with ConfigAccessor

use of org.apache.helix.ConfigAccessor in project helix by apache.

the class TestConfigThreadpoolSize method TestThreadPoolSizeConfig.

@Test
public void TestThreadPoolSizeConfig() {
    String instanceName = PARTICIPANT_PREFIX + "_" + (START_PORT + 0);
    HelixManager manager = _participants[0];
    ConfigAccessor accessor = manager.getConfigAccessor();
    ConfigScope scope = new ConfigScopeBuilder().forCluster(manager.getClusterName()).forParticipant(instanceName).build();
    accessor.set(scope, "TestMsg." + HelixTaskExecutor.MAX_THREADS, "" + 12);
    scope = new ConfigScopeBuilder().forCluster(manager.getClusterName()).build();
    accessor.set(scope, "TestMsg." + HelixTaskExecutor.MAX_THREADS, "" + 8);
    for (int i = 0; i < NODE_NR; i++) {
        instanceName = PARTICIPANT_PREFIX + "_" + (START_PORT + i);
        _participants[i].getMessagingService().registerMessageHandlerFactory("TestMsg", new TestMessagingHandlerFactory());
        _participants[i].getMessagingService().registerMessageHandlerFactory("TestMsg2", new TestMessagingHandlerFactory2());
    }
    for (int i = 0; i < NODE_NR; i++) {
        instanceName = PARTICIPANT_PREFIX + "_" + (START_PORT + i);
        DefaultMessagingService svc = (DefaultMessagingService) (_participants[i].getMessagingService());
        HelixTaskExecutor helixExecutor = svc.getExecutor();
        ThreadPoolExecutor executor = (ThreadPoolExecutor) (helixExecutor._executorMap.get("TestMsg"));
        ThreadPoolExecutor executor2 = (ThreadPoolExecutor) (helixExecutor._executorMap.get("TestMsg2"));
        if (i != 0) {
            Assert.assertEquals(8, executor.getMaximumPoolSize());
        } else {
            Assert.assertEquals(12, executor.getMaximumPoolSize());
        }
        Assert.assertEquals(HelixTaskExecutor.DEFAULT_PARALLEL_TASKS, executor2.getMaximumPoolSize());
    }
}
Also used : HelixManager(org.apache.helix.HelixManager) ConfigScope(org.apache.helix.model.ConfigScope) DefaultMessagingService(org.apache.helix.messaging.DefaultMessagingService) ConfigAccessor(org.apache.helix.ConfigAccessor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ConfigScopeBuilder(org.apache.helix.model.builder.ConfigScopeBuilder) Test(org.testng.annotations.Test)

Aggregations

ConfigAccessor (org.apache.helix.ConfigAccessor)41 ClusterConfig (org.apache.helix.model.ClusterConfig)15 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)14 BeforeClass (org.testng.annotations.BeforeClass)14 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)10 ClusterSetup (org.apache.helix.tools.ClusterSetup)9 Date (java.util.Date)7 HelixConfigScope (org.apache.helix.model.HelixConfigScope)7 HelixConfigScopeBuilder (org.apache.helix.model.builder.HelixConfigScopeBuilder)7 Test (org.testng.annotations.Test)7 HelixException (org.apache.helix.HelixException)6 Path (javax.ws.rs.Path)5 BestPossibleExternalViewVerifier (org.apache.helix.tools.ClusterVerifiers.BestPossibleExternalViewVerifier)4 HelixClusterVerifier (org.apache.helix.tools.ClusterVerifiers.HelixClusterVerifier)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 GET (javax.ws.rs.GET)3 HelixManager (org.apache.helix.HelixManager)3 ZNRecord (org.apache.helix.ZNRecord)3 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)3