Commit 16ef231
authored
Implement dynamic token stale period based on token TTL (#677)
## Summary
Extends the token refresh buffer from a fixed 5 minutes to a dynamic
period that adapts to token lifetime, improving reliability across
different token types while maintaining backward compatibility.
The TTL is computed based on the remaining time to live at the moment
the token is received and not its real TTL. For example, if the token
has a TTL of 60 minutes but was acquired 20 minutes before being used by
the SDK (e.g. through the CLI), its effective TTL will be 60 − 20 = 40
minutes.
## Why
The previous 5 minute stale period barely covered the allowed monthly
downtime of ~4.32 minutes. Thus, if the auth services were to be down
and a request were to come 4 minute into the stale period it would have
only 1 minute to obtain a new valid token before expiry. With an
extended stale period of 20 minutes, the SDK has an extra ~15 minutes of
stale but valid tokens to use, allowing the auth system to recover.
## Changes
- Increase the maximum stale period from 5 to 20 minutes to support
99.95%
availability.
- Implement dynamic stale period calculation: min(TTL × 0.5, 20
minutes).
- Compute stale period per-token at acquisition time.
### Backward compatibility:
- The public Builder method setStaleDuration() is preserved. Calling it
disables dynamic mode via a useDynamicStaleDuration flag, reverting the
behavior to the legacy fixed-duration stale window. This ensures that
any caller already configuring a custom stale period is unaffected.
## Implementation:
- Add computeStaleDuration(Token) that computes min(TTL / 2,
MAX_STALE_DURATION).
- Add useDynamicStaleDuration flag to the Builder, defaulting to true;
setStaleDuration() sets it to false.
- Add volatile dynamicStaleDuration field; initialized to
MAX_STALE_DURATION as a safe default when no token is pre-set, or
computed from the pre-set token via computeStaleDuration() when one is
provided.
- Update getTokenState() to use dynamicStaleDuration or the legacy
staleDuration based on the flag.
- Update getTokenBlocking() to recompute the stale period after a
successful synchronous refresh.
- Update triggerAsyncRefresh() to recompute the stale period after a
successful async refresh.
## Testing
- Update testAsyncRefreshParametrized to use TestClockSupplier for
deterministic time control, adding a clockAdvanceMinutes parameter to
bring tokens into the stale window without relying on wall-clock timing.
- Add a capped stale duration scenario: 60-min TTL token advanced 41
minutes leaves lifeTime = 19 min ≤ 20 min cap → STALE, verifying
MAX_STALE_DURATION is correctly applied.
- Update testAsyncRefreshFailureFallback to use a 4-minute TTL token
advanced by 3 minutes to reliably enter the stale window under the
dynamic formula.1 parent 7554de9 commit 16ef231
File tree
3 files changed
+119
-15
lines changed- databricks-sdk-java/src
- main/java/com/databricks/sdk/core/oauth
- test/java/com/databricks/sdk/core/oauth
3 files changed
+119
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
Lines changed: 57 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
36 | 41 | | |
37 | 42 | | |
38 | 43 | | |
| |||
42 | 47 | | |
43 | 48 | | |
44 | 49 | | |
45 | | - | |
46 | | - | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
47 | 56 | | |
48 | 57 | | |
49 | 58 | | |
| |||
59 | 68 | | |
60 | 69 | | |
61 | 70 | | |
62 | | - | |
| 71 | + | |
| 72 | + | |
63 | 73 | | |
64 | 74 | | |
65 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
66 | 82 | | |
67 | 83 | | |
68 | 84 | | |
| |||
75 | 91 | | |
76 | 92 | | |
77 | 93 | | |
| 94 | + | |
78 | 95 | | |
79 | 96 | | |
80 | 97 | | |
| |||
130 | 147 | | |
131 | 148 | | |
132 | 149 | | |
| 150 | + | |
133 | 151 | | |
134 | 152 | | |
135 | 153 | | |
| |||
188 | 206 | | |
189 | 207 | | |
190 | 208 | | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
191 | 224 | | |
192 | 225 | | |
193 | 226 | | |
| |||
197 | 230 | | |
198 | 231 | | |
199 | 232 | | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
200 | 237 | | |
201 | 238 | | |
202 | 239 | | |
203 | 240 | | |
| 241 | + | |
204 | 242 | | |
205 | 243 | | |
206 | 244 | | |
| |||
228 | 266 | | |
229 | 267 | | |
230 | 268 | | |
| 269 | + | |
231 | 270 | | |
232 | | - | |
| 271 | + | |
233 | 272 | | |
234 | 273 | | |
235 | 274 | | |
236 | 275 | | |
237 | 276 | | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
238 | 285 | | |
239 | 286 | | |
240 | 287 | | |
| |||
279 | 326 | | |
280 | 327 | | |
281 | 328 | | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
282 | 335 | | |
283 | 336 | | |
284 | 337 | | |
| |||
Lines changed: 61 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
22 | 37 | | |
23 | 38 | | |
24 | 39 | | |
25 | 40 | | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
32 | 68 | | |
33 | 69 | | |
34 | 70 | | |
35 | 71 | | |
36 | 72 | | |
37 | 73 | | |
38 | 74 | | |
| 75 | + | |
39 | 76 | | |
40 | 77 | | |
41 | 78 | | |
42 | 79 | | |
43 | 80 | | |
| 81 | + | |
| 82 | + | |
44 | 83 | | |
45 | 84 | | |
46 | 85 | | |
47 | 86 | | |
48 | 87 | | |
49 | | - | |
| 88 | + | |
50 | 89 | | |
51 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
52 | 95 | | |
53 | 96 | | |
54 | 97 | | |
| |||
69 | 112 | | |
70 | 113 | | |
71 | 114 | | |
| 115 | + | |
72 | 116 | | |
73 | 117 | | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
74 | 121 | | |
75 | 122 | | |
76 | 123 | | |
| |||
90 | 137 | | |
91 | 138 | | |
92 | 139 | | |
93 | | - | |
| 140 | + | |
94 | 141 | | |
95 | 142 | | |
96 | 143 | | |
97 | 144 | | |
98 | 145 | | |
99 | | - | |
| 146 | + | |
100 | 147 | | |
101 | 148 | | |
102 | 149 | | |
| |||
132 | 179 | | |
133 | 180 | | |
134 | 181 | | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
135 | 185 | | |
136 | 186 | | |
137 | 187 | | |
| |||
0 commit comments