Search in sources :

Example 16 with SchedulerStateManagerAdaptor

use of com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor in project heron by twitter.

the class RuntimeManagerRunner method restartTopologyHandler.

/**
   * Handler to restart a topology
   */
@VisibleForTesting
void restartTopologyHandler(String topologyName) throws TopologyRuntimeManagementException {
    Integer containerId = Context.topologyContainerId(config);
    Scheduler.RestartTopologyRequest restartTopologyRequest = Scheduler.RestartTopologyRequest.newBuilder().setTopologyName(topologyName).setContainerIndex(containerId).build();
    // i.e. TMasterLocation does not exist
    if (containerId == -1 || containerId == 0) {
        // get the instance of state manager to clean state
        SchedulerStateManagerAdaptor stateManager = Runtime.schedulerStateManagerAdaptor(runtime);
        Boolean result = stateManager.deleteTMasterLocation(topologyName);
        if (result == null || !result) {
            throw new TopologyRuntimeManagementException("Failed to clear TMaster location. Check whether TMaster set it correctly.");
        }
    }
    if (!schedulerClient.restartTopology(restartTopologyRequest)) {
        throw new TopologyRuntimeManagementException(String.format("Failed to restart topology '%s'", topologyName));
    }
    // Clean the connection when we are done.
    LOG.fine("Scheduler restarted topology successfully.");
}
Also used : Scheduler(com.twitter.heron.proto.scheduler.Scheduler) SchedulerStateManagerAdaptor(com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 17 with SchedulerStateManagerAdaptor

use of com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor 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 18 with SchedulerStateManagerAdaptor

use of com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor 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)

Example 19 with SchedulerStateManagerAdaptor

use of com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor in project heron by twitter.

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);
        // 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 20 with SchedulerStateManagerAdaptor

use of com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor in project heron by twitter.

the class SchedulerClientFactory method getSchedulerClient.

/**
   * Implementation of getSchedulerClient - Used to create objects
   * Currently it creates either HttpServiceSchedulerClient or LibrarySchedulerClient
   *
   * @return getSchedulerClient created. return null if failed to create ISchedulerClient instance
   */
public ISchedulerClient getSchedulerClient() throws SchedulerException {
    LOG.fine("Creating scheduler client");
    ISchedulerClient schedulerClient;
    if (Context.schedulerService(config)) {
        // get the instance of the state manager
        SchedulerStateManagerAdaptor statemgr = Runtime.schedulerStateManagerAdaptor(runtime);
        Scheduler.SchedulerLocation schedulerLocation = statemgr.getSchedulerLocation(Runtime.topologyName(runtime));
        if (schedulerLocation == null) {
            throw new SchedulerException("Failed to get scheduler location from state manager");
        }
        LOG.log(Level.FINE, "Scheduler is listening on location: {0} ", schedulerLocation.toString());
        schedulerClient = new HttpServiceSchedulerClient(config, runtime, schedulerLocation.getHttpEndpoint());
    } else {
        // create an instance of scheduler
        final IScheduler scheduler = LauncherUtils.getInstance().getSchedulerInstance(config, runtime);
        LOG.fine("Invoke scheduler as a library");
        schedulerClient = new LibrarySchedulerClient(config, runtime, scheduler);
    }
    return schedulerClient;
}
Also used : SchedulerException(com.twitter.heron.spi.scheduler.SchedulerException) Scheduler(com.twitter.heron.proto.scheduler.Scheduler) IScheduler(com.twitter.heron.spi.scheduler.IScheduler) IScheduler(com.twitter.heron.spi.scheduler.IScheduler) SchedulerStateManagerAdaptor(com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor)

Aggregations

SchedulerStateManagerAdaptor (com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor)34 Test (org.junit.Test)20 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Config (com.twitter.heron.spi.common.Config)13 PackingPlan (com.twitter.heron.spi.packing.PackingPlan)12 Scheduler (com.twitter.heron.proto.scheduler.Scheduler)8 ISchedulerClient (com.twitter.heron.scheduler.client.ISchedulerClient)6 ILauncher (com.twitter.heron.spi.scheduler.ILauncher)6 PackingPlans (com.twitter.heron.proto.system.PackingPlans)5 HeronTopology (com.twitter.heron.api.HeronTopology)4 TopologyAPI (com.twitter.heron.api.generated.TopologyAPI)4 IStateManager (com.twitter.heron.spi.statemgr.IStateManager)4 ExecutionEnvironment (com.twitter.heron.proto.system.ExecutionEnvironment)3 IScheduler (com.twitter.heron.spi.scheduler.IScheduler)3 HashSet (java.util.HashSet)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 RoundRobinPacking (com.twitter.heron.packing.roundrobin.RoundRobinPacking)2 LauncherUtils (com.twitter.heron.scheduler.utils.LauncherUtils)2 PackingPlanProtoDeserializer (com.twitter.heron.spi.packing.PackingPlanProtoDeserializer)2 IScalable (com.twitter.heron.spi.scheduler.IScalable)2