Search in sources :

Example 36 with ZKHelixAdmin

use of org.apache.helix.manager.zk.ZKHelixAdmin in project ambry by linkedin.

the class HelixParticipantTest method clear.

@After
public void clear() {
    ZKHelixAdmin admin = new ZKHelixAdmin("localhost:" + zkInfo.getPort());
    admin.dropCluster(clusterName);
}
Also used : ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) After(org.junit.After)

Example 37 with ZKHelixAdmin

use of org.apache.helix.manager.zk.ZKHelixAdmin in project ambry by linkedin.

the class HelixVcrUtil method createCluster.

/**
 * Create a helix cluster with given information.
 * @param destZkString the cluster's zk string
 * @param destClusterName the cluster's name
 */
public static void createCluster(String destZkString, String destClusterName, VcrHelixConfig config) {
    HelixZkClient destZkClient = getHelixZkClient(destZkString);
    HelixAdmin destAdmin = new ZKHelixAdmin(destZkClient);
    if (ZKUtil.isClusterSetup(destClusterName, destZkClient)) {
        errorAndExit("Failed to create cluster because " + destClusterName + " already exist.");
    }
    ClusterSetup clusterSetup = new ClusterSetup.Builder().setZkAddress(destZkString).build();
    clusterSetup.addCluster(destClusterName, true);
    // set ALLOW_PARTICIPANT_AUTO_JOIN
    HelixConfigScope configScope = new HelixConfigScopeBuilder(HelixConfigScope.ConfigScopeProperty.CLUSTER).forCluster(destClusterName).build();
    Map<String, String> helixClusterProperties = new HashMap<>();
    helixClusterProperties.put(ZKHelixManager.ALLOW_PARTICIPANT_AUTO_JOIN, String.valueOf(config.getClusterConfigFields().isAllowAutoJoin()));
    destAdmin.setConfig(configScope, helixClusterProperties);
    setClusterConfig(destZkClient, destClusterName, config, false);
    logger.info("Cluster {} is created successfully!", destClusterName);
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) HashMap(java.util.HashMap) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) FullAutoModeISBuilder(org.apache.helix.model.builder.FullAutoModeISBuilder) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixConfigScope(org.apache.helix.model.HelixConfigScope) HelixAdmin(org.apache.helix.HelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) ClusterSetup(org.apache.helix.tools.ClusterSetup)

Example 38 with ZKHelixAdmin

use of org.apache.helix.manager.zk.ZKHelixAdmin in project ambry by linkedin.

the class HelixVcrUtil method updateResourceAndPartition.

/**
 * Update dest cluster information based on src cluster.
 * Dest cluster resource will be recreated if it mismatches that in src cluster.
 * @param srcZkString the src cluster's zk string
 * @param srcClusterName the src cluster's name
 * @param destZkString the dest cluster's zk string
 * @param destClusterName the dest cluster's name
 * @param dryRun run the update process but without actual change.
 */
public static void updateResourceAndPartition(String srcZkString, String srcClusterName, String destZkString, String destClusterName, VcrHelixConfig config, boolean dryRun) {
    HelixAdmin srcAdmin = new ZKHelixAdmin(srcZkString);
    Set<String> srcResources = new HashSet<>(srcAdmin.getResourcesInCluster(srcClusterName));
    HelixAdmin destAdmin = new ZKHelixAdmin(destZkString);
    Set<String> destResources = new HashSet<>(destAdmin.getResourcesInCluster(destClusterName));
    logger.info("========VCR Helix Update Starts========");
    // Remove stale resources
    for (String resource : destResources) {
        if (!srcResources.contains(resource)) {
            if (dryRun) {
                logger.info("DryRun: Drop Resource {}", resource);
            } else {
                // This resource need to be dropped.
                logger.info("Dropped Resource {}, Partitions: {}", resource, destAdmin.getResourceIdealState(destClusterName, resource).getPartitionSet());
                destAdmin.dropResource(destClusterName, resource);
            }
        }
    }
    for (String resource : srcResources) {
        if (ignoreResourceKeyWords.stream().anyMatch(resource::contains)) {
            logger.info("Resource {} from src cluster is ignored", resource);
            continue;
        }
        boolean createNewResource = false;
        boolean dropResource = false;
        if (destResources.contains(resource)) {
            // check if every partition exist.
            Set<String> srcPartitions = srcAdmin.getResourceIdealState(srcClusterName, resource).getPartitionSet();
            Set<String> destPartitions = destAdmin.getResourceIdealState(destClusterName, resource).getPartitionSet();
            if (srcPartitions.size() != destPartitions.size()) {
                dropResource = true;
                createNewResource = true;
            } else {
                for (String partition : srcPartitions) {
                    if (!destPartitions.contains(partition)) {
                        dropResource = true;
                        createNewResource = true;
                        break;
                    }
                }
            }
            if (dropResource && dryRun) {
                logger.info("DryRun: Drop Resource {}", resource);
            } else if (dropResource) {
                // This resource need to be recreate.
                logger.info("Dropped Resource {}, Partitions: {}", resource, destAdmin.getResourceIdealState(destClusterName, resource).getPartitionSet());
                destAdmin.dropResource(destClusterName, resource);
            }
        } else {
            createNewResource = true;
        }
        if (createNewResource) {
            // add new resource
            Set<String> srcPartitions = srcAdmin.getResourceIdealState(srcClusterName, resource).getPartitionSet();
            IdealState idealState = buildIdealState(resource, srcPartitions, config.getIdealStateConfigFields());
            if (dryRun) {
                logger.info("DryRun: Add Resource {} with partition {}", resource, srcPartitions);
            } else {
                destAdmin.addResource(destClusterName, resource, idealState);
                destAdmin.rebalance(destClusterName, resource, config.getIdealStateConfigFields().getNumReplicas(), "", "");
                logger.info("Added Resource {}  with partition {}", resource, srcPartitions);
            }
        } else {
            logger.info("Resource {} is up to date. No action needed.", resource);
        }
    }
    logger.info("Cluster {} is updated successfully!", destClusterName);
    logger.info("========VCR Helix Update Ends========");
}
Also used : ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) HelixAdmin(org.apache.helix.HelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) IdealState(org.apache.helix.model.IdealState) HashSet(java.util.HashSet)

Example 39 with ZKHelixAdmin

use of org.apache.helix.manager.zk.ZKHelixAdmin in project ambry by linkedin.

the class HelixVcrUtil method maintainCluster.

/**
 * Enable or disable maintenance mode for a cluster.
 * @param destZkString the cluster's zk string
 * @param destClusterName the cluster's name
 * @param enable enter maintenance mode if true
 */
public static void maintainCluster(String destZkString, String destClusterName, boolean enable) {
    HelixZkClient destZkClient = getHelixZkClient(destZkString);
    HelixAdmin destAdmin = new ZKHelixAdmin(destZkClient);
    destAdmin.enableMaintenanceMode(destClusterName, enable);
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) HelixAdmin(org.apache.helix.HelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin)

Example 40 with ZKHelixAdmin

use of org.apache.helix.manager.zk.ZKHelixAdmin in project ambry by linkedin.

the class HelixVcrUtil method isSrcDestSync.

/**
 * A method to verify resources and partitions in src cluster and dest cluster are same.
 */
public static boolean isSrcDestSync(String srcZkString, String srcClusterName, String destZkString, String destClusterName) {
    HelixAdmin srcAdmin = new ZKHelixAdmin(srcZkString);
    Set<String> srcResources = new HashSet<>(srcAdmin.getResourcesInCluster(srcClusterName));
    HelixAdmin destAdmin = new ZKHelixAdmin(destZkString);
    Set<String> destResources = new HashSet<>(destAdmin.getResourcesInCluster(destClusterName));
    for (String resource : srcResources) {
        if (HelixVcrUtil.ignoreResourceKeyWords.stream().anyMatch(resource::contains)) {
            System.out.println("Resource " + resource + " from src cluster is ignored");
            continue;
        }
        if (destResources.contains(resource)) {
            // check if every partition exist.
            Set<String> srcPartitions = srcAdmin.getResourceIdealState(srcClusterName, resource).getPartitionSet();
            Set<String> destPartitions = destAdmin.getResourceIdealState(destClusterName, resource).getPartitionSet();
            for (String partition : srcPartitions) {
                if (!destPartitions.contains(partition)) {
                    return false;
                }
            }
        } else {
            return false;
        }
    }
    return true;
}
Also used : ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) HelixAdmin(org.apache.helix.HelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) HashSet(java.util.HashSet)

Aggregations

ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)70 HelixAdmin (org.apache.helix.HelixAdmin)31 IdealState (org.apache.helix.model.IdealState)25 Test (org.testng.annotations.Test)23 Date (java.util.Date)21 InstanceConfig (org.apache.helix.model.InstanceConfig)16 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)14 ZNRecord (org.apache.helix.ZNRecord)13 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)13 ZNRecordSerializer (org.apache.helix.manager.zk.ZNRecordSerializer)12 ZkClient (org.apache.helix.manager.zk.ZkClient)12 ClusterStateVerifier (org.apache.helix.tools.ClusterStateVerifier)12 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)11 StateModelDefinition (org.apache.helix.model.StateModelDefinition)11 HashMap (java.util.HashMap)10 HashSet (java.util.HashSet)10 HelixDataAccessor (org.apache.helix.HelixDataAccessor)8 ExternalView (org.apache.helix.model.ExternalView)8 Test (org.junit.Test)8 BestPossAndExtViewZkVerifier (org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier)7