Search in sources :

Example 71 with HelixAdmin

use of org.apache.helix.HelixAdmin 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 72 with HelixAdmin

use of org.apache.helix.HelixAdmin 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 73 with HelixAdmin

use of org.apache.helix.HelixAdmin 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)

Example 74 with HelixAdmin

use of org.apache.helix.HelixAdmin in project ambry by linkedin.

the class TestUtils 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)

Example 75 with HelixAdmin

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

the class HelixStateTransitionHandler method disablePartition.

void disablePartition() {
    String instanceName = _manager.getInstanceName();
    String resourceName = _message.getResourceName();
    String partitionName = _message.getPartitionName();
    String clusterName = _manager.getClusterName();
    HelixAdmin admin = _manager.getClusterManagmentTool();
    admin.enablePartition(false, clusterName, instanceName, resourceName, Arrays.asList(partitionName));
    logger.info("error in transit from ERROR to " + _message.getToState() + " for partition: " + partitionName + ". disable it on " + instanceName);
}
Also used : HelixAdmin(org.apache.helix.HelixAdmin)

Aggregations

HelixAdmin (org.apache.helix.HelixAdmin)104 IdealState (org.apache.helix.model.IdealState)44 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)43 Test (org.testng.annotations.Test)40 HashMap (java.util.HashMap)30 ZNRecord (org.apache.helix.ZNRecord)28 Date (java.util.Date)22 InstanceConfig (org.apache.helix.model.InstanceConfig)22 ArrayList (java.util.ArrayList)18 Map (java.util.Map)17 HashSet (java.util.HashSet)16 ExternalView (org.apache.helix.model.ExternalView)16 StateModelDefinition (org.apache.helix.model.StateModelDefinition)16 IOException (java.io.IOException)13 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)13 HelixDataAccessor (org.apache.helix.HelixDataAccessor)12 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)12 TreeMap (java.util.TreeMap)11 PropertyKey (org.apache.helix.PropertyKey)11 HelixConfigScopeBuilder (org.apache.helix.model.builder.HelixConfigScopeBuilder)11