Search in sources :

Example 66 with ClusterSetup

use of org.apache.helix.tools.ClusterSetup in project helix by apache.

the class IntegrationTest method main.

public static void main(String[] args) throws InterruptedException {
    ZkServer server = null;
    ;
    try {
        String baseDir = "/tmp/IntegrationTest/";
        final String dataDir = baseDir + "zk/dataDir";
        final String logDir = baseDir + "/tmp/logDir";
        FileUtils.deleteDirectory(new File(dataDir));
        FileUtils.deleteDirectory(new File(logDir));
        IDefaultNameSpace defaultNameSpace = new IDefaultNameSpace() {

            @Override
            public void createDefaultNameSpace(ZkClient zkClient) {
            }
        };
        int zkPort = 2199;
        final String zkAddress = "localhost:" + zkPort;
        server = new ZkServer(dataDir, logDir, defaultNameSpace, zkPort);
        server.start();
        ClusterSetup setup = new ClusterSetup(zkAddress);
        final String clusterName = "file-store-test";
        setup.deleteCluster(clusterName);
        setup.addCluster(clusterName, true);
        setup.addInstanceToCluster(clusterName, "localhost_12001");
        setup.addInstanceToCluster(clusterName, "localhost_12002");
        setup.addInstanceToCluster(clusterName, "localhost_12003");
        setup.addResourceToCluster(clusterName, "repository", 1, "MasterSlave");
        setup.rebalanceResource(clusterName, "repository", 3);
        // Set the configuration
        final String instanceName1 = "localhost_12001";
        addConfiguration(setup, baseDir, clusterName, instanceName1);
        final String instanceName2 = "localhost_12002";
        addConfiguration(setup, baseDir, clusterName, instanceName2);
        final String instanceName3 = "localhost_12003";
        addConfiguration(setup, baseDir, clusterName, instanceName3);
        Thread thread1 = new Thread(new Runnable() {

            @Override
            public void run() {
                FileStore fileStore = null;
                try {
                    fileStore = new FileStore(zkAddress, clusterName, instanceName1);
                    fileStore.connect();
                } catch (Exception e) {
                    System.err.println("Exception" + e);
                    fileStore.disconnect();
                }
            }
        });
        // START NODES
        Thread thread2 = new Thread(new Runnable() {

            @Override
            public void run() {
                FileStore fileStore = new FileStore(zkAddress, clusterName, instanceName2);
                fileStore.connect();
            }
        });
        // START NODES
        Thread thread3 = new Thread(new Runnable() {

            @Override
            public void run() {
                FileStore fileStore = new FileStore(zkAddress, clusterName, instanceName3);
                fileStore.connect();
            }
        });
        System.out.println("STARTING NODES");
        thread1.start();
        thread2.start();
        thread3.start();
        // Start Controller
        final HelixManager manager = HelixControllerMain.startHelixController(zkAddress, clusterName, "controller", HelixControllerMain.STANDALONE);
        Thread.sleep(5000);
        printStatus(manager);
        listFiles(baseDir);
        System.out.println("Writing files a.txt and b.txt to current master " + baseDir + "localhost_12001" + "/filestore");
        FileUtils.writeStringToFile(new File(baseDir + "localhost_12001" + "/filestore/a.txt"), "some_data in a");
        FileUtils.writeStringToFile(new File(baseDir + "localhost_12001" + "/filestore/b.txt"), "some_data in b");
        Thread.sleep(10000);
        listFiles(baseDir);
        Thread.sleep(5000);
        System.out.println("Stopping the MASTER node:" + "localhost_12001");
        thread1.interrupt();
        Thread.sleep(10000);
        printStatus(manager);
        System.out.println("Writing files c.txt and d.txt to current master " + baseDir + "localhost_12002" + "/filestore");
        FileUtils.writeStringToFile(new File(baseDir + "localhost_12002" + "/filestore/c.txt"), "some_data in c");
        FileUtils.writeStringToFile(new File(baseDir + "localhost_12002" + "/filestore/d.txt"), "some_data in d");
        Thread.sleep(10000);
        listFiles(baseDir);
        System.out.println("Create or modify any files under " + baseDir + "localhost_12002" + "/filestore" + " and it should get replicated to " + baseDir + "localhost_12003" + "/filestore");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (server != null) {
        // server.shutdown();
        }
    }
    Thread.currentThread().join();
}
Also used : ZkClient(org.I0Itec.zkclient.ZkClient) HelixManager(org.apache.helix.HelixManager) IDefaultNameSpace(org.I0Itec.zkclient.IDefaultNameSpace) ClusterSetup(org.apache.helix.tools.ClusterSetup) IOException(java.io.IOException) File(java.io.File) ZkServer(org.I0Itec.zkclient.ZkServer)

Example 67 with ClusterSetup

use of org.apache.helix.tools.ClusterSetup in project helix by apache.

the class ClusterAccessor method updateCluster.

@POST
@Path("{clusterId}")
public Response updateCluster(@PathParam("clusterId") String clusterId, @QueryParam("command") String commandStr, @QueryParam("superCluster") String superCluster, String content) {
    Command command;
    try {
        command = getCommand(commandStr);
    } catch (HelixException ex) {
        return badRequest(ex.getMessage());
    }
    ClusterSetup clusterSetup = getClusterSetup();
    HelixAdmin helixAdmin = getHelixAdmin();
    switch(command) {
        case activate:
            if (superCluster == null) {
                return badRequest("Super Cluster name is missing!");
            }
            try {
                clusterSetup.activateCluster(clusterId, superCluster, true);
            } catch (Exception ex) {
                _logger.error("Failed to add cluster " + clusterId + " to super cluster " + superCluster);
                return serverError(ex);
            }
            break;
        case expand:
            try {
                clusterSetup.expandCluster(clusterId);
            } catch (Exception ex) {
                _logger.error("Failed to expand cluster " + clusterId);
                return serverError(ex);
            }
            break;
        case enable:
            try {
                helixAdmin.enableCluster(clusterId, true);
            } catch (Exception ex) {
                _logger.error("Failed to enable cluster " + clusterId);
                return serverError(ex);
            }
            break;
        case disable:
            try {
                helixAdmin.enableCluster(clusterId, false);
            } catch (Exception ex) {
                _logger.error("Failed to disable cluster " + clusterId);
                return serverError(ex);
            }
            break;
        case enableMaintenanceMode:
            try {
                helixAdmin.enableMaintenanceMode(clusterId, true, content);
            } catch (Exception ex) {
                _logger.error("Failed to enable maintenance mode " + clusterId);
                return serverError(ex);
            }
            break;
        case disableMaintenanceMode:
            try {
                helixAdmin.enableMaintenanceMode(clusterId, false);
            } catch (Exception ex) {
                _logger.error("Failed to disable maintenance mode " + clusterId);
                return serverError(ex);
            }
            break;
        default:
            return badRequest("Unsupported command " + command);
    }
    return OK();
}
Also used : HelixException(org.apache.helix.HelixException) ClusterSetup(org.apache.helix.tools.ClusterSetup) HelixAdmin(org.apache.helix.HelixAdmin) HelixException(org.apache.helix.HelixException) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 68 with ClusterSetup

use of org.apache.helix.tools.ClusterSetup in project helix by apache.

the class AdminTestBase method beforeSuite.

@BeforeSuite
public void beforeSuite() throws Exception {
    // TODO: use logging.properties file to config java.util.logging.Logger levels
    java.util.logging.Logger topJavaLogger = java.util.logging.Logger.getLogger("");
    topJavaLogger.setLevel(Level.WARNING);
    // start zk
    _zkServer = TestHelper.startZkServer(ZK_ADDR);
    AssertJUnit.assertTrue(_zkServer != null);
    ZKClientPool.reset();
    _gZkClient = new ZkClient(ZK_ADDR, ZkClient.DEFAULT_CONNECTION_TIMEOUT, ZkClient.DEFAULT_SESSION_TIMEOUT, new ZNRecordSerializer());
    _gSetupTool = new ClusterSetup(_gZkClient);
    // start admin
    _adminThread = new AdminThread(ZK_ADDR, ADMIN_PORT);
    _adminThread.start();
    // create a client
    _gClient = new Client(Protocol.HTTP);
    // wait for the web service to start
    Thread.sleep(100);
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) AdminThread(org.apache.helix.webapp.AdminTestHelper.AdminThread) ClusterSetup(org.apache.helix.tools.ClusterSetup) ZkClient(org.apache.helix.manager.zk.ZkClient) Client(org.restlet.Client) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer) BeforeSuite(org.testng.annotations.BeforeSuite)

Example 69 with ClusterSetup

use of org.apache.helix.tools.ClusterSetup in project helix by apache.

the class ClusterResource method getClusterRepresentation.

StringRepresentation getClusterRepresentation(String clusterName) throws JsonGenerationException, JsonMappingException, IOException {
    ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
    ClusterSetup setupTool = new ClusterSetup(zkClient);
    List<String> instances = setupTool.getClusterManagementTool().getInstancesInCluster(clusterName);
    ZNRecord clusterSummayRecord = new ZNRecord("Cluster Summary");
    clusterSummayRecord.setListField("participants", instances);
    List<String> resources = setupTool.getClusterManagementTool().getResourcesInCluster(clusterName);
    clusterSummayRecord.setListField("resources", resources);
    List<String> models = setupTool.getClusterManagementTool().getStateModelDefs(clusterName);
    clusterSummayRecord.setListField("stateModelDefs", models);
    HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
    Builder keyBuilder = accessor.keyBuilder();
    LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
    if (leader != null) {
        clusterSummayRecord.setSimpleField("LEADER", leader.getInstanceName());
    } else {
        clusterSummayRecord.setSimpleField("LEADER", "");
    }
    StringRepresentation representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(clusterSummayRecord), MediaType.APPLICATION_JSON);
    return representation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) StringRepresentation(org.restlet.representation.StringRepresentation) Builder(org.apache.helix.PropertyKey.Builder) ClusterSetup(org.apache.helix.tools.ClusterSetup) ZNRecord(org.apache.helix.ZNRecord)

Example 70 with ClusterSetup

use of org.apache.helix.tools.ClusterSetup in project helix by apache.

the class ConfigResource method getConfigKeys.

StringRepresentation getConfigKeys(ConfigScopeProperty scopeProperty, String... keys) throws Exception {
    StringRepresentation representation = null;
    ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
    ClusterSetup setupTool = new ClusterSetup(zkClient);
    HelixAdmin admin = setupTool.getClusterManagementTool();
    ZNRecord record = new ZNRecord(scopeProperty + " Config");
    HelixConfigScope scope = new HelixConfigScopeBuilder(scopeProperty, keys).build();
    List<String> configKeys = admin.getConfigKeys(scope);
    record.setListField(scopeProperty.toString(), configKeys);
    representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(record), MediaType.APPLICATION_JSON);
    return representation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) StringRepresentation(org.restlet.representation.StringRepresentation) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixConfigScope(org.apache.helix.model.HelixConfigScope) ClusterSetup(org.apache.helix.tools.ClusterSetup) HelixAdmin(org.apache.helix.HelixAdmin) ZNRecord(org.apache.helix.ZNRecord)

Aggregations

ClusterSetup (org.apache.helix.tools.ClusterSetup)79 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)33 BeforeClass (org.testng.annotations.BeforeClass)33 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)28 ZkClient (org.apache.helix.manager.zk.ZkClient)26 Date (java.util.Date)23 IOException (java.io.IOException)14 HelixException (org.apache.helix.HelixException)14 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)11 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)11 Test (org.testng.annotations.Test)11 ConfigAccessor (org.apache.helix.ConfigAccessor)9 ZNRecord (org.apache.helix.ZNRecord)9 HelixDataAccessor (org.apache.helix.HelixDataAccessor)8 IdealState (org.apache.helix.model.IdealState)8 BestPossAndExtViewZkVerifier (org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier)7 HelixAdmin (org.apache.helix.HelixAdmin)6 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)6 StringRepresentation (org.restlet.representation.StringRepresentation)6 ClusterDistributedController (org.apache.helix.integration.manager.ClusterDistributedController)5