2023-07-31 02:37:09 -07:00
|
|
|
import { Service } from 'typedi';
|
perf(core): Improve caching service (#8213)
Story: https://linear.app/n8n/issue/PAY-1188
- Implement Redis hashes on the caching service, based on Micha's work
in #7747, adapted from `node-cache-manager-ioredis-yet`. Optimize
workflow ownership lookups and manual webhook lookups with Redis hashes.
- Simplify the caching service by removing all currently unused methods
and options: `enable`, `disable`, `getCache`, `keys`, `keyValues`,
`refreshFunctionEach`, `refreshFunctionMany`, `refreshTtl`, etc.
- Remove the flag `N8N_CACHE_ENABLED`. Currently some features on
`master` are broken with caching disabled, and test webhooks now rely
entirely on caching, for multi-main setup support. We originally
introduced this flag to protect against excessive memory usage, but
total cache usage is low enough that we decided to drop this setting.
Apparently this flag was also never documented.
- Overall caching service refactor: use generics, reduce branching, add
discriminants for cache kinds for better type safety, type caching
events, improve readability, remove outdated docs, etc. Also refactor
and expand caching service tests.
Follow-up to: https://github.com/n8n-io/n8n/pull/8176
---------
Co-authored-by: Michael Auerswald <michael.auerswald@gmail.com>
2024-01-05 02:52:44 -08:00
|
|
|
import { CacheService } from '@/services/cache/cache.service';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository';
|
|
|
|
import type { User } from '@db/entities/User';
|
2023-08-02 23:58:36 -07:00
|
|
|
import { RoleService } from './role.service';
|
2023-12-11 04:35:09 -08:00
|
|
|
import { UserRepository } from '@/databases/repositories/user.repository';
|
2023-12-05 01:11:18 -08:00
|
|
|
import type { ListQuery } from '@/requests';
|
2023-11-29 03:25:10 -08:00
|
|
|
import { ApplicationError } from 'n8n-workflow';
|
2023-07-31 02:37:09 -07:00
|
|
|
|
|
|
|
@Service()
|
|
|
|
export class OwnershipService {
|
|
|
|
constructor(
|
|
|
|
private cacheService: CacheService,
|
2023-12-11 04:35:09 -08:00
|
|
|
private userRepository: UserRepository,
|
2023-08-02 23:58:36 -07:00
|
|
|
private roleService: RoleService,
|
2023-07-31 02:37:09 -07:00
|
|
|
private sharedWorkflowRepository: SharedWorkflowRepository,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the user who owns the workflow. Note that workflow ownership is **immutable**.
|
|
|
|
*/
|
|
|
|
async getWorkflowOwnerCached(workflowId: string) {
|
perf(core): Improve caching service (#8213)
Story: https://linear.app/n8n/issue/PAY-1188
- Implement Redis hashes on the caching service, based on Micha's work
in #7747, adapted from `node-cache-manager-ioredis-yet`. Optimize
workflow ownership lookups and manual webhook lookups with Redis hashes.
- Simplify the caching service by removing all currently unused methods
and options: `enable`, `disable`, `getCache`, `keys`, `keyValues`,
`refreshFunctionEach`, `refreshFunctionMany`, `refreshTtl`, etc.
- Remove the flag `N8N_CACHE_ENABLED`. Currently some features on
`master` are broken with caching disabled, and test webhooks now rely
entirely on caching, for multi-main setup support. We originally
introduced this flag to protect against excessive memory usage, but
total cache usage is low enough that we decided to drop this setting.
Apparently this flag was also never documented.
- Overall caching service refactor: use generics, reduce branching, add
discriminants for cache kinds for better type safety, type caching
events, improve readability, remove outdated docs, etc. Also refactor
and expand caching service tests.
Follow-up to: https://github.com/n8n-io/n8n/pull/8176
---------
Co-authored-by: Michael Auerswald <michael.auerswald@gmail.com>
2024-01-05 02:52:44 -08:00
|
|
|
const cachedValue = await this.cacheService.getHashValue<User>(
|
|
|
|
'workflow-ownership',
|
|
|
|
workflowId,
|
|
|
|
);
|
2023-07-31 02:37:09 -07:00
|
|
|
|
2023-12-11 04:35:09 -08:00
|
|
|
if (cachedValue) return this.userRepository.create(cachedValue);
|
2023-07-31 02:37:09 -07:00
|
|
|
|
2023-08-02 23:58:36 -07:00
|
|
|
const workflowOwnerRole = await this.roleService.findWorkflowOwnerRole();
|
2023-07-31 02:37:09 -07:00
|
|
|
|
2023-11-29 03:25:10 -08:00
|
|
|
if (!workflowOwnerRole) throw new ApplicationError('Failed to find workflow owner role');
|
2023-07-31 02:37:09 -07:00
|
|
|
|
|
|
|
const sharedWorkflow = await this.sharedWorkflowRepository.findOneOrFail({
|
|
|
|
where: { workflowId, roleId: workflowOwnerRole.id },
|
|
|
|
relations: ['user', 'user.globalRole'],
|
|
|
|
});
|
|
|
|
|
perf(core): Improve caching service (#8213)
Story: https://linear.app/n8n/issue/PAY-1188
- Implement Redis hashes on the caching service, based on Micha's work
in #7747, adapted from `node-cache-manager-ioredis-yet`. Optimize
workflow ownership lookups and manual webhook lookups with Redis hashes.
- Simplify the caching service by removing all currently unused methods
and options: `enable`, `disable`, `getCache`, `keys`, `keyValues`,
`refreshFunctionEach`, `refreshFunctionMany`, `refreshTtl`, etc.
- Remove the flag `N8N_CACHE_ENABLED`. Currently some features on
`master` are broken with caching disabled, and test webhooks now rely
entirely on caching, for multi-main setup support. We originally
introduced this flag to protect against excessive memory usage, but
total cache usage is low enough that we decided to drop this setting.
Apparently this flag was also never documented.
- Overall caching service refactor: use generics, reduce branching, add
discriminants for cache kinds for better type safety, type caching
events, improve readability, remove outdated docs, etc. Also refactor
and expand caching service tests.
Follow-up to: https://github.com/n8n-io/n8n/pull/8176
---------
Co-authored-by: Michael Auerswald <michael.auerswald@gmail.com>
2024-01-05 02:52:44 -08:00
|
|
|
void this.cacheService.setHash('workflow-ownership', { [workflowId]: sharedWorkflow.user });
|
2023-07-31 02:37:09 -07:00
|
|
|
|
|
|
|
return sharedWorkflow.user;
|
|
|
|
}
|
2023-08-22 04:19:37 -07:00
|
|
|
|
2023-12-05 01:11:18 -08:00
|
|
|
addOwnedByAndSharedWith(
|
|
|
|
rawWorkflow: ListQuery.Workflow.WithSharing,
|
|
|
|
): ListQuery.Workflow.WithOwnedByAndSharedWith;
|
|
|
|
addOwnedByAndSharedWith(
|
|
|
|
rawCredential: ListQuery.Credentials.WithSharing,
|
|
|
|
): ListQuery.Credentials.WithOwnedByAndSharedWith;
|
|
|
|
addOwnedByAndSharedWith(
|
|
|
|
rawEntity: ListQuery.Workflow.WithSharing | ListQuery.Credentials.WithSharing,
|
|
|
|
): ListQuery.Workflow.WithOwnedByAndSharedWith | ListQuery.Credentials.WithOwnedByAndSharedWith {
|
|
|
|
const { shared, ...rest } = rawEntity;
|
2023-08-22 04:19:37 -07:00
|
|
|
|
2023-12-05 01:11:18 -08:00
|
|
|
const entity = rest as
|
|
|
|
| ListQuery.Workflow.WithOwnedByAndSharedWith
|
|
|
|
| ListQuery.Credentials.WithOwnedByAndSharedWith;
|
2023-08-22 04:19:37 -07:00
|
|
|
|
2023-12-05 01:11:18 -08:00
|
|
|
Object.assign(entity, { ownedBy: null, sharedWith: [] });
|
2023-09-04 01:37:16 -07:00
|
|
|
|
|
|
|
shared?.forEach(({ user, role }) => {
|
|
|
|
const { id, email, firstName, lastName } = user;
|
|
|
|
|
|
|
|
if (role.name === 'owner') {
|
2023-12-05 01:11:18 -08:00
|
|
|
entity.ownedBy = { id, email, firstName, lastName };
|
2023-09-04 01:37:16 -07:00
|
|
|
} else {
|
2023-12-05 01:11:18 -08:00
|
|
|
entity.sharedWith.push({ id, email, firstName, lastName });
|
2023-09-04 01:37:16 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-05 01:11:18 -08:00
|
|
|
return entity;
|
2023-09-04 01:37:16 -07:00
|
|
|
}
|
2023-12-11 04:35:09 -08:00
|
|
|
|
|
|
|
async getInstanceOwner() {
|
|
|
|
const globalOwnerRole = await this.roleService.findGlobalOwnerRole();
|
|
|
|
|
2024-01-17 07:08:50 -08:00
|
|
|
return await this.userRepository.findOneOrFail({
|
2023-12-11 04:35:09 -08:00
|
|
|
where: { globalRoleId: globalOwnerRole.id },
|
|
|
|
relations: ['globalRole'],
|
|
|
|
});
|
|
|
|
}
|
2023-07-31 02:37:09 -07:00
|
|
|
}
|