-
Notifications
You must be signed in to change notification settings - Fork 1
fix(schema-polling): improve polling #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
slimee
wants to merge
24
commits into
main
Choose a base branch
from
perf/improve-schema-polling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+846
−363
Open
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e83e465
feat(schema-polling): implement initial schema fetch and improve poll…
slimee 109cd32
feat(schema-polling): enhance schema polling with initial synchronous…
slimee faa6fb3
fix(schema polling): improve initial polling behavior and logging
slimee 967d9bd
fix(rpc): fix RuboCop offenses in schema_polling_client
slimee 0af3bc6
chore: fix typo
slimee 74e8835
chore: merge main into perf/improve-schema-polling
slimee 132044b
fix(rpc): remove redundant assignment in build method
slimee 6215deb
fix(schema-polling): move digest require to top of file
slimee 503b38a
chore: trigger CI rerun
slimee 408e75e
fix(schema): update polling interval option naming and improve ETag h…
slimee e31919d
fix(schema-polling): improve initial sync handling and error reportin…
slimee b65252d
refactor(schema polling): update log levels for schema sync completio…
slimee d54d4d2
fix(schema_polling): remove unnecessary whitespace in initial_sync_co…
slimee 36e2b48
fix(schema polling): trigger schema update callback only after initia…
slimee 63c3956
feat(datasource): add graceful shutdown hook for schema polling cleanup
slimee ac06b19
refactor(schema_polling): improve thread stopping mechanism to avoid …
slimee 9115481
fix(schema_polling): ensure graceful shutdown of polling thread
slimee ab74679
fix: call only one time the get schema at start
arnaud-moncel 4053f38
fix: call with introspection
arnaud-moncel f62484a
chore: improve polling
arnaud-moncel af3e857
fix: polling start
arnaud-moncel cf38c5e
feat: shared thread pool for polling
409cf5f
fix: clean up
162284b
fix: add options for thread pool
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,40 +1,47 @@ | ||
| require 'openssl' | ||
| require 'json' | ||
| require 'time' | ||
| require 'digest' | ||
|
|
||
| module ForestAdminDatasourceRpc | ||
| module Utils | ||
| class SchemaPollingClient | ||
| attr_reader :closed | ||
| attr_reader :closed, :current_schema | ||
|
|
||
| DEFAULT_POLLING_INTERVAL = 600 # seconds (10 minutes) | ||
| MIN_POLLING_INTERVAL = 1 # seconds (minimum safe interval) | ||
| MAX_POLLING_INTERVAL = 3600 # seconds (1 hour max) | ||
| DEFAULT_POLLING_INTERVAL = 600 | ||
| MIN_POLLING_INTERVAL = 1 | ||
| MAX_POLLING_INTERVAL = 3600 | ||
|
|
||
| def initialize(uri, auth_secret, options = {}, &on_schema_change) | ||
| def initialize(uri, auth_secret, options = {}, introspection_schema = nil, &on_schema_change) | ||
| @uri = uri | ||
| @auth_secret = auth_secret | ||
| @polling_interval = options[:polling_interval] || DEFAULT_POLLING_INTERVAL | ||
| @on_schema_change = on_schema_change | ||
| @closed = false | ||
| @introspection_schema = introspection_schema | ||
| @current_schema = nil | ||
| @cached_etag = nil | ||
| @polling_thread = nil | ||
| @mutex = Mutex.new | ||
| @connection_attempts = 0 | ||
| @initial_sync_completed = false | ||
|
|
||
| # Validate polling interval | ||
| validate_polling_interval! | ||
|
|
||
| # RPC client for schema fetching with ETag support | ||
| @rpc_client = RpcClient.new(@uri, @auth_secret) | ||
| end | ||
|
|
||
| def start | ||
| return if @closed | ||
| def start # rubocop:disable Naming/PredicateMethod | ||
| return false if @closed | ||
|
|
||
| @mutex.synchronize do | ||
| return if @polling_thread&.alive? | ||
| return false if @polling_thread&.alive? | ||
|
|
||
| # Always try to fetch initial schema from RPC agent | ||
| ForestAdminAgent::Facades::Container.logger&.log('Info', "Getting schema from RPC agent on #{@uri}.") | ||
| fetch_initial_schema_sync | ||
|
|
||
| # Start async polling thread | ||
| @polling_thread = Thread.new do | ||
| polling_loop | ||
| rescue StandardError => e | ||
|
|
@@ -49,6 +56,7 @@ def start | |
| 'Info', | ||
| "[Schema Polling] Polling started (interval: #{@polling_interval}s)" | ||
| ) | ||
| true | ||
| end | ||
|
|
||
| def stop | ||
|
|
@@ -57,43 +65,104 @@ def stop | |
| @closed = true | ||
| ForestAdminAgent::Facades::Container.logger&.log('Debug', '[Schema Polling] Stopping polling') | ||
|
|
||
| @mutex.synchronize do | ||
| if @polling_thread&.alive? | ||
| @polling_thread.kill | ||
| @polling_thread = nil | ||
| end | ||
| end | ||
| @polling_thread.join(2) if @polling_thread&.alive? | ||
| @polling_thread = nil | ||
|
|
||
| ForestAdminAgent::Facades::Container.logger&.log('Debug', '[Schema Polling] Polling stopped') | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def compute_etag(schema) | ||
| return nil if schema.nil? | ||
|
|
||
| Digest::SHA1.hexdigest(schema.to_json) | ||
| end | ||
|
|
||
| def fetch_initial_schema_sync | ||
| # If we have an introspection schema, send its ETag to avoid re-downloading unchanged schema | ||
| introspection_etag = compute_etag(@introspection_schema) if @introspection_schema | ||
| result = @rpc_client.fetch_schema('/forest/rpc-schema', if_none_match: introspection_etag) | ||
|
|
||
| if result == RpcClient::NotModified | ||
| # Schema unchanged from introspection - use introspection | ||
| @current_schema = @introspection_schema | ||
| @cached_etag = introspection_etag | ||
| @initial_sync_completed = true | ||
| ForestAdminAgent::Facades::Container.logger&.log( | ||
| 'Info', | ||
| "[Schema Polling] RPC schema unchanged (HTTP 304), using introspection (ETag: #{@cached_etag})" | ||
| ) | ||
| else | ||
| # New schema from RPC | ||
| @current_schema = result.body | ||
| @cached_etag = result.etag || compute_etag(@current_schema) | ||
| @initial_sync_completed = true | ||
| ForestAdminAgent::Facades::Container.logger&.log( | ||
| 'Debug', | ||
| "[Schema Polling] Initial schema fetched successfully (ETag: #{@cached_etag})" | ||
| ) | ||
| end | ||
|
|
||
| @introspection_schema = nil | ||
| rescue Faraday::ConnectionFailed, Faraday::TimeoutError, | ||
| ForestAdminAgent::Http::Exceptions::AuthenticationOpenIdClient, StandardError => e | ||
| handle_initial_fetch_error(e) | ||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| end | ||
|
|
||
| def handle_initial_fetch_error(error) | ||
| if @introspection_schema | ||
| # Fallback to introspection schema - don't crash | ||
| @current_schema = @introspection_schema | ||
| @cached_etag = compute_etag(@current_schema) | ||
| @introspection_schema = nil | ||
| @initial_sync_completed = true | ||
| ForestAdminAgent::Facades::Container.logger&.log( | ||
| 'Warn', | ||
| "RPC agent at #{@uri} is unreachable (#{error.class}: #{error.message}), " \ | ||
| "using provided introspection schema (ETag: #{@cached_etag})" | ||
| ) | ||
| else | ||
| # No introspection - re-raise to crash | ||
| ForestAdminAgent::Facades::Container.logger&.log( | ||
| 'Error', | ||
| "Failed to get schema from RPC agent at #{@uri}: #{error.class} - #{error.message}" | ||
| ) | ||
| raise error | ||
| end | ||
| end | ||
|
|
||
| def polling_loop | ||
| etag_status = @cached_etag ? "with ETag: #{@cached_etag}" : 'without initial ETag' | ||
| ForestAdminAgent::Facades::Container.logger&.log( | ||
| 'Debug', | ||
| "[Schema Polling] Starting polling loop (interval: #{@polling_interval}s)" | ||
| "[Schema Polling] Starting polling loop (interval: #{@polling_interval}s, #{etag_status})" | ||
| ) | ||
|
|
||
| loop do | ||
| break if @closed | ||
|
|
||
| etag_info = @cached_etag || 'none' | ||
| ForestAdminAgent::Facades::Container.logger&.log( | ||
| 'Debug', | ||
| "[Schema Polling] Waiting #{@polling_interval}s before next check (current ETag: #{etag_info})" | ||
| ) | ||
| sleep_with_interrupt(@polling_interval) | ||
| break if @closed | ||
|
|
||
| begin | ||
| check_schema | ||
| rescue StandardError => e | ||
| handle_error(e) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Sleep with interrupt check (check every second for early termination) | ||
| ForestAdminAgent::Facades::Container.logger&.log( | ||
| 'Debug', | ||
| "[Schema Polling] Waiting #{@polling_interval}s before next check (current ETag: #{@cached_etag || "none"})" | ||
| ) | ||
| remaining = @polling_interval | ||
| while remaining.positive? && !@closed | ||
| sleep([remaining, 1].min) | ||
| remaining -= 1 | ||
| end | ||
| def sleep_with_interrupt(duration) | ||
| remaining = duration | ||
| while remaining.positive? && !@closed | ||
| sleep([remaining, 1].min) | ||
| remaining -= 1 | ||
| end | ||
| end | ||
|
|
||
|
|
@@ -162,22 +231,27 @@ def handle_schema_unchanged | |
| end | ||
|
|
||
| def handle_schema_changed(result) | ||
| new_etag = result.etag | ||
| @cached_etag.nil? ? handle_initial_schema(new_etag) : handle_schema_update(result.body, new_etag) | ||
| @connection_attempts = 0 | ||
| end | ||
| new_schema = result.body | ||
| new_etag = result.etag || compute_etag(new_schema) | ||
|
|
||
| def handle_initial_schema(etag) | ||
| @cached_etag = etag | ||
| ForestAdminAgent::Facades::Container.logger&.log( | ||
| 'Debug', | ||
| "[Schema Polling] Initial schema loaded successfully (ETag: #{etag})" | ||
| ) | ||
| if @initial_sync_completed | ||
| handle_schema_update(new_schema, new_etag) | ||
| else | ||
| @cached_etag = new_etag | ||
| @current_schema = new_schema | ||
| @initial_sync_completed = true | ||
| ForestAdminAgent::Facades::Container.logger&.log( | ||
| 'Info', | ||
| "[Schema Polling] Initial sync completed successfully (ETag: #{new_etag})" | ||
| ) | ||
| end | ||
| @connection_attempts = 0 | ||
| end | ||
|
|
||
| def handle_schema_update(schema, etag) | ||
| old_etag = @cached_etag | ||
| @cached_etag = etag | ||
| @current_schema = schema | ||
| msg = "[Schema Polling] Schema changed detected (old ETag: #{old_etag}, new ETag: #{etag}), " \ | ||
| 'triggering reload callback' | ||
| ForestAdminAgent::Facades::Container.logger&.log('Info', msg) | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.