Skip to content

Commit 27e855c

Browse files
authored
Fairness Keys & Weights (#1768)
1 parent f9c94a5 commit 27e855c

File tree

8 files changed

+370
-265
lines changed

8 files changed

+370
-265
lines changed

packages/common/src/priority.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import type { temporal } from '@temporalio/proto';
1111
* calling workflow, then use the default (documented on the field).
1212
* The overall semantics of Priority are:
1313
* 1. First, consider "priority_key": lower number goes first.
14-
* (more will be added here later)
14+
* 2. Then, consider fairness: the fairness mechanism attempts to dispatch tasks for a given key in
15+
* proportion to its weight.
1516
*/
1617
export interface Priority {
1718
/**
@@ -26,13 +27,43 @@ export interface Priority {
2627
* The default priority is (min+max)/2. With the default max of 5 and min of 1, that comes out to 3.
2728
*/
2829
priorityKey?: number;
30+
31+
/**
32+
* FairnessKey is a short string that's used as a key for a fairness
33+
* balancing mechanism. It may correspond to a tenant id, or to a fixed
34+
* string like "high" or "low". The default is the empty string.
35+
*
36+
* The fairness mechanism attempts to dispatch tasks for a given key in
37+
* proportion to its weight. For example, using a thousand distinct tenant
38+
* ids, each with a weight of 1.0 (the default) will result in each tenant
39+
* getting a roughly equal share of task dispatch throughput.
40+
*
41+
* Fairness keys are limited to 64 bytes.
42+
*/
43+
fairnessKey?: string;
44+
45+
/**
46+
* FairnessWeight for a task can come from multiple sources for
47+
* flexibility. From highest to lowest precedence:
48+
* 1. Weights for a small set of keys can be overridden in task queue
49+
* configuration with an API.
50+
* 2. It can be attached to the workflow/activity in this field.
51+
* 3. The default weight of 1.0 will be used.
52+
*
53+
* Weight values are clamped to the range [0.001, 1000].
54+
*/
55+
fairnessWeight?: number;
2956
}
3057

3158
/**
3259
* Turn a proto compatible Priority into a TS Priority
3360
*/
3461
export function decodePriority(priority?: temporal.api.common.v1.IPriority | null): Priority {
35-
return { priorityKey: priority?.priorityKey ?? undefined };
62+
return {
63+
priorityKey: priority?.priorityKey ?? undefined,
64+
fairnessKey: priority?.fairnessKey ?? undefined,
65+
fairnessWeight: priority?.fairnessWeight ?? undefined,
66+
};
3667
}
3768

3869
/**
@@ -50,5 +81,7 @@ export function compilePriority(priority: Priority): temporal.api.common.v1.IPri
5081

5182
return {
5283
priorityKey: priority.priorityKey ?? 0,
84+
fairnessKey: priority.fairnessKey ?? '',
85+
fairnessWeight: priority.fairnessWeight ?? 0,
5386
};
5487
}

0 commit comments

Comments
 (0)