Search in sources :

Example 1 with FrameworkInfo

use of org.apache.mesos.v1.Protos.FrameworkInfo in project Singularity by HubSpot.

the class SingularityMesosSchedulerClient method subscribe.

/**
 * The first call to mesos, needed to setup connection properly and identify
 * a framework.
 *
 * @throws URISyntaxException if the URL provided was not a syntactically correct URL.
 */
public void subscribe(String mesosMasterURI, SingularityMesosScheduler scheduler) throws URISyntaxException {
    FrameworkInfo frameworkInfo = buildFrameworkInfo();
    if (mesosMasterURI == null || mesosMasterURI.contains("zk:")) {
        throw new IllegalArgumentException(String.format("Must use master address for http api (e.g. http://localhost:5050/api/v1/scheduler) was %s", mesosMasterURI));
    }
    if (openStream == null || openStream.isUnsubscribed()) {
        // Do we get here ever?
        if (subscriberThread != null) {
            subscriberThread.interrupt();
        }
        subscriberThread = new Thread() {

            public void run() {
                try {
                    connect(URI.create(mesosMasterURI), frameworkInfo, scheduler);
                } catch (RuntimeException | URISyntaxException e) {
                    LOG.error("Could not connect: ", e);
                    scheduler.onConnectException(e);
                }
            }
        };
        subscriberThread.start();
    }
}
Also used : FrameworkInfo(org.apache.mesos.v1.Protos.FrameworkInfo)

Example 2 with FrameworkInfo

use of org.apache.mesos.v1.Protos.FrameworkInfo in project Singularity by HubSpot.

the class SingularityMesosSchedulerClient method connect.

/**
 * Sets up the connection and is blocking in wait for calls from mesos
 * master.
 */
private void connect(URI mesosMasterURI, FrameworkInfo frameworkInfo, SingularityMesosScheduler scheduler) throws URISyntaxException {
    MesosClientBuilder<Call, Event> clientBuilder = ProtobufMesosClientBuilder.schedulerUsingProtos().mesosUri(mesosMasterURI).applicationUserAgentEntry(UserAgentEntries.userAgentEntryForMavenArtifact("com.hubspot.singularity", "SingularityService")).onSendEventBackpressureBuffer().onSendErrorRetry().onBackpressureBuffer(scheduler.getEventBufferSize(), () -> {
        String message = String.format("Overflow of event buffer (%s), singularity could not keep up!", scheduler.getEventBufferSize());
        scheduler.onUncaughtException(new EventBufferOverflowException(message));
    }, BackpressureOverflow.ON_OVERFLOW_ERROR);
    Call subscribeCall = Call.newBuilder().setType(Call.Type.SUBSCRIBE).setFrameworkId(frameworkInfo.getId()).setSubscribe(Call.Subscribe.newBuilder().setFrameworkInfo(frameworkInfo).build()).build();
    MesosClientBuilder<Call, Event> subscribe = clientBuilder.subscribe(subscribeCall);
    this.scheduler = scheduler;
    subscribe.processStream(unicastEvents -> {
        final Observable<Event> events = unicastEvents.share();
        events.filter(event -> event.getType() == Event.Type.ERROR).map(event -> event.getError().getMessage()).subscribe(scheduler::error, scheduler::onUncaughtException);
        events.filter(event -> event.getType() == Event.Type.FAILURE).map(Event::getFailure).subscribe(scheduler::failure, scheduler::onUncaughtException);
        events.filter(event -> event.getType() == Event.Type.HEARTBEAT).subscribe(scheduler::heartbeat, scheduler::onUncaughtException);
        events.filter(event -> event.getType() == Event.Type.INVERSE_OFFERS).map(event -> event.getInverseOffers().getInverseOffersList()).subscribe(scheduler::inverseOffers, scheduler::onUncaughtException);
        events.filter(event -> event.getType() == Event.Type.MESSAGE).map(Event::getMessage).subscribe(scheduler::message, scheduler::onUncaughtException);
        events.filter(event -> event.getType() == Event.Type.OFFERS).map(event -> event.getOffers().getOffersList()).subscribe(scheduler::resourceOffers, scheduler::onUncaughtException);
        events.filter(event -> event.getType() == Event.Type.RESCIND).map(event -> event.getRescind().getOfferId()).subscribe(scheduler::rescind, scheduler::onUncaughtException);
        events.filter(event -> event.getType() == Event.Type.RESCIND_INVERSE_OFFER).map(event -> event.getRescindInverseOffer().getInverseOfferId()).subscribe(scheduler::rescindInverseOffer, scheduler::onUncaughtException);
        events.filter(event -> event.getType() == Event.Type.SUBSCRIBED).map(Event::getSubscribed).subscribe(subscribed -> {
            this.frameworkId = subscribed.getFrameworkId();
            scheduler.subscribed(subscribed);
        }, scheduler::onSubscribeException);
        events.filter(event -> event.getType() == Event.Type.UPDATE).map(event -> event.getUpdate().getStatus()).filter(status -> {
            if (!status.hasAgentId() || !status.getAgentId().hasValue()) {
                LOG.warn("Filtering out status update without agentId {}", status);
                return false;
            } else {
                return true;
            }
        }).subscribe(scheduler::statusUpdate, scheduler::onUncaughtException);
        // This is the observable that is responsible for sending calls to mesos master.
        PublishSubject<Optional<SinkOperation<Call>>> p = PublishSubject.create();
        // toSerialised handles the fact that we can add calls on different threads.
        publisher = p.toSerialized();
        return publisher.onBackpressureBuffer();
    });
    MesosClient<Call, Event> client = clientBuilder.build();
    openStream = client.openStream();
    try {
        openStream.await();
    } catch (Throwable t) {
        if (Throwables.getCausalChain(t).stream().anyMatch(throwable -> throwable instanceof InterruptedException)) {
            LOG.warn("Observable interrupted, closed stream from mesos");
        } else {
            LOG.error("Observable was unexpectedly closed", t);
            scheduler.onUncaughtException(t);
        }
    }
}
Also used : Reconcile(org.apache.mesos.v1.scheduler.Protos.Call.Reconcile) Inject(com.google.inject.Inject) UIConfiguration(com.hubspot.singularity.config.UIConfiguration) PrematureChannelClosureException(io.netty.handler.codec.PrematureChannelClosureException) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) Offer(org.apache.mesos.v1.Protos.Offer) FrameworkID(org.apache.mesos.v1.Protos.FrameworkID) SinkOperation(com.hubspot.mesos.rx.java.SinkOperation) Type(org.apache.mesos.v1.scheduler.Protos.Call.Type) MesosClientBuilder(com.hubspot.mesos.rx.java.MesosClientBuilder) Filters(org.apache.mesos.v1.Protos.Filters) URI(java.net.URI) AwaitableSubscription(com.hubspot.mesos.rx.java.AwaitableSubscription) Acknowledge(org.apache.mesos.v1.scheduler.Protos.Call.Acknowledge) FrameworkInfo(org.apache.mesos.v1.Protos.FrameworkInfo) Accept(org.apache.mesos.v1.scheduler.Protos.Call.Accept) Builder(org.apache.mesos.v1.scheduler.Protos.Call.Builder) Message(org.apache.mesos.v1.scheduler.Protos.Call.Message) ExecutorID(org.apache.mesos.v1.Protos.ExecutorID) ByteString(com.google.protobuf.ByteString) Decline(org.apache.mesos.v1.scheduler.Protos.Call.Decline) List(java.util.List) Request(org.apache.mesos.v1.scheduler.Protos.Call.Request) ProtobufMesosClientBuilder(com.hubspot.mesos.rx.java.protobuf.ProtobufMesosClientBuilder) Optional(java.util.Optional) KillPolicy(org.apache.mesos.v1.Protos.KillPolicy) PublishSubject(rx.subjects.PublishSubject) Singleton(com.google.inject.Singleton) CompletableFuture(java.util.concurrent.CompletableFuture) Event(org.apache.mesos.v1.scheduler.Protos.Event) Observable(rx.Observable) SerializedSubject(rx.subjects.SerializedSubject) ConnectException(java.net.ConnectException) BackpressureOverflow(rx.BackpressureOverflow) ExecutorService(java.util.concurrent.ExecutorService) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) UiResource(com.hubspot.singularity.resources.ui.UiResource) Logger(org.slf4j.Logger) Throwables(com.google.common.base.Throwables) UserAgentEntries(com.hubspot.mesos.rx.java.util.UserAgentEntries) AgentID(org.apache.mesos.v1.Protos.AgentID) SinkOperations(com.hubspot.mesos.rx.java.SinkOperations) OfferID(org.apache.mesos.v1.Protos.OfferID) SingularityServiceUIModule(com.hubspot.singularity.resources.SingularityServiceUIModule) AtomicLong(java.util.concurrent.atomic.AtomicLong) Kill(org.apache.mesos.v1.scheduler.Protos.Call.Kill) SingularityManagedThreadPoolFactory(com.hubspot.singularity.SingularityManagedThreadPoolFactory) Shutdown(org.apache.mesos.v1.scheduler.Protos.Call.Shutdown) TaskID(org.apache.mesos.v1.Protos.TaskID) Call(org.apache.mesos.v1.scheduler.Protos.Call) Named(com.google.inject.name.Named) MesosClient(com.hubspot.mesos.rx.java.MesosClient) MesosConfiguration(com.hubspot.singularity.config.MesosConfiguration) Mesos4xxException(com.hubspot.mesos.rx.java.Mesos4xxException) Call(org.apache.mesos.v1.scheduler.Protos.Call) Optional(java.util.Optional) ByteString(com.google.protobuf.ByteString) Event(org.apache.mesos.v1.scheduler.Protos.Event)

Example 3 with FrameworkInfo

use of org.apache.mesos.v1.Protos.FrameworkInfo in project Singularity by HubSpot.

the class SingularityMesosSchedulerClient method subscribe.

/**
 * The first call to mesos, needed to setup connection properly and identify
 * a framework.
 *
 * @throws URISyntaxException if the URL provided was not a syntactically correct URL.
 */
public void subscribe(URI mesosMasterURI, SingularityMesosScheduler scheduler) throws URISyntaxException {
    FrameworkInfo frameworkInfo = buildFrameworkInfo();
    if (mesosMasterURI == null || mesosMasterURI.getScheme().contains("zk")) {
        throw new IllegalArgumentException(String.format("Must use master address for http api (e.g. http://localhost:5050/api/v1/scheduler) was %s", mesosMasterURI));
    }
    if (openStream == null || openStream.isUnsubscribed()) {
        // Do we get here ever?
        if (subscriberThread != null) {
            subscriberThread.interrupt();
        }
        subscriberThread = new Thread() {

            public void run() {
                try {
                    connect(mesosMasterURI, frameworkInfo, scheduler);
                } catch (RuntimeException | URISyntaxException e) {
                    if (!Throwables.getCausalChain(e).stream().anyMatch(t -> t instanceof InterruptedException)) {
                        LOG.error("Could not connect: ", e);
                        scheduler.onSubscribeException(e);
                    } else {
                        LOG.warn("Interruped stream from mesos on subscriber thread, closing");
                    }
                }
            }
        };
        subscriberThread.start();
    }
}
Also used : Reconcile(org.apache.mesos.v1.scheduler.Protos.Call.Reconcile) Inject(com.google.inject.Inject) UIConfiguration(com.hubspot.singularity.config.UIConfiguration) PrematureChannelClosureException(io.netty.handler.codec.PrematureChannelClosureException) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) Offer(org.apache.mesos.v1.Protos.Offer) FrameworkID(org.apache.mesos.v1.Protos.FrameworkID) SinkOperation(com.hubspot.mesos.rx.java.SinkOperation) Type(org.apache.mesos.v1.scheduler.Protos.Call.Type) MesosClientBuilder(com.hubspot.mesos.rx.java.MesosClientBuilder) Filters(org.apache.mesos.v1.Protos.Filters) URI(java.net.URI) AwaitableSubscription(com.hubspot.mesos.rx.java.AwaitableSubscription) Acknowledge(org.apache.mesos.v1.scheduler.Protos.Call.Acknowledge) FrameworkInfo(org.apache.mesos.v1.Protos.FrameworkInfo) Accept(org.apache.mesos.v1.scheduler.Protos.Call.Accept) Builder(org.apache.mesos.v1.scheduler.Protos.Call.Builder) Message(org.apache.mesos.v1.scheduler.Protos.Call.Message) ExecutorID(org.apache.mesos.v1.Protos.ExecutorID) ByteString(com.google.protobuf.ByteString) Decline(org.apache.mesos.v1.scheduler.Protos.Call.Decline) List(java.util.List) Request(org.apache.mesos.v1.scheduler.Protos.Call.Request) ProtobufMesosClientBuilder(com.hubspot.mesos.rx.java.protobuf.ProtobufMesosClientBuilder) Optional(java.util.Optional) KillPolicy(org.apache.mesos.v1.Protos.KillPolicy) PublishSubject(rx.subjects.PublishSubject) Singleton(com.google.inject.Singleton) CompletableFuture(java.util.concurrent.CompletableFuture) Event(org.apache.mesos.v1.scheduler.Protos.Event) Observable(rx.Observable) SerializedSubject(rx.subjects.SerializedSubject) ConnectException(java.net.ConnectException) BackpressureOverflow(rx.BackpressureOverflow) ExecutorService(java.util.concurrent.ExecutorService) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) UiResource(com.hubspot.singularity.resources.ui.UiResource) Logger(org.slf4j.Logger) Throwables(com.google.common.base.Throwables) UserAgentEntries(com.hubspot.mesos.rx.java.util.UserAgentEntries) AgentID(org.apache.mesos.v1.Protos.AgentID) SinkOperations(com.hubspot.mesos.rx.java.SinkOperations) OfferID(org.apache.mesos.v1.Protos.OfferID) SingularityServiceUIModule(com.hubspot.singularity.resources.SingularityServiceUIModule) AtomicLong(java.util.concurrent.atomic.AtomicLong) Kill(org.apache.mesos.v1.scheduler.Protos.Call.Kill) SingularityManagedThreadPoolFactory(com.hubspot.singularity.SingularityManagedThreadPoolFactory) Shutdown(org.apache.mesos.v1.scheduler.Protos.Call.Shutdown) TaskID(org.apache.mesos.v1.Protos.TaskID) Call(org.apache.mesos.v1.scheduler.Protos.Call) Named(com.google.inject.name.Named) MesosClient(com.hubspot.mesos.rx.java.MesosClient) MesosConfiguration(com.hubspot.singularity.config.MesosConfiguration) Mesos4xxException(com.hubspot.mesos.rx.java.Mesos4xxException) FrameworkInfo(org.apache.mesos.v1.Protos.FrameworkInfo)

Aggregations

FrameworkInfo (org.apache.mesos.v1.Protos.FrameworkInfo)3 Throwables (com.google.common.base.Throwables)2 Inject (com.google.inject.Inject)2 Singleton (com.google.inject.Singleton)2 Named (com.google.inject.name.Named)2 ByteString (com.google.protobuf.ByteString)2 AwaitableSubscription (com.hubspot.mesos.rx.java.AwaitableSubscription)2 Mesos4xxException (com.hubspot.mesos.rx.java.Mesos4xxException)2 MesosClient (com.hubspot.mesos.rx.java.MesosClient)2 MesosClientBuilder (com.hubspot.mesos.rx.java.MesosClientBuilder)2 SinkOperation (com.hubspot.mesos.rx.java.SinkOperation)2 SinkOperations (com.hubspot.mesos.rx.java.SinkOperations)2 ProtobufMesosClientBuilder (com.hubspot.mesos.rx.java.protobuf.ProtobufMesosClientBuilder)2 UserAgentEntries (com.hubspot.mesos.rx.java.util.UserAgentEntries)2 SingularityManagedThreadPoolFactory (com.hubspot.singularity.SingularityManagedThreadPoolFactory)2 MesosConfiguration (com.hubspot.singularity.config.MesosConfiguration)2 SingularityConfiguration (com.hubspot.singularity.config.SingularityConfiguration)2 UIConfiguration (com.hubspot.singularity.config.UIConfiguration)2 SingularityServiceUIModule (com.hubspot.singularity.resources.SingularityServiceUIModule)2 UiResource (com.hubspot.singularity.resources.ui.UiResource)2