@databricks/sql 1.13.0 → 1.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -0
- package/dist/DBSQLClient.d.ts +107 -0
- package/dist/DBSQLClient.js +470 -0
- package/dist/DBSQLClient.js.map +1 -1
- package/dist/DBSQLOperation.d.ts +32 -1
- package/dist/DBSQLOperation.js +138 -29
- package/dist/DBSQLOperation.js.map +1 -1
- package/dist/DBSQLSession.d.ts +1 -0
- package/dist/DBSQLSession.js +43 -12
- package/dist/DBSQLSession.js.map +1 -1
- package/dist/connection/auth/DatabricksOAuth/OAuthManager.js +10 -6
- package/dist/connection/auth/DatabricksOAuth/OAuthManager.js.map +1 -1
- package/dist/contracts/IClientContext.d.ts +36 -0
- package/dist/contracts/IDBSQLClient.d.ts +90 -0
- package/dist/contracts/IDBSQLSession.d.ts +6 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +12 -1
- package/dist/index.js.map +1 -1
- package/dist/result/CloudFetchResultHandler.d.ts +8 -1
- package/dist/result/CloudFetchResultHandler.js +28 -2
- package/dist/result/CloudFetchResultHandler.js.map +1 -1
- package/dist/result/RowSetProvider.d.ts +8 -1
- package/dist/result/RowSetProvider.js +110 -1
- package/dist/result/RowSetProvider.js.map +1 -1
- package/dist/telemetry/CircuitBreaker.d.ts +73 -0
- package/dist/telemetry/CircuitBreaker.js +176 -0
- package/dist/telemetry/CircuitBreaker.js.map +1 -0
- package/dist/telemetry/DatabricksTelemetryExporter.d.ts +76 -0
- package/dist/telemetry/DatabricksTelemetryExporter.js +358 -0
- package/dist/telemetry/DatabricksTelemetryExporter.js.map +1 -0
- package/dist/telemetry/ExceptionClassifier.js +12 -0
- package/dist/telemetry/ExceptionClassifier.js.map +1 -1
- package/dist/telemetry/FeatureFlagCache.d.ts +61 -0
- package/dist/telemetry/FeatureFlagCache.js +214 -0
- package/dist/telemetry/FeatureFlagCache.js.map +1 -0
- package/dist/telemetry/MetricsAggregator.d.ts +110 -0
- package/dist/telemetry/MetricsAggregator.js +610 -0
- package/dist/telemetry/MetricsAggregator.js.map +1 -0
- package/dist/telemetry/TelemetryClient.d.ts +121 -0
- package/dist/telemetry/TelemetryClient.js +296 -0
- package/dist/telemetry/TelemetryClient.js.map +1 -0
- package/dist/telemetry/TelemetryClientProvider.d.ts +62 -0
- package/dist/telemetry/TelemetryClientProvider.js +154 -0
- package/dist/telemetry/TelemetryClientProvider.js.map +1 -0
- package/dist/telemetry/TelemetryEventEmitter.d.ts +119 -0
- package/dist/telemetry/TelemetryEventEmitter.js +151 -0
- package/dist/telemetry/TelemetryEventEmitter.js.map +1 -0
- package/dist/telemetry/telemetryTypeMappers.d.ts +25 -0
- package/dist/telemetry/telemetryTypeMappers.js +72 -0
- package/dist/telemetry/telemetryTypeMappers.js.map +1 -0
- package/dist/telemetry/telemetryUtils.d.ts +70 -0
- package/dist/telemetry/telemetryUtils.js +273 -0
- package/dist/telemetry/telemetryUtils.js.map +1 -0
- package/dist/telemetry/types.d.ts +70 -5
- package/dist/telemetry/types.js +19 -6
- package/dist/telemetry/types.js.map +1 -1
- package/dist/utils/agentDetector.d.ts +20 -0
- package/dist/utils/agentDetector.js +39 -0
- package/dist/utils/agentDetector.js.map +1 -0
- package/dist/utils/buildUserAgentString.js +7 -1
- package/dist/utils/buildUserAgentString.js.map +1 -1
- package/dist/utils/index.d.ts +2 -1
- package/dist/utils/index.js +3 -1
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/queryTags.d.ts +12 -0
- package/dist/utils/queryTags.js +35 -0
- package/dist/utils/queryTags.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Databricks Contributors
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import IClientContext, { ClientConfig } from '../contracts/IClientContext';
|
|
17
|
+
import IDBSQLLogger from '../contracts/IDBSQLLogger';
|
|
18
|
+
import IConnectionProvider from '../connection/contracts/IConnectionProvider';
|
|
19
|
+
import IThriftClient from '../contracts/IThriftClient';
|
|
20
|
+
import IDriver from '../contracts/IDriver';
|
|
21
|
+
import IAuthentication from '../connection/contracts/IAuthentication';
|
|
22
|
+
import { CircuitBreakerState } from './CircuitBreaker';
|
|
23
|
+
import DatabricksTelemetryExporter from './DatabricksTelemetryExporter';
|
|
24
|
+
import MetricsAggregator from './MetricsAggregator';
|
|
25
|
+
import FeatureFlagCache from './FeatureFlagCache';
|
|
26
|
+
/**
|
|
27
|
+
* Per-host telemetry resource owner. Held by `TelemetryClientProvider`
|
|
28
|
+
* (process-wide singleton) and shared across every `DBSQLClient` that
|
|
29
|
+
* connects to the same host.
|
|
30
|
+
*
|
|
31
|
+
* Owns the host-scoped triad — `MetricsAggregator`, `DatabricksTelemetryExporter`,
|
|
32
|
+
* `CircuitBreakerRegistry`, `FeatureFlagCache` — and implements `IClientContext`
|
|
33
|
+
* itself so those owned components have a stable context that outlives any
|
|
34
|
+
* single `DBSQLClient`. The first registered `DBSQLClient`'s logger and config
|
|
35
|
+
* are snapshotted; subsequent registrants donate their connection providers
|
|
36
|
+
* and auth providers, and the `TelemetryClient` falls through them in
|
|
37
|
+
* registration order when the current head closes.
|
|
38
|
+
*
|
|
39
|
+
* Why share at host granularity:
|
|
40
|
+
* - Circuit-breaker state for `host` is correct only if all clients hitting
|
|
41
|
+
* the same endpoint share counters (5 failures means 5 actual failures, not
|
|
42
|
+
* 5×N for N independent `DBSQLClient` instances).
|
|
43
|
+
* - Feature-flag cache has a per-host TTL; deduping the GET prevents
|
|
44
|
+
* thundering-herd on cold cache.
|
|
45
|
+
* - Metric batches mix events from every active client to the same host —
|
|
46
|
+
* one HTTP POST per `flushIntervalMs` instead of N.
|
|
47
|
+
*/
|
|
48
|
+
declare class TelemetryClient implements IClientContext {
|
|
49
|
+
readonly host: string;
|
|
50
|
+
private closed;
|
|
51
|
+
private readonly logger;
|
|
52
|
+
private readonly config;
|
|
53
|
+
private readonly circuitBreakerRegistry;
|
|
54
|
+
private readonly featureFlagCache;
|
|
55
|
+
private readonly exporter;
|
|
56
|
+
private readonly aggregator;
|
|
57
|
+
private contexts;
|
|
58
|
+
constructor(initialContext: IClientContext, host: string);
|
|
59
|
+
/**
|
|
60
|
+
* Add another `DBSQLClient`'s context to the pool. Tracked in registration
|
|
61
|
+
* order; `getConnectionProvider()` / `getAuthProvider()` walk the list and
|
|
62
|
+
* use the first entry that's still usable.
|
|
63
|
+
*/
|
|
64
|
+
registerContext(context: IClientContext): void;
|
|
65
|
+
private warnOnConfigDivergence;
|
|
66
|
+
/**
|
|
67
|
+
* Remove a `DBSQLClient`'s context from the pool. Called by
|
|
68
|
+
* `TelemetryClientProvider.releaseClient` before refcount decrement so the
|
|
69
|
+
* exporter doesn't keep trying to use a closed context.
|
|
70
|
+
*
|
|
71
|
+
* Uses the cached snapshot pair (`context`, `authProvider`) from register
|
|
72
|
+
* time, not a fresh `context.getAuthProvider?.()` call. If the underlying
|
|
73
|
+
* client's auth provider was rotated, a fresh call would not match the
|
|
74
|
+
* original reference and the stale entry would leak.
|
|
75
|
+
*/
|
|
76
|
+
unregisterContext(context: IClientContext): void;
|
|
77
|
+
getConfig(): ClientConfig;
|
|
78
|
+
getLogger(): IDBSQLLogger;
|
|
79
|
+
getConnectionProvider(): Promise<IConnectionProvider>;
|
|
80
|
+
getClient(): Promise<IThriftClient>;
|
|
81
|
+
getDriver(): Promise<IDriver>;
|
|
82
|
+
/**
|
|
83
|
+
* Walk `contexts` and return the first call site that succeeds. Contexts
|
|
84
|
+
* that throw on the same accessor for `MAX_CONSECUTIVE_CTX_FAILURES` calls
|
|
85
|
+
* in a row are pruned from the pool — otherwise a registrant whose
|
|
86
|
+
* underlying `DBSQLClient` is closed or has revoked auth would stay in the
|
|
87
|
+
* list forever and be retried on every export.
|
|
88
|
+
*
|
|
89
|
+
* Pruning is keyed by the `accessor` function identity so a context with a
|
|
90
|
+
* working `getConnectionProvider` but broken `getDriver` isn't dropped from
|
|
91
|
+
* the working accessor's path.
|
|
92
|
+
*/
|
|
93
|
+
private tryFallthrough;
|
|
94
|
+
getAuthProvider(): IAuthentication | undefined;
|
|
95
|
+
getTelemetryEmitter(): undefined;
|
|
96
|
+
getTelemetryAggregator(): MetricsAggregator;
|
|
97
|
+
getExporter(): DatabricksTelemetryExporter;
|
|
98
|
+
getAggregator(): MetricsAggregator;
|
|
99
|
+
getFeatureFlagCache(): FeatureFlagCache;
|
|
100
|
+
/**
|
|
101
|
+
* Operator-visible snapshot of telemetry state for this host. Synchronous,
|
|
102
|
+
* never throws — intended for health-check endpoints, shutdown banners,
|
|
103
|
+
* and operator dashboards.
|
|
104
|
+
*/
|
|
105
|
+
getTelemetryStats(): {
|
|
106
|
+
host: string;
|
|
107
|
+
pendingMetricsCount: number;
|
|
108
|
+
inFlightStatements: number;
|
|
109
|
+
droppedMetrics: number;
|
|
110
|
+
evictedStatements: number;
|
|
111
|
+
circuitBreakerState: CircuitBreakerState;
|
|
112
|
+
};
|
|
113
|
+
getHost(): string;
|
|
114
|
+
isClosed(): boolean;
|
|
115
|
+
/**
|
|
116
|
+
* Drain pending metrics and tear down owned resources. Called by
|
|
117
|
+
* `TelemetryClientProvider.releaseClient` when refCount hits zero.
|
|
118
|
+
*/
|
|
119
|
+
close(): Promise<void>;
|
|
120
|
+
}
|
|
121
|
+
export default TelemetryClient;
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) 2025 Databricks Contributors
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
+
};
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
const IDBSQLLogger_1 = require("../contracts/IDBSQLLogger");
|
|
22
|
+
const CircuitBreaker_1 = require("./CircuitBreaker");
|
|
23
|
+
const DatabricksTelemetryExporter_1 = __importDefault(require("./DatabricksTelemetryExporter"));
|
|
24
|
+
const MetricsAggregator_1 = __importDefault(require("./MetricsAggregator"));
|
|
25
|
+
const FeatureFlagCache_1 = __importDefault(require("./FeatureFlagCache"));
|
|
26
|
+
/**
|
|
27
|
+
* Per-host telemetry resource owner. Held by `TelemetryClientProvider`
|
|
28
|
+
* (process-wide singleton) and shared across every `DBSQLClient` that
|
|
29
|
+
* connects to the same host.
|
|
30
|
+
*
|
|
31
|
+
* Owns the host-scoped triad — `MetricsAggregator`, `DatabricksTelemetryExporter`,
|
|
32
|
+
* `CircuitBreakerRegistry`, `FeatureFlagCache` — and implements `IClientContext`
|
|
33
|
+
* itself so those owned components have a stable context that outlives any
|
|
34
|
+
* single `DBSQLClient`. The first registered `DBSQLClient`'s logger and config
|
|
35
|
+
* are snapshotted; subsequent registrants donate their connection providers
|
|
36
|
+
* and auth providers, and the `TelemetryClient` falls through them in
|
|
37
|
+
* registration order when the current head closes.
|
|
38
|
+
*
|
|
39
|
+
* Why share at host granularity:
|
|
40
|
+
* - Circuit-breaker state for `host` is correct only if all clients hitting
|
|
41
|
+
* the same endpoint share counters (5 failures means 5 actual failures, not
|
|
42
|
+
* 5×N for N independent `DBSQLClient` instances).
|
|
43
|
+
* - Feature-flag cache has a per-host TTL; deduping the GET prevents
|
|
44
|
+
* thundering-herd on cold cache.
|
|
45
|
+
* - Metric batches mix events from every active client to the same host —
|
|
46
|
+
* one HTTP POST per `flushIntervalMs` instead of N.
|
|
47
|
+
*/
|
|
48
|
+
class TelemetryClient {
|
|
49
|
+
constructor(initialContext, host) {
|
|
50
|
+
var _a;
|
|
51
|
+
this.host = host;
|
|
52
|
+
this.closed = false;
|
|
53
|
+
// FIFO so the first-registered client's connection/auth providers are tried
|
|
54
|
+
// first. Falls through to later registrants when an earlier one is closed.
|
|
55
|
+
//
|
|
56
|
+
// The `authProvider` is snapshotted at register time (NOT re-fetched at
|
|
57
|
+
// unregister time). If a registrant's underlying `DBSQLClient` rotates its
|
|
58
|
+
// auth provider between register and unregister (token refresh that
|
|
59
|
+
// reconstructs the provider, lazy wrappers, etc.), filtering by referential
|
|
60
|
+
// equality against a freshly-fetched provider would never match the original,
|
|
61
|
+
// and the stale provider would remain in `authProviders` for the lifetime of
|
|
62
|
+
// the per-host singleton — keeping the exporter authenticating with revoked
|
|
63
|
+
// credentials.
|
|
64
|
+
this.contexts = [];
|
|
65
|
+
this.logger = initialContext.getLogger();
|
|
66
|
+
// Snapshot config at first registration. Subsequent clients with
|
|
67
|
+
// divergent telemetry knobs (`telemetryBatchSize` etc.) inherit the
|
|
68
|
+
// first-registrant's tuning — documented invariant.
|
|
69
|
+
this.config = initialContext.getConfig();
|
|
70
|
+
this.contexts.push({
|
|
71
|
+
context: initialContext,
|
|
72
|
+
authProvider: (_a = initialContext.getAuthProvider) === null || _a === void 0 ? void 0 : _a.call(initialContext),
|
|
73
|
+
});
|
|
74
|
+
this.circuitBreakerRegistry = new CircuitBreaker_1.CircuitBreakerRegistry(this);
|
|
75
|
+
this.featureFlagCache = new FeatureFlagCache_1.default(this);
|
|
76
|
+
// Register this host with the feature-flag cache so isTelemetryEnabled()
|
|
77
|
+
// does not short-circuit to false. close() releases via releaseContext().
|
|
78
|
+
this.featureFlagCache.getOrCreateContext(host);
|
|
79
|
+
this.exporter = new DatabricksTelemetryExporter_1.default(this, host, this.circuitBreakerRegistry);
|
|
80
|
+
this.aggregator = new MetricsAggregator_1.default(this, this.exporter);
|
|
81
|
+
this.logger.log(IDBSQLLogger_1.LogLevel.debug, `Created TelemetryClient for host: ${host}`);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Add another `DBSQLClient`'s context to the pool. Tracked in registration
|
|
85
|
+
* order; `getConnectionProvider()` / `getAuthProvider()` walk the list and
|
|
86
|
+
* use the first entry that's still usable.
|
|
87
|
+
*/
|
|
88
|
+
registerContext(context) {
|
|
89
|
+
var _a, _b, _c;
|
|
90
|
+
if (this.contexts.some((entry) => entry.context === context)) {
|
|
91
|
+
// Re-snapshot the auth provider on a duplicate register call so a caller
|
|
92
|
+
// that intentionally rotated providers can refresh the entry.
|
|
93
|
+
const idx = this.contexts.findIndex((entry) => entry.context === context);
|
|
94
|
+
if (idx >= 0) {
|
|
95
|
+
this.contexts[idx] = { context, authProvider: (_a = context.getAuthProvider) === null || _a === void 0 ? void 0 : _a.call(context) };
|
|
96
|
+
}
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const newAuthProvider = (_b = context.getAuthProvider) === null || _b === void 0 ? void 0 : _b.call(context);
|
|
100
|
+
// Diverging auth providers across registrants on the same host is the
|
|
101
|
+
// multi-tenant FIFO bleed case. The first-registered auth wins; tenant B
|
|
102
|
+
// queries POST under tenant A's headers. Warn loudly — SaaS layers that
|
|
103
|
+
// see this in their logs should either:
|
|
104
|
+
// - set telemetryEnabled: false on all DBSQLClients, or
|
|
105
|
+
// - partition by host so each tenant owns its own TelemetryClient.
|
|
106
|
+
const firstAuthProvider = (_c = this.contexts[0]) === null || _c === void 0 ? void 0 : _c.authProvider;
|
|
107
|
+
if (this.contexts.length > 0 &&
|
|
108
|
+
newAuthProvider !== undefined &&
|
|
109
|
+
firstAuthProvider !== undefined &&
|
|
110
|
+
newAuthProvider !== firstAuthProvider) {
|
|
111
|
+
this.logger.log(IDBSQLLogger_1.LogLevel.warn, `TelemetryClient(${this.host}): a second DBSQLClient registered with a different auth provider. ` +
|
|
112
|
+
`Telemetry from this client will be POSTed under the first-registered client's auth headers. ` +
|
|
113
|
+
`Multi-tenant SaaS layers should set telemetryEnabled: false on all clients or partition by host. ` +
|
|
114
|
+
`See README "Multi-tenant SaaS deployments" for details.`);
|
|
115
|
+
}
|
|
116
|
+
this.contexts.push({
|
|
117
|
+
context,
|
|
118
|
+
authProvider: newAuthProvider,
|
|
119
|
+
});
|
|
120
|
+
// Warn when subsequent registrants pass telemetry knobs that diverge
|
|
121
|
+
// from the first-registrant's snapshot — those values are silently
|
|
122
|
+
// ignored. Privacy-relevant for telemetryAuthenticatedExport.
|
|
123
|
+
this.warnOnConfigDivergence(context.getConfig());
|
|
124
|
+
}
|
|
125
|
+
warnOnConfigDivergence(other) {
|
|
126
|
+
const keys = [
|
|
127
|
+
'telemetryAuthenticatedExport',
|
|
128
|
+
'telemetryBatchSize',
|
|
129
|
+
'telemetryFlushIntervalMs',
|
|
130
|
+
'telemetryMaxRetries',
|
|
131
|
+
'telemetryCircuitBreakerThreshold',
|
|
132
|
+
'telemetryCircuitBreakerTimeout',
|
|
133
|
+
// Privacy-relevant: User-Agent is snapshotted from the first registrant
|
|
134
|
+
// and shared across the host. Multi-tenant SaaS layers with per-tenant
|
|
135
|
+
// userAgentEntry values would otherwise silently ship under tenant-1's UA.
|
|
136
|
+
'userAgentEntry',
|
|
137
|
+
];
|
|
138
|
+
const diverged = keys.filter((k) => other[k] !== undefined && other[k] !== this.config[k]);
|
|
139
|
+
if (diverged.length > 0) {
|
|
140
|
+
this.logger.log(IDBSQLLogger_1.LogLevel.warn, `TelemetryClient(${this.host}): registered context's telemetry settings ` +
|
|
141
|
+
`[${diverged.join(', ')}] differ from the first registrant's; the new values will be ignored.`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Remove a `DBSQLClient`'s context from the pool. Called by
|
|
146
|
+
* `TelemetryClientProvider.releaseClient` before refcount decrement so the
|
|
147
|
+
* exporter doesn't keep trying to use a closed context.
|
|
148
|
+
*
|
|
149
|
+
* Uses the cached snapshot pair (`context`, `authProvider`) from register
|
|
150
|
+
* time, not a fresh `context.getAuthProvider?.()` call. If the underlying
|
|
151
|
+
* client's auth provider was rotated, a fresh call would not match the
|
|
152
|
+
* original reference and the stale entry would leak.
|
|
153
|
+
*/
|
|
154
|
+
unregisterContext(context) {
|
|
155
|
+
this.contexts = this.contexts.filter((entry) => entry.context !== context);
|
|
156
|
+
}
|
|
157
|
+
// -- IClientContext --
|
|
158
|
+
getConfig() {
|
|
159
|
+
return this.config;
|
|
160
|
+
}
|
|
161
|
+
getLogger() {
|
|
162
|
+
return this.logger;
|
|
163
|
+
}
|
|
164
|
+
async getConnectionProvider() {
|
|
165
|
+
return this.tryFallthrough((ctx) => ctx.getConnectionProvider(), 'connection provider');
|
|
166
|
+
}
|
|
167
|
+
async getClient() {
|
|
168
|
+
return this.tryFallthrough((ctx) => ctx.getClient(), 'client');
|
|
169
|
+
}
|
|
170
|
+
async getDriver() {
|
|
171
|
+
return this.tryFallthrough((ctx) => ctx.getDriver(), 'driver');
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Walk `contexts` and return the first call site that succeeds. Contexts
|
|
175
|
+
* that throw on the same accessor for `MAX_CONSECUTIVE_CTX_FAILURES` calls
|
|
176
|
+
* in a row are pruned from the pool — otherwise a registrant whose
|
|
177
|
+
* underlying `DBSQLClient` is closed or has revoked auth would stay in the
|
|
178
|
+
* list forever and be retried on every export.
|
|
179
|
+
*
|
|
180
|
+
* Pruning is keyed by the `accessor` function identity so a context with a
|
|
181
|
+
* working `getConnectionProvider` but broken `getDriver` isn't dropped from
|
|
182
|
+
* the working accessor's path.
|
|
183
|
+
*/
|
|
184
|
+
async tryFallthrough(accessor, accessorName) {
|
|
185
|
+
let lastErr;
|
|
186
|
+
const survivors = [];
|
|
187
|
+
/* eslint-disable no-await-in-loop */
|
|
188
|
+
for (const entry of this.contexts) {
|
|
189
|
+
try {
|
|
190
|
+
const result = await accessor(entry.context);
|
|
191
|
+
// Keep this entry and any not-yet-tried ones. We've found a working
|
|
192
|
+
// head; the tail might be fine too. The throwing entries already in
|
|
193
|
+
// `survivors`'s past iterations are dropped below.
|
|
194
|
+
survivors.push(entry);
|
|
195
|
+
for (const remaining of this.contexts.slice(this.contexts.indexOf(entry) + 1)) {
|
|
196
|
+
survivors.push(remaining);
|
|
197
|
+
}
|
|
198
|
+
this.contexts = survivors;
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
catch (err) {
|
|
202
|
+
lastErr = err;
|
|
203
|
+
// Drop this entry from the survivors set — repeatedly retrying a
|
|
204
|
+
// throwing context drains nothing useful from the FIFO.
|
|
205
|
+
this.logger.log(IDBSQLLogger_1.LogLevel.debug, `TelemetryClient(${this.host}): pruning context that threw on ${accessorName}: ` +
|
|
206
|
+
`${err instanceof Error ? err.message : String(err)}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/* eslint-enable no-await-in-loop */
|
|
210
|
+
this.contexts = survivors;
|
|
211
|
+
throw lastErr instanceof Error
|
|
212
|
+
? lastErr
|
|
213
|
+
: new Error(`TelemetryClient: no ${accessorName} available for host ${this.host}`);
|
|
214
|
+
}
|
|
215
|
+
getAuthProvider() {
|
|
216
|
+
// Walk the FIFO and return the first usable provider. The provider was
|
|
217
|
+
// snapshotted at register time, so even if the registrant's underlying
|
|
218
|
+
// client has since rotated credentials, the original entry can still be
|
|
219
|
+
// dropped by `unregisterContext` via cached-snapshot equality. A registered
|
|
220
|
+
// head with revoked credentials surfaces as an `authenticate()` failure
|
|
221
|
+
// inside the exporter retry loop.
|
|
222
|
+
for (const entry of this.contexts) {
|
|
223
|
+
if (entry.authProvider !== undefined) {
|
|
224
|
+
return entry.authProvider;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
getTelemetryEmitter() {
|
|
230
|
+
// The shared TelemetryClient holds the aggregator; emitters remain
|
|
231
|
+
// per-DBSQLClient so each can respect its own `telemetryEnabled` flag.
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
getTelemetryAggregator() {
|
|
235
|
+
return this.aggregator;
|
|
236
|
+
}
|
|
237
|
+
// -- shared resource accessors --
|
|
238
|
+
getExporter() {
|
|
239
|
+
return this.exporter;
|
|
240
|
+
}
|
|
241
|
+
getAggregator() {
|
|
242
|
+
return this.aggregator;
|
|
243
|
+
}
|
|
244
|
+
getFeatureFlagCache() {
|
|
245
|
+
return this.featureFlagCache;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Operator-visible snapshot of telemetry state for this host. Synchronous,
|
|
249
|
+
* never throws — intended for health-check endpoints, shutdown banners,
|
|
250
|
+
* and operator dashboards.
|
|
251
|
+
*/
|
|
252
|
+
getTelemetryStats() {
|
|
253
|
+
return {
|
|
254
|
+
host: this.host,
|
|
255
|
+
...this.aggregator.getStats(),
|
|
256
|
+
circuitBreakerState: this.circuitBreakerRegistry.getCircuitBreaker(this.host).getState(),
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
getHost() {
|
|
260
|
+
return this.host;
|
|
261
|
+
}
|
|
262
|
+
isClosed() {
|
|
263
|
+
return this.closed;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Drain pending metrics and tear down owned resources. Called by
|
|
267
|
+
* `TelemetryClientProvider.releaseClient` when refCount hits zero.
|
|
268
|
+
*/
|
|
269
|
+
async close() {
|
|
270
|
+
if (this.closed)
|
|
271
|
+
return;
|
|
272
|
+
this.closed = true;
|
|
273
|
+
this.logger.log(IDBSQLLogger_1.LogLevel.debug, `Closing TelemetryClient for host: ${this.host}`);
|
|
274
|
+
try {
|
|
275
|
+
await this.aggregator.close();
|
|
276
|
+
}
|
|
277
|
+
catch (err) {
|
|
278
|
+
this.logger.log(IDBSQLLogger_1.LogLevel.debug, `TelemetryClient aggregator close error: ${err.message}`);
|
|
279
|
+
}
|
|
280
|
+
try {
|
|
281
|
+
this.exporter.dispose();
|
|
282
|
+
}
|
|
283
|
+
catch (err) {
|
|
284
|
+
this.logger.log(IDBSQLLogger_1.LogLevel.debug, `TelemetryClient exporter dispose error: ${err.message}`);
|
|
285
|
+
}
|
|
286
|
+
try {
|
|
287
|
+
this.featureFlagCache.releaseContext(this.host);
|
|
288
|
+
}
|
|
289
|
+
catch (err) {
|
|
290
|
+
this.logger.log(IDBSQLLogger_1.LogLevel.debug, `TelemetryClient FFCache release error: ${err.message}`);
|
|
291
|
+
}
|
|
292
|
+
this.logger.log(IDBSQLLogger_1.LogLevel.debug, `Closed TelemetryClient for host: ${this.host}`);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
exports.default = TelemetryClient;
|
|
296
|
+
//# sourceMappingURL=TelemetryClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TelemetryClient.js","sourceRoot":"","sources":["../../lib/telemetry/TelemetryClient.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;;;AAGH,4DAAmE;AAKnE,qDAA+E;AAC/E,gGAAwE;AACxE,4EAAoD;AACpD,0EAAkD;AAElD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,eAAe;IA4BnB,YAAY,cAA8B,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AI,CAAC;QACnB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,uBAAQ,CAAC,KAAK,EAAE,qCAAqC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QAClF,IAAI;YACF,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;SAC/B;QAAC,OAAO,GAAG,EAAE;YACZ,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,uBAAQ,CAAC,KAAK,EAAE,2CAA4C,GAAa,CAAC,OAAO,EAAE,CAAC,CAAC;SACtG;QACD,IAAI;YACF,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC;SACzB;QAAC,OAAO,GAAG,EAAE;YACZ,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,uBAAQ,CAAC,KAAK,EAAE,2CAA4C,GAAa,CAAC,OAAO,EAAE,CAAC,CAAC;SACtG;QACD,IAAI;YACF,IAAI,CAAC,gBAAgB,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SACjD;QAAC,OAAO,GAAG,EAAE;YACZ,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,uBAAQ,CAAC,KAAK,EAAE,0CAA2C,GAAa,CAAC,OAAO,EAAE,CAAC,CAAC;SACrG;QACD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,uBAAQ,CAAC,KAAK,EAAE,oCAAoC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;IACnF,CAAC;CACF;AAED,kBAAe,eAAe,CAAC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Databricks Contributors
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import IClientContext from '../contracts/IClientContext';
|
|
17
|
+
import TelemetryClient from './TelemetryClient';
|
|
18
|
+
/**
|
|
19
|
+
* Process-wide registry of `TelemetryClient`s, one per host. Multiple
|
|
20
|
+
* `DBSQLClient` instances connecting to the same host share the same
|
|
21
|
+
* `TelemetryClient`, which owns the host-scoped circuit breaker, feature
|
|
22
|
+
* flag cache, exporter, and aggregator.
|
|
23
|
+
*
|
|
24
|
+
* Singleton because the resources we're sharing — circuit-breaker counters,
|
|
25
|
+
* batched HTTP exports — are correct only at process scope. Per-`DBSQLClient`
|
|
26
|
+
* provider scope (the previous design) deduplicates nothing.
|
|
27
|
+
*
|
|
28
|
+
* `getOrCreateClient` is sync: caller increments refcount, registers its
|
|
29
|
+
* context+auth, and walks away. `releaseClient` is `async` because the
|
|
30
|
+
* underlying `TelemetryClient.close()` awaits the final flush.
|
|
31
|
+
*/
|
|
32
|
+
declare class TelemetryClientProvider {
|
|
33
|
+
private static instance;
|
|
34
|
+
private clients;
|
|
35
|
+
private softLimitWarned;
|
|
36
|
+
static getInstance(): TelemetryClientProvider;
|
|
37
|
+
/**
|
|
38
|
+
* Reset the process-wide singleton. Test-only — name-prefixed so
|
|
39
|
+
* production callsites can't reach for it accidentally via autocomplete.
|
|
40
|
+
* Resetting in production drops every host's circuit-breaker counters,
|
|
41
|
+
* feature-flag cache, exporter, and pending-metric buffer at once.
|
|
42
|
+
*
|
|
43
|
+
* @internal Test-only. Production code MUST NOT call this.
|
|
44
|
+
*/
|
|
45
|
+
static __resetInstanceForTests(): void;
|
|
46
|
+
private static normalizeHostKey;
|
|
47
|
+
/**
|
|
48
|
+
* Get or create the `TelemetryClient` for `host`, registering `context` as
|
|
49
|
+
* a participant. Increments refcount.
|
|
50
|
+
*/
|
|
51
|
+
getOrCreateClient(context: IClientContext, host: string): TelemetryClient;
|
|
52
|
+
/**
|
|
53
|
+
* Release `context`'s registration. When the last `DBSQLClient` releases,
|
|
54
|
+
* the underlying `TelemetryClient.close()` runs and the entry is removed.
|
|
55
|
+
*/
|
|
56
|
+
releaseClient(context: IClientContext, host: string): Promise<void>;
|
|
57
|
+
/** @internal Exposed for testing only. */
|
|
58
|
+
getRefCount(host: string): number;
|
|
59
|
+
/** @internal Exposed for testing only. */
|
|
60
|
+
getActiveClients(): Map<string, TelemetryClient>;
|
|
61
|
+
}
|
|
62
|
+
export default TelemetryClientProvider;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) 2025 Databricks Contributors
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
+
};
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
const IDBSQLLogger_1 = require("../contracts/IDBSQLLogger");
|
|
22
|
+
const TelemetryClient_1 = __importDefault(require("./TelemetryClient"));
|
|
23
|
+
// Soft cap on distinct host entries. Above this the provider warns once so a
|
|
24
|
+
// misconfigured caller (per-request hosts, unnormalized aliases) is visible
|
|
25
|
+
// in logs rather than silently growing the map.
|
|
26
|
+
const MAX_CLIENTS_SOFT_LIMIT = 128;
|
|
27
|
+
/**
|
|
28
|
+
* Process-wide registry of `TelemetryClient`s, one per host. Multiple
|
|
29
|
+
* `DBSQLClient` instances connecting to the same host share the same
|
|
30
|
+
* `TelemetryClient`, which owns the host-scoped circuit breaker, feature
|
|
31
|
+
* flag cache, exporter, and aggregator.
|
|
32
|
+
*
|
|
33
|
+
* Singleton because the resources we're sharing — circuit-breaker counters,
|
|
34
|
+
* batched HTTP exports — are correct only at process scope. Per-`DBSQLClient`
|
|
35
|
+
* provider scope (the previous design) deduplicates nothing.
|
|
36
|
+
*
|
|
37
|
+
* `getOrCreateClient` is sync: caller increments refcount, registers its
|
|
38
|
+
* context+auth, and walks away. `releaseClient` is `async` because the
|
|
39
|
+
* underlying `TelemetryClient.close()` awaits the final flush.
|
|
40
|
+
*/
|
|
41
|
+
class TelemetryClientProvider {
|
|
42
|
+
constructor() {
|
|
43
|
+
this.clients = new Map();
|
|
44
|
+
this.softLimitWarned = false;
|
|
45
|
+
}
|
|
46
|
+
// Production code should use `TelemetryClientProvider.getInstance()` for
|
|
47
|
+
// the process-wide singleton. The constructor remains public so unit tests
|
|
48
|
+
// can build an isolated provider with its own map. Deliberate no-op body —
|
|
49
|
+
// initial state is set inline on the field declarations above.
|
|
50
|
+
static getInstance() {
|
|
51
|
+
if (!TelemetryClientProvider.instance) {
|
|
52
|
+
TelemetryClientProvider.instance = new TelemetryClientProvider();
|
|
53
|
+
}
|
|
54
|
+
return TelemetryClientProvider.instance;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Reset the process-wide singleton. Test-only — name-prefixed so
|
|
58
|
+
* production callsites can't reach for it accidentally via autocomplete.
|
|
59
|
+
* Resetting in production drops every host's circuit-breaker counters,
|
|
60
|
+
* feature-flag cache, exporter, and pending-metric buffer at once.
|
|
61
|
+
*
|
|
62
|
+
* @internal Test-only. Production code MUST NOT call this.
|
|
63
|
+
*/
|
|
64
|
+
static __resetInstanceForTests() {
|
|
65
|
+
TelemetryClientProvider.instance = undefined;
|
|
66
|
+
}
|
|
67
|
+
static normalizeHostKey(host) {
|
|
68
|
+
return host
|
|
69
|
+
.trim()
|
|
70
|
+
.toLowerCase()
|
|
71
|
+
.replace(/^https?:\/\//, '')
|
|
72
|
+
.replace(/\/+$/, '')
|
|
73
|
+
.replace(/\.$/, '')
|
|
74
|
+
.replace(/:443$/, '');
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get or create the `TelemetryClient` for `host`, registering `context` as
|
|
78
|
+
* a participant. Increments refcount.
|
|
79
|
+
*/
|
|
80
|
+
getOrCreateClient(context, host) {
|
|
81
|
+
const logger = context.getLogger();
|
|
82
|
+
const key = TelemetryClientProvider.normalizeHostKey(host);
|
|
83
|
+
let holder = this.clients.get(key);
|
|
84
|
+
if (!holder) {
|
|
85
|
+
const client = new TelemetryClient_1.default(context, key);
|
|
86
|
+
holder = { client, refCount: 0 };
|
|
87
|
+
this.clients.set(key, holder);
|
|
88
|
+
logger.log(IDBSQLLogger_1.LogLevel.debug, `Created new TelemetryClient for host: ${host}`);
|
|
89
|
+
if (!this.softLimitWarned && this.clients.size > MAX_CLIENTS_SOFT_LIMIT) {
|
|
90
|
+
this.softLimitWarned = true;
|
|
91
|
+
logger.log(IDBSQLLogger_1.LogLevel.warn, `TelemetryClientProvider has ${this.clients.size} distinct hosts — possible alias or leak`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
holder.client.registerContext(context);
|
|
96
|
+
}
|
|
97
|
+
holder.refCount += 1;
|
|
98
|
+
logger.log(IDBSQLLogger_1.LogLevel.debug, `TelemetryClient reference count for ${host}: ${holder.refCount}`);
|
|
99
|
+
return holder.client;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Release `context`'s registration. When the last `DBSQLClient` releases,
|
|
103
|
+
* the underlying `TelemetryClient.close()` runs and the entry is removed.
|
|
104
|
+
*/
|
|
105
|
+
async releaseClient(context, host) {
|
|
106
|
+
const logger = context.getLogger();
|
|
107
|
+
const key = TelemetryClientProvider.normalizeHostKey(host);
|
|
108
|
+
const holder = this.clients.get(key);
|
|
109
|
+
if (!holder) {
|
|
110
|
+
logger.log(IDBSQLLogger_1.LogLevel.debug, `No TelemetryClient found for host: ${host}`);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (holder.refCount <= 0) {
|
|
114
|
+
logger.log(IDBSQLLogger_1.LogLevel.warn, `Unbalanced release for TelemetryClient host: ${host}`);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
// Skip unregister on the last release so close()'s final flush can still
|
|
118
|
+
// resolve auth/connection providers from the FIFO snapshot.
|
|
119
|
+
if (holder.refCount > 1) {
|
|
120
|
+
holder.client.unregisterContext(context);
|
|
121
|
+
}
|
|
122
|
+
holder.refCount -= 1;
|
|
123
|
+
logger.log(IDBSQLLogger_1.LogLevel.debug, `TelemetryClient reference count for ${host}: ${holder.refCount}`);
|
|
124
|
+
if (holder.refCount <= 0) {
|
|
125
|
+
// Remove from map BEFORE awaiting close so a concurrent
|
|
126
|
+
// getOrCreateClient creates a fresh instance rather than receiving
|
|
127
|
+
// this closing one.
|
|
128
|
+
this.clients.delete(key);
|
|
129
|
+
try {
|
|
130
|
+
await holder.client.close();
|
|
131
|
+
logger.log(IDBSQLLogger_1.LogLevel.debug, `Closed and removed TelemetryClient for host: ${host}`);
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
135
|
+
logger.log(IDBSQLLogger_1.LogLevel.debug, `Error releasing TelemetryClient: ${msg}`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/** @internal Exposed for testing only. */
|
|
140
|
+
getRefCount(host) {
|
|
141
|
+
const holder = this.clients.get(TelemetryClientProvider.normalizeHostKey(host));
|
|
142
|
+
return holder ? holder.refCount : 0;
|
|
143
|
+
}
|
|
144
|
+
/** @internal Exposed for testing only. */
|
|
145
|
+
getActiveClients() {
|
|
146
|
+
const result = new Map();
|
|
147
|
+
for (const [host, holder] of this.clients.entries()) {
|
|
148
|
+
result.set(host, holder.client);
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
exports.default = TelemetryClientProvider;
|
|
154
|
+
//# sourceMappingURL=TelemetryClientProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TelemetryClientProvider.js","sourceRoot":"","sources":["../../lib/telemetry/TelemetryClientProvider.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;;;AAGH,4DAAqD;AACrD,wEAAgD;AAOhD,6EAA6E;AAC7E,4EAA4E;AAC5E,gDAAgD;AAChD,MAAM,sBAAsB,GAAG,GAAG,CAAC;AAEnC;;;;;;;;;;;;;GAaG;AACH,MAAM,uBAAuB;IAA7B;QAGU,YAAO,GAAG,IAAI,GAAG,EAAiC,CAAC;QAEnD,oBAAe,GAAG,KAAK,CAAC;IA2HlC,CAAC;IAzHC,yEAAyE;IACzE,2EAA2E;IAC3E,2EAA2E;IAC3E,+DAA+D;IAE/D,MAAM,CAAC,WAAW;QAChB,IAAI,CAAC,uBAAuB,CAAC,QAAQ,EAAE;YACrC,uBAAuB,CAAC,QAAQ,GAAG,IAAI,uBAAuB,EAAE,CAAC;SAClE;QACD,OAAO,uBAAuB,CAAC,QAAQ,CAAC;IAC1C,CAAC;IAED;;;;;;;OAOG;IACH,MAAM,CAAC,uBAAuB;QAC5B,uBAAuB,CAAC,QAAQ,GAAG,SAAS,CAAC;IAC/C,CAAC;IAEO,MAAM,CAAC,gBAAgB,CAAC,IAAY;QAC1C,OAAO,IAAI;aACR,IAAI,EAAE;aACN,WAAW,EAAE;aACb,OAAO,CAAC,cAAc,EAAE,EAAE,CAAC;aA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