Skip to content
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

chore(postgres): not loading the libpq library by default & better user feedback #2028

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ ifneq ($(USE_LIBBACKTRACE), 0)
deps: | libbacktrace
endif

ifeq ($(POSTGRES), 1)
NIM_PARAMS := $(NIM_PARAMS) -d:postgres -d:nimDebugDlOpen
endif

clean: | clean-libbacktrace


Expand Down Expand Up @@ -218,7 +222,7 @@ docs: | build deps
#####################
# -d:insecure - Necessary to enable Prometheus HTTP endpoint for metrics
# -d:chronicles_colors:none - Necessary to disable colors in logs for Docker
DOCKER_IMAGE_NIMFLAGS ?= -d:chronicles_colors:none -d:insecure
DOCKER_IMAGE_NIMFLAGS ?= -d:chronicles_colors:none -d:insecure -d:postgres
DOCKER_IMAGE_NIMFLAGS := $(DOCKER_IMAGE_NIMFLAGS) $(HEAPTRACK_PARAMS)

# build a docker image for the fleet
Expand Down
4 changes: 0 additions & 4 deletions waku/waku_archive/archive.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import
../common/databases/dburl,
../common/databases/db_sqlite,
./driver,
./driver/queue_driver,
./driver/sqlite_driver,
./driver/sqlite_driver/migrations as archive_driver_sqlite_migrations,
./driver/postgres_driver/postgres_driver,
./retention_policy,
./retention_policy/retention_policy_capacity,
./retention_policy/retention_policy_time,
Expand Down
53 changes: 33 additions & 20 deletions waku/waku_archive/driver/builder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import
../../common/databases/db_sqlite,
./sqlite_driver,
./sqlite_driver/migrations as archive_driver_sqlite_migrations,
./queue_driver,
./postgres_driver
./queue_driver

export
sqlite_driver,
queue_driver,
postgres_driver
queue_driver

when defined(postgres):
import ./postgres_driver ## This import adds dependency with an external libpq library
export postgres_driver

proc new*(T: type ArchiveDriver,
url: string,
Expand Down Expand Up @@ -78,22 +80,33 @@ proc new*(T: type ArchiveDriver,
return ok(res.get())

of "postgres":
const MaxNumConns = 5 #TODO: we may need to set that from app args (maybe?)
let res = PostgresDriver.new(url, MaxNumConns, onErrAction)
if res.isErr():
return err("failed to init postgres archive driver: " & res.error)

let driver = res.get()

try:
# The table should exist beforehand.
let newTableRes = waitFor driver.createMessageTable()
if newTableRes.isErr():
return err("error creating table: " & newTableRes.error)
except CatchableError:
return err("exception creating table: " & getCurrentExceptionMsg())

return ok(driver)
when defined(postgres):
const MaxNumConns = 5 #TODO: we may need to set that from app args (maybe?)
let res = PostgresDriver.new(url, MaxNumConns, onErrAction)
if res.isErr():
return err("failed to init postgres archive driver: " & res.error)

let driver = res.get()

try:
# The table should exist beforehand.
let newTableRes = waitFor driver.createMessageTable()
if newTableRes.isErr():
return err("error creating table: " & newTableRes.error)
except CatchableError:
return err("exception creating table: " & getCurrentExceptionMsg())

return ok(driver)

else:
## Error variable to be used if the user is trying to mount Store with a Postgres database
var errPgMsg = "ERROR:"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the error message here (i.e. the builder.nim) can have no knowledge of the encapsulating application, wakunode2, or make commands, etc. It's good that we're trying to be helpful, but at most the driver knows something like: "Postgres has been configured but not been compiled. Check compiler definitions."
I think it's OK to link to the install guide here, even though strictly speaking this module should not even know it's part of nwaku. :)

(Not for this PR, but the correct way to do it is to enumerate the error types here, rather than just returning error strings. That way the application wakunode2 receives an error result when setting up the archive and the application can then log more instructions as it has the context to do so. For now, this will be overkill.)

errPgMsg &= "The POSTGRES flag should be passed to the make command, e.g. "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds me that the message is more like targeting developers not node operators.
It's lucky when it is met.
I would rather say something "This build does not support store with PostgresSQL."

errPgMsg &= "`make POSTGRES=1 wakunode2`, when setting a postgres "
errPgMsg &= "database as the message archiver. Also, bear in mind that the 'libpq' library "
errPgMsg &= "should be installed beforehand. Hint on how to install it: "
errPgMsg &= "https://docs.waku.org/guides/nwaku/build-source#prerequisites"
return err(errPgMsg)

else:
debug "setting up in-memory waku archive driver"
Expand Down