Skip to content

Commit 5572701

Browse files
committed
Automatically prepend needed localhost:127 to Derby JDBC connection strings
1 parent c504c3b commit 5572701

26 files changed

Lines changed: 151 additions & 94 deletions

Cargo.lock

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.nix

Lines changed: 33 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ tracing = "0.1"
3434

3535
[patch."https://github.com/stackabletech/operator-rs.git"]
3636
# stackable-operator = { path = "../operator-rs/crates/stackable-operator" }
37-
# stackable-operator = { git = "https://github.com/stackabletech//operator-rs.git", branch = "main" }
37+
stackable-operator = { git = "https://github.com/stackabletech//operator-rs.git", branch = "feat/derby-host-part" }

crate-hashes.json

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

rust/operator-binary/src/crd/authentication.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ deepStorage:
259259
configMapName: druid-hdfs
260260
directory: /druid
261261
metadataDatabase:
262-
derby:
263-
location: "//localhost:1527/var/druid/metadata.db"
262+
derby: {}
264263
zookeeperConfigMapName: zk-config-map
265264
"#;
266265

rust/operator-binary/src/crd/database.rs

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use std::ops::Deref;
2-
31
use serde::{Deserialize, Serialize};
42
use stackable_operator::{
53
database_connections::{
4+
self, TemplatingMechanism,
65
databases::{
76
derby::DerbyConnection, mysql::MysqlConnection, postgresql::PostgresqlConnection,
87
},
9-
drivers::jdbc::JdbcDatabaseConnection,
8+
drivers::jdbc::{JdbcDatabaseConnection, JdbcDatabaseConnectionDetails},
109
},
1110
schemars::{self, JsonSchema},
1211
};
@@ -47,14 +46,74 @@ impl MetadataDatabaseConnection {
4746
}
4847
}
4948

50-
impl Deref for MetadataDatabaseConnection {
51-
type Target = dyn JdbcDatabaseConnection;
52-
53-
fn deref(&self) -> &Self::Target {
49+
impl JdbcDatabaseConnection for MetadataDatabaseConnection {
50+
/// We do *not* implement [`std::ops::Deref`]` for [`MetadataDatabaseConnection`], as we need
51+
/// some special handling for Derby.
52+
fn jdbc_connection_details_with_templating(
53+
&self,
54+
unique_database_name: &str,
55+
templating_mechanism: &TemplatingMechanism,
56+
) -> Result<JdbcDatabaseConnectionDetails, database_connections::Error> {
5457
match self {
55-
Self::Postgresql(p) => p,
56-
Self::Mysql(m) => m,
57-
Self::Derby(d) => d,
58+
Self::Postgresql(p) => p.jdbc_connection_details_with_templating(
59+
unique_database_name,
60+
templating_mechanism,
61+
),
62+
Self::Mysql(m) => m.jdbc_connection_details_with_templating(
63+
unique_database_name,
64+
templating_mechanism,
65+
),
66+
Self::Derby(d) => {
67+
// According to the [Druid docs](https://druid.apache.org/docs/latest/design/metadata-storage/#derby)
68+
// we should configure something like
69+
// `jdbc:derby://localhost:1527//opt/var/druid_state/derby;create=true`
70+
// instead of the usual `jdbc:derby:/opt/var/druid_state/derby;create=true`.
71+
//
72+
// It looks like Druid always starts Derby at `localhost:1527`, regardless of what we configure here,
73+
// so we can hardcode it here.
74+
d.jdbc_connection_details_with_host_part(unique_database_name, "localhost:1527")
75+
}
5876
}
5977
}
6078
}
79+
80+
#[cfg(test)]
81+
mod tests {
82+
use rstest::rstest;
83+
use stackable_operator::utils::yaml_from_str_singleton_map;
84+
85+
use super::*;
86+
87+
#[rstest]
88+
#[case::postgres(
89+
"postgresql:
90+
host: druid-postgresql
91+
database: druid
92+
credentialsSecretName: druid-credentials",
93+
"jdbc:postgresql://druid-postgresql:5432/druid"
94+
)]
95+
#[case::derby(
96+
"derby: {}",
97+
"jdbc:derby://localhost:1527//tmp/derby/METADATA/derby.db;create=true"
98+
)]
99+
#[case::derby_custom_location(
100+
"derby:
101+
location: /user/provided.db",
102+
"jdbc:derby://localhost:1527//user/provided.db;create=true"
103+
)]
104+
fn test_connection_url(
105+
#[case] database_connection_yaml: &str,
106+
#[case] expected_connection_url: &str,
107+
) {
108+
let database_connection: MetadataDatabaseConnection =
109+
yaml_from_str_singleton_map(database_connection_yaml).expect("invalid YAML");
110+
111+
let jdbc_connection_details = database_connection
112+
.jdbc_connection_details("METADATA")
113+
.expect("failed to get JDBC connection details");
114+
assert_eq!(
115+
jdbc_connection_details.connection_url.as_str(),
116+
expected_connection_url
117+
);
118+
}
119+
}

rust/operator-binary/src/crd/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,15 @@ impl v1alpha1::DruidCluster {
796796
}
797797
}
798798

799+
#[cfg(test)]
800+
impl stackable_operator::versioned::test_utils::RoundtripTestData for v1alpha1::DruidClusterSpec {
801+
fn roundtrip_test_data() -> Vec<Self> {
802+
todo!(
803+
"Return some test data according to https://stackabletech.github.io/operator-rs/stackable_versioned/test_utils/trait.RoundtripTestData.html"
804+
)
805+
}
806+
}
807+
799808
#[derive(
800809
Clone,
801810
Debug,

rust/operator-binary/src/crd/tls.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ deepStorage:
4141
configMapName: druid-hdfs
4242
directory: /druid
4343
metadataDatabase:
44-
derby:
45-
location: "//localhost:1527/var/druid/metadata.db"
44+
derby: {}
4645
zookeeperConfigMapName: zk-config-map
4746
"#;
4847

rust/operator-binary/src/druid_controller.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use stackable_operator::{
3232
},
3333
constants::RESTART_CONTROLLER_ENABLED_LABEL,
3434
crd::s3,
35+
database_connections::drivers::jdbc::JdbcDatabaseConnection as _,
3536
k8s_openapi::{
3637
DeepMerge,
3738
api::{

tests/templates/kuttl/authorizer/04-install-druid.yaml.j2

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ spec:
2828
configMapName: druid-hdfs
2929
directory: /druid
3030
metadataDatabase:
31-
derby:
32-
location: "//localhost:1527/var/druid/metadata.db"
31+
derby: {}
3332
{% if lookup('env', 'VECTOR_AGGREGATOR') %}
3433
vectorAggregatorConfigMapName: vector-aggregator-discovery
3534
{% endif %}

0 commit comments

Comments
 (0)