Skip to content

Issues noticed while dockerizing the bq connection to flapi - #1

Merged
jrosskopf merged 6 commits into
DataZooDE:mainfrom
NithinKumaraNT:bq_dockerize
Nov 6, 2024
Merged

jrosskopf merged 6 commits into
DataZooDE:mainfrom
NithinKumaraNT:bq_dockerize

Conversation

@NithinKumaraNT

@NithinKumaraNT NithinKumaraNT commented Oct 30, 2024 •

Copy link
Copy Markdown
Collaborator

The document describes the issues encountered while setting up the docker-compose-bigquery environment.

Issue 1: bigquery extension fails to load sometimes

The error message in the logs is:

docker-compose-bigquery-flapi-1  | (2024-10-30 11:26:51) [INFO    ] Loading configuration file: "/app/examples/flapi.yaml"
docker-compose-bigquery-flapi-1  | (2024-10-30 11:26:51) [INFO    ] Parsing main configuration
docker-compose-bigquery-flapi-1  | (2024-10-30 11:26:51) [INFO    ] Parsing connections
docker-compose-bigquery-flapi-1  | (2024-10-30 11:26:51) [INFO    ] Parsed 2 connections
docker-compose-bigquery-flapi-1  | (2024-10-30 11:26:51) [INFO    ] Parsing rate limit configuration
docker-compose-bigquery-flapi-1  | (2024-10-30 11:26:51) [INFO    ] Parsing auth configuration
docker-compose-bigquery-flapi-1  | (2024-10-30 11:26:51) [INFO    ] Parsing DuckDB configuration
docker-compose-bigquery-flapi-1  | (2024-10-30 11:26:51) [INFO    ] Parsing template configuration
docker-compose-bigquery-flapi-1  | (2024-10-30 11:26:51) [INFO    ] Parsing global heartbeat configuration
docker-compose-bigquery-flapi-1  | (2024-10-30 11:26:51) [INFO    ] Loading endpoint configs recursively from: "/app/examples/sqls"
docker-compose-bigquery-flapi-1  | (2024-10-30 11:26:51) [INFO    ] Loaded 2 endpoint configurations
docker-compose-bigquery-flapi-1  | (2024-10-30 11:26:51) [INFO    ] Configuration loaded successfully
docker-compose-bigquery-flapi-1  | terminate called after throwing an instance of 'std::runtime_error'
docker-compose-bigquery-flapi-1  |   what():  Error creating database, Details: Failed to execute init statement INSTALL 'bigquery' FROM 'http://storage.googleapis.com/hafenkran';
docker-compose-bigquery-flapi-1  | LOAD 'bigquery';
docker-compose-bigquery-flapi-1  | 
docker-compose-bigquery-flapi-1  |  Error: IO Error: Failed to download extension "bigquery" at URL "http://storage.googleapis.com/hafenkran/v1.1.2/linux_amd64/bigquery.duckdb_extension.gz"
docker-compose-bigquery-flapi-1  | 
docker-compose-bigquery-flapi-1  | Candidate extensions: "icu", "iceberg", "parquet", "inet", "azure" (ERROR Failed to read connection)
docker-compose-bigquery-flapi-1  | 
docker-compose-bigquery-flapi-1 exited with code 139

Issue 2: With no erros or warnings, the container fails with error code 139.

The error message in the logs is:

docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:45) [INFO    ] Loading configuration file: "/app/examples/flapi.yaml"
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Parsing main configuration
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Parsing connections
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Parsed 2 connections
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Parsing rate limit configuration
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Parsing auth configuration
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Parsing DuckDB configuration
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Parsing template configuration
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Parsing global heartbeat configuration
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Loading endpoint configs recursively from: "/app/examples/sqls"
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Loaded 2 endpoint configurations
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Configuration loaded successfully
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Warming up endpoint caches, this might take some time...
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Checking if cache should be refreshed for endpoint: /publicis
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Cache is fresh: publicis_cache_1730287651: (table age) 75s <= 300s (refreshTime from config)
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Finished warming up endpoint caches! Let's go!
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Setting up routes...
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Routes set up completed
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] APIServer initialized
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Server starting on port 8080...
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] flAPI server is running at http://0.0.0.0:8080 using 4 threads
docker-compose-bigquery-flapi-1  | 
docker-compose-bigquery-flapi-1  | (2024-10-30 11:28:46) [INFO    ] Call `app.loglevel(crow::LogLevel::Warning)` to hide Info level logs.
docker-compose-bigquery-flapi-1  | 
docker-compose-bigquery-flapi-1 exited with code 132

Issue 3: Swagger UI does not show api specs

image

@NithinKumaraNT

Copy link
Copy Markdown
Collaborator Author

Thanks for checking but I still face the following errors :

Issue 1 : Sub directories are not scanned properly for SQL files to cache

docker-compose-bigquery-flapi-1  | (2024-10-31 08:07:05) [INFO    ] Configuration loaded successfully
docker-compose-bigquery-flapi-1  | (2024-10-31 08:07:05) [DEBUG   ] DuckDB Library Version: v1.1.2
docker-compose-bigquery-flapi-1  | (2024-10-31 08:07:05) [DEBUG   ] DuckDB DB Version: v1.1.2
docker-compose-bigquery-flapi-1  | (2024-10-31 08:07:05) [DEBUG   ] Executing init statement for connection: bigquery-lakehouse
docker-compose-bigquery-flapi-1  | (2024-10-31 08:07:07) [DEBUG   ] 2 init statements executed successfully: 
docker-compose-bigquery-flapi-1  | FORCE INSTALL 'bigquery' FROM 'http://storage.googleapis.com/hafenkran';
docker-compose-bigquery-flapi-1  | LOAD 'bigquery';
docker-compose-bigquery-flapi-1  | 
docker-compose-bigquery-flapi-1  | 
docker-compose-bigquery-flapi-1  | (2024-10-31 08:07:07) [INFO    ] Warming up endpoint caches, this might take some time...
docker-compose-bigquery-flapi-1  | (2024-10-31 08:07:07) [INFO    ] Checking if cache should be refreshed for endpoint: /publicis
docker-compose-bigquery-flapi-1  | (2024-10-31 08:07:07) [INFO    ] Cache is fresh: publicis_cache_1730361935: (table age) 92s <= 300s (refreshTime from config)
docker-compose-bigquery-flapi-1  | (2024-10-31 08:07:07) [INFO    ] Checking if cache should be refreshed for endpoint: /recommendations/bydevice
docker-compose-bigquery-flapi-1  | (2024-10-31 08:07:07) [INFO    ] Cache table not found: 'recommendations_bydevice_cache%', need to refresh
docker-compose-bigquery-flapi-1  | (2024-10-31 08:07:07) [INFO    ] Starting to refresh cache: flapi_cache.recommendations_bydevice_cache_1730362027
docker-compose-bigquery-flapi-1  | terminate called after throwing an instance of 'std::runtime_error'
docker-compose-bigquery-flapi-1  |   what():  Error creating database, Details: Template file not found: /app/examples/sqls/bydevice_cache.sql

I believe it is not scanning the recommendations folder for SQL files ?

Issue 2 : Empty server response from server

flapi git:(bq_dockerize) > curl http://0.0.0.0:8080
curl: (52) Empty reply from server
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:26) [INFO    ] Configuration loaded successfully
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:26) [DEBUG   ] DuckDB Library Version: v1.1.2
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:26) [DEBUG   ] DuckDB DB Version: v1.1.2
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:26) [DEBUG   ] Executing init statement for connection: bigquery-lakehouse
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:28) [DEBUG   ] 2 init statements executed successfully: 
docker-compose-bigquery-flapi-1  | FORCE INSTALL 'bigquery' FROM 'http://storage.googleapis.com/hafenkran';
docker-compose-bigquery-flapi-1  | LOAD 'bigquery';
docker-compose-bigquery-flapi-1  | 
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:28) [INFO    ] Warming up endpoint caches, this might take some time...
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:28) [INFO    ] Checking if cache should be refreshed for endpoint: /publicis
docker-compose-bigquery-flapi-1  | 
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:28) [INFO    ] Cache is fresh: publicis_cache_1730362274: (table age) 194s <= 300s (refreshTime from config)
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:28) [INFO    ] Checking if cache should be refreshed for endpoint: /recommendations/bydevice
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:28) [INFO    ] Cache is fresh: recommendations_bydevice_cache_1730362335: (table age) 133s <= 600s (refreshTime from config)
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:28) [INFO    ] Finished warming up endpoint caches! Let's go!
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:28) [INFO    ] Setting up routes...
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:28) [INFO    ] Routes set up completed
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:28) [INFO    ] APIServer initialized
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:28) [INFO    ] Server starting on port 8080...
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:28) [INFO    ] flAPI server is running at http://0.0.0.0:8080 using 4 threads
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:28) [INFO    ] Call `app.loglevel(crow::LogLevel::Warning)` to hide Info level logs.
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:28) [DEBUG   ] 0x55d83a8236d0 {0} queue length: 1
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:31) [DEBUG   ] 0x55d83a8e3fe0 {1} queue length: 1
docker-compose-bigquery-flapi-1  | 
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:31) [DEBUG   ] 0x55d83a952b20 timer cancelled: 0x7f5b7edfafa0 0
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:31) [DEBUG   ] task_timer cancelled: 0x7f5b7edfafa0 0
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:31) [DEBUG   ] task_timer scheduled: 0x7f5b7edfafa0 1
docker-compose-bigquery-flapi-1  | (2024-10-31 08:14:31) [DEBUG   ] 0x55d83a952b20 timer added: 0x7f5b7edfafa0 1
docker-compose-bigquery-flapi-1  | 
docker-compose-bigquery-flapi-1 exited with code 132

@jrosskopf
jrosskopf self-requested a review October 31, 2024 12:56
@jrosskopf
jrosskopf merged commit e6fb7e0 into DataZooDE:main Nov 6, 2024
jrosskopf added a commit that referenced this pull request May 22, 2026
Part of #40. Implements integration tests for the spike's documented
round-trip behaviours, plus a fix surfaced by the tests.

New file: test/integration/test_self_packaging.py (8 tests, ~24 sec
wall on Linux x86_64 debug build):

  #2 pack_produces_bundled_executable
       `flapi pack --in <fixture> --out <new>` succeeds, output exists,
       exec bits preserved (Unix), file size >= host binary size.

  #4 info_reports_bundle_contents
       `<bundled> info` shows EOCD offset, bundle size, and lists every
       fixture entry by name.

  #5 self_hosting_rebundle
       The bundled binary can pack a new bundled binary
       (`<bundled> pack ...`), which itself has a valid bundle.

  #6 repack_idempotence
       Three rounds of packing using each previous output as host
       binary -- file size identical across rounds.

  #7 truncated_bundle_falls_back_to_filesystem
       Chop 1 KiB off the tail. `info` reports "none". A
       `--validate-config -c <fixture>/flapi.yaml` invocation exits
       cleanly (no SIGSEGV/SIGABRT) -- the spike safety-net behaviour.

  #8 unpack_restores_all_entries
       `<bundled> unpack --to <dir>` writes every entry; CSV bytes
       round-trip byte-for-byte.

  pack_refuses_secret_files_by_default
       `.env` in input -> non-zero exit, "secret" in error message,
       AND no half-written output binary on disk.

  pack_with_allow_secrets_bundles_env_files
       --allow-secrets override bundles `.env` despite the deny list;
       `info` lists it.

Implementation fix (src/pack.cpp): `Pack()` previously copied the
multi-GB host binary BEFORE walking the input tree and validating the
secret deny list. A rejected `.env` therefore left a half-written
1.7 GB file in the user's tmp dir. Re-ordered: walk + validate first,
then copy + append. Surfaced by `test_pack_refuses_secret_files_by_default`.

Spike behaviours #1, #3, #9 (filesystem-mode + bundled-mode HTTP +
read_csv('embed://...') endpoint) require server-lifecycle test
infrastructure plus resolving bundled-mode config-path lookup
(template.path resolution from inside an `embed://` config) -- deferred
to a follow-up issue. The DuckDB-side embed:// integration is already
proven in-process by the unit tests in #44.

Closes #46 (partial -- server-lifecycle behaviours deferred).
jrosskopf added a commit that referenced this pull request May 22, 2026
Part of #40. Implements integration tests for the spike's documented
round-trip behaviours, plus a fix surfaced by the tests.

New file: test/integration/test_self_packaging.py (8 tests, ~24 sec
wall on Linux x86_64 debug build):

  #2 pack_produces_bundled_executable
       `flapi pack --in <fixture> --out <new>` succeeds, output exists,
       exec bits preserved (Unix), file size >= host binary size.

  #4 info_reports_bundle_contents
       `<bundled> info` shows EOCD offset, bundle size, and lists every
       fixture entry by name.

  #5 self_hosting_rebundle
       The bundled binary can pack a new bundled binary
       (`<bundled> pack ...`), which itself has a valid bundle.

  #6 repack_idempotence
       Three rounds of packing using each previous output as host
       binary -- file size identical across rounds.

  #7 truncated_bundle_falls_back_to_filesystem
       Chop 1 KiB off the tail. `info` reports "none". A
       `--validate-config -c <fixture>/flapi.yaml` invocation exits
       cleanly (no SIGSEGV/SIGABRT) -- the spike safety-net behaviour.

  #8 unpack_restores_all_entries
       `<bundled> unpack --to <dir>` writes every entry; CSV bytes
       round-trip byte-for-byte.

  pack_refuses_secret_files_by_default
       `.env` in input -> non-zero exit, "secret" in error message,
       AND no half-written output binary on disk.

  pack_with_allow_secrets_bundles_env_files
       --allow-secrets override bundles `.env` despite the deny list;
       `info` lists it.

Implementation fix (src/pack.cpp): `Pack()` previously copied the
multi-GB host binary BEFORE walking the input tree and validating the
secret deny list. A rejected `.env` therefore left a half-written
1.7 GB file in the user's tmp dir. Re-ordered: walk + validate first,
then copy + append. Surfaced by `test_pack_refuses_secret_files_by_default`.

Spike behaviours #1, #3, #9 (filesystem-mode + bundled-mode HTTP +
read_csv('embed://...') endpoint) require server-lifecycle test
infrastructure plus resolving bundled-mode config-path lookup
(template.path resolution from inside an `embed://` config) -- deferred
to a follow-up issue. The DuckDB-side embed:// integration is already
proven in-process by the unit tests in #44.

Closes #46 (partial -- server-lifecycle behaviours deferred).
jrosskopf added a commit that referenced this pull request May 25, 2026
Before this change, `flapi pack` produced a working binary at the CLI
level (`info`, `unpack`, etc.) but `./flapi-bundled` started from a
clean cwd crashed with "Could not open file: flapi.yaml" -- the
EmbeddedArchiveFileProvider was registered globally but the config /
endpoint / template loaders bypassed it via std::ifstream and
std::filesystem::recursive_directory_iterator.

Four call sites had to be routed through FileProviderFactory so the
in-memory archive is actually consulted when SetBundleContents has
fired:

- extended_yaml_parser: parseFile and the static loadYamlFile now
  read via FileProviderFactory::CreateProvider(path)->ReadFile(...)
  instead of std::ifstream. resolveIncludePath swaps
  std::filesystem::exists for a provider-aware ConfigFileExists so
  {{include from ...}} directives find their targets inside the
  bundle.
- config_manager::parseTemplateConfig: in bundled mode, skip the
  std::filesystem::absolute() step -- the template path is a bundle
  key like "sqls", not a filesystem path; turning it into
  "/cwd/sqls" prevents the embedded provider from finding it.
- config_manager::loadEndpointConfigsRecursively: in bundled mode,
  enumerate .yaml/.yml entries by prefix-scanning the bundle map
  instead of std::filesystem::recursive_directory_iterator. The
  EmbeddedArchiveFileProvider's ListFiles is non-recursive by
  contract, so the walk is inlined here.
- config_manager::getFileProvider: in bundled mode, return a fresh
  provider from FileProviderFactory so SQLTemplateProcessor reads
  SQL templates from the bundle rather than the LocalFileProvider
  baked into ConfigLoader.

Plus a small fix in sql_template_processor::getFullTemplatePath:
when the endpoint parser has already resolved templateSource against
its YAML's parent dir (which in bundled mode produces a full bundle
key like "sqls/hello.sql"), don't re-prepend the basePath. Mirrors
the existing absolute-path short-circuit just above.

test_self_packaging_http.py adds the three pytest cases from the
issue acceptance:
- #1 unbundled flapi serves /hello with 200 + body
- #3 bundled flapi from clean cwd (with the source tree deleted)
  serves the same /hello
- #9 a SQL template using read_csv('embed://data/cities.csv')
  returns the bundled CSV rows over HTTP

All 17 integration tests pass (existing CLI-level self-packaging
suite + env-override suite + the 3 new HTTP cases). 637/637 C++ unit
tests pass.

Closes #62.
jrosskopf added a commit that referenced this pull request Sep 15, 2026
P0 issue 1. flAPI had three separate clocks timing overlapping operations
(api_server.cpp:218, mcp_tool_handler.cpp:14 and :35) and a request id that
nothing outside the audit logger could see, so an audit line, a log line and a
telemetry event could not be joined. RequestContext is the one object per request
that fixes that; the HTTP span will hang off the same object later.

It is created on EVERY request on EVERY route whether or not tracing is on, so
allocation was a design constraint rather than an afterthought: fixed-width
std::array for the ids (a 32-char W3C trace id exceeds libstdc++'s 15-byte SSO
buffer, so std::string would allocate per request - measured in
test_alloc_budget.cpp), const char* into static storage for the closed
enumerations, string_view for raw_path and route_template, and an audit_params
vector only populated when audit is enabled. Measured cost on the cheap routes:
+6.6% TTFB median, well inside the 15% gate.

Also closes drift bug #1: docs/CONFIG_REFERENCE.md:513 and examples/flapi.yaml
have always claimed the audit log "covers both REST and MCP traffic", but the
only AuditLogger call site was in the MCP tool handler. REST is now covered.

Three behaviours worth calling out, each pinned by a test:

- A 401 rejected in AuthMiddleware is audited. Crow does NOT call after_handle
  when a middleware completes the response inside before_handle
  (crow/http_connection.h:183-207), which is exactly what AuthMiddleware does, so
  a middleware that trusts after_handle silently loses every 401. Those paths now
  complete the request explicitly and after_handle is idempotent.
- One audit line per operation. An MCP tools/call arrives as an HTTP POST, so the
  new middleware would report the same work twice under two names. MCP suppresses
  the transport-level line and keeps its richer tool-level one, carrying the
  shared request id across. Suppression is set at the MCP transport so adding REST
  coverage does not silently expand MCP's audit scope to handshakes.
- Health probes are not audited. In Kubernetes they are the highest-volume route
  and carry no compliance information; auditing them would bury real entries.

X-Request-Id is on every response and is always server-minted - an inbound value
is never echoed, or a client could forge collisions and inject into log lines.

Trace ids are emitted into the audit line only when non-empty, so the audit
schema is byte-identical for operators who never enable tracing.

The middleware deliberately does NOT resolve routes: AuthMiddleware and
handleDynamicRequest already do, and a third O(N) scan would tax /health, which
resolves nothing today. The handler writes the template back instead.

Unit 705 passed, integration 580 passed, load gate passed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants