From c71dbf45b1f91154bd9af84df49290955c106478 Mon Sep 17 00:00:00 2001 From: Lenny Chen Date: Mon, 23 Nov 2026 17:19:18 -0800 Subject: [PATCH 1/2] Add Ruby External Storage draft samples and round-trip tests --- .../data-handling/external-storage/.gitignore | 3 + .../data-handling/external-storage/Gemfile | 7 +++ .../data-handling/external-storage/README.md | 43 +++++++++++++ .../external-storage/configuration.rb | 49 +++++++++++++++ .../external-storage/integration_test.rb | 32 ++++++++++ .../large_payload_activity.rb | 9 +++ .../large_payload_workflow.rb | 10 ++++ .../external-storage/local_disk_driver.rb | 60 +++++++++++++++++++ .../data-handling/external-storage/run.rb | 60 +++++++++++++++++++ 9 files changed, 273 insertions(+) create mode 100644 ruby-sandbox/best-practices/data-handling/external-storage/.gitignore create mode 100644 ruby-sandbox/best-practices/data-handling/external-storage/Gemfile create mode 100644 ruby-sandbox/best-practices/data-handling/external-storage/README.md create mode 100644 ruby-sandbox/best-practices/data-handling/external-storage/configuration.rb create mode 100644 ruby-sandbox/best-practices/data-handling/external-storage/integration_test.rb create mode 100644 ruby-sandbox/best-practices/data-handling/external-storage/large_payload_activity.rb create mode 100644 ruby-sandbox/best-practices/data-handling/external-storage/large_payload_workflow.rb create mode 100644 ruby-sandbox/best-practices/data-handling/external-storage/local_disk_driver.rb create mode 100644 ruby-sandbox/best-practices/data-handling/external-storage/run.rb diff --git a/ruby-sandbox/best-practices/data-handling/external-storage/.gitignore b/ruby-sandbox/best-practices/data-handling/external-storage/.gitignore new file mode 100644 index 0000000..2c5642e --- /dev/null +++ b/ruby-sandbox/best-practices/data-handling/external-storage/.gitignore @@ -0,0 +1,3 @@ +payload-store/ +.bundle/ +vendor/ diff --git a/ruby-sandbox/best-practices/data-handling/external-storage/Gemfile b/ruby-sandbox/best-practices/data-handling/external-storage/Gemfile new file mode 100644 index 0000000..021f404 --- /dev/null +++ b/ruby-sandbox/best-practices/data-handling/external-storage/Gemfile @@ -0,0 +1,7 @@ +source 'https://rubygems.org' + +# Unreleased public API: replace this pin with a published version after SDK release. +gem 'temporalio', git: 'https://github.com/temporalio/sdk-ruby.git', + ref: 'b0f24ba3da8b58c58b50929095a94932c88e3177', glob: 'temporalio/*.gemspec', submodules: true +gem 'aws-sdk-s3' +gem 'minitest', '~> 5.25' diff --git a/ruby-sandbox/best-practices/data-handling/external-storage/README.md b/ruby-sandbox/best-practices/data-handling/external-storage/README.md new file mode 100644 index 0000000..d99a23f --- /dev/null +++ b/ruby-sandbox/best-practices/data-handling/external-storage/README.md @@ -0,0 +1,43 @@ +# Ruby External Storage (unreleased SDK draft) + +This sample targets the pending public API in temporalio/sdk-ruby PR #688, pinned +in this directory's Gemfile. It does not run against the released temporalio +1.11.0 gem. Replace the source dependency with a published SDK version after +release. Do not present this sample as released Public Preview documentation yet. + +The SDK provides the S3 driver. The separate `aws-sdk-s3` gem supplies its AWS +client. Use Ruby 3.3 or later and follow the SDK source-build requirements for the +pinned source dependency. + +From this directory: + +```sh +bundle install +bundle exec ruby -wc run.rb +TEMPORAL_CLI_PATH=/path/to/temporal bundle exec ruby integration_test.rb +``` + +To run the program, start a local Temporal Service, then run `bundle exec ruby run.rb`. It uses local disk by default; set `S3_BUCKET` and `AWS_REGION` to use S3. +Clients and Workers need GetObject and PutObject on `v0/*` and ListBucket on the +bucket. Configure AWS credentials using the AWS SDK's default credential chain. + +The integration tests start a local Temporal Service and send a 3 MiB value +through Workflow input, Activity input/result, and Workflow result. They verify +the returned value and four small references in Event History. The S3 test skips +unless S3_BUCKET is set. Use a disposable bucket and remove its objects afterward. +The local driver is for development; all processes must share its directory. + +## Validation of this draft + +Validated with Ruby 3.4.7 against the pinned SDK Ruby source loaded ahead of the +published 1.11.0 native bridge. The local Workflow-and-Activity test passed with a +3 MiB payload, four History references, and a largest reference of 211 bytes. +This does not validate building the SDK's native extension from the pinned Git +source. Recheck against the complete published package before release. + +The live S3 integration test also passed against a temporary bucket in us-east-2: +3,145,728 bytes returned intact, four History references, largest reference 528 +bytes. Both local and S3 tests passed (four assertions, no skips). Confirmed two +stored objects under `v0/`, each 3,145,759 bytes, then removed all objects and the +bucket and verified cleanup. +All Ruby sample files pass `ruby -wc`. diff --git a/ruby-sandbox/best-practices/data-handling/external-storage/configuration.rb b/ruby-sandbox/best-practices/data-handling/external-storage/configuration.rb new file mode 100644 index 0000000..c616e8c --- /dev/null +++ b/ruby-sandbox/best-practices/data-handling/external-storage/configuration.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'temporalio/client' +require 'temporalio/worker' +require 'temporalio/converters/external_storage' +require 'temporalio/contrib/aws/s3_driver' +require 'temporalio/contrib/aws/s3_driver/aws_sdk_s3_storage_driver_client' + +module StorageConfiguration + def self.s3_driver(bucket) + # @@@SNIPSTART ruby-external-storage-s3-create + s3_client = Aws::S3::Client.new + driver = Temporalio::Contrib::Aws::S3Driver::S3StorageDriver.new( + client: Temporalio::Contrib::Aws::S3Driver::AwsSdkS3StorageDriverClient.new(s3_client), + bucket: bucket + ) + # @@@SNIPEND + driver + end + + def self.data_converter(driver) + # @@@SNIPSTART ruby-external-storage-converter + data_converter = Temporalio::Converters::DataConverter.new( + external_storage: Temporalio::Converters::ExternalStorage.new(drivers: [driver]) + ) + # @@@SNIPEND + data_converter + end + + def self.threshold(driver) + # @@@SNIPSTART ruby-external-storage-threshold + storage = Temporalio::Converters::ExternalStorage.new( + drivers: [driver], + payload_size_threshold: 256 * 1024 + ) + # @@@SNIPEND + storage + end + + def self.migration(preferred, legacy) + # @@@SNIPSTART ruby-external-storage-multiple-drivers + storage = Temporalio::Converters::ExternalStorage.new( + drivers: [preferred, legacy], + driver_selector: ->(_context, _payload) { preferred } + ) + # @@@SNIPEND + storage + end +end diff --git a/ruby-sandbox/best-practices/data-handling/external-storage/integration_test.rb b/ruby-sandbox/best-practices/data-handling/external-storage/integration_test.rb new file mode 100644 index 0000000..b4685fb --- /dev/null +++ b/ruby-sandbox/best-practices/data-handling/external-storage/integration_test.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require 'minitest/autorun' +require 'tmpdir' +require 'temporalio/testing/workflow_environment' +require_relative 'run' + +class ExternalStorageTest < Minitest::Test + def verify_round_trip(driver) + Temporalio::Testing::WorkflowEnvironment.start_local( + data_converter: StorageConfiguration.data_converter(driver), + dev_server_existing_path: ENV['TEMPORAL_CLI_PATH'] + ) do |env| + result = ExternalStorageExample.run(env.client) + assert_equal 3 * 1024 * 1024, result.fetch(:result_bytes) + assert_equal 4, result.fetch(:reference_count) + puts JSON.generate(result) + end + end + + def test_local_disk + Dir.mktmpdir('ruby-external-storage-') do |directory| + verify_round_trip(LocalDiskDriver.new(directory)) + end + end + + def test_s3 + skip 'Set S3_BUCKET and configure AWS credentials for the live S3 test' unless ENV['S3_BUCKET'] + + verify_round_trip(StorageConfiguration.s3_driver(ENV.fetch('S3_BUCKET'))) + end +end diff --git a/ruby-sandbox/best-practices/data-handling/external-storage/large_payload_activity.rb b/ruby-sandbox/best-practices/data-handling/external-storage/large_payload_activity.rb new file mode 100644 index 0000000..bc5aab3 --- /dev/null +++ b/ruby-sandbox/best-practices/data-handling/external-storage/large_payload_activity.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'temporalio/activity' + +class LargePayloadActivity < Temporalio::Activity::Definition + def execute(input) + input + end +end diff --git a/ruby-sandbox/best-practices/data-handling/external-storage/large_payload_workflow.rb b/ruby-sandbox/best-practices/data-handling/external-storage/large_payload_workflow.rb new file mode 100644 index 0000000..e9e56aa --- /dev/null +++ b/ruby-sandbox/best-practices/data-handling/external-storage/large_payload_workflow.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +require 'temporalio/workflow' +require_relative 'large_payload_activity' + +class LargePayloadWorkflow < Temporalio::Workflow::Definition + def execute(input) + Temporalio::Workflow.execute_activity(LargePayloadActivity, input, start_to_close_timeout: 30) + end +end diff --git a/ruby-sandbox/best-practices/data-handling/external-storage/local_disk_driver.rb b/ruby-sandbox/best-practices/data-handling/external-storage/local_disk_driver.rb new file mode 100644 index 0000000..6342e66 --- /dev/null +++ b/ruby-sandbox/best-practices/data-handling/external-storage/local_disk_driver.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require 'digest' +require 'fileutils' +require 'tempfile' +require 'temporalio/api' +require 'temporalio/converters/storage_driver' +require 'temporalio/converters/storage_driver_claim' + +# @@@SNIPSTART ruby-external-storage-local-driver +# For development only. All Clients and Workers must share this directory. +class LocalDiskDriver < Temporalio::Converters::StorageDriver + def initialize(directory) + @directory = File.expand_path(directory) + FileUtils.mkdir_p(@directory) + end + + def name + 'local-disk' + end + + def type + 'example.local-disk' + end + + def store(_context, payloads) + payloads.map do |payload| + bytes = payload.to_proto + key = Digest::SHA256.hexdigest(bytes) + path = File.join(@directory, key) + unless File.exist?(path) + Tempfile.create('.payload-', @directory) do |file| + file.binmode + file.write(bytes) + file.close + begin + # Publish the complete file without replacing another writer's file. + File.link(file.path, path) + rescue Errno::EEXIST + # Another writer stored the same payload first. + end + end + end + Temporalio::Converters::StorageDriverClaim.new(claim_data: { 'key' => key }) + end + end + + def retrieve(_context, claims) + claims.map do |claim| + key = claim.claim_data.fetch('key') + raise 'Invalid payload key' unless /\A[0-9a-f]{64}\z/.match?(key) + + bytes = File.binread(File.join(@directory, key)) + raise 'Payload checksum mismatch' unless Digest::SHA256.hexdigest(bytes) == key + + Temporalio::Api::Common::V1::Payload.decode(bytes) + end + end +end +# @@@SNIPEND diff --git a/ruby-sandbox/best-practices/data-handling/external-storage/run.rb b/ruby-sandbox/best-practices/data-handling/external-storage/run.rb new file mode 100644 index 0000000..0507d45 --- /dev/null +++ b/ruby-sandbox/best-practices/data-handling/external-storage/run.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require 'json' +require 'securerandom' +require 'temporalio/env_config' +require_relative 'configuration' +require_relative 'local_disk_driver' +require_relative 'large_payload_workflow' + +module ExternalStorageExample + def self.run(client) + task_queue = "external-storage-#{SecureRandom.uuid}" + # @@@SNIPSTART ruby-external-storage-worker + worker = Temporalio::Worker.new( + client: client, + task_queue: task_queue, + workflows: [LargePayloadWorkflow], + activities: [LargePayloadActivity], + workflow_payload_codec_thread_pool: Temporalio::Worker::ThreadPool.new + ) + # @@@SNIPEND + worker.run do + input = 'x' * (3 * 1024 * 1024) + handle = client.start_workflow(LargePayloadWorkflow, input, + id: "external-storage-#{SecureRandom.uuid}", task_queue: task_queue, + execution_timeout: 60) + raise 'Returned payload differs from input' unless handle.result == input + + payloads = handle.fetch_history.events.flat_map do |event| + attributes = event.workflow_execution_started_event_attributes || event.activity_task_scheduled_event_attributes || + event.activity_task_completed_event_attributes || event.workflow_execution_completed_event_attributes + next [] unless attributes + + values = attributes.respond_to?(:input) ? attributes.input : attributes.result + values ? values.payloads.to_a : [] + end + unless payloads.size == 4 && payloads.all? { |p| p.metadata['messageType'] == 'temporal.api.sdk.v1.ExternalStorageReference' && p.to_proto.bytesize < 1024 } + raise 'Expected four small External Storage references in Event History' + end + { result_bytes: input.bytesize, reference_count: payloads.size, + largest_reference_bytes: payloads.map { |p| p.to_proto.bytesize }.max } + end + end +end + +if $PROGRAM_NAME == __FILE__ + driver = if ENV['S3_BUCKET'] + StorageConfiguration.s3_driver(ENV.fetch('S3_BUCKET')) + else + LocalDiskDriver.new(ENV.fetch('PAYLOAD_STORE_DIR', 'payload-store')) + end + data_converter = StorageConfiguration.data_converter(driver) + # @@@SNIPSTART ruby-external-storage-client + args, options = Temporalio::EnvConfig::ClientConfig.load_client_connect_options + args[0] ||= 'localhost:7233' + args[1] ||= 'default' + client = Temporalio::Client.connect(*args, **options, data_converter: data_converter) + # @@@SNIPEND + puts JSON.generate(ExternalStorageExample.run(client)) +end From c2b538e3e0bfaa425206373d51cdffd518f7ac70 Mon Sep 17 00:00:00 2001 From: Lenny Chen Date: Mon, 23 Nov 2026 17:33:30 -0800 Subject: [PATCH 2/2] Use published Ruby SDK and gate sample on integration CI --- .github/workflows/ci-on-pr.yml | 7 +++++ .../data-handling/external-storage/.gitignore | 1 + .../data-handling/external-storage/Gemfile | 4 +-- .../data-handling/external-storage/README.md | 27 ++++--------------- 4 files changed, 14 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci-on-pr.yml b/.github/workflows/ci-on-pr.yml index a828694..80eb8bd 100644 --- a/.github/workflows/ci-on-pr.yml +++ b/.github/workflows/ci-on-pr.yml @@ -123,6 +123,13 @@ jobs: done exit $status + - name: Install External Storage sample dependencies + working-directory: ruby-sandbox/best-practices/data-handling/external-storage + run: bundle install + - name: Test External Storage round trip + working-directory: ruby-sandbox/best-practices/data-handling/external-storage + run: bundle exec ruby integration_test.rb + rust: name: Rust runs-on: ubuntu-latest diff --git a/ruby-sandbox/best-practices/data-handling/external-storage/.gitignore b/ruby-sandbox/best-practices/data-handling/external-storage/.gitignore index 2c5642e..446030c 100644 --- a/ruby-sandbox/best-practices/data-handling/external-storage/.gitignore +++ b/ruby-sandbox/best-practices/data-handling/external-storage/.gitignore @@ -1,3 +1,4 @@ payload-store/ .bundle/ vendor/ +Gemfile.lock diff --git a/ruby-sandbox/best-practices/data-handling/external-storage/Gemfile b/ruby-sandbox/best-practices/data-handling/external-storage/Gemfile index 021f404..c870d35 100644 --- a/ruby-sandbox/best-practices/data-handling/external-storage/Gemfile +++ b/ruby-sandbox/best-practices/data-handling/external-storage/Gemfile @@ -1,7 +1,5 @@ source 'https://rubygems.org' -# Unreleased public API: replace this pin with a published version after SDK release. -gem 'temporalio', git: 'https://github.com/temporalio/sdk-ruby.git', - ref: 'b0f24ba3da8b58c58b50929095a94932c88e3177', glob: 'temporalio/*.gemspec', submodules: true +gem 'temporalio', '~> 1.11' gem 'aws-sdk-s3' gem 'minitest', '~> 5.25' diff --git a/ruby-sandbox/best-practices/data-handling/external-storage/README.md b/ruby-sandbox/best-practices/data-handling/external-storage/README.md index d99a23f..84956c3 100644 --- a/ruby-sandbox/best-practices/data-handling/external-storage/README.md +++ b/ruby-sandbox/best-practices/data-handling/external-storage/README.md @@ -1,13 +1,8 @@ -# Ruby External Storage (unreleased SDK draft) - -This sample targets the pending public API in temporalio/sdk-ruby PR #688, pinned -in this directory's Gemfile. It does not run against the released temporalio -1.11.0 gem. Replace the source dependency with a published SDK version after -release. Do not present this sample as released Public Preview documentation yet. +# Ruby External Storage The SDK provides the S3 driver. The separate `aws-sdk-s3` gem supplies its AWS -client. Use Ruby 3.3 or later and follow the SDK source-build requirements for the -pinned source dependency. +client. Use Ruby 3.3 or later and a released `temporalio` gem with External Storage +support. From this directory: @@ -27,17 +22,5 @@ the returned value and four small references in Event History. The S3 test skips unless S3_BUCKET is set. Use a disposable bucket and remove its objects afterward. The local driver is for development; all processes must share its directory. -## Validation of this draft - -Validated with Ruby 3.4.7 against the pinned SDK Ruby source loaded ahead of the -published 1.11.0 native bridge. The local Workflow-and-Activity test passed with a -3 MiB payload, four History references, and a largest reference of 211 bytes. -This does not validate building the SDK's native extension from the pinned Git -source. Recheck against the complete published package before release. - -The live S3 integration test also passed against a temporary bucket in us-east-2: -3,145,728 bytes returned intact, four History references, largest reference 528 -bytes. Both local and S3 tests passed (four assertions, no skips). Confirmed two -stored objects under `v0/`, each 3,145,759 bytes, then removed all objects and the -bucket and verified cleanup. -All Ruby sample files pass `ruby -wc`. +CI installs the published gems and runs the local-disk integration test. The S3 +test requires AWS credentials and a bucket and is run separately.