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();
}
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);
}
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();
}
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()));
}
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());
}
}
Aggregations