Package org.apache.helix.model

Examples of org.apache.helix.model.ResourceAssignment


    // Verify the result
    BestPossibleStateOutput bestPossibleStateOutput =
        event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.toString());
    Assert.assertNotNull(bestPossibleStateOutput);
    ResourceId resourceId = ResourceId.from("TestDB0");
    ResourceAssignment assignment = bestPossibleStateOutput.getResourceAssignment(resourceId);
    Assert.assertNotNull(assignment);
    Resource resource = cluster.getResource(resourceId);
    verifySemiAutoRebalance(resource, assignment);

    System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis()));
View Full Code Here


    Cluster cluster = clusterAccessor.readCluster();

    ResourceId resourceId = ResourceId.from("TestDB0");
    Resource resource = cluster.getResource(resourceId);
    ResourceCurrentState currentStateOutput = new ResourceCurrentState();
    ResourceAssignment semiAutoResult =
        resource
            .getIdealState()
            .getRebalancerRef()
            .getRebalancer()
            .computeResourceMapping(resource.getIdealState(), resource.getRebalancerConfig(), null,
View Full Code Here

      Assert.fail("Fail to register ClusterStatusMonitor");
    }

    // Test #setPerInstanceResourceStatus()
    BestPossibleStateOutput bestPossibleStates = new BestPossibleStateOutput();
    ResourceAssignment assignment = new ResourceAssignment(ResourceId.from(testDB));
    Map<ParticipantId, State> replicaMap = Maps.newHashMap();
    replicaMap.put(ParticipantId.from("localhost_12918"), State.from("MASTER"));
    replicaMap.put(ParticipantId.from("localhost_12919"), State.from("SLAVE"));
    replicaMap.put(ParticipantId.from("localhost_12920"), State.from("SLAVE"));
    replicaMap.put(ParticipantId.from("localhost_12921"), State.from("OFFLINE"));
    replicaMap.put(ParticipantId.from("localhost_12922"), State.from("DROPPED"));
    assignment.addReplicaMap(PartitionId.from(testDB_0), replicaMap);
    bestPossibleStates.setResourceAssignment(ResourceId.from(testDB), assignment);

    Map<String, InstanceConfig> instanceConfigMap = Maps.newHashMap();
    for (int i = 0; i < n; i++) {
      String instanceName = "localhost_" + (12918 + i);
      InstanceConfig config = new InstanceConfig(instanceName);
      instanceConfigMap.put(instanceName, config);
    }

    Map<ResourceId, ResourceConfig> resourceMap = Maps.newHashMap();
    ResourceId resourceId = ResourceId.from(testDB);
    AutoModeISBuilder idealStateBuilder = new AutoModeISBuilder(resourceId).add(testDB_0);
    idealStateBuilder.setStateModel("MasterSlave");
    IdealState idealState = idealStateBuilder.build();
    ResourceConfig resourceConfig =
        new ResourceConfig.Builder(resourceId).idealState(idealState).build();
    resourceMap.put(resourceId, resourceConfig);

    Map<String, StateModelDefinition> stateModelDefMap = Maps.newHashMap();
    StateModelDefinition msStateModelDef =
        new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave());
    stateModelDefMap.put("MasterSlave", msStateModelDef);

    monitor.setPerInstanceResourceStatus(bestPossibleStates, instanceConfigMap, resourceMap,
        stateModelDefMap);

    // localhost_12918 should have 1 partition because it's MASTER
    ObjectName objName =
        monitor.getObjectName(monitor.getPerInstanceResourceBeanName("localhost_12918", testDB));
    Object value = _server.getAttribute(objName, "PartitionGauge");
    Assert.assertTrue(value instanceof Long);
    Assert.assertEquals((Long) value, new Long(1));
    value = _server.getAttribute(objName, "SensorName");
    Assert.assertTrue(value instanceof String);
    Assert.assertEquals((String) value, String.format("%s.%s.%s.%s.%s",
        ClusterStatusMonitor.PARTICIPANT_STATUS_KEY, clusterName, ClusterStatusMonitor.DEFAULT_TAG,
        "localhost_12918", testDB));

    // localhost_12919 should have 1 partition because it's SLAVE
    objName =
        monitor.getObjectName(monitor.getPerInstanceResourceBeanName("localhost_12919", testDB));
    value = _server.getAttribute(objName, "PartitionGauge");
    Assert.assertTrue(value instanceof Long);
    Assert.assertEquals((Long) value, new Long(1));

    // localhost_12921 should have 0 partition because it's OFFLINE
    objName =
        monitor.getObjectName(monitor.getPerInstanceResourceBeanName("localhost_12921", testDB));
    value = _server.getAttribute(objName, "PartitionGauge");
    Assert.assertTrue(value instanceof Long);
    Assert.assertEquals((Long) value, new Long(0));

    // localhost_12922 should have 0 partition because it's DROPPED
    objName =
        monitor.getObjectName(monitor.getPerInstanceResourceBeanName("localhost_12922", testDB));
    value = _server.getAttribute(objName, "PartitionGauge");
    Assert.assertTrue(value instanceof Long);
    Assert.assertEquals((Long) value, new Long(0));

    // Missing localhost_12918 in best possible ideal-state should remove it from mbean
    replicaMap.remove(ParticipantId.from("localhost_12918"));
    assignment.addReplicaMap(PartitionId.from(testDB_0), replicaMap);
    bestPossibleStates.setResourceAssignment(ResourceId.from(testDB), assignment);
    monitor.setPerInstanceResourceStatus(bestPossibleStates, instanceConfigMap, resourceMap,
        stateModelDefMap);
    try {
      objName =
View Full Code Here

  private BestPossibleStateOutput getEmptyBestPossibleStateOutput(List<IdealState> idealStates) {
    BestPossibleStateOutput output = new BestPossibleStateOutput();
    for (IdealState idealState : idealStates) {
      ResourceId resourceId = idealState.getResourceId();
      ResourceAssignment assignment = new ResourceAssignment(resourceId);
      for (PartitionId partitionId : idealState.getPartitionIdSet()) {
        Map<ParticipantId, State> emptyMap = Collections.emptyMap();
        assignment.addReplicaMap(partitionId, emptyMap);
      }
      output.setResourceAssignment(resourceId, assignment);
    }
    return output;
  }
View Full Code Here

   * @param stateModelDef state model the resource follows
   * @return assignment for the dropped resource
   */
  private ResourceAssignment mapDroppedResource(Cluster cluster, ResourceId resourceId,
      ResourceCurrentState currentStateOutput, StateModelDefinition stateModelDef) {
    ResourceAssignment partitionMapping = new ResourceAssignment(resourceId);
    Set<PartitionId> mappedPartitions =
        currentStateOutput.getCurrentStateMappedPartitions(resourceId);
    if (mappedPartitions == null) {
      return partitionMapping;
    }
    for (PartitionId partitionId : mappedPartitions) {
      Set<ParticipantId> disabledParticipantsForPartition =
          ConstraintBasedAssignment.getDisabledParticipants(cluster.getParticipantMap(),
              partitionId);
      Map<State, String> upperBounds =
          ConstraintBasedAssignment
              .stateConstraints(stateModelDef, resourceId, cluster.getConfig());
      partitionMapping.addReplicaMap(partitionId, ConstraintBasedAssignment
          .computeAutoBestStateForPartition(upperBounds, cluster.getLiveParticipantMap().keySet(),
              stateModelDef, null, currentStateOutput.getCurrentStateMap(resourceId, partitionId),
              disabledParticipantsForPartition, true));
    }
    return partitionMapping;
View Full Code Here

      }
      ResourceConfig resourceConfig = resourceMap.get(resourceId);
      RebalancerConfig rebalancerConfig = resourceConfig.getRebalancerConfig();
      IdealState idealState = resourceConfig.getIdealState();
      StateModelDefinition stateModelDef = stateModelDefs.get(idealState.getStateModelDefId());
      ResourceAssignment resourceAssignment = null;
      // use a cached rebalancer if possible
      RebalancerRef ref = idealState.getRebalancerRef();
      HelixRebalancer rebalancer = null;
      if (_rebalancerMap.containsKey(resourceId)) {
        HelixRebalancer candidateRebalancer = _rebalancerMap.get(resourceId);
        if (ref != null && candidateRebalancer.getClass().equals(ref.toString())) {
          rebalancer = candidateRebalancer;
        }
      }

      // otherwise instantiate a new one
      if (rebalancer == null) {
        if (ref != null) {
          rebalancer = ref.getRebalancer();
        }
        HelixManager manager = event.getAttribute("helixmanager");
        ControllerContextProvider provider =
            event.getAttribute(AttributeName.CONTEXT_PROVIDER.toString());
        if (rebalancer == null) {
          rebalancer = new FallbackRebalancer();
        }
        rebalancer.init(manager, provider);
        _rebalancerMap.put(resourceId, rebalancer);
      }
      ResourceAssignment currentAssignment = null;
      try {
        resourceAssignment =
            rebalancer.computeResourceMapping(idealState, rebalancerConfig, currentAssignment,
                cluster, currentStateOutput);
      } catch (Exception e) {
View Full Code Here

      BestPossibleStateOutput assignments =
          event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.toString());
      List<ResourceAssignment> changedAssignments = Lists.newLinkedList();
      List<PropertyKey> changedKeys = Lists.newLinkedList();
      for (ResourceId resourceId : assignments.getAssignedResources()) {
        ResourceAssignment assignment = assignments.getResourceAssignment(resourceId);
        Resource resource = cluster.getResource(resourceId);
        boolean toAdd = false;
        if (resource != null) {
          ResourceAssignment existAssignment = resource.getResourceAssignment();
          if (existAssignment == null || !existAssignment.equals(assignment)) {
            toAdd = true;
          }
        } else {
          toAdd = true;
        }
View Full Code Here

    if (!isReady) {
      return emptyAssignment(resourceName, currStateOutput);
    }

    // Grab the old assignment, or an empty one if it doesn't exist
    ResourceAssignment prevAssignment = TaskUtil.getPrevResourceAssignment(_manager, resourceName);
    if (prevAssignment == null) {
      prevAssignment = new ResourceAssignment(ResourceId.from(resourceName));
    }

    // Will contain the list of partitions that must be explicitly dropped from the ideal state that
    // is stored in zk.
    // Fetch the previous resource assignment from the property store. This is required because of
    // HELIX-230.
    Set<Integer> partitionsToDrop = new TreeSet<Integer>();

    ResourceAssignment newAssignment =
        computeResourceMapping(resourceName, workflowCfg, jobCfg, prevAssignment, clusterData
            .getLiveParticipantMap().keySet(), currStateOutput, workflowCtx, jobCtx,
            partitionsToDrop, clusterData);

    if (!partitionsToDrop.isEmpty()) {
View Full Code Here

        }
      }
    }

    // Construct a ResourceAssignment object from the map of partition assignments.
    ResourceAssignment ra = new ResourceAssignment(ResourceId.from(jobResource));
    for (Map.Entry<Integer, PartitionAssignment> e : paMap.entrySet()) {
      PartitionAssignment pa = e.getValue();
      ra.addReplicaMap(PartitionId.from(pName(jobResource, e.getKey())),
          ImmutableMap.of(ParticipantId.from(pa._instance), State.from(pa._state)));
    }
    return ra;
  }
View Full Code Here

  }

  private static ResourceAssignment emptyAssignment(String name,
      ResourceCurrentState currStateOutput) {
    ResourceId resourceId = ResourceId.from(name);
    ResourceAssignment assignment = new ResourceAssignment(resourceId);
    Set<PartitionId> partitions = currStateOutput.getCurrentStateMappedPartitions(resourceId);
    for (PartitionId partition : partitions) {
      Map<ParticipantId, State> currentStateMap =
          currStateOutput.getCurrentStateMap(resourceId, partition);
      Map<ParticipantId, State> replicaMap = Maps.newHashMap();
      for (ParticipantId participantId : currentStateMap.keySet()) {
        replicaMap.put(participantId, State.from(HelixDefinedState.DROPPED));
      }
      assignment.addReplicaMap(partition, replicaMap);
    }
    return assignment;
  }
View Full Code Here

TOP

Related Classes of org.apache.helix.model.ResourceAssignment

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.