Skip to content

Commit

Permalink
WIP view SPI
Browse files Browse the repository at this point in the history
  • Loading branch information
johanandren committed Dec 10, 2024
1 parent 75b2a5b commit 224f7c5
Show file tree
Hide file tree
Showing 17 changed files with 786 additions and 1,160 deletions.
6 changes: 5 additions & 1 deletion akka-javasdk-maven/akka-javasdk-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@

<!-- These are dependent on runtime environment and cannot be customized by users -->
<maven.compiler.release>21</maven.compiler.release>
<kalix-runtime.version>1.3.0-a14205d</kalix-runtime.version>
<<<<<<< Updated upstream
<kalix-runtime.version>1.3.0-a14205d-51-c76efec3-dev-SNAPSHOT</kalix-runtime.version>
=======
<kalix-runtime.version>1.3.0-a14205d-51-c76efec3-dev-SNAPSHOT</kalix-runtime.version>
>>>>>>> Stashed changes

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skip.docker>false</skip.docker>
Expand Down
2 changes: 1 addition & 1 deletion akka-javasdk-tests/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

<root level="DEBUG">
<appender-ref ref="CapturingAppender"/>
<!-- <appender-ref ref="STDOUT"/>-->
<appender-ref ref="STDOUT"/>
</root>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public interface UpdateContext extends MetadataContext {
*/
Optional<String> eventSubject();

// FIXME is this needed anymore?
/** The name of the event being handled. */
String eventName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import akka.javasdk.annotations.Produce.ServiceStream
import akka.javasdk.annotations.Produce.ToTopic
import akka.javasdk.consumer.Consumer
import akka.javasdk.impl.serialization.JsonSerializer
import akka.javasdk.impl.view.ViewDescriptorFactory
import akka.javasdk.keyvalueentity.KeyValueEntity
import akka.javasdk.timedaction.TimedAction
import akka.javasdk.view.TableUpdater
Expand Down Expand Up @@ -359,8 +358,6 @@ private[impl] object ComponentDescriptorFactory {
def getFactoryFor(component: Class[_]): ComponentDescriptorFactory = {
if (Reflect.isEntity(component) || Reflect.isWorkflow(component))
EntityDescriptorFactory
else if (Reflect.isView(component))
ViewDescriptorFactory
else if (Reflect.isConsumer(component))
ConsumerDescriptorFactory
else
Expand Down
32 changes: 13 additions & 19 deletions akka-javasdk/src/main/scala/akka/javasdk/impl/SdkRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import java.lang.reflect.Constructor
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.util.concurrent.CompletionStage

import scala.annotation.nowarn
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.Promise
import scala.jdk.FutureConverters._
import scala.reflect.ClassTag
import scala.util.control.NonFatal

import akka.Done
import akka.actor.typed.ActorSystem
import akka.annotation.InternalApi
Expand Down Expand Up @@ -47,8 +45,7 @@ import akka.javasdk.impl.reflection.Reflect
import akka.javasdk.impl.reflection.Reflect.Syntax.AnnotatedElementOps
import akka.javasdk.impl.timedaction.TimedActionService
import akka.javasdk.impl.timer.TimerSchedulerImpl
import akka.javasdk.impl.view.ViewService
import akka.javasdk.impl.view.ViewsImpl
import akka.javasdk.impl.view.ViewDescriptorFactory
import akka.javasdk.impl.workflow.WorkflowImpl
import akka.javasdk.impl.workflow.WorkflowService
import akka.javasdk.keyvalueentity.KeyValueEntity
Expand Down Expand Up @@ -84,9 +81,9 @@ import io.opentelemetry.context.{ Context => OtelContext }
import kalix.protocol.discovery.Discovery
import kalix.protocol.event_sourced_entity.EventSourcedEntities
import kalix.protocol.value_entity.ValueEntities
import kalix.protocol.view.Views
import kalix.protocol.workflow_entity.WorkflowEntities
import org.slf4j.LoggerFactory

import scala.jdk.OptionConverters.RichOptional
import scala.jdk.CollectionConverters._

Expand All @@ -101,6 +98,7 @@ import akka.runtime.sdk.spi.ConsumerDescriptor
import akka.runtime.sdk.spi.EventSourcedEntityDescriptor
import akka.runtime.sdk.spi.SpiEventSourcedEntity
import akka.runtime.sdk.spi.TimedActionDescriptor
import akka.runtime.sdk.spi.views.SpiViewDescriptor

/**
* INTERNAL API
Expand Down Expand Up @@ -334,7 +332,7 @@ private final class Sdk(
Some(keyValueEntityService(clz.asInstanceOf[Class[KeyValueEntity[Nothing]]]))
} else if (Reflect.isView(clz)) {
logger.debug(s"Registering View [${clz.getName}]")
Some(viewService(clz.asInstanceOf[Class[View]]))
None // no factory, handled below
} else throw new IllegalArgumentException(s"Component class of unknown component type [$clz]")

service match {
Expand Down Expand Up @@ -450,6 +448,13 @@ private final class Sdk(
timedActionSpi)
}

val viewDescriptors: Seq[SpiViewDescriptor] =
componentClasses
.filter(hasComponentId)
.collect {
case clz if classOf[View].isAssignableFrom(clz) => ViewDescriptorFactory(clz, serializer, sdkExecutionContext)
}

// these are available for injecting in all kinds of component that are primarily
// for side effects
// Note: config is also always available through the combination with user DI way down below
Expand All @@ -466,7 +471,6 @@ private final class Sdk(

var eventSourcedEntitiesEndpoint: Option[EventSourcedEntities] = None
var valueEntitiesEndpoint: Option[ValueEntities] = None
var viewsEndpoint: Option[Views] = None
var workflowEntitiesEndpoint: Option[WorkflowEntities] = None

val classicSystem = system.classicSystem
Expand Down Expand Up @@ -506,10 +510,6 @@ private final class Sdk(
case (serviceClass, _: Map[String, TimedActionService[_]] @unchecked)
if serviceClass == classOf[TimedActionService[_]] =>

case (serviceClass, viewServices: Map[String, ViewService[_]] @unchecked)
if serviceClass == classOf[ViewService[_]] =>
viewsEndpoint = Some(new ViewsImpl(viewServices, sdkDispatcherName))

case (serviceClass, _) =>
sys.error(s"Unknown service type: $serviceClass")
}
Expand Down Expand Up @@ -566,8 +566,9 @@ private final class Sdk(
override def discovery: Discovery = discoveryEndpoint
override def eventSourcedEntityDescriptors: Seq[EventSourcedEntityDescriptor] =
Sdk.this.eventSourcedEntityDescriptors

override def valueEntities: Option[ValueEntities] = valueEntitiesEndpoint
override def views: Option[Views] = viewsEndpoint
override def views: Seq[SpiViewDescriptor] = viewDescriptors
override def workflowEntities: Option[WorkflowEntities] = workflowEntitiesEndpoint
override def httpEndpointDescriptors: Seq[HttpEndpointDescriptor] =
Sdk.this.httpEndpointDescriptors
Expand Down Expand Up @@ -637,13 +638,6 @@ private final class Sdk(
case p if p == classOf[KeyValueEntityContext] => context
})

private def viewService[V <: View](clz: Class[V]): ViewService[V] =
new ViewService[V](
clz,
serializer,
// remember to update component type API doc and docs if changing the set of injectables
wiredInstance(_)(PartialFunction.empty))

private def httpEndpointFactory[E](httpEndpointClass: Class[E]): HttpEndpointConstructionContext => E = {
(context: HttpEndpointConstructionContext) =>
lazy val requestContext = new RequestContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private[javasdk] object ViewClientImpl {
// extract view id
val declaringClass = method.getDeclaringClass
val componentId = ComponentDescriptorFactory.readComponentIdIdValue(declaringClass)
val methodName = method.getName.capitalize
val methodName = method.getName
val queryReturnType = getViewQueryReturnType(method)
ViewMethodProperties(componentId, method, methodName, declaringClass, queryReturnType)
}
Expand Down

This file was deleted.

Loading

0 comments on commit 224f7c5

Please sign in to comment.