Commit dd0c7b9
authored
Fix IdleConnectionEvictor thread leak in long-running services (#1271)
## Summary
Fixes #1221 — `IdleConnectionEvictor` threads accumulate over time in
long-running services even when all JDBC connections are properly
closed.
**Root cause (two related bugs in
`DatabricksDriverFeatureFlagsContextFactory`):**
1. **Ref-count overcounting**: `getInstance()` incremented the ref-count
on every call, but `isTelemetryEnabled()` calls it repeatedly for the
same connection while `DatabricksConnection.close()` only calls
`removeInstance()` once. This meant the feature-flags context (and its
15-minute scheduler) never shut down after the last connection closed.
2. **Stale connection context → orphaned HTTP client**: The
feature-flags context is shared per-host but HTTP clients are scoped
per-connection UUID. The context stored the *first* connection's
`connectionContext`. When that connection closed, its HTTP client was
removed from the factory. On the next 15-min refresh,
`refreshAllFeatureFlags()` called
`DatabricksHttpClientFactory.getClient(staleContext)` — the UUID was
gone, so `computeIfAbsent` silently created a brand-new
`DatabricksHttpClient` (and a new `IdleConnectionEvictor` thread) that
nobody would ever clean up.
**Fix** (mirrors the pattern already used correctly in
`TelemetryClientFactory`):
- `FeatureFlagsContextHolder` now tracks connection UUIDs
(`ConcurrentHashMap<String, IDatabricksConnectionContext>`) instead of a
bare ref-count.
- `getInstance()` registers the UUID idempotently — no overcounting.
- `removeInstance()` removes the UUID; when the set is empty the context
shuts down.
- When the "owner" connection closes but others remain, the stored
`connectionContext` is atomically updated to another active connection
so future HTTP-client lookups resolve to a live client.
## Test plan
- [x] `DatabricksDriverFeatureFlagsContextFactoryTest` — updated
existing tests for new UUID-based semantics and added:
- `testMultipleCallsWithSameConnectionAreIdempotent` — single
connection, multiple `getInstance` calls, one `removeInstance` is
sufficient
- `testMultipleConnectionsRequireAllToClose` — two connections share
context; closing one keeps it alive, closing both shuts it down
- `testConnectionContextUpdatedWhenOwnerConnectionCloses` — verifies the
stored `connectionContext` switches to another connection when the owner
closes
- [x] All existing `DatabricksDriverFeatureFlagsContextTest` tests still
pass
- [x] Full build with `mvn test` passes
---------
Signed-off-by: Samikshya Chand <samikshya.chand@databricks.com>1 parent 03ca3c1 commit dd0c7b9
5 files changed
Lines changed: 163 additions & 53 deletions
File tree
- src
- main/java/com/databricks/jdbc/common/safe
- test/java/com/databricks/jdbc/common/safe
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
Lines changed: 9 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
| 36 | + | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| |||
139 | 139 | | |
140 | 140 | | |
141 | 141 | | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
142 | 150 | | |
143 | 151 | | |
144 | 152 | | |
| |||
Lines changed: 33 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
| |||
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
32 | | - | |
| 34 | + | |
33 | 35 | | |
34 | | - | |
| 36 | + | |
35 | 37 | | |
36 | | - | |
37 | | - | |
| 38 | + | |
| 39 | + | |
38 | 40 | | |
39 | 41 | | |
40 | 42 | | |
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
44 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
45 | 50 | | |
46 | 51 | | |
47 | 52 | | |
48 | 53 | | |
49 | 54 | | |
50 | 55 | | |
| 56 | + | |
51 | 57 | | |
52 | 58 | | |
53 | 59 | | |
54 | | - | |
55 | | - | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
56 | 64 | | |
57 | 65 | | |
58 | 66 | | |
59 | | - | |
60 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
61 | 75 | | |
62 | 76 | | |
63 | 77 | | |
| |||
67 | 81 | | |
68 | 82 | | |
69 | 83 | | |
70 | | - | |
| 84 | + | |
71 | 85 | | |
72 | | - | |
73 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
74 | 94 | | |
75 | 95 | | |
Lines changed: 14 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | | - | |
| 8 | + | |
8 | 9 | | |
9 | | - | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
12 | 22 | | |
13 | 23 | | |
Lines changed: 106 additions & 35 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
| 48 | + | |
47 | 49 | | |
48 | 50 | | |
49 | 51 | | |
| |||
74 | 76 | | |
75 | 77 | | |
76 | 78 | | |
77 | | - | |
78 | | - | |
| 79 | + | |
| 80 | + | |
79 | 81 | | |
80 | 82 | | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
| 83 | + | |
| 84 | + | |
85 | 85 | | |
86 | | - | |
87 | | - | |
| 86 | + | |
| 87 | + | |
88 | 88 | | |
89 | | - | |
90 | | - | |
| 89 | + | |
91 | 90 | | |
92 | 91 | | |
93 | | - | |
94 | | - | |
| 92 | + | |
| 93 | + | |
95 | 94 | | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
100 | 98 | | |
101 | 99 | | |
102 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
103 | 165 | | |
104 | 166 | | |
105 | 167 | | |
| |||
108 | 170 | | |
109 | 171 | | |
110 | 172 | | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
116 | 184 | | |
117 | 185 | | |
118 | 186 | | |
119 | 187 | | |
120 | | - | |
| 188 | + | |
121 | 189 | | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
| 190 | + | |
| 191 | + | |
126 | 192 | | |
127 | | - | |
128 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
129 | 196 | | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
| 197 | + | |
| 198 | + | |
134 | 199 | | |
135 | | - | |
136 | | - | |
137 | | - | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
138 | 207 | | |
139 | 208 | | |
140 | 209 | | |
| |||
167 | 236 | | |
168 | 237 | | |
169 | 238 | | |
| 239 | + | |
| 240 | + | |
170 | 241 | | |
171 | 242 | | |
172 | 243 | | |
| |||
0 commit comments