Skip to content

Commit f9ab1cb

Browse files
oh0873pan3793
authored andcommitted
[KYUUBI #7407] STGroup free to avoid OOM Kill
### Why are the changes needed? When LDAP Authentication is used ST Token is created and saved in the cache, but it is never freed up. This is causing continuous increase in heap usage, eventually causing out-of-memory for kyuubi server pods. This changes is added to clear ST Tokens. Also ST Group is added to avoid any race condition during clean up. ### How was this patch tested? This patch was tested in our customer environment. We observed there's no more continuous heap increase after the fix. ### Was this patch authored or co-authored using generative AI tooling? Cursor auto-complete feature was used. Closes #7408 from oh0873/hoonoh/STTokenCleanups. Closes #7407 72740e0 [Hoon Oh] Update kyuubi-common/src/main/scala/org/apache/kyuubi/service/authentication/ldap/Query.scala 18ee9bb [Hoon Oh] Added () to createFilter and render dd0f910 [Hoon Oh] Explicit group definition 321c430 [Hoon Oh] Update kyuubi-common/src/main/scala/org/apache/kyuubi/service/authentication/ldap/Query.scala 28cadbc [Hoon Oh] STGroup free to avoid OOM Kill Lead-authored-by: Hoon Oh <hoonoh@geico.com> Co-authored-by: Hoon Oh <92890928+oh0873@users.noreply.github.com> Signed-off-by: Cheng Pan <chengpan@apache.org> (cherry picked from commit 73a1af1) Signed-off-by: Cheng Pan <chengpan@apache.org>
1 parent 40e266d commit f9ab1cb

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

  • kyuubi-common/src/main/scala/org/apache/kyuubi/service/authentication/ldap

kyuubi-common/src/main/scala/org/apache/kyuubi/service/authentication/ldap/Query.scala

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ package org.apache.kyuubi.service.authentication.ldap
2020
import java.util
2121
import javax.naming.directory.SearchControls
2222

23-
import org.stringtemplate.v4.ST
23+
import org.stringtemplate.v4.{ST, STGroup}
2424

2525
/**
2626
* The object that encompasses all components of a Directory Service search query.
2727
*
28+
* The caller must and can only call each [[filter]] and [[build]] once,
29+
* otherwise [[ST]] internal cache may leak and cause heap OOM.
30+
*
2831
* @see [[LdapSearch]]
2932
*/
3033
object Query {
@@ -40,6 +43,9 @@ object Query {
4043
* A builder of the [[Query]].
4144
*/
4245
final class QueryBuilder {
46+
47+
/** The [[STGroup]] used only for this builder's filter template; unloaded in [[build]]. */
48+
private var filterTemplateGroup: Option[STGroup] = None
4349
private var filterTemplate: ST = _
4450
private val controls: SearchControls = {
4551
val _controls = new SearchControls
@@ -56,7 +62,9 @@ object Query {
5662
* @return the current instance of the builder
5763
*/
5864
def filter(filterTemplate: String): Query.QueryBuilder = {
59-
this.filterTemplate = new ST(filterTemplate)
65+
val group = new STGroup()
66+
this.filterTemplateGroup = Some(group)
67+
this.filterTemplate = new ST(group, filterTemplate)
6068
this
6169
}
6270

@@ -112,7 +120,7 @@ object Query {
112120
require(filterTemplate != null, "filter is required for LDAP search query")
113121
}
114122

115-
private def createFilter: String = filterTemplate.render
123+
private def createFilter(): String = filterTemplate.render()
116124

117125
private def updateControls(): Unit = {
118126
if (!returningAttributes.isEmpty) controls.setReturningAttributes(
@@ -126,7 +134,9 @@ object Query {
126134
*/
127135
def build: Query = {
128136
validate()
129-
val filter: String = createFilter
137+
val filter: String = createFilter()
138+
// Unload template cache after render to avoid CompiledST/STToken retention
139+
filterTemplateGroup.foreach(_.unload())
130140
updateControls()
131141
new Query(filter, controls)
132142
}

0 commit comments

Comments
 (0)