Skip to content

Commit 0f44462

Browse files
authored
refactor(FIR-35470): remove redundant account id resolution (#116)
1 parent 4114cee commit 0f44462

File tree

15 files changed

+56
-323
lines changed

15 files changed

+56
-323
lines changed

src/common/tokenCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AccountInfo } from "../connection/base";
1+
import { AccountInfo } from "../connection/connection_v1";
22

33
export type TokenKey = {
44
clientId: string;

src/connection/base.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from "../types";
77
import { Statement } from "../statement";
88
import { generateUserAgent } from "../common/util";
9-
import { ConnectionError, CompositeError } from "../common/errors";
9+
import { CompositeError } from "../common/errors";
1010
import JSONbig from "json-bigint";
1111

1212
const defaultQuerySettings = {
@@ -17,11 +17,6 @@ const defaultResponseSettings = {
1717
normalizeData: false
1818
};
1919

20-
export interface AccountInfo {
21-
id: string;
22-
infraVersion: number;
23-
}
24-
2520
const updateParametersHeader = "Firebolt-Update-Parameters";
2621
const allowedUpdateParameters = ["database"];
2722
const updateEndpointHeader = "Firebolt-Update-Endpoint";
@@ -34,7 +29,6 @@ export abstract class Connection {
3429
protected options: ConnectionOptions;
3530
protected userAgent: string;
3631
protected parameters: Record<string, string>;
37-
protected accountInfo: AccountInfo | undefined;
3832
engineEndpoint!: string;
3933
activeRequests = new Set<{ abort: () => void }>();
4034

@@ -53,13 +47,6 @@ export abstract class Connection {
5347

5448
abstract resolveEngineEndpoint(): Promise<string>;
5549

56-
abstract resolveAccountInfo(): Promise<AccountInfo>;
57-
58-
async resolveAccountId() {
59-
const accInfo = await this.resolveAccountInfo();
60-
return accInfo.id;
61-
}
62-
6350
protected getRequestUrl(executeQueryOptions: ExecuteQueryOptions): string {
6451
const params = this.getBaseParameters(executeQueryOptions);
6552

@@ -132,16 +119,6 @@ export abstract class Connection {
132119

133120
private async handleUpdateEndpointHeader(headerValue: string): Promise<void> {
134121
const [endpoint, newParams] = this.splitEndpoint(headerValue);
135-
136-
// Validate account_id if present
137-
const currentAccountId =
138-
this.accountInfo?.id ?? (await this.resolveAccountId());
139-
if (newParams.account_id && currentAccountId !== newParams.account_id) {
140-
throw new ConnectionError({
141-
message: `Failed to execute USE ENGINE command. Account parameter mismatch. Contact support.`
142-
});
143-
}
144-
145122
// Remove url parameters and update engineEndpoint
146123
this.engineEndpoint = endpoint;
147124
this.parameters = {

src/connection/connection_v1.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { ACCOUNT, ACCOUNT_BY_NAME } from "../common/api";
2-
import { AccountInfo, Connection as BaseConnection } from "./base";
2+
import { Connection as BaseConnection } from "./base";
33
import { ResourceManager } from "../service";
44

5-
const INFRA_VERSION = 1;
5+
export interface AccountInfo {
6+
id: string;
7+
}
68

79
export class ConnectionV1 extends BaseConnection {
10+
protected accountInfo: AccountInfo | undefined;
11+
812
async resolveEngineEndpoint() {
913
const resourceManager = new ResourceManager({
1014
connection: this,
@@ -27,6 +31,11 @@ export class ConnectionV1 extends BaseConnection {
2731
return this.engineEndpoint;
2832
}
2933

34+
async resolveAccountId() {
35+
const accInfo = await this.resolveAccountInfo();
36+
return accInfo.id;
37+
}
38+
3039
async resolveAccountInfo(): Promise<AccountInfo> {
3140
if (this.accountInfo === undefined) {
3241
const { httpClient, apiEndpoint } = this.context;
@@ -37,15 +46,15 @@ export class ConnectionV1 extends BaseConnection {
3746
const { account_id } = await httpClient
3847
.request<{ account_id: string }>("GET", url)
3948
.ready();
40-
this.accountInfo = { id: account_id, infraVersion: INFRA_VERSION };
49+
this.accountInfo = { id: account_id };
4150
} else {
4251
const url = `${apiEndpoint}/${ACCOUNT}`;
4352
const {
4453
account: { id }
4554
} = await httpClient
4655
.request<{ account: { id: string } }>("GET", url)
4756
.ready();
48-
this.accountInfo = { id, infraVersion: INFRA_VERSION };
57+
this.accountInfo = { id };
4958
}
5059
}
5160
return this.accountInfo;

src/connection/connection_v2.ts

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { AccountNotFoundError, ApiError } from "../common/errors";
2-
import {
3-
ACCOUNT_ID_BY_NAME,
4-
ACCOUNT_SYSTEM_ENGINE,
5-
QUERY_URL
6-
} from "../common/api";
2+
import { ACCOUNT_SYSTEM_ENGINE, QUERY_URL } from "../common/api";
73

8-
import { Connection as BaseConnection, AccountInfo } from "./base";
4+
import { Connection as BaseConnection } from "./base";
95
import { Cache, inMemoryCache, noneCache } from "../common/tokenCache";
106

117
export class ConnectionV2 extends BaseConnection {
@@ -56,44 +52,13 @@ export class ConnectionV2 extends BaseConnection {
5652
}
5753
}
5854

59-
async resolveAccountInfo(): Promise<AccountInfo> {
60-
if (this.accountInfo === undefined) {
61-
const cachedValue = this.cache.accountInfoStorage.get({
62-
account: this.account,
63-
apiEndpoint: this.context.apiEndpoint
64-
});
65-
if (cachedValue) {
66-
this.accountInfo = cachedValue;
67-
} else {
68-
const { httpClient, apiEndpoint } = this.context;
69-
const url = `${apiEndpoint}/${ACCOUNT_ID_BY_NAME(this.account)}`;
70-
const { id, infraVersion } = await httpClient
71-
.request<{ id: string; region: string; infraVersion: string }>(
72-
"GET",
73-
url
74-
)
75-
.ready();
76-
this.accountInfo = { id, infraVersion: parseInt(infraVersion ?? "2") };
77-
this.cache.accountInfoStorage.set(
78-
{
79-
account: this.account,
80-
apiEndpoint: this.context.apiEndpoint
81-
},
82-
this.accountInfo
83-
);
84-
}
85-
}
86-
return this.accountInfo;
87-
}
88-
8955
async resolveEngineEndpoint() {
9056
const { engineName, database } = this.options;
9157
// Connect to system engine first
9258
const [systemUrl, systemParameters] =
9359
await this.getSystemEngineEndpointAndParameters();
9460
this.engineEndpoint = new URL(QUERY_URL, systemUrl).href;
9561
this.parameters = { ...this.parameters, ...systemParameters };
96-
this.accountInfo = await this.resolveAccountInfo();
9762

9863
if (database) {
9964
await this.execute(`USE DATABASE "${database}"`);

src/service/database/v1/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ import {
44
ACCOUNT_ENGINE_URL_BY_DATABASE_NAME,
55
ResultsPage
66
} from "../../../common/api";
7-
import { ResourceManagerContext } from "../../../types";
7+
import { ResourceManagerContextV1 } from "../../../types";
88
import { DatabaseModel } from "./model";
99
import { ID, Database } from "./types";
1010
import { CreateDatabaseOptions } from "../types";
1111
import { resolveRegionKey } from "../../utils";
1212
import { ResourceManager } from "../../index";
13-
1413
export class DatabaseService {
15-
private readonly context: ResourceManagerContext;
14+
private readonly context: ResourceManagerContextV1;
1615

17-
constructor(context: ResourceManagerContext) {
16+
constructor(context: ResourceManagerContextV1) {
1817
this.context = context;
1918
}
2019

src/service/database/v1/model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import {
22
ACCOUNT_ENGINE_URL_BY_DATABASE_NAME,
33
ACCOUNT_DATABASE
44
} from "../../../common/api";
5-
import { ResourceManagerContext } from "../../../types";
5+
import { ResourceManagerContextV1 } from "../../../types";
66
import { ID, Database } from "./types";
77
import { ResourceManager } from "../../index";
88

99
export class DatabaseModel {
10-
private readonly context: ResourceManagerContext;
10+
private readonly context: ResourceManagerContextV1;
1111
id: ID;
1212
name: string;
1313
description: string;
1414

15-
constructor(context: ResourceManagerContext, database: Database) {
15+
constructor(context: ResourceManagerContextV1, database: Database) {
1616
const { id, name, description } = database;
1717
this.id = id;
1818
this.name = name;

src/service/database/v1/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ export type Database = {
44
id: ID;
55
name: string;
66
description: string;
7-
};
7+
};

src/service/engine/v1/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
ENGINES_BY_IDS_URL,
77
ResultsPage
88
} from "../../../common/api";
9-
import { ResourceManagerContext } from "../../../types";
9+
import { ResourceManagerContextV1 } from "../../../types";
1010
import { EngineModel } from "./model";
1111
import { Engine, ID } from "./types";
1212
import { CreateEngineOptions } from "../types";
@@ -19,9 +19,9 @@ import {
1919
import { DatabaseService } from "../../database/v1";
2020

2121
export class EngineService {
22-
private readonly context: ResourceManagerContext;
22+
private readonly context: ResourceManagerContextV1;
2323

24-
constructor(context: ResourceManagerContext) {
24+
constructor(context: ResourceManagerContextV1) {
2525
this.context = context;
2626
}
2727

src/service/engine/v1/model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import {
44
ACCOUNT_ENGINE_STOP,
55
ACCOUNT_ENGINE_RESTART
66
} from "../../../common/api";
7-
import { ResourceManagerContext } from "../../../types";
7+
import { ResourceManagerContextV1 } from "../../../types";
88
import { ID, Engine, EngineStatusSummary } from "./types";
99

1010
export class EngineModel {
11-
private readonly context: ResourceManagerContext;
11+
private readonly context: ResourceManagerContextV1;
1212
id: ID;
1313
name: string;
1414
description: string;
1515
endpoint: string;
1616
current_status_summary: EngineStatusSummary;
1717

18-
constructor(context: ResourceManagerContext, engine: Engine) {
18+
constructor(context: ResourceManagerContextV1, engine: Engine) {
1919
const { id, name, description, endpoint, current_status_summary } = engine;
2020
this.id = id;
2121
this.name = name;

src/service/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DatabaseService as DatabaseServiceV1 } from "./database/v1";
44
import { EngineServiceInterface } from "./engine/types";
55
import { EngineService as EngineServiceV2 } from "./engine";
66
import { EngineService as EngineServiceV1 } from "./engine/v1";
7-
import { ResourceManagerContext } from "../types";
7+
import { ResourceManagerContext, ResourceManagerContextV1 } from "../types";
88

99
export class ResourceManager {
1010
private context: ResourceManagerContext;
@@ -18,8 +18,12 @@ export class ResourceManager {
1818
this.engine = new EngineServiceV2(this.context);
1919
this.database = new DatabaseServiceV2(this.context);
2020
} else if (httpClient.authenticator.isUsernamePassword()) {
21-
this.engine = new EngineServiceV1(this.context);
22-
this.database = new DatabaseServiceV1(this.context);
21+
this.engine = new EngineServiceV1(
22+
this.context as ResourceManagerContextV1
23+
);
24+
this.database = new DatabaseServiceV1(
25+
this.context as ResourceManagerContextV1
26+
);
2327
} else {
2428
throw new Error(
2529
"Invalid auth credentials provided. Please check your credentials and try again."

0 commit comments

Comments
 (0)