-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transform): Add Meta KV Store Lock Transform (#177)
* feat(kv): Add Locker Interface * feat(aws): Add DynamoDB DeleteItem, PutItem Methods * feat(transform): Add Meta KV Store Lock * docs(examples): Add Exactly Once Examples * docs(transform): Update Locker Comment * fix(dynamodb): KV Store Lock Errors * fix(transform): KV Store Locked Keys * docs(examples): Add DynamoDB Distributed Lock Pattern * docs(examples): Formatting
- Loading branch information
Showing
20 changed files
with
654 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
examples/config/transform/meta/exactly_once_consumer/config.jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// This example shows how to use the `meta_kv_store_lock` transform to | ||
// create an "exactly once" semantic for a pipeline consumer. | ||
local sub = import '../../../../../build/config/substation.libsonnet'; | ||
|
||
// In production environments a distributed KV store should be used. | ||
local kv = sub.kv_store.memory(); | ||
|
||
{ | ||
transforms: [ | ||
// If a message acquires a lock, then it is tagged for inspection. | ||
sub.tf.meta.kv_store.lock(settings={ | ||
kv_store: kv, | ||
prefix: 'eo_consumer', | ||
ttl_offset: '1m', | ||
transform: sub.tf.obj.insert({ object: { target_key: 'meta eo_consumer' }, value: 'locked' }), | ||
}), | ||
// Messages that are not locked are dropped from the pipeline. | ||
sub.tf.meta.switch({ cases: [ | ||
{ | ||
condition: sub.cnd.none([ | ||
sub.cnd.str.eq({ object: { source_key: 'meta eo_consumer' }, value: 'locked' }), | ||
]), | ||
transform: sub.tf.utility.drop(), | ||
}, | ||
] }), | ||
// At this point only locked messages exist in the pipeline. | ||
sub.tf.send.stdout(), | ||
], | ||
} |
8 changes: 8 additions & 0 deletions
8
examples/config/transform/meta/exactly_once_consumer/data.jsonl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{"a":"b"} | ||
{"a":"b"} | ||
{"c":"d"} | ||
{"a":"b"} | ||
{"c":"d"} | ||
{"c":"d"} | ||
{"e":"f"} | ||
{"a":"b"} |
21 changes: 21 additions & 0 deletions
21
examples/config/transform/meta/exactly_once_producer/config.jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// This example shows how to use the `meta_kv_store_lock` transform to | ||
// create an "exactly once" semantic for a pipeline producer. | ||
local sub = import '../../../../../build/config/substation.libsonnet'; | ||
|
||
// In production environments a distributed KV store should be used. | ||
local kv = sub.kv_store.memory(); | ||
|
||
{ | ||
transforms: [ | ||
// This only prints messages that acquire a lock. Any message | ||
// that fails to acquire a lock will be skipped. An error in the | ||
// sub-transform will cause all previously locked messages to be | ||
// unlocked. | ||
sub.tf.meta.err({ transform: sub.tf.meta.kv_store.lock(settings={ | ||
kv_store: kv, | ||
prefix: 'eo_producer', | ||
ttl_offset: '1m', | ||
transform: sub.tf.send.stdout(), | ||
}) }), | ||
], | ||
} |
8 changes: 8 additions & 0 deletions
8
examples/config/transform/meta/exactly_once_producer/data.jsonl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{"a":"b"} | ||
{"a":"b"} | ||
{"c":"d"} | ||
{"a":"b"} | ||
{"c":"d"} | ||
{"c":"d"} | ||
{"e":"f"} | ||
{"a":"b"} |
24 changes: 24 additions & 0 deletions
24
examples/config/transform/meta/exactly_once_system/config.jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// This example shows how to use the `meta_kv_store_lock` transform to | ||
// create an "exactly once" semantic for an entire pipeline system. | ||
local sub = import '../../../../../build/config/substation.libsonnet'; | ||
|
||
// In production environments a distributed KV store should be used. | ||
local kv = sub.kv_store.memory(); | ||
|
||
{ | ||
transforms: [ | ||
// All messages are locked before being sent through other transform | ||
// functions, ensuring that the message is processed only once. | ||
// An error in any sub-transform will cause all previously locked | ||
// messages to be unlocked. | ||
sub.tf.meta.err({ transform: sub.tf.meta.kv_store.lock(settings={ | ||
kv_store: kv, | ||
prefix: 'eo_system', | ||
ttl_offset: '1m', | ||
transform: sub.tf.meta.pipeline({ transforms: [ | ||
sub.tf.obj.insert({ object: { target_key: 'processed' }, value: true }), | ||
sub.tf.send.stdout(), | ||
] }), | ||
}) }), | ||
], | ||
} |
8 changes: 8 additions & 0 deletions
8
examples/config/transform/meta/exactly_once_system/data.jsonl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{"a":"b"} | ||
{"a":"b"} | ||
{"c":"d"} | ||
{"a":"b"} | ||
{"c":"d"} | ||
{"c":"d"} | ||
{"e":"f"} | ||
{"a":"b"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
examples/terraform/aws/dynamodb/distributed_lock/config/node/config.jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
local sub = import '../../../../../../../build/config/substation.libsonnet'; | ||
|
||
local kv = sub.kv_store.aws_dynamodb({ | ||
table_name: 'substation', | ||
attributes: { partition_key: 'PK', ttl: 'ttl' }, | ||
}); | ||
|
||
{ | ||
transforms: [ | ||
// All messages are locked before they are sent through other | ||
// transform functions, ensuring that the message is processed | ||
// exactly once. | ||
// | ||
// An error in any sub-transform will cause all previously locked | ||
// messages to be unlocked; this only applies to messages that have | ||
// not yet been flushed by a control message. Use the `utility_control` | ||
// transform to manage how often messages are flushed. | ||
sub.tf.meta.kv_store.lock(settings={ | ||
kv_store: kv, | ||
prefix: 'distributed_lock', | ||
ttl_offset: '1m', | ||
transform: sub.tf.meta.pipeline({ transforms: [ | ||
// Delaying and simulating an error makes it possible to | ||
// test message unlocking in real-time (view changes using | ||
// the DynamoDB console). Uncomment the lines below to see | ||
// how it works. | ||
// | ||
// sub.tf.utility.delay({ duration: '10s' }), | ||
// sub.pattern.transform.conditional( | ||
// condition=sub.cnd.utility.random(), | ||
// transform=sub.tf.utility.err({ message: 'simulating error to trigger unlock' }), | ||
// ), | ||
// | ||
// Messages are printed to the console. After this, they are locked | ||
// and will not be printed again until the lock expires. | ||
sub.tf.send.stdout(), | ||
] }), | ||
}), | ||
], | ||
} |
40 changes: 40 additions & 0 deletions
40
examples/terraform/aws/dynamodb/distributed_lock/terraform/_resources.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
data "aws_caller_identity" "caller" {} | ||
|
||
module "appconfig" { | ||
source = "../../../../../../build/terraform/aws/appconfig" | ||
|
||
config = { | ||
name = "substation" | ||
environments = [{ name = "example" }] | ||
} | ||
} | ||
|
||
module "ecr" { | ||
source = "../../../../../../build/terraform/aws/ecr" | ||
|
||
config = { | ||
name = "substation" | ||
force_delete = true | ||
} | ||
} | ||
|
||
module "dynamodb" { | ||
source = "../../../../../../build/terraform/aws/dynamodb" | ||
|
||
config = { | ||
name = "substation" | ||
hash_key = "PK" | ||
ttl = "ttl" | ||
|
||
attributes = [ | ||
{ | ||
name = "PK" | ||
type = "S" | ||
} | ||
] | ||
} | ||
|
||
access = [ | ||
module.node.role.name, | ||
] | ||
} |
26 changes: 26 additions & 0 deletions
26
examples/terraform/aws/dynamodb/distributed_lock/terraform/node.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module "node" { | ||
source = "../../../../../../build/terraform/aws/lambda" | ||
appconfig = module.appconfig | ||
|
||
config = { | ||
name = "node" | ||
description = "Substation node that transforms data exactly-once using a distributed lock" | ||
image_uri = "${module.ecr.url}:v1.3.0" | ||
image_arm = true | ||
env = { | ||
"SUBSTATION_CONFIG" : "http://localhost:2772/applications/substation/environments/example/configurations/node" | ||
"SUBSTATION_LAMBDA_HANDLER" : "AWS_API_GATEWAY" | ||
"SUBSTATION_DEBUG" : true | ||
} | ||
} | ||
|
||
depends_on = [ | ||
module.appconfig.name, | ||
module.ecr.url, | ||
] | ||
} | ||
|
||
resource "aws_lambda_function_url" "node" { | ||
function_name = module.node.name | ||
authorization_type = "NONE" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.