Skip to content

Commit 4543b1b

Browse files
frodr33Frank Rodriguez
andauthored
Add initial value functionality for session key (#75)
This commit introduces a new parameter "initValue" in ms.session.key.field that allows users to provide initial values for session keys. Co-authored-by: Frank Rodriguez <frrodrig@frrodrig-mn1.linkedin.biz>
1 parent 4a2d54c commit 4543b1b

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

cdi-core/src/main/java/com/linkedin/cdi/extractor/MultistageExtractor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Iterator;
3939
import java.util.List;
4040
import java.util.Map;
41+
import java.util.Objects;
4142
import java.util.regex.Matcher;
4243
import java.util.regex.Pattern;
4344
import javax.annotation.Nullable;
@@ -839,6 +840,7 @@ protected JsonObject getInitialWorkUnitParameters() {
839840
* Initial work unit variable values include
840841
* - watermarks defined for each work unit
841842
* - initial pagination defined at the source level
843+
* - initial session key values.
842844
*
843845
* @return work unit specific initial parameters for the first request to source
844846
*/
@@ -849,6 +851,14 @@ private JsonObject getInitialWorkUnitVariableValues() {
849851
for (Map.Entry<ParameterTypes, Long> entry : jobKeys.getPaginationInitValues().entrySet()) {
850852
variableValues.addProperty(entry.getKey().toString(), entry.getValue());
851853
}
854+
855+
JsonObject sessionKeyField = jobKeys.getSessionKeyField();
856+
if (Objects.nonNull(sessionKeyField)) {
857+
if (sessionKeyField.has("initValue")) {
858+
variableValues.addProperty(ParameterTypes.SESSION.toString(), sessionKeyField.get("initValue").toString());
859+
}
860+
}
861+
852862
return variableValues;
853863
}
854864

docs/parameters/ms.session.key.field.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ a session in backend by a status field, a session cursor, or through
2525
`ms.session.key.field` specifies the key field in response in order to retrieve the
2626
status for session control and the condition for termination.
2727

28-
This property takes the form a JsonObject with a **name**, a **condition**,
29-
and a **failCondition**.
28+
This property takes the form a JsonObject with a **name**, a **condition**, a **failCondition**,
29+
and a **initValue**.
3030

3131
it takes the form a Json object with a "name", "condition", and "failCondition".
3232
- **name** specifies the field in response that gives session info, it is required
3333
- **condition** specifies when the session should stop.
3434
- **failCondition** specifies when the session should fail.
35-
- "condition" and "failCondition" are optional
35+
- **initValue** specifies the initial value of the session key. It is a string value such as "MDAzMzIwMDAwMXlqN3ZEQUFR"
36+
- "condition", "failCondition", and "initValue" are optional
3637
- "condition" takes precedence over "failCondition"
3738

3839
The "name" element of ms.session.key.field is one of following:
@@ -95,6 +96,17 @@ URL parameters or request bodies if HttpSource is used.
9596
However, for the first HTTP request, because the
9697
session variable has a NULL value, it will not be used in the request.
9798

99+
The **initValue** parameter contains an optional initial session key value.
100+
When **initValue** is not provided and a `session` type variable is defined in
101+
[ms.parameters](ms.parameters.md), the default behavior is used in which on the
102+
initial request the session key is not included in the request URI or body, and on
103+
subsequent requests it is included. When an initial value is provided, this value
104+
will be passed to the `session` type variable on the initial run. This can be useful
105+
in certain scenarios:
106+
- To start cursor pagination from a specific page key when working with one work unit
107+
- to use cursor pagination on a POST request URI in which typically the variables defined in
108+
[ms.parameters](ms.parameters.md) are passed to the request body.
109+
98110
### Examples
99111

100112
In the following, the session key provides a stop condition:
@@ -118,4 +130,14 @@ streaming data.
118130

119131
- `ms.session.key.field={"name": "end_of_stream", "condition": {"regexp": "^true$"}}`
120132

133+
In the following, the session key is used as a cursor in the URI for a POST request. We can reference the session variable
134+
defined in [ms.parameters](ms.parameters.md) inside [ms.source.uri](ms.source.uri.md) such as `<URI>/?next_page={{next_page}}`.
135+
On the initial request, the cursor value must be present otherwise the next_page variable would not be substituted. So, an
136+
initial value is defined in **ms.session.key.field** using "initValue". In this example, the API accepts an empty value
137+
for cursor as the same thing as no value. "" is substituted in as the initial cursor. Subsequent requests for cursor pagination
138+
work as normal.
139+
- `ms.source.uri`=`https://<URI>/?next_page={{next_page}}'`
140+
- `ms.session.key.field={"name": "next_page", "condition": {"regexp": ""}, "initValue": ""}`
141+
- `ms.parameters=[{"name":"next_page", "type":"session"}]`
142+
121143
[back to summary](summary.md#mssessionkeyfield)

0 commit comments

Comments
 (0)