|
| 1 | +// Copyright 2023 The SeamDB Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::net::SocketAddr; |
| 16 | +use std::time::Duration; |
| 17 | + |
| 18 | +use anyhow::{anyhow, Result}; |
| 19 | +use clap::Parser; |
| 20 | +use pgwire::tokio::process_socket; |
| 21 | +use seamdb::cluster::{ClusterEnv, EtcdClusterMetaDaemon, EtcdNodeRegistry, NodeId}; |
| 22 | +use seamdb::endpoint::{Endpoint, Params, ServiceUri}; |
| 23 | +use seamdb::log::{KafkaLogFactory, LogManager, MemoryLogFactory}; |
| 24 | +use seamdb::protos::TableDescriptor; |
| 25 | +use seamdb::sql::postgres::PostgresqlHandlerFactory; |
| 26 | +use seamdb::tablet::{TabletClient, TabletNode}; |
| 27 | +use tokio::net::{TcpListener, TcpStream}; |
| 28 | +use tracing::{info, instrument}; |
| 29 | +use tracing_subscriber::prelude::*; |
| 30 | +use tracing_subscriber::{fmt, EnvFilter}; |
| 31 | + |
| 32 | +async fn new_log_manager(uri: ServiceUri<'_>) -> Result<LogManager> { |
| 33 | + match uri.scheme() { |
| 34 | + "memory" => LogManager::new(MemoryLogFactory::new(), &MemoryLogFactory::ENDPOINT, &Params::default()).await, |
| 35 | + "kafka" => LogManager::new(KafkaLogFactory {}, &uri.endpoint(), uri.params()).await, |
| 36 | + scheme => Err(anyhow!("unsupported log schema: {}, supported: memory, kafka", scheme)), |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +#[instrument(skip_all, fields(addr = %addr))] |
| 41 | +async fn serve_connection(factory: PostgresqlHandlerFactory, stream: TcpStream, addr: SocketAddr) { |
| 42 | + match process_socket(stream, None, factory).await { |
| 43 | + Ok(_) => info!("connection terminated"), |
| 44 | + Err(err) => info!("connection terminated: {err}"), |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Parser, Debug)] |
| 49 | +#[command(version, about, long_about = None)] |
| 50 | +pub struct Args { |
| 51 | + /// Meta cluster uri to store cluster wide metadata, e.g. etcd://etcd-cluster/scope. |
| 52 | + #[arg(long = "cluster.uri")] |
| 53 | + cluster_uri: String, |
| 54 | + /// Cluster name. |
| 55 | + #[arg(long = "cluster.name", default_value = "seamdb")] |
| 56 | + cluster_name: String, |
| 57 | + /// Log cluster uri to store WAL logs, e.g. kafka://kafka-cluster. |
| 58 | + #[arg(long = "log.uri")] |
| 59 | + log_uri: String, |
| 60 | + /// Port to serve PostgreSQL compatible SQL statements. |
| 61 | + #[arg(long = "sql.postgresql.port", default_value_t = 5432)] |
| 62 | + pgsql_port: u16, |
| 63 | +} |
| 64 | + |
| 65 | +#[tokio::main] |
| 66 | +async fn main() { |
| 67 | + let (non_blocking, _guard) = tracing_appender::non_blocking(std::io::stdout()); |
| 68 | + |
| 69 | + tracing_subscriber::registry() |
| 70 | + .with(fmt::layer().with_writer(non_blocking).with_level(true).with_file(true).with_line_number(true)) |
| 71 | + .with(EnvFilter::from_default_env()) |
| 72 | + .init(); |
| 73 | + |
| 74 | + let args = Args::parse(); |
| 75 | + let cluster_uri = ServiceUri::parse(&args.cluster_uri).unwrap(); |
| 76 | + let log_uri = ServiceUri::parse(&args.log_uri).unwrap(); |
| 77 | + |
| 78 | + let node_id = NodeId::new_random(); |
| 79 | + info!("Starting node {node_id}"); |
| 80 | + |
| 81 | + let listener = TcpListener::bind(("127.0.0.1", 0)).await.unwrap(); |
| 82 | + let address = format!("http://{}", listener.local_addr().unwrap()); |
| 83 | + let endpoint = Endpoint::try_from(address.as_str()).unwrap(); |
| 84 | + let (nodes, lease) = |
| 85 | + EtcdNodeRegistry::join(cluster_uri.clone(), node_id.clone(), Some(endpoint.to_owned())).await.unwrap(); |
| 86 | + let log_manager = new_log_manager(log_uri).await.unwrap(); |
| 87 | + let cluster_env = ClusterEnv::new(log_manager.into(), nodes).with_replicas(1); |
| 88 | + let mut cluster_meta_handle = |
| 89 | + EtcdClusterMetaDaemon::start(args.cluster_name, cluster_uri.clone(), cluster_env.clone()).await.unwrap(); |
| 90 | + let descriptor_watcher = cluster_meta_handle.watch_descriptor(None).await.unwrap(); |
| 91 | + let deployment_watcher = cluster_meta_handle.watch_deployment(None).await.unwrap(); |
| 92 | + let cluster_env = cluster_env.with_descriptor(descriptor_watcher).with_deployment(deployment_watcher.monitor()); |
| 93 | + let _node = TabletNode::start(node_id, listener, lease, cluster_env.clone()); |
| 94 | + let client = TabletClient::new(cluster_env).scope(TableDescriptor::POSTGRESQL_DIALECT_PREFIX); |
| 95 | + tokio::time::sleep(Duration::from_secs(20)).await; |
| 96 | + |
| 97 | + let factory = PostgresqlHandlerFactory::new(client); |
| 98 | + let listener = TcpListener::bind(format!("0.0.0.0:{}", args.pgsql_port)).await.unwrap(); |
| 99 | + info!("Listening on {} ...", listener.local_addr().unwrap()); |
| 100 | + loop { |
| 101 | + let (stream, addr) = listener.accept().await.unwrap(); |
| 102 | + tokio::spawn(serve_connection(factory.clone(), stream, addr)); |
| 103 | + } |
| 104 | +} |
0 commit comments