Skip to content

Commit cbff5cd

Browse files
committed
Begin expanding funciton parameters instead of taking a function
1 parent cb21b84 commit cbff5cd

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

Diff for: clickhouse-admin/src/clickward.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl From<ClickwardError> for HttpError {
3434
}
3535
}
3636

37-
#[derive(Debug)]
37+
#[derive(Debug, Clone, Copy)]
3838
pub struct Clickward {}
3939

4040
impl Clickward {

Diff for: clickhouse-admin/src/context.rs

+35-13
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use omicron_common::address::CLICKHOUSE_TCP_PORT;
1919
use omicron_common::api::external::Generation;
2020
use oximeter_db::Client as OximeterClient;
2121
use oximeter_db::OXIMETER_VERSION;
22-
use slog::{info, error};
2322
use slog::Logger;
23+
use slog::{error, info};
2424
use std::fs::File;
2525
use std::io::{BufRead, BufReader};
2626
use std::net::SocketAddrV6;
@@ -76,6 +76,7 @@ impl KeeperServerContext {
7676
pub struct ServerContext {
7777
clickhouse_cli: ClickhouseCli,
7878
clickward: Clickward,
79+
7980
oximeter_client: OximeterClient,
8081
log: Logger,
8182
pub generation: Mutex<Option<Generation>>,
@@ -93,6 +94,8 @@ impl ServerContext {
9394

9495
let ip = listen_address.ip();
9596
let address = SocketAddrV6::new(*ip, CLICKHOUSE_TCP_PORT, 0, 0);
97+
// TODO: Instead of creating OximeterClient here, pass along the `address` and create the client
98+
// in self.oximeter_client. This client is not cloneable, copyable by design.
9699
let oximeter_client = OximeterClient::new(address.into(), log);
97100
let clickward = Clickward::new();
98101
let log = log.new(slog::o!("component" => "ServerContext"));
@@ -125,16 +128,16 @@ impl ServerContext {
125128
&self.clickhouse_cli
126129
}
127130

128-
pub fn clickward(&self) -> &Clickward {
129-
&self.clickward
131+
pub fn clickward(&self) -> Clickward {
132+
self.clickward
130133
}
131134

132135
pub fn oximeter_client(&self) -> &OximeterClient {
133136
&self.oximeter_client
134137
}
135138

136-
pub fn log(&self) -> &Logger {
137-
&self.log
139+
pub fn log(&self) -> Logger {
140+
self.log.clone()
138141
}
139142

140143
pub fn generation(&self) -> Option<Generation> {
@@ -145,14 +148,22 @@ impl ServerContext {
145148
pub enum ClickhouseAdminServerRequest {
146149
/// Generates configuration files for a replicated cluster
147150
GenerateConfig {
151+
// TODO: Remove ctx once the generation number is handled
152+
// via a watcher channel
148153
ctx: Arc<ServerContext>,
154+
clickward: Clickward,
155+
log: Logger,
149156
replica_settings: ServerConfigurableSettings,
150157
response: oneshot::Sender<Result<ReplicaConfig, HttpError>>,
151158
},
152159
/// Initiliases the oximeter database on either a single node
153160
/// ClickHouse installation or a replicated cluster.
154161
DbInit {
162+
// TODO: Create oximeter client in a different way
163+
// meanwhile use ctx
164+
// oximeter_client: OximeterClient,
155165
ctx: Arc<ServerContext>,
166+
log: Logger,
156167
replicated: bool,
157168
response: oneshot::Sender<Result<(), HttpError>>,
158169
},
@@ -165,28 +176,35 @@ async fn long_running_ch_admin_server_task(
165176
match request {
166177
ClickhouseAdminServerRequest::GenerateConfig {
167178
ctx,
179+
clickward,
180+
log,
168181
replica_settings,
169182
response,
170183
} => {
171184
// TODO: Remove clone
172-
let result =
173-
generate_config_and_enable_svc(ctx.clone(), replica_settings);
185+
let result = generate_config_and_enable_svc(
186+
ctx.clone(),
187+
clickward,
188+
replica_settings,
189+
);
174190
if let Err(e) = response.send(result) {
175191
error!(
176-
&ctx.log,
192+
&log,
177193
"failed to send value from configuration generation to channel: {e:?}"
178194
);
179195
};
180196
}
181197
ClickhouseAdminServerRequest::DbInit {
198+
// oximeter_client,
182199
ctx,
200+
log,
183201
replicated,
184202
response,
185203
} => {
186-
let result = init_db(ctx.clone(), replicated).await;
204+
let result = init_db(ctx, log.clone(), replicated).await;
187205
if let Err(e) = response.send(result) {
188206
error!(
189-
&ctx.log,
207+
log,
190208
"failed to send value from database initialization to channel: {e:?}"
191209
);
192210
};
@@ -197,7 +215,10 @@ async fn long_running_ch_admin_server_task(
197215

198216
pub fn generate_config_and_enable_svc(
199217
// TODO: Expand ctx into separate items
218+
// TODO: Send the generation number via the watcher channel first before
219+
// removing ctx entirely
200220
ctx: Arc<ServerContext>,
221+
clickward: Clickward,
201222
replica_settings: ServerConfigurableSettings,
202223
) -> Result<ReplicaConfig, HttpError> {
203224
let mut current_generation = ctx.generation.lock().unwrap();
@@ -220,7 +241,7 @@ pub fn generate_config_and_enable_svc(
220241
}
221242
};
222243

223-
let output = ctx.clickward().generate_server_config(replica_settings)?;
244+
let output = clickward.generate_server_config(replica_settings)?;
224245

225246
// We want to update the generation number only if the config file has been
226247
// generated successfully.
@@ -234,11 +255,12 @@ pub fn generate_config_and_enable_svc(
234255
}
235256

236257
pub async fn init_db(
237-
// TODO: Expand ctx into separate items
258+
// TODO: Create oximeter client in a different way
259+
// meanwhile use ctx
238260
ctx: Arc<ServerContext>,
261+
log: Logger,
239262
replicated: bool,
240263
) -> Result<(), HttpError> {
241-
let log = ctx.log();
242264
// Initialize the database only if it was not previously initialized.
243265
// TODO: Migrate schema to newer version without wiping data.
244266
let client = ctx.oximeter_client();

Diff for: clickhouse-admin/src/http_entrypoints.rs

+10
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,15 @@ impl ClickhouseAdminServerApi for ClickhouseAdminServerImpl {
4949
) -> Result<HttpResponseCreated<ReplicaConfig>, HttpError> {
5050
let ctx = rqctx.context();
5151
let replica_settings = body.into_inner();
52+
let clickward = ctx.clickward();
53+
let log = ctx.log();
5254

5355
let (response_tx, response_rx) = oneshot::channel();
5456
ctx.tx
5557
.send_async(ClickhouseAdminServerRequest::GenerateConfig {
5658
ctx: ctx.clone(),
59+
clickward,
60+
log,
5761
replica_settings,
5862
response: response_tx,
5963
})
@@ -118,10 +122,14 @@ impl ClickhouseAdminServerApi for ClickhouseAdminServerImpl {
118122
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
119123
let ctx = rqctx.context();
120124
let replicated = true;
125+
// TODO: Create oximeter client in a different way
126+
// let oximeter_client = ctx.oximeter_client();
127+
let log = ctx.log();
121128
let (response_tx, response_rx) = oneshot::channel();
122129
ctx.tx
123130
.send_async(ClickhouseAdminServerRequest::DbInit {
124131
ctx: ctx.clone(),
132+
log,
125133
replicated,
126134
response: response_tx,
127135
})
@@ -246,10 +254,12 @@ impl ClickhouseAdminSingleApi for ClickhouseAdminSingleImpl {
246254
) -> Result<HttpResponseUpdatedNoContent, HttpError> {
247255
let ctx = rqctx.context();
248256
let replicated = false;
257+
let log = ctx.log();
249258
let (response_tx, response_rx) = oneshot::channel();
250259
ctx.tx
251260
.send_async(ClickhouseAdminServerRequest::DbInit {
252261
ctx: ctx.clone(),
262+
log,
253263
replicated,
254264
response: response_tx,
255265
})

0 commit comments

Comments
 (0)