Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CB-5839 fix: resource blocking #3199

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion webapp/packages/core-connections/src/ConnectionInfoResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { action, makeObservable, observable, runInAction, toJS } from 'mobx';
import { AppAuthService, UserInfoResource } from '@cloudbeaver/core-authentication';
import { injectable } from '@cloudbeaver/core-di';
import { ExecutorInterrupter, type ISyncExecutor, SyncExecutor } from '@cloudbeaver/core-executor';
import { NodeManagerUtils } from '@cloudbeaver/core-navigation-tree';
import { ProjectInfoResource, ProjectsService } from '@cloudbeaver/core-projects';
import {
CachedMapAllKey,
Expand Down Expand Up @@ -291,9 +292,18 @@ export class ConnectionInfoResource extends CachedMapResource<IConnectionInfoPar
return this.get(key).every(connection => connection?.connected ?? false);
}

// TODO: we need here node path ie ['', 'project://', 'database://...', '...']
getConnectionIdForNodeId(projectId: string, nodeId: string): IConnectionInfoParams | undefined {
if (!NodeManagerUtils.isDatabaseObject(nodeId)) {
return;
}

return createConnectionParam(projectId, NodeManagerUtils.getConnectionId(nodeId));
}

// TODO: we need here node path ie ['', 'project://', 'database://...', '...']
getConnectionForNode(nodeId: string): Connection | undefined {
if (!nodeId.startsWith('database://')) {
if (!NodeManagerUtils.isDatabaseObject(nodeId)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ export class ConnectionsManagerService {
return connection.connection;
}

addOpenedConnection(connection: Connection): void {
this.connectionInfo.add(connection);
}

getObjectContainerById(connectionKey: IConnectionInfoParams, objectCatalogId?: string, objectSchemaId?: string): ObjectContainer | undefined {
if (objectCatalogId) {
const objectContainers = this.containerContainers.getCatalogData(connectionKey, objectCatalogId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export class DBObjectResource extends CachedMapResource<string, DBObject> {
});

this.beforeLoad.addHandler(async (originalKey, context) => {
await this.navTreeResource.waitLoad();
const parentKey = this.aliases.isAlias(originalKey, DBObjectParentKey);
const pageKey =
this.aliases.isAlias(originalKey, CachedResourceOffsetPageKey) || this.aliases.isAlias(originalKey, CachedResourceOffsetPageListKey);
Expand All @@ -80,6 +79,15 @@ export class DBObjectResource extends CachedMapResource<string, DBObject> {
}

const key = ResourceKeyUtils.toList(this.aliases.transformToKey(originalKey));

for (const nodeId of key) {
const preloaded = await this.navTreeResource.preloadParents(nodeId);

if (!preloaded) {
throw new DetailsError('Not found: ' + nodeId);
}
}

const parents = [
...new Set(
key.map(nodeId => this.navNodeInfoResource.get(nodeId)?.parentId).filter<string>((nodeId): nodeId is string => nodeId !== undefined),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,17 +455,22 @@ export class NavTreeResource extends CachedMapResource<string, string[], Record<
this.navNodeInfoResource.delete(items.exclude(key));
}

async preloadParents(nodeId: string): Promise<boolean> {
if (!this.navNodeInfoResource.has(nodeId) && nodeId !== ROOT_NODE_PATH) {
await this.navNodeInfoResource.loadNodeParents(nodeId);
}
const parents = this.navNodeInfoResource.getParents(nodeId);
return await this.preloadNodeParents(parents, nodeId);
}

protected override async preLoadData(key: ResourceKey<string>, contexts: IExecutionContext<ResourceKey<string>>): Promise<void> {
await ResourceKeyUtils.forEachAsync(key, async nodeId => {
if (isResourceAlias(nodeId)) {
return;
}

if (!this.navNodeInfoResource.has(nodeId) && nodeId !== ROOT_NODE_PATH) {
await this.navNodeInfoResource.loadNodeParents(nodeId);
}
const preloaded = await this.preloadParents(nodeId);
const parents = this.navNodeInfoResource.getParents(nodeId);
const preloaded = await this.preloadNodeParents(parents, nodeId);

if (!preloaded) {
const cause = new DetailsError(`Entity not found:\n"${nodeId}"\nPath:\n${parents.map(parent => `"${parent}"`).join('\n')}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@
* you may not use this file except in compliance with the License.
*/

const CONNECTION_NODE_ID_PREFIX = 'database://';

export const NodeManagerUtils = {
getConnectionId(nodeId: string) {
const indexOfConnectionPart = nodeId.indexOf('/', CONNECTION_NODE_ID_PREFIX.length);
const connectionId = nodeId.slice(CONNECTION_NODE_ID_PREFIX.length, indexOfConnectionPart > -1 ? indexOfConnectionPart : nodeId.length);

return connectionId;
},

connectionIdToConnectionNodeId(connectionId: string): string {
return `database://${connectionId}`;
return `${CONNECTION_NODE_ID_PREFIX}${connectionId}`;
},

isDatabaseObject(objectId: string): boolean {
return objectId.startsWith('database://');
isDatabaseObject(nodeId: string): boolean {
return nodeId.startsWith(CONNECTION_NODE_ID_PREFIX);
},

concatSchemaAndCatalog(catalogId?: string, schemaId?: string): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ import {
useResource,
useUserData,
} from '@cloudbeaver/core-blocks';
import { ConnectionInfoActiveProjectKey, ConnectionInfoResource } from '@cloudbeaver/core-connections';
import { useService } from '@cloudbeaver/core-di';
import { NotificationService } from '@cloudbeaver/core-events';
import { ExecutorInterrupter, type ISyncExecutor, SyncExecutor } from '@cloudbeaver/core-executor';
import { type NavNode, NavNodeInfoResource, NavTreeResource, ROOT_NODE_PATH } from '@cloudbeaver/core-navigation-tree';
import { type NavNode, NavNodeInfoResource, NavTreeResource } from '@cloudbeaver/core-navigation-tree';
import { ProjectInfoResource, ProjectsService } from '@cloudbeaver/core-projects';
import {
CachedMapAllKey,
Expand Down Expand Up @@ -147,11 +146,9 @@ export interface IElementsTree extends ILoadableState {

export function useElementsTree(options: IOptions): IElementsTree {
const projectsService = useService(ProjectsService);
const projectInfoResource = useService(ProjectInfoResource);
const notificationService = useService(NotificationService);
const navNodeInfoResource = useService(NavNodeInfoResource);
const navTreeResource = useService(NavTreeResource);
const connectionInfoResource = useService(ConnectionInfoResource);
const elementsTreeService = useService(ElementsTreeService);

const [localTreeNodesState] = useState(
Expand Down Expand Up @@ -189,8 +186,6 @@ export function useElementsTree(options: IOptions): IElementsTree {
const functionsRef = useObjectRef({
async loadTree(...nodes: string[]) {
await Promise.all(loadingNodes.values());
await projectInfoResource.load();
await connectionInfoResource.load(ConnectionInfoActiveProjectKey);
const preloadedRoot = await elementsTree.loadPath(options.folderExplorer.state.fullPath);

if (preloadedRoot !== options.folderExplorer.state.folder) {
Expand Down Expand Up @@ -218,11 +213,6 @@ export function useElementsTree(options: IOptions): IElementsTree {
},

async loadNode(nodeId: string) {
await projectInfoResource.waitLoad();
await connectionInfoResource.waitLoad();
await navTreeResource.waitLoad();
await navNodeInfoResource.waitLoad();

const expanded = elementsTree.isNodeExpanded(nodeId, true);
if (!expanded && nodeId !== options.root) {
if (navNodeInfoResource.isOutdated(nodeId)) {
Expand Down Expand Up @@ -792,11 +782,6 @@ export function useElementsTree(options: IOptions): IElementsTree {
],
});

useExecutor({
executor: projectInfoResource.onDataOutdated,
handlers: [() => navTreeResource.markOutdated(ROOT_NODE_PATH)],
});

useExecutor({
executor: navTreeResource.onItemUpdate,
handlers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import { action, makeObservable } from 'mobx';

import {
type Connection,
ConnectionInfoResource,
ConnectionsManagerService,
createConnectionParam,
type IConnectionInfoParams,
NavNodeExtensionsService,
} from '@cloudbeaver/core-connections';
Expand Down Expand Up @@ -78,10 +78,16 @@ export class NavigationTreeService extends View<string> {

async loadNestedNodes(id = ROOT_NODE_PATH, tryConnect?: boolean): Promise<boolean> {
if (this.isConnectionNode(id)) {
let connection = this.connectionInfoResource.getConnectionForNode(id);
const node = this.navNodeInfoResource.get(id);

if (connection) {
connection = await this.connectionInfoResource.load(createConnectionParam(connection));
if (!node?.projectId) {
return false;
}

const connectionParam = this.connectionInfoResource.getConnectionIdForNodeId(node.projectId, id);
let connection: Connection | undefined;
if (connectionParam) {
connection = await this.connectionInfoResource.load(connectionParam);
} else {
return false;
}
Expand All @@ -91,22 +97,20 @@ export class NavigationTreeService extends View<string> {
return false;
}

const connected = await this.tryInitConnection(createConnectionParam(connection));
const connected = await this.tryInitConnection(connectionParam);
if (!connected) {
return false;
}
}
}

await this.navTreeResource.waitLoad();

if (tryConnect && this.navTreeResource.getException(id)) {
this.navTreeResource.markOutdated(id);
}

const parents = this.navNodeInfoResource.getParents(id);
const preloaded = await this.navTreeResource.preloadParents(id);

if (parents.length > 0 && !this.navNodeInfoResource.has(id)) {
if (!preloaded) {
return false;
}

Expand Down
Loading