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

fix(worker): digest by key #7569

Merged
merged 1 commit into from
Jan 23, 2025
Merged
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert the removal of digest key in Dashboard.

Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export const DigestControlValues = () => {
return null;
}

const { ['amount']: amount, ['unit']: unit, ['cron']: cron } = uiSchema.properties ?? {};
const { ['amount']: amount, ['digestKey']: digestKey, ['unit']: unit, ['cron']: cron } = uiSchema.properties ?? {};

return (
<div className="flex flex-col">
{/* {digestKey && (
{digestKey && (
<>
<SidebarContent size="lg">
{getComponentByType({
Expand All @@ -25,7 +25,7 @@ export const DigestControlValues = () => {
</SidebarContent>
<Separator />
</>
)} */}
)}
{((amount && unit) || cron) && (
<>
<SidebarContent size="lg">
Expand Down
14 changes: 8 additions & 6 deletions apps/worker/src/app/workflow/usecases/add-job/add-job.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,14 @@ export class AddJob {
private async updateMetadata(response: ExecuteOutput, command: AddJobCommand) {
let metadata = {} as IWorkflowStepMetadata;
const outputs = response.outputs as DigestOutput;
// digest value is pre-computed by framework and passed as digestKey
const outputDigestValue = outputs?.digestKey;
const digestType = getDigestType(outputs);

if (isTimedDigestOutput(outputs)) {
metadata = {
type: DigestTypeEnum.TIMED,
digestKey: outputs?.digestKey,
digestValue: outputDigestValue,
timed: { cronExpression: outputs?.cron },
} as IDigestTimedMetadata;

Expand All @@ -278,7 +280,7 @@ export class AddJob {
{
$set: {
'digest.type': metadata.type,
'digest.digestKey': metadata.digestKey,
'digest.digestValue': metadata.digestValue,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any backwards compatability concerns?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm, saw it later in the query builder

Copy link
Contributor Author

@djabarovgeorge djabarovgeorge Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this part is about the framework digest key, which was not working before.
and in the query builder we created a clear separation of the 2 versions, so it would be clear there as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm shocked 🤯

'digest.amount': metadata.amount,
'digest.unit': metadata.unit,
'digest.timed.cronExpression': metadata.timed?.cronExpression,
Expand All @@ -291,7 +293,7 @@ export class AddJob {
metadata = {
type: digestType,
amount: outputs?.amount,
digestKey: outputs?.digestKey,
digestValue: outputDigestValue,
unit: outputs.unit ? castUnitToDigestUnitEnum(outputs?.unit) : undefined,
backoff: digestType === DigestTypeEnum.BACKOFF,
backoffAmount: outputs.lookBackWindow?.amount,
Expand All @@ -306,7 +308,7 @@ export class AddJob {
{
$set: {
'digest.type': metadata.type,
'digest.digestKey': metadata.digestKey,
'digest.digestValue': metadata.digestValue,
'digest.amount': metadata.amount,
'digest.unit': metadata.unit,
'digest.backoff': metadata.backoff,
Expand All @@ -321,7 +323,7 @@ export class AddJob {
metadata = {
type: digestType,
amount: outputs?.amount,
digestKey: outputs?.digestKey,
digestValue: outputDigestValue,
unit: outputs.unit ? castUnitToDigestUnitEnum(outputs?.unit) : undefined,
} as IDigestRegularMetadata;

Expand All @@ -333,7 +335,7 @@ export class AddJob {
{
$set: {
'digest.type': metadata.type,
'digest.digestKey': metadata.digestKey,
'digest.digestValue': metadata.digestValue,
'digest.amount': metadata.amount,
'digest.unit': metadata.unit,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class MergeOrCreateDigest {

const digestMeta = job.digest as IDigestBaseMetadata;
const digestKey = digestMeta?.digestKey;
const digestValue = getNestedValue(job.payload, digestKey);
const digestValue = digestMeta?.digestValue ?? getNestedValue(job.payload, digestKey);

const digestAction = command.filtered
? { digestResult: DigestCreationResultEnum.SKIPPED }
Expand Down Expand Up @@ -150,9 +150,13 @@ export class MergeOrCreateDigest {
}

private getLockKey(job: JobEntity, digestKey: string | undefined, digestValue: string | number | undefined): string {
let resource = `environment:${job._environmentId}:template:${job._templateId}:subscriber:${job._subscriberId}`;
const resource = `environment:${job._environmentId}:template:${job._templateId}:subscriber:${job._subscriberId}`;
if (digestKey && digestValue) {
resource = `${resource}:digestKey:${digestKey}:digestValue:${digestValue}`;
return `${resource}:digestKey:${digestKey}:digestValue:${digestValue}`;
}

if (digestValue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the scenario where we have a key but not a digestValue? Is the key only the fixed subscriberId?

Copy link
Contributor Author

@djabarovgeorge djabarovgeorge Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry i missed this one before merging.
in v2 in the end there is no digestKey only parsed key which is the digestValue, so if digestValue is missing we will use dynamic subscriberId

return `${resource}:digestValue:${digestValue}`;
}

return resource;
Expand Down
23 changes: 14 additions & 9 deletions libs/dal/src/repositories/job/job.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ export class JobRepository extends BaseRepository<JobDBModel, JobEntity, Enforce
job.digest?.type === DigestTypeEnum.BACKOFF ||
(job.digest as IDigestRegularMetadata)?.backoff ||
(digestMeta?.backoff && digestMeta?.backoff);
const digestQuery = this.buildDigestQuery(digestKey, digestValue);

if (isBackoff) {
const trigger = await this.getTrigger(job, digestMeta, digestKey, digestValue);
const trigger = await this.getTriggerJob(job, digestMeta, digestQuery);
if (!trigger) {
return {
digestResult: DigestCreationResultEnum.SKIPPED,
Expand Down Expand Up @@ -219,7 +220,7 @@ export class JobRepository extends BaseRepository<JobDBModel, JobEntity, Enforce
_templateId: job._templateId,
_environmentId: this.convertStringToObjectId(job._environmentId),
_subscriberId: this.convertStringToObjectId(job._subscriberId),
...(digestKey && { [`payload.${digestKey}`]: digestValue }),
...digestQuery,
},
'_id _notificationId'
);
Expand Down Expand Up @@ -252,18 +253,22 @@ export class JobRepository extends BaseRepository<JobDBModel, JobEntity, Enforce
};
}

private buildDigestQuery(digestKey: string | undefined, digestValue: string | number | undefined) {
const digestQueryV1 = digestKey ? { [`payload.${digestKey}`]: digestValue } : null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In v1, we handled digest aggregation by:

  1. Extracting a value from the job payload using a specified digest key
  2. Checking if another job already existed with the same digest key-value pair
  3. If a match was found, we would merge the new job with the existing one, otherwise create new one.

// Digest key parsing is handled by the framework, leaving only the digest value available here
const digestQueryV2 = !digestKey && digestValue ? { [`digest.digestValue`]: digestValue } : null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now we store the final digest value directly. We don't track how this value was generated - we simply check if this exact digest value already exists in our system.

const digestQuery = digestQueryV1 || digestQueryV2;

return digestQuery || {};
}

private getBackoffDate(metadata: IDigestRegularMetadata | undefined) {
return sub(new Date(), {
[metadata?.backoffUnit as string]: metadata?.backoffAmount,
});
}

private getTrigger(
job: JobEntity,
metadata?: IDigestRegularMetadata,
digestKey?: string,
digestValue?: string | number
) {
private getTriggerJob(job: JobEntity, metadata?: IDigestRegularMetadata, digestQuery?: Record<string, unknown>) {
const query = {
updatedAt: {
$gte: this.getBackoffDate(metadata),
Expand All @@ -276,7 +281,7 @@ export class JobRepository extends BaseRepository<JobDBModel, JobEntity, Enforce
type: StepTypeEnum.TRIGGER,
_environmentId: job._environmentId,
_subscriberId: job._subscriberId,
...(digestKey && { [`payload.${digestKey}`]: digestValue }),
...digestQuery,
};

return this.findOne(query);
Expand Down
3 changes: 3 additions & 0 deletions libs/dal/src/repositories/job/job.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ const jobSchema = new Schema<JobDBModel>(
digestKey: {
type: Schema.Types.String,
},
digestValue: {
type: Schema.Types.String,
},
type: {
type: Schema.Types.String,
},
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/entities/step/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export interface IAmountAndUnitDigest {

export interface IDigestBaseMetadata extends IAmountAndUnitDigest {
digestKey?: string;
digestValue?: string;
}

export interface IDigestRegularMetadata extends IDigestBaseMetadata {
Expand Down
Loading