Search in sources :

Example 1 with PackingException

use of com.twitter.heron.spi.packing.PackingException in project heron by twitter.

the class LauncherUtils method createPackingPlan.

/**
   * Returns a packing plan generated by configured packing class
   */
public PackingPlan createPackingPlan(final Config config, final Config runtime) throws PackingException {
    // Create an instance of the packing class
    String packingClass = Context.packingClass(config);
    IPacking packing;
    try {
        // create an instance of the packing class
        packing = ReflectionUtils.newInstance(packingClass);
    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
        throw new PackingException(String.format("Failed to instantiate packing instance using packing class %s", packingClass), e);
    }
    try {
        TopologyAPI.Topology topology = Runtime.topology(runtime);
        packing.initialize(config, topology);
        return packing.pack();
    } finally {
        SysUtils.closeIgnoringExceptions(packing);
    }
}
Also used : IPacking(com.twitter.heron.spi.packing.IPacking) PackingException(com.twitter.heron.spi.packing.PackingException) TopologyAPI(com.twitter.heron.api.generated.TopologyAPI)

Example 2 with PackingException

use of com.twitter.heron.spi.packing.PackingException in project incubator-heron by apache.

the class SubmitterMain method submitTopology.

/**
 * Submit a topology
 * 1. Instantiate necessary resources
 * 2. Valid whether it is legal to submit a topology
 * 3. Call LauncherRunner
 */
public void submitTopology() throws TopologySubmissionException {
    // build primary runtime config first
    Config primaryRuntime = Config.newBuilder().putAll(LauncherUtils.getInstance().createPrimaryRuntime(topology)).build();
    // call launcher directly here if in dry-run mode
    if (Context.dryRun(config)) {
        callLauncherRunner(primaryRuntime);
        return;
    }
    // 1. Do prepare work
    // create an instance of state manager
    String statemgrClass = Context.stateManagerClass(config);
    IStateManager statemgr;
    // Create an instance of the launcher class
    String launcherClass = Context.launcherClass(config);
    ILauncher launcher;
    // create an instance of the uploader class
    String uploaderClass = Context.uploaderClass(config);
    IUploader uploader;
    // create an instance of state manager
    try {
        statemgr = ReflectionUtils.newInstance(statemgrClass);
    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
        throw new TopologySubmissionException(String.format("Failed to instantiate state manager class '%s'", statemgrClass), e);
    }
    // create an instance of launcher
    try {
        launcher = ReflectionUtils.newInstance(launcherClass);
    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
        throw new LauncherException(String.format("Failed to instantiate launcher class '%s'", launcherClass), e);
    }
    // create an instance of uploader
    try {
        uploader = ReflectionUtils.newInstance(uploaderClass);
    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
        throw new UploaderException(String.format("Failed to instantiate uploader class '%s'", uploaderClass), e);
    }
    // Put it in a try block so that we can always clean resources
    try {
        // initialize the state manager
        statemgr.initialize(config);
        // initialize the uploader
        uploader.initialize(config);
        // TODO(mfu): timeout should read from config
        SchedulerStateManagerAdaptor adaptor = new SchedulerStateManagerAdaptor(statemgr, 5000);
        // Check if topology is already running
        validateSubmit(adaptor, topology.getName());
        LOG.log(Level.FINE, "Topology {0} to be submitted", topology.getName());
        Config runtimeWithoutPackageURI = Config.newBuilder().putAll(primaryRuntime).putAll(LauncherUtils.getInstance().createAdaptorRuntime(adaptor)).put(Key.LAUNCHER_CLASS_INSTANCE, launcher).build();
        PackingPlan packingPlan = LauncherUtils.getInstance().createPackingPlan(config, runtimeWithoutPackageURI);
        // The packing plan might call for a number of containers different than the config
        // settings. If that's the case we need to modify the configs to match.
        runtimeWithoutPackageURI = updateNumContainersIfNeeded(runtimeWithoutPackageURI, topology, packingPlan);
        // If the packing plan is valid we will upload necessary packages
        URI packageURI = uploadPackage(uploader);
        // Update the runtime config with the packageURI
        Config runtimeAll = Config.newBuilder().putAll(runtimeWithoutPackageURI).put(Key.TOPOLOGY_PACKAGE_URI, packageURI).build();
        callLauncherRunner(runtimeAll);
    } catch (LauncherException | PackingException e) {
        // we undo uploading of topology package only if launcher fails to
        // launch topology, which will throw LauncherException or PackingException
        uploader.undo();
        throw e;
    } finally {
        SysUtils.closeIgnoringExceptions(uploader);
        SysUtils.closeIgnoringExceptions(launcher);
        SysUtils.closeIgnoringExceptions(statemgr);
    }
}
Also used : IStateManager(com.twitter.heron.spi.statemgr.IStateManager) IUploader(com.twitter.heron.spi.uploader.IUploader) LauncherException(com.twitter.heron.spi.scheduler.LauncherException) Config(com.twitter.heron.spi.common.Config) PackingPlan(com.twitter.heron.spi.packing.PackingPlan) UploaderException(com.twitter.heron.spi.uploader.UploaderException) URI(java.net.URI) SchedulerStateManagerAdaptor(com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor) ILauncher(com.twitter.heron.spi.scheduler.ILauncher) PackingException(com.twitter.heron.spi.packing.PackingException)

Example 3 with PackingException

use of com.twitter.heron.spi.packing.PackingException in project incubator-heron by apache.

the class PackingUtils method assertIsValidInstance.

/**
 * Verifies the Instance has enough RAM and that it can fit within the container limits.
 *
 * @param instanceResources The resources allocated to the instance
 * @throws PackingException if the instance is invalid
 */
private static void assertIsValidInstance(Resource instanceResources, ByteAmount minInstanceRam, Resource maxContainerResources, int paddingPercentage) throws PackingException {
    if (instanceResources.getRam().lessThan(minInstanceRam)) {
        throw new PackingException(String.format("Instance requires ram %s which is less than the minimum ram per instance of %s", instanceResources.getRam(), minInstanceRam));
    }
    ByteAmount instanceRam = instanceResources.getRam().increaseBy(paddingPercentage);
    if (instanceRam.greaterThan(maxContainerResources.getRam())) {
        throw new PackingException(String.format("This instance requires containers of at least %s ram. The current max container " + "size is %s", instanceRam, maxContainerResources.getRam()));
    }
    double instanceCpu = Math.round(PackingUtils.increaseBy(instanceResources.getCpu(), paddingPercentage));
    if (instanceCpu > maxContainerResources.getCpu()) {
        throw new PackingException(String.format("This instance requires containers with at least %s cpu cores. The current max container" + "size is %s cores", instanceCpu, maxContainerResources.getCpu()));
    }
    ByteAmount instanceDisk = instanceResources.getDisk().increaseBy(paddingPercentage);
    if (instanceDisk.greaterThan(maxContainerResources.getDisk())) {
        throw new PackingException(String.format("This instance requires containers of at least %s disk. The current max container" + "size is %s", instanceDisk, maxContainerResources.getDisk()));
    }
}
Also used : ByteAmount(com.twitter.heron.common.basics.ByteAmount) PackingException(com.twitter.heron.spi.packing.PackingException)

Example 4 with PackingException

use of com.twitter.heron.spi.packing.PackingException in project incubator-heron by apache.

the class PackingPlanBuilder method initContainers.

private void initContainers() {
    assertResourceSettings();
    Map<Integer, Container> newContainerMap = this.containers;
    HashMap<String, TreeSet<Integer>> newComponentIndexes = this.componentIndexes;
    TreeSet<Integer> newTaskIds = this.taskIds;
    if (newComponentIndexes == null) {
        newComponentIndexes = new HashMap<>();
    }
    if (newTaskIds == null) {
        newTaskIds = new TreeSet<>();
    }
    // if this is the first time called, initialize container map with empty or existing containers
    if (newContainerMap == null) {
        if (this.existingPacking == null) {
            newContainerMap = new HashMap<>();
            for (int containerId = 1; containerId <= numContainers; containerId++) {
                newContainerMap.put(containerId, new Container(containerId, this.maxContainerResource, this.requestedContainerPadding));
            }
        } else {
            try {
                newContainerMap = getContainers(this.existingPacking, this.requestedContainerPadding, newComponentIndexes, newTaskIds);
            } catch (ResourceExceededException e) {
                throw new PackingException("Could not initialize containers using existing packing plan", e);
            }
        }
    }
    if (this.numContainers > newContainerMap.size()) {
        List<Scorer<Container>> scorers = new ArrayList<>();
        scorers.add(new ContainerIdScorer());
        List<Container> sortedContainers = sortContainers(scorers, newContainerMap.values());
        int nextContainerId = sortedContainers.get(sortedContainers.size() - 1).getContainerId() + 1;
        Resource capacity = newContainerMap.get(sortedContainers.get(0).getContainerId()).getCapacity();
        for (int i = 0; i < numContainers - newContainerMap.size(); i++) {
            newContainerMap.put(nextContainerId, new Container(nextContainerId, capacity, this.requestedContainerPadding));
            nextContainerId++;
        }
    }
    this.containers = newContainerMap;
    this.componentIndexes = newComponentIndexes;
    this.taskIds = newTaskIds;
}
Also used : ResourceExceededException(com.twitter.heron.packing.ResourceExceededException) ArrayList(java.util.ArrayList) Resource(com.twitter.heron.spi.packing.Resource) TreeSet(java.util.TreeSet) PackingException(com.twitter.heron.spi.packing.PackingException)

Example 5 with PackingException

use of com.twitter.heron.spi.packing.PackingException in project incubator-heron by apache.

the class LauncherUtils method createPackingPlan.

/**
 * Returns a packing plan generated by configured packing class
 */
public PackingPlan createPackingPlan(final Config config, final Config runtime) throws PackingException {
    // Create an instance of the packing class
    String packingClass = Context.packingClass(config);
    IPacking packing;
    try {
        // create an instance of the packing class
        packing = ReflectionUtils.newInstance(packingClass);
    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
        throw new PackingException(String.format("Failed to instantiate packing instance using packing class %s", packingClass), e);
    }
    try {
        TopologyAPI.Topology topology = Runtime.topology(runtime);
        packing.initialize(config, topology);
        return packing.pack();
    } finally {
        SysUtils.closeIgnoringExceptions(packing);
    }
}
Also used : IPacking(com.twitter.heron.spi.packing.IPacking) PackingException(com.twitter.heron.spi.packing.PackingException) TopologyAPI(com.twitter.heron.api.generated.TopologyAPI)

Aggregations

PackingException (com.twitter.heron.spi.packing.PackingException)10 Config (com.twitter.heron.spi.common.Config)4 SchedulerStateManagerAdaptor (com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor)4 URI (java.net.URI)4 TopologyAPI (com.twitter.heron.api.generated.TopologyAPI)2 ByteAmount (com.twitter.heron.common.basics.ByteAmount)2 ResourceExceededException (com.twitter.heron.packing.ResourceExceededException)2 IPacking (com.twitter.heron.spi.packing.IPacking)2 PackingPlan (com.twitter.heron.spi.packing.PackingPlan)2 Resource (com.twitter.heron.spi.packing.Resource)2 ILauncher (com.twitter.heron.spi.scheduler.ILauncher)2 LauncherException (com.twitter.heron.spi.scheduler.LauncherException)2 IStateManager (com.twitter.heron.spi.statemgr.IStateManager)2 IUploader (com.twitter.heron.spi.uploader.IUploader)2 UploaderException (com.twitter.heron.spi.uploader.UploaderException)2 ArrayList (java.util.ArrayList)2 TreeSet (java.util.TreeSet)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2