Search in sources :

Example 1 with PackingPlanProtoDeserializer

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

the class RuntimeManagerRunner method buildNewPackingPlan.

@VisibleForTesting
PackingPlans.PackingPlan buildNewPackingPlan(PackingPlans.PackingPlan currentProtoPlan, Map<String, Integer> changeRequests, TopologyAPI.Topology topology) throws PackingException {
    PackingPlanProtoDeserializer deserializer = new PackingPlanProtoDeserializer();
    PackingPlanProtoSerializer serializer = new PackingPlanProtoSerializer();
    PackingPlan currentPackingPlan = deserializer.fromProto(currentProtoPlan);
    Map<String, Integer> componentCounts = currentPackingPlan.getComponentCounts();
    Map<String, Integer> componentChanges = parallelismDelta(componentCounts, changeRequests);
    // Create an instance of the packing class
    String repackingClass = Context.repackingClass(config);
    IRepacking packing;
    try {
        // create an instance of the packing class
        packing = ReflectionUtils.newInstance(repackingClass);
    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
        throw new IllegalArgumentException("Failed to instantiate packing instance: " + repackingClass, e);
    }
    LOG.info("Updating packing plan using " + repackingClass);
    try {
        packing.initialize(config, topology);
        PackingPlan packedPlan = packing.repack(currentPackingPlan, componentChanges);
        return serializer.toProto(packedPlan);
    } finally {
        SysUtils.closeIgnoringExceptions(packing);
    }
}
Also used : PackingPlanProtoDeserializer(com.twitter.heron.spi.packing.PackingPlanProtoDeserializer) PackingPlanProtoSerializer(com.twitter.heron.spi.packing.PackingPlanProtoSerializer) IRepacking(com.twitter.heron.spi.packing.IRepacking) PackingPlan(com.twitter.heron.spi.packing.PackingPlan) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with PackingPlanProtoDeserializer

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

the class RuntimeManagerRunner method updateTopologyHandler.

/**
   * Handler to update a topology
   */
@VisibleForTesting
void updateTopologyHandler(String topologyName, String newParallelism) throws TopologyRuntimeManagementException, PackingException, UpdateDryRunResponse {
    LOG.fine(String.format("updateTopologyHandler called for %s with %s", topologyName, newParallelism));
    SchedulerStateManagerAdaptor manager = Runtime.schedulerStateManagerAdaptor(runtime);
    TopologyAPI.Topology topology = manager.getTopology(topologyName);
    Map<String, Integer> changeRequests = parseNewParallelismParam(newParallelism);
    PackingPlans.PackingPlan currentPlan = manager.getPackingPlan(topologyName);
    if (!changeDetected(currentPlan, changeRequests)) {
        throw new TopologyRuntimeManagementException(String.format("The component parallelism request (%s) is the same as the " + "current topology parallelism. Not taking action.", newParallelism));
    }
    PackingPlans.PackingPlan proposedPlan = buildNewPackingPlan(currentPlan, changeRequests, topology);
    if (Context.dryRun(config)) {
        PackingPlanProtoDeserializer deserializer = new PackingPlanProtoDeserializer();
        PackingPlan oldPlan = deserializer.fromProto(currentPlan);
        PackingPlan newPlan = deserializer.fromProto(proposedPlan);
        throw new UpdateDryRunResponse(topology, config, newPlan, oldPlan, changeRequests);
    }
    Scheduler.UpdateTopologyRequest updateTopologyRequest = Scheduler.UpdateTopologyRequest.newBuilder().setCurrentPackingPlan(currentPlan).setProposedPackingPlan(proposedPlan).build();
    LOG.fine("Sending Updating topology request: " + updateTopologyRequest);
    if (!schedulerClient.updateTopology(updateTopologyRequest)) {
        throw new TopologyRuntimeManagementException(String.format("Failed to update topology with Scheduler, updateTopologyRequest=" + updateTopologyRequest));
    }
    // Clean the connection when we are done.
    LOG.fine("Scheduler updated topology successfully.");
}
Also used : UpdateDryRunResponse(com.twitter.heron.scheduler.dryrun.UpdateDryRunResponse) Scheduler(com.twitter.heron.proto.scheduler.Scheduler) PackingPlan(com.twitter.heron.spi.packing.PackingPlan) SchedulerStateManagerAdaptor(com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor) TopologyAPI(com.twitter.heron.api.generated.TopologyAPI) PackingPlans(com.twitter.heron.proto.system.PackingPlans) PackingPlanProtoDeserializer(com.twitter.heron.spi.packing.PackingPlanProtoDeserializer) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with PackingPlanProtoDeserializer

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

the class RuntimeManagerRunner method updateTopologyComponentParallelism.

@VisibleForTesting
void updateTopologyComponentParallelism(String topologyName, String newParallelism) throws TopologyRuntimeManagementException, PackingException, UpdateDryRunResponse {
    LOG.fine(String.format("updateTopologyHandler called for %s with %s", topologyName, newParallelism));
    SchedulerStateManagerAdaptor manager = Runtime.schedulerStateManagerAdaptor(runtime);
    TopologyAPI.Topology topology = manager.getTopology(topologyName);
    Map<String, Integer> changeRequests = parseNewParallelismParam(newParallelism);
    PackingPlans.PackingPlan currentPlan = manager.getPackingPlan(topologyName);
    if (!changeDetected(currentPlan, changeRequests)) {
        throw new TopologyRuntimeManagementException(String.format("The component parallelism request (%s) is the same as the " + "current topology parallelism. Not taking action.", newParallelism));
    }
    PackingPlans.PackingPlan proposedPlan = buildNewPackingPlan(currentPlan, changeRequests, topology);
    if (Context.dryRun(config)) {
        PackingPlanProtoDeserializer deserializer = new PackingPlanProtoDeserializer();
        PackingPlan oldPlan = deserializer.fromProto(currentPlan);
        PackingPlan newPlan = deserializer.fromProto(proposedPlan);
        throw new UpdateDryRunResponse(topology, config, newPlan, oldPlan, changeRequests);
    }
    Scheduler.UpdateTopologyRequest updateTopologyRequest = Scheduler.UpdateTopologyRequest.newBuilder().setCurrentPackingPlan(currentPlan).setProposedPackingPlan(proposedPlan).build();
    LOG.fine("Sending Updating topology request: " + updateTopologyRequest);
    if (!schedulerClient.updateTopology(updateTopologyRequest)) {
        throw new TopologyRuntimeManagementException("Failed to update topology with Scheduler, updateTopologyRequest=" + updateTopologyRequest + "The topology can be in a strange stage. " + "Please check carefully or redeploy the topology !!");
    }
}
Also used : UpdateDryRunResponse(com.twitter.heron.scheduler.dryrun.UpdateDryRunResponse) Scheduler(com.twitter.heron.proto.scheduler.Scheduler) PackingPlan(com.twitter.heron.spi.packing.PackingPlan) SchedulerStateManagerAdaptor(com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor) TopologyAPI(com.twitter.heron.api.generated.TopologyAPI) PackingPlans(com.twitter.heron.proto.system.PackingPlans) PackingPlanProtoDeserializer(com.twitter.heron.spi.packing.PackingPlanProtoDeserializer) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with PackingPlanProtoDeserializer

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

the class RuntimeManagerRunner method buildNewPackingPlan.

@VisibleForTesting
PackingPlans.PackingPlan buildNewPackingPlan(PackingPlans.PackingPlan currentProtoPlan, Map<String, Integer> changeRequests, TopologyAPI.Topology topology) throws PackingException {
    PackingPlanProtoDeserializer deserializer = new PackingPlanProtoDeserializer();
    PackingPlanProtoSerializer serializer = new PackingPlanProtoSerializer();
    PackingPlan currentPackingPlan = deserializer.fromProto(currentProtoPlan);
    Map<String, Integer> componentCounts = currentPackingPlan.getComponentCounts();
    Map<String, Integer> componentChanges = parallelismDelta(componentCounts, changeRequests);
    // Create an instance of the packing class
    String repackingClass = Context.repackingClass(config);
    IRepacking packing;
    try {
        // create an instance of the packing class
        packing = ReflectionUtils.newInstance(repackingClass);
    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
        throw new IllegalArgumentException("Failed to instantiate packing instance: " + repackingClass, e);
    }
    LOG.info("Updating packing plan using " + repackingClass);
    try {
        packing.initialize(config, topology);
        PackingPlan packedPlan = packing.repack(currentPackingPlan, componentChanges);
        return serializer.toProto(packedPlan);
    } finally {
        SysUtils.closeIgnoringExceptions(packing);
    }
}
Also used : PackingPlanProtoDeserializer(com.twitter.heron.spi.packing.PackingPlanProtoDeserializer) PackingPlanProtoSerializer(com.twitter.heron.spi.packing.PackingPlanProtoSerializer) IRepacking(com.twitter.heron.spi.packing.IRepacking) PackingPlan(com.twitter.heron.spi.packing.PackingPlan) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with PackingPlanProtoDeserializer

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

the class SchedulerMain method runScheduler.

/**
   * Run the scheduler.
   * It is a blocking call, and it will return in 2 cases:
   * 1. The topology is requested to kill
   * 2. Unexpected exceptions happen
   *
   * @return true if scheduled successfully
   */
public boolean runScheduler() {
    IScheduler scheduler = null;
    String statemgrClass = Context.stateManagerClass(config);
    IStateManager statemgr;
    try {
        // create an instance of state manager
        statemgr = ReflectionUtils.newInstance(statemgrClass);
    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
        LOG.log(Level.SEVERE, "Failed to instantiate instances using config: " + config, e);
        return false;
    }
    SchedulerServer server = null;
    boolean isSuccessful = false;
    // Put it in a try block so that we can always clean resources
    try {
        // initialize the state manager
        statemgr.initialize(config);
        // TODO(mfu): timeout should read from config
        SchedulerStateManagerAdaptor adaptor = new SchedulerStateManagerAdaptor(statemgr, 5000);
        // get a packed plan and schedule it
        PackingPlans.PackingPlan serializedPackingPlan = adaptor.getPackingPlan(topology.getName());
        if (serializedPackingPlan == null) {
            LOG.log(Level.SEVERE, "Failed to fetch PackingPlan for topology:{0} from the state manager", topology.getName());
            return false;
        }
        LOG.log(Level.INFO, "Packing plan fetched from state: {0}", serializedPackingPlan);
        PackingPlan packedPlan = new PackingPlanProtoDeserializer().fromProto(serializedPackingPlan);
        // build the runtime config
        LauncherUtils launcherUtils = LauncherUtils.getInstance();
        Config runtime = Config.newBuilder().putAll(launcherUtils.createPrimaryRuntime(topology)).putAll(launcherUtils.createAdaptorRuntime(adaptor)).put(Key.SCHEDULER_SHUTDOWN, getShutdown()).put(Key.SCHEDULER_PROPERTIES, properties).build();
        Config ytruntime = launcherUtils.createConfigWithPackingDetails(runtime, packedPlan);
        // invoke scheduler
        scheduler = launcherUtils.getSchedulerInstance(config, ytruntime);
        if (scheduler == null) {
            return false;
        }
        isSuccessful = scheduler.onSchedule(packedPlan);
        if (!isSuccessful) {
            LOG.severe("Failed to schedule topology");
            return false;
        }
        // Failures in server initialization throw exceptions
        // get the scheduler server endpoint for receiving requests
        server = getServer(ytruntime, scheduler, schedulerServerPort);
        // start the server to manage runtime requests
        server.start();
        // write the scheduler location to state manager
        // Make sure it happens after IScheduler.onScheduler
        isSuccessful = SchedulerUtils.setSchedulerLocation(runtime, String.format("%s:%d", server.getHost(), server.getPort()), scheduler);
        if (isSuccessful) {
            // wait until kill request or some interrupt occurs if the scheduler starts successfully
            LOG.info("Waiting for termination... ");
            Runtime.schedulerShutdown(ytruntime).await();
        }
    } catch (IOException e) {
        LOG.log(Level.SEVERE, "Failed to start server", e);
        return false;
    } finally {
        // Clean the resources
        if (server != null) {
            server.stop();
        }
        // 4. Close the resources
        SysUtils.closeIgnoringExceptions(scheduler);
        SysUtils.closeIgnoringExceptions(statemgr);
    }
    return isSuccessful;
}
Also used : IStateManager(com.twitter.heron.spi.statemgr.IStateManager) Config(com.twitter.heron.spi.common.Config) SystemConfig(com.twitter.heron.common.config.SystemConfig) PackingPlan(com.twitter.heron.spi.packing.PackingPlan) IOException(java.io.IOException) LauncherUtils(com.twitter.heron.scheduler.utils.LauncherUtils) SchedulerStateManagerAdaptor(com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor) PackingPlans(com.twitter.heron.proto.system.PackingPlans) PackingPlanProtoDeserializer(com.twitter.heron.spi.packing.PackingPlanProtoDeserializer) SchedulerServer(com.twitter.heron.scheduler.server.SchedulerServer) IScheduler(com.twitter.heron.spi.scheduler.IScheduler)

Aggregations

PackingPlanProtoDeserializer (com.twitter.heron.spi.packing.PackingPlanProtoDeserializer)9 PackingPlan (com.twitter.heron.spi.packing.PackingPlan)8 PackingPlans (com.twitter.heron.proto.system.PackingPlans)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 SchedulerStateManagerAdaptor (com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor)4 TopologyAPI (com.twitter.heron.api.generated.TopologyAPI)2 SystemConfig (com.twitter.heron.common.config.SystemConfig)2 Scheduler (com.twitter.heron.proto.scheduler.Scheduler)2 UpdateDryRunResponse (com.twitter.heron.scheduler.dryrun.UpdateDryRunResponse)2 SchedulerServer (com.twitter.heron.scheduler.server.SchedulerServer)2 LauncherUtils (com.twitter.heron.scheduler.utils.LauncherUtils)2 Config (com.twitter.heron.spi.common.Config)2 IRepacking (com.twitter.heron.spi.packing.IRepacking)2 PackingPlanProtoSerializer (com.twitter.heron.spi.packing.PackingPlanProtoSerializer)2 IScheduler (com.twitter.heron.spi.scheduler.IScheduler)2 IStateManager (com.twitter.heron.spi.statemgr.IStateManager)2 IOException (java.io.IOException)2