This is an automated email from the ASF dual-hosted git repository.

gurwls223 pushed a commit to branch branch-3.5
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-3.5 by this push:
     new b20955003f9 [SPARK-44278][CONNECT] Implement a GRPC server interceptor 
that cleans up thread local properties
b20955003f9 is described below

commit b20955003f9566bc1caeb625c0eb51402e5eab94
Author: Yihong He <[email protected]>
AuthorDate: Thu Jul 20 08:59:29 2023 +0900

    [SPARK-44278][CONNECT] Implement a GRPC server interceptor that cleans up 
thread local properties
    
    ### What changes were proposed in this pull request?
    
    This pr implements a GRPC server interceptor that cleans up thread local 
properties (e.g. SparkContext local properties) for Spark Connect service.
    
    ### Why are the changes needed?
    
    To prevent the leakage of thread-local variables from one request to 
another request, ensure proper isolation between requests.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No
    
    ### How was this patch tested?
    
    Existing tests
    
    Closes #41831 from heyihong/SPARK-44278.
    
    Authored-by: Yihong He <[email protected]>
    Signed-off-by: Hyukjin Kwon <[email protected]>
    (cherry picked from commit fa9725fac0c0c0aca5d86f2a5f0a80f821d5d828)
    Signed-off-by: Hyukjin Kwon <[email protected]>
---
 .../LocalPropertiesCleanupInterceptor.scala        | 51 ++++++++++++++++++++++
 .../connect/service/InterceptorRegistrySuite.scala | 10 +++++
 2 files changed, 61 insertions(+)

diff --git 
a/connector/connect/server/src/main/scala/org/apache/spark/sql/connect/service/LocalPropertiesCleanupInterceptor.scala
 
b/connector/connect/server/src/main/scala/org/apache/spark/sql/connect/service/LocalPropertiesCleanupInterceptor.scala
new file mode 100644
index 00000000000..1d9acc4d75e
--- /dev/null
+++ 
b/connector/connect/server/src/main/scala/org/apache/spark/sql/connect/service/LocalPropertiesCleanupInterceptor.scala
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.connect.service
+
+import io.grpc.{Metadata, ServerCall, ServerCallHandler, ServerInterceptor}
+import io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener
+
+import org.apache.spark.SparkContext
+
+/**
+ * Interceptor for cleaning up local properties in the SparkContext after gRPC 
server calls.
+ */
+class LocalPropertiesCleanupInterceptor extends ServerInterceptor {
+
+  override def interceptCall[ReqT, RespT](
+      serverCall: ServerCall[ReqT, RespT],
+      metadata: Metadata,
+      serverCallHandler: ServerCallHandler[ReqT, RespT]): 
ServerCall.Listener[ReqT] = {
+    new SimpleForwardingServerCallListener[ReqT](
+      serverCallHandler.startCall(serverCall, metadata)) {
+      override def onComplete(): Unit = {
+        cleanupLocalProperties()
+        super.onComplete()
+      }
+
+      override def onCancel(): Unit = {
+        cleanupLocalProperties()
+        super.onCancel()
+      }
+
+      private def cleanupLocalProperties(): Unit = {
+        SparkContext.getActive.foreach(_.getLocalProperties.clear())
+      }
+    }
+  }
+}
diff --git 
a/connector/connect/server/src/test/scala/org/apache/spark/sql/connect/service/InterceptorRegistrySuite.scala
 
b/connector/connect/server/src/test/scala/org/apache/spark/sql/connect/service/InterceptorRegistrySuite.scala
index 7f85966f0a7..33f6627ee0e 100644
--- 
a/connector/connect/server/src/test/scala/org/apache/spark/sql/connect/service/InterceptorRegistrySuite.scala
+++ 
b/connector/connect/server/src/test/scala/org/apache/spark/sql/connect/service/InterceptorRegistrySuite.scala
@@ -184,4 +184,14 @@ class InterceptorRegistrySuite extends SharedSparkSession {
       assert(interceptors.head.isInstanceOf[LoggingInterceptor])
     }
   }
+
+  test("LocalPropertiesCleanupInterceptor initializes when configured in spark 
conf") {
+    withSparkConf(
+      Connect.CONNECT_GRPC_INTERCEPTOR_CLASSES.key ->
+        
"org.apache.spark.sql.connect.service.LocalPropertiesCleanupInterceptor") {
+      val interceptors = 
SparkConnectInterceptorRegistry.createConfiguredInterceptors()
+      assert(interceptors.size == 1)
+      assert(interceptors.head.isInstanceOf[LocalPropertiesCleanupInterceptor])
+    }
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to