scala.concurrent.Promise Scala Examples
The following examples show how to use scala.concurrent.Promise.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: TimeBoundObserver.scala From daml with Apache License 2.0 | 6 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.testing import com.daml.timer.Delayed import io.grpc.Context import io.grpc.stub.StreamObserver import scala.concurrent.duration.FiniteDuration import scala.concurrent.{ExecutionContext, Future, Promise} final class TimeBoundObserver[T](duration: FiniteDuration)( implicit executionContext: ExecutionContext) extends StreamObserver[T] { private val promise = Promise[Vector[T]] private val buffer = Vector.newBuilder[T] Delayed.by(duration)(onCompleted()) def result: Future[Vector[T]] = promise.future override def onNext(value: T): Unit = { buffer += value } override def onError(t: Throwable): Unit = { val _ = promise.tryFailure(t) } override def onCompleted(): Unit = { val _succeeded = promise.trySuccess(buffer.result()) val _cancelled = Context.current().withCancellation().cancel(null) } }
Example 2
Source File: WaitForCompletionsObserver.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.testing import java.util.concurrent.atomic.AtomicInteger import com.daml.ledger.api.v1.command_completion_service.CompletionStreamResponse import io.grpc.Context import io.grpc.stub.StreamObserver import scala.concurrent.{Future, Promise} object WaitForCompletionsObserver { def apply(n: Int)(attach: StreamObserver[CompletionStreamResponse] => Unit): Future[Unit] = { if (n < 1) { Future.failed(new IllegalArgumentException( s"Invalid argument $n, `WaitForCompletionsObserver` requires a strictly positive integer as an argument")) } else { val observer = new WaitForCompletionsObserver(n) attach(observer) observer.result } } } final class WaitForCompletionsObserver private (expectedCompletions: Int) extends StreamObserver[CompletionStreamResponse] { private val promise = Promise[Unit] private val counter = new AtomicInteger(0) val result: Future[Unit] = promise.future override def onNext(v: CompletionStreamResponse): Unit = { val total = counter.addAndGet(v.completions.size) if (total >= expectedCompletions) { val _1 = promise.trySuccess(()) val _2 = Context.current().withCancellation().cancel(null) } } override def onError(throwable: Throwable): Unit = { val _ = promise.tryFailure(throwable) } override def onCompleted(): Unit = { val _ = promise.tryFailure(new RuntimeException("no more completions")) } }
Example 3
Source File: SizeBoundObserver.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.testing import io.grpc.Context import io.grpc.stub.StreamObserver import scala.concurrent.{Future, Promise} private[testing] final class SizeBoundObserver[A](cap: Int, p: A => Boolean) extends StreamObserver[A] { private val promise = Promise[Vector[A]]() private val items = Vector.newBuilder[A] private var counter = 0 val result: Future[Vector[A]] = promise.future // Since builders don't guarantee to return a reliable size when asked // we rely on a simple mutable variable and synchronize on the promise override def onNext(value: A): Unit = promise.synchronized { if (p(value)) { items += value counter += 1 } if (counter == cap) { promise.trySuccess(items.result()) val _ = Context.current().withCancellation().cancel(null) } } override def onError(t: Throwable): Unit = promise.synchronized { val _ = promise.tryFailure(t) } override def onCompleted(): Unit = promise.synchronized { val _ = promise.trySuccess(items.result()) } }
Example 4
Source File: InfiniteRetries.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.sandbox.perf import akka.actor.ActorSystem import scala.concurrent.duration.{DurationInt, FiniteDuration} import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.{Failure, Success} trait InfiniteRetries { protected def retry[T](action: => Future[T], delay: FiniteDuration = 10.millis)( implicit system: ActorSystem): Future[T] = { implicit val ec: ExecutionContext = system.dispatcher action.transformWith { case Success(v) => Future.successful(v) case Failure(t) => val p = Promise[T]() system.scheduler.scheduleOnce( delay, () => retry[T](action, delay).onComplete { case Success(s) => p.success(s) case Failure(throwable) => p.failure(throwable) } ) p.future } } }
Example 5
Source File: ExtractMaterializedValue.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.util.akkastreams import akka.stream.scaladsl.Flow import akka.stream.{Attributes, FlowShape, Inlet, Outlet} import akka.stream.stage.{GraphStageLogic, GraphStageWithMaterializedValue, InHandler, OutHandler} import scala.concurrent.{Future, Promise} class ExtractMaterializedValue[T, Mat](toMaterialized: T => Option[Mat]) extends GraphStageWithMaterializedValue[FlowShape[T, T], Future[Mat]] { val inlet: Inlet[T] = Inlet[T]("in") val outlet: Outlet[T] = Outlet[T]("out") override def createLogicAndMaterializedValue( inheritedAttributes: Attributes): (GraphStageLogic, Future[Mat]) = { val promise = Promise[Mat]() val logic = new GraphStageLogic(shape) { setHandler( inlet, new InHandler { override def onPush(): Unit = { val input = grab(inlet) push(outlet, input) toMaterialized(input).foreach { materialized => promise.trySuccess(materialized) setSimplerHandler() } } private def setSimplerHandler(): Unit = { setHandler(inlet, new InHandler { override def onPush(): Unit = push(outlet, grab(inlet)) }) } override def onUpstreamFailure(ex: Throwable): Unit = { promise.tryFailure(ex) super.onUpstreamFailure(ex) } override def onUpstreamFinish(): Unit = { promise.tryFailure( new RuntimeException("Upstream completed before matching element arrived.")) super.onUpstreamFinish() } } ) setHandler( outlet, new OutHandler { override def onPull(): Unit = pull(inlet) override def onDownstreamFinish(cause: Throwable): Unit = { promise.tryFailure( new RuntimeException("Downstream completed before matching element arrived.")) super.onDownstreamFinish(cause) } } ) } logic -> promise.future } override def shape: FlowShape[T, T] = FlowShape(inlet, outlet) } object ExtractMaterializedValue { def apply[T, Mat](toOutputOrMaterialized: T => Option[Mat]): Flow[T, T, Future[Mat]] = Flow.fromGraph(new ExtractMaterializedValue[T, Mat](toOutputOrMaterialized)) }
Example 6
Source File: LedgerApiServer.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.apiserver import akka.actor.ActorSystem import akka.stream.Materializer import com.daml.logging.{ContextualizedLogger, LoggingContext} import com.daml.metrics.Metrics import com.daml.ports.Port import com.daml.resources.{Resource, ResourceOwner} import io.grpc.ServerInterceptor import io.netty.handler.ssl.SslContext import scala.concurrent.{ExecutionContext, Future, Promise} final class LedgerApiServer( apiServicesOwner: ResourceOwner[ApiServices], desiredPort: Port, maxInboundMessageSize: Int, address: Option[String], sslContext: Option[SslContext] = None, interceptors: List[ServerInterceptor] = List.empty, metrics: Metrics, )(implicit actorSystem: ActorSystem, materializer: Materializer, logCtx: LoggingContext) extends ResourceOwner[ApiServer] { private val logger = ContextualizedLogger.get(this.getClass) override def acquire()(implicit executionContext: ExecutionContext): Resource[ApiServer] = { val servicesClosedPromise = Promise[Unit]() for { eventLoopGroups <- new ServerEventLoopGroups.Owner( actorSystem.name, workerParallelism = sys.runtime.availableProcessors(), bossParallelism = 1, ).acquire() apiServicesResource = apiServicesOwner.acquire() apiServices <- apiServicesResource server <- new GrpcServerOwner( address, desiredPort, maxInboundMessageSize, sslContext, interceptors, metrics, eventLoopGroups, apiServices.services, ).acquire() // Notify the caller that the services have been closed, so a reset request can complete // without blocking on the server terminating. _ <- Resource(Future.successful(()))(_ => apiServicesResource.release().map(_ => servicesClosedPromise.success(()))) } yield { val host = address.getOrElse("localhost") val actualPort = server.getPort val transportMedium = if (sslContext.isDefined) "TLS" else "plain text" logger.info(s"Listening on $host:$actualPort over $transportMedium.") new ApiServer { override val port: Port = Port(server.getPort) override def servicesClosed(): Future[Unit] = servicesClosedPromise.future } } } }
Example 7
Source File: EventLoopGroupOwner.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.apiserver import java.util.UUID import java.util.concurrent.TimeUnit.MILLISECONDS import com.daml.resources.{Resource, ResourceOwner} import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.nio.{NioServerSocketChannel, NioSocketChannel} import io.netty.channel.{Channel, EventLoopGroup, ServerChannel} import io.netty.util.concurrent.DefaultThreadFactory import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.Try final class EventLoopGroupOwner(threadPoolName: String, parallelism: Int) extends ResourceOwner[EventLoopGroup] { override def acquire()(implicit executionContext: ExecutionContext): Resource[EventLoopGroup] = Resource( Future(new NioEventLoopGroup( parallelism, new DefaultThreadFactory(s"$threadPoolName-grpc-event-loop-${UUID.randomUUID()}", true))))( group => { val promise = Promise[Unit]() val future = group.shutdownGracefully(0, 0, MILLISECONDS) future.addListener((f: io.netty.util.concurrent.Future[_]) => promise.complete(Try(f.get).map(_ => ()))) promise.future } ) } object EventLoopGroupOwner { val clientChannelType: Class[_ <: Channel] = classOf[NioSocketChannel] val serverChannelType: Class[_ <: ServerChannel] = classOf[NioServerSocketChannel] }
Example 8
Source File: HandleOfferResult.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.apiserver.services.tracking import akka.stream.QueueOfferResult import com.daml.platform.server.api.ApiException import com.google.rpc.status.Status import io.grpc.{Status => GrpcStatus} import scala.concurrent.Promise import scala.util.{Failure, Success, Try} private[tracking] object HandleOfferResult { val toGrpcStatus: PartialFunction[Try[QueueOfferResult], Option[GrpcStatus]] = { case Failure(t) => t match { case i: IllegalStateException if i.getMessage == "You have to wait for previous offer to be resolved to send another request" => Some( GrpcStatus.RESOURCE_EXHAUSTED .withDescription("Ingress buffer is full")) case _ => Some( GrpcStatus.ABORTED .withDescription(s"Failure: ${t.getClass.getSimpleName}: ${t.getMessage}") .withCause(t)) } case Success(QueueOfferResult.Failure(t)) => Some( GrpcStatus.ABORTED .withDescription(s"Failed to enqueue: ${t.getClass.getSimpleName}: ${t.getMessage}") .withCause(t)) case Success(QueueOfferResult.Dropped) => Some( GrpcStatus.RESOURCE_EXHAUSTED .withDescription("Ingress buffer is full")) case Success(QueueOfferResult.QueueClosed) => Some(GrpcStatus.ABORTED.withDescription("Queue closed")) case Success(QueueOfferResult.Enqueued) => None // Promise will be completed downstream. } def toStatusMessage: PartialFunction[Try[QueueOfferResult], Status] = toGrpcStatus.andThen(_.fold(Status())(e => Status(e.getCode.value(), e.getDescription))) def completePromise(promise: Promise[_]): PartialFunction[Try[QueueOfferResult], Unit] = toGrpcStatus.andThen(_.foreach(s => promise.tryFailure(new ApiException(s)))) }
Example 9
Source File: ExpiringStreamServiceCallAuthTests.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.sandbox.auth import java.time.Duration import com.daml.grpc.{GrpcException, GrpcStatus} import com.daml.platform.sandbox.services.SubmitAndWaitDummyCommand import com.daml.platform.testing.StreamConsumer import com.daml.timer.Delayed import io.grpc.Status import io.grpc.stub.StreamObserver import scala.concurrent.duration.DurationInt import scala.concurrent.{Future, Promise} import scala.util.control.NonFatal trait ExpiringStreamServiceCallAuthTests[T] extends ReadOnlyServiceCallAuthTests with SubmitAndWaitDummyCommand { protected def stream: Option[String] => StreamObserver[T] => Unit private def expectExpiration(token: String): Future[Unit] = { val promise = Promise[Unit]() stream(Option(token))(new StreamObserver[T] { @volatile private[this] var gotSomething = false def onNext(value: T): Unit = { gotSomething = true } def onError(t: Throwable): Unit = { t match { case GrpcException(GrpcStatus(Status.Code.PERMISSION_DENIED, _), _) if gotSomething => val _ = promise.trySuccess(()) case NonFatal(e) => val _ = promise.tryFailure(e) } } def onCompleted(): Unit = { val _ = promise.tryFailure(new RuntimeException("stream completed before token expiration")) } }) promise.future } private def canActAsMainActorExpiresInFiveSeconds = toHeader(expiringIn(Duration.ofSeconds(5), readWriteToken(mainActor))) private def canReadAsMainActorExpiresInFiveSeconds = toHeader(expiringIn(Duration.ofSeconds(5), readOnlyToken(mainActor))) it should "break a stream in flight upon read-only token expiration" in { val _ = Delayed.Future.by(10.seconds)(submitAndWait()) expectExpiration(canReadAsMainActorExpiresInFiveSeconds).map(_ => succeed) } it should "break a stream in flight upon read/write token expiration" in { val _ = Delayed.Future.by(10.seconds)(submitAndWait()) expectExpiration(canActAsMainActorExpiresInFiveSeconds).map(_ => succeed) } override def serviceCallWithToken(token: Option[String]): Future[Any] = submitAndWait().flatMap(_ => new StreamConsumer[T](stream(token)).first()) }
Example 10
Source File: FutureTimeouts.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.akkastreams import akka.actor.ActorSystem import com.daml.dec.DirectExecutionContext import org.scalatest.{Assertion, AsyncWordSpec} import scala.concurrent.{Future, Promise, TimeoutException} import scala.concurrent.duration.FiniteDuration import scala.util.Try import scala.util.control.NoStackTrace trait FutureTimeouts { self: AsyncWordSpec => protected def system: ActorSystem protected def expectTimeout(f: Future[Any], duration: FiniteDuration): Future[Assertion] = { val promise: Promise[Any] = Promise[Any]() val cancellable = system.scheduler.scheduleOnce(duration, { () => promise.failure( new TimeoutException(s"Future timed out after $duration as expected.") with NoStackTrace) () })(system.dispatcher) f.onComplete((_: Try[Any]) => cancellable.cancel())(DirectExecutionContext) recoverToSucceededIf[TimeoutException]( Future.firstCompletedOf[Any](List[Future[Any]](f, promise.future))(DirectExecutionContext)) } }
Example 11
Source File: MultiFixtureBase.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.ledger.api.testing.utils import java.util.concurrent.{Executors, ScheduledExecutorService, TimeUnit} import com.daml.dec.DirectExecutionContext import org.scalatest._ import org.scalatest.concurrent.{AsyncTimeLimitedTests, ScaledTimeSpans} import org.scalatest.exceptions.TestCanceledException import org.scalatest.time.Span import scala.collection.immutable.Iterable import scala.concurrent.duration.DurationInt import scala.concurrent.{Future, Promise, TimeoutException} import scala.util.control.{NoStackTrace, NonFatal} trait MultiFixtureBase[FixtureId, TestContext] extends Assertions with BeforeAndAfterAll with ScaledTimeSpans with AsyncTimeLimitedTests { self: AsyncTestSuite => private var es: ScheduledExecutorService = _ override protected def beforeAll(): Unit = { super.beforeAll() es = Executors.newScheduledThreadPool(1) } override protected def afterAll(): Unit = { es.shutdownNow() super.afterAll() } protected class TestFixture(val id: FixtureId, createContext: () => TestContext) { def context(): TestContext = createContext() } def timeLimit: Span = scaled(30.seconds) object TestFixture { def apply(id: FixtureId, createContext: () => TestContext): TestFixture = new TestFixture(id, createContext) def unapply(testFixture: TestFixture): Option[(FixtureId, TestContext)] = Some((testFixture.id, testFixture.context())) } protected def fixtures: Iterable[TestFixture] protected def allFixtures(runTest: TestContext => Future[Assertion]): Future[Assertion] = forAllFixtures(fixture => runTest(fixture.context)) protected def forAllFixtures(runTest: TestFixture => Future[Assertion]): Future[Assertion] = { forAllMatchingFixtures { case f => runTest(f) } } protected def forAllMatchingFixtures( runTest: PartialFunction[TestFixture, Future[Assertion]]): Future[Assertion] = { if (parallelExecution) { val results = fixtures.map( fixture => if (runTest.isDefinedAt(fixture)) runTestAgainstFixture(fixture, runTest) else Future.successful(succeed)) Future.sequence(results).map(foldAssertions) } else { fixtures.foldLeft(Future.successful(succeed)) { case (resultSoFar, thisFixture) => resultSoFar.flatMap { case Succeeded => runTestAgainstFixture(thisFixture, runTest) case other => Future.successful(other) } } } } }
Example 12
Source File: BufferingObserver.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.grpc.adapter.utils import java.util.concurrent.ConcurrentLinkedQueue import java.util.concurrent.atomic.AtomicInteger import io.grpc.stub.StreamObserver import scala.concurrent.Promise class BufferingObserver[T](limit: Option[Int] = None) extends StreamObserver[T] { private val promise = Promise[Vector[T]]() val buffer = new ConcurrentLinkedQueue[T]() val size = new AtomicInteger(0) def resultsF = promise.future override def onError(t: Throwable): Unit = promise.failure(t) override def onCompleted(): Unit = { val vec = Vector.newBuilder[T] buffer.forEach((e) => vec += e) promise.trySuccess(vec.result()) () } override def onNext(value: T): Unit = { size.updateAndGet(curr => { if (limit.fold(false)(_ <= curr)) { onCompleted() curr } else { buffer.add(value) curr + 1 } }) () } }
Example 13
Source File: ServerAdapter.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.grpc.adapter.server.akka import akka.stream.scaladsl.Sink import com.daml.grpc.adapter.ExecutionSequencerFactory import com.daml.grpc.adapter.server.rs.ServerSubscriber import io.grpc.stub.{ServerCallStreamObserver, StreamObserver} import scala.concurrent.{Future, Promise} object ServerAdapter { def toSink[Resp](streamObserver: StreamObserver[Resp])( implicit executionSequencerFactory: ExecutionSequencerFactory): Sink[Resp, Future[Unit]] = { val subscriber = new ServerSubscriber[Resp]( streamObserver.asInstanceOf[ServerCallStreamObserver[Resp]], executionSequencerFactory.getExecutionSequencer) Sink .fromSubscriber(subscriber) .mapMaterializedValue(_ => { val promise = Promise[Unit]() subscriber.completionFuture.handle[Unit]((_, throwable) => { if (throwable == null) promise.success(()) else promise.failure(throwable) () }) promise.future }) } }
Example 14
Source File: ScalaUtil.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.ledger.client.binding.util import java.util.concurrent.{ScheduledExecutorService, ScheduledFuture, TimeUnit} import com.typesafe.scalalogging.LazyLogging import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future, Promise, TimeoutException} object ScalaUtil { implicit class FutureOps[T](val future: Future[T]) extends LazyLogging { def timeout( name: String, failTimeout: FiniteDuration = 1.minute, warnTimeout: FiniteDuration = 30.seconds)( implicit ec: ExecutionContext, scheduler: ScheduledExecutorService): Future[T] = { val promise = Promise[T] @SuppressWarnings(Array("org.wartremover.warts.JavaSerializable")) val warningTask = schedule(warnTimeout) { logger.warn("Function {} takes more than {}", name, warnTimeout) } val errorTask = schedule(failTimeout) { val error = new TimeoutException(s"Function call $name took more than $failTimeout") promise.tryFailure(error) () } future.onComplete { outcome => warningTask.cancel(false) errorTask.cancel(false) promise.tryComplete(outcome) } promise.future } private def schedule(timeout: FiniteDuration)(f: => Unit)( implicit scheduler: ScheduledExecutorService): ScheduledFuture[_] = { val runnable = new Runnable { override def run(): Unit = f } scheduler.schedule(runnable, timeout.toMillis, TimeUnit.MILLISECONDS) } def timeoutWithDefaultWarn(name: String, failTimeout: FiniteDuration)( implicit ec: ExecutionContext, scheduler: ScheduledExecutorService): Future[T] = timeout(name, failTimeout, 10.seconds) } }
Example 15
Source File: ScalaUtilIT.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.ledger.client.binding.util import java.util.concurrent.{Executors, ScheduledExecutorService} import com.daml.ledger.client.binding.util.ScalaUtil.FutureOps import org.scalatest.concurrent.AsyncTimeLimitedTests import org.scalatest.time.Span import org.scalatest.time.SpanSugar._ import org.scalatest.{AsyncWordSpec, BeforeAndAfterAll, Matchers} import scala.concurrent.{Future, Promise, TimeoutException} class ScalaUtilIT extends AsyncWordSpec with AsyncTimeLimitedTests with Matchers with BeforeAndAfterAll { implicit val scheduler: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor() override def afterAll(): Unit = { scheduler.shutdownNow() super.afterAll() } "FutureOps" can { "future with timeout" should { "fail Future with TimoutException after specified duration" in { val promise = Promise[Unit]() // never completes val future = promise.future.timeout("name", 1000.millis, 100.millis) recoverToSucceededIf[TimeoutException](future) } "be able to complete within specified duration" in { val future = Future { "result" }.timeoutWithDefaultWarn("name", 1.second) future.map(_ shouldBe "result") } } } override lazy val timeLimit: Span = 10.seconds }
Example 16
Source File: ResettableResourceOwner.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.resources import java.util.concurrent.atomic.AtomicReference import com.daml.resources.ResettableResourceOwner._ import scala.annotation.tailrec import scala.concurrent.{ExecutionContext, Future, Promise} class ResettableResourceOwner[A, ResetValue] private ( initialValue: ResetValue, owner: Reset => ResetValue => ResourceOwner[A], resetOperation: A => Future[ResetValue], ) extends ResourceOwner[A] { override def acquire()(implicit executionContext: ExecutionContext): Resource[A] = new Resource[A] { private val resettableOwner: ResetValue => ResourceOwner[A] = owner(reset _) @volatile private var resource = resettableOwner(initialValue).acquire() private val resetPromise = new AtomicReference[Option[Promise[Unit]]](None) override def asFuture: Future[A] = resetPromise.get().getOrElse(Promise.successful(())).future.flatMap(_ => resource.asFuture) override def release(): Future[Unit] = resetPromise.get().getOrElse(Promise.successful(())).future.flatMap(_ => resource.release()) @tailrec private def reset(): Future[Unit] = { val currentResetPromise = resetPromise.get() currentResetPromise match { case None => val newResetPromise = Some(Promise[Unit]()) if (resetPromise.compareAndSet(None, newResetPromise)) { for { value <- resource.asFuture _ <- resource.release() resetValue <- resetOperation(value) } yield { resource = resettableOwner(resetValue).acquire() newResetPromise.get.success(()) resetPromise.set(None) } } else { reset() } case Some(currentResetPromise) => currentResetPromise.future } } } } object ResettableResourceOwner { type Reset = () => Future[Unit] def apply[A](owner: Reset => ResourceOwner[A]) = new ResettableResourceOwner[A, Unit]( initialValue = (), reset => _ => owner(reset), resetOperation = _ => Future.unit, ) def apply[A, ResetValue]( initialValue: ResetValue, owner: Reset => ResetValue => ResourceOwner[A], resetOperation: A => Future[ResetValue], ) = new ResettableResourceOwner(initialValue, owner, resetOperation) }
Example 17
Source File: AkkaResourceOwnerSpec.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.resources.akka import akka.actor.{Actor, ActorSystem, Props} import akka.stream.Materializer import akka.stream.scaladsl.{Keep, Sink, Source} import akka.{Done, NotUsed} import com.daml.resources.ResourceOwner import org.scalatest.{AsyncWordSpec, Matchers} import scala.concurrent.{Future, Promise} class AkkaResourceOwnerSpec extends AsyncWordSpec with Matchers { "a function returning an ActorSystem" should { "convert to a ResourceOwner" in { val testPromise = Promise[Int]() class TestActor extends Actor { @SuppressWarnings(Array("org.wartremover.warts.Any")) override def receive: Receive = { case value: Int => testPromise.success(value) case value => testPromise.failure(new IllegalArgumentException(s"$value")) } } val resource = for { actorSystem <- AkkaResourceOwner .forActorSystem(() => ActorSystem("TestActorSystem")) .acquire() actor <- ResourceOwner .successful(actorSystem.actorOf(Props(new TestActor))) .acquire() } yield (actorSystem, actor) for { resourceFuture <- resource.asFuture (actorSystem, actor) = resourceFuture _ = actor ! 7 result <- testPromise.future _ <- resource.release() } yield { result should be(7) an[IllegalStateException] should be thrownBy actorSystem.actorOf(Props(new TestActor)) } } } "a function returning a Materializer" should { "convert to a ResourceOwner" in { val resource = for { actorSystem <- AkkaResourceOwner .forActorSystem(() => ActorSystem("TestActorSystem")) .acquire() materializer <- AkkaResourceOwner.forMaterializer(() => Materializer(actorSystem)).acquire() } yield materializer for { materializer <- resource.asFuture numbers <- Source(1 to 10) .toMat(Sink.seq)(Keep.right[NotUsed, Future[Seq[Int]]]) .run()(materializer) _ <- resource.release() } yield { numbers should be(1 to 10) an[IllegalStateException] should be thrownBy Source .single(0) .toMat(Sink.ignore)(Keep.right[NotUsed, Future[Done]]) .run()(materializer) } } } }
Example 18
Source File: JsMessageBuilder.scala From iotchain with MIT License | 5 votes |
package jbok.network.http import java.nio.ByteBuffer import cats.effect.{Async, IO} import org.scalajs.dom.{Blob, Event, FileReader, UIEvent} import scala.concurrent.Promise import scala.scalajs.js.typedarray.TypedArrayBufferOps._ import scala.scalajs.js.typedarray._ import scala.scalajs.js.| trait JsMessageBuilder[F[_], P] { import JsMessageBuilder._ def responseType: String def pack(payload: P): Message def unpack(msg: Message): F[Option[P]] } object JsMessageBuilder { type Message = String | ArrayBuffer | Blob implicit def JsMessageBuilderString[F[_]](implicit F: Async[F]): JsMessageBuilder[F, String] = new JsMessageBuilder[F, String] { val responseType = "" def pack(payload: String): Message = payload def unpack(msg: Message): F[Option[String]] = (msg: Any) match { case s: String => F.pure(Some(s)) case b: Blob => readBlob[F, String, String](_.readAsText(b))(identity) case _ => F.pure(None) } } implicit def JsMessageBuilderByteBuffer[F[_]](implicit F: Async[F]): JsMessageBuilder[F, ByteBuffer] = new JsMessageBuilder[F, ByteBuffer] { val responseType = "arraybuffer" def pack(payload: ByteBuffer): Message = payload.arrayBuffer.slice(payload.position, payload.limit) def unpack(msg: Message): F[Option[ByteBuffer]] = (msg: Any) match { case a: ArrayBuffer => F.pure(Option(TypedArrayBuffer.wrap(a))) case b: Blob => readBlob[F, ArrayBuffer, ByteBuffer](_.readAsArrayBuffer(b))(TypedArrayBuffer.wrap(_)) case _ => F.pure(None) } } private def readBlob[F[_], R, W](doRead: FileReader => Unit)(conv: R => W)(implicit F: Async[F]): F[Option[W]] = { val promise = Promise[Option[W]]() val reader = new FileReader reader.onload = (_: UIEvent) => { val s = reader.result.asInstanceOf[R] promise.success(Option(conv(s))) } reader.onerror = (_: Event) => { promise.success(None) } doRead(reader) F.liftIO(IO.fromFuture(IO(promise.future))) } }
Example 19
Source File: TestSpec.scala From nanotest-strawman with Apache License 2.0 | 5 votes |
package verify import scala.concurrent.{ ExecutionContext, Future, Promise } import scala.util.control.NonFatal import scala.util.{ Failure, Success } import verify.sourcecode.SourceLocation case class TestSpec[I, +O](name: String, f: I => Future[Result[O]]) extends (I => Future[Result[O]]) { override def apply(v1: I): Future[Result[O]] = f(v1) } object TestSpec { def async[Env](name: String, cb: Env => Future[Unit])(implicit ec: ExecutionContext): TestSpec[Env, Unit] = TestSpec( name, { env => val f: Future[Unit] = try cb(env) catch { case NonFatal(ex) => Future.failed(ex) } val p = Promise[Result[Unit]]() f.onComplete { case Success(_) => p.success(Result.Success(())) case Failure(ex) => p.success(Result.from(ex)) } p.future } ) def sync[Env](name: String, cb: Env => Void): TestSpec[Env, Unit] = TestSpec( name, { env => try { cb(env) match { case Void.UnitRef => Future.successful(Result.Success(())) case Void.Caught(ref, loc) => Future.successful(unexpected(ref, loc)) } } catch { case NonFatal(ex) => Future.successful(Result.from(ex)) } } ) private def unexpected[A](ref: A, loc: SourceLocation): Result[Nothing] = Result.Failure( s"Problem with test spec, expecting `Unit`, but received: $ref ", None, Some(loc) ) }
Example 20
Source File: BlockingIO.scala From gbf-raidfinder with MIT License | 5 votes |
package walfie.gbf.raidfinder.util import java.util.concurrent.atomic.AtomicLong import java.util.concurrent.{Executors, ThreadFactory} import scala.concurrent.{ExecutionContext, ExecutionContextExecutor, Future, Promise, blocking} import scala.util.control.NonFatal import monix.execution.Scheduler // https://github.com/alexandru/scala-best-practices/blob/master/sections/4-concurrency-parallelism.md object BlockingIO { private val ioThreadPool = Scheduler.io(name = "io-thread") def future[T](t: => T): Future[T] = { val p = Promise[T]() val runnable = new Runnable { def run() = try { p.success(blocking(t)) } catch { case NonFatal(ex) => p.failure(ex) } } ioThreadPool.execute(runnable) p.future } }
Example 21
Source File: NettyRpcCallContext.scala From drizzle-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.rpc.netty import scala.concurrent.Promise import org.apache.spark.internal.Logging import org.apache.spark.network.client.RpcResponseCallback import org.apache.spark.rpc.{RpcAddress, RpcCallContext} private[netty] abstract class NettyRpcCallContext(override val senderAddress: RpcAddress) extends RpcCallContext with Logging { protected def send(message: Any): Unit override def reply(response: Any): Unit = { send(response) } override def sendFailure(e: Throwable): Unit = { send(RpcFailure(e)) } } private[netty] class RemoteNettyRpcCallContext( nettyEnv: NettyRpcEnv, callback: RpcResponseCallback, senderAddress: RpcAddress) extends NettyRpcCallContext(senderAddress) { override protected def send(message: Any): Unit = { val reply = nettyEnv.serialize(message) callback.onSuccess(reply) } }
Example 22
Source File: BlockTransferService.scala From drizzle-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.network import java.io.Closeable import java.nio.ByteBuffer import scala.concurrent.{Future, Promise} import scala.concurrent.duration.Duration import scala.reflect.ClassTag import org.apache.spark.internal.Logging import org.apache.spark.network.buffer.{ManagedBuffer, NioManagedBuffer} import org.apache.spark.network.shuffle.{BlockFetchingListener, ShuffleClient} import org.apache.spark.scheduler.MapStatus import org.apache.spark.storage.{BlockId, StorageLevel} import org.apache.spark.util.ThreadUtils private[spark] abstract class BlockTransferService extends ShuffleClient with Closeable with Logging { def uploadBlockSync( hostname: String, port: Int, execId: String, blockId: BlockId, blockData: ManagedBuffer, level: StorageLevel, classTag: ClassTag[_]): Unit = { val future = uploadBlock(hostname, port, execId, blockId, blockData, level, classTag) ThreadUtils.awaitResult(future, Duration.Inf) } }
Example 23
Source File: JobWaiter.scala From drizzle-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.scheduler import java.util.concurrent.atomic.AtomicInteger import scala.concurrent.{Future, Promise} import org.apache.spark.internal.Logging def cancel() { dagScheduler.cancelJob(jobId) } override def taskSucceeded(index: Int, result: Any): Unit = { // resultHandler call must be synchronized in case resultHandler itself is not thread safe. synchronized { resultHandler(index, result.asInstanceOf[T]) } if (finishedTasks.incrementAndGet() == totalTasks) { jobPromise.success(()) } } override def jobFailed(exception: Exception): Unit = { if (!jobPromise.tryFailure(exception)) { logWarning("Ignore failure", exception) } } }
Example 24
Source File: Main.scala From scala-json-rpc with MIT License | 5 votes |
package io.github.shogowada.scala.jsonrpc.example.e2e.websocket import java.io.IOException import io.github.shogowada.scala.jsonrpc.JSONRPCServerAndClient import io.github.shogowada.scala.jsonrpc.Types.JSONSender import io.github.shogowada.scala.jsonrpc.client.JSONRPCClient import io.github.shogowada.scala.jsonrpc.serializers.UpickleJSONSerializer import io.github.shogowada.scala.jsonrpc.server.JSONRPCServer import io.github.shogowada.scalajs.reactjs.ReactDOM import io.github.shogowada.scalajs.reactjs.VirtualDOM._ import org.scalajs.dom import org.scalajs.dom.WebSocket import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.{Future, Promise} import scala.scalajs.js.JSApp import scala.util.{Failure, Try} object Main extends JSApp { override def main(): Unit = { val futureWebSocket = createFutureWebSocket() val serverAndClient = createServerAndClient(futureWebSocket) val mountNode = dom.document.getElementById("mount-node") ReactDOM.render( <((new TodoListView(serverAndClient.createAPI[TodoRepositoryAPI])) ()).empty, mountNode ) } private def createFutureWebSocket(): Future[WebSocket] = { val promisedWebSocket: Promise[WebSocket] = Promise() val webSocket = new dom.WebSocket(webSocketUrl) webSocket.onopen = (_: dom.Event) => { promisedWebSocket.success(webSocket) } webSocket.onerror = (event: dom.Event) => { promisedWebSocket.failure(new IOException(event.toString)) } promisedWebSocket.future } private def webSocketUrl: String = { val location = dom.window.location val protocol = location.protocol match { case "http:" => "ws:" case "https:" => "wss:" } s"$protocol//${location.host}/jsonrpc" } private def createServerAndClient(futureWebSocket: Future[WebSocket]): JSONRPCServerAndClient[UpickleJSONSerializer] = { val jsonSerializer = UpickleJSONSerializer() val server = JSONRPCServer(jsonSerializer) val jsonSender: JSONSender = (json: String) => { futureWebSocket .map(webSocket => Try(webSocket.send(json))) .flatMap(tried => tried.fold( throwable => Future.failed(throwable), _ => Future(None) )) } val client = JSONRPCClient(jsonSerializer, jsonSender) val serverAndClient = JSONRPCServerAndClient(server, client) futureWebSocket.foreach(webSocket => { webSocket.onmessage = (event: dom.MessageEvent) => { val message = event.data.toString serverAndClient.receiveAndSend(message).onComplete { case Failure(throwable) => { println("Failed to send response", throwable) } case _ => } } }) serverAndClient } }
Example 25
Source File: JSONRPCPromisedResponseRepository.scala From scala-json-rpc with MIT License | 5 votes |
package io.github.shogowada.scala.jsonrpc.client import io.github.shogowada.scala.jsonrpc.Types.Id import scala.concurrent.Promise class JSONRPCPromisedResponseRepository { private var idToPromisedResponseMap: Map[Id, Promise[String]] = Map.empty def addAndGet(id: Id): Promise[String] = this.synchronized { val promisedResponse: Promise[String] = Promise() idToPromisedResponseMap = idToPromisedResponseMap + (id -> promisedResponse) promisedResponse } def getAndRemove(id: Id): Option[Promise[String]] = this.synchronized { val maybePromisedResponse = idToPromisedResponseMap.get(id) idToPromisedResponseMap = idToPromisedResponseMap - id maybePromisedResponse } }
Example 26
Source File: Retry.scala From incubator-s2graph with Apache License 2.0 | 5 votes |
package org.apache.s2graph.counter.util import scala.annotation.tailrec import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.{Failure, Success, Try} object Retry { @tailrec def apply[T](n: Int, withSleep: Boolean = true, tryCount: Int = 0)(fn: => T): T = { Try { fn } match { case Success(x) => x case Failure(e) if e.isInstanceOf[RetryStopException] => throw e.getCause case _ if n > 1 => // backoff if (withSleep) Thread.sleep(tryCount * 1000) apply(n - 1, withSleep, tryCount + 1)(fn) case Failure(e) => throw e } } } object RetryAsync { def apply[T](n: Int, withSleep: Boolean = true, tryCount: Int = 0)(fn: => Future[T])(implicit ex: ExecutionContext): Future[T] = { val promise = Promise[T]() fn onComplete { case Success(x) => promise.success(x) case Failure(e) if e.isInstanceOf[RetryStopException] => promise.failure(e.getCause) case _ if n > 1 => // backoff if (withSleep) Thread.sleep(tryCount * 1000) apply(n - 1, withSleep, tryCount + 1)(fn) case Failure(e) => promise.failure(e) } promise.future } } class RetryStopException(message: String, cause: Throwable) extends Exception(message, cause) { def this(message: String) = this(message, null) def this(cause: Throwable) = this(cause.toString, cause) }
Example 27
Source File: AskActor.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.actors import akka.actor.{Actor, ActorRef, ActorSystem, Props, Status} import scala.concurrent.duration.FiniteDuration import scala.concurrent.{Future, Promise, TimeoutException} import scala.reflect.ClassTag class AskActor[T](p: Promise[T], timeout: FiniteDuration)(implicit ct: ClassTag[T]) extends Actor { import context.dispatcher private val timeoutCancelable = context.system.scheduler.scheduleOnce(timeout, self, AskActor.timeoutMessage) override val receive: Receive = { case x => // Fix in Scala 2.13 timeoutCancelable.cancel() context.stop(self) x match { case x: T if x.getClass == ct.runtimeClass => p.trySuccess(x) case e: Status.Failure => p.tryFailure(e.cause) case _ => p.tryFailure(new IllegalArgumentException(s"Expected ${ct.runtimeClass.getName}, but got $x")) } } } object AskActor { private val timeoutMessage = { val reason = new TimeoutException("Typed ask is timed out!") reason.setStackTrace(Array.empty) Status.Failure(reason) } def props[T](p: Promise[T], timeout: FiniteDuration)(implicit ct: ClassTag[T]) = Props(new AskActor(p, timeout)) def mk[T](timeout: FiniteDuration)(implicit ct: ClassTag[T], system: ActorSystem): (ActorRef, Future[T]) = { val p = Promise[T]() val ref = system.actorOf(props(p, timeout)) (ref, p.future) } }
Example 28
Source File: Implicits.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.grpc.integration.effect import io.netty.util.concurrent.{Future => NettyFuture} import scala.concurrent.{CancellationException, Future, Promise} object Implicits { final implicit class NettyFutureOps[T](val self: NettyFuture[T]) extends AnyVal { def asScala: Future[T] = { val r = Promise[T]() self.addListener { (future: NettyFuture[T]) => if (future.isSuccess) r.success(future.get()) else if (future.isCancelled) r.failure(new CancellationException) else r.failure(future.cause()) } r.future } } }
Example 29
Source File: GlobalTimer.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.it.time import io.netty.util.{HashedWheelTimer, Timer} import scala.concurrent.duration.FiniteDuration import scala.concurrent.{Future, Promise} import scala.util.control.NonFatal object GlobalTimer { val instance: Timer = new HashedWheelTimer() sys.addShutdownHook { instance.stop() } implicit class TimerOpsImplicits(val timer: Timer) extends AnyVal { def schedule[A](f: => Future[A], delay: FiniteDuration): Future[A] = { val p = Promise[A] try { timer.newTimeout(_ => p.completeWith(f), delay.length, delay.unit) } catch { case NonFatal(e) => p.failure(e) } p.future } def sleep(term: FiniteDuration): Future[Unit] = schedule(Future.successful(()), term) } }
Example 30
Source File: FoldResourceSink.scala From gfc-aws-s3 with Apache License 2.0 | 5 votes |
package com.gilt.gfc.aws.s3.akka import akka.stream.stage._ import akka.stream._ import akka.stream.scaladsl.Sink import scala.concurrent.{Future, Promise} import scala.util.Try class FoldResourceSink[TState, TItem, Mat]( open: () => TState, onEach: (TState, TItem) => (TState), close: TState => Mat, onFailure: (Throwable, TState) => Unit ) extends GraphStageWithMaterializedValue[SinkShape[TItem], Future[Mat]] { private val in = Inlet[TItem]("Resource.Sink") override val shape: Shape = SinkShape(in) class FoldResourceSinkLogic(materializedPromise: Promise[Mat]) extends GraphStageLogic(shape) with InHandler { var state: TState = _ override def preStart(): Unit = { state = open() pull(in) } def onPush(): Unit = { val value = grab(in) try { state = onEach(state, value) pull(in) } catch { case ex: Throwable => fail(ex) } } override def onUpstreamFinish(): Unit = { val materializedValue = Try(close(state)) materializedPromise.complete(materializedValue) } override def onUpstreamFailure(ex: Throwable): Unit = { fail(ex) } private def fail(ex: Throwable) = { onFailure(ex, state) materializedPromise.tryFailure(ex) failStage(ex) } setHandler(in, this) } def createLogicAndMaterializedValue(inheritedAttributes: Attributes): (GraphStageLogic, Future[Mat]) = { val completePromise = Promise[Mat]() val stageLogic = new FoldResourceSinkLogic(completePromise) stageLogic -> completePromise.future } } object FoldResourceSink { implicit class SinkExtension(val sink: Sink.type) extends AnyVal { def foldResource[TState, TItem, Mat]( open: () => TState, onEach: (TState, TItem) => (TState), close: TState => Mat, onFailure: (Throwable, TState) => Unit = (ex: Throwable, f: TState) => () ): FoldResourceSink[TState, TItem, Mat] = { new FoldResourceSink(open, onEach, close, onFailure) } } }
Example 31
Source File: SlackRtmClientTest.scala From slack-scala-client with MIT License | 5 votes |
package slack import java.util.concurrent.{CountDownLatch, TimeUnit} import slack.api.SlackApiClient import slack.models.Reply import slack.rtm.SlackRtmClient import scala.concurrent.duration._ import scala.concurrent.{Await, Promise} import org.scalatest.funsuite.AnyFunSuite import org.scalatest.matchers.should.Matchers class SlackRtmClientTest extends AnyFunSuite with Matchers with Credentials { rtmToken match { case Some(slackToken) => val channel = system.settings.config.getString("test.channel") lazy val rtmClient = { val rtm = SlackRtmClient(slackToken) assert(rtm.state.self.id != null) rtm } test("rtm typing") { rtmClient.indicateTyping(channel) } test("team domain") { val domain = rtmClient.state.team.domain val name = rtmClient.state.team.name domain should be(system.settings.config.getString("test.team.domain")) name should be(system.settings.config.getString("test.team.name")) } test("send message and parse reply") { val latch = new CountDownLatch(1) val promise = Promise[Long]() rtmClient.onEvent { case r: Reply => assert(r.reply_to.equals(Await.result(promise.future, 2.seconds))) latch.countDown() case e => println("EVENT >>>>> " + e) } val messageIdFuture = rtmClient.sendMessage(channel, "Hi there") promise.completeWith(messageIdFuture) latch.await(5, TimeUnit.SECONDS) } ignore("edit message as bot") { val rtmApi = SlackApiClient(slackToken) val future = rtmApi.updateChatMessage(channel, "1465891701.000006", "edit-x", asUser = Some(true)) val result = Await.result(future, 5.seconds) assert(result.ok.equals(true)) } case _ => println("Skipping the test as the API credentials are not available") } }
Example 32
Source File: Clients.scala From lila-ws with GNU Affero General Public License v3.0 | 5 votes |
package lila.ws import akka.actor.typed.scaladsl.Behaviors import scala.concurrent.Promise object Clients { sealed trait Control final case class Start(behavior: ClientBehavior, promise: Promise[Client]) extends Control final case class Stop(client: Client) extends Control def behavior = Behaviors.receive[Control] { (ctx, msg) => msg match { case Start(behavior, promise) => promise success ctx.spawnAnonymous(behavior) Behaviors.same case Stop(client) => ctx.stop(client) Behaviors.same } } }
Example 33
Source File: Effects.scala From monadless with Apache License 2.0 | 5 votes |
package io.monadless.lst import scala.util.Failure import scala.util.Try import scala.util.Success import scala.concurrent.Future import scala.concurrent.Promise object Effects { val optionEffect = new SyncEffect[Option] { def point[T](v: T) = Some(v) def lift[T](v: => T) = Option(v) def apply[T](o: Option[T]) = o match { case Some(v) => Sync(Left(v)) case None => Sync(Right(None)) } } val tryEffect = new SyncEffect[Try] { def point[T](v: T) = Success(v) def lift[T](v: => T) = Try(v) def apply[T](o: Try[T]) = o match { case Success(v) => Sync(Left(v)) case Failure(ex) => Sync(Right(Failure(ex))) } } val futureEffect = new AsyncEffect[Future] { import scala.concurrent.ExecutionContext.Implicits.global def point[T](v: T) = Future.successful(v) def lift[T](v: => T) = Future(v) def async[T](r: Async[Future[T]]): Future[T] = { val p = Promise[T]() r.cb(p.completeWith(_)) p.future } def apply[T](o: Future[T]) = Async { f => o.onComplete { case Success(v) => f(Left(v)) case Failure(ex) => f(Right(Future.failed(ex))) } } } }
Example 34
Source File: WaitForTaskDsl.scala From algoliasearch-client-scala with MIT License | 5 votes |
package algolia.dsl import java.time.ZonedDateTime import java.util.concurrent.{Executors, ThreadFactory, TimeUnit} import algolia.definitions.{WaitForTaskDefinition, WaitForTimeoutException} import algolia.responses.{AlgoliaTask, TaskStatus} import algolia.{AlgoliaClient, Executable} import io.netty.util.{HashedWheelTimer, Timeout, TimerTask} import scala.concurrent.{ExecutionContext, Future, Promise} trait WaitForTaskDsl { case object waitFor { def task(task: AlgoliaTask): WaitForTaskDefinition = WaitForTaskDefinition(task.idToWaitFor) def task(taskID: Long): WaitForTaskDefinition = WaitForTaskDefinition(taskID) } implicit object WaitForTaskDefinitionExecutable extends Executable[WaitForTaskDefinition, TaskStatus] { // Run every 100 ms, use a wheel with 512 buckets private lazy val timer = { val threadFactory = new ThreadFactory { override def newThread(r: Runnable): Thread = { val t = Executors.defaultThreadFactory().newThread(r) t.setDaemon(true) t.setName("algolia-waitfor-thread-" + ZonedDateTime.now()) t } } new HashedWheelTimer(threadFactory, 100, TimeUnit.MILLISECONDS, 512) } override def apply(client: AlgoliaClient, query: WaitForTaskDefinition)( implicit executor: ExecutionContext ): Future[TaskStatus] = { def request(d: Long, totalDelay: Long): Future[TaskStatus] = delay[TaskStatus](d) { client.request[TaskStatus](query.build()) }.flatMap { res => if (res.status == "published") { Future.successful(res) } else if (totalDelay > query.maxDelay) { Future.failed( WaitForTimeoutException( s"Waiting for task `${query.taskId}` on index `${query.index.get}` timeout after ${d}ms" ) ) } else { request(d * 2, totalDelay + d) } } request(query.baseDelay, 0L) } private def delay[T](delay: Long)(block: => Future[T]): Future[T] = { val promise = Promise[T]() val task = new TimerTask { override def run(timeout: Timeout): Unit = promise.completeWith(block) } timer.newTimeout(task, delay, TimeUnit.MILLISECONDS) promise.future } } }
Example 35
Source File: PhaseCache.scala From Converter with GNU General Public License v3.0 | 5 votes |
package org.scalablytyped.converter.internal.phases import java.nio.channels.{ClosedByInterruptException, FileLockInterruptionException} import java.util import org.scalablytyped.converter.internal.phases.PhaseCache.Ref import scala.concurrent.{ExecutionException, Future, Promise} import scala.util.control.NonFatal class PhaseCache[Id, U](initialCapacity: Int = 1000) { private val m: util.Map[Ref[(Id, IsCircular)], Ref[Future[PhaseRes[Id, U]]]] = new util.HashMap(initialCapacity) def getOrElse(key: (Id, IsCircular))(compute: Promise[PhaseRes[Id, U]] => Unit): Future[PhaseRes[Id, U]] = { val keyRef = new Ref(key) var op: Option[Promise[PhaseRes[Id, U]]] = None val ret = synchronized { val existingFuture: Option[Future[PhaseRes[Id, U]]] = m.get(keyRef) match { case null => None case uRef => uRef.get match { case null => None case u => Some(u) } } existingFuture match { case None => val p = Promise[PhaseRes[Id, U]]() val future = p.future m.put(keyRef, new Ref(future)) op = Some(p) future case Some(found) => found } } op.foreach { p => try compute(p) catch { case x: FileLockInterruptionException => throw x case x: InterruptedException => throw x case x: ClosedByInterruptException => throw x case x: ExecutionException if x.getCause != null => p.failure(x.getCause) case NonFatal(th) => p.failure(th) } } ret } } object PhaseCache { private final class Ref[T](t: T) extends java.lang.ref.SoftReference[T](t) { override def equals(obj: Any): Boolean = obj match { case that: Ref[_] => that.get == get case _ => false } override def hashCode: Int = get.## } }
Example 36
Source File: AmqpPublisher.scala From reliable-http-client with Apache License 2.0 | 5 votes |
package rhttpc.transport.amqp import java.io._ import akka.agent.Agent import com.rabbitmq.client._ import org.slf4j.LoggerFactory import rhttpc.transport.SerializingPublisher.SerializedMessage import rhttpc.transport.{Message, Publisher, Serializer, SerializingPublisher} import rhttpc.utils.Recovered._ import scala.concurrent.{ExecutionContext, Future, Promise} private[amqp] class AmqpPublisher[PubMsg](channel: Channel, queueName: String, exchangeName: String, protected val serializer: Serializer[PubMsg], prepareProperties: PartialFunction[SerializedMessage, AMQP.BasicProperties]) (implicit ec: ExecutionContext) extends SerializingPublisher[PubMsg] with ConfirmListener { private lazy val logger = LoggerFactory.getLogger(getClass) private val seqNoOnAckPromiseAgent = Agent[Map[Long, Promise[Unit]]](Map.empty) override private[rhttpc] def publishSerialized(msg: SerializedMessage): Future[Unit] = { val properties = prepareProperties.applyOrElse( msg, (_: SerializedMessage) => throw new IllegalArgumentException(s"Not supported message type: $msg") ) val ackPromise = Promise[Unit]() for { _ <- seqNoOnAckPromiseAgent.alter { curr => val publishSeqNo = channel.getNextPublishSeqNo logger.debug(s"PUBLISH: $publishSeqNo") channel.basicPublish(exchangeName, queueName, properties, msg.content) curr + (publishSeqNo -> ackPromise) } ack <- ackPromise.future } yield ack } override def handleAck(deliveryTag: Long, multiple: Boolean): Unit = { logger.debug(s"ACK: $deliveryTag, multiple = $multiple") confirm(deliveryTag, multiple)(_.success(Unit)) } override def handleNack(deliveryTag: Long, multiple: Boolean): Unit = { logger.debug(s"NACK: $deliveryTag, multiple = $multiple") confirm(deliveryTag, multiple)(_.failure(NoPubMsgAckException)) } private def confirm(deliveryTag: Long, multiple: Boolean) (complete: Promise[Unit] => Unit): Unit = { seqNoOnAckPromiseAgent.alter { curr => val (toAck, rest) = curr.partition { case (seqNo, ackPromise) => seqNo == deliveryTag || multiple && seqNo <= deliveryTag } toAck.foreach { case (seqNo, ackPromise) => complete(ackPromise) } rest } } override def start(): Unit = {} override def stop(): Future[Unit] = { recoveredFuture("completing publishing", currentPublishingFuturesComplete) .map(_ => recovered("channel closing", channel.close())) } private def currentPublishingFuturesComplete: Future[Unit] = seqNoOnAckPromiseAgent.future() .flatMap(map => Future.sequence(map.values.map(_.future))) .map(_ => Unit) } case object NoPubMsgAckException extends Exception(s"No acknowledgement for published message")
Example 37
Source File: PromiseSubscriptionCommandsListener.scala From reliable-http-client with Apache License 2.0 | 5 votes |
package rhttpc.client.subscription import akka.actor.{Actor, Props, Status} import scala.concurrent.Promise private class PromiseSubscriptionCommandsListener(pubPromise: ReplyFuture, replyPromise: Promise[Any]) (subscriptionManager: SubscriptionManager) extends PublicationListener { import context.dispatcher override def subscriptionPromiseRegistered(sub: SubscriptionOnResponse): Unit = {} override def receive: Actor.Receive = { case RequestPublished(sub) => subscriptionManager.confirmOrRegister(sub, self) context.become(waitForMessage) case RequestAborted(sub, cause) => replyPromise.failure(cause) context.stop(self) } private val waitForMessage: Receive = { case MessageFromSubscription(Status.Failure(ex), sub) => replyPromise.failure(ex) context.stop(self) case MessageFromSubscription(msg, sub) => replyPromise.success(msg) context.stop(self) } pubPromise.pipeTo(this) } private[subscription] object PromiseSubscriptionCommandsListener { def props(pubPromise: ReplyFuture, replyPromise: Promise[Any]) (subscriptionManager: SubscriptionManager): Props = Props(new PromiseSubscriptionCommandsListener(pubPromise, replyPromise)(subscriptionManager)) }
Example 38
Source File: MockTransport.scala From reliable-http-client with Apache License 2.0 | 5 votes |
package rhttpc.client import akka.actor.ActorRef import akka.pattern._ import akka.util.Timeout import rhttpc.client.protocol.{Correlated, FailureExchange, SuccessExchange} import rhttpc.transport._ import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.{Failure, Success} class MockTransport(awaitCond: (() => Boolean) => Unit)(implicit ec: ExecutionContext) extends PubSubTransport { @volatile private var _publicationPromise: Promise[Unit] = _ @volatile private var _replySubscriptionPromise: Promise[String] = _ @volatile private var _ackOnReplySubscriptionFuture: Future[Any] = _ @volatile private var consumer: ActorRef = _ def publicationPromise: Promise[Unit] = { awaitCond(() => _publicationPromise != null) _publicationPromise } def replySubscriptionPromise: Promise[String] = { awaitCond(() => _replySubscriptionPromise != null) _replySubscriptionPromise } def ackOnReplySubscriptionFuture: Future[Any] = { awaitCond(() => _ackOnReplySubscriptionFuture != null) _ackOnReplySubscriptionFuture } override def publisher[PubMsg: Serializer](data: OutboundQueueData): Publisher[PubMsg] = new Publisher[PubMsg] { override def publish(request: Message[PubMsg]): Future[Unit] = { request.content match { case Correlated(msg, correlationId) => _publicationPromise = Promise[Unit]() _replySubscriptionPromise = Promise[String]() implicit val timeout = Timeout(5 seconds) _replySubscriptionPromise.future.onComplete { case Success(result) => _ackOnReplySubscriptionFuture = consumer ? Correlated(SuccessExchange(msg, result), correlationId) case Failure(ex) => _ackOnReplySubscriptionFuture = consumer ? Correlated(FailureExchange(msg, ex), correlationId) } _publicationPromise.future case other => throw new IllegalArgumentException("Illegal message content: " + other) } } override def start(): Unit = {} override def stop(): Future[Unit] = Future.successful(Unit) } override def fullMessageSubscriber[SubMsg: Deserializer](data: InboundQueueData, consumer: ActorRef): Subscriber[SubMsg] = subscriber(data, consumer) override def subscriber[SubMsg: Deserializer](data: InboundQueueData, consumer: ActorRef): Subscriber[SubMsg] = new Subscriber[SubMsg] { MockTransport.this.consumer = consumer override def start(): Unit = {} override def stop(): Future[Unit] = Future.successful(Unit) } override def stop(): Future[Unit] = Future.successful(Unit) } object MockProxyTransport extends PubSubTransport { override def publisher[PubMsg: Serializer](queueData: OutboundQueueData): Publisher[PubMsg] = new Publisher[PubMsg] { override def publish(msg: Message[PubMsg]): Future[Unit] = Future.successful(Unit) override def start(): Unit = {} override def stop(): Future[Unit] = Future.successful(Unit) } override def fullMessageSubscriber[SubMsg: Deserializer](data: InboundQueueData, consumer: ActorRef): Subscriber[SubMsg] = subscriber(data, consumer) override def subscriber[SubMsg: Deserializer](queueData: InboundQueueData, consumer: ActorRef): Subscriber[SubMsg] = new Subscriber[SubMsg] { override def start(): Unit = {} override def stop(): Future[Unit] = Future.successful(Unit) } override def stop(): Future[Unit] = Future.successful(Unit) }
Example 39
Source File: LoggingState.scala From logging with Apache License 2.0 | 5 votes |
package com.persist.logging import akka.actor._ import LogActor.{AkkaMessage, LogActorMessage} import scala.language.existentials import scala.concurrent.Promise import scala.collection.mutable import TimeActorMessages._ private[logging] object LoggingState extends ClassLogging { // Queue of messages sent before logger is started private[logging] val msgs = new mutable.Queue[LogActorMessage]() @volatile var doTrace:Boolean = false @volatile var doDebug: Boolean = false @volatile var doInfo: Boolean = true @volatile var doWarn: Boolean = true @volatile var doError: Boolean = true private[logging] var loggingSys: LoggingSystem = null private[logging] var logger: Option[ActorRef] = None @volatile private[logging] var loggerStopping = false private[logging] var doTime: Boolean = false private[logging] var timeActorOption: Option[ActorRef] = None // Use to sync akka logging actor shutdown private[logging] val akkaStopPromise = Promise[Unit] private[logging] def sendMsg(msg: LogActorMessage) { if (loggerStopping) { println(s"*** Log message received after logger shutdown: $msg") } else { logger match { case Some(a) => a ! msg case None => msgs.synchronized { msgs.enqueue(msg) } } } } private[logging] def akkaMsg(m: AkkaMessage) { if (m.msg == "DIE") { akkaStopPromise.trySuccess(()) } else { sendMsg(m) } } private[logging] def timeStart(id: RequestId, name: String, uid: String) { timeActorOption foreach { case timeActor => val time = System.nanoTime() / 1000 timeActor ! TimeStart(id, name, uid, time) } } private[logging] def timeEnd(id: RequestId, name: String, uid: String) { timeActorOption foreach { case timeActor => val time = System.nanoTime() / 1000 timeActor ! TimeEnd(id, name, uid, time) } } }
Example 40
Source File: Search.scala From Principles-of-Reactive-Programming with GNU General Public License v3.0 | 5 votes |
package suggestions package search import org.json4s._ import scala.concurrent.{ ExecutionContext, Future, Promise } import ExecutionContext.Implicits.global import scala.language.postfixOps import scala.collection._ import scala.collection.JavaConverters._ import scala.util.Try import scala.async.Async._ import rx.lang.scala.Observable import observablex.{SchedulerEx, ObservableEx} import ObservableEx._ import dispatch._ import org.json4s.native._ import retrofit.http.{GET, Query} import retrofit.Callback import retrofit.client.Response import retrofit.{RetrofitError, Callback, RestAdapter} import com.google.gson.annotations.SerializedName object Search { trait WikipediaService { @GET("/w/api.php?action=opensearch&format=json&limit=15") def suggestions(@Query("search") term: String, callback: Callback[Array[AnyRef]]): Unit @GET("/w/api.php?action=parse&format=json&prop=text§ion=0") def page(@Query("page") term: String, callback: Callback[Page]): Unit } val restAdapter = new RestAdapter.Builder().setEndpoint("https://en.wikipedia.org").build() val service = restAdapter.create(classOf[WikipediaService]) def callbackFuture[T]: (Callback[T], Future[T]) = { val p = Promise[T]() val cb = new Callback[T] { def success(t: T, response: Response) = { p success t } def failure(error: RetrofitError) = { p failure error } } (cb, p.future) } def wikipediaSuggestionRetrofit(term: String): Future[List[String]] = { async { val (cb, f) = callbackFuture[Array[AnyRef]] service.suggestions(term, cb) val result = await { f } val arraylist = result(1).asInstanceOf[java.util.List[String]] arraylist.asScala.toList } } def wikipediaPageRetrofit(term: String): Future[String] = { async { val (cb, f) = callbackFuture[Page] service.page(term, cb) val result = await { f } result.parse.text.all } } def wikipediaSuggestion(term: String): Future[List[String]] = wikipediaSuggestionRetrofit(term) def wikipediaPage(term: String): Future[String] = wikipediaPageRetrofit(term) }
Example 41
Source File: WebSocketConnectorImpl.scala From Sidechains-SDK with MIT License | 5 votes |
package com.horizen.websocket import java.net.URI import javax.websocket.{ClientEndpoint, CloseReason, MessageHandler, SendHandler, SendResult, Session} import org.glassfish.tyrus.client.{ClientManager, ClientProperties} import scorex.util.ScorexLogging import scala.concurrent.duration.FiniteDuration import scala.concurrent.{Future, Promise} import scala.util.Try @ClientEndpoint class WebSocketConnectorImpl(bindAddress: String, connectionTimeout: FiniteDuration, messageHandler: WebSocketMessageHandler, reconnectionHandler: WebSocketReconnectionHandler) extends WebSocketConnector with WebSocketChannel with ScorexLogging { private var userSession: Session = _ private val client = ClientManager.createClient() private val reconnectHandler: ClientManager.ReconnectHandler = new ClientManager.ReconnectHandler() { override def getDelay: Long = { reconnectionHandler.getDelay.toSeconds } // will be executed whenever @OnClose annotated method (or Endpoint.onClose(..)) is executed on client side. // this should happen when established connection is lost for any reason override def onDisconnect(closeReason: CloseReason): Boolean = { log.info("onDisconnect. Reason: " + closeReason.toString) if (closeReason.getCloseCode.getCode == 1000) reconnectionHandler.onDisconnection(DisconnectionCode.ON_SUCCESS, closeReason.getReasonPhrase) else reconnectionHandler.onDisconnection(DisconnectionCode.UNEXPECTED, closeReason.getReasonPhrase) } // is invoked when client fails to connect to remote endpoint override def onConnectFailure(exception: Exception): Boolean = reconnectionHandler.onConnectionFailed(exception) } override def isStarted: Boolean = userSession != null && userSession.isOpen override def start(): Try[Unit] = Try { if (isStarted) throw new IllegalStateException("Connector is already started.") client.getProperties.put(ClientProperties.RECONNECT_HANDLER, reconnectHandler) client.getProperties.put(ClientProperties.HANDSHAKE_TIMEOUT, String.valueOf(connectionTimeout.toMillis)) log.info("Starting web socket connector...") userSession = client.connectToServer(this, new URI(bindAddress)) reconnectionHandler.onConnectionSuccess() log.info("Web socket connector started.") userSession.addMessageHandler(new MessageHandler.Whole[String]() { override def onMessage(t: String): Unit = { log.info("Message received from server: " + t) messageHandler.onReceivedMessage(t) } }) } override def asyncStart(): Future[Try[Unit]] = { val promise: Promise[Try[Unit]] = Promise[Try[Unit]] new Thread(new Runnable { override def run(): Unit = { promise.success(start()) } }).start() promise.future } override def stop(): Try[Unit] = Try { log.info("Stopping web socket connector...") userSession.close() log.info("Web socket connector stopped.") } override def sendMessage(message: String): Unit = { try { userSession.getAsyncRemote().sendText(message, new SendHandler { override def onResult(sendResult: SendResult): Unit = { if (!sendResult.isOK) { log.info("Send message failed.") messageHandler.onSendMessageErrorOccurred(message, sendResult.getException) } else log.info("Message sent") } } ) } catch { case e: Throwable => messageHandler.onSendMessageErrorOccurred(message, e) } } }
Example 42
Source File: SidechainTransactionActor.scala From Sidechains-SDK with MIT License | 5 votes |
package com.horizen.api.http import akka.actor.{Actor, ActorRef, ActorSystem, Props} import com.horizen.SidechainTypes import com.horizen.api.http.SidechainTransactionActor.ReceivableMessages.BroadcastTransaction import scorex.core.NodeViewHolder.ReceivableMessages.LocallyGeneratedTransaction import scorex.core.network.NodeViewSynchronizer.ReceivableMessages.{FailedTransaction, SuccessfulTransaction} import scorex.util.{ModifierId, ScorexLogging} import scala.collection.concurrent.TrieMap import scala.concurrent.{ExecutionContext, Promise} class SidechainTransactionActor[T <: SidechainTypes#SCBT](sidechainNodeViewHolderRef: ActorRef)(implicit ec: ExecutionContext) extends Actor with ScorexLogging { private var transactionMap : TrieMap[String, Promise[ModifierId]] = TrieMap() override def preStart(): Unit = { context.system.eventStream.subscribe(self, classOf[SuccessfulTransaction[T]]) context.system.eventStream.subscribe(self, classOf[FailedTransaction]) } protected def broadcastTransaction: Receive = { case BroadcastTransaction(transaction) => val promise = Promise[ModifierId] val future = promise.future transactionMap(transaction.id) = promise sender() ! future sidechainNodeViewHolderRef ! LocallyGeneratedTransaction[SidechainTypes#SCBT](transaction) } protected def sidechainNodeViewHolderEvents: Receive = { case SuccessfulTransaction(transaction) => transactionMap.remove(transaction.id) match { case Some(promise) => promise.success(transaction.id) case None => } case FailedTransaction(transactionId, throwable, _) => transactionMap.remove(transactionId) match { case Some(promise) => promise.failure(throwable) case None => } } override def receive: Receive = { broadcastTransaction orElse sidechainNodeViewHolderEvents orElse { case message: Any => log.error("SidechainTransactionActor received strange message: " + message) } } } object SidechainTransactionActor { object ReceivableMessages { case class BroadcastTransaction[T <: SidechainTypes#SCBT](transaction: T) } } object SidechainTransactionActorRef { def props(sidechainNodeViewHolderRef: ActorRef) (implicit ec: ExecutionContext): Props = Props(new SidechainTransactionActor(sidechainNodeViewHolderRef)) def apply(sidechainNodeViewHolderRef: ActorRef) (implicit system: ActorSystem, ec: ExecutionContext): ActorRef = system.actorOf(props(sidechainNodeViewHolderRef)) }
Example 43
Source File: GlobalTimer.scala From EncryCore with GNU General Public License v3.0 | 5 votes |
package encry.it.util import io.netty.util.{HashedWheelTimer, Timer} import scala.concurrent.{ExecutionContext, Future, Promise} import scala.concurrent.duration.FiniteDuration import scala.util.control.NonFatal object GlobalTimer { val timer: Timer = new HashedWheelTimer() sys.addShutdownHook { timer.stop() } implicit class TimerExt(val timer: Timer) extends AnyVal { def schedule[A](f: => Future[A], delay: FiniteDuration): Future[A] = { val p = Promise[A] try { timer.newTimeout(_ => p.completeWith(f), delay.length, delay.unit) } catch { case NonFatal(e) => p.failure(e) } p.future } def sleep(term: FiniteDuration): Future[Unit] = schedule(Future.successful(()), term) def retryUntil[A](f: => Future[A], cond: A => Boolean, retryInterval: FiniteDuration)(implicit ec: ExecutionContext): Future[A] = f.flatMap(v => if (cond(v)) Future.successful(v) else schedule(retryUntil(f, cond, retryInterval), retryInterval)) } }
Example 44
Source File: ProcessStep.scala From process with Apache License 2.0 | 5 votes |
package processframework import scala.concurrent.duration.Duration import scala.concurrent.{ ExecutionContext, Future, Promise } import scala.reflect.ClassTag import akka.actor.{ Actor, ActorContext, ActorRef, Props } import akka.util.Timeout trait ProcessStep[S] { implicit def context: ActorContext private[processframework] val promise: Promise[Unit] = Promise[Unit]() type Execution = S ⇒ Unit type UpdateFunction = PartialFunction[Process.Event, S ⇒ S] type CommandToEvent = PartialFunction[Any, Process.Event] def execute()(implicit process: ActorRef): Execution def receiveCommand: CommandToEvent def updateState: UpdateFunction def retryInterval: Duration = Duration.Inf final def isCompleted = promise.isCompleted final def markDone(): Unit = promise.trySuccess(()) final def markDone(newState: S): S = { markDone() newState } private[processframework] def abort(): Unit = promise.tryFailure(new RuntimeException("Process aborted")) final def onComplete(completeFn: ((ActorContext, S)) ⇒ Unit)(implicit executionContext: ExecutionContext, process: ActorRef): Unit = promise.future.foreach { _ ⇒ process ! PersistentProcess.Perform(completeFn) } final def onCompleteAsync(completeFn: ⇒ Unit)(implicit executionContext: ExecutionContext): Unit = promise.future.foreach(_ ⇒ completeFn) final def ~>(next: ProcessStep[S]*)(implicit context: ActorContext): ProcessStep[S] = new Chain(this, next: _*) private[processframework] def run()(implicit process: ActorRef, executionContext: ExecutionContext, classTag: ClassTag[S]): Future[Unit] = runImpl private val innerActor = context.actorOf(Props(new Actor { def receive = { case msg if receiveCommand.isDefinedAt(msg) ⇒ val event = receiveCommand(msg) context.parent ! event } })) private[processframework] def handleUpdateState: UpdateFunction = if (isCompleted) PartialFunction.empty[Process.Event, S ⇒ S] else updateState private[processframework] def handleReceiveCommand: CommandToEvent = if (isCompleted) PartialFunction.empty[Any, Process.Event] else receiveCommand private[processframework] def executeWithPossibleRetry()(implicit process: ActorRef): Execution = { state ⇒ implicit val _ = context.dispatcher if (retryInterval.isFinite()) context.system.scheduler.scheduleOnce(Duration.fromNanos(retryInterval.toNanos)) { if (!isCompleted) executeWithPossibleRetry()(process)(state) } execute()(process)(state) } private[processframework] def runImpl()(implicit process: ActorRef, executionContext: ExecutionContext, classTag: ClassTag[S]): Future[Unit] = { import akka.pattern.ask import scala.concurrent.duration._ implicit val timeout: Timeout = 5 seconds if (!isCompleted) (process ? Process.GetState).mapTo[S].foreach(executeWithPossibleRetry()(innerActor)) promise.future } }
Example 45
Source File: Choice.scala From process with Apache License 2.0 | 5 votes |
package processframework import scala.concurrent.{ ExecutionContext, Future, Promise } import scala.reflect.ClassTag import akka.actor.{ ActorContext, ActorRef } class Choice[S](condition: S ⇒ Boolean, processIfTrue: ProcessStep[S], processIfFalse: ProcessStep[S])(implicit val context: ActorContext, classTag: ClassTag[S]) extends ProcessStep[S] { private[processframework] val truePromise: Promise[Unit] = Promise[Unit]() private[processframework] val falsePromise: Promise[Unit] = Promise[Unit]() var result = Option.empty[Boolean] override private[processframework] def abort(): Unit = { processIfTrue.abort() processIfFalse.abort() super.abort() } def receiveCommand: CommandToEvent = { if (truePromise.isCompleted) processIfTrue.receiveCommand else if (falsePromise.isCompleted) processIfFalse.receiveCommand else PartialFunction.empty } def updateState: UpdateFunction = { case event if processIfFalse.handleUpdateState.isDefinedAt(event) || processIfTrue.handleUpdateState.isDefinedAt(event) ⇒ result match { case Some(true) ⇒ truePromise.trySuccess(()) processIfTrue.updateState.apply(event) case Some(false) ⇒ falsePromise.trySuccess(()) processIfFalse.updateState.apply(event) case None ⇒ { state ⇒ result = Some(condition(state)) updateState.apply(event)(state) } } } override private[processframework] def runImpl()(implicit self: ActorRef, executionContext: ExecutionContext, classTag: ClassTag[S]): Future[Unit] = { val trueFlow = truePromise.future flatMap { _ ⇒ processIfTrue.run() } val falseFlow = falsePromise.future flatMap { _ ⇒ processIfFalse.run() } super.runImpl() Future.firstCompletedOf(List(trueFlow, falseFlow)) } def execute()(implicit process: ActorRef): Execution = { state ⇒ val choiceResult = condition(state) result = Some(choiceResult) if (choiceResult) truePromise.trySuccess(()) else falsePromise.trySuccess(()) } }
Example 46
Source File: ToFutureImplicits.scala From octopus with Apache License 2.0 | 5 votes |
package octopus.async.scalaz import octopus.async.ToFuture import scalaz.{-\/, \/-} import scalaz.concurrent.Task import scala.concurrent.{Future, Promise} object ToFutureImplicits { implicit val scalazTaskToFuture: ToFuture[Task] = new ToFuture[Task] { def toFuture[A](value: Task[A]): Future[A] = { val p: Promise[A] = Promise() value.unsafePerformAsync { case -\/(ex) => p.failure(ex) () case \/-(r) => p.success(r) () } p.future } } }
Example 47
Source File: Policy.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.policy import akka.util.Timeout import com.webtrends.harness.command.{BaseCommandResponse, CommandResponse} import scala.concurrent.{Promise, Future} import scala.concurrent.duration._ import scala.util.{Failure, Success} def policyName : String = getClass.getSimpleName def decomposeCommandResponse[T<:AnyRef:Manifest](bean:Future[BaseCommandResponse[T]]) : Future[T] = { import scala.concurrent.ExecutionContext.Implicits.global val f = Promise[T]() bean.mapTo[CommandResponse[T]] onComplete { case Success(resp) => f.success(resp.data.get) case Failure(f) => PolicyException("Error in decomposeCommandResponse", f) } f.future } }
Example 48
Source File: PolicyManager.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.policy import akka.pattern.{ask, pipe} import akka.actor.{ActorRef, Props} import akka.routing.{RoundRobinPool, FromConfig} import com.webtrends.harness.HarnessConstants import com.webtrends.harness.app.{PrepareForShutdown, HActor} import com.webtrends.harness.app.HarnessActor.SystemReady import org.slf4j.LoggerFactory import scala.collection.mutable import scala.concurrent.{Future, Promise} import scala.util.{Success, Failure} case class GetPolicies() class PolicyManager extends PrepareForShutdown { import context.dispatcher override def receive = super.receive orElse { case GetPolicies => pipe(getPolicies) to sender case SystemReady => // ignore } protected def getPolicies : Future[Map[String, Policy]] = { Future { PolicyManager.getPolicies.get } } } object PolicyManager { private val externalLogger = LoggerFactory.getLogger(this.getClass) // map that stores the name of the command with the actor it references val policyMap = mutable.Map[String, Policy]() def props = Props[PolicyManager] def addPolicy[T<:Policy](name:String, ref:T) = { ref.addCommands externalLogger.debug(s"Policy $name inserted into Policy Manager map.") policyMap += (name -> ref) } protected def removePolicy(name:String) : Boolean = { policyMap.get(name) match { case Some(n) => externalLogger.debug(s"Policy $name removed from Policy Manager map.") policyMap -= name true case None => false } } def getPolicy(name:String) : Option[Policy] = policyMap.get(name) def getPolicies : Option[Map[String, Policy]] = Some(policyMap.toMap) }
Example 49
Source File: HealthCheckProvider.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.health import java.util.jar.Attributes.Name import java.util.jar.{Attributes, JarFile} import akka.actor.Actor import akka.pattern._ import akka.util.Timeout import com.webtrends.harness.HarnessConstants import com.webtrends.harness.logging.ActorLoggingAdapter import com.webtrends.harness.service.messages.CheckHealth import com.webtrends.harness.utils.ConfigUtil import org.joda.time.DateTime import scala.collection.mutable import scala.concurrent.duration._ import scala.concurrent.{Future, Promise} import scala.util.{Failure, Success} trait HealthCheckProvider { this: Actor with ActorLoggingAdapter => val upTime = DateTime.now implicit val timeout = ConfigUtil.getDefaultTimeout(context.system.settings.config, HarnessConstants.KeyDefaultTimeout, Timeout(15 seconds)) val scalaVersion = util.Properties.versionString val file = getClass.getProtectionDomain.getCodeSource.getLocation.getFile val manifest = file match { case _ if file.endsWith(".jar") => new JarFile(file).getManifest case _ => val man = new java.util.jar.Manifest() man.getMainAttributes.put(Name.IMPLEMENTATION_TITLE, "Webtrends Harness Service") man.getMainAttributes.put(Name.IMPLEMENTATION_VERSION, "develop-SNAPSHOT") man.getMainAttributes.put(new Attributes.Name("Implementation-Build"), "N/A") man } val application = manifest.getMainAttributes.getValue(Name.IMPLEMENTATION_TITLE) val version = manifest.getMainAttributes.getValue(Name.IMPLEMENTATION_VERSION) val alerts: mutable.Buffer[ComponentHealth] = mutable.Buffer() def runChecks: Future[ApplicationHealth] = { import context.dispatcher // Ask for the health of each component val future = (context.actorSelection(HarnessConstants.ActorPrefix) ? CheckHealth).mapTo[Seq[HealthComponent]] val p = Promise[ApplicationHealth] future.onComplete({ case Success(checks) => // Rollup alerts for any critical or degraded components checks.foreach(checkComponents) // Rollup the statuses val overallHealth = rollupStatuses(alerts) alerts.clear() p success ApplicationHealth(application, version, upTime, overallHealth.state, overallHealth.details, checks) case Failure(e) => log.error("An error occurred while fetching the health request results", e) p success ApplicationHealth(application, version, upTime, ComponentState.CRITICAL, e.getMessage, Nil) }) p.future } }
Example 50
Source File: ActorHealth.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.health import akka.actor.{Actor, ActorRef} import akka.pattern._ import akka.util.Timeout import com.webtrends.harness.HarnessConstants import com.webtrends.harness.logging.Logger import com.webtrends.harness.service.messages.CheckHealth import com.webtrends.harness.utils.ConfigUtil import scala.concurrent.duration._ import scala.concurrent.{Future, Promise} import scala.util.{Failure, Success, Try} trait ActorHealth { this: Actor => private val _log = Logger(this, context.system) import context.dispatcher implicit val checkTimeout:Timeout = ConfigUtil.getDefaultTimeout(context.system.settings.config, HarnessConstants.KeyDefaultTimeout, Timeout(15 seconds)) def health:Receive = { case CheckHealth => pipe(Try(checkHealth) .recover({ case e: Exception => _log.error("Error fetching health", e) Future.successful(HealthComponent(getClass.getSimpleName, ComponentState.CRITICAL, "Exception when trying to check the health: %s".format(e.getMessage))) }).get ) to sender() } def checkHealth: Future[HealthComponent] = { val p = Promise[HealthComponent]() getHealth.onComplete { case Success(s) => val healthFutures = getHealthChildren map { ref => (ref ? CheckHealth).mapTo[HealthComponent] recover { case _: AskTimeoutException => _log.warn(s"Health Check time out on child actor ${ref.path.toStringWithoutAddress}") HealthComponent(getClass.getSimpleName, ComponentState.CRITICAL, "Time out on child: %s".format(ref.path.toStringWithoutAddress)) case ex: Exception => HealthComponent(ref.path.name, ComponentState.CRITICAL, s"Failure to get health of child component. ${ex.getMessage}") } } Future.sequence(healthFutures) onComplete { case Failure(f) => _log.debug(f, "Failed to retrieve health of children objects") p success HealthComponent(s.name, ComponentState.CRITICAL, s"Failure to get health of child components. ${f.getMessage}") case Success(healths) => healths foreach { it => s.addComponent(it) } p success s } case Failure(f) => _log.debug(f, "Failed to get health from component") p success HealthComponent(self.path.toString, ComponentState.CRITICAL, f.getMessage) } p.future } }
Example 51
Source File: CommandHelper.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.command import akka.actor.{Props, ActorRef, Actor} import akka.pattern.ask import akka.util.Timeout import com.webtrends.harness.app.Harness import scala.concurrent.duration._ import com.webtrends.harness.HarnessConstants import com.webtrends.harness.logging.ActorLoggingAdapter import scala.concurrent.{Promise, Future} import scala.util.{Failure, Success} trait CommandHelper extends ActorLoggingAdapter with BaseCommandHelper { this: Actor => override lazy implicit val actorSystem = context.system } def executeCommand[T:Manifest](name:String, bean:Option[CommandBean]=None, server:Option[String]=None, port:Int=2552)(implicit timeout:Timeout) : Future[BaseCommandResponse[T]] = { val p = Promise[BaseCommandResponse[T]] initCommandManager onComplete { case Success(_) => commandManager match { case Some(cm) => val msg = server match { case Some(srv) => ExecuteRemoteCommand(name, srv, port, bean, timeout) case None => ExecuteCommand(name, bean, timeout) } (cm ? msg)(timeout).mapTo[BaseCommandResponse[T]] onComplete { case Success(s) => p success s case Failure(f) => p failure CommandException("CommandManager", f) } case None => p failure CommandException("CommandManager", "CommandManager not found!") } case Failure(f) => p failure f } p.future } }
Example 52
Source File: IterateeSpecification.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.libs.iteratee import com.webtrends.harness.libs.iteratee.internal.executeFuture import scala.concurrent.{ Await, ExecutionContext, Future, Promise } import scala.concurrent.duration.{ Duration, SECONDS, MILLISECONDS } import scala.util.Try def delayed(it: => Iteratee[String, String], delay: Duration = Duration(5, MILLISECONDS))(implicit ec: ExecutionContext): Iteratee[String, String] = { Iteratee.flatten(timeout(it, delay)) } val timer = new java.util.Timer(true) def timeout[A](a: => A, d: Duration)(implicit e: ExecutionContext): Future[A] = { val p = Promise[A]() timer.schedule(new java.util.TimerTask { def run() { p.complete(Try(a)) } }, d.toMillis) p.future } }
Example 53
Source File: NonBlockingMutexSpec.scala From wookiee with Apache License 2.0 | 5 votes |
package com.webtrends.harness.libs.concurrent import scala.language.reflectiveCalls import org.specs2.mutable._ import java.util.concurrent.atomic.AtomicInteger import scala.concurrent.{ ExecutionContext, Promise, Future, Await } import scala.concurrent.duration.{ Duration, SECONDS } object NonBlockingMutexSpec extends Specification { val waitTime = Duration(2, SECONDS) trait Tester { def run(body: => Unit): Unit } class MutexTester extends Tester { val mutex = new NonBlockingMutex() def run(body: => Unit) = mutex.exclusive(body) } class NaiveTester extends Tester { def run(body: => Unit) = body } def countOrderingErrors(runs: Int, tester: Tester)(implicit ec: ExecutionContext): Future[Int] = { val result = Promise[Int]() val runCount = new AtomicInteger(0) val orderingErrors = new AtomicInteger(0) for (i <- 0 until runs) { tester.run { val observedRunCount = runCount.getAndIncrement() // We see observedRunCount != i then this task was run out of order if (observedRunCount != i) { orderingErrors.incrementAndGet() // Record the error } // If this is the last task, complete our result promise if ((observedRunCount + 1) >= runs) { result.success(orderingErrors.get) } } } result.future } "NonBlockingMutex" should { "run a single operation" in { val p = Promise[Int]() val mutex = new NonBlockingMutex() mutex.exclusive { p.success(1) } Await.result(p.future, waitTime) must_== (1) } "run two operations" in { val p1 = Promise[Unit]() val p2 = Promise[Unit]() val mutex = new NonBlockingMutex() mutex.exclusive { p1.success(()) } mutex.exclusive { p2.success(()) } Await.result(p1.future, waitTime) must_== (()) Await.result(p2.future, waitTime) must_== (()) } "run code in order" in { import ExecutionContext.Implicits.global def percentageOfRunsWithOrderingErrors(runSize: Int, tester: Tester): Int = { val results: Seq[Future[Int]] = for (i <- 0 until 9) yield { countOrderingErrors(runSize, tester) } Await.result(Future.sequence(results), waitTime).filter(_ > 0).size * 10 } // Iteratively increase the run size until we get observable errors 90% of the time // We want a high error rate because we want to then use the MutexTester // on the same run size and know that it is fixing up some problems. If the run size // is too small then the MutexTester probably isn't doing anything. We use // dynamic run sizing because the actual size that produces errors will vary // depending on the environment in which this test is run. var runSize = 8 // This usually reaches 8192 on my dev machine with 10 simultaneous queues var errorPercentage = 0 while (errorPercentage < 90 && runSize < 1000000) { runSize = runSize << 1 errorPercentage = percentageOfRunsWithOrderingErrors(runSize, new NaiveTester()) } //println(s"Got $errorPercentage% ordering errors on run size of $runSize") // Now show that this run length works fine with the MutexTester percentageOfRunsWithOrderingErrors(runSize, new MutexTester()) must_== 0 } } }
Example 54
Source File: HydraDirectives.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.core.http import akka.http.scaladsl.marshalling.ToResponseMarshallable import akka.http.scaladsl.model.StatusCode import akka.http.scaladsl.model.Uri.Path import akka.http.scaladsl.model.headers.Location import akka.http.scaladsl.server._ import hydra.common.config.ConfigSupport import scala.concurrent.Promise trait HydraDirectives extends Directives with ConfigSupport { def imperativelyComplete(inner: ImperativeRequestContext => Unit): Route = { ctx: RequestContext => val p = Promise[RouteResult]() inner(new ImperativeRequestContextImpl(ctx, p)) p.future } def completeWithLocationHeader[T](status: StatusCode, resourceId: T): Route = extractRequestContext { requestContext => val request = requestContext.request val location = request.uri.withPath(Path("/" + resourceId.toString)) respondWithHeader(Location(location)) { complete(status) } } } trait ImperativeRequestContext { def complete(obj: ToResponseMarshallable): Unit def failWith(error: Throwable): Unit } // an imperative wrapper for request context final class ImperativeRequestContextImpl( val ctx: RequestContext, promise: Promise[RouteResult] ) extends ImperativeRequestContext { private implicit val ec = ctx.executionContext def complete(obj: ToResponseMarshallable): Unit = ctx.complete(obj).onComplete(promise.complete) def failWith(error: Throwable): Unit = ctx.fail(error).onComplete(promise.complete) }
Example 55
Source File: package.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.example import io.vertx.core.{ AsyncResult, DeploymentOptions, Handler, Vertx } import scala.concurrent.{ ExecutionContext, Future, Promise } import scala.reflect.ClassTag package object vertx { object ExampleVertxExtensions { implicit class PromiseHandler[A](promise: Promise[A]) { def asVertxHandler: Handler[AsyncResult[A]] = new Handler[AsyncResult[A]] { override def handle(res: AsyncResult[A]): Unit = { if (res.succeeded()) { promise.success(res.result()) } else { promise.failure(res.cause()) } } } } implicit class RichVertxDeployment(vertx: Vertx) { def deploy[T](options: DeploymentOptions = new DeploymentOptions())(implicit t: ClassTag[T], ec: ExecutionContext): Future[String] = { val promise = Promise[String] vertx.deployVerticle(t.runtimeClass.getName, options, promise.asVertxHandler) promise.future } } } }
Example 56
Source File: Writer.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.example.querydb //#writer import java.lang.{ Long => JLong } import akka.actor.ActorRef import com.datastax.driver.core._ import com.rbmhtechnology.eventuate.EventsourcedWriter import scala.concurrent.Future override def readSuccess(result: Long): Option[Long] = Some(result + 1L) } object Writer { import java.util.concurrent.Executor import com.google.common.util.concurrent.ListenableFuture import scala.concurrent.{ ExecutionContext, Promise } import scala.language.implicitConversions import scala.util.Try implicit class ListenableFutureConverter[A](lf: ListenableFuture[A])(implicit executionContext: ExecutionContext) { def toFuture: Future[A] = { val promise = Promise[A] lf.addListener(new Runnable { def run() = promise.complete(Try(lf.get())) }, executionContext.asInstanceOf[Executor]) promise.future } } } //#
Example 57
Source File: StorageProvider.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.eventuate.adapter.vertx.japi.rx import java.lang.{ Long => JLong } import com.rbmhtechnology.eventuate.adapter.vertx.api.{ StorageProvider => SStorageProvider } import rx.{ Observable, Observer } import scala.concurrent.{ ExecutionContext, Future, Promise } def writeProgress(id: String, sequenceNr: JLong): Observable[JLong] } object StorageProvider { private def futureObserver[A](p: Promise[A]): Observer[A] = new Observer[A] { override def onNext(v: A): Unit = { if (!p.isCompleted) { p.success(v) } } override def onError(e: Throwable): Unit = { p.failure(e) } override def onCompleted(): Unit = { if (!p.isCompleted) { p.failure(new IllegalStateException("No item emitted by Observable")) } } } implicit class StorageProviderConverter(delegate: StorageProvider) { def asScala: SStorageProvider = new SStorageProvider { override def readProgress(id: String)(implicit executionContext: ExecutionContext): Future[Long] = { val p = Promise[JLong] delegate.readProgress(id).subscribe(futureObserver(p)) p.future.map(Long2long) } override def writeProgress(id: String, sequenceNr: Long)(implicit executionContext: ExecutionContext): Future[Long] = { val p = Promise[JLong] delegate.writeProgress(id, sequenceNr).subscribe(futureObserver(p)) p.future.map(Long2long) } } } }
Example 58
Source File: VertxProducer.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.eventuate.adapter.vertx import com.rbmhtechnology.eventuate.DurableEvent import com.rbmhtechnology.eventuate.adapter.vertx.api.EventMetadata import io.vertx.core.Vertx import io.vertx.core.eventbus.{ DeliveryOptions, Message } import scala.concurrent.duration.FiniteDuration import scala.concurrent.{ ExecutionContext, Future, Promise } trait VertxProducer { def vertx: Vertx protected def deliveryOptions(event: DurableEvent): DeliveryOptions = new DeliveryOptions().setHeaders(EventMetadata(event).toHeaders) } trait VertxPublisher extends VertxProducer { def publish(address: String, evt: DurableEvent): Unit = vertx.eventBus().publish(address, evt.payload, deliveryOptions(evt)) } trait VertxSender extends VertxProducer { import VertxHandlerConverters._ def send[A](address: String, evt: DurableEvent, timeout: FiniteDuration)(implicit ec: ExecutionContext): Future[A] = { val promise = Promise[Message[A]] vertx.eventBus().send(address, evt.payload, deliveryOptions(evt).setSendTimeout(timeout.toMillis), promise.asVertxHandler) promise.future.map(_.body) } def send(address: String, evt: DurableEvent): Unit = vertx.eventBus().send(address, evt.payload, deliveryOptions(evt)) }
Example 59
Source File: package.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.eventuate.adapter import com.rbmhtechnology.eventuate.EventsourcedView import io.vertx.core.{ Future => VertxFuture, _ } import io.vertx.rxjava.core.{ Vertx => RxVertx } import rx.functions.Func1 import scala.concurrent.Promise import scala.util.{ Failure, Success } package object vertx { object VertxConverters { import scala.language.implicitConversions implicit def rxVertxToVertx(rxVertx: RxVertx): Vertx = rxVertx.getDelegate.asInstanceOf[Vertx] implicit def vertxToRxVertx(vertx: Vertx): RxVertx = new RxVertx(vertx) } object VertxHandlerConverters { implicit class Fn0AsHandler(fn: => Unit) { def asVertxHandler: Handler[Void] = new Handler[Void] { override def handle(event: Void): Unit = fn } } implicit class Fn1AsHandler[A](fn: A => Unit) { def asVertxHandler: Handler[A] = new Handler[A] { override def handle(event: A): Unit = fn(event) } } implicit class EventuateHandlerAsVertxHandler[A](h: EventsourcedView.Handler[A]) { def asVertxHandler: Handler[AsyncResult[A]] = new Handler[AsyncResult[A]] { override def handle(ar: AsyncResult[A]): Unit = { if (ar.succeeded()) { h(Success(ar.result())) } else { h(Failure(ar.cause())) } } } } implicit class HandlerAsEventuateHandler[A](h: Handler[AsyncResult[A]]) { def asEventuateHandler: EventsourcedView.Handler[A] = { case Success(res) => h.handle(VertxFuture.succeededFuture(res)) case Failure(err) => h.handle(VertxFuture.failedFuture(err)) } } implicit class PromiseAsVertxHandler[A](promise: Promise[A]) { def asVertxHandler: Handler[AsyncResult[A]] = new Handler[AsyncResult[A]] { override def handle(ar: AsyncResult[A]): Unit = { if (ar.succeeded()) { promise.success(ar.result()) } else { promise.failure(ar.cause()) } } } } } object RxConverters { implicit class Fn1AsRxFunc1[A, B](fn: A => B) { def asRx: Func1[A, B] = new Func1[A, B] { override def call(a: A): B = fn(a) } } } object VertxExtensions { implicit class RichMultiMap(map: MultiMap) { def getAsOption(name: String): Option[String] = Option(map.get(name)) def getOrElseThrow(name: String): String = if (map.contains(name)) { map.get(name) } else { throw new IllegalArgumentException(s"No entry for key '$name' found.") } } } }
Example 60
Source File: LeveldbDeletionActor.scala From eventuate with Apache License 2.0 | 5 votes |
package com.rbmhtechnology.eventuate.log.leveldb import java.io.Closeable import akka.actor.Actor import akka.actor.PoisonPill import akka.actor.Props import com.rbmhtechnology.eventuate.log.leveldb.LeveldbEventLog._ import org.iq80.leveldb.DB import org.iq80.leveldb.ReadOptions import org.iq80.leveldb.WriteOptions import scala.annotation.tailrec import scala.concurrent.Promise private object LeveldbDeletionActor { case object DeleteBatch def props(leveldb: DB, leveldbReadOptions: ReadOptions, leveldbWriteOptions: WriteOptions, batchSize: Int, toSequenceNr: Long, promise: Promise[Unit]): Props = Props(new LeveldbDeletionActor(leveldb, leveldbReadOptions, leveldbWriteOptions, batchSize, toSequenceNr, promise)) } private class LeveldbDeletionActor( val leveldb: DB, val leveldbReadOptions: ReadOptions, val leveldbWriteOptions: WriteOptions, batchSize: Int, toSequenceNr: Long, promise: Promise[Unit]) extends Actor with WithBatch { import LeveldbDeletionActor._ val eventKeyIterator: CloseableIterator[EventKey] = newEventKeyIterator override def preStart() = self ! DeleteBatch override def postStop() = eventKeyIterator.close() override def receive = { case DeleteBatch => withBatch { batch => eventKeyIterator.take(batchSize).foreach { eventKey => batch.delete(eventKeyBytes(eventKey.classifier, eventKey.sequenceNr)) } } if (eventKeyIterator.hasNext) { self ! DeleteBatch } else { promise.success(()) self ! PoisonPill } } private def newEventKeyIterator: CloseableIterator[EventKey] = { new Iterator[EventKey] with Closeable { val iterator = leveldb.iterator(leveldbReadOptions.snapshot(leveldb.getSnapshot)) iterator.seek(eventKeyBytes(EventKey.DefaultClassifier, 1L)) @tailrec override def hasNext: Boolean = { val key = eventKey(iterator.peekNext().getKey) key != eventKeyEnd && (key.sequenceNr <= toSequenceNr || { iterator.seek(eventKeyBytes(key.classifier + 1, 1L)) hasNext }) } override def next() = eventKey(iterator.next().getKey) override def close() = { iterator.close() leveldbReadOptions.snapshot().close() } } } }
Example 61
Source File: ClickhouseIndexingSubscriberTest.scala From clickhouse-scala-client with GNU Lesser General Public License v3.0 | 5 votes |
package com.crobox.clickhouse.stream import akka.stream.scaladsl._ import com.crobox.clickhouse.{ClickhouseClient, ClickhouseClientAsyncSpec} import org.scalatest.concurrent.{Eventually, ScalaFutures} import scala.concurrent.duration._ import scala.concurrent.{Await, Future, Promise} import scala.util.{Random, Try} class ClickhouseIndexingSubscriberTest extends ClickhouseClientAsyncSpec with ScalaFutures with Eventually { import system.dispatcher val client: ClickhouseClient = new ClickhouseClient(Some(config)) var subscriberCompletes: Promise[Unit] = Promise[Unit] val createDb = "CREATE DATABASE IF NOT EXISTS test" val dropDb = "DROP DATABASE IF EXISTS test" val createTable = """CREATE TABLE test.insert |( | i UInt64, | s String, | a Array(UInt32) |) ENGINE = Memory""".stripMargin override protected def beforeEach(): Unit = { super.beforeAll() Await.ready(for { _ <- client.execute(createDb) create <- client.execute(createTable) } yield create, timeout.duration) subscriberCompletes = Promise[Unit] } override protected def afterEach(): Unit = { super.afterEach() Await.ready(client.execute(dropDb), timeout.duration) } def unparsedInserts(key: String): Seq[Map[String, Any]] = (1 to 10).map( _ => Map( "i" -> Random.nextInt(100), "s" -> key, "a" -> (1 to Random.nextInt(20)).map(_ => Random.nextInt(200)) ) ) def parsedInserts(key: String) = unparsedInserts(key).map( _.mapValues({ case value: Int => value.toString case value: String => "\"" + value + "\"" case value: IndexedSeq[_] => "[" + value.mkString(", ") + "]" }).map { case (k, v) => s""""$k" : $v""" } .mkString(", ") ) it should "index items" in { val inserts = parsedInserts("two") val res = Source .fromIterator(() => inserts.toIterator) .map(data => Insert("test.insert", "{" + data + "}")) .runWith(ClickhouseSink.insertSink(config, client, Some("no-overrides"))) Await.ready(res, 5.seconds) checkRowCount("two").map(_ shouldBe inserts.size) } private def checkRowCount(key: String): Future[Int] = client .query(s"SELECT count(*) FROM test.insert WHERE s = '$key'") .map(res => Try(res.stripLineEnd.toInt).getOrElse(0)) }
Example 62
Source File: SqsPublishSinkGraphStageLogic.scala From akka-stream-sqs with Apache License 2.0 | 5 votes |
package me.snov.akka.sqs.shape import akka.Done import akka.stream._ import akka.stream.stage.{GraphStageLogic, InHandler} import com.amazonaws.handlers.AsyncHandler import com.amazonaws.services.sqs.model._ import me.snov.akka.sqs.client.SqsClient import scala.concurrent.Promise private[sqs] class SqsPublishSinkGraphStageLogic( client: SqsClient, in: Inlet[SendMessageRequest], shape: SinkShape[SendMessageRequest], promise: Promise[Done] ) extends GraphStageLogic(shape) with StageLogging { private val MaxInFlight = 10 private var inFlight = 0 private var isShutdownInProgress = false private var amazonSendMessageHandler: AsyncHandler[SendMessageRequest, SendMessageResult] = _ setHandler(in, new InHandler { override def onPush(): Unit = { inFlight += 1 client.sendMessageAsync(grab(in), amazonSendMessageHandler) tryPull() } @scala.throws[Exception](classOf[Exception]) override def onUpstreamFailure(exception: Throwable): Unit = { log.error(exception, "Upstream failure: {}", exception.getMessage) failStage(exception) promise.tryFailure(exception) } @scala.throws[Exception](classOf[Exception]) override def onUpstreamFinish(): Unit = { log.debug("Upstream finish") isShutdownInProgress = true tryShutdown() } }) override def preStart(): Unit = { setKeepGoing(true) val failureCallback = getAsyncCallback[Throwable](handleFailure) val sendCallback = getAsyncCallback[SendMessageResult](handleResult) amazonSendMessageHandler = new AsyncHandler[SendMessageRequest, SendMessageResult] { override def onError(exception: Exception): Unit = failureCallback.invoke(exception) override def onSuccess(request: SendMessageRequest, result: SendMessageResult): Unit = sendCallback.invoke(result) } // This requests one element at the Sink startup. pull(in) } private def tryShutdown(): Unit = if (isShutdownInProgress && inFlight <= 0) { completeStage() promise.trySuccess(Done) } private def tryPull(): Unit = if (inFlight < MaxInFlight && !isClosed(in) && !hasBeenPulled(in)) { pull(in) } private def handleFailure(exception: Throwable): Unit = { log.error(exception, "Client failure: {}", exception.getMessage) inFlight -= 1 failStage(exception) promise.tryFailure(exception) } private def handleResult(result: SendMessageResult): Unit = { log.debug(s"Sent message {}", result.getMessageId) inFlight -= 1 tryShutdown() tryPull() } }
Example 63
Source File: SqsPublishSinkShape.scala From akka-stream-sqs with Apache License 2.0 | 5 votes |
package me.snov.akka.sqs.shape import akka.Done import akka.stream._ import akka.stream.stage.{GraphStageLogic, GraphStageWithMaterializedValue} import com.amazonaws.services.sqs.model.{SendMessageRequest, SendMessageResult} import me.snov.akka.sqs.client.{SqsClient, SqsSettings} import scala.concurrent.{Future, Promise} object SqsPublishSinkShape { def apply(settings: SqsSettings): SqsPublishSinkShape = apply(SqsClient(settings)) def apply(client: SqsClient): SqsPublishSinkShape = new SqsPublishSinkShape(client) } class SqsPublishSinkShape(client: SqsClient) extends GraphStageWithMaterializedValue[SinkShape[SendMessageRequest], Future[Done]] { val in: Inlet[SendMessageRequest] = Inlet("SqsPublishSinkShape.in") override val shape: SinkShape[SendMessageRequest] = SinkShape(in) override def createLogicAndMaterializedValue(inheritedAttributes: Attributes): (GraphStageLogic, Future[Done]) = { val promise = Promise[Done]() val logic = new SqsPublishSinkGraphStageLogic(client, in, shape, promise) (logic, promise.future) } }
Example 64
Source File: SqsAckSinkShape.scala From akka-stream-sqs with Apache License 2.0 | 5 votes |
package me.snov.akka.sqs.shape import akka.Done import akka.stream._ import akka.stream.stage.GraphStageWithMaterializedValue import me.snov.akka.sqs.MessageActionPair import me.snov.akka.sqs.client.{SqsClient, SqsSettings} import scala.concurrent.{Future, Promise} object SqsAckSinkShape { def apply(settings: SqsSettings): SqsAckSinkShape = apply(SqsClient(settings)) def apply(client: SqsClient): SqsAckSinkShape = new SqsAckSinkShape(client) } class SqsAckSinkShape(client: SqsClient) extends GraphStageWithMaterializedValue[SinkShape[MessageActionPair], Future[Done]] { val in: Inlet[MessageActionPair] = Inlet("SqsAckSinkShape.in") override val shape: SinkShape[MessageActionPair] = SinkShape(in) override def createLogicAndMaterializedValue(inheritedAttributes: Attributes) = { val promise = Promise[Done]() val logic = new SqsAckSinkGraphStageLogic(client, in, shape, promise) (logic, promise.future) } }
Example 65
Source File: Streamed.scala From play-ws with Apache License 2.0 | 5 votes |
package play.api.libs.ws.ahc import java.net.URI import org.reactivestreams.Subscriber import org.reactivestreams.Subscription import org.reactivestreams.Publisher import play.shaded.ahc.io.netty.handler.codec.http.HttpHeaders import akka.Done import play.shaded.ahc.org.asynchttpclient.AsyncHandler.State import play.shaded.ahc.org.asynchttpclient._ import play.shaded.ahc.org.asynchttpclient.handler.StreamedAsyncHandler import scala.concurrent.Promise case class StreamedState( statusCode: Int = -1, statusText: String = "", uriOption: Option[URI] = None, responseHeaders: Map[String, scala.collection.Seq[String]] = Map.empty, publisher: Publisher[HttpResponseBodyPart] = EmptyPublisher ) class DefaultStreamedAsyncHandler[T]( f: java.util.function.Function[StreamedState, T], streamStarted: Promise[T], streamDone: Promise[Done] ) extends StreamedAsyncHandler[Unit] with AhcUtilities { private var state = StreamedState() def onStream(publisher: Publisher[HttpResponseBodyPart]): State = { if (this.state.publisher != EmptyPublisher) State.ABORT else { this.state = state.copy(publisher = publisher) streamStarted.success(f(state)) State.CONTINUE } } override def onStatusReceived(status: HttpResponseStatus): State = { if (this.state.publisher != EmptyPublisher) State.ABORT else { state = state.copy( statusCode = status.getStatusCode, statusText = status.getStatusText, uriOption = Option(status.getUri.toJavaNetURI) ) State.CONTINUE } } override def onHeadersReceived(h: HttpHeaders): State = { if (this.state.publisher != EmptyPublisher) State.ABORT else { state = state.copy(responseHeaders = headersToMap(h)) State.CONTINUE } } override def onBodyPartReceived(bodyPart: HttpResponseBodyPart): State = throw new IllegalStateException("Should not have received bodypart") override def onCompleted(): Unit = { // EmptyPublisher can be replaces with `Source.empty` when we carry out the refactoring // mentioned in the `execute2` method. streamStarted.trySuccess(f(state.copy(publisher = EmptyPublisher))) streamDone.trySuccess(Done) } override def onThrowable(t: Throwable): Unit = { streamStarted.tryFailure(t) streamDone.tryFailure(t) } } private case object EmptyPublisher extends Publisher[HttpResponseBodyPart] { def subscribe(s: Subscriber[_ >: HttpResponseBodyPart]): Unit = { if (s eq null) throw new NullPointerException("Subscriber must not be null, rule 1.9") s.onSubscribe(CancelledSubscription) s.onComplete() } private case object CancelledSubscription extends Subscription { override def request(elements: Long): Unit = () override def cancel(): Unit = () } }
Example 66
Source File: ConcurrentConverters.scala From mango with Apache License 2.0 | 5 votes |
package com.kakao.mango.concurrent import java.util.concurrent.{ConcurrentMap, TimeUnit, TimeoutException} import com.kakao.shaded.netty.util.{HashedWheelTimer, Timeout, TimerTask} import scala.collection.JavaConversions._ import scala.concurrent.duration._ import scala.concurrent.{Future, Promise} import scala.language.implicitConversions def timeout(duration: Duration): Future[Nothing] = { val promise = Promise[Nothing]() timer.newTimeout(new TimerTask { override def run(timeout: Timeout): Unit = { promise.failure(new TimeoutException(s"Operation was timed out after $duration")) } }, duration.toMillis, TimeUnit.MILLISECONDS) promise.future } implicit def toRichFuture[T](future: Future[T])(implicit timeout: Duration = 5.seconds): RichFuture[T] = new RichFuture[T](future, timeout) implicit def toEnsuring[K, V](map: ConcurrentMap[K, V]): EnsuringMap[K, V] = new EnsuringMap(map) implicit def toEnsuring[K, V](map: scala.collection.concurrent.Map[K, V]): EnsuringMap[K, V] = new EnsuringMap(map) }
Example 67
Source File: DruidClient.scala From XSQL with Apache License 2.0 | 5 votes |
package org.apache.spark.sql.execution.datasources.druid import com.ning.http.client.{ AsyncCompletionHandler, AsyncHttpClient, AsyncHttpClientConfig, Response } import org.json4s._ import org.json4s.jackson._ import org.json4s.jackson.JsonMethods._ import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.{Failure, Success} import org.apache.spark.internal.Logging def descTable(datasouceName: String): Seq[(String, Any)] = { val future = execute(DescTableRequest(datasouceName).toJson, DescTableResponse.parse) var data: Seq[(String, Any)] = null future.onComplete { case Success(resp) => data = resp.data case Failure(ex) => ex.printStackTrace() } while (!future.isCompleted) { Thread.sleep(500) } data } def close(): Unit = { client.close() } }
Example 68
Source File: JustinDB.scala From JustinDB with Apache License 2.0 | 5 votes |
package justin.db import akka.actor.ActorSystem import akka.cluster.Cluster import akka.cluster.http.management.ClusterHttpManagement import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.stream.{ActorMaterializer, Materializer} import buildinfo.BuildInfo import com.typesafe.scalalogging.StrictLogging import justin.db.actors.{StorageNodeActor, StorageNodeActorRef} import justin.db.client.ActorRefStorageNodeClient import justin.db.cluster.datacenter.Datacenter import justin.db.consistenthashing.{NodeId, Ring} import justin.db.replica.N import justin.db.storage.PluggableStorageProtocol import justin.db.storage.provider.StorageProvider import justin.httpapi.{BuildInfoRouter, HealthCheckRouter, HttpRouter} import scala.concurrent.duration._ import scala.concurrent.{Await, ExecutionContext, Promise} import scala.language.reflectiveCalls // $COVERAGE-OFF$ final class JustinDB object JustinDB extends StrictLogging { private[this] def validConfiguration(justinDBConfig: JustinDBConfig): Unit = { require(justinDBConfig.replication.N > 0, "replication N factor can't be smaller or equal 0") require(justinDBConfig.ring.`members-count` > 0, "members-counter can't be smaller or equal 0") require(justinDBConfig.ring.partitions > 0, "ring partitions can't be smaller or equal 0") require(justinDBConfig.ring.partitions >= justinDBConfig.ring.`members-count`, "number of ring partitions can't be smaller than number of members-count") require(justinDBConfig.replication.N <= justinDBConfig.ring.`members-count`, "replication N factor can't be bigger than defined members-count number") } private[this] def initStorage(justinConfig: JustinDBConfig) = { val provider = StorageProvider.apply(justinConfig.storage.provider) logger.info("Storage provider: " + provider.name) provider.init } def init(justinConfig: JustinDBConfig)(implicit actorSystem: ActorSystem): JustinDB = { validConfiguration(justinConfig) val processOrchestrator = Promise[JustinDB] implicit val executor: ExecutionContext = actorSystem.dispatcher implicit val materializer: Materializer = ActorMaterializer() val storage: PluggableStorageProtocol = initStorage(justinConfig) val cluster = Cluster(actorSystem) cluster.registerOnMemberUp { // STORAGE ACTOR val storageNodeActorRef = StorageNodeActorRef { val nodeId = NodeId(justinConfig.`kubernetes-hostname`.split("-").last.toInt) val ring = Ring(justinConfig.ring.`members-count`, justinConfig.ring.partitions) val n = N(justinConfig.replication.N) val datacenter = Datacenter(justinConfig.dc.`self-data-center`) actorSystem.actorOf( props = StorageNodeActor.props(nodeId, datacenter, storage, ring, n), name = StorageNodeActor.name(nodeId, datacenter) ) } // AKKA-MANAGEMENT ClusterHttpManagement(cluster).start().map { _ => logger.info("Cluster HTTP-Management is ready!") }.recover { case ex => processOrchestrator.failure(ex) } // HTTP API val routes = logRequestResult(actorSystem.name) { new HttpRouter(new ActorRefStorageNodeClient(storageNodeActorRef)).routes ~ new HealthCheckRouter().routes ~ new BuildInfoRouter().routes(BuildInfo.toJson) } Http() .bindAndHandle(routes, justinConfig.http.interface, justinConfig.http.port) .map { binding => logger.info(s"HTTP server started at ${binding.localAddress}"); processOrchestrator.trySuccess(new JustinDB) } .recover { case ex => logger.error("Could not start HTTP server", ex); processOrchestrator.failure(ex) } } Await.result(processOrchestrator.future, 2.minutes) } } // $COVERAGE-ON$
Example 69
Source File: ArrowsStdlib.scala From arrows with Apache License 2.0 | 5 votes |
package benchmarks import scala.concurrent.ExecutionContext.Implicits.global import arrows.stdlib.Arrow import arrows.stdlib.Task import org.openjdk.jmh.annotations.Benchmark import org.openjdk.jmh.annotations.Benchmark import scala.util.Try import scala.concurrent.Promise import scala.concurrent.Await import scala.concurrent.duration.Duration trait ArrowsStdlib { this: Benchmarks => private[this] final val arrowGen = ArrowsStdlibArrowGen(dist) private[this] final val taskGen = ArrowsStdlibTaskGen(dist) @Benchmark def arrowsStdlibArrow = { Try(Await.result(arrowGen.run(1), Duration.Inf)) } @Benchmark def arrowsStdlibTask = { Try(Await.result(taskGen(1).run, Duration.Inf)) } } object ArrowsStdlibTaskGen extends Gen[Int => Task[Int]] { def sync = Task.successful _ def async(schedule: Runnable => Unit) = { v => val p = Promise[Int]() schedule(() => p.success(v)) Task.async(p.future) } def failure(ex: Throwable) = v => Task.failed(ex) def map(t: Int => Task[Int], f: Int => Int) = t.andThen(_.map(f)) def flatMap(t: Int => Task[Int], f: Int => Task[Int]) = t.andThen(_.flatMap(f)) def handle(t: Int => Task[Int], i: Int) = t.andThen(_.recover { case _ => i }) } object ArrowsStdlibArrowGen extends Gen[Arrow[Int, Int]] { def sync = Arrow[Int] def async(schedule: Runnable => Unit) = Arrow[Int].flatMap { v => val p = Promise[Int]() schedule(() => p.success(v)) Task.async(p.future) } def failure(ex: Throwable) = Arrow.failed(ex) def map(t: Arrow[Int, Int], f: Int => Int) = t.map(f) def flatMap(t: Arrow[Int, Int], f: Arrow[Int, Int]) = t.flatMap(f) def handle(t: Arrow[Int, Int], i: Int) = t.recover { case _ => i } }
Example 70
Source File: ScalaFuture.scala From arrows with Apache License 2.0 | 5 votes |
package benchmarks import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future import scala.concurrent.Promise import org.openjdk.jmh.annotations.Benchmark import scala.util.Try trait ScalaFuture { this: Benchmarks => private[this] final val sFut = ScalaFutureGen(dist) @Benchmark def scalaFuture = { import scala.concurrent._ import scala.concurrent.duration._ Try(Await.result(sFut(1), Duration.Inf)) } } object ScalaFutureGen extends Gen[Int => Future[Int]] { def sync = Future.successful _ def async(schedule: Runnable => Unit) = { v => val p = Promise[Int]() schedule(() => p.success(v)) p.future } def failure(ex: Throwable) = _ => Future.failed(ex) def map(t: Int => Future[Int], f: Int => Int) = t.andThen(_.map(f)) def flatMap(t: Int => Future[Int], f: Int => Future[Int]) = t.andThen(_.flatMap(f)) def handle(t: Int => Future[Int], i: Int) = t.andThen(_.recover { case _ => i }) }
Example 71
Source File: UsesServerRPC.scala From udash-core with Apache License 2.0 | 5 votes |
package io.udash.rpc.internals import com.avsystem.commons.SharedExtensions._ import io.udash.rpc._ import io.udash.utils.{CallbacksHandler, Registration} import org.scalajs.dom import scala.concurrent.duration.{Duration, DurationInt} import scala.concurrent.{Future, Promise} import scala.scalajs.js import scala.scalajs.js.Dictionary def onCallFailure(callback: exceptionCallbacks.CallbackType): Registration = exceptionCallbacks.register(callback) private def handleException(ex: Throwable): Unit = exceptionCallbacks.fire(ex) def handleResponse(response: RpcResponse): Unit = { pendingCalls.remove(response.callId) .foreach { promise => response match { case RpcResponseSuccess(r, _) => promise.success(r) case RpcResponseException(_, exception, _) => handleException(exception) promise.failure(exception) case RpcResponseFailure(cause, error, _) => val exception = RpcFailure(cause, error) handleException(exception) promise.failure(exception) } } } override protected[rpc] def fireRemote(getterChain: List[RpcInvocation], invocation: RpcInvocation): Unit = sendRpcRequest(RpcFire(invocation, getterChain)) protected[rpc] def callRemote(callId: String, getterChain: List[RpcInvocation], invocation: RpcInvocation): Unit = sendRpcRequest(RpcCall(invocation, getterChain, callId)) private def sendRpcRequest(request: RpcRequest): Unit = connector.sendRpcRequest(request) protected class RawRemoteRPC(getterChain: List[RpcInvocation]) extends ServerRawRpc { def fire(invocation: RpcInvocation): Unit = fireRemote(getterChain, invocation) def call(invocation: RpcInvocation): Future[JsonStr] = Promise[JsonStr]().setup { promise => val callId = newCallId() callRemote(callId, getterChain, invocation) pendingCalls.put(callId, promise) dom.window.setTimeout( () => handleResponse(RpcResponseException("Request timeout", UsesServerRPC.CallTimeout(callTimeout), callId)), callTimeout.toMillis.toDouble ) }.future def get(invocation: RpcInvocation): ServerRawRpc = new RawRemoteRPC(invocation :: getterChain) } } object UsesServerRPC { case class CallTimeout(callTimeout: Duration) extends RuntimeException(s"Response missing after $callTimeout.") }
Example 72
Source File: AsyncUdashSharedTest.scala From udash-core with Apache License 2.0 | 5 votes |
package io.udash.testing import org.scalactic.source.Position import org.scalajs.dom import org.scalatest.{Assertion, Succeeded} import scala.concurrent.{ExecutionContext, Future, Promise} import scala.scalajs.concurrent.JSExecutionContext import scala.scalajs.js.Date import scala.util.{Failure, Success} trait AsyncUdashSharedTest extends AsyncUdashSharedTestBase { override implicit def executionContext: ExecutionContext = JSExecutionContext.queue override def retrying(code: => Any)(implicit patienceConfig: PatienceConfig, pos: Position): Future[Assertion] = { val start = Date.now() val p = Promise[Assertion] var lastEx: Option[Throwable] = None def startTest(): Unit = { dom.window.setTimeout(() => { if (patienceConfig.timeout.toMillis > Date.now() - start) { try { code p.complete(Success(Succeeded)) } catch { case ex: Throwable => lastEx = Some(ex) startTest() } } else { p.complete(Failure(lastEx.getOrElse(RetryingTimeout()))) } }, patienceConfig.interval.toMillis.toDouble) } startTest() p.future } }
Example 73
Source File: instances.scala From cats-retry with Apache License 2.0 | 5 votes |
package retry package alleycats import cats.{Eval, Id} import scala.concurrent.duration.FiniteDuration import scala.concurrent.{Future, Promise} import java.util.concurrent.{ThreadFactory, Executors} object instances { implicit val threadSleepId: Sleep[Id] = new Sleep[Id] { def sleep(delay: FiniteDuration): Id[Unit] = Thread.sleep(delay.toMillis) } implicit val threadSleepEval: Sleep[Eval] = new Sleep[Eval] { def sleep(delay: FiniteDuration): Eval[Unit] = Eval.later(Thread.sleep(delay.toMillis)) } private lazy val scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory { override def newThread(runnable: Runnable) = { val t = new Thread(runnable) t.setDaemon(true) t.setName("cats-retry scheduler") t } }) implicit val threadSleepFuture: Sleep[Future] = new Sleep[Future] { def sleep(delay: FiniteDuration): Future[Unit] = { val promise = Promise[Unit]() scheduler.schedule(new Runnable { def run: Unit = { promise.success(()) () } }, delay.length, delay.unit) promise.future } } }
Example 74
Source File: UsbHidExchangePerformer.scala From ledger-manager-chrome with MIT License | 5 votes |
package co.ledger.manager.web.core.device.usb import co.ledger.wallet.core.device.Device.CommunicationException import co.ledger.wallet.core.utils.HexUtils import co.ledger.wallet.core.utils.logs.{Loggable, Logger} import co.ledger.manager.web.core.device.LedgerTransportHelper import co.ledger.manager.web.core.device.usb.UsbDeviceImpl.UsbExchangePerformer import scala.collection.mutable.ArrayBuffer import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.{Future, Promise} import scala.scalajs.js import scala.scalajs.js.UndefOr import scala.util.{Failure, Success} class UsbHidExchangePerformer(connection: UsbDeviceImpl.Connection, var debug: Boolean, var useLedgerTransport: Boolean ) extends UsbExchangePerformer with Loggable { override implicit val LogTag: String = "APDU" val HidBufferSize = 64 val LedgerDefaultChannel = 0x1000 val Sw1DataAvailable = 0x61 private val chrome = js.Dynamic.global.chrome override def close(): Unit = { chrome.hid.disconnect(connection.connectionId) } override def performExchange(cmd: Array[Byte]): Future[Array[Byte]] = { var command = cmd if (useLedgerTransport) { command = LedgerTransportHelper.wrapCommandAPDU(LedgerDefaultChannel, cmd, HidBufferSize) } def sendBlocks(offset: Int = 0): Future[Unit] = { val blockSize = if (command.length - offset > HidBufferSize) HidBufferSize else command.length - offset System.arraycopy(command, offset, _transferBuffer, 0, blockSize) send(_transferBuffer) flatMap {(_) => if (offset + blockSize < command.length) sendBlocks(offset + blockSize) else Future.successful() } } def receiveLegacyBlock(buffer: ArrayBuffer[Byte]): Future[Array[Byte]] = { null } def receiveLedgerBlock(buffer: ArrayBuffer[Byte]): Future[Array[Byte]] = { receive().flatMap {(response) => buffer ++= response val responseData = LedgerTransportHelper.unwrapResponseAPDU(LedgerDefaultChannel, buffer.toArray, HidBufferSize) if (responseData == null) { receiveLedgerBlock(buffer) } else { Future.successful(responseData) } } } def receiveBlocks(buffer: ArrayBuffer[Byte] = ArrayBuffer.empty[Byte]): Future[Array[Byte]] = { if (useLedgerTransport) receiveLedgerBlock(buffer) else receiveLegacyBlock(buffer) } sendBlocks().flatMap((_) => receiveBlocks()) andThen { case Success(result) => case Failure(ex) => ex.printStackTrace() } } private def send(bytes: Array[Byte]): Future[Unit] = { import scala.scalajs.js.typedarray._ val promise = Promise[Unit]() chrome.hid.send(connection.connectionId, 0, byteArray2Int8Array(bytes).buffer, { () => if (js.isUndefined(chrome.runtime.lastError)) promise.success() else promise.failure(CommunicationException(chrome.runtime.lastError.message.toString)) }) promise.future } private def receive(): Future[Array[Byte]] = { import scala.scalajs.js.typedarray._ val promise = Promise[Array[Byte]]() chrome.hid.receive(connection.connectionId, {(reportId: UndefOr[Int], data: TypedArray[_, _]) => if (js.isUndefined(chrome.runtime.lastError)) promise.success(int8Array2ByteArray(new Int8Array(data))) else promise.failure(CommunicationException(chrome.runtime.lastError.message.toString)) }) promise.future } private val _transferBuffer = new Array[Byte](HidBufferSize) }
Example 75
Source File: IndexedDb.scala From ledger-manager-chrome with MIT License | 5 votes |
package co.ledger.manager.web.core.idb import org.scalajs.dom.{ErrorEvent, Event, idb} import org.scalajs.dom.raw.IDBVersionChangeEvent import scala.concurrent.{Future, Promise} import scala.scalajs.js object IndexedDb { def open(databaseName: String, version: Option[Int] = Some(1))(upgradeHandler: (idb.Database, idb.Transaction) => Unit): Future[idb.Database] = { val promise = Promise[idb.Database]() val request = version match { case Some(v) => factory.open(databaseName, v) case None => factory.open(databaseName) } request.onupgradeneeded = {(event: IDBVersionChangeEvent) => val db = event.target.asInstanceOf[js.Dynamic].result.asInstanceOf[idb.Database] val transaction = event.currentTarget.asInstanceOf[js.Dynamic].transaction.asInstanceOf[idb.Transaction] upgradeHandler(db, transaction) } request.onblocked = {(event: Event) => } request.onsuccess = {(event: Event) => val db = event.target.asInstanceOf[js.Dynamic].result.asInstanceOf[idb.Database] promise.success(db) } request.onerror = {(event: ErrorEvent) => promise.failure(new Exception(event.message)) } promise.future } def delete(databaseName: String) = { factory.deleteDatabase(databaseName) } private def factory = js.Dynamic.global.indexedDB.asInstanceOf[idb.Factory] }
Example 76
Source File: AckedFlow.scala From reactive-activemq with Apache License 2.0 | 5 votes |
package akka.stream.integration package activemq import akka.actor.ActorRef import akka.camel.CamelMessage import akka.stream._ import akka.stream.stage.{ GraphStage, GraphStageLogic, InHandler, OutHandler } import scala.concurrent.{ ExecutionContext, Future, Promise } private[activemq] class AckedFlow[A, B](implicit ec: ExecutionContext) extends GraphStage[FlowShape[(ActorRef, B), AckTup[A, B]]] { val in = Inlet[(ActorRef, B)]("AckedFlow.in") val out = Outlet[AckTup[A, B]]("AckedFlow.out") override val shape: FlowShape[(ActorRef, B), AckTup[A, B]] = FlowShape.of(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) { var promises = Vector.empty[(Promise[A], Future[A])] setHandler(in, new InHandler { override def onPush(): Unit = { val (ref, b) = grab(in) val p = Promise[A]() val eventualResponse = p.future eventualResponse.onSuccess(successResponse(ref)) eventualResponse.onFailure { case cause: Throwable => ref ! akka.actor.Status.Failure(cause) } promises = promises.filterNot(_._1.isCompleted) :+ (p -> eventualResponse) push(out, p -> b) } }) setHandler(out, new OutHandler { override def onPull(): Unit = { pull(in) } }) } def successResponse(source: ActorRef): PartialFunction[A, Unit] = { case _ => source ! akka.camel.Ack } } class AckedResponseFlow[A, B](implicit ec: ExecutionContext, builder: MessageBuilder[A, CamelMessage]) extends AckedFlow[A, B] { override def successResponse(source: ActorRef): PartialFunction[A, Unit] = { case msg => source ! builder.build(msg) } }
Example 77
Source File: PromiseTest.scala From reactive-activemq with Apache License 2.0 | 5 votes |
package akka.stream.integration package promise import scala.concurrent.{ Future, Promise } class PromiseTest extends TestSpec { def withPromise[A]()(fn: (Promise[A], Future[A]) => Unit): Unit = { val p: Promise[A] = Promise[A]() val f: Future[A] = p.future fn(p, f) } "a promise" should "be completed successfully" in withPromise[Int]() { (p, f) => p success 1 p shouldBe 'completed f.futureValue shouldBe 1 p shouldBe 'completed } it should "not be completed multiple times" in withPromise[Int]() { (p, f) => p success 1 p shouldBe 'completed intercept[IllegalStateException] { p success 2 } } it should "not be completed with a success and then with a failure" in withPromise[Int]() { (p, f) => p failure new RuntimeException("Test failure") p shouldBe 'completed f.toTry should be a 'failure intercept[IllegalStateException] { p success 2 } } it should "be completed with a failure" in withPromise[Int]() { (p, f) => p failure new RuntimeException("Test failure") p shouldBe 'completed f.toTry should be a 'failure } it should "not be completed with a failure and then with a success" in withPromise[Int]() { (p, f) => p failure new RuntimeException("Test failure") p shouldBe 'completed intercept[IllegalStateException] { p success 2 } } }
Example 78
Source File: SeqPromisesTest.scala From reactive-activemq with Apache License 2.0 | 5 votes |
package akka.stream.integration package promise import scala.collection.immutable.Seq import scala.concurrent.{ Future, Promise } import scala.util.Try class SeqPromisesTest extends TestSpec { def withPromise[T, U](complete: Try[T] => U, failure: PartialFunction[Throwable, U]): (Promise[T], Future[T]) = { val p = Promise[T]() val f = p.future f.onComplete(complete) f.onFailure(failure) p -> f } def withPromises()(f: Seq[(Promise[Unit], Future[Unit])] => Unit): Unit = f(Seq( withPromise((_: Try[Unit]) => (), PartialFunction.empty), withPromise((_: Try[Unit]) => (), PartialFunction.empty), withPromise((_: Try[Unit]) => (), PartialFunction.empty) )) it should "complete a promise" in withPromises() { xs => xs.head._1.success(()) xs.head._2.futureValue shouldBe () xs.filterNot(_._1.isCompleted).size shouldBe 2 } it should "complete multiple promises" in withPromises() { xs => xs.zipWithIndex.foreach { case ((p, f), 0) => p success (); f.futureValue shouldBe () case ((p, f), 1) => p success (); f.futureValue shouldBe () case ((p, f), 2) => p success (); f.futureValue shouldBe () } } }
Example 79
Source File: ActiveMqFlowTest.scala From reactive-activemq with Apache License 2.0 | 5 votes |
package akka.stream.integration package activemq import akka.stream.integration.PersonDomain.Person import akka.stream.scaladsl.Flow import scala.concurrent.Promise class ActiveMqFlowTest extends ActiveMqTestSpec { behavior of "ActiveMqFlow" it should "propagate messages from input to output unmodified, if mediated by the identity flow" in { withTestTopicPublisher("AckBidiFlowTestInput") { pub => withTestTopicSubscriber("AckBidiFlowTestOutput") { sub => withActiveMqBidiFlow("AckBidiFlowTestInput", "AckBidiFlowTestOutput") { flow => val identityFlow = Flow[Person].map(identity) val ref = flow.join(identityFlow).run() pub.sendNext(testPerson1) sub.request(1) sub.expectNextPF { case (p: Promise[Unit], `testPerson1`) => p.success(()) } pub.sendNext(testPerson2) sub.request(1) sub.expectNextPF { case (p: Promise[Unit], `testPerson2`) => p.success(()) } pub.sendComplete() sub.cancel() ref } } } } }
Example 80
Source File: ActiveMqProducerTest.scala From reactive-activemq with Apache License 2.0 | 5 votes |
package akka.stream.integration package activemq import akka.stream.integration.PersonDomain.Person import akka.stream.scaladsl.{ Keep, Source } import scala.concurrent.Promise import scala.concurrent.duration._ class ActiveMqProducerTest extends TestSpec { it should "produce messages to a queue" in { withTestTopicSubscriber() { sub => withTestTopicPublisher() { pub => pub.sendNext(testPerson1) pub.sendComplete() sub.request(1) sub.expectNextPF { case (p: Promise[Unit], `testPerson1`) => p.success(()) } sub.expectNoMsg(500.millis) sub.cancel() } } } it should "produce multiple messages to a queue" in { withTestTopicSubscriber() { sub => withTestTopicPublisher() { pub => (0 to 10).foreach { _ => pub.sendNext(testPerson1) sub.request(1) sub.expectNextPF { case (p: Promise[Unit], `testPerson1`) => p.success(()) } } pub.sendComplete() sub.cancel() } } } it should "send 250 messages to the queue" in { import PersonDomain._ val numberOfPersons = 250 Source.repeat(testPerson1).take(numberOfPersons).runWith(ActiveMqProducer("PersonProducer")).toTry should be a 'success } it should "send and receive 250 messages from the queue" in { val numberOfPersons = 250 Source.repeat(testPerson1).take(numberOfPersons).runWith(ActiveMqProducer[Person]("PersonProducer")).toTry should be a 'success val (ref, fxs) = ActiveMqConsumer[Person]("PersonConsumer").take(numberOfPersons).toMat(AckSink.seq)(Keep.both).run() fxs.toTry should be a 'success terminateEndpoint(ref) } }
Example 81
Source File: ActiveMqProducerSourceTest.scala From reactive-activemq with Apache License 2.0 | 5 votes |
package akka.stream.integration package activemq import scala.concurrent.Promise class ActiveMqProducerSourceTest extends TestSpec { it should "consume messages from the queue" in { withTestTopicSubscriber() { sub => withTestTopicPublisher() { pub => pub.sendNext(testPerson1) pub.sendComplete() sub.request(1) sub.expectNextPF { case (p: Promise[Unit], `testPerson1`) => p.success(()) } sub.cancel() } } } }
Example 82
Source File: ActiveMqReqRespFlowTest.scala From reactive-activemq with Apache License 2.0 | 5 votes |
package akka.stream.integration package activemq import akka.actor.ActorRef import akka.stream.integration.PersonDomain.Person import scala.concurrent.Promise class ActiveMqReqRespFlowTest extends ActiveMqTestSpec { behavior of "ActiveMqReqRespFlow" it should "support request-response for a single message" in { withBackendFlow { implicit backendFlow => flowProbe => withReqRespBidiFlow("AckBidiFlowReqRespTestInput") { testFlow => var ref: ActorRef = null withTestTopicPublisher("AckBidiFlowReqRespTestInput") { pub => withTestTopicSubscriber("AckBidiFlowReqRespTestOutput") { sub => // echo all received messages flowProbe.setAutoPilot(identity[Person] _) ref = testFlow.join(backendFlow).run() sub.request(2) pub.sendNext(testPerson1) sub.expectNextPF { case (p: Promise[Unit], `testPerson1`) => p.success(()) } sub.cancel() pub.sendComplete() } } ref } } } it should "support request-response for a multiple messages" in { withBackendFlow { implicit backendFlow => flowProbe => withReqRespBidiFlow("AckBidiFlowReqRespTestInput") { testFlow => var ref: ActorRef = null withTestTopicPublisher("AckBidiFlowReqRespTestInput") { pub => withTestTopicSubscriber("AckBidiFlowReqRespTestOutput") { sub => // echo all received messages flowProbe.setAutoPilot(identity[Person] _) ref = testFlow.join(backendFlow).run() sub.request(2) pub.sendNext(testPerson1) sub.expectNextPF { case (p: Promise[Unit], `testPerson1`) => p.success(()) } pub.sendNext(testPerson2) sub.expectNextPF { case (p: Promise[Unit], `testPerson2`) => p.success(()) } sub.cancel() pub.sendComplete() } } ref } } } }
Example 83
Source File: KafkaProducerConnector.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.connector.kafka import akka.actor.ActorSystem import akka.pattern.after import org.apache.kafka.clients.producer._ import org.apache.kafka.common.errors._ import org.apache.kafka.common.serialization.StringSerializer import pureconfig._ import pureconfig.generic.auto._ import org.apache.openwhisk.common.{Counter, Logging, TransactionId} import org.apache.openwhisk.connector.kafka.KafkaConfiguration._ import org.apache.openwhisk.core.ConfigKeys import org.apache.openwhisk.core.connector.{Message, MessageProducer} import org.apache.openwhisk.core.entity.{ByteSize, UUIDs} import org.apache.openwhisk.utils.Exceptions import scala.collection.JavaConverters._ import scala.concurrent.duration._ import scala.concurrent.{blocking, ExecutionContext, Future, Promise} import scala.util.{Failure, Success} class KafkaProducerConnector( kafkahosts: String, id: String = UUIDs.randomUUID().toString, maxRequestSize: Option[ByteSize] = None)(implicit logging: Logging, actorSystem: ActorSystem) extends MessageProducer with Exceptions { implicit val ec: ExecutionContext = actorSystem.dispatcher private val gracefulWaitTime = 100.milliseconds override def sentCount(): Long = sentCounter.cur override def close(): Unit = { logging.info(this, "closing producer") producer.close() } private val sentCounter = new Counter() private def createProducer(): KafkaProducer[String, String] = { val config = Map(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG -> kafkahosts) ++ configMapToKafkaConfig(loadConfigOrThrow[Map[String, String]](ConfigKeys.kafkaCommon)) ++ configMapToKafkaConfig(loadConfigOrThrow[Map[String, String]](ConfigKeys.kafkaProducer)) ++ (maxRequestSize map { max => Map("max.request.size" -> max.size.toString) } getOrElse Map.empty) verifyConfig(config, ProducerConfig.configNames().asScala.toSet) tryAndThrow("creating producer")(new KafkaProducer(config, new StringSerializer, new StringSerializer)) } private def recreateProducer(): Unit = { logging.info(this, s"recreating producer") tryAndSwallow("closing old producer")(producer.close()) logging.info(this, s"old producer closed") producer = createProducer() } @volatile private var producer = createProducer() }
Example 84
Source File: PoolingRestClient.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.http import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.marshalling._ import akka.http.scaladsl.model._ import akka.http.scaladsl.settings.ConnectionPoolSettings import akka.http.scaladsl.unmarshalling._ import akka.stream.{ActorMaterializer, OverflowStrategy, QueueOfferResult} import akka.stream.scaladsl.{Flow, _} import spray.json._ import scala.concurrent.{ExecutionContext, Future, Promise} import scala.concurrent.duration._ import scala.util.{Failure, Success, Try} def requestJson[T: RootJsonReader](futureRequest: Future[HttpRequest]): Future[Either[StatusCode, T]] = request(futureRequest).flatMap { response => if (response.status.isSuccess) { Unmarshal(response.entity.withoutSizeLimit).to[T].map(Right.apply) } else { Unmarshal(response.entity).to[String].flatMap { body => val statusCode = response.status val reason = if (body.nonEmpty) s"${statusCode.reason} (details: $body)" else statusCode.reason val customStatusCode = StatusCodes .custom(intValue = statusCode.intValue, reason = reason, defaultMessage = statusCode.defaultMessage) // This is important, as it drains the entity stream. // Otherwise the connection stays open and the pool dries up. response.discardEntityBytes().future.map(_ => Left(customStatusCode)) } } } def shutdown(): Future[Unit] = Future.successful(materializer.shutdown()) } object PoolingRestClient { def mkRequest(method: HttpMethod, uri: Uri, body: Future[MessageEntity] = Future.successful(HttpEntity.Empty), headers: List[HttpHeader] = List.empty)(implicit ec: ExecutionContext): Future[HttpRequest] = { body.map { b => HttpRequest(method, uri, headers, b) } } def mkJsonRequest(method: HttpMethod, uri: Uri, body: JsValue, headers: List[HttpHeader] = List.empty)( implicit ec: ExecutionContext): Future[HttpRequest] = { val b = Marshal(body).to[MessageEntity] mkRequest(method, uri, b, headers) } }
Example 85
Source File: RxObservableImplicits.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.database.cosmosdb import com.microsoft.azure.cosmosdb.{FeedResponse, Resource, ResourceResponse} import rx.Observable import rx.functions.Action1 import scala.collection.JavaConverters._ import scala.concurrent.{Future, Promise} private[cosmosdb] trait RxObservableImplicits { implicit class RxScalaObservable[T](observable: Observable[T]) { def head(): Future[T] = { def toHandler[P](f: (P) => Unit): Action1[P] = (t: P) => f(t) val promise = Promise[T]() observable.single.subscribe(toHandler(promise.success), toHandler(promise.failure)) promise.future } } implicit class RxScalaResourceObservable[T <: Resource](observable: Observable[ResourceResponse[T]]) { def blockingResult(): T = observable.toBlocking.single.getResource } implicit class RxScalaFeedObservable[T <: Resource](observable: Observable[FeedResponse[T]]) { def blockingOnlyResult(): Option[T] = { val value = observable.toBlocking.single val results = value.getResults.asScala require(results.isEmpty || results.size == 1, s"More than one result found $results") results.headOption } } }
Example 86
Source File: KafkaEventProducer.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.database.cosmosdb.cache import akka.Done import akka.actor.ActorSystem import akka.kafka.scaladsl.Producer import akka.kafka.{ProducerMessage, ProducerSettings} import akka.stream.scaladsl.{Keep, Sink, Source} import akka.stream.{ActorMaterializer, OverflowStrategy, QueueOfferResult} import org.apache.kafka.clients.consumer.ConsumerConfig import org.apache.kafka.clients.producer.ProducerRecord import org.apache.openwhisk.connector.kafka.KamonMetricsReporter import scala.collection.immutable.Seq import scala.concurrent.{ExecutionContext, Future, Promise} case class KafkaEventProducer( settings: ProducerSettings[String, String], topic: String, eventProducerConfig: EventProducerConfig)(implicit system: ActorSystem, materializer: ActorMaterializer) extends EventProducer { private implicit val executionContext: ExecutionContext = system.dispatcher private val queue = Source .queue[(Seq[String], Promise[Done])](eventProducerConfig.bufferSize, OverflowStrategy.dropNew) //TODO Use backpressure .map { case (msgs, p) => ProducerMessage.multi(msgs.map(newRecord), p) } .via(Producer.flexiFlow(producerSettings)) .map { case ProducerMessage.MultiResult(_, passThrough) => passThrough.success(Done) case _ => //As we use multi mode only other modes need not be handled } .toMat(Sink.ignore)(Keep.left) .run override def send(msg: Seq[String]): Future[Done] = { val promise = Promise[Done] queue.offer(msg -> promise).flatMap { case QueueOfferResult.Enqueued => promise.future case QueueOfferResult.Dropped => Future.failed(new Exception("Kafka request queue is full.")) case QueueOfferResult.QueueClosed => Future.failed(new Exception("Kafka request queue was closed.")) case QueueOfferResult.Failure(f) => Future.failed(f) } } def close(): Future[Done] = { queue.complete() queue.watchCompletion() } private def newRecord(msg: String) = new ProducerRecord[String, String](topic, "messages", msg) private def producerSettings = settings.withProperty(ConsumerConfig.METRIC_REPORTER_CLASSES_CONFIG, KamonMetricsReporter.name) }
Example 87
Source File: MultiRepository.scala From mleap with Apache License 2.0 | 5 votes |
package ml.combust.mleap.executor.repository import java.net.URI import java.nio.file.Path import java.util.concurrent.TimeUnit import akka.actor.ActorSystem import com.typesafe.config.Config import ml.combust.mleap.executor.error.BundleException import scala.collection.JavaConverters._ import scala.concurrent.{Await, Future, Promise} import scala.concurrent.duration._ import scala.util.Try class MultiRepository(repositories: Seq[Repository]) extends Repository { val terminatePromise: Promise[Unit] = Promise[Unit] new Thread { override def run(): Unit = { terminatePromise.complete(Try { for (repository <- repositories) { repository.awaitTermination(Long.MaxValue, TimeUnit.DAYS) } }) } }.start() override def downloadBundle(uri: URI): Future[Path] = { for (repository <- repositories) { if (repository.canHandle(uri)) return repository.downloadBundle(uri) } Future.failed(new BundleException("could not find a repository to download the bundle file")) } override def canHandle(uri: URI): Boolean = { for (repository <- repositories) { if (repository.canHandle(uri)) return true } false } override def shutdown(): Unit = { for (repository <- repositories) { repository.shutdown() } } override def awaitTermination(timeout: Long, unit: TimeUnit): Unit = synchronized { Try(Await.ready(terminatePromise.future, FiniteDuration(timeout, unit))) } } object MultiRepositoryProvider extends RepositoryProvider { override def create(config: Config) (implicit system: ActorSystem): MultiRepository = { val rConfigs = config.getConfigList("repositories") val repositories = for (rConfig <- rConfigs.asScala) yield { Repository.fromConfig(rConfig) } new MultiRepository(repositories) } }
Example 88
Source File: TypeConverters.scala From mleap with Apache License 2.0 | 5 votes |
package ml.combust.mleap.springboot import java.net.URI import java.util.concurrent.TimeUnit import com.google.protobuf.ProtocolStringList import ml.combust.mleap.executor import ml.combust.mleap.pb._ import scala.concurrent.{ExecutionContext, Future, Promise} import scala.concurrent.duration.FiniteDuration import scala.concurrent.duration._ import scala.util.Try import ml.combust.mleap.runtime.types.BundleTypeConverters._ object TypeConverters { import scala.language.implicitConversions implicit def getTimeout(ms: Int): FiniteDuration = FiniteDuration(ms, TimeUnit.MILLISECONDS) implicit def pbToExecutorLoadModelRequest(request: LoadModelRequest): executor.LoadModelRequest = executor.LoadModelRequest(modelName = request.modelName, uri = URI.create(request.uri), config = request.config.map(pbToExecutorModelConfig), force = request.force) implicit def javaPbToExecutorLoadModelRequest(request: Mleap.LoadModelRequest): executor.LoadModelRequest = { val modelConfig = Option(request.getConfig).map(javaPbToExecutorModelConfig) executor.LoadModelRequest(modelName = request.getModelName, uri = URI.create(request.getUri), config = modelConfig, force = request.getForce) } implicit def pbToExecutorModelConfig(config: ModelConfig): executor.ModelConfig = { executor.ModelConfig(memoryTimeout = config.memoryTimeout.map(_.millis), diskTimeout = config.diskTimeout.map(_.millis)) } implicit def javaPbToExecutorModelConfig(config: Mleap.ModelConfig): executor.ModelConfig = { executor.ModelConfig(memoryTimeout = Option(config.getMemoryTimeout).map(_.getValue.millis), diskTimeout = Option(config.getDiskTimeout).map(_.getValue.millis)) } implicit def executorToPbModelConfig(config: executor.ModelConfig): ModelConfig = ModelConfig(memoryTimeout = config.memoryTimeout.map(_.toMillis), diskTimeout = config.diskTimeout.map(_.toMillis)) implicit def executorToPbModel(model: executor.Model): Model = Model(name = model.name, uri = model.uri.toString, config = Some(model.config)) implicit def pbToExecutorModel(model: Model): executor.Model = executor.Model(name = model.name, uri = URI.create(model.uri), config = model.config.get) implicit def executorToPbBundleMeta(meta: executor.BundleMeta): BundleMeta = BundleMeta(bundle = Some(meta.info.asBundle), inputSchema = Some(meta.inputSchema), outputSchema = Some(meta.outputSchema)) implicit def pbToExecutorTransformOptions(options: TransformOptions): executor.TransformOptions = executor.TransformOptions(select = options.select, selectMode = options.selectMode) implicit def javaPbToExecutorTransformOptions(options: Mleap.TransformOptions): executor.TransformOptions = executor.TransformOptions(select = options.getSelectList, selectMode = options.getSelectMode) implicit def javaPbToExecutorSelectMode(sm: Mleap.SelectMode): executor.SelectMode = if (sm == Mleap.SelectMode.SELECT_MODE_RELAXED) executor.SelectMode.Relaxed else if (sm == Mleap.SelectMode.SELECT_MODE_STRICT) executor.SelectMode.Strict else executor.SelectMode.Strict implicit def javaPbToExecutorSelect(select: ProtocolStringList): Option[Seq[String]] = if (select.isEmpty) None else Some(select.toArray().map(_.asInstanceOf[String]).toSeq) implicit def pbToExecutorSelectMode(sm: SelectMode): executor.SelectMode = if (sm.isSelectModeRelaxed) executor.SelectMode.Relaxed else if (sm.isSelectModeStrict) executor.SelectMode.Strict else executor.SelectMode.Strict implicit def pbToExecutorSelect(select: Seq[String]): Option[Seq[String]] = if (select.isEmpty) None else Some(select) implicit class RichFuture[T](f: Future[T]) { def mapAll[U](pf: PartialFunction[Try[T], U])(implicit executor: ExecutionContext): Future[U] = { val p = Promise[U]() f.onComplete(r => p.complete(Try(pf(r))))(executor) p.future } } }
Example 89
Source File: package.scala From BusFloatingData with Apache License 2.0 | 5 votes |
package de.nierbeck.floating.data import com.google.common.util.concurrent.{FutureCallback, Futures, ListenableFuture} import de.nierbeck.floating.data.domain.{BoundingBox, LatLon} import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.{Failure, Success, Try} package object server { val Traversable = scala.collection.immutable.Traversable type Traversable[+A] = scala.collection.immutable.Traversable[A] val Iterable = scala.collection.immutable.Iterable type Iterable[+A] = scala.collection.immutable.Iterable[A] val Seq = scala.collection.immutable.Seq type Seq[+A] = scala.collection.immutable.Seq[A] val IndexedSeq = scala.collection.immutable.IndexedSeq type IndexedSeq[+A] = scala.collection.immutable.IndexedSeq[A] def futureToFutureTry[T](f: Future[T])(implicit ec: ExecutionContext): Future[Try[T]] = f.map(Success(_)).recover { case exception: Exception => Failure(exception) } implicit class RichListenableFuture[T](lf: ListenableFuture[T]) { def toFuture: Future[T] = { val p = Promise[T]() Futures.addCallback(lf, new FutureCallback[T] { def onFailure(t: Throwable): Unit = p failure t def onSuccess(result: T): Unit = p success result }) p.future } } def toBoundingBox(bbox: String): BoundingBox = { val bboxCoords: Array[String] = bbox.split(",") val boundingBox: BoundingBox = new BoundingBox(LatLon(bboxCoords(0).toFloat, bboxCoords(1).toFloat), LatLon(bboxCoords(2).toFloat, bboxCoords(3).toFloat)) boundingBox } }
Example 90
Source File: AirSpecTask.scala From airframe with Apache License 2.0 | 5 votes |
package wvlet.airspec.runner import sbt.testing._ import wvlet.airframe.Design import wvlet.airspec.runner.AirSpecSbtRunner.AirSpecConfig import wvlet.log.LogSupport import scala.concurrent.duration.Duration import scala.concurrent.{Await, Promise} def execute( eventHandler: EventHandler, loggers: Array[sbt.testing.Logger], continuation: Array[sbt.testing.Task] => Unit ): Unit = { try { new AirSpecTaskRunner(taskDef, config, taskLogger, eventHandler, classLoader).runTask } finally { continuation(Array.empty) } } }
Example 91
Source File: FutureTimeoutSupport.scala From perf_tester with Apache License 2.0 | 5 votes |
package akka.pattern import scala.concurrent.{ ExecutionContext, Promise, Future } import akka.actor._ import scala.util.control.NonFatal import scala.concurrent.duration.FiniteDuration import java.util.concurrent.CompletionStage import java.util.concurrent.CompletableFuture import akka.dispatch.Futures import java.util.function.BiConsumer trait FutureTimeoutSupport { def afterCompletionStage[T](duration: FiniteDuration, using: Scheduler)(value: ⇒ CompletionStage[T])(implicit ec: ExecutionContext): CompletionStage[T] = if (duration.isFinite() && duration.length < 1) { try value catch { case NonFatal(t) ⇒ Futures.failedCompletionStage(t) } } else { val p = new CompletableFuture[T] using.scheduleOnce(duration) { try { val future = value future.whenComplete(new BiConsumer[T, Throwable] { override def accept(t: T, ex: Throwable): Unit = { if (t != null) p.complete(t) if (ex != null) p.completeExceptionally(ex) } }) } catch { case NonFatal(ex) ⇒ p.completeExceptionally(ex) } } p } }
Example 92
Source File: AngulateTestSuite.scala From scalajs-angulate with MIT License | 5 votes |
// - Project: scalajs-angulate (https://github.com/jokade/scalajs-angulate) // Description: Utility trait for tests // // Distributed under the MIT License (see included file LICENSE) package test import biz.enef.angulate.Module.RichModule import biz.enef.angulate.{Scope, Angular} import biz.enef.angulate.core.JQLite import biz.enef.angulate.{Controller, Scope, Module, angular} import utest._ import scala.concurrent.Promise import scala.scalajs.js import scala.scalajs.js.UndefOr import js.Dynamic.literal def controller[T](name: String)(implicit module: RichModule) : T = { val $controller = dependency[js.Function2[String,js.Object,js.Any]]("$controller") val $rootScope = dependency[Scope]("$rootScope") val scope = $rootScope.$new(false) val res = $controller(name, literal($scope = scope)) scope.asInstanceOf[T] } def promise() : AssertionPromise = new AssertionPromise( Promise[Unit]() ) class AssertionPromise(p: Promise[Unit]) { def ok() = p.success( () ) def fail() = p.failure(new RuntimeException) def future = p.future def assert(expr: =>Boolean) : Unit = if(expr) ok() else fail() } }
Example 93
Source File: Endpoints.scala From endpoints4s with MIT License | 5 votes |
package endpoints4s.xhr.future import endpoints4s.xhr import scala.concurrent.{Future, Promise} type Result[A] = Future[A] def endpoint[A, B]( request: Request[A], response: Response[B], docs: EndpointDocs = EndpointDocs() ): Endpoint[A, B] = new Endpoint[A, B](request) { def apply(a: A) = { val promise = Promise[B]() performXhr(request, response, a)( _.fold( exn => { promise.failure(exn); () }, b => { promise.success(b); () } ), xhr => { promise.failure(new Exception(xhr.responseText)); () } ) promise.future } } }
Example 94
Source File: PingPongSuite.scala From lsp4s with Apache License 2.0 | 5 votes |
package tests import java.util.concurrent.ConcurrentLinkedQueue import minitest.SimpleTestSuite import monix.execution.Scheduler.Implicits.global import scala.collection.JavaConverters._ import scala.concurrent.Promise import scala.meta.jsonrpc._ import scala.meta.jsonrpc.testkit._ import scribe.Logger object PingPongSuite extends SimpleTestSuite { private val Ping = Endpoint.notification[String]("ping") private val Pong = Endpoint.notification[String]("pong") private val Hello = Endpoint.request[String, String]("hello") testAsync("ping pong") { val promise = Promise[Unit]() val pongs = new ConcurrentLinkedQueue[String]() val services = Services .empty(Logger.root) .request(Hello) { msg => s"$msg, World!" } .notification(Pong) { message => assert(pongs.add(message)) if (pongs.size() == 2) { promise.complete(util.Success(())) } } val pongBack: LanguageClient => Services = { client => services.notification(Ping) { message => Pong.notify(message.replace("Ping", "Pong"))(client) } } val conn = TestConnection(pongBack, pongBack) for { _ <- Ping.notify("Ping from client")(conn.alice.client) _ <- Ping.notify("Ping from server")(conn.bob.client) Right(helloWorld) <- Hello.request("Hello")(conn.alice.client).runAsync _ <- promise.future } yield { assertEquals(helloWorld, "Hello, World!") val obtainedPongs = pongs.asScala.toList.sorted val expectedPongs = List("Pong from client", "Pong from server") assertEquals(obtainedPongs, expectedPongs) conn.cancel() } } }
Example 95
Source File: InputRequestReplyHandler.scala From incubator-toree with Apache License 2.0 | 5 votes |
package org.apache.toree.kernel.protocol.v5.handler import akka.actor.ActorRef import org.apache.toree.comm.{CommRegistrar, CommStorage} import org.apache.toree.communication.utils.OrderedSupport import org.apache.toree.kernel.protocol.v5.{SystemActorType, KernelMessage} import org.apache.toree.kernel.protocol.v5.content.{InputReply, CommOpen} import org.apache.toree.kernel.protocol.v5.kernel.{Utilities, ActorLoader} import org.apache.toree.kernel.protocol.v5 import org.apache.toree.utils.MessageLogSupport import play.api.libs.json.Json import scala.concurrent.{Promise, Future} class InputRequestReplyHandler( actorLoader: ActorLoader, responseMap: collection.mutable.Map[String, ActorRef] ) extends OrderedSupport with MessageLogSupport { // TODO: Is there a better way than storing actor refs? def receive = { case kernelMessage: KernelMessage => startProcessing() val kernelMessageType = kernelMessage.header.msg_type val inputRequestType = v5.MessageType.Outgoing.InputRequest.toString val inputReplyType = v5.MessageType.Incoming.InputReply.toString // Is this an outgoing message to request data? if (kernelMessageType == inputRequestType) { val session = kernelMessage.parentHeader.session responseMap(session) = sender logger.debug("Associating input request with session " + session) actorLoader.load(SystemActorType.KernelMessageRelay) ! kernelMessage // Is this an incoming response to a previous request for data? } else if (kernelMessageType == inputReplyType) { val session = kernelMessage.header.session val inputReply = Json.parse(kernelMessage.contentString).as[InputReply] logger.debug(s"Received input reply for session $session with value " + s"'${inputReply.value}'") responseMap(session) ! inputReply.value responseMap.remove(session) } finishedProcessing() } override def orderedTypes() : Seq[Class[_]] = {Seq(classOf[KernelMessage])} }
Example 96
Source File: BrokerTransformerSpec.scala From incubator-toree with Apache License 2.0 | 5 votes |
package org.apache.toree.interpreter.broker import org.apache.toree.interpreter.{ExecuteError, Results} import org.scalatest.concurrent.Eventually import scala.concurrent.Promise import org.scalatest.{FunSpec, Matchers, OneInstancePerTest} class BrokerTransformerSpec extends FunSpec with Matchers with OneInstancePerTest with Eventually { private val brokerTransformer = new BrokerTransformer describe("BrokerTransformer") { describe("#transformToInterpreterResult") { it("should convert to success with result output if no failure") { val codeResultPromise = Promise[BrokerTypes.CodeResults]() val transformedFuture = brokerTransformer.transformToInterpreterResult( codeResultPromise.future ) val successOutput = "some success" codeResultPromise.success(successOutput) eventually { val result = transformedFuture.value.get.get result should be((Results.Success, Left(Map("text/plain" -> successOutput)))) } } it("should convert to error with broker exception if failure") { val codeResultPromise = Promise[BrokerTypes.CodeResults]() val transformedFuture = brokerTransformer.transformToInterpreterResult( codeResultPromise.future ) val failureException = new BrokerException("some failure") codeResultPromise.failure(failureException) eventually { val result = transformedFuture.value.get.get result should be((Results.Error, Right(ExecuteError( name = failureException.getClass.getName, value = failureException.getLocalizedMessage, stackTrace = failureException.getStackTrace.map(_.toString).toList )))) } } } } }
Example 97
Source File: CallbackTest.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.core.api import org.bitcoins.core.util.FutureUtil import org.bitcoins.testkit.util.BitcoinSAsyncTest import org.scalatest.Assertion import scala.concurrent.Promise import scala.concurrent.duration._ import scala.util.Success class CallbackTest extends BitcoinSAsyncTest { val testTimeout: FiniteDuration = 10.seconds it must "show callbacks being blocked" in { val promise = Promise[Assertion]() val f1: Callback[Unit] = _ => { Thread.sleep(testTimeout.toMillis) promise.complete(fail("2nd callback did not start before timeout")) FutureUtil.unit } val f2: Callback[Unit] = _ => { promise.complete(Success(succeed)) FutureUtil.unit } val handler = CallbackHandler[Unit, Callback[Unit]](name = "name", Vector(f1, f2)) // Start execution of callbacks handler.execute(()) // Return result of the callbacks, f2 should complete first promise.future } }
Example 98
Source File: ZMQSubscriberTest.scala From bitcoin-s with MIT License | 5 votes |
package org.bitcoins.zmq import java.net.InetSocketAddress import org.bitcoins.core.util.BytesUtil import org.scalatest.flatspec.AsyncFlatSpec import org.slf4j.LoggerFactory import org.zeromq.{ZFrame, ZMQ, ZMsg} import scodec.bits.ByteVector import scala.concurrent.Promise class ZMQSubscriberTest extends AsyncFlatSpec { private val logger = LoggerFactory.getLogger(this.getClass().toString) behavior of "ZMQSubscriber" it must "connect to a regtest instance of a daemon and stream txs/blocks from it" in { //note for this unit test to pass, you need to setup a bitcoind instance yourself //and set the bitcoin.conf file to allow for //zmq connections //see: https://github.com/bitcoin/bitcoin/blob/master/doc/zmq.md val socket = new InetSocketAddress("tcp://127.0.0.1", 29000) val zmqSub = new ZMQSubscriber(socket, None, None, rawTxListener, rawBlockListener) //stupid, doesn't test anything, for now. You need to look at log output to verify this is working // TODO: In the future this could use the testkit to verify the subscriber by calling generate(1) zmqSub.start() Thread.sleep(10000) // 10 seconds zmqSub.stop succeed } it must "be able to subscribe to a publisher and read a value" in { val port = Math.abs(scala.util.Random.nextInt % 14000) + 1000 val socket = new InetSocketAddress("tcp://127.0.0.1", port) val context = ZMQ.context(1) val publisher = context.socket(ZMQ.PUB) val uri = socket.getHostString + ":" + socket.getPort publisher.bind(uri) val valuePromise = Promise[String]() val fakeBlockListener: Option[ByteVector => Unit] = Some { bytes => val str = new String(bytes.toArray) valuePromise.success(str) () } val sub = new ZMQSubscriber(socket, None, None, None, fakeBlockListener) sub.start() Thread.sleep(1000) val testValue = "sweet, sweet satoshis" val msg = new ZMsg() msg.add(new ZFrame(RawBlock.topic)) msg.add(new ZFrame(testValue)) val sent = msg.send(publisher) assert(sent) valuePromise.future.map { str => sub.stop publisher.close() context.term() assert(str == testValue) } } val rawBlockListener: Option[ByteVector => Unit] = Some { { bytes: ByteVector => val hex = BytesUtil.encodeHex(bytes) logger.debug(s"received raw block ${hex}") } } val hashBlockListener: Option[ByteVector => Unit] = Some { { bytes: ByteVector => val hex = BytesUtil.encodeHex(bytes) logger.debug(s"received raw block hash ${hex}") } } val rawTxListener: Option[ByteVector => Unit] = Some { { bytes: ByteVector => val hex = BytesUtil.encodeHex(bytes) logger.debug(s"received raw tx ${hex}") } } }
Example 99
Source File: GrpcSourceStage.scala From grpcakkastream with MIT License | 5 votes |
package grpc.akkastreams import akka.stream.{Attributes, Outlet, SourceShape} import akka.stream.stage.{GraphStageLogic, GraphStageWithMaterializedValue, OutHandler} import io.grpc.stub.{CallStreamObserver, StreamObserver} import scala.concurrent.{Future, Promise} class GrpcSourceStage[I, O](requestStream: CallStreamObserver[O]) extends GraphStageWithMaterializedValue[SourceShape[I], Future[StreamObserver[I]]] { val out = Outlet[I]("grpc.out") override val shape: SourceShape[I] = SourceShape.of(out) override def createLogicAndMaterializedValue( inheritedAttributes: Attributes ): (GraphStageLogic, Future[StreamObserver[I]]) = { val promise: Promise[StreamObserver[I]] = Promise() val logic = new GraphStageLogic(shape) with OutHandler { val inObs = new StreamObserver[I] { override def onError(t: Throwable) = getAsyncCallback((t: Throwable) => fail(out, t)).invoke(t) override def onCompleted() = getAsyncCallback((_: Unit) => complete(out)).invoke(()) override def onNext(value: I) = getAsyncCallback((value: I) => push(out, value)).invoke(value) } override def onPull(): Unit = requestStream.request(1) override def preStart(): Unit = { requestStream.disableAutoInboundFlowControl() promise.success(inObs) } setHandler(out, this) } (logic, promise.future) } }
Example 100
Source File: CountDownLatch.scala From Akka-Cookbook with MIT License | 5 votes |
package com.packt.chapter10 import akka.Done import akka.actor.{Actor, ActorLogging, ActorRef, ActorSystem, Props} import scala.concurrent.{Await, Future, Promise} import scala.concurrent.duration._ object CountDownLatch { case object CountDown def apply(count:Int)(implicit actorSystem: ActorSystem) = { val promise = Promise[Done]() val props = Props(classOf[CountDownLatchActor], count, promise) val countDownLatchActor = actorSystem.actorOf(props, "countDownLatchActor") new CountDownLatch(countDownLatchActor, promise) } } class CountDownLatch(private val actor: ActorRef, private val promise: Promise[Done]) { import CountDownLatch._ def countDown() = actor ! CountDown def await() : Unit = Await.result(promise.future, 10 minutes) val result : Future[Done] = promise.future } class CountDownLatchActor(count: Int, promise: Promise[Done]) extends Actor with ActorLogging { import CountDownLatch._ var remaining = count def receive = { case CountDown if remaining - 1 == 0 => log.info("Counting down") promise.success(Done) log.info("Gate opened") context.stop(self) case CountDown => log.info("Counting down") remaining -= 1 } }
Example 101
Source File: LagomDevModeServiceDiscovery.scala From lagom with Apache License 2.0 | 5 votes |
package com.lightbend.lagom.devmode.internal.registry import java.net.InetAddress import java.net.URI import akka.actor.ActorSystem import akka.discovery.ServiceDiscovery._ import akka.discovery.Discovery import akka.discovery.Lookup import akka.discovery.ServiceDiscovery import scala.concurrent.ExecutionContext import scala.concurrent.Future import scala.concurrent.Promise import scala.concurrent.duration.FiniteDuration private[lagom] class LagomDevModeServiceDiscovery(system: ActorSystem) extends ServiceDiscovery { private val clientPromise = Promise[ServiceRegistryClient] private implicit val ec: ExecutionContext = system.dispatcher def setServiceRegistryClient(client: ServiceRegistryClient): Unit = clientPromise.success(client) override def lookup(lookup: Lookup, resolveTimeout: FiniteDuration): Future[Resolved] = for { client <- clientPromise.future uris <- client.locateAll(lookup.serviceName, lookup.portName) } yield Resolved(lookup.serviceName, uris.map(toResolvedTarget)) private def toResolvedTarget(uri: URI) = ResolvedTarget( uri.getHost, optionalPort(uri.getPort), // we don't have the InetAddress, but instead of using None // we default to localhost as such we can use it for Akka Cluster Bootstrap eventually address = Some(InetAddress.getLocalHost) ) private def optionalPort(port: Int): Option[Int] = if (port < 0) None else Some(port) } private[lagom] object LagomDevModeServiceDiscovery { def apply(system: ActorSystem): LagomDevModeServiceDiscovery = Discovery(system) .loadServiceDiscovery("lagom-dev-mode") .asInstanceOf[LagomDevModeServiceDiscovery] }
Example 102
Source File: NettyFutureConverters.scala From lagom with Apache License 2.0 | 5 votes |
package com.lightbend.lagom.internal import io.netty.channel.Channel import io.netty.channel.ChannelFuture import io.netty.channel.ChannelFutureListener import io.netty.util.concurrent.GenericFutureListener import io.netty.util.concurrent.{ Future => NettyFuture } import scala.concurrent.Future import scala.concurrent.Promise object NettyFutureConverters { implicit class ToFuture[T](future: NettyFuture[T]) { def toScala: Future[T] = { val promise = Promise[T]() future.addListener(new GenericFutureListener[NettyFuture[T]] { def operationComplete(future: NettyFuture[T]) = { if (future.isSuccess) { promise.success(future.getNow) } else if (future.isCancelled) { promise.failure(new RuntimeException("Future cancelled")) } else { promise.failure(future.cause()) } } }) promise.future } } implicit class ChannelFutureToFuture(future: ChannelFuture) { def channelFutureToScala: Future[Channel] = { val promise = Promise[Channel]() future.addListener(new ChannelFutureListener { def operationComplete(future: ChannelFuture) = { if (future.isSuccess) { promise.success(future.channel()) } else if (future.isCancelled) { promise.failure(new RuntimeException("Future cancelled")) } else { promise.failure(future.cause()) } } }) promise.future } } }
Example 103
Source File: ServiceLocatorSessionProvider.scala From lagom with Apache License 2.0 | 5 votes |
package com.lightbend.lagom.internal.persistence.cassandra import java.net.InetSocketAddress import java.net.URI import scala.collection.immutable import scala.concurrent.ExecutionContext import scala.concurrent.Future import scala.concurrent.Promise import scala.concurrent.duration._ import scala.util.control.NoStackTrace import akka.actor.ActorSystem import akka.persistence.cassandra.ConfigSessionProvider import com.typesafe.config.Config import play.api.Logger private[lagom] final class ServiceLocatorSessionProvider(system: ActorSystem, config: Config) extends ConfigSessionProvider(system, config) { private val log = Logger(getClass) override def lookupContactPoints( clusterId: String )(implicit ec: ExecutionContext): Future[immutable.Seq[InetSocketAddress]] = { ServiceLocatorHolder(system).serviceLocatorEventually.flatMap { serviceLocatorAdapter => serviceLocatorAdapter.locateAll(clusterId).map { case Nil => throw new NoContactPointsException(s"No contact points for [$clusterId]") case uris => log.debug(s"Found Cassandra contact points: $uris") // URIs must be all valid uris.foreach { uri => require(uri.getHost != null, s"missing host in $uri for Cassandra contact points $clusterId") require(uri.getPort != -1, s"missing port in $uri for Cassandra contact points $clusterId") } uris.map { uri => new InetSocketAddress(uri.getHost, uri.getPort) } } } } } private[lagom] final class NoContactPointsException(msg: String) extends RuntimeException(msg) with NoStackTrace
Example 104
Source File: NettyRpcCallContext.scala From sparkoscope with Apache License 2.0 | 5 votes |
package org.apache.spark.rpc.netty import scala.concurrent.Promise import org.apache.spark.internal.Logging import org.apache.spark.network.client.RpcResponseCallback import org.apache.spark.rpc.{RpcAddress, RpcCallContext} private[netty] abstract class NettyRpcCallContext(override val senderAddress: RpcAddress) extends RpcCallContext with Logging { protected def send(message: Any): Unit override def reply(response: Any): Unit = { send(response) } override def sendFailure(e: Throwable): Unit = { send(RpcFailure(e)) } } private[netty] class RemoteNettyRpcCallContext( nettyEnv: NettyRpcEnv, callback: RpcResponseCallback, senderAddress: RpcAddress) extends NettyRpcCallContext(senderAddress) { override protected def send(message: Any): Unit = { val reply = nettyEnv.serialize(message) callback.onSuccess(reply) } }
Example 105
Source File: BlockTransferService.scala From sparkoscope with Apache License 2.0 | 5 votes |
package org.apache.spark.network import java.io.Closeable import java.nio.ByteBuffer import scala.concurrent.{Future, Promise} import scala.concurrent.duration.Duration import scala.reflect.ClassTag import org.apache.spark.internal.Logging import org.apache.spark.network.buffer.{ManagedBuffer, NioManagedBuffer} import org.apache.spark.network.shuffle.{BlockFetchingListener, ShuffleClient} import org.apache.spark.storage.{BlockId, StorageLevel} import org.apache.spark.util.ThreadUtils private[spark] abstract class BlockTransferService extends ShuffleClient with Closeable with Logging { def uploadBlockSync( hostname: String, port: Int, execId: String, blockId: BlockId, blockData: ManagedBuffer, level: StorageLevel, classTag: ClassTag[_]): Unit = { val future = uploadBlock(hostname, port, execId, blockId, blockData, level, classTag) ThreadUtils.awaitResult(future, Duration.Inf) } }
Example 106
Source File: JobWaiter.scala From sparkoscope with Apache License 2.0 | 5 votes |
package org.apache.spark.scheduler import java.util.concurrent.atomic.AtomicInteger import scala.concurrent.{Future, Promise} import org.apache.spark.internal.Logging def cancel() { dagScheduler.cancelJob(jobId) } override def taskSucceeded(index: Int, result: Any): Unit = { // resultHandler call must be synchronized in case resultHandler itself is not thread safe. synchronized { resultHandler(index, result.asInstanceOf[T]) } if (finishedTasks.incrementAndGet() == totalTasks) { jobPromise.success(()) } } override def jobFailed(exception: Exception): Unit = { if (!jobPromise.tryFailure(exception)) { logWarning("Ignore failure", exception) } } }
Example 107
Source File: AsyncSocketChannelObservable.scala From monix-nio with Apache License 2.0 | 5 votes |
package monix.nio.tcp import java.net.InetSocketAddress import monix.execution.Callback import monix.nio._ import monix.reactive.observers.Subscriber import scala.concurrent.Promise final class AsyncSocketChannelObservable private[tcp] ( host: String, port: Int, override val bufferSize: Int = 256 * 1024) extends AsyncChannelObservable { private[this] val connectedSignal = Promise[Unit]() private[this] var taskSocketChannel: Option[TaskSocketChannel] = None private[this] var closeOnComplete = true private[tcp] def this(tsc: TaskSocketChannel, buffSize: Int, closeWhenDone: Boolean) = { this("", 0, buffSize) this.taskSocketChannel = Option(tsc) this.closeOnComplete = closeWhenDone } override lazy val channel = taskSocketChannel.map(asc => asyncChannelWrapper(asc, closeOnComplete)) override def init(subscriber: Subscriber[Array[Byte]]) = { import subscriber.scheduler if (taskSocketChannel.isDefined) { connectedSignal.success(()) } else { val connectCallback = new Callback[Throwable, Unit]() { override def onSuccess(value: Unit): Unit = { connectedSignal.success(()) } override def onError(ex: Throwable): Unit = { connectedSignal.failure(ex) closeChannel() subscriber.onError(ex) } } taskSocketChannel = Option(TaskSocketChannel()) taskSocketChannel.foreach(_.connect(new InetSocketAddress(host, port)).runAsync(connectCallback)) } connectedSignal.future } }
Example 108
Source File: AsyncSocketChannelConsumer.scala From monix-nio with Apache License 2.0 | 5 votes |
package monix.nio.tcp import java.net.InetSocketAddress import monix.execution.Callback import monix.nio.AsyncChannelConsumer import scala.concurrent.Promise final class AsyncSocketChannelConsumer private[tcp] ( host: String, port: Int) extends AsyncChannelConsumer { private[this] var taskSocketChannel: Option[TaskSocketChannel] = None private[this] var closeOnComplete = true private[tcp] def this(tsc: TaskSocketChannel, closeWhenDone: Boolean) = { this("", 0) this.taskSocketChannel = Option(tsc) this.closeOnComplete = closeWhenDone } override lazy val channel = taskSocketChannel.map(tsc => asyncChannelWrapper(tsc, closeOnComplete)) override def init(subscriber: AsyncChannelSubscriber) = { import subscriber.scheduler val connectedPromise = Promise[Unit]() if (taskSocketChannel.isDefined) { connectedPromise.success(()) } else { val connectCallback = new Callback[Throwable, Unit]() { override def onSuccess(value: Unit): Unit = { connectedPromise.success(()) } override def onError(ex: Throwable): Unit = { connectedPromise.failure(ex) subscriber.closeChannel() subscriber.onError(ex) } } taskSocketChannel = Option(TaskSocketChannel()) taskSocketChannel.foreach(_.connect(new InetSocketAddress(host, port)).runAsync(connectCallback)) } connectedPromise.future } }
Example 109
Source File: AsyncChannelConsumer.scala From monix-nio with Apache License 2.0 | 5 votes |
package monix.nio import java.nio.ByteBuffer import monix.execution.Ack.{ Continue, Stop } import monix.execution.{ Ack, Callback, Cancelable, Scheduler } import monix.execution.atomic.Atomic import monix.execution.cancelables.{ AssignableCancelable, SingleAssignCancelable } import monix.reactive.Consumer import monix.reactive.observers.Subscriber import scala.concurrent.{ Future, Promise } import scala.util.control.NonFatal private[nio] abstract class AsyncChannelConsumer extends Consumer[Array[Byte], Long] { def channel: Option[AsyncChannel] def withInitialPosition: Long = 0L def init(subscriber: AsyncChannelSubscriber): Future[Unit] = Future.successful(()) class AsyncChannelSubscriber(consumerCallback: Callback[Throwable, Long])(implicit val scheduler: Scheduler) extends Subscriber[Array[Byte]] { self => private[this] lazy val initFuture = init(self) private[this] val callbackCalled = Atomic(false) private[this] var position = withInitialPosition override def onNext(elem: Array[Byte]): Future[Ack] = { def write(): Future[Ack] = { val promise = Promise[Ack]() channel.foreach { sc => try { sc .write(ByteBuffer.wrap(elem), position) .runAsync( new Callback[Throwable, Int] { override def onError(exc: Throwable) = { closeChannel() sendError(exc) promise.success(Stop) } override def onSuccess(result: Int): Unit = { position += result promise.success(Continue) } }) } catch { case NonFatal(ex) => sendError(ex) promise.success(Stop) } } promise.future } if (initFuture.value.isEmpty) { initFuture.flatMap(_ => write()) } else { write() } } override def onComplete(): Unit = { channel.collect { case sc if sc.closeOnComplete => closeChannel() } if (callbackCalled.compareAndSet(expect = false, update = true)) consumerCallback.onSuccess(position) } override def onError(ex: Throwable): Unit = { closeChannel() sendError(ex) } private[nio] def onCancel(): Unit = { callbackCalled.set(true) closeChannel() } private[nio] def sendError(t: Throwable) = if (callbackCalled.compareAndSet(expect = false, update = true)) { scheduler.execute(new Runnable { def run() = consumerCallback.onError(t) }) } private[nio] final def closeChannel()(implicit scheduler: Scheduler) = channel.foreach(_.close().runToFuture) } override def createSubscriber(cb: Callback[Throwable, Long], s: Scheduler): (Subscriber[Array[Byte]], AssignableCancelable) = { val out = new AsyncChannelSubscriber(cb)(s) val extraCancelable = Cancelable(() => out.onCancel()) val conn = SingleAssignCancelable.plusOne(extraCancelable) (out, conn) } }
Example 110
Source File: WatchService.scala From monix-nio with Apache License 2.0 | 5 votes |
package monix.nio.file import java.nio.file.StandardWatchEventKinds.{ ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY } import java.nio.file.WatchEvent.Kind import java.nio.file.{ Path, WatchEvent, WatchKey } import com.sun.nio.file.SensitivityWatchEventModifier import monix.execution.{ Callback, Cancelable, Scheduler } import scala.concurrent.{ Future, Promise } import scala.concurrent.duration.TimeUnit import scala.util.control.NonFatal abstract class WatchService extends AutoCloseable { def poll(timeout: Long, timeUnit: TimeUnit, cb: Callback[Throwable, Option[WatchKey]]): Unit def poll(timeout: Long, timeUnit: TimeUnit): Future[Option[WatchKey]] = { val p = Promise[Option[WatchKey]]() poll(timeout, timeUnit, Callback.fromPromise(p)) p.future } def poll(cb: Callback[Throwable, Option[WatchKey]]): Unit def poll(): Future[Option[WatchKey]] = { val p = Promise[Option[WatchKey]]() poll(Callback.fromPromise(p)) p.future } def take(cb: Callback[Throwable, WatchKey]): Unit def take(): Future[WatchKey] = { val p = Promise[WatchKey]() take(Callback.fromPromise(p)) p.future } } object WatchService { val SupportedEvents: Set[Kind[_]] = Set(ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY) def apply(path: Path, events: Kind[_]*)(implicit scheduler: Scheduler): WatchService = { val watcher = path.getFileSystem.newWatchService() val watchFor = if (events.isEmpty) SupportedEvents else events path.register( watcher, watchFor.toArray, SensitivityWatchEventModifier.HIGH.asInstanceOf[WatchEvent.Modifier]) new NIOWatcherServiceImplementation(watcher) } private final class NIOWatcherServiceImplementation(watcher: java.nio.file.WatchService)(implicit scheduler: Scheduler) extends WatchService { override def poll(timeout: Long, timeUnit: TimeUnit, cb: Callback[Throwable, Option[WatchKey]]): Unit = { try { val key = Option(watcher.poll(timeout, timeUnit)) cb.onSuccess(key) } catch { case NonFatal(ex) => cb.onError(ex) } } override def poll(cb: Callback[Throwable, Option[WatchKey]]): Unit = { try { val key = Option(watcher.poll()) cb.onSuccess(key) } catch { case NonFatal(ex) => cb.onError(ex) } } override def take(cb: Callback[Throwable, WatchKey]): Unit = { try { val key = watcher.take() cb.onSuccess(key) } catch { case NonFatal(ex) => cb.onError(ex) } } override def close(): Unit = cancelable.cancel() private[this] val cancelable: Cancelable = Cancelable { () => try watcher.close() catch { case NonFatal(ex) => scheduler.reportFailure(ex) } } } }
Example 111
Source File: UdpIntegrationSpec.scala From monix-nio with Apache License 2.0 | 5 votes |
package monix.nio.udp import java.net.InetSocketAddress import minitest.SimpleTestSuite import monix.eval.Task import monix.execution.Ack import monix.execution.Ack.{ Continue, Stop } import monix.reactive.Observable import scala.concurrent.duration._ import scala.concurrent.{ Await, Promise } object UdpIntegrationSpec extends SimpleTestSuite { implicit val ctx = monix.execution.Scheduler.Implicits.global test("send and receive UDP packets successfully") { val data = Array.fill(8)("monix") val writes = (ch: TaskDatagramChannel, to: InetSocketAddress) => Observable .fromIterable(data) .mapEval(data => ch.send(Packet(data.getBytes, to))) val readsPromise = Promise[String]() val recv = new StringBuilder("") val reads = (ch: TaskDatagramChannel, maxSize: Int) => Observable .repeatEval(ch.receive(maxSize, 2.seconds)) .mapEval(t => t) .map { packet => packet.foreach(p => recv.append(new String(p.data))) packet } .guaranteeCase(_ => Task(readsPromise.success(recv.mkString))) .subscribe(_.fold[Ack](Stop)(_ => Continue)) val program = for { ch <- bind("localhost", 2115).map { ch => reads(ch, 64) ch } sent <- writes(ch, new InetSocketAddress("localhost", 2115)).sumL received <- Task.fromFuture(readsPromise.future) _ <- ch.close() } yield sent == 40 & received == data.mkString("") val result = Await.result(program.runToFuture, 10.seconds) assert(result) } }
Example 112
Source File: WatchServiceTest.scala From monix-nio with Apache License 2.0 | 5 votes |
package monix.nio.file import java.io.File import java.nio.file.{ Paths, WatchEvent } import minitest.SimpleTestSuite import monix.eval.Task import monix.execution.Ack.{ Continue, Stop } import scala.concurrent.duration._ import scala.concurrent.{ Await, Promise } object WatchServiceTest extends SimpleTestSuite { implicit val ctx = monix.execution.Scheduler.Implicits.global test("file event captured") { val path = Paths.get(System.getProperty("java.io.tmpdir")) val watchP = Promise[Boolean]() val watchT = Task.evalAsync { watchAsync(path).timeoutOnSlowUpstream(10.seconds).subscribe( (events: Array[WatchEvent[_]]) => { val captured = events.find(e => s"${e.kind().name()} - ${e.context().toString}".contains("monix")) if (captured.isDefined) { watchP.success(true) Stop } else { Continue } }, err => watchP.failure(err), () => watchP.success(true)) } val fileT = Task.evalAsync { val temp = File.createTempFile("monix", ".tmp", path.toFile) Thread.sleep(2000) temp.delete() } watchT.runToFuture fileT.runToFuture val result = Await.result(watchP.future, 20.seconds) assert(result) } }
Example 113
Source File: IntegrationTest.scala From monix-nio with Apache License 2.0 | 5 votes |
package monix.nio.file import java.nio.file.{ Files, Paths, StandardOpenOption } import java.util import minitest.SimpleTestSuite import monix.execution.Callback import monix.nio.file import scala.concurrent.duration._ import scala.concurrent.{ Await, Promise } import scala.util.control.NonFatal object IntegrationTest extends SimpleTestSuite { test("same file generated") { implicit val ctx = monix.execution.Scheduler.Implicits.global val from = Paths.get(this.getClass.getResource("/testFiles/file.txt").toURI) val to = Paths.get("src/test/resources/out.txt") val consumer = file.writeAsync(to) val p = Promise[Boolean]() val callback = new Callback[Throwable, Long] { override def onSuccess(value: Long): Unit = p.success(true) override def onError(ex: Throwable): Unit = p.failure(ex) } readAsync(from, 3) .consumeWith(consumer) .runAsync(callback) val result = Await.result(p.future, 3.second) assert(result) val f1 = Files.readAllBytes(from) val f2 = Files.readAllBytes(to) Files.delete(to) // clean assert(util.Arrays.equals(f1, f2)) } test("add data to existing file") { implicit val ctx = monix.execution.Scheduler.Implicits.global val from = Paths.get(this.getClass.getResource("/testFiles/file.txt").toURI) val to = Paths.get("src/test/resources/existing.txt") val strSeq = Seq("A", "\u0024", "\u00A2", "\u20AC", new String(Array(0xF0, 0x90, 0x8D, 0x88).map(_.toByte)), "B") try { Files.write(to, strSeq.flatMap(_.getBytes).toArray, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.APPEND) } catch { case NonFatal(e) => fail(s"got error: $e") } val consumer = file.appendAsync(to, Files.size(to)) val p = Promise[Boolean]() val callback = new Callback[Throwable, Long] { override def onSuccess(value: Long): Unit = p.success(true) override def onError(ex: Throwable): Unit = p.failure(ex) } readAsync(from, 3) .consumeWith(consumer) .runAsync(callback) val result = Await.result(p.future, 3.second) assert(result) val f1 = Files.readAllBytes(from) val f2 = Files.readAllBytes(to) Files.delete(to) // clean val all1: Seq[Byte] = strSeq.flatMap(_.getBytes) ++ f1.toSeq assertEquals(all1, f2.toSeq) } }
Example 114
Source File: CodecTest.scala From monix-nio with Apache License 2.0 | 5 votes |
package monix.nio.file import java.nio.file.{ Files, Paths } import java.util import minitest.SimpleTestSuite import monix.eval.Task import monix.execution.Callback import monix.execution.Scheduler.Implicits.{ global => ctx } import monix.nio.file import monix.nio.text.UTF8Codec.{ utf8Decode, utf8Encode } import monix.reactive.Observable import scala.concurrent.duration._ import scala.concurrent.{ Await, Promise } object CodecTest extends SimpleTestSuite { test("decode file utf8") { val from = Paths.get(this.getClass.getResource("/testFiles/specialChars.txt").toURI) val p = Promise[Seq[Byte]]() val callback = new Callback[Throwable, List[Array[Byte]]] { override def onSuccess(value: List[Array[Byte]]): Unit = p.success(value.flatten) override def onError(ex: Throwable): Unit = p.failure(ex) } readAsync(from, 3) .pipeThrough(utf8Decode) .pipeThrough(utf8Encode) .toListL .runAsync(callback) val result = Await.result(p.future, 3.second) val f1 = Files.readAllBytes(from) val f2 = result assert(util.Arrays.equals(f1, f2.toArray)) } test("decode special chars") { val strSeq = Seq("A", "\u0024", "\u00A2", "\u20AC", new String(Array(0xF0, 0x90, 0x8D, 0x88).map(_.toByte)), "B") for (grouping <- 1 to 12) { val obsSeq = Observable .fromIterator(Task(strSeq.flatMap(_.getBytes).grouped(grouping).map(_.toArray))) .pipeThrough(utf8Decode) val p = Promise[Boolean]() val callback = new Callback[Throwable, List[String]] { override def onSuccess(value: List[String]): Unit = { p.success(if (value.mkString == strSeq.mkString) true else false) } override def onError(ex: Throwable): Unit = p.failure(ex) } obsSeq.toListL.runAsync(callback) val result = Await.result(p.future, 3.second) assert(result) } } test("copy file utf8") { val from = Paths.get(this.getClass.getResource("/testFiles/specialChars.txt").toURI) val to = Paths.get("src/test/resources/res.txt") val consumer = file.writeAsync(to) val p = Promise[Long]() val callback = new Callback[Throwable, Long] { override def onSuccess(value: Long): Unit = p.success(value) override def onError(ex: Throwable): Unit = p.failure(ex) } readAsync(from, 3) .pipeThrough(utf8Decode) .map { str => //Console.println(str) str } .pipeThrough(utf8Encode) .consumeWith(consumer) .runAsync(callback) val result = Await.result(p.future, 3.second) val f1 = Files.readAllBytes(from) val f2 = result Files.delete(to) assertEquals(f1.size, f2) } }
Example 115
Source File: Utils.scala From telegram with Apache License 2.0 | 5 votes |
import java.util.{Timer, TimerTask} import scala.concurrent.duration.Duration import scala.concurrent.{Future, Promise} import scala.util.Try object Utils { def after[T](duration: Duration)(block: => T): Future[T] = { val promise = Promise[T]() val t = new Timer() t.schedule(new TimerTask { override def run(): Unit = { promise.complete(Try(block)) } }, duration.toMillis) promise.future } }
Example 116
Source File: WebRoutes.scala From telegram with Apache License 2.0 | 5 votes |
package com.bot4s.telegram.api import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import com.bot4s.telegram.future.BotExecutionContext import slogging.StrictLogging import scala.concurrent.{Future, Promise} trait WebRoutes extends BotBase[Future] with StrictLogging { _: BotExecutionContext with AkkaImplicits => val port: Int val interfaceIp: String = "::0" def routes: Route = reject private var bindingFuture: Future[Http.ServerBinding] = _ @volatile private var eol: Promise[Unit] = _ abstract override def run(): Future[Unit] = synchronized { if (eol != null) { throw new RuntimeException("Bot is already running") } bindingFuture = Http().bindAndHandle(routes, interfaceIp, port) bindingFuture.foreach { _ => logger.info(s"Listening on $interfaceIp:$port") } sys.addShutdownHook { shutdown() } eol = Promise[Unit]() val t = Future.sequence(Seq(eol.future, super.run())) t.map(_ => ()) } abstract override def shutdown(): Unit = synchronized { if (eol == null) { throw new RuntimeException("Bot is not running") } super.shutdown() for { b <- bindingFuture _ <- b.unbind() t <- system.terminate() } { eol.success(()) eol = null } } }
Example 117
Source File: BlockTransferService.scala From SparkCore with Apache License 2.0 | 5 votes |
package org.apache.spark.network import java.io.Closeable import java.nio.ByteBuffer import scala.concurrent.{Promise, Await, Future} import scala.concurrent.duration.Duration import org.apache.spark.Logging import org.apache.spark.network.buffer.{NioManagedBuffer, ManagedBuffer} import org.apache.spark.network.shuffle.{ShuffleClient, BlockFetchingListener} import org.apache.spark.storage.{BlockManagerId, BlockId, StorageLevel} private[spark] abstract class BlockTransferService extends ShuffleClient with Closeable with Logging { def uploadBlockSync( hostname: String, port: Int, execId: String, blockId: BlockId, blockData: ManagedBuffer, level: StorageLevel): Unit = { Await.result(uploadBlock(hostname, port, execId, blockId, blockData, level), Duration.Inf) } }
Example 118
Source File: NettyBlockTransferSecuritySuite.scala From SparkCore with Apache License 2.0 | 5 votes |
package org.apache.spark.network.netty import java.nio._ import java.util.concurrent.TimeUnit import scala.concurrent.duration._ import scala.concurrent.{Await, Promise} import scala.util.{Failure, Success, Try} import org.apache.commons.io.IOUtils import org.apache.spark.network.buffer.{ManagedBuffer, NioManagedBuffer} import org.apache.spark.network.shuffle.BlockFetchingListener import org.apache.spark.network.{BlockDataManager, BlockTransferService} import org.apache.spark.storage.{BlockId, ShuffleBlockId} import org.apache.spark.{SecurityManager, SparkConf} import org.mockito.Mockito._ import org.scalatest.mock.MockitoSugar import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, FunSuite, ShouldMatchers} class NettyBlockTransferSecuritySuite extends FunSuite with MockitoSugar with ShouldMatchers { test("security default off") { val conf = new SparkConf() .set("spark.app.id", "app-id") testConnection(conf, conf) match { case Success(_) => // expected case Failure(t) => fail(t) } } test("security on same password") { val conf = new SparkConf() .set("spark.authenticate", "true") .set("spark.authenticate.secret", "good") .set("spark.app.id", "app-id") testConnection(conf, conf) match { case Success(_) => // expected case Failure(t) => fail(t) } } test("security on mismatch password") { val conf0 = new SparkConf() .set("spark.authenticate", "true") .set("spark.authenticate.secret", "good") .set("spark.app.id", "app-id") val conf1 = conf0.clone.set("spark.authenticate.secret", "bad") testConnection(conf0, conf1) match { case Success(_) => fail("Should have failed") case Failure(t) => t.getMessage should include ("Mismatched response") } } test("security mismatch auth off on server") { val conf0 = new SparkConf() .set("spark.authenticate", "true") .set("spark.authenticate.secret", "good") .set("spark.app.id", "app-id") val conf1 = conf0.clone.set("spark.authenticate", "false") testConnection(conf0, conf1) match { case Success(_) => fail("Should have failed") case Failure(t) => // any funny error may occur, sever will interpret SASL token as RPC } } test("security mismatch auth off on client") { val conf0 = new SparkConf() .set("spark.authenticate", "false") .set("spark.authenticate.secret", "good") .set("spark.app.id", "app-id") val conf1 = conf0.clone.set("spark.authenticate", "true") testConnection(conf0, conf1) match { case Success(_) => fail("Should have failed") case Failure(t) => t.getMessage should include ("Expected SaslMessage") } } private def fetchBlock( self: BlockTransferService, from: BlockTransferService, execId: String, blockId: BlockId): Try[ManagedBuffer] = { val promise = Promise[ManagedBuffer]() self.fetchBlocks(from.hostName, from.port, execId, Array(blockId.toString), new BlockFetchingListener { override def onBlockFetchFailure(blockId: String, exception: Throwable): Unit = { promise.failure(exception) } override def onBlockFetchSuccess(blockId: String, data: ManagedBuffer): Unit = { promise.success(data.retain()) } }) Await.ready(promise.future, FiniteDuration(1000, TimeUnit.MILLISECONDS)) promise.future.value.get } }
Example 119
Source File: rabenchmarks.scala From reactive-async with BSD 2-Clause "Simplified" License | 5 votes |
package com.phaller.rasync package bench import com.phaller.rasync.cell.CellCompleter import com.phaller.rasync.lattice.lattices.{ NaturalNumberKey, NaturalNumberLattice } import com.phaller.rasync.pool.HandlerPool import lattice.Lattice import org.scalameter.api._ import org.scalameter.picklers.noPickler._ import scala.concurrent.{ Await, Promise } import scala.concurrent.duration._ object ReactiveAsyncBenchmarks extends PerformanceTest.Microbenchmark { performance of "Cells" in { measure method "create and putFinal" in { using(size) config ( exec.benchRuns -> 9) in { r => { implicit val pool = new HandlerPool(NaturalNumberKey, nrOfThreads) for (i <- 1 to r) { pool.execute(() => { val cellCompleter = CellCompleter[Int, Null]() cellCompleter.putFinal(1) }) } waitUntilQuiescent(pool) } } } } performance of "Cells" in { measure method "putNext" in { using(Gen.unit(s"$nrOfCells cells")) config ( exec.benchRuns -> 9) in { (Unit) => implicit val pool = new HandlerPool(NaturalNumberKey, nrOfThreads) val cellCompleter = CellCompleter[Int, Null]() for (i <- 1 to nrOfCells) pool.execute(() => cellCompleter.putNext(i)) waitUntilQuiescent(pool) } } } def waitUntilQuiescent(pool: HandlerPool[_, _]): Unit = { val p = Promise[Boolean] pool.onQuiescent { () => p.success(true) } Await.ready(p.future, 30.seconds) } }
Example 120
Source File: fpbenchmarks.scala From reactive-async with BSD 2-Clause "Simplified" License | 5 votes |
package com.phaller.rasync package bench import scala.concurrent.Promise import scala.annotation.tailrec import org.scalameter.api._ import org.scalameter.picklers.noPickler._ object FuturesAndPromisesBenchmarks extends PerformanceTest.Microbenchmark { performance of "Promises" in { measure method "refinement" in { using(Gen.unit(s"$nrOfPromises promises")) config ( exec.benchRuns -> 9) in { (Unit) => { var i = 0 val promises = createListPromises(nrOfPromises, List.empty) for (p <- promises) { i = i + 1 p.success(i) } } } } } @tailrec def createListPromises(amount: Int, promises: List[Promise[Int]]): List[Promise[Int]] = { val p = Promise[Int] if (amount == 0) p :: promises else createListPromises(amount - 1, p :: promises) } }
Example 121
Source File: PoolSuite.scala From reactive-async with BSD 2-Clause "Simplified" License | 5 votes |
package com.phaller.rasync package test import java.util.concurrent.{ ConcurrentHashMap, CountDownLatch } import com.phaller.rasync.cell.{ Cell, CellCompleter } import org.scalatest.FunSuite import scala.concurrent.{ Await, Promise } import scala.concurrent.duration._ import com.phaller.rasync.lattice.Updater import com.phaller.rasync.pool.HandlerPool import com.phaller.rasync.test.lattice.{ IntUpdater, StringIntKey } class PoolSuite extends FunSuite { test("onQuiescent") { val pool = HandlerPool[Int] var i = 0 while (i < 10000) { val p1 = Promise[Boolean]() val p2 = Promise[Boolean]() pool.execute { () => { p1.success(true) }: Unit } pool.onQuiescent { () => p2.success(true) } try { Await.result(p2.future, 1.seconds) } catch { case t: Throwable => assert(false, s"failure after $i iterations") } i += 1 } pool.shutdown() } test("register cells concurrently") { implicit val stringIntUpdater: Updater[Int] = new IntUpdater implicit val pool = new HandlerPool[Int, Null](new StringIntKey("s")) var regCells = new ConcurrentHashMap[Cell[Int, Null], Cell[Int, Null]]() for (_ <- 1 to 1000) { pool.execute(() => { val completer = CellCompleter[Int, Null]() completer.cell.trigger() regCells.put(completer.cell, completer.cell) () }) } val fut = pool.quiescentResolveCell // set all (registered) cells to 1 via key.fallback Await.ready(fut, 5.seconds) regCells.values().removeIf(_.getResult() != 0) assert(regCells.size === 0) } test("register cells concurrently 2") { implicit val stringIntUpdater: Updater[Int] = new IntUpdater implicit val pool = new HandlerPool[Int, Null](new StringIntKey("s")) var regCells = new ConcurrentHashMap[Cell[Int, Null], Cell[Int, Null]]() for (_ <- 1 to 1000) { pool.execute(() => { val completer = CellCompleter[Int, Null]() regCells.put(completer.cell, completer.cell) () }) } val fut = pool.quiescentResolveCell // set all (registered) cells to 1 via key.fallback Await.ready(fut, 5.seconds) assert(regCells.size === 1000) } test("handler pool quiescence") { implicit val pool = new HandlerPool[Int, Null] val latch = new CountDownLatch(1) val latch2 = new CountDownLatch(1) pool.execute { () => latch.await() } pool.onQuiescent { () => latch2.countDown() } latch.countDown() latch2.await() assert(true) pool.onQuiescenceShutdown() } }
Example 122
Source File: ClusterBootstrap.scala From akka-management with Apache License 2.0 | 5 votes |
package akka.management.cluster.bootstrap import java.util.concurrent.atomic.AtomicReference import akka.AkkaVersion import scala.concurrent.{ Future, Promise, TimeoutException } import scala.concurrent.duration._ import akka.actor.ActorSystem import akka.actor.ClassicActorSystemProvider import akka.actor.ExtendedActorSystem import akka.actor.Extension import akka.actor.ExtensionId import akka.actor.ExtensionIdProvider import akka.annotation.InternalApi import akka.cluster.Cluster import akka.discovery.{ Discovery, ServiceDiscovery } import akka.event.Logging import akka.http.scaladsl.model.Uri import akka.http.scaladsl.server.Route import akka.management.cluster.bootstrap.contactpoint.HttpClusterBootstrapRoutes import akka.management.cluster.bootstrap.internal.BootstrapCoordinator import akka.management.scaladsl.ManagementRouteProviderSettings import akka.management.scaladsl.ManagementRouteProvider final class ClusterBootstrap(implicit system: ExtendedActorSystem) extends Extension with ManagementRouteProvider { import ClusterBootstrap.Internal._ import system.dispatcher private val log = Logging(system, classOf[ClusterBootstrap]) private final val bootstrapStep = new AtomicReference[BootstrapStep](NotRunning) AkkaVersion.require("cluster-bootstrap", "2.5.27") val settings: ClusterBootstrapSettings = ClusterBootstrapSettings(system.settings.config, log) // used for initial discovery of contact points lazy val discovery: ServiceDiscovery = settings.contactPointDiscovery.discoveryMethod match { case "akka.discovery" => val discovery = Discovery(system).discovery log.info("Bootstrap using default `akka.discovery` method: {}", Logging.simpleName(discovery)) discovery case otherDiscoveryMechanism => log.info("Bootstrap using `akka.discovery` method: {}", otherDiscoveryMechanism) Discovery(system).loadServiceDiscovery(otherDiscoveryMechanism) } private val joinDecider: JoinDecider = { system.dynamicAccess .createInstanceFor[JoinDecider]( settings.joinDecider.implClass, List((classOf[ActorSystem], system), (classOf[ClusterBootstrapSettings], settings)) ) .get } private[this] val _selfContactPointUri: Promise[Uri] = Promise() override def routes(routeProviderSettings: ManagementRouteProviderSettings): Route = { log.info(s"Using self contact point address: ${routeProviderSettings.selfBaseUri}") this.setSelfContactPoint(routeProviderSettings.selfBaseUri) new HttpClusterBootstrapRoutes(settings).routes } def start(): Unit = if (Cluster(system).settings.SeedNodes.nonEmpty) { log.warning( "Application is configured with specific `akka.cluster.seed-nodes`: {}, bailing out of the bootstrap process! " + "If you want to use the automatic bootstrap mechanism, make sure to NOT set explicit seed nodes in the configuration. " + "This node will attempt to join the configured seed nodes.", Cluster(system).settings.SeedNodes.mkString("[", ", ", "]") ) } else if (bootstrapStep.compareAndSet(NotRunning, Initializing)) { log.info("Initiating bootstrap procedure using {} method...", settings.contactPointDiscovery.discoveryMethod) ensureSelfContactPoint() val bootstrapProps = BootstrapCoordinator.props(discovery, joinDecider, settings) val bootstrap = system.systemActorOf(bootstrapProps, "bootstrapCoordinator") // Bootstrap already logs in several other execution points when it can't form a cluster, and why. selfContactPoint.foreach { uri => bootstrap ! BootstrapCoordinator.Protocol.InitiateBootstrapping(uri) } } else log.warning("Bootstrap already initiated, yet start() method was called again. Ignoring.") private[bootstrap] object Internal { sealed trait BootstrapStep case object NotRunning extends BootstrapStep case object Initializing extends BootstrapStep } }
Example 123
Source File: AsyncController.scala From play-webpack-typescript-react with MIT License | 5 votes |
package controllers import akka.actor.ActorSystem import javax.inject._ import play.api.mvc._ import scala.concurrent.{ExecutionContext, Future, Promise} import scala.concurrent.duration._ def message = Action.async { getFutureMessage(1.second).map { msg => Ok(msg) } } private def getFutureMessage(delayTime: FiniteDuration): Future[String] = { val promise: Promise[String] = Promise[String]() actorSystem.scheduler.scheduleOnce(delayTime) { promise.success("Hi!"); () }(actorSystem.dispatcher) promise.future } }
Example 124
Source File: PlotServer.scala From DynaML with Apache License 2.0 | 5 votes |
package io.github.mandar2812.dynaml.graphics.charts.repl import unfiltered.request._ import unfiltered.response._ import scala.concurrent.duration.Duration import scala.concurrent.{Await, Promise} class PlotServer extends UnfilteredWebApp[UnfilteredWebApp.Arguments] { // this is fulfilled by the plot command, to allow a browser to wait for plot to reload var p = Promise[Unit]() private class WebApp extends unfiltered.filter.Plan { def intent = { // handle jsonp case req @ GET(Path(Seg("check" :: Nil)) & Params(params)) => implicit val responder = req val str = """[]""" val response = params.get("callback") match { case Some(v) => val callbackName = v.head s"$callbackName($str)" case _ => str } // block for plot command to fulfill promise, and release this result to trigger browser reload Await.result(p.future, Duration.Inf) JsonContent ~> ResponseString(response) case _ => Pass } } def parseArgs(args: Array[String]) = { val parsed = new UnfilteredWebApp.Arguments{} parsed.parse(args) parsed } def setup(parsed: UnfilteredWebApp.Arguments): unfiltered.filter.Plan = { new WebApp } def htmlRoot: String = "/" }
Example 125
Source File: Request.scala From scredis with Apache License 2.0 | 5 votes |
package scredis.protocol import java.nio.ByteBuffer import scredis.exceptions._ import scredis.serialization.UTF8StringReader import scala.concurrent.{Future, Promise} private[scredis] def reset(): Unit = { promise = Promise[A]() _buffer = null _bytes = null } private[scredis] def encode(): Unit = if (_buffer == null && _bytes == null) { command match { case x: ZeroArgCommand => _bytes = x.encoded case _ => _buffer = command.encode(args.toList) } } private[scredis] def encoded: Either[Array[Byte], ByteBuffer] = if (_bytes != null) { Left(_bytes) } else { Right(_buffer) } private[scredis] def complete(response: Response): Unit = { response match { case SimpleStringResponse("QUEUED") => case ClusterErrorResponse(error,message) => failure(RedisClusterErrorResponseException(error,message)) case ErrorResponse(message) => failure(RedisErrorResponseException(message)) case response => try { success(decode(response)) } catch { case e @ RedisTransactionAbortedException => failure(e) case e: RedisReaderException => failure(e) case e: Throwable => failure( RedisProtocolException(s"Unexpected response for request '$this': $response", e) ) } } } private[scredis] def success(value: Any): Unit = { try { promise.success(value.asInstanceOf[A]) } catch { case e: IllegalStateException => } finally { Protocol.release() } } private[scredis] def failure(throwable: Throwable): Unit = { try { promise.failure(throwable) } catch { case e: IllegalStateException => } finally { Protocol.release() } } def decode: Decoder[A] def argsCount: Int = args.size def isReadOnly: Boolean = command.isReadOnly override def toString: String = (command +: args).map { case bytes: Array[Byte] => UTF8StringReader.read(bytes) case x => x.toString }.mkString(" ") }
Example 126
Source File: EsHelper.scala From akka-stream-extensions with Apache License 2.0 | 5 votes |
package com.mfglabs.stream package extensions.elasticsearch import org.elasticsearch.action.{ActionListener, ListenableActionFuture} import scala.concurrent.{Future, Promise} object EsHelper { implicit class RichListenableActionFuture[T](laf: ListenableActionFuture[T]) { def asScala: Future[T] = { val p = Promise[T]() laf.addListener(new ActionListener[T] { def onResponse(response: T) = p.success(response) def onFailure(e: Throwable) = p.failure(e) def onFailure(e: Exception) = p.failure(e) } ) p.future } } }
Example 127
Source File: ZMQActor.scala From eclair with Apache License 2.0 | 5 votes |
package fr.acinq.eclair.blockchain.bitcoind.zmq import akka.Done import akka.actor.{Actor, ActorLogging} import fr.acinq.bitcoin.{Block, Transaction} import fr.acinq.eclair.blockchain.{NewBlock, NewTransaction} import org.zeromq.ZMQ.Event import org.zeromq.{SocketType, ZContext, ZMQ, ZMsg} import scala.annotation.tailrec import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Promise} import scala.util.Try class ZMQActor(address: String, connected: Option[Promise[Done]] = None) extends Actor with ActorLogging { import ZMQActor._ val ctx = new ZContext val subscriber = ctx.createSocket(SocketType.SUB) subscriber.monitor("inproc://events", ZMQ.EVENT_CONNECTED | ZMQ.EVENT_DISCONNECTED) subscriber.connect(address) subscriber.subscribe("rawblock".getBytes(ZMQ.CHARSET)) subscriber.subscribe("rawtx".getBytes(ZMQ.CHARSET)) val monitor = ctx.createSocket(SocketType.PAIR) monitor.connect("inproc://events") implicit val ec: ExecutionContext = context.system.dispatcher // we check messages in a non-blocking manner with an interval, making sure to retrieve all messages before waiting again @tailrec final def checkEvent: Unit = Option(Event.recv(monitor, ZMQ.DONTWAIT)) match { case Some(event) => self ! event checkEvent case None => () } @tailrec final def checkMsg: Unit = Option(ZMsg.recvMsg(subscriber, ZMQ.DONTWAIT)) match { case Some(msg) => self ! msg checkMsg case None => () } self ! Symbol("checkEvent") self ! Symbol("checkMsg") override def receive: Receive = { case Symbol("checkEvent") => checkEvent context.system.scheduler.scheduleOnce(1 second, self, Symbol("checkEvent")) case Symbol("checkMsg") => checkMsg context.system.scheduler.scheduleOnce(1 second, self, Symbol("checkMsg")) case event: Event => event.getEvent match { case ZMQ.EVENT_CONNECTED => log.info(s"connected to ${event.getAddress}") Try(connected.map(_.success(Done))) context.system.eventStream.publish(ZMQConnected) case ZMQ.EVENT_DISCONNECTED => log.warning(s"disconnected from ${event.getAddress}") context.system.eventStream.publish(ZMQDisconnected) case x => log.error(s"unexpected event $x") } case msg: ZMsg => msg.popString() match { case "rawblock" => val block = Block.read(msg.pop().getData) log.debug("received blockid={}", block.blockId) context.system.eventStream.publish(NewBlock(block)) case "rawtx" => val tx = Transaction.read(msg.pop().getData) log.debug("received txid={}", tx.txid) context.system.eventStream.publish(NewTransaction(tx)) case topic => log.warning(s"unexpected topic=$topic") } } override def postStop(): Unit = { ctx.close() } } object ZMQActor { // @formatter:off sealed trait ZMQEvent case object ZMQConnected extends ZMQEvent case object ZMQDisconnected extends ZMQEvent // @formatter:on }
Example 128
Source File: Server.scala From eclair with Apache License 2.0 | 5 votes |
package fr.acinq.eclair.io import java.net.InetSocketAddress import akka.Done import akka.actor.{Actor, ActorRef, DiagnosticActorLogging, Props} import akka.event.Logging.MDC import akka.io.Tcp.SO.KeepAlive import akka.io.{IO, Tcp} import fr.acinq.eclair.Logs.LogCategory import fr.acinq.eclair.{Logs, NodeParams} import scala.concurrent.Promise class Server(nodeParams: NodeParams, switchboard: ActorRef, router: ActorRef, address: InetSocketAddress, bound: Option[Promise[Done]] = None) extends Actor with DiagnosticActorLogging { import Tcp._ import context.system IO(Tcp) ! Bind(self, address, options = KeepAlive(true) :: Nil, pullMode = true) def receive() = { case Bound(localAddress) => bound.map(_.success(Done)) log.info(s"bound on $localAddress") // Accept connections one by one sender() ! ResumeAccepting(batchSize = 1) context.become(listening(sender())) case CommandFailed(_: Bind) => bound.map(_.failure(new RuntimeException("TCP bind failed"))) context stop self } def listening(listener: ActorRef): Receive = { case Connected(remote, _) => log.info(s"connected to $remote") val connection = sender val peerConnection = context.actorOf(PeerConnection.props( nodeParams = nodeParams, switchboard = switchboard, router = router )) peerConnection ! PeerConnection.PendingAuth(connection, remoteNodeId_opt = None, address = remote, origin_opt = None) listener ! ResumeAccepting(batchSize = 1) } override def mdc(currentMessage: Any): MDC = Logs.mdc(Some(LogCategory.CONNECTION)) } object Server { def props(nodeParams: NodeParams, switchboard: ActorRef, router: ActorRef, address: InetSocketAddress, bound: Option[Promise[Done]] = None): Props = Props(new Server(nodeParams, switchboard, router: ActorRef, address, bound)) }
Example 129
Source File: WaitForFundingCreatedInternalStateSpec.scala From eclair with Apache License 2.0 | 5 votes |
package fr.acinq.eclair.channel.states.b import akka.testkit.{TestFSMRef, TestProbe} import fr.acinq.bitcoin.{ByteVector32, Satoshi} import fr.acinq.eclair.TestConstants.{Alice, Bob} import fr.acinq.eclair.blockchain.{MakeFundingTxResponse, TestWallet} import fr.acinq.eclair.channel._ import fr.acinq.eclair.channel.states.StateTestsHelperMethods import fr.acinq.eclair.wire._ import fr.acinq.eclair.{TestConstants, TestKitBaseClass} import org.scalatest.Outcome import org.scalatest.funsuite.FixtureAnyFunSuiteLike import scodec.bits.ByteVector import scala.concurrent.duration._ import scala.concurrent.{Future, Promise} class WaitForFundingCreatedInternalStateSpec extends TestKitBaseClass with FixtureAnyFunSuiteLike with StateTestsHelperMethods { case class FixtureParam(alice: TestFSMRef[State, Data, Channel], alice2bob: TestProbe, bob2alice: TestProbe, alice2blockchain: TestProbe) override def withFixture(test: OneArgTest): Outcome = { val noopWallet = new TestWallet { override def makeFundingTx(pubkeyScript: ByteVector, amount: Satoshi, feeRatePerKw: Long): Future[MakeFundingTxResponse] = Promise[MakeFundingTxResponse].future // will never be completed } val setup = init(wallet = noopWallet) import setup._ val aliceInit = Init(Alice.channelParams.features) val bobInit = Init(Bob.channelParams.features) within(30 seconds) { alice ! INPUT_INIT_FUNDER(ByteVector32.Zeroes, TestConstants.fundingSatoshis, TestConstants.pushMsat, TestConstants.feeratePerKw, TestConstants.feeratePerKw, Alice.channelParams, alice2bob.ref, bobInit, ChannelFlags.Empty, ChannelVersion.STANDARD) bob ! INPUT_INIT_FUNDEE(ByteVector32.Zeroes, Bob.channelParams, bob2alice.ref, aliceInit) alice2bob.expectMsgType[OpenChannel] alice2bob.forward(bob) bob2alice.expectMsgType[AcceptChannel] bob2alice.forward(alice) awaitCond(alice.stateName == WAIT_FOR_FUNDING_INTERNAL) withFixture(test.toNoArgTest(FixtureParam(alice, alice2bob, bob2alice, alice2blockchain))) } } test("recv Error") { f => import f._ alice ! Error(ByteVector32.Zeroes, "oops") awaitCond(alice.stateName == CLOSED) } test("recv CMD_CLOSE") { f => import f._ alice ! CMD_CLOSE(None) awaitCond(alice.stateName == CLOSED) } }
Example 130
Source File: PlayJaxwsClientCallback.scala From play-soap with Apache License 2.0 | 5 votes |
package play.soap import java.util import org.apache.cxf.endpoint.ClientCallback import scala.concurrent.Promise private[soap] class PlayJaxwsClientCallback(promise: Promise[Any], noResponseValue: Any = null) extends ClientCallback { override def handleResponse(ctx: util.Map[String, AnyRef], response: Array[AnyRef]) = { // If there's no return value, the response will be null if (response != null) { promise.trySuccess(response(0)) } else { promise.trySuccess(noResponseValue) } } override def handleException(ctx: util.Map[String, AnyRef], ex: Throwable) = { promise.tryFailure(ex) } }
Example 131
Source File: Actor.scala From effpi with MIT License | 5 votes |
// Effpi - verified message-passing programs in Dotty // Copyright 2019 Alceste Scalas and Elias Benussi // Released under the MIT License: https://opensource.org/licenses/MIT package effpi.actor import java.util.concurrent.atomic.AtomicInteger import scala.concurrent.{Future, Promise, Await} import effpi.channel.{Channel, InChannel, OutChannel, QueueChannel} import effpi.process.{ProcVar, Process, In} import effpi.system._ import scala.concurrent.duration.Duration abstract class Mailbox[+A] extends InChannel[A] private class MailboxImpl[A](c: InChannel[A]) extends Mailbox[A] { override val synchronous: Boolean = c.synchronous override val name: Option[String] = c.name override def receive()(implicit timeout: Duration) = c.receive()(timeout) override def poll() = c.poll() override def enqueue(i: (Map[ProcVar[_], (_) => Process], List[() => Process], In[InChannel[Any], Any, Any => Process])) = c.enqueue(i) override def dequeue() = c.dequeue() override def waiting = c.waiting } abstract class ActorRef[-A] extends OutChannel[A] { def ! = send } private class ActorRefImpl[A](c: OutChannel[A]) (maybeDual: Option[Mailbox[Any]]) extends ActorRef[A] { override val synchronous: Boolean = c.synchronous override val name: Option[String] = c.name override def send(v: A) = c.send(v) override val dualIn: Mailbox[Any] = maybeDual match { case None => new MailboxImpl(c.dualIn) case Some(d) => d } override def create[B](synchronous: Boolean, name: Option[String] = None): Channel[B] = { c.create[B](synchronous, name) } } protected[actor] abstract class ActorChannel[A] extends Channel[A] { val mbox: Mailbox[A] val ref: ActorRef[A] } private class ActorChannelImpl[A](override val mbox: Mailbox[A], override val ref: ActorRef[A]) extends ActorChannel[A] { assert(mbox.synchronous == ref.synchronous) override val synchronous: Boolean = mbox.synchronous assert(mbox.name == ref.name) override val name: Option[String] = mbox.name } object ActorChannel { def ask[Req, Resp](srv: ActorRef[Req], query: ActorRef[Resp] => Req) (implicit ps: ProcessSystem, timeout: Duration): Resp = { import effpi.process.{dsl => pdsl} val respPromise = Promise[Resp]() val respFuture = respPromise.future val pipe = ActorChannel[Resp]() val askProcess = { pdsl.send(srv, query(pipe.ref)) >> pdsl.receive(pipe.mbox) { msg: Resp => respPromise.success(msg) pdsl.nil } } askProcess.spawn(ps) Await.result(respFuture, Duration.Inf) } }
Example 132
Source File: ForkJoinCreation.scala From effpi with MIT License | 5 votes |
// Effpi - verified message-passing programs in Dotty // Copyright 2019 Alceste Scalas and Elias Benussi // Released under the MIT License: https://opensource.org/licenses/MIT package effpi.benchmarks.effpi import scala.concurrent.duration.Duration import scala.concurrent.{ Future, Promise, Await } import scala.concurrent.ExecutionContext.Implicits.global import effpi.actor.ActorRef import effpi.actor.dsl._ import effpi.process._ import effpi.process.dsl.{Yielding, pyield, Loop, Rec, rec => prec, loop => ploop} import effpi.system._ object ForkJoinCreation { implicit val timeout: Duration = Duration.Inf case class Message(msg: String) type SimpleActor = Read[Message, PNil] val simpleActor = Behavior[Message, SimpleActor] { read { _ => nil } } type Sender = Rec[RecAt, (SendTo[ActorRef[Message], Message] >>: Loop[RecAt] | PNil)] def sender(receivers: Array[ActorRef[Message]])(endTimePromise: Promise[Long]) = Behavior[Nothing, Sender] { var i = 0 prec(RecA) { if (i < receivers.length) { send(receivers(i), Message("Hello, World!")) >> { i += 1 ploop(RecA) } } else { endTimePromise.success(System.nanoTime()) nil } } } def bench(numActors: Int, psC: () => ProcessSystem) = { implicit val ps = psC() val endTimePromise = Promise[Long]() val endTimeFuture = endTimePromise.future val startTime = System.nanoTime() val simpleActorsRef = (1 to numActors).toArray.map{ _ => Actor.spawn(simpleActor)} Actor.spawn(sender(simpleActorsRef)(endTimePromise))(ps) val endTime = Await.result(endTimeFuture, Duration.Inf) val creationDuration = endTime - startTime ps.kill() creationDuration } }
Example 133
Source File: CancellableFuturePool.scala From almond with BSD 3-Clause "New" or "Revised" License | 5 votes |
package almond.interpreter.util import java.lang.Thread.UncaughtExceptionHandler import java.util.concurrent.{Executors, ThreadFactory} import almond.logger.LoggerContext import scala.concurrent.{Future, Promise} import scala.util.control.NonFatal import scala.util.{Failure, Success} final class CancellableFuturePool( logCtx: LoggerContext ) { private val log = logCtx(getClass) private val pool = Executors.newCachedThreadPool( // from scalaz.concurrent.Strategy.DefaultDaemonThreadFactory new ThreadFactory { val defaultThreadFactory = Executors.defaultThreadFactory() def newThread(r: Runnable) = { val t = defaultThreadFactory.newThread(r) t.setDaemon(true) t.setUncaughtExceptionHandler( new UncaughtExceptionHandler { def uncaughtException(t: Thread, e: Throwable) = log.warn(s"Uncaught exception in thread $t", e) } ) t } } ) def future[T](result: => T): Future[T] = { val p = Promise[T]() pool.submit( new Runnable { def run() = p.complete { try Success(result) catch { case NonFatal(e) => Failure(e) } } } ) p.future } def cancellableFuture[T](result: T): CancellableFuture[T] = { @volatile var completionThreadOpt = Option.empty[Thread] def result0(): T = { completionThreadOpt = Some(Thread.currentThread()) try result finally { completionThreadOpt = None } } def cancel(): Unit = for (t <- completionThreadOpt) t.stop() CancellableFuture(future(result0()), () => cancel()) } def shutdown(): Unit = pool.shutdown() }
Example 134
Source File: ReactRefTest.scala From slinky with MIT License | 5 votes |
package slinky.core import org.scalajs.dom import org.scalajs.dom.html import slinky.core.facade.React import slinky.web.ReactDOM import slinky.web.html.{div, ref} import scala.concurrent.Promise import org.scalatest.Assertion import org.scalatest.funsuite.AsyncFunSuite class ReactRefTest extends AsyncFunSuite { test("Can pass in a ref object to an HTML tag and use it") { val elemRef = React.createRef[html.Div] ReactDOM.render( div(ref := elemRef)("hello!"), dom.document.createElement("div") ) assert(elemRef.current.innerHTML == "hello!") } test("Can pass in a ref object to a Slinky component and use it") { val promise: Promise[Assertion] = Promise() val ref = React.createRef[TestForceUpdateComponent.Def] ReactDOM.render( TestForceUpdateComponent(() => promise.success(assert(true))).withRef(ref), dom.document.createElement("div") ) ref.current.forceUpdate() promise.future } test("Can use forwardRef to pass down a ref to a lower element") { val forwarded = React.forwardRef[String, html.Div](FunctionalComponent((props, rf) => { div(ref := rf)(props) })) val divRef = React.createRef[html.Div] ReactDOM.render( forwarded("hello").withRef(divRef), dom.document.createElement("div") ) assert(divRef.current.innerHTML == "hello") } }
Example 135
Source File: DiscordClientActor.scala From AckCord with MIT License | 5 votes |
package ackcord import scala.concurrent.duration.FiniteDuration import scala.concurrent.{Future, Promise} import ackcord.requests.Ratelimiter import akka.Done import akka.actor.CoordinatedShutdown import akka.actor.typed._ import akka.actor.typed.scaladsl._ import akka.actor.typed.scaladsl.adapter._ import akka.pattern.gracefulStop class DiscordClientActor( ctx: ActorContext[DiscordClientActor.Command], shardBehaviors: Seq[Behavior[DiscordShard.Command]], cache: Cache ) extends AbstractBehavior[DiscordClientActor.Command](ctx) { import DiscordClientActor._ implicit val system: ActorSystem[Nothing] = context.system import system.executionContext val shards: Seq[ActorRef[DiscordShard.Command]] = shardBehaviors.zipWithIndex.map(t => context.spawn(t._1, s"Shard${t._2}")) var shardShutdownManager: ActorRef[DiscordShard.StopShard.type] = _ val musicManager: ActorRef[MusicManager.Command] = context.spawn(MusicManager(cache), "MusicManager") val rateLimiter: ActorRef[Ratelimiter.Command] = context.spawn(Ratelimiter(), "Ratelimiter") private val shutdown = CoordinatedShutdown(system.toClassic) shutdown.addTask("service-stop", "stop-discord") { () => gracefulStop(shardShutdownManager.toClassic, shutdown.timeout("service-stop"), DiscordShard.StopShard) .map(_ => Done) } def login(): Unit = { require(shardShutdownManager == null, "Already logged in") shardShutdownManager = context.spawn(ShardShutdownManager(shards), "ShardShutdownManager") DiscordShard.startShards(shards) } def logout(timeout: FiniteDuration): Future[Boolean] = { import akka.actor.typed.scaladsl.adapter._ val promise = Promise[Boolean] require(shardShutdownManager != null, "Not logged in") promise.completeWith(gracefulStop(shardShutdownManager.toClassic, timeout, DiscordShard.StopShard)) promise.future } override def onMessage(msg: Command): Behavior[Command] = { msg match { case DiscordClientActor.Login => login() case Logout(timeout, replyTo) => replyTo ! LogoutReply(logout(timeout)) case GetShards(replyTo) => replyTo ! GetShardsReply(shards) case GetMusicManager(replyTo) => replyTo ! GetMusicManagerReply(musicManager) case GetRatelimiter(replyTo) => replyTo ! GetRatelimiterReply(rateLimiter) } Behaviors.same } } object DiscordClientActor { def apply( shardBehaviors: Seq[Behavior[DiscordShard.Command]], cache: Cache ): Behavior[Command] = Behaviors.setup(ctx => new DiscordClientActor(ctx, shardBehaviors, cache)) sealed trait Command case object Login extends Command case class Logout(timeout: FiniteDuration, replyTo: ActorRef[LogoutReply]) extends Command case class GetShards(replyTo: ActorRef[GetShardsReply]) extends Command case class GetMusicManager(replyTo: ActorRef[GetMusicManagerReply]) extends Command case class GetRatelimiter(replyTo: ActorRef[GetRatelimiterReply]) extends Command case class LogoutReply(done: Future[Boolean]) case class GetShardsReply(shards: Seq[ActorRef[DiscordShard.Command]]) case class GetMusicManagerReply(musicManager: ActorRef[MusicManager.Command]) case class GetRatelimiterReply(ratelimiter: ActorRef[Ratelimiter.Command]) }
Example 136
Source File: VoiceUDPFlow.scala From AckCord with MIT License | 5 votes |
package ackcord.voice import java.net.InetSocketAddress import java.nio.ByteOrder import scala.concurrent.{Future, Promise} import ackcord.data.{RawSnowflake, UserId} import ackcord.util.UdpConnectedFlow import akka.NotUsed import akka.actor.typed.ActorSystem import akka.stream.scaladsl.{BidiFlow, Concat, Flow, GraphDSL, Keep, Source} import akka.stream.{BidiShape, OverflowStrategy} import akka.util.ByteString object VoiceUDPFlow { val silence = ByteString(0xF8, 0xFF, 0xFE) val SampleRate = 48000 val FrameSize = 960 val FrameTime = 20 def flow[Mat]( remoteAddress: InetSocketAddress, ssrc: Int, serverId: RawSnowflake, userId: UserId, secretKeys: Source[Option[ByteString], Mat] )(implicit system: ActorSystem[Nothing]): Flow[ByteString, AudioAPIMessage.ReceivedData, (Mat, Future[FoundIP])] = NaclBidiFlow .bidiFlow(ssrc, serverId, userId, secretKeys) .atopMat(voiceBidi(ssrc).reversed)(Keep.both) .async .join(Flow[ByteString].buffer(32, OverflowStrategy.backpressure).via(UdpConnectedFlow.flow(remoteAddress))) def voiceBidi(ssrc: Int): BidiFlow[ByteString, ByteString, ByteString, ByteString, Future[FoundIP]] = { implicit val byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN val ipDiscoveryPacket = { val byteBuilder = ByteString.createBuilder byteBuilder.sizeHint(74) byteBuilder.putShort(0x1).putShort(70).putInt(ssrc) byteBuilder.putBytes(new Array[Byte](66)) byteBuilder.result() } val valvePromise = Promise[Unit] val valve = Source.future(valvePromise.future).drop(1).asInstanceOf[Source[ByteString, NotUsed]] val ipDiscoveryFlow = Flow[ByteString] .viaMat(new IPDiscoveryFlow(() => valvePromise.success(())))(Keep.right) BidiFlow .fromGraph(GraphDSL.create(ipDiscoveryFlow) { implicit b => ipDiscovery => import GraphDSL.Implicits._ val voiceIn = b.add(Flow[ByteString]) val ipDiscoverySource = b.add(Source.single(ipDiscoveryPacket) ++ valve) val ipDiscoveryAndThenVoiceData = b.add(Concat[ByteString]()) ipDiscoverySource ~> ipDiscoveryAndThenVoiceData voiceIn ~> ipDiscoveryAndThenVoiceData BidiShape( ipDiscovery.in, ipDiscovery.out, voiceIn.in, ipDiscoveryAndThenVoiceData.out ) }) } case class FoundIP(address: String, port: Int) }
Example 137
Source File: IPDiscoveryFlow.scala From AckCord with MIT License | 5 votes |
package ackcord.voice import java.nio.ByteOrder import scala.concurrent.{Future, Promise} import akka.stream.scaladsl.Flow import akka.stream.stage._ import akka.stream.{Attributes, FlowShape, Inlet, Outlet} import akka.util.ByteString class IPDiscoveryFlow(openValve: () => Unit) extends GraphStageWithMaterializedValue[FlowShape[ByteString, ByteString], Future[VoiceUDPFlow.FoundIP]] { val in: Inlet[ByteString] = Inlet("IPDiscoveryFlow.in") val out: Outlet[ByteString] = Outlet("IPDiscoveryFlow.out") override def shape: FlowShape[ByteString, ByteString] = FlowShape(in, out) override def createLogicAndMaterializedValue( inheritedAttributes: Attributes ): (GraphStageLogic, Future[VoiceUDPFlow.FoundIP]) = { val promise = Promise[VoiceUDPFlow.FoundIP] val logic = new GraphStageLogicWithLogging(shape) with InHandler with OutHandler { override def onPush(): Unit = { val data = grab(in) log.debug(s"Grabbing data for IP discovery $data") val byteBuf = data.asByteBuffer.order(ByteOrder.BIG_ENDIAN) val tpe = byteBuf.getShort require(tpe == 0x2, s"Was expecting IP discovery result, got $tpe") byteBuf.getShort //Length byteBuf.getInt //SSRC val nullTermString = new Array[Byte](64) byteBuf.get(nullTermString) val address = new String(nullTermString, 0, nullTermString.iterator.takeWhile(_ != 0).length) val port = byteBuf.getChar.toInt //Char is unsigned short promise.success(VoiceUDPFlow.FoundIP(address, port)) log.debug("Success doing IP discovery") setHandler( in, new InHandler { override def onPush(): Unit = push(out, grab(in)) } ) openValve() } override def onPull(): Unit = pull(in) override def onUpstreamFailure(ex: Throwable): Unit = { promise.tryFailure(new Exception("Connection failed.", ex)) super.onUpstreamFailure(ex) } setHandlers(in, out, this) } (logic, promise.future) } } object IPDiscoveryFlow { def flow(openValve: () => Unit): Flow[ByteString, ByteString, Future[VoiceUDPFlow.FoundIP]] = Flow.fromGraph(new IPDiscoveryFlow(openValve)) }
Example 138
Source File: GatewayProcessor.scala From ez-framework with Apache License 2.0 | 5 votes |
package com.ecfront.ez.framework.service.gateway import java.util.Date import com.ecfront.common.{JsonHelper, Resp} import com.ecfront.ez.framework.core.eventbus.EventBusProcessor import com.ecfront.ez.framework.core.helper.TimeHelper import com.ecfront.ez.framework.core.interceptor.EZAsyncInterceptorProcessor import com.ecfront.ez.framework.core.logger.Logging import com.ecfront.ez.framework.core.rpc._ import com.ecfront.ez.framework.core.{EZ, EZContext} import com.ecfront.ez.framework.service.gateway.interceptor.{EZAPIContext, GatewayInterceptor} import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Promise trait GatewayProcessor extends Logging { protected val HTTP_STATUS_200: Int = 200 protected val HTTP_STATUS_302: Int = 302 protected val FLAG_PROXY: String = "X-Forwarded-For" protected def execute(body: String, context: EZAPIContext, resultFun: Resp[(EZAPIContext, Map[String, Any])] => Unit): Unit = { if (EZ.isDebug) { logger.trace(s"Execute a request [${context.method}][${context.realUri}],from ${context.remoteIP} | $body") } EZAsyncInterceptorProcessor.process[EZAPIContext](GatewayInterceptor.category, context, { (context, _) => val p = Promise[Resp[EZAPIContext]]() val msg = EventBusProcessor.toAllowedMessage(body) val cxt = new EZContext cxt.id = EZ.createUUID cxt.startTime = TimeHelper.msf.format(new Date).toLong cxt.sourceIP = EZ.Info.projectIp cxt.sourceRPCPath = context.realUri if (context.optInfo.isDefined) { cxt.token = context.optInfo.get.token cxt.optAccCode = context.optInfo.get.accountCode cxt.optOrgCode = context.optInfo.get.organizationCode } EZContext.setContext(cxt) // 最长10分钟 EZ.eb.ackAsync[Resp[Any]](EZ.eb.packageAddress(context.method, context.templateUri), msg, context.parameters, 600 * 1000) { (replyMessage, replyHeader) => context.executeResult = if (replyHeader.contains(RPCProcessor.RESP_TYPE_FLAG)) { replyHeader(RPCProcessor.RESP_TYPE_FLAG) match { case "DownloadFile" => JsonHelper.toObject[Resp[DownloadFile]](replyMessage) case "ReqFile" => JsonHelper.toObject[Resp[ReqFile]](replyMessage) case "Raw" => JsonHelper.toObject[Resp[Raw]](replyMessage) case "RespRedirect" => JsonHelper.toObject[Resp[RespRedirect]](replyMessage) case _ => replyMessage } } else { replyMessage } p.success(Resp.success(context)) } p.future }).onSuccess { case resp => resultFun(resp) } } }
Example 139
Source File: SimpleContextThreadApp.scala From aloha with Apache License 2.0 | 5 votes |
package me.jrwang.app import scala.concurrent.duration.Duration import scala.concurrent.{Await, Promise} import me.jrwang.aloha.app.{AbstractApplication, ExitCode, ExitState} class SimpleContextThreadApp extends AbstractApplication { val workThread: Thread = new Thread(new Runnable { override def run(): Unit = { try { 1 to 100 foreach { _ => println("...") Thread.sleep(1000) } result.success(ExitState(ExitCode.SUCCESS, Some("success"))) } catch { case _: InterruptedException => result.success(ExitState(ExitCode.FAILED, Some("killed"))) } } }) override def start(): Promise[ExitState] = { println("Start job.") workThread.start() result } override def shutdown(reason: Option[String]): Unit = { println("Kill job.") workThread.interrupt() } override def clean(): Unit = {} } object SimpleContextThreadApp { def main(args: Array[String]): Unit = { val job = new SimpleContextThreadApp val result = job.start() new Thread(new Runnable { override def run(): Unit = { Thread.sleep(10000) job.shutdown(Some("No reason")) } }).start() try { val a = Await.result(result.future, Duration.Inf) println(a) } catch { case e: Throwable => e.printStackTrace() } } }
Example 140
Source File: Application.scala From aloha with Apache License 2.0 | 5 votes |
package me.jrwang.aloha.app import java.io.File import scala.concurrent.Promise import me.jrwang.aloha.common.{AlohaConf, AlohaException, Logging} import me.jrwang.aloha.scheduler.AlohaUserCodeClassLoaders trait Application { def start(): Promise[ExitState] def shutdown(reason: Option[String]): Unit def withDescription(desc: ApplicationDescription): Application def withApplicationDir(appDir: File): Application def withAlohaConf(conf: AlohaConf): Application def clean(): Unit } object Application extends Logging { def create(appDesc: ApplicationDescription): Application = { //TODO we should download dependencies and resource files logInfo(s"Create module for [$appDesc]") val fullClassName = appDesc.entryPoint try { val urls = appDesc.libs.map(new File(_)).filter(_.exists()) .flatMap(_.listFiles().filter(_.isFile)).map(_.toURI.toURL) val classLoader = AlohaUserCodeClassLoaders.childFirst(urls) Thread.currentThread().setContextClassLoader(classLoader) val klass = classLoader.loadClass(fullClassName) require(classOf[Application].isAssignableFrom(klass), s"$fullClassName is not a subclass of ${classOf[Application].getName}.") klass.getConstructor().newInstance().asInstanceOf[Application].withDescription(appDesc) } catch { case _: NoSuchMethodException => throw new AlohaException( s"$fullClassName did not have a zero-argument constructor." + s"Note: if the class is defined inside of another Scala class, then its constructors " + s"may accept an implicit parameter that references the enclosing class; in this case, " + s"you must define the class as a top-level class in order to prevent this extra" + " parameter from breaking Atom's ability to find a valid constructor.") case e: Throwable => throw e } } }
Example 141
Source File: AbstractApplication.scala From aloha with Apache License 2.0 | 5 votes |
package me.jrwang.aloha.app import java.io.File import scala.concurrent.Promise import me.jrwang.aloha.common.AlohaConf abstract class AbstractApplication extends Application { protected val result: Promise[ExitState] = Promise() protected var appDesc: ApplicationDescription = _ protected var appDir: File = _ protected var alohaConf: AlohaConf = _ override def withDescription(desc: ApplicationDescription): Application = { this.appDesc = desc this } override def withApplicationDir(appDir: File): Application = { this.appDir = appDir this } override def withAlohaConf(conf: AlohaConf): Application = { this.alohaConf = conf this } }
Example 142
Source File: ApplicationWithProcess.scala From aloha with Apache License 2.0 | 5 votes |
package me.jrwang.aloha.app import java.io.File import java.nio.charset.StandardCharsets import scala.collection.JavaConverters._ import scala.concurrent.Promise import com.google.common.io.Files import me.jrwang.aloha.common.Logging import me.jrwang.aloha.common.util.{FileAppender, Utils} abstract class ApplicationWithProcess extends AbstractApplication with Logging { private var process: Process = _ private var stdoutAppender: FileAppender = _ private var stderrAppender: FileAppender = _ // Timeout to wait for when trying to terminate an app. private val APP_TERMINATE_TIMEOUT_MS = 10 * 1000 def getProcessBuilder(): ProcessBuilder private var stateMonitorThread: Thread = _ override def start(): Promise[ExitState] = { val processBuilder = getProcessBuilder() val command = processBuilder.command() val formattedCommand = command.asScala.mkString("\"", "\" \"", "\"") logInfo(s"Launch command: $formattedCommand") processBuilder.directory(appDir) process = processBuilder.start() // Redirect its stdout and stderr to files val stdout = new File(appDir, "stdout") stdoutAppender = FileAppender(process.getInputStream, stdout, alohaConf) val header = "Aloha Application Command: %s\n%s\n\n".format( formattedCommand, "=" * 40) val stderr = new File(appDir, "stderr") Files.write(header, stderr, StandardCharsets.UTF_8) stderrAppender = FileAppender(process.getErrorStream, stderr, alohaConf) stateMonitorThread = new Thread("app-state-monitor-thread") { override def run(): Unit = { val exitCode = process.waitFor() if(exitCode == 0) { result.success(ExitState(ExitCode.SUCCESS, Some("success"))) } else { result.success(ExitState(ExitCode.FAILED, Some("failed"))) } } } stateMonitorThread.start() result } override def shutdown(reason: Option[String]): Unit = { if (process != null) { logInfo("Killing process!") if (stdoutAppender != null) { stdoutAppender.stop() } if (stderrAppender != null) { stderrAppender.stop() } val exitCode = Utils.terminateProcess(process, APP_TERMINATE_TIMEOUT_MS) if (exitCode.isEmpty) { logWarning("Failed to terminate process: " + process + ". This process will likely be orphaned.") } } } }
Example 143
Source File: NettyRpcCallContext.scala From aloha with Apache License 2.0 | 5 votes |
package me.jrwang.aloha.rpc.netty import scala.concurrent.Promise import io.netty.buffer.Unpooled import me.jrwang.aloha.rpc.{RpcAddress, RpcCallContext} import me.jrwang.aloha.transport.client.RpcResponseCallback abstract class NettyRpcCallContext(override val senderAddress: RpcAddress) extends RpcCallContext { protected def send(message: Any): Unit override def reply(response: Any): Unit = { send(response) } override def sendFailure(e: Throwable): Unit = { send(RpcFailure(e)) } } class RemoteNettyRpcCallContext( nettyEnv: NettyRpcEnv, callback: RpcResponseCallback, senderAddress: RpcAddress) extends NettyRpcCallContext(senderAddress) { override protected def send(message: Any): Unit = { val reply = nettyEnv.serialize(message) callback.onSuccess(Unpooled.wrappedBuffer(reply)) } }
Example 144
Source File: FutureConverters.scala From firebase4s with MIT License | 5 votes |
package com.firebase4s.util import scala.concurrent.{Future, Promise} import com.google.api.core.ApiFuture object FutureConverters { private[firebase4s]def scalaFutureFromApiFuture[A](future: ApiFuture[A]): Future[A] = { val p = Promise[A] future.addListener(() => { try { p.success(future.get) } catch { case e: Exception => p.failure(e) } }, ExecutionContextExecutorServiceBridge(scala.concurrent.ExecutionContext.global)) p.future } }
Example 145
Source File: LegacyChannelInitializer.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.network.client import java.io.IOException import com.wavesplatform.network._ import com.wavesplatform.utils.ScorexLogging import io.netty.channel._ import io.netty.channel.socket.SocketChannel import io.netty.handler.codec.{LengthFieldBasedFrameDecoder, LengthFieldPrepender} import scala.concurrent.Promise import scala.concurrent.duration._ class ClientHandshakeHandler(handshake: Handshake, promise: Promise[Channel]) extends ChannelInboundHandlerAdapter with ScorexLogging { private def removeHandlers(ctx: ChannelHandlerContext): Unit = { ctx.pipeline().remove(classOf[HandshakeTimeoutHandler]) ctx.pipeline().remove(this) } override def channelRead(ctx: ChannelHandlerContext, msg: AnyRef): Unit = msg match { case HandshakeTimeoutExpired => log.error("Timeout expired while waiting for handshake") ctx.close() promise.failure(new IOException("No handshake")) case remoteHandshake: Handshake => if (handshake.applicationName != remoteHandshake.applicationName) { log.warn(s"Remote application name ${remoteHandshake.applicationName} does not match local ${handshake.applicationName}") ctx.close() } else { promise.success(ctx.channel()) log.info(s"Accepted handshake $remoteHandshake") removeHandlers(ctx) } case _ => super.channelRead(ctx, msg) } override def channelActive(ctx: ChannelHandlerContext): Unit = { ctx.writeAndFlush(handshake.encode(ctx.alloc().buffer())) super.channelActive(ctx) } } // Used only in tests and Generator class LegacyChannelInitializer(trafficLoggerSettings: TrafficLogger.Settings, handshake: Handshake, promise: Promise[Channel]) extends ChannelInitializer[SocketChannel] { private val lengthFieldLength = 4 private val maxFieldLength = 1024 * 1024 override def initChannel(ch: SocketChannel): Unit = ch.pipeline() .addLast( new HandshakeDecoder(PeerDatabase.NoOp), new HandshakeTimeoutHandler(30.seconds), new ClientHandshakeHandler(handshake, promise), new LengthFieldPrepender(lengthFieldLength), new LengthFieldBasedFrameDecoder(maxFieldLength, 0, lengthFieldLength, 0, lengthFieldLength), new LegacyFrameCodec(PeerDatabase.NoOp, 3.minutes), new TrafficLogger(trafficLoggerSettings) ) }
Example 146
Source File: NetworkSender.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.network.client import java.io.IOException import java.net.InetSocketAddress import java.nio.channels.ClosedChannelException import com.wavesplatform.network.TrafficLogger import com.wavesplatform.utils.ScorexLogging import io.netty.channel.Channel import io.netty.channel.group.DefaultChannelGroup import io.netty.util.concurrent.GlobalEventExecutor import scala.concurrent.{ExecutionContext, Future, Promise} class NetworkSender(trafficLoggerSettings: TrafficLogger.Settings, chainId: Char, name: String, nonce: Long)(implicit ec: ExecutionContext) extends ScorexLogging { private[this] val MessagesBatchSize = 100 private[this] val allChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE) private[this] val client = new NetworkClient(trafficLoggerSettings, chainId, name, nonce, allChannels) def connect(address: InetSocketAddress): Future[Channel] = client.connect(address) def send(channel: Channel, messages: Any*): Future[Unit] = { def doWrite(messages: Seq[Any]): Future[Unit] = if (messages.isEmpty) Future.successful(()) else if (!channel.isWritable) Future.failed(new ClosedChannelException) else { val (send, keep) = messages.splitAt(MessagesBatchSize) val futures = send.toVector.map { msg => val result = Promise[Unit]() channel.write(msg).addListener { (f: io.netty.util.concurrent.Future[Void]) => if (!f.isSuccess) { val cause = Option(f.cause()).getOrElse(new IOException("Can't send a message to the channel")) log.error(s"Can't send a message to the channel: $msg", cause) result.failure(cause) } else { result.success(()) } } result.future } channel.flush() Future.sequence(futures).flatMap(_ => doWrite(keep)) } doWrite(messages) } def close(): Unit = client.shutdown() }
Example 147
Source File: NetworkClient.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.network.client import java.io.IOException import java.net.InetSocketAddress import com.wavesplatform.Version import com.wavesplatform.network.{Handshake, TrafficLogger} import com.wavesplatform.settings._ import com.wavesplatform.utils.ScorexLogging import io.netty.bootstrap.Bootstrap import io.netty.channel._ import io.netty.channel.group.ChannelGroup import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.nio.NioSocketChannel import scala.concurrent.{Future, Promise} class NetworkClient(trafficLoggerSettings: TrafficLogger.Settings, chainId: Char, nodeName: String, nonce: Long, allChannels: ChannelGroup) extends ScorexLogging { private val workerGroup = new NioEventLoopGroup() private val handshake = Handshake(Constants.ApplicationName + chainId, Version.VersionTuple, nodeName, nonce, None) def connect(remoteAddress: InetSocketAddress): Future[Channel] = { val p = Promise[Channel] val bootstrap = new Bootstrap() .group(workerGroup) .channel(classOf[NioSocketChannel]) .handler(new LegacyChannelInitializer(trafficLoggerSettings, handshake, p)) log.debug(s"Connecting to $remoteAddress") val channelFuture = bootstrap.connect(remoteAddress) channelFuture.addListener((_: io.netty.util.concurrent.Future[Void]) => { log.debug(s"Connected to $remoteAddress") channelFuture.channel().write(p) }) val channel = channelFuture.channel() allChannels.add(channel) channel.closeFuture().addListener { (chf: ChannelFuture) => if (!p.isCompleted) { val cause = Option(chf.cause()).getOrElse(new IllegalStateException("The connection is closed before handshake")) p.failure(new IOException(cause)) } log.debug(s"Connection to $remoteAddress closed") allChannels.remove(chf.channel()) } p.future } def shutdown(): Unit = try { allChannels.close().await() log.debug("Closed all channels") } finally { workerGroup.shutdownGracefully() } }
Example 148
Source File: package.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.it import com.wavesplatform.settings.Constants import com.wavesplatform.state.DataEntry import io.netty.util.Timer import scala.concurrent.duration.FiniteDuration import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.control.NonFatal package object util { implicit class TimerExt(val timer: Timer) extends AnyVal { def schedule[A](f: => Future[A], delay: FiniteDuration): Future[A] = { val p = Promise[A] try { timer.newTimeout(_ => p.completeWith(f), delay.length, delay.unit) } catch { case NonFatal(e) => p.failure(e) } p.future } def sleep(term: FiniteDuration): Future[Unit] = schedule(Future.successful(()), term) def retryUntil[A](f: => Future[A], cond: A => Boolean, retryInterval: FiniteDuration)(implicit ec: ExecutionContext): Future[A] = f.flatMap(v => if (cond(v)) Future.successful(v) else schedule(retryUntil(f, cond, retryInterval), retryInterval)) } implicit class DoubleExt(val d: Double) extends AnyVal { def waves: Long = (BigDecimal(d) * Constants.UnitsInWave).toLong } implicit class TypedDataEntry(entry: DataEntry[_]) { def as[T]: T = entry.asInstanceOf[T] } }
Example 149
Source File: CassandraWrapper.scala From stream-reactor with Apache License 2.0 | 5 votes |
package com.datamountaineer.streamreactor.connect.cassandra import com.datastax.driver.core.{ResultSet, ResultSetFuture} import com.google.common.util.concurrent.{FutureCallback, Futures} import scala.concurrent.{Future, Promise} import scala.language.{implicitConversions, postfixOps} object CassandraWrapper { implicit def resultSetFutureToScala(f: ResultSetFuture): Future[ResultSet] = { val p = Promise[ResultSet]() Futures.addCallback(f, new FutureCallback[ResultSet] { def onSuccess(r: ResultSet): Unit = p success r def onFailure(t: Throwable): Unit = p failure t }) p.future } }
Example 150
Source File: CassandraResultSetWrapper.scala From stream-reactor with Apache License 2.0 | 5 votes |
package com.datamountaineer.streamreactor.connect.cassandra.utils implicit def resultSetFutureToScala(f: ResultSetFuture): Future[ResultSet] = { val p = Promise[ResultSet]() Futures.addCallback(f, new FutureCallback[ResultSet] { def onSuccess(r: ResultSet): Unit = p success r def onFailure(t: Throwable): Unit = p failure t }) p.future } }
Example 151
Source File: EtlGraphImpl.scala From fusion-data with Apache License 2.0 | 5 votes |
package mass.rdp.etl.graph import akka.NotUsed import akka.stream.scaladsl.{ Sink, Source } import com.typesafe.scalalogging.StrictLogging import javax.script.SimpleBindings import mass.connector.Connector import mass.connector.sql._ import mass.core.event.{ EventData, EventDataSimple } import mass.core.script.ScriptManager import mass.rdp.RdpSystem import mass.rdp.etl.{ EtlResult, EtlWorkflowExecution, SqlEtlResult } import scala.collection.immutable import scala.concurrent.{ Future, Promise } import scala.util.{ Failure, Success } case class EtlGraphImpl(graphSetting: EtlGraphSetting) extends EtlGraph with StrictLogging { override def run(connectors: immutable.Seq[Connector], rdpSystem: RdpSystem): EtlWorkflowExecution = { implicit val ec = rdpSystem.materializer.system.dispatcher implicit val mat = rdpSystem.materializer def getConnector(name: String): Connector = connectors.find(_.name == name) orElse rdpSystem.connectorSystem.getConnector(name) getOrElse (throw new EtlGraphException(s"connector ref: $name 不存在")) val promise = Promise[EtlResult]() val source = dataSource(getConnector(graphSource.connector.ref), rdpSystem) val sink = dataSink(getConnector(graphSink.connector.ref), rdpSystem) graphFlows .foldLeft(source)((s, etlFlow) => s.map { event => val engine = ScriptManager.scriptJavascript val bindings = new SimpleBindings() bindings.put("event", event.asInstanceOf[EventDataSql]) val data = engine.eval(etlFlow.script.content.get, bindings) // TODO 在此可设置是否发送通知消息给在线监控系统 logger.debug(s"engine: $engine, event: $event, result data: $data") EventDataSimple(data) }) .runWith(sink) .onComplete { case Success(result) => promise.success(SqlEtlResult(result)) case Failure(e) => promise.failure(e) } new EtlWorkflowExecution(promise, () => ()) } private def dataSource(connector: Connector, rdpSystem: RdpSystem): Source[EventData, NotUsed] = rdpSystem.streamFactories.get(connector.`type`.toString) match { case Some(b) => b.buildSource(connector, graphSource) case _ => throw new EtlGraphException(s"未知Connector: $connector") } private def dataSink(connector: Connector, rdpSystem: RdpSystem): Sink[EventData, Future[JdbcSinkResult]] = rdpSystem.streamFactories.get(connector.`type`.toString) match { case Some(b) => b.buildSink(connector, graphSink) case _ => throw new EtlGraphException(s"未知Connector: $connector") } }
Example 152
Source File: MaterializeValue.scala From fusion-data with Apache License 2.0 | 5 votes |
package example.akkastream.graph import akka.NotUsed import akka.actor.ActorSystem import akka.stream.ActorMaterializer import akka.stream.scaladsl.{ Flow, Keep, RunnableGraph, Sink, Source, Tcp } import akka.util.ByteString import scala.concurrent.{ Future, Promise } object MaterializeValue { implicit val system = ActorSystem() implicit val mat = ActorMaterializer() import system.dispatcher case class MyClass(private val p: Promise[Option[Int]], conn: Tcp.OutgoingConnection) extends AutoCloseable { override def close(): Unit = p.trySuccess(None) } // Materializes to Promise[Option[Int]] val source: Source[Int, Promise[Option[Int]]] = Source.maybe[Int] // Materializes to NotUsed val flow1: Flow[Int, Int, NotUsed] = Flow[Int].take(100) // Materializes to Promise[Int] val nestedSource : Source[Int, Promise[Option[Int]]] = source.viaMat(flow1)(Keep.left).named("nestedSource") // viaMat === via()(Keep.left) // val nestedSource2: Source[Int, NotUsed] = source.viaMat(flow1)(Keep.right) // Materializes to NotUsed val flow2: Flow[Int, ByteString, NotUsed] = Flow[Int].map(i => ByteString(i.toString)) // Materializes to Future[Tcp.OutgoingConnection (Keep.right) val flow3: Flow[ByteString, ByteString, Future[Tcp.OutgoingConnection]] = Tcp().outgoingConnection("localhost", 8080) val nestedFlow: Flow[Int, ByteString, Future[Tcp.OutgoingConnection]] = flow2.viaMat(flow3)(Keep.right) val nestedFlow2: Flow[Int, ByteString, NotUsed] = flow2.viaMat(flow3)(Keep.left) // flow2.via(flow3) val nestedFlow3: Flow[Int, ByteString, (NotUsed, Future[Tcp.OutgoingConnection])] = flow2.viaMat(flow3)(Keep.both) // Materializes to Future[String] (Keep.right) val sink: Sink[ByteString, Future[String]] = Sink.fold[String, ByteString]("")(_ + _.utf8String) val nestedSink: Sink[Int, (Future[Tcp.OutgoingConnection], Future[String])] = nestedFlow.toMat(sink)(Keep.both) def f(p: Promise[Option[Int]], rest: (Future[Tcp.OutgoingConnection], Future[String])): Future[MyClass] = { val connFuture = rest._1 connFuture.map(outConn => MyClass(p, outConn)) } // Materializes to Future[MyClass] val runnableGraph: RunnableGraph[Future[MyClass]] = nestedSource.toMat(nestedSink)(f) val r: RunnableGraph[Promise[Option[Int]]] = nestedSource.toMat(nestedSink)(Keep.left) val r2: RunnableGraph[(Future[Tcp.OutgoingConnection], Future[String])] = nestedSource.toMat(nestedSink)(Keep.right) }
Example 153
Source File: BrokerTransformerSpec.scala From incubator-toree with Apache License 2.0 | 5 votes |
package org.apache.toree.interpreter.broker import org.apache.toree.interpreter.{ExecuteError, Results} import org.scalatest.concurrent.Eventually import scala.concurrent.Promise import org.scalatest.{FunSpec, Matchers, OneInstancePerTest} class BrokerTransformerSpec extends FunSpec with Matchers with OneInstancePerTest with Eventually { private val brokerTransformer = new BrokerTransformer describe("BrokerTransformer") { describe("#transformToInterpreterResult") { it("should convert to success with result output if no failure") { val codeResultPromise = Promise[BrokerTypes.CodeResults]() val transformedFuture = brokerTransformer.transformToInterpreterResult( codeResultPromise.future ) val successOutput = "some success" codeResultPromise.success(successOutput) eventually { val result = transformedFuture.value.get.get result should be((Results.Success, Left(Map("text/plain" -> successOutput)))) } } it("should convert to error with broker exception if failure") { val codeResultPromise = Promise[BrokerTypes.CodeResults]() val transformedFuture = brokerTransformer.transformToInterpreterResult( codeResultPromise.future ) val failureException = new BrokerException("some failure") codeResultPromise.failure(failureException) eventually { val result = transformedFuture.value.get.get result should be((Results.Error, Right(ExecuteError( name = failureException.getClass.getName, value = failureException.getLocalizedMessage, stackTrace = failureException.getStackTrace.map(_.toString).toList )))) } } } } }
Example 154
Source File: InputRequestReplyHandler.scala From incubator-toree with Apache License 2.0 | 5 votes |
package org.apache.toree.kernel.protocol.v5.handler import akka.actor.ActorRef import org.apache.toree.comm.{CommRegistrar, CommStorage} import org.apache.toree.communication.utils.OrderedSupport import org.apache.toree.kernel.protocol.v5.{SystemActorType, KernelMessage} import org.apache.toree.kernel.protocol.v5.content.{InputReply, CommOpen} import org.apache.toree.kernel.protocol.v5.kernel.{Utilities, ActorLoader} import org.apache.toree.kernel.protocol.v5 import org.apache.toree.utils.MessageLogSupport import play.api.libs.json.Json import scala.concurrent.{Promise, Future} class InputRequestReplyHandler( actorLoader: ActorLoader, responseMap: collection.mutable.Map[String, ActorRef] ) extends OrderedSupport with MessageLogSupport { // TODO: Is there a better way than storing actor refs? def receive = { case kernelMessage: KernelMessage => startProcessing() val kernelMessageType = kernelMessage.header.msg_type val inputRequestType = v5.MessageType.Outgoing.InputRequest.toString val inputReplyType = v5.MessageType.Incoming.InputReply.toString // Is this an outgoing message to request data? if (kernelMessageType == inputRequestType) { val session = kernelMessage.parentHeader.session responseMap(session) = sender logger.debug("Associating input request with session " + session) actorLoader.load(SystemActorType.KernelMessageRelay) ! kernelMessage // Is this an incoming response to a previous request for data? } else if (kernelMessageType == inputReplyType) { val session = kernelMessage.header.session val inputReply = Json.parse(kernelMessage.contentString).as[InputReply] logger.debug(s"Received input reply for session $session with value " + s"'${inputReply.value}'") responseMap(session) ! inputReply.value responseMap.remove(session) } finishedProcessing() } override def orderedTypes() : Seq[Class[_]] = {Seq(classOf[KernelMessage])} }
Example 155
Source File: ZIODirectives.scala From full-scala-stack with Apache License 2.0 | 5 votes |
package api import akka.http.scaladsl.marshalling.{ Marshaller, Marshalling } import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.server.{ Directive, Directive1, Route, RouteResult } import akka.http.scaladsl.marshalling.ToResponseMarshaller import akka.http.scaladsl.server.directives.RouteDirectives import akka.http.scaladsl.util.FastFuture._ import zio.{ DefaultRuntime, Task, ZIO } import scala.concurrent.{ Future, Promise } import scala.util.{ Failure, Success } def zioCompleteOrRecoverWith(magnet: ZIOCompleteOrRecoverWithMagnet): Directive1[Throwable] = magnet.directive } object ZIODirectives extends ZIODirectives trait ZIOCompleteOrRecoverWithMagnet { def directive: Directive1[Throwable] } object ZIOCompleteOrRecoverWithMagnet extends ZIODirectives { implicit def apply[T]( task: => Task[T] )(implicit m: ToResponseMarshaller[T]): ZIOCompleteOrRecoverWithMagnet = new ZIOCompleteOrRecoverWithMagnet { override val directive: Directive1[Throwable] = Directive[Tuple1[Throwable]] { inner => ctx => val future = unsafeRunToFuture(task) import ctx.executionContext future.fast.transformWith { case Success(res) => ctx.complete(res) case Failure(error) => inner(Tuple1(error))(ctx) } } } }
Example 156
Source File: FutureAwaitWithFailFastFn.scala From kafka-connect-common with Apache License 2.0 | 5 votes |
package com.datamountaineer.streamreactor.connect.concurrent import java.util.concurrent.{ExecutorService, TimeUnit} import com.typesafe.scalalogging.StrictLogging import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration._ import scala.concurrent.{Await, Future, Promise} import scala.util.Failure object FutureAwaitWithFailFastFn extends StrictLogging { def apply(executorService: ExecutorService, futures: Seq[Future[Unit]], duration: Duration): Unit = { //make sure we ask the executor to shutdown to ensure the process exits executorService.shutdown() val promise = Promise[Boolean]() //stop on the first failure futures.foreach { f => f.failed.foreach { case t => if (promise.tryFailure(t)) { executorService.shutdownNow() } } } val fut = Future.sequence(futures) fut.foreach { case t => if (promise.trySuccess(true)) { val failed = executorService.shutdownNow() if (failed.size() > 0) { logger.error(s"${failed.size()} task have failed.") } } } Await.ready(promise.future, duration).value match { case Some(Failure(t)) => executorService.awaitTermination(1, TimeUnit.MINUTES) //throw the underlying error throw t case _ => executorService.awaitTermination(1, TimeUnit.MINUTES) } } def apply[T](executorService: ExecutorService, futures: Seq[Future[T]], duration: Duration = 1.hours): Seq[T] = { //make sure we ask the executor to shutdown to ensure the process exits executorService.shutdown() val promise = Promise[Boolean]() //stop on the first failure futures.foreach { f => f.failed.foreach { case t => if (promise.tryFailure(t)) { executorService.shutdownNow() } } } val fut = Future.sequence(futures) fut.foreach { case t => if (promise.trySuccess(true)) { val failed = executorService.shutdownNow() if (failed.size() > 0) { logger.error(s"${failed.size()} task have failed.") } } } Await.ready(promise.future, duration).value match { case Some(Failure(t)) => executorService.awaitTermination(1, TimeUnit.MINUTES) //throw the underlying error throw t case _ => executorService.awaitTermination(1, TimeUnit.MINUTES) //return the result from each of the futures Await.result(Future.sequence(futures), 1.minute) } } }
Example 157
Source File: ExecutorExtension.scala From kafka-connect-common with Apache License 2.0 | 5 votes |
package com.datamountaineer.streamreactor.connect.concurrent import java.util.concurrent.Executor import scala.concurrent.{Future, Promise} object ExecutorExtension { implicit class RunnableWrapper(val executor: Executor) extends AnyVal { def submit[T](thunk: => T): Future[T] = { val promise = Promise[T]() executor.execute(new Runnable { override def run(): Unit = { try { val t = thunk promise.success(t) } catch { case t: Throwable => promise.failure(t) } } }) promise.future } } }
Example 158
Source File: effects.scala From izanami with Apache License 2.0 | 5 votes |
package libs import java.util.concurrent.CompletionStage import scala.concurrent.{Future, Promise} object effects { implicit class CSOps[T](cs: CompletionStage[T]) { def toFuture: Future[T] = { val p = Promise[T] cs.whenComplete((ok, e) => { if (e != null) { p.failure(e) } else { p.success(ok) } }) p.future } } }
Example 159
Source File: HttpManagementServer.scala From akka-cluster-manager with MIT License | 5 votes |
package io.orkestra.cluster.management import java.util.concurrent.atomic.AtomicReference import akka.Done import akka.actor.{ActorSystem, ActorRef} import akka.cluster.Cluster import akka.http.scaladsl.Http import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Directives._ import akka.stream.{ActorMaterializer, Materializer} import io.orkestra.cluster.protocol.Response.{Failure, Success} import io.orkestra.cluster.routing.ClusterListener._ import akka.pattern.ask import play.api.libs.json.Json import scala.concurrent.{Promise, Future, ExecutionContext} import scala.concurrent.duration._ class HttpManagementServer(clusterListener: ActorRef, hostName: String = "127.0.0.1", port: Int = 33333)( implicit val system: ActorSystem, implicit val materializer: Materializer, implicit val executer: ExecutionContext ) { import PlayJsonSupport._ def handleOrkestraRequest(req: ManagementReguest) = (clusterListener ? req)(3.seconds).map { case res: Success => res.httpStatusCode -> res.asJson case res: Failure => res.httpStatusCode -> res.asJson } def orkestraRoutes = pathPrefix("orkestra" / "routers") { pathEndOrSingleSlash { get { complete(handleOrkestraRequest(GetRouters)) } } ~ path(Segment ~ Slash.?) { role => get { complete(handleOrkestraRequest(GetRouter(role))) } } ~ path(Segment / Remaining ~ Slash.?) { (role, routeePath) => delete { complete(handleOrkestraRequest(DeleteRoutee(role, routeePath))) } } } private val bindingFuture = new AtomicReference[Future[Http.ServerBinding]]() def start() = { val serverBindingPromise = Promise[Http.ServerBinding]() if (bindingFuture.compareAndSet(null, serverBindingPromise.future)) { Http().bindAndHandle(orkestraRoutes, hostName, port) println(Console.CYAN + s"cluster http management server online at http://${hostName}:${port}/" + Console.WHITE) } } def shutdown = if (bindingFuture.get() == null) { Future(Done) } else { val stopFuture = bindingFuture.get().flatMap(_.unbind()).map(_ => Done) bindingFuture.set(null) stopFuture } }
Example 160
Source File: TerminationListenerActor.scala From seahorse-workflow-executor with Apache License 2.0 | 5 votes |
package io.deepsense.workflowexecutor.executor import scala.concurrent.Promise import akka.actor.{Actor, Props} import io.deepsense.sparkutils.AkkaUtils import io.deepsense.models.workflows.ExecutionReport class TerminationListenerActor(finishedExecutionStatus: Promise[ExecutionReport]) extends Actor { override def receive: Receive = { case status: ExecutionReport => finishedExecutionStatus.success(status) AkkaUtils.terminate(context.system) } } object TerminationListenerActor { def props(finishedExecutionReport: Promise[ExecutionReport]): Props = Props(new TerminationListenerActor(finishedExecutionReport)) }
Example 161
Source File: CustomCodeEntryPoint.scala From seahorse-workflow-executor with Apache License 2.0 | 5 votes |
package io.deepsense.workflowexecutor.customcode import java.util.concurrent.TimeoutException import java.util.concurrent.atomic.AtomicReference import scala.annotation.tailrec import scala.concurrent.duration._ import scala.concurrent.{Await, Promise} import org.apache.spark.api.java.JavaSparkContext import org.apache.spark.sql.DataFrame import org.apache.spark.{SparkConf, SparkContext} import io.deepsense.commons.utils.Logging import io.deepsense.deeplang._ import io.deepsense.sparkutils.SparkSQLSession class CustomCodeEntryPoint( val sparkContext: SparkContext, val sparkSQLSession: SparkSQLSession, val dataFrameStorage: DataFrameStorage, val operationExecutionDispatcher: OperationExecutionDispatcher) extends Logging { import io.deepsense.workflowexecutor.customcode.CustomCodeEntryPoint._ def getSparkContext: JavaSparkContext = sparkContext def getSparkSQLSession: SparkSQLSession = sparkSQLSession def getNewSparkSQLSession: SparkSQLSession = sparkSQLSession.newSession() def getSparkConf: SparkConf = sparkContext.getConf private val codeExecutor: AtomicReference[Promise[CustomCodeExecutor]] = new AtomicReference(Promise()) private val pythonPort: AtomicReference[Promise[Int]] = new AtomicReference(Promise()) def getCodeExecutor(timeout: Duration): CustomCodeExecutor = getFromPromise(codeExecutor.get, timeout) def getPythonPort(timeout: Duration): Int = getFromPromise(pythonPort.get, timeout) def registerCodeExecutor(newCodeExecutor: CustomCodeExecutor): Unit = replacePromise(codeExecutor, newCodeExecutor) def registerCallbackServerPort(newPort: Int): Unit = replacePromise(pythonPort, newPort) def retrieveInputDataFrame(workflowId: String, nodeId: String, portNumber: Int): DataFrame = dataFrameStorage.getInputDataFrame(workflowId, nodeId, portNumber).get def retrieveOutputDataFrame(workflowId: String, nodeId: String, portNumber: Int): DataFrame = dataFrameStorage.getOutputDataFrame(workflowId, nodeId, portNumber).get def registerOutputDataFrame( workflowId: String, nodeId: String, portNumber: Int, dataFrame: DataFrame): Unit = dataFrameStorage.setOutputDataFrame(workflowId, nodeId, portNumber, dataFrame) def executionCompleted(workflowId: String, nodeId: String): Unit = operationExecutionDispatcher.executionEnded(workflowId, nodeId, Right(())) def executionFailed(workflowId: String, nodeId: String, error: String): Unit = operationExecutionDispatcher.executionEnded(workflowId, nodeId, Left(error)) } object CustomCodeEntryPoint { private case class PromiseReplacedException() extends Exception @tailrec private def getFromPromise[T](promise: => Promise[T], timeout: Duration): T = { try { Await.result(promise.future, timeout) } catch { case e: TimeoutException => throw e case e: PromiseReplacedException => getFromPromise(promise, timeout) } } private def replacePromise[T](promise: AtomicReference[Promise[T]], newValue: T): Unit = { val oldPromise = promise.getAndSet(Promise.successful(newValue)) try { oldPromise.failure(new PromiseReplacedException) } catch { // The oldPromise will have been completed always, except for the first time. // The illegal state is expected, but we have to complete the oldPromise, // since someone might be waiting on it. case e: IllegalStateException => () } } case class CustomCodeEntryPointConfig( pyExecutorSetupTimeout: Duration = 5.seconds) }
Example 162
Source File: OperationExecutionDispatcher.scala From seahorse-workflow-executor with Apache License 2.0 | 5 votes |
package io.deepsense.deeplang import scala.collection.concurrent.TrieMap import scala.concurrent.{Future, Promise} import io.deepsense.commons.models.Id class OperationExecutionDispatcher { import OperationExecutionDispatcher._ private val operationEndPromises: TrieMap[OperationId, Promise[Result]] = TrieMap.empty def executionStarted(workflowId: Id, nodeId: Id): Future[Result] = { val promise: Promise[Result] = Promise() require(operationEndPromises.put((workflowId, nodeId), promise).isEmpty) promise.future } def executionEnded(workflowId: Id, nodeId: Id, result: Result): Unit = { val promise = operationEndPromises.remove((workflowId, nodeId)) require(promise.isDefined) promise.get.success(result) } } object OperationExecutionDispatcher { type OperationId = (Id, Id) type Error = String type Result = Either[Error, Unit] }
Example 163
Source File: package.scala From reactive-cli with Apache License 2.0 | 5 votes |
package com.lightbend.rp.reactivecli import scala.concurrent.{ ExecutionContext, Future, Promise } import scala.util.{ Failure, Success, Try } package object concurrent { implicit val executionContext: ExecutionContext = Platform.executionContext def attempt[T](f: Future[T]): Future[Try[T]] = f .map(Success.apply) .recover { case t: Throwable => Failure(t) } def optionToFuture[T](option: Option[T], failMsg: String): Future[T] = option.fold(Future.failed[T](new NoSuchElementException(failMsg)))(Future.successful) def wrapFutureOption[T](f: Future[T]): Future[Option[T]] = { val p = Promise[Option[T]] f.onComplete { case Failure(f) => p.success(None) case Success(s) => p.success(Some(s)) } p.future } }
Example 164
Source File: SimpleHttpResponse.scala From RosHTTP with MIT License | 5 votes |
package fr.hmil.roshttp.response import java.nio.ByteBuffer import fr.hmil.roshttp.BackendConfig import fr.hmil.roshttp.exceptions.ResponseException import fr.hmil.roshttp.util.{HeaderMap, Utils} import monix.execution.Scheduler import monix.reactive.Observable import scala.collection.mutable import scala.concurrent.{Future, Promise} import scala.util.{Failure, Success} class SimpleHttpResponse( val statusCode: Int, val headers: HeaderMap[String], val body: String) extends HttpResponse object SimpleHttpResponse extends HttpResponseFactory[SimpleHttpResponse] { override def apply( header: HttpResponseHeader, bodyStream: Observable[ByteBuffer], config: BackendConfig) (implicit scheduler: Scheduler): Future[SimpleHttpResponse] = { val charset = Utils.charsetFromContentType(header.headers.getOrElse("content-type", null)) val buffers = mutable.Queue[ByteBuffer]() val promise = Promise[SimpleHttpResponse]() val streamCollector = bodyStream. foreach(elem => buffers.enqueue(elem)). map({_ => val body = recomposeBody(buffers, config.maxChunkSize, charset) new SimpleHttpResponse(header.statusCode, header.headers, body) }) streamCollector.onComplete({ case res:Success[SimpleHttpResponse] => promise.trySuccess(res.value) case e:Failure[_] => promise.tryFailure(new ResponseException(e.exception, header)) }) promise.future } private def recomposeBody(seq: mutable.Queue[ByteBuffer], maxChunkSize: Int, charset: String): String = { // Allocate maximum expected body length val buffer = ByteBuffer.allocate(seq.length * maxChunkSize) val totalBytes = seq.foldLeft(0)({ (count, chunk) => buffer.put(chunk) count + chunk.limit() }) buffer.limit(totalBytes) Utils.getStringFromBuffer(buffer, charset) } }
Example 165
Source File: FutureTrySpec.scala From scala-common with Apache License 2.0 | 5 votes |
import com.softwaremill.futuretry._ import org.scalatest.concurrent.ScalaFutures import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.prop.TableDrivenPropertyChecks import org.scalatest.matchers.must.Matchers import scala.concurrent.duration.Duration import scala.concurrent.{Future, Await, Promise} import scala.util.{Failure, Success, Try} class FutureTrySpec extends AnyFlatSpec with Matchers with TableDrivenPropertyChecks with ScalaFutures { import scala.concurrent.ExecutionContext.Implicits.global "tried" must "convert a successful result into a Success" in { val p = Promise[String] p.complete(Try("a")) val transformedFuture = p.future.tried transformedFuture.futureValue must be(Success("a")) } it must "convert an exceptional result into a Failure" in { val p = Promise[String] val exception = new RuntimeException("blah") p.complete(Try(throw exception)) val transformedFuture = p.future.tried transformedFuture.futureValue must be(Failure(exception)) } "transform" must "correctly transform between all Try variants in" in { val exception = new RuntimeException("bloh") val scenarios = Table[Try[String], Try[String] => Try[String], Try[String]] ( ("original value", "transform", "expected output"), (Success("a"), identity[Try[String]], Success("a")), (Failure(exception), (x: Try[String]) => x match { case Failure(e) => Success(e.toString); case _ => ??? }, Success(exception.toString)), (Success("a"), (x: Try[String]) => x match { case Success(_) => Failure(exception); case _ => ??? }, Failure(exception)), (Failure(exception), identity[Try[String]], Failure(exception)) ) forAll(scenarios) { (orgValue, f, output) => { val p = Promise[String] p.complete(orgValue) val transformedFuture = p.future.transformTry(f) transformedFuture.tried.futureValue must be(output) } } } }
Example 166
Source File: ElasticWriter.scala From elastic-indexer4s with MIT License | 5 votes |
package com.yannick_cw.elastic_indexer4s.elasticsearch import akka.actor.ActorSystem import akka.stream.scaladsl.Sink import com.sksamuel.elastic4s.http.ElasticDsl._ import com.sksamuel.elastic4s.http.bulk.BulkResponseItem import com.sksamuel.elastic4s.streams.ReactiveElastic._ import com.sksamuel.elastic4s.streams.{BulkIndexingSubscriber, RequestBuilder, ResponseListener} import com.yannick_cw.elastic_indexer4s.Index_results.{IndexError, StageSucceeded, StageSuccess} import com.yannick_cw.elastic_indexer4s.elasticsearch.elasic_config.ElasticWriteConfig import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.Try import scala.util.control.NonFatal class ElasticWriter(esConf: ElasticWriteConfig)(implicit system: ActorSystem, ex: ExecutionContext) { import esConf._ //promise that is passed to the error and completion function of the elastic subscriber private val elasticFinishPromise: Promise[Unit] = Promise[Unit]() private def esSubscriber[A: RequestBuilder]: BulkIndexingSubscriber[A] = client.subscriber[A]( batchSize = writeBatchSize, completionFn = { () => Try(elasticFinishPromise.success(())); () }, errorFn = { t: Throwable => Try(elasticFinishPromise.failure(t)); () }, listener = new ResponseListener[A] { override def onAck(resp: BulkResponseItem, original: A): Unit = () override def onFailure(resp: BulkResponseItem, original: A): Unit = //todo not yet sure if this could break too early Try(elasticFinishPromise.failure(new Exception("Failed indexing with: " + resp.error))) }, concurrentRequests = writeConcurrentRequest, maxAttempts = writeMaxAttempts ) def esSink[A: RequestBuilder]: Sink[A, Future[Unit]] = Sink .fromSubscriber(esSubscriber[A]) .mapMaterializedValue(_ => elasticFinishPromise.future) private def tryIndexCreation: Try[Future[Either[IndexError, StageSucceeded]]] = Try( client .execute( mappingSetting.fold( typed => createIndex(indexName) .mappings(typed.mappings) .analysis(typed.analyzer) .shards(typed.shards) .replicas(typed.replicas), unsafe => createIndex(indexName) .source(unsafe.source.spaces2) ) ) .map(response => response.fold[Either[IndexError, StageSucceeded]]( Left(IndexError(s"Index creation failed with error: ${response.error}")))(_ => Right(StageSuccess(s"Index $indexName was created"))))) def createNewIndex: Future[Either[IndexError, StageSucceeded]] = Future .fromTry(tryIndexCreation) .flatten .recover { case NonFatal(t) => Left(IndexError("Index creation failed.", Some(t))) } } object ElasticWriter { def apply(esConf: ElasticWriteConfig)(implicit system: ActorSystem, ex: ExecutionContext): ElasticWriter = new ElasticWriter(esConf) }
Example 167
Source File: package.scala From libisabelle with Apache License 2.0 | 5 votes |
package info.hupel.isabelle import scala.concurrent.{Future, Promise} import scalaz.concurrent.Task package object setup { implicit class TaskOps[T](task: Task[T]) { def toScalaFuture: Future[T] = { val promise = Promise[T] task.unsafePerformAsync { res => res.fold(promise.failure, promise.success) () } promise.future } } }
Example 168
Source File: FutureAndPromise.scala From scala-tutorials with MIT License | 5 votes |
package com.baeldung.scala.concurrency import java.math.BigInteger import java.net.URL import java.security.MessageDigest import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.Duration import scala.concurrent.{Await, ExecutionContext, Future, Promise} import scala.util.control.NonFatal import scala.util.{Failure, Success} object ScalaAndPromise { def sampleFuture(): Future[Int] = Future { println("Long running computation started.") val result = { Thread.sleep(5) 5 } println("Our computation, finally finished.") result } type Name = String type Email = String type Password = String type Avatar = URL case class User(name: Name, email: Email, password: Password, avatar: Avatar) def exist(email: Email): Future[Boolean] = Future { Thread.sleep(100) // Call to the database takes time true } def md5hash(str: String): String = new BigInteger(1, MessageDigest .getInstance("MD5") .digest(str.getBytes) ).toString(16) def avatar(email: Email): Future[Avatar] = Future { Thread.sleep(200) // Call to the database takes time new Avatar("http://avatar.example.com/user/23k520f23f4.png") } def createUser(name: Name, email: Email, password: Password): Future[User] = for { _ <- exist(email) avatar <- avatar(email) hashedPassword = md5hash(password) } yield User(name, email, hashedPassword, avatar) def runByPromise[T](block: => T)(implicit ec: ExecutionContext): Future[T] = { val p = Promise[T] ec.execute { () => try { p.success(block) } catch { case NonFatal(e) => p.failure(e) } } p.future } } object FutureAndPromiseApp extends App { import ScalaAndPromise._ // Access to the value of Future by passing callback to the onComplete val userFuture: Future[User] = createUser("John", "[email protected]", "secret") userFuture.onComplete { case Success(user) => println(s"User created: $user") case Failure(exception) => println(s"Creating user failed due to the exception: $exception") } // Access to the value of Future by applying the result function on the Future value val user: User = Await.result(userFuture, Duration.Inf) // Forcing the Future value to be complete val completedFuture: Future[User] = Await.ready(userFuture, Duration.Inf) completedFuture.value.get match { case Success(user) => println(s"User created: $user") case Failure(exception) => println(s"Creating user failed due to the exception: $exception") } }
Example 169
Source File: Futures.scala From courscala with Apache License 2.0 | 5 votes |
package org.coursera.common.concurrent import scala.concurrent.ExecutionContext import scala.concurrent.Future import scala.concurrent.Promise import scala.util.Failure import scala.util.Success import scala.util.Try import scala.util.control.NonFatal object Futures extends FutureExtractors { def findMatch[T, U]( futures: TraversableOnce[Future[T]]) (pf: PartialFunction[T, U]) (implicit ec: ExecutionContext): Future[Option[U]] = { Future.find(futures)(pf.isDefinedAt).map(_.map(pf)) } def option[T](option: Option[Future[T]])(implicit ec: ExecutionContext): Future[Option[T]] = option.map(_.map(Some(_))).getOrElse(Future.successful(None)) def map[K, V](m: Map[K, Future[V]])(implicit ec: ExecutionContext): Future[Map[K, V]] = { val elementFutures = m.map { case (key, valueFuture) => valueFuture.map(key -> _) } Future.sequence(elementFutures).map(_.toMap) } object Implicits { implicit class FutureOps[T](future: Future[T]) { def toTry(implicit ec: ExecutionContext): Future[Try[T]] = { future .map(Success.apply) .recover(PartialFunction(Failure.apply)) } } } }
Example 170
Source File: ExecutorServiceBase.scala From sparkplug with MIT License | 5 votes |
package springnz.sparkplug import akka.actor._ import akka.testkit.TestKitBase import springnz.sparkplug.core.{ Configurer, LocalConfigurer } import springnz.sparkplug.executor.ExecutorService import springnz.sparkplug.executor.MessageTypes._ import scala.concurrent.Promise trait ExecutorServiceBase { self: TestKitBase ⇒ class ExecutorServiceFixture(probe: ActorRef, clientName: String, brokerName: String) { val readyPromise = Promise[Unit] val executorService = new ExecutorService("TestService", brokerName) { // Run it locally in Spark override val configurer: Configurer = new LocalConfigurer("TestService", None) } val clientActor = system.actorOf(Props(new Actor { override def receive = { case ServerReady ⇒ probe forward ServerReady sender ! ClientReady readyPromise.success(()) } }), clientName) executorService.start(system, s"/user/$clientName") } }
Example 171
Source File: Usgs.scala From reactive-programming with Apache License 2.0 | 5 votes |
package com.test.week4 import rx.lang.scala.Observable import scala.concurrent.{ Promise, Future } import scala.util.Random object Usgs { lazy val rnd = new Random() case class EarthQuake(magnitude: Double, location: (Double, Double)) case class Country(name: String) def stream: Observable[EarthQuake] = Observable(observer ⇒ { try { while (!observer.isUnsubscribed) { observer.onNext(generateEarthQuake) } observer.onCompleted() } catch { case ex: Throwable ⇒ observer.onError(ex) } }) }
Example 172
Source File: HttpUtils.scala From reactive-programming with Apache License 2.0 | 5 votes |
package com.github.dnvriend import java.util.concurrent.Executor import com.ning.http.client.AsyncHttpClient import org.jsoup.Jsoup import org.jsoup.nodes.Document import org.jsoup.select.Elements import scala.collection.JavaConverters._ import scala.concurrent.{ Promise, ExecutionContext, Future } import scala.util.Try object HttpClient { def apply(): AsyncHttpClient = new AsyncHttpClient implicit class HttpClientToScala(client: AsyncHttpClient) { def get(url: String)(implicit ec: Executor): Future[String] = { val f = client.prepareGet(url).execute val p = Promise[String]() f.addListener(new Runnable { override def run(): Unit = { val response = f.get if (response.getStatusCode < 400) p.success(response.getResponseBodyExcerpt(131072)) else p.failure(new RuntimeException(s"BadStatus: ${response.getStatusCode}")) } }, ec) p.future } } } object HttpUtils { implicit class FindLinksFuture(self: Future[String])(implicit ec: ExecutionContext) { def links: Future[Option[Iterator[String]]] = self.map(body ⇒ findLinks(body)) } def findLinks(body: String): Option[Iterator[String]] = Try(Jsoup.parse(body)).map { (document: Document) ⇒ val links: Elements = document.select("a[href]") for (link ← links.iterator().asScala; if link.absUrl("href").startsWith("http://")) yield link.absUrl("href") }.toOption }
Example 173
Source File: UpdateApi.scala From iep-apps with Apache License 2.0 | 5 votes |
package com.netflix.iep.lwc import akka.actor.ActorRefFactory import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.RouteResult import com.netflix.atlas.akka.CustomDirectives._ import com.netflix.atlas.akka.WebApi import com.netflix.atlas.webapi.PublishApi import scala.concurrent.Promise class UpdateApi(implicit val actorRefFactory: ActorRefFactory) extends WebApi { private val publishRef = actorRefFactory.actorSelection("/user/publish") override def routes: Route = { endpointPathPrefix("database" / "v1" / "update") { post { extractRequestContext { ctx => parseEntity(customJson(p => PublishApi.decodeList(p))) { datapoints => val promise = Promise[RouteResult]() publishRef ! PublishApi.PublishRequest(datapoints, Nil, promise, ctx) _ => promise.future } } } } } }
Example 174
Source File: actor.scala From lacasa with BSD 3-Clause "New" or "Revised" License | 5 votes |
package lacasa.test import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 import scala.concurrent.ExecutionContext import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.{Future, Promise, Await} import scala.concurrent.duration._ import scala.spores._ import scala.spores.SporeConv._ import lacasa.{System, Box, CanAccess, Actor, ActorRef} import Box._ class NonSneaky { def process(a: Array[Int]): Unit = { for (i <- 0 until a.length) a(i) = a(i) + 1 } } class ActorA(next: ActorRef[C]) extends Actor[C] { def receive(msg: Box[C])(implicit access: CanAccess { type C = msg.C }): Unit = { msg.open(spore { (obj: C) => // OK: update array obj.arr(0) = 100 // OK: create instance of ocap class val ns = new NonSneaky ns.process(obj.arr) }) next.send(msg)(spore { () => }) } } class ActorB(p: Promise[String]) extends Actor[C] { def receive(msg: Box[C])(implicit access: CanAccess { type C = msg.C }): Unit = { msg.open(spore { x => p.success(x.arr.mkString(",")) }) } } class C { var arr: Array[Int] = _ } @RunWith(classOf[JUnit4]) class Spec { @Test def test(): Unit = { // to check result val p: Promise[String] = Promise() val sys = System() val b = sys.actor[C](new ActorB(p)) val a = sys.actor[C](new ActorA(b)) try { mkBox[C] { packed => import packed.access val box: packed.box.type = packed.box // initialize object in box with new array box.open(spore { obj => obj.arr = Array(1, 2, 3, 4) }) a.send(box)(spore { () => }) } } catch { case t: Throwable => val res = Await.result(p.future, 2.seconds) assert(res == "101,3,4,5") } } }
Example 175
package lacasa.test.uniqueness import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 import scala.concurrent.ExecutionContext import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.{Future, Promise, Await} import scala.concurrent.duration._ import scala.spores._ import scala.spores.SporeConv._ import lacasa.{System, Box, CanAccess, Actor, ActorRef} import Box._ class C { var f: D = null //var count = 0 } class D { var g: C = null } sealed abstract class Msg final case class Start() extends Msg //final case class Repeat(obj: C) extends Msg class ActorA(next: ActorRef[C]) extends Actor[Msg] { def receive(msg: Box[Msg])(implicit access: CanAccess { type C = msg.C }): Unit = { // create box with externally-unique object mkBox[C] { packed => implicit val acc = packed.access val box: packed.box.type = packed.box // initialize object in box box.open(spore { obj => val d = new D d.g = obj obj.f = d }) next.send(box)(spore { () => }) } } } class ActorB(p: Promise[Boolean]) extends Actor[C] { def receive(msg: Box[C])(implicit access: CanAccess { type C = msg.C }): Unit = { msg.open(spore { x => val d = x.f // check that `d` refers back to `x` p.success(d.g == x) }) } } @RunWith(classOf[JUnit4]) class Spec { @Test def test(): Unit = { // to check result val p: Promise[Boolean] = Promise() val sys = System() val b = sys.actor[C](new ActorB(p)) val a = sys.actor[Msg](new ActorA(b)) try { mkBox[Start] { packed => import packed.access val box: packed.box.type = packed.box a.send(box)(spore { () => }) } } catch { case t: Throwable => val res = Await.result(p.future, 2.seconds) assert(res) } } }
Example 176
Source File: example1.scala From lacasa with BSD 3-Clause "New" or "Revised" License | 5 votes |
package lacasa.test.examples import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 import scala.concurrent.ExecutionContext import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.{Future, Promise, Await} import scala.concurrent.duration._ import scala.spores._ import lacasa.{System, Box, CanAccess, Actor, ActorRef, doNothing} import Box._ class Message1 { var arr: Array[Int] = _ } class Start { var next: ActorRef[Message1] = _ } class ActorA extends Actor[Any] { override def receive(b: Box[Any]) (implicit acc: CanAccess { type C = b.C }) { b.open(spore { x => x match { case s: Start => mkBox[Message1] { packed => implicit val access = packed.access packed.box open { msg => msg.arr = Array(1, 2, 3, 4) } s.next.send(packed.box) { doNothing.consume(packed.box) } } case other => // .. } }) } } class ActorB(p: Promise[String]) extends Actor[Message1] { override def receive(box: Box[Message1]) (implicit acc: CanAccess { type C = box.C }) { // Strings are Safe, and can therefore be extracted from the box. p.success(box.extract(_.arr.mkString(","))) } } @RunWith(classOf[JUnit4]) class Spec { @Test def test(): Unit = { // to check result val p: Promise[String] = Promise() val sys = System() val a = sys.actor[ActorA, Any] val b = sys.actor[Message1](new ActorB(p)) try { mkBox[Start] { packed => import packed.access val box: packed.box.type = packed.box box open { s => s.next = capture(b) // !!! captures `b` within `open` } a.send(box) { doNothing.consume(packed.box) } } } catch { case t: Throwable => val res = Await.result(p.future, 2.seconds) assert(res == "1,2,3,4") } } }
Example 177
Source File: MultiFuture.scala From sbt-dependency-updates with Apache License 2.0 | 5 votes |
package org.jmotor.sbt.concurrent import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.atomic.AtomicInteger import org.jmotor.sbt.exception.MultiException import scala.concurrent.Promise class MultiFuture[T](p: Promise[T], count: Int, default: T) { private[this] val counter = new AtomicInteger(0) private[this] val errors = new CopyOnWriteArrayList[Throwable]() def tryComplete(): Unit = { if (counter.incrementAndGet() == count) { if (errors.isEmpty) { p success default } else { import scala.collection.JavaConverters._ p failure MultiException(errors.asScala: _*) } } } def tryComplete(throwable: Throwable): Unit = { errors.add(throwable) tryComplete() } }
Example 178
Source File: RegionSpec.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.core import org.scalatest.{FreeSpec, Matchers} import swave.core.graph.GlyphSet import swave.core.macros._ import scala.concurrent.Promise import scala.io.Source import scala.util.control.NonFatal // format: OFF class RegionSpec extends FreeSpec with Matchers { implicit val env = StreamEnv() "Example 1" tests { Spout.ints(0) .map(_.toString) .to(Drain.head) } "Example 2" tests { Spout.ints(0) .take(10) .asyncBoundary() .map(_.toString) .to(Drain.head) } "Example 3" tests { Spout(1, 2, 3) .fanOutBroadcast() .sub.map(_.toString).end .sub.asyncBoundary().end .fanInConcat() .to(Drain.head) } "Example 4" tests { def upperChars(s: String): Spout[Char] = Spout(s.iterator).map(_.toUpper) def drain(promise: Promise[String]): Drain[Char, Unit] = Drain.mkString(limit = 100) .captureResult(promise) .async("bar") val result2 = Promise[String]() upperChars("Hello") .asyncBoundary("foo") .fanOutBroadcast() .sub.drop(2).concat(upperChars("-Friend-").asyncBoundary()).end .sub.take(2).asyncBoundary().multiply(2).end .fanInConcat() .tee(Pipe[Char].asyncBoundary().deduplicate.to(drain(result2))) .map(_.toLower) .to(Drain.mkString(100)) } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// val examples: Map[String, String] = Source.fromInputStream(getClass.getResourceAsStream("/RegionSpec.examples.txt")) .getLines() .scanLeft(Left(Nil): Either[List[String], List[String]]) { case (Left(lines), "") ⇒ Right(lines.reverse) case (Left(lines), line) ⇒ Left(line :: lines) case (Right(_), line) ⇒ Left(line :: Nil) } .collect { case Right(renderString) ⇒ renderString } .toList .groupBy(_.head) .map { case (name, listOfRenderLines) ⇒ requireArg(listOfRenderLines.size == 1, ", which means you have an example name duplication in examples.txt") name → listOfRenderLines.head.tail.mkString("\n") } implicit class Example(name: String) { def tests(pipeNet: ⇒ StreamGraph[_]): Unit = name in { val expectedRendering = examples.getOrElse(name + ':', sys.error(s"Section for '$name' not found in examples.txt")) val superRegions = Graph.superRegions(pipeNet.seal().regions) val rendering = superRegions.map { sr => Graph.from(sr.head.entryPoint, Graph.ExpandModules.All) .withGlyphSet(GlyphSet.`2x2 ASCII`) .withStageFormat((s, _) => s"${s.kind.name}: ${sr.indexOf(s.region)}") .render() }.mkString("\n***\n") try rendering shouldEqual expectedRendering catch { case NonFatal(e) ⇒ println(rendering) throw e } } } }
Example 179
Source File: HttpTestExtensions.scala From sttp with Apache License 2.0 | 5 votes |
package sttp.client.testing import org.scalajs.dom.FileReader import org.scalajs.dom.raw.{Event, UIEvent} import sttp.client._ import sttp.client.dom.experimental.{FilePropertyBag, File => DomFile} import sttp.client.internal.SparkMD5 import scala.concurrent.{Future, Promise} import scala.language.higherKinds import scala.scalajs.js import scala.scalajs.js.JSConverters._ import scala.scalajs.js.JavaScriptException import scala.scalajs.js.typedarray.AB2TA import HttpTest.endpoint trait HttpTestExtensions[F[_]] extends AsyncExecutionContext { self: HttpTest[F] => private def withTemporaryFile[T](content: Option[Array[Byte]])(f: DomFile => Future[T]): Future[T] = { val data = content.getOrElse(Array.empty) val file = new DomFile( Array(data.toTypedArray.asInstanceOf[js.Any]).toJSArray, "temp.txt", FilePropertyBag( `type` = "text/plain" ) ) f(file) } private def withTemporaryNonExistentFile[T](f: DomFile => Future[T]): Future[T] = withTemporaryFile(None)(f) private def md5FileHash(file: DomFile): Future[String] = { val p = Promise[String]() val fileReader = new FileReader() fileReader.onload = (_: UIEvent) => { val arrayBuffer = fileReader.result.asInstanceOf[scala.scalajs.js.typedarray.ArrayBuffer] val hash = SparkMD5.ArrayBuffer.hash(arrayBuffer) p.success(hash) } fileReader.onerror = (_: Event) => p.failure(JavaScriptException("Error reading file")) fileReader.onabort = (_: Event) => p.failure(JavaScriptException("File read aborted")) fileReader.readAsArrayBuffer(file) p.future } "body" - { "post a file" in { withTemporaryFile(Some(testBodyBytes)) { f => postEcho.body(f).send().toFuture().map { response => response.body should be(Right(expectedPostEchoResponse)) } } } } "download file" - { "download a binary file using asFile" in { withTemporaryNonExistentFile { file => val req = basicRequest.get(uri"$endpoint/download/binary").response(asFile(file)) req.send().toFuture().flatMap { resp => md5FileHash(resp.body.right.get).map { _ shouldBe binaryFileMD5Hash } } } } "download a text file using asFile" in { withTemporaryNonExistentFile { file => val req = basicRequest.get(uri"$endpoint/download/text").response(asFile(file)) req.send().toFuture().flatMap { resp => md5FileHash(resp.body.right.get).map { _ shouldBe textFileMD5Hash } } } } } "multipart" - { def mp = basicRequest.post(uri"$endpoint/multipart") "send a multipart message with a file" in { withTemporaryFile(Some(testBodyBytes)) { f => val req = mp.multipartBody(multipartFile("p1", f), multipart("p2", "v2")) req.send().toFuture().map { resp => resp.body should be(Right(s"p1=$testBody (${f.name}), p2=v2$defaultFileName")) } } } } }
Example 180
Source File: package.scala From sttp with Apache License 2.0 | 5 votes |
package sttp.client.impl import _root_.zio._ import _root_.zio.blocking.Blocking import sttp.client.testing.ConvertToFuture import scala.concurrent.{Future, Promise} import scala.util.{Failure, Success} package object zio { val runtime: Runtime[ZEnv] = Runtime.default type BlockingTask[A] = ZIO[Blocking, Throwable, A] val convertZioTaskToFuture: ConvertToFuture[Task] = new ConvertToFuture[Task] { override def toFuture[T](value: Task[T]): Future[T] = { val p = Promise[T]() runtime.unsafeRunSync(value) match { case Exit.Failure(c) => p.complete( Failure( c.failures.headOption.orElse(c.defects.headOption).getOrElse(new RuntimeException(s"Unknown cause: $c")) ) ) case Exit.Success(v) => p.complete(Success(v)) } p.future } } val convertZioBlockingTaskToFuture: ConvertToFuture[BlockingTask] = new ConvertToFuture[BlockingTask] { override def toFuture[T](value: BlockingTask[T]): Future[T] = { val p = Promise[T]() runtime.unsafeRunSync(value) match { case Exit.Failure(c) => p.complete( Failure( c.failures.headOption.orElse(c.defects.headOption).getOrElse(new RuntimeException(s"Unknown cause: $c")) ) ) case Exit.Success(v) => p.complete(Success(v)) } p.future } } }
Example 181
Source File: package.scala From sttp with Apache License 2.0 | 5 votes |
package sttp.client.impl import sttp.client.testing.ConvertToFuture import _root_.scalaz.concurrent.Task import _root_.scalaz.{-\/, \/-} import scala.concurrent.{Future, Promise} import scala.util.{Failure, Success} package object scalaz { val convertScalazTaskToFuture: ConvertToFuture[Task] = new ConvertToFuture[Task] { // from https://github.com/Verizon/delorean override def toFuture[T](value: Task[T]): Future[T] = { val p = Promise[T]() value.unsafePerformAsync { case \/-(a) => p.complete(Success(a)); () case -\/(t) => p.complete(Failure(t)); () } p.future } } }
Example 182
Source File: FinagleBackendTest.scala From sttp with Apache License 2.0 | 5 votes |
package sttp.client.finagle import sttp.client.{NothingT, SttpBackend} import com.twitter.util.{Return, Throw, Future => TFuture} import sttp.client.testing.{ConvertToFuture, HttpTest} import scala.concurrent.{Future, Promise} class FinagleBackendTest extends HttpTest[TFuture] { override implicit val backend: SttpBackend[TFuture, Nothing, NothingT] = FinagleBackend() override implicit val convertToFuture: ConvertToFuture[TFuture] = new ConvertToFuture[TFuture] { override def toFuture[T](value: TFuture[T]): Future[T] = { val promise: Promise[T] = Promise() value.respond { case Return(value) => promise.success(value) case Throw(exception) => promise.failure(exception) } promise.future } } override def throwsExceptionOnUnsupportedEncoding = false override def supportsCustomMultipartContentType = false }
Example 183
Source File: AkkaHttpWebsocketTest.scala From sttp with Apache License 2.0 | 5 votes |
package sttp.client.akkahttp import java.util.concurrent.ConcurrentLinkedQueue import akka.Done import akka.http.scaladsl.model.ws.{Message, TextMessage} import akka.stream.Materializer import akka.stream.scaladsl._ import org.scalatest.BeforeAndAfterAll import org.scalatest.concurrent.{Eventually, IntegrationPatience} import sttp.client._ import scala.collection.JavaConverters._ import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.Success import org.scalatest.flatspec.AsyncFlatSpec import org.scalatest.matchers.should.Matchers import sttp.client.testing.HttpTest.wsEndpoint class AkkaHttpWebsocketTest extends AsyncFlatSpec with Matchers with BeforeAndAfterAll with Eventually with IntegrationPatience { implicit val ec: ExecutionContext = scala.concurrent.ExecutionContext.global implicit val backend: SttpBackend[Future, Nothing, Flow[Message, Message, *]] = AkkaHttpBackend() it should "send and receive ten messages" in { val received = new ConcurrentLinkedQueue[String]() val sink: Sink[Message, Future[Done]] = collectionSink(received) val n = 10 val source: Source[Message, Promise[Option[Message]]] = Source((1 to n).map(i => TextMessage(s"test$i"))).concatMat(Source.maybe[Message])(Keep.right) val flow: Flow[Message, Message, (Future[Done], Promise[Option[Message]])] = Flow.fromSinkAndSourceMat(sink, source)(Keep.both) basicRequest.get(uri"$wsEndpoint/ws/echo").openWebsocket(flow).flatMap { r => eventually { received.asScala.toList shouldBe (1 to n).map(i => s"echo: test$i").toList } r.result._2.complete(Success(None)) // the source should now complete r.result._1.map(_ => succeed) // the future should be completed once the stream completes (and the ws closes) } } it should "receive two messages" in { val received = new ConcurrentLinkedQueue[String]() val sink: Sink[Message, Future[Done]] = collectionSink(received) val source: Source[Message, Promise[Option[Message]]] = Source.maybe[Message] val flow: Flow[Message, Message, Promise[Option[Message]]] = Flow.fromSinkAndSourceMat(sink, source)(Keep.right) basicRequest.get(uri"$wsEndpoint/ws/send_and_wait").openWebsocket(flow).flatMap { r => eventually { received.asScala.toList shouldBe List("test10", "test20") } r.result.success(None) // closing succeed } } it should "error if the endpoint is not a websocket" in { basicRequest.get(uri"$wsEndpoint/echo").openWebsocket(Flow.apply[Message]).failed.map { t => t shouldBe a[NotAWebsocketException] } } def collectionSink(queue: ConcurrentLinkedQueue[String]): Sink[Message, Future[Done]] = Sink .setup[Message, Future[Done]] { (_materializer, _) => Flow[Message] // mapping with parallelism 1 so that messages don't get reordered .mapAsync(1) { case m: TextMessage => implicit val materializer: Materializer = _materializer m.toStrict(1.second).map(Some(_)) case _ => Future.successful(None) } .collect { case Some(TextMessage.Strict(text)) => text } .toMat(Sink.foreach(queue.add))(Keep.right) } .mapMaterializedValue(_.flatMap(identity)) override protected def afterAll(): Unit = { backend.close() super.afterAll() } }
Example 184
Source File: JobRunServiceFixture.scala From metronome with Apache License 2.0 | 5 votes |
package dcos.metronome package jobrun import java.time.Clock import dcos.metronome.model._ import mesosphere.marathon.core.task.Task import scala.collection.concurrent.TrieMap import scala.concurrent.duration.Duration import scala.concurrent.{Future, Promise} object JobRunServiceFixture { def simpleJobRunService(): JobRunService = new JobRunService { val specs = TrieMap.empty[JobRunId, StartedJobRun] override def getJobRun(jobRunId: JobRunId): Future[Option[StartedJobRun]] = { Future.successful(specs.get(jobRunId)) } override def killJobRun(jobRunId: JobRunId): Future[StartedJobRun] = { specs.get(jobRunId) match { case Some(value) => Future.successful(value) case None => Future.failed(JobRunDoesNotExist(jobRunId)) } } override def activeRuns(jobSpecId: JobId): Future[Iterable[StartedJobRun]] = { Future.successful(specs.values.filter(_.jobRun.jobSpec.id == jobSpecId)) } override def listRuns(filter: JobRun => Boolean): Future[Iterable[StartedJobRun]] = { Future.successful(specs.values.filter(r => filter(r.jobRun))) } override def startJobRun(jobSpec: JobSpec, schedule: Option[ScheduleSpec] = None): Future[StartedJobRun] = { val startingDeadline: Option[Duration] = schedule.map(_.startingDeadline) val run = JobRun( JobRunId(jobSpec), jobSpec, JobRunStatus.Active, Clock.systemUTC().instant(), None, startingDeadline, Map.empty[Task.Id, JobRunTask] ) val startedRun = StartedJobRun(run, Promise[JobResult].future) specs += run.id -> startedRun Future.successful(startedRun) } } }
Example 185
Source File: JobRunModule.scala From metronome with Apache License 2.0 | 5 votes |
package dcos.metronome package jobrun import java.time.Clock import akka.actor.{ActorContext, ActorSystem, Props} import dcos.metronome.jobrun.impl.{ JobRunExecutorActor, JobRunPersistenceActor, JobRunServiceActor, JobRunServiceDelegate } import dcos.metronome.model.{JobResult, JobRun, JobRunId} import dcos.metronome.repository.Repository import mesosphere.marathon.MarathonSchedulerDriverHolder import mesosphere.marathon.core.launchqueue.LaunchQueue import mesosphere.marathon.core.leadership.LeadershipModule import mesosphere.marathon.core.task.tracker.InstanceTracker import mesosphere.marathon.metrics.Metrics import scala.concurrent.Promise class JobRunModule( config: JobRunConfig, actorSystem: ActorSystem, clock: Clock, jobRunRepository: Repository[JobRunId, JobRun], launchQueue: LaunchQueue, instanceTracker: InstanceTracker, driverHolder: MarathonSchedulerDriverHolder, metrics: Metrics, leadershipModule: LeadershipModule ) { import com.softwaremill.macwire._ private[this] def executorFactory(jobRun: JobRun, promise: Promise[JobResult]): Props = { val persistenceActorFactory = (id: JobRunId, context: ActorContext) => context.actorOf(JobRunPersistenceActor.props(id, jobRunRepository, metrics)) JobRunExecutorActor.props( jobRun, promise, persistenceActorFactory, launchQueue, instanceTracker, driverHolder, clock )(actorSystem.scheduler) } val jobRunServiceActor = leadershipModule.startWhenLeader( JobRunServiceActor.props(clock, executorFactory, jobRunRepository, metrics), "JobRunService" ) def jobRunService: JobRunService = wire[JobRunServiceDelegate] }
Example 186
Source File: JsonSupport.scala From akka-stream-json with Apache License 2.0 | 5 votes |
package de.knutwalker.akka.http import de.knutwalker.akka.stream.JsonStreamParser import akka.http.scaladsl.model.HttpEntity import akka.http.scaladsl.model.MediaTypes.`application/json` import akka.http.scaladsl.unmarshalling.{ FromEntityUnmarshaller, Unmarshaller } import akka.http.scaladsl.util.FastFuture import akka.stream.scaladsl.Sink import akka.stream.stage.{ GraphStageLogic, GraphStageWithMaterializedValue, InHandler } import akka.stream.{ AbruptStageTerminationException, Attributes, Inlet, SinkShape } import jawn.Facade import scala.concurrent.{ Future, Promise } import java.util.NoSuchElementException object JsonSupport extends JsonSupport { private def firstElementSink[J <: AnyRef]: Sink[J, Future[J]] = Sink.fromGraph(new FirstElementSinkStage[J]) private final class FirstElementSinkStage[J <: AnyRef] extends GraphStageWithMaterializedValue[SinkShape[J], Future[J]] { private[this] val in: Inlet[J] = Inlet("firstElement.in") override val shape: SinkShape[J] = SinkShape.of(in) override protected def initialAttributes: Attributes = Attributes.name("firstElement") override def createLogicAndMaterializedValue(inheritedAttributes: Attributes): (GraphStageLogic, Future[J]) = { val p: Promise[J] = Promise() (new GraphStageLogic(shape) with InHandler { private[this] var element: J = null.asInstanceOf[J] override def preStart(): Unit = pull(in) def onPush(): Unit = { if (element eq null) { element = grab(in) } pull(in) } override def onUpstreamFinish(): Unit = { val el = element element = null.asInstanceOf[J] if (el ne null) { p.trySuccess(el) } else { p.tryFailure(new NoSuchElementException("No complete json entity consumed")) } completeStage() } override def onUpstreamFailure(ex: Throwable): Unit = { element = null.asInstanceOf[J] p.tryFailure(ex) failStage(ex) } override def postStop(): Unit = { if (!p.isCompleted) { p.failure(new AbruptStageTerminationException(this)) () } } setHandler(in, this) }, p.future) } override def toString: String = "FirstElementSinkStage" } } trait JsonSupport { implicit def jsonUnmarshaller[J <: AnyRef : Facade]: FromEntityUnmarshaller[J] = Unmarshaller.withMaterializer[HttpEntity, J](_ => implicit mat => { case HttpEntity.Strict(_, data) => FastFuture(JsonStreamParser.parse[J](data)) case entity => entity.dataBytes.via(JsonStreamParser[J]).runWith(JsonSupport.firstElementSink[J]) }).forContentTypes(`application/json`) }
Example 187
Source File: ClientFlowIdleTimeoutSpec.scala From squbs with Apache License 2.0 | 5 votes |
package org.squbs.httpclient import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.stream.ActorMaterializer import akka.stream.scaladsl.{Sink, Source, TcpIdleTimeoutException} import com.typesafe.config.ConfigFactory import org.scalatest.{AsyncFlatSpec, BeforeAndAfterAll, Matchers} import org.squbs.resolver.ResolverRegistry import org.squbs.testkit.Timeouts.awaitMax import scala.concurrent.{Await, Promise} import scala.util.{Failure, Success} object ClientFlowIdleTimeoutSpec { val config = ConfigFactory.parseString( """ |akka { | loggers = [ | "akka.event.Logging$DefaultLogger" | ] | | loglevel = "DEBUG" | | http { | server { | idle-timeout = 240 s | request-timeout = 120 s | } | | client.idle-timeout = 1 s | | host-connection-pool.max-retries = 0 | } |} """.stripMargin) implicit val system = ActorSystem("ClientFlowIdleTimeoutSpec", config) implicit val materializer = ActorMaterializer() ResolverRegistry(system).register[HttpEndpoint]("LocalhostEndpointResolver") { (svcName, _) => svcName match { case "slow" => Some(HttpEndpoint(s"http://localhost:$port")) case _ => None }} import akka.http.scaladsl.server.Directives._ import system.dispatcher val route = path("slow") { get { val promise = Promise[String] // Never completing the promise onComplete(promise.future) { case Success(value) => complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Slow...!")) case Failure(ex) => complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Slow failed...!")) } } } val serverBinding = Await.result(Http().bindAndHandle(route, "localhost", 0), awaitMax) val port = serverBinding.localAddress.getPort } class ClientFlowIdleTimeoutSpec extends AsyncFlatSpec with Matchers with BeforeAndAfterAll { import ClientFlowIdleTimeoutSpec._ override def afterAll: Unit = { serverBinding.unbind() map {_ => system.terminate()} } it should "drop the connection after idle-timeout and resume the stream with new connections" in { val clientFlow = ClientFlow[Int]("slow") val result = Source(1 to 10) .map(HttpRequest(uri = "/slow") -> _) .via(clientFlow) .runWith(Sink.seq) result map { r => val failures = r.map(_._1).filter(_.isFailure).map(_.failed) failures should have size 10 failures.forall(_.get.isInstanceOf[TcpIdleTimeoutException]) shouldBe true r.map(_._2) should contain theSameElementsAs(1 to 10) } } }
Example 188
Source File: TestStage.scala From swave with Mozilla Public License 2.0 | 5 votes |
package swave.core.internal.testkit import scala.collection.immutable.VectorBuilder import scala.concurrent.{Future, Promise} import swave.core.impl.stages.StageImpl private[testkit] trait TestStage extends StageImpl { private[this] val resultBuilder = new VectorBuilder[AnyRef] private[this] var _resultSize = 0 private[this] var _fixtureState: TestFixture.State = TestFixture.State.Starting private[this] val _finishedState = Promise[TestFixture.State.Finished]() private[this] var onElem: AnyRef ⇒ Unit = x ⇒ () def fixtureState: TestFixture.State = _fixtureState def fixtureState_=(value: TestFixture.State): Unit = { value match { case TestFixture.State.Cancelled ⇒ _finishedState.success(TestFixture.State.Cancelled) case TestFixture.State.Completed ⇒ _finishedState.success(TestFixture.State.Completed) case TestFixture.State.Error(e) ⇒ _finishedState.failure(e) case _ ⇒ } _fixtureState = value } def finishedState: Future[TestFixture.State.Finished] = _finishedState.future private[testkit] final def result[T]: Vector[T] = resultBuilder.result().asInstanceOf[Vector[T]] private[testkit] final def resultSize: Int = _resultSize protected final def recordElem(elem: AnyRef): Unit = { resultBuilder += elem _resultSize += 1 onElem(elem) } def appendElemHandler(f: AnyRef ⇒ Unit): Unit = { val prev = onElem onElem = { elem ⇒ prev(elem) f(elem) } } def id: Int def formatLong: String def scriptedSize: Int }
Example 189
Source File: MetadataLoaderGroup.scala From sbt-dependency-updates with Apache License 2.0 | 5 votes |
package org.jmotor.sbt.metadata import org.apache.maven.artifact.versioning.ArtifactVersion import org.jmotor.artifact.exception.ArtifactNotFoundException import org.jmotor.artifact.metadata.MetadataLoader import org.jmotor.artifact.metadata.loader.IvyPatternsMetadataLoader import org.jmotor.sbt.concurrent.MultiFuture import sbt.librarymanagement.Constant import sbt.librarymanagement.Patch import sbt.librarymanagement.{ Binary, Disabled, Full, ModuleID } import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.{ ExecutionContext, Future, Promise } import scala.util.{ Failure, Success } class MetadataLoaderGroup(scalaVersion: String, scalaBinaryVersion: String, loaders: Seq[MetadataLoader]) { def getVersions(module: ModuleID, sbtSettings: Option[(String, String)]): Future[Seq[ArtifactVersion]] = { if (loaders.lengthCompare(1) > 0) { firstCompletedOf(loaders.map { loader ⇒ val (artifactId, attrs) = getArtifactIdAndAttrs(loader, module, sbtSettings) loader.getVersions(module.organization, artifactId, attrs) }) } else { loaders.headOption.fold(Future.successful(Seq.empty[ArtifactVersion])) { loader ⇒ val (artifactId, attrs) = getArtifactIdAndAttrs(loader, module, sbtSettings) loader.getVersions(module.organization, artifactId, attrs) } } } private[metadata] def firstCompletedOf(futures: TraversableOnce[Future[Seq[ArtifactVersion]]]) (implicit executor: ExecutionContext): Future[Seq[ArtifactVersion]] = { val p = Promise[Seq[ArtifactVersion]]() val multiFuture = new MultiFuture[Seq[ArtifactVersion]](p, futures.size, Seq.empty) futures foreach { future ⇒ future.onComplete { case Success(r) if r.nonEmpty ⇒ p trySuccess r case Success(_) ⇒ multiFuture.tryComplete() case Failure(_: ArtifactNotFoundException) ⇒ multiFuture.tryComplete() case Failure(t) ⇒ multiFuture.tryComplete(t) }(scala.concurrent.ExecutionContext.Implicits.global) } p.future } private[metadata] def getArtifactIdAndAttrs(loader: MetadataLoader, module: ModuleID, sbtSettings: Option[(String, String)]): (String, Map[String, String]) = { val remapVersion = module.crossVersion match { case _: Disabled ⇒ None case _: Binary ⇒ Option(scalaBinaryVersion) case _: Full ⇒ Option(scalaVersion) case _: Patch ⇒ Option(scalaVersion) case constant: Constant ⇒ Option(constant.value) case _ ⇒ None } val name = remapVersion.map(v ⇒ s"${module.name}_$v").getOrElse(module.name) loader match { case _: IvyPatternsMetadataLoader if sbtSettings.isDefined ⇒ val settings = sbtSettings.get name -> Map("sbtVersion" -> settings._1, "scalaVersion" -> settings._2) case _ ⇒ name -> Map.empty } } } object MetadataLoaderGroup { def apply(scalaVersion: String, scalaBinaryVersion: String, loaders: MetadataLoader*): MetadataLoaderGroup = { new MetadataLoaderGroup(scalaVersion, scalaBinaryVersion, loaders) } }
Example 190
Source File: BlockTransferService.scala From BigDatalog with Apache License 2.0 | 5 votes |
package org.apache.spark.network import java.io.Closeable import java.nio.ByteBuffer import scala.concurrent.{Promise, Await, Future} import scala.concurrent.duration.Duration import org.apache.spark.Logging import org.apache.spark.network.buffer.{NioManagedBuffer, ManagedBuffer} import org.apache.spark.network.shuffle.{ShuffleClient, BlockFetchingListener} import org.apache.spark.storage.{BlockManagerId, BlockId, StorageLevel} private[spark] abstract class BlockTransferService extends ShuffleClient with Closeable with Logging { def uploadBlockSync( hostname: String, port: Int, execId: String, blockId: BlockId, blockData: ManagedBuffer, level: StorageLevel): Unit = { Await.result(uploadBlock(hostname, port, execId, blockId, blockData, level), Duration.Inf) } }
Example 191
Source File: NettyRpcCallContext.scala From BigDatalog with Apache License 2.0 | 5 votes |
package org.apache.spark.rpc.netty import scala.concurrent.Promise import org.apache.spark.Logging import org.apache.spark.network.client.RpcResponseCallback import org.apache.spark.rpc.{RpcAddress, RpcCallContext} private[netty] abstract class NettyRpcCallContext(override val senderAddress: RpcAddress) extends RpcCallContext with Logging { protected def send(message: Any): Unit override def reply(response: Any): Unit = { send(response) } override def sendFailure(e: Throwable): Unit = { send(RpcFailure(e)) } } private[netty] class RemoteNettyRpcCallContext( nettyEnv: NettyRpcEnv, callback: RpcResponseCallback, senderAddress: RpcAddress) extends NettyRpcCallContext(senderAddress) { override protected def send(message: Any): Unit = { val reply = nettyEnv.serialize(message) callback.onSuccess(reply) } }
Example 192
Source File: ExecutionApi.scala From c4proto with Apache License 2.0 | 5 votes |
package ee.cone.c4actor import java.nio.file.Path import java.util.concurrent.ExecutorService import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.Try trait Execution extends Runnable { def onShutdown(hint: String, f:()=>Unit): ()=>Unit def complete(): Unit def skippingFuture[T](value: T): SkippingFuture[T] def newExecutorService(prefix: String, threadCount: Option[Int]): ExecutorService def fatal[T](future: ExecutionContext=>Future[T]): Unit def mainExecutionContext: ExecutionContext def success[T](promise: Promise[T], value: T): Unit } trait SkippingFuture[T] { def map(body: T => T): SkippingFuture[T] def value: Option[Try[T]] } trait ExecutableApp { def execution: Runnable // we need this while we have componentRegistry.resolve to avoid 2 componentRegistry-s } trait Executable extends Runnable // mark Executable with Early // if it is highly optimal to start it before the world is ready; // Early Executable SHOULD NOT write to anything (kafka,db,jms) // because another instance of the same app may be still alive; trait Early abstract class ExecutionFilter(val check: Executable=>Boolean) trait Config { def get(key: String): String } trait ListConfig { def get(key: String): List[String] } case class ActorName(value: String) trait SimpleSigner extends Signer[List[String]] trait Signer[T] { def sign(data: T, until: Long): String def retrieve(check: Boolean): Option[String]=>Option[T] } //// object Trace { //m. b. to util def apply[T](f: =>T): T = try { f } catch { case e: Throwable => System.err.println(s"TRACED: ${e.getMessage}") e.printStackTrace() throw e } } object FinallyClose { def apply[A<:AutoCloseable,R](o: A)(f: A=>R): R = try f(o) finally o.close() def apply[A,R](close: A=>Unit)(o: A)(f: A=>R): R = try f(o) finally close(o) } trait CatchNonFatal { def apply[T](aTry: =>T)(hint: =>String)(aCatch: Throwable=>T): T } case class NanoTimer(startedAt: Long = System.nanoTime){ def ms: Long = (System.nanoTime - startedAt) / 1000000 }
Example 193
Source File: WorldProviderImpl.scala From c4proto with Apache License 2.0 | 5 votes |
package ee.cone.c4gate_server import ee.cone.c4actor.QProtocol.S_Firstborn import ee.cone.c4actor.Types.{NextOffset, SrcId} import ee.cone.c4actor._ import ee.cone.c4assemble.Types.{Each, Values} import ee.cone.c4assemble.c4assemble import ee.cone.c4di.{c4, provide} import scala.concurrent.duration.Duration import scala.concurrent.{Await, ExecutionContext, Future, Promise} class WorldProviderImpl( qMessages: QMessages, receiverF: Future[StatefulReceiver[WorldMessage]], offsetOpt: Option[NextOffset], txAdd: LTxAdd, ) extends WorldProvider { def tx[R](f: Context=>(List[LEvent[Product]],R))(implicit executionContext: ExecutionContext): Future[TxRes[R]] = { val promise = Promise[Context]() for { receiver <- receiverF local <- { receiver.send(new WorldConsumerMessage(promise,offsetOpt)) promise.future } } yield { val (events,res) = f(local) val nLocal = txAdd.add(events)(local) val offset = ReadAfterWriteOffsetKey.of(qMessages.send(nLocal)) new TxRes(res,new WorldProviderImpl(qMessages,receiverF,Option(offset),txAdd)) } } } // todo: fix? if TxAdd had done nothing, offset will be minimal, // but promise does not resolve instantly; // ex. pong can take 200ms until the next WorldProviderMessage @c4assemble("WorldProviderApp") class WorldProviderAssembleBase(worldProviderProvider: WorldProviderProviderImpl){ def join( srcId: SrcId, firstborn: Each[S_Firstborn] ): Values[(SrcId,TxTransform)] = List(WithPK(WorldProviderTx()(worldProviderProvider.receiverFuture))) } case class WorldProviderTx(srcId: SrcId="WorldProviderTx")(receiverF: Future[StatefulReceiver[WorldMessage]]) extends TxTransform { def transform(local: Context): Context = concurrent.blocking{ val nLocal = new Context(local.injected, local.assembled, local.executionContext, Map.empty) Await.result(receiverF, Duration.Inf).send(new WorldProviderMessage(nLocal)) local } } @c4("WorldProviderApp") final class WorldProviderProviderImpl( qMessages: QMessages, execution: Execution, statefulReceiverFactory: StatefulReceiverFactory, getOffset: GetOffset, txAdd: LTxAdd, )( receiverPromise: Promise[StatefulReceiver[WorldMessage]] = Promise() ) extends Executable { def receiverFuture: Future[StatefulReceiver[WorldMessage]] = receiverPromise.future @provide def getWorldProvider: Seq[WorldProvider] = List(new WorldProviderImpl(qMessages,receiverFuture,None,txAdd)) def run(): Unit = execution.fatal { implicit ec => val receiverF = statefulReceiverFactory.create(List(new WorldProviderReceiverImpl(None,Nil)(getOffset,execution))) ignorePromise(receiverPromise.completeWith(receiverF)) receiverF } private def ignorePromise[T](value: Promise[T]): Unit = () } class WorldProviderReceiverImpl(localOpt: Option[Context], waitList: List[WorldConsumerMessage])(getOffset: GetOffset, execution: Execution) extends Observer[WorldMessage] { def activate(message: WorldMessage): Observer[WorldMessage] = message match { case incoming: WorldProviderMessage => val incomingOffset = getOffset.of(incoming.local) val(toHold,toResolve) = waitList.partition(sm=>sm.offsetOpt.exists(incomingOffset < _)) toResolve.foreach(m=>execution.success(m.promise,incoming.local)) new WorldProviderReceiverImpl(Option(incoming.local), toHold)(getOffset,execution) case incoming: WorldConsumerMessage if incoming.offsetOpt.isEmpty && localOpt.nonEmpty => execution.success(incoming.promise, localOpt.get) this case incoming: WorldConsumerMessage => new WorldProviderReceiverImpl(localOpt, incoming :: waitList)(getOffset,execution) } } sealed trait WorldMessage class WorldConsumerMessage(val promise: Promise[Context], val offsetOpt: Option[NextOffset]) extends WorldMessage class WorldProviderMessage(val local: Context) extends WorldMessage
Example 194
Source File: PulsarSinkGraphStage.scala From pulsar4s with Apache License 2.0 | 5 votes |
package com.sksamuel.pulsar4s.akka.streams import akka.Done import akka.stream.stage.{AsyncCallback, GraphStageLogic, GraphStageWithMaterializedValue, InHandler} import akka.stream.{Attributes, Inlet, SinkShape} import com.sksamuel.exts.Logging import com.sksamuel.pulsar4s.{Producer, ProducerMessage} import scala.concurrent.{ExecutionContextExecutor, Future, Promise} import scala.util.{Failure, Success} class PulsarSinkGraphStage[T](createFn: () => Producer[T]) extends GraphStageWithMaterializedValue[SinkShape[ProducerMessage[T]], Future[Done]] with Logging { private val in = Inlet.create[ProducerMessage[T]]("pulsar.in") override def shape: SinkShape[ProducerMessage[T]] = SinkShape.of(in) override def createLogicAndMaterializedValue(inheritedAttributes: Attributes): (GraphStageLogic, Future[Done]) = { val promise = Promise[Done]() val logic: GraphStageLogic = new GraphStageLogic(shape) with InHandler { setHandler(in, this) implicit def context: ExecutionContextExecutor = super.materializer.executionContext var producer: Producer[T] = _ var next: AsyncCallback[ProducerMessage[T]] = _ var error: Throwable = _ override def preStart(): Unit = { producer = createFn() next = getAsyncCallback { _ => pull(in) } pull(in) } override def onPush(): Unit = { try { val t = grab(in) logger.debug(s"Sending message $t") producer.sendAsync(t).onComplete { case Success(_) => next.invoke(t) case Failure(e) => logger.error("Failing pulsar sink stage", e) failStage(e) } } catch { case e: Throwable => logger.error("Failing pulsar sink stage", e) failStage(e) } } override def postStop(): Unit = { logger.debug("Graph stage stopping; closing producer") producer.flush() producer.close() } override def onUpstreamFailure(ex: Throwable): Unit = { promise.tryFailure(ex) } override def onUpstreamFinish(): Unit = { promise.trySuccess(Done) } } (logic, promise.future) } }
Example 195
Source File: PulsarMultiSinkGraphStage.scala From pulsar4s with Apache License 2.0 | 5 votes |
package com.sksamuel.pulsar4s.akka.streams import akka.Done import akka.stream.stage.{AsyncCallback, GraphStageLogic, GraphStageWithMaterializedValue, InHandler} import akka.stream.{Attributes, Inlet, SinkShape} import com.sksamuel.exts.Logging import com.sksamuel.pulsar4s.{Producer, ProducerMessage, Topic} import scala.concurrent.duration._ import scala.concurrent.{Await, ExecutionContextExecutor, Future, Promise} import scala.util.{Failure, Success} class PulsarMultiSinkGraphStage[T](createFn: Topic => Producer[T], initTopics: Set[Topic] = Set.empty) extends GraphStageWithMaterializedValue[SinkShape[(Topic, ProducerMessage[T])], Future[Done]] with Logging { private val in = Inlet.create[(Topic, ProducerMessage[T])]("pulsar.in") override def shape: SinkShape[(Topic, ProducerMessage[T])] = SinkShape.of(in) override def createLogicAndMaterializedValue(inheritedAttributes: Attributes): (GraphStageLogic, Future[Done]) = { val promise = Promise[Done]() val logic: GraphStageLogic = new GraphStageLogic(shape) with InHandler { setHandler(in, this) implicit def context: ExecutionContextExecutor = super.materializer.executionContext var producers: Map[Topic, Producer[T]] = _ var next: AsyncCallback[(Topic, ProducerMessage[T])] = _ var error: Throwable = _ override def preStart(): Unit = { producers = initTopics.map(t => t -> createFn(t)).toMap next = getAsyncCallback { _ => pull(in) } pull(in) } private def getProducer(topic: Topic): Producer[T] = producers.get(topic) match { case Some(p) => p case None => logger.debug(s"creating new producer for topic $topic") val producer = createFn(topic) producers += topic -> producer producer } override def onPush(): Unit = { try { val (topic, message) = grab(in) logger.debug(s"Sending message $message to $topic") val producer = getProducer(topic) producer.sendAsync(message).onComplete { case Success(_) => next.invoke(topic -> message) case Failure(e) => logger.error("Failing pulsar sink stage", e) failStage(e) } } catch { case e: Throwable => logger.error("Failing pulsar sink stage", e) failStage(e) } } override def postStop(): Unit = { logger.debug("Graph stage stopping; closing producers") val fs = producers.flatMap { case (_, p) => Seq( p.flushAsync, p.closeAsync ) } Await.ready(Future.sequence(fs), 15.seconds) } override def onUpstreamFailure(ex: Throwable): Unit = { promise.tryFailure(ex) } override def onUpstreamFinish(): Unit = { promise.trySuccess(Done) } } (logic, promise.future) } }
Example 196
Source File: KillableSingleThread.scala From shellbase with Apache License 2.0 | 5 votes |
package com.sumologic.shellbase.interrupts import java.util.concurrent.TimeoutException import scala.concurrent.duration.Duration import scala.concurrent.{Await, Future, Promise} import scala.util.Try class KillableSingleThread[T](fn: => T) { private val resultPromise = Promise[T]() private val thread = new Thread("killable-thread") { override def run(): Unit = { resultPromise.tryComplete(Try(fn)) } } private def interrupt(): Unit = { thread.interrupt() } private def stop(): Unit = { //noinspection ScalaDeprecation thread.stop() resultPromise.tryFailure(new ThreadDeath) } def future: Future[T] = resultPromise.future def start(): Unit = { thread.start() } def waitForCompletion(waitDuration: Duration): Boolean = { try { Await.ready(resultPromise.future, waitDuration) true } catch { case _: TimeoutException => false case _: Throwable => future.isCompleted } } def kill(gracePeriod: Duration): Unit = { interrupt() if (!waitForCompletion(gracePeriod)) { stop() } } }
Example 197
Source File: ProductionNode.scala From ingraph with Eclipse Public License 1.0 | 5 votes |
package ingraph.ire.nodes.unary import akka.actor.Actor import ingraph.ire.datatypes.Tuple import ingraph.ire.listeners.{AddListener, ChangeListener} import ingraph.ire.messages._ import scala.collection.mutable import scala.concurrent.Promise class ProductionNode(queryName: String, val expectedTerminatorCount: Int = 1) extends Actor { val log = context.system.log val receivedTerminatorCount = mutable.Map.empty[Int, Int] // TODO find datastructure that stores order but has O(1) delete val results = new mutable.ListBuffer[Tuple] val terminatorPromises = mutable.Map.empty[Int, Promise[Iterable[Tuple]]] val inputsToResume = mutable.Map.empty[Int, Iterable[ReteMessage => Unit]] val listeners = new mutable.ListBuffer[ChangeListener] var t0 = System.nanoTime() def getAndResetElapsedTime(): Long = { val t1 = System.nanoTime() val retVal = t1 - t0 t0 = t1 retVal } override def receive: Actor.Receive = { case ChangeSet(p, n) => p.foreach { t => results += t listeners.foreach(_.added(t)) } n.foreach { t => results -= t listeners.foreach(_.removed(t)) } case ExpectMoreTerminators(terminatorID, inputs) => inputsToResume(terminatorID) = inputs case TerminatorMessage(terminatorID, messageID, route) => log.info(s"terminator$messageID received") receivedTerminatorCount(messageID) += 1 if (receivedTerminatorCount(messageID) == expectedTerminatorCount) { inputsToResume(terminatorID).foreach(input => input(Resume(messageID))) listeners.foreach(_.terminated()) terminatorPromises(messageID).success(results.toList) receivedTerminatorCount.drop(messageID) terminatorPromises.drop(messageID) } case ExpectTerminator(terminatorID, messageID, promise) => receivedTerminatorCount(messageID) = 0 terminatorPromises(messageID) = promise case AddListener(listener) => results.foreach(listener.added) listener.terminated() listeners += listener } }
Example 198
Source File: Terminator.scala From ingraph with Eclipse Public License 1.0 | 5 votes |
package ingraph.ire.messages import java.io.{IOException, ObjectInputStream, ObjectOutputStream} import akka.actor.ActorRef import ingraph.ire.datatypes.Tuple import ingraph.ire.util.AtomicUniqueCounter import scala.collection.mutable import scala.concurrent.{Future, Promise} class Terminator private(terminatorID: Int, val inputs: Iterable[ReteMessage => Unit], production: ActorRef) extends ReteMessage with Serializable { var lastMessageID = -1 def send(): Future[Iterable[Tuple]] = { val messageID = Terminator.idCounter.getNext lastMessageID = messageID val promise = Promise[Iterable[Tuple]] production ! ExpectTerminator(terminatorID, messageID, promise) val future = promise.future inputs.foreach(input => { input(Pause(messageID)) input(TerminatorMessage(terminatorID, messageID)) }) future } def resend(): Future[Iterable[Tuple]] = { val promise = Promise[Iterable[Tuple]] production ! ExpectTerminator(terminatorID, lastMessageID, promise) val future = promise.future inputs.foreach(input => { input(TerminatorMessage(terminatorID, lastMessageID)) }) future } @throws(classOf[IOException]) private def writeObject(out: ObjectOutputStream): Unit = {} @throws(classOf[IOException]) private def readObject(in: ObjectInputStream): Unit = {} } object Terminator { val idCounter = new AtomicUniqueCounter def apply(inputs: Iterable[ReteMessage => Unit], productionNode: ActorRef): Terminator = { val id = idCounter.getNext productionNode ! ExpectMoreTerminators(id, inputs) new Terminator(id, inputs, productionNode) } } trait TerminatorHandler { val expectedTerminatorCount: Int val terminatorCount = new mutable.HashMap[Int, Int] def forward(terminator: TerminatorMessage) def handleTerminator(terminator: TerminatorMessage): Unit = { val count = terminatorCount.getOrElse(terminator.messageID, 0) + 1 if (count >= expectedTerminatorCount) { forward(terminator) terminatorCount -= terminator.messageID } terminatorCount(terminator.messageID) = count } }
Example 199
Source File: ReactApolloTest.scala From apollo-scalajs with MIT License | 5 votes |
package com.apollographql.scalajs.react import com.apollographql.scalajs.cache.InMemoryCache import com.apollographql.scalajs.link.{HttpLink, HttpLinkOptions} import com.apollographql.scalajs.{ApolloBoostClient, ApolloClient, CurrencyRatesQuery, UnfetchFetch} import org.scalajs.dom.document import org.scalatest.{Assertion, AsyncFunSuite} import slinky.web.ReactDOM import slinky.web.html.div import scala.concurrent.Promise import scala.scalajs.js import scala.scalajs.js.JSON class ReactApolloTest extends AsyncFunSuite { js.Dynamic.global.window.fetch = UnfetchFetch implicit override def executionContext = scala.concurrent.ExecutionContext.Implicits.global test("Can mount an ApolloProvider with a client instance") { assert(!js.isUndefined( ReactDOM.render( ApolloProvider( client = ApolloBoostClient(uri = "https://graphql-currency-rates.glitch.me") )( div() ), document.createElement("div") ) )) } test("Can server-side render data to string based on a query") { val link = new HttpLink(options = HttpLinkOptions(uri = "https://graphql-currency-rates.glitch.me")) val cache = new InMemoryCache() val client = new ApolloClient(options = js.Dynamic.literal(ssrMode = true, link = link, cache = cache)) ReactApolloServer.renderToStringWithData( ApolloProvider(ApolloProvider.Props(client = client))( Query( CurrencyRatesQuery, CurrencyRatesQuery.Variables("USD") ) { d => if (d.data.isDefined) { div(d.data.get.rates.get.head.get.currency.get) } else "" } ) ).toFuture.map { html => assert(html == """<div data-reactroot="">AED</div>""") } } }
Example 200
Source File: IOConnection.scala From cats-effect with Apache License 2.0 | 5 votes |
package cats.effect package internals import java.util.concurrent.atomic.AtomicReference import scala.annotation.tailrec import scala.concurrent.Promise val uncancelable: IOConnection = new Uncancelable final private class Uncancelable extends IOConnection { def cancel = IO.unit def isCanceled: Boolean = false def push(token: CancelToken[IO]): Unit = () def pop(): CancelToken[IO] = IO.unit def pushPair(lh: IOConnection, rh: IOConnection): Unit = () } final private class Impl extends IOConnection { private[this] val state = new AtomicReference(List.empty[CancelToken[IO]]) private[this] val p: Promise[Unit] = Promise() val cancel = IO.suspend { state.getAndSet(null) match { case Nil => IO { p.success(()); () } case null => IOFromFuture(p.future) case list => CancelUtils .cancelAll(list.iterator) .redeemWith(ex => IO(p.success(())).flatMap(_ => IO.raiseError(ex)), _ => IO { p.success(()); () }) } } def isCanceled: Boolean = state.get eq null @tailrec def push(cancelable: CancelToken[IO]): Unit = state.get() match { case null => cancelable.unsafeRunAsyncAndForget() case list => val update = cancelable :: list if (!state.compareAndSet(list, update)) push(cancelable) } def pushPair(lh: IOConnection, rh: IOConnection): Unit = push(CancelUtils.cancelAll(lh.cancel, rh.cancel)) @tailrec def pop(): CancelToken[IO] = state.get() match { case null | Nil => IO.unit case current @ (x :: xs) => if (!state.compareAndSet(current, xs)) pop() else x } } }