Skip to content

Commit 3529422

Browse files
authored
IndexStorage implementation for postgres (#2954)
* feat: initial implementation * feat: use db migrations * feat: drop_prefix improvements * fix: issues introduced by merge with main * Address PR comments --------- Co-authored-by: kmatasfp <33095685+kmatas@users.noreply.github.com>
1 parent 3794771 commit 3529422

File tree

8 files changed

+574
-3
lines changed

8 files changed

+574
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

golem-worker-executor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ http-body-util = { workspace = true }
6464
humansize = { workspace = true }
6565
humantime-serde = { workspace = true }
6666
hyper = { workspace = true }
67+
include_dir = { workspace = true }
6768
itertools = { workspace = true }
6869
lazy_static = { workspace = true }
6970
log = { workspace = true }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE TABLE index_storage (
2+
namespace TEXT NOT NULL,
3+
key TEXT NOT NULL,
4+
id BIGINT NOT NULL,
5+
value BYTEA NOT NULL,
6+
PRIMARY KEY (namespace, key, id)
7+
);
8+
9+
ALTER TABLE index_storage SET (
10+
autovacuum_vacuum_scale_factor = 0.01,
11+
autovacuum_vacuum_threshold = 1024,
12+
autovacuum_analyze_scale_factor = 0.02,
13+
autovacuum_analyze_threshold = 1024,
14+
autovacuum_vacuum_cost_limit = 2000
15+
);

golem-worker-executor/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use crate::services::{
6565
HasWorkerActivator, HasWorkerService,
6666
};
6767
use crate::storage::indexed::multi_sqlite::MultiSqliteIndexedStorage;
68+
use crate::storage::indexed::postgres::PostgresIndexedStorage;
6869
use crate::storage::indexed::redis::RedisIndexedStorage;
6970
use crate::storage::indexed::sqlite::SqliteIndexedStorage;
7071
use crate::storage::indexed::IndexedStorage;
@@ -391,6 +392,13 @@ pub async fn create_worker_executor_impl<Ctx: WorkerCtx, A: Bootstrap<Ctx> + ?Si
391392
let pool = RedisPool::configured(redis).await?;
392393
Arc::new(RedisIndexedStorage::new(pool.clone()))
393394
}
395+
IndexedStorageConfig::Postgres(postgres) => {
396+
Arc::new(
397+
PostgresIndexedStorage::configured(postgres)
398+
.await
399+
.map_err(|err| anyhow!(err))?,
400+
)
401+
}
394402
IndexedStorageConfig::KVStoreSqlite(_) => {
395403
let sqlite = sqlite
396404
.clone()

golem-worker-executor/src/services/golem_config.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use anyhow::Context;
1616
use figment::providers::{Format, Toml};
1717
use figment::Figment;
1818
use golem_common::config::{
19-
ConfigExample, ConfigLoader, DbSqliteConfig, HasConfigExamples, RedisConfig,
19+
ConfigExample, ConfigLoader, DbPostgresConfig, DbSqliteConfig, HasConfigExamples, RedisConfig,
2020
};
2121
use golem_common::model::base64::Base64;
2222
use golem_common::model::RetryConfig;
@@ -607,6 +607,7 @@ impl SafeDisplay for KeyValueStorageInMemoryConfig {
607607
pub enum IndexedStorageConfig {
608608
KVStoreRedis(IndexedStorageKVStoreRedisConfig),
609609
Redis(RedisConfig),
610+
Postgres(IndexedStoragePostgresConfig),
610611
KVStoreSqlite(IndexedStorageKVStoreSqliteConfig),
611612
KVStoreMultiSqlite(IndexedStorageKVStoreMultiSqliteConfig),
612613
Sqlite(DbSqliteConfig),
@@ -626,6 +627,10 @@ impl SafeDisplay for IndexedStorageConfig {
626627
let _ = writeln!(&mut result, "redis:");
627628
let _ = writeln!(&mut result, "{}", inner.to_safe_string_indented());
628629
}
630+
IndexedStorageConfig::Postgres(inner) => {
631+
let _ = writeln!(&mut result, "postgres:");
632+
let _ = writeln!(&mut result, "{}", inner.to_safe_string_indented());
633+
}
629634
IndexedStorageConfig::KVStoreSqlite(inner) => {
630635
let _ = writeln!(&mut result, "sqlite kv-store:");
631636
let _ = writeln!(&mut result, "{}", inner.to_safe_string_indented());
@@ -704,6 +709,31 @@ impl SafeDisplay for IndexedStorageInMemoryConfig {
704709
}
705710
}
706711

712+
#[derive(Clone, Debug, Serialize, Deserialize)]
713+
pub struct IndexedStoragePostgresConfig {
714+
#[serde(flatten)]
715+
pub postgres: DbPostgresConfig,
716+
#[serde(default = "default_indexed_storage_postgres_drop_prefix_delete_batch_size")]
717+
pub drop_prefix_delete_batch_size: u64,
718+
}
719+
720+
impl SafeDisplay for IndexedStoragePostgresConfig {
721+
fn to_safe_string(&self) -> String {
722+
let mut result = String::new();
723+
let _ = writeln!(&mut result, "{}", self.postgres.to_safe_string_indented());
724+
let _ = writeln!(
725+
&mut result,
726+
"drop prefix delete batch size: {}",
727+
self.drop_prefix_delete_batch_size
728+
);
729+
result
730+
}
731+
}
732+
733+
fn default_indexed_storage_postgres_drop_prefix_delete_batch_size() -> u64 {
734+
1024
735+
}
736+
707737
#[derive(Clone, Debug, Serialize, Deserialize)]
708738
pub struct MemoryConfig {
709739
pub system_memory_override: Option<u64>,

golem-worker-executor/src/storage/indexed/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use golem_common::serialization::{deserialize, serialize};
2222

2323
pub mod memory;
2424
pub mod multi_sqlite;
25+
pub mod postgres;
2526
pub mod redis;
2627
pub mod sqlite;
2728

0 commit comments

Comments
 (0)