Skip to content

Commit 76ede4d

Browse files
chore: avoiding calling through the mesh to the proxy (#1055)
1 parent 96cdf54 commit 76ede4d

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

sdk/java-sdk-testkit/src/main/java/kalix/javasdk/testkit/KalixTestKit.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public KalixTestKit start(final Config config) {
175175
proxyContainer.start();
176176
started = true;
177177
// pass on proxy and host to GrpcClients to allow for inter-component communication
178-
GrpcClients.get(runner.system()).setSelfServicePort(proxyContainer.getProxyPort());
178+
GrpcClients.get(runner.system()).setProxyPort(proxyContainer.getProxyPort());
179179
return this;
180180
}
181181

sdk/java-sdk/src/main/scala/kalix/javasdk/impl/DiscoveryImpl.scala

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ class DiscoveryImpl(system: ActorSystem, services: Map[String, Service]) extends
7070
* Discover what components the user function wishes to serve.
7171
*/
7272
override def discover(in: ProxyInfo): scala.concurrent.Future[Spec] = {
73+
log.info(
74+
"Received discovery call from [{} {}] at [{}]:[{}] supporting Kalix protocol {}.{}",
75+
in.proxyName,
76+
in.proxyVersion,
77+
in.internalProxyHostname,
78+
in.proxyPort,
79+
in.protocolMajorVersion,
80+
in.protocolMinorVersion)
7381
if (isVersionProbe(in)) {
7482
// only (silently) send service info for hybrid proxy version probe
7583
Future.successful(Spec(serviceInfo = Some(serviceInfo)))
@@ -78,23 +86,21 @@ class DiscoveryImpl(system: ActorSystem, services: Map[String, Service]) extends
7886
val proxyTerminatedPromise = if (in.devMode) Promise.successful[Done](Done) else Promise[Done]()
7987
proxyTerminatedRef.getAndSet(proxyTerminatedPromise).trySuccess(Done)
8088

81-
log.info(
82-
"Received discovery call from [{} {}] at [{}] supporting Kalix protocol {}.{}",
83-
in.proxyName,
84-
in.proxyVersion,
85-
in.proxyHostname,
86-
in.protocolMajorVersion,
87-
in.protocolMinorVersion)
8889
log.debug(s"Supported sidecar entity types: {}", in.supportedEntityTypes.mkString("[", ",", "]"))
8990

9091
val unsupportedServices = services.values.filterNot { service =>
9192
in.supportedEntityTypes.contains(service.componentType)
9293
}
9394

94-
val grpcClients = GrpcClients.get(system)
95+
val grpcClients = GrpcClients(system)
9596
// pass the deployed name of the service on to GrpcClients for cross component calls
96-
GrpcClients.get(system).setProxyHostname(in.proxyHostname)
97-
97+
if (in.internalProxyHostname.isEmpty) {
98+
// for backward compatibiliy with proxy 1.0.14 or older
99+
grpcClients.setProxyHostname(in.proxyHostname)
100+
} else {
101+
grpcClients.setProxyHostname(in.internalProxyHostname)
102+
}
103+
grpcClients.setProxyPort(in.proxyPort)
98104
grpcClients.setIdentificationInfo(in.identificationInfo)
99105

100106
if (unsupportedServices.nonEmpty) {

sdk/java-sdk/src/main/scala/kalix/javasdk/impl/GrpcClients.scala

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ final class GrpcClients(system: ExtendedActorSystem) extends Extension {
6161
private val log = LoggerFactory.getLogger(classOf[GrpcClients])
6262

6363
@volatile private var proxyHostname: Option[String] = None
64-
@volatile private var selfPort: Option[Int] = None
64+
@volatile private var proxyPort: Option[Int] = None
6565
@volatile private var identificationInfo: Option[IdentificationInfo] = None
6666
private implicit val ec: ExecutionContext = system.dispatcher
6767
private val clients = new ConcurrentHashMap[Key, AnyRef]()
@@ -84,15 +84,14 @@ final class GrpcClients(system: ExtendedActorSystem) extends Extension {
8484
identificationInfo = info
8585
}
8686

87-
def setSelfServicePort(port: Int): Unit = {
87+
def setProxyPort(port: Int): Unit = {
8888
log.debug("Setting port to: [{}]", port)
89-
selfPort = Some(port)
89+
proxyPort = Some(port)
9090
}
9191

9292
def getComponentGrpcClient[T](serviceClass: Class[T]): T = {
93-
getLocalGrpcClient(serviceClass)
93+
getProxyGrpcClient(serviceClass)
9494
}
95-
9695
def getProxyGrpcClient[T](serviceClass: Class[T]): T = {
9796
getLocalGrpcClient(serviceClass)
9897
}
@@ -104,13 +103,22 @@ final class GrpcClients(system: ExtendedActorSystem) extends Extension {
104103
def getGrpcClient[T](serviceClass: Class[T], service: String): T =
105104
getGrpcClient(serviceClass, service, port = 80, remoteAddHeader)
106105

107-
/** Local gRPC clients point to services (user components or Kalix services) in the same deployable */
106+
/** gRPC clients point to services (user components or Kalix services) in the same deployable */
108107
private def getLocalGrpcClient[T](serviceClass: Class[T]): T = {
109-
proxyHostname match {
110-
case Some("localhost") => getGrpcClient(serviceClass, "localhost", selfPort.getOrElse(9000), localAddHeader)
111-
case Some(selfName) => getGrpcClient(serviceClass, selfName, 80, localAddHeader)
112-
case None =>
113-
throw new IllegalStateException("Self service name not set by proxy at discovery, too old proxy version?")
108+
(proxyHostname, proxyPort) match {
109+
case (Some(internalProxyHostname), Some(port)) =>
110+
getGrpcClient(serviceClass, internalProxyHostname, port, localAddHeader)
111+
// for backward compatibiliy with proxy 1.0.14 or older.
112+
case (Some("localhost"), None) =>
113+
log.warn("you are using an old version of the Kalix proxy")
114+
getGrpcClient(serviceClass, "localhost", proxyPort.getOrElse(9000), localAddHeader)
115+
// for backward compatibiliy with proxy 1.0.14 or older
116+
case (Some(proxyHostname), None) =>
117+
log.warn("you are using an old version of the Kalix proxy")
118+
getGrpcClient(serviceClass, proxyHostname, 80, localAddHeader)
119+
case _ =>
120+
throw new IllegalStateException(
121+
"Service proxy hostname and port are not set by proxy at discovery, too old proxy version?")
114122
}
115123
}
116124

0 commit comments

Comments
 (0)