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 new file mode 100644 index 0000000..446030c --- /dev/null +++ b/ruby-sandbox/best-practices/data-handling/external-storage/.gitignore @@ -0,0 +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 new file mode 100644 index 0000000..c870d35 --- /dev/null +++ b/ruby-sandbox/best-practices/data-handling/external-storage/Gemfile @@ -0,0 +1,5 @@ +source 'https://rubygems.org' + +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 new file mode 100644 index 0000000..84956c3 --- /dev/null +++ b/ruby-sandbox/best-practices/data-handling/external-storage/README.md @@ -0,0 +1,26 @@ +# 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 a released `temporalio` gem with External Storage +support. + +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. + +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. 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