1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::convert::TryFrom;
use std::sync::Arc;
use tracing::debug;
use once_cell::sync::OnceCell;
use fluvio_socket::{SharedMultiplexerSocket, MultiplexerSocket};
use fluvio_future::task::run_block_on;
use fluvio_future::net::DomainConnector;
use semver::Version;
use crate::config::ConfigFile;
use crate::admin::FluvioAdmin;
use crate::TopicProducer;
use crate::PartitionConsumer;
use crate::FluvioError;
use crate::FluvioConfig;
use crate::spu::SpuPool;
use crate::sockets::{ClientConfig, Versions, SerialFrame, VersionedSerialSocket};
pub struct Fluvio {
socket: SharedMultiplexerSocket,
config: Arc<ClientConfig>,
versions: Versions,
spu_pool: OnceCell<Arc<SpuPool>>,
}
impl Fluvio {
pub async fn connect() -> Result<Self, FluvioError> {
let config_file = ConfigFile::load_default_or_new()?;
let cluster_config = config_file.config().current_cluster()?;
Self::connect_with_config(cluster_config).await
}
pub async fn connect_with_config(config: &FluvioConfig) -> Result<Self, FluvioError> {
let connector = DomainConnector::try_from(config.tls.clone())?;
let config = ClientConfig::new(&config.endpoint, connector);
let inner_client = config.connect().await?;
debug!("connected to cluster at: {}", inner_client.config().addr());
let (socket, config, versions) = inner_client.split();
check_platform_compatible(versions.platform_version())?;
let socket = MultiplexerSocket::shared(socket);
let spu_pool = OnceCell::new();
Ok(Self {
socket,
config,
versions,
spu_pool,
})
}
fn spu_pool(&self) -> Result<Arc<SpuPool>, FluvioError> {
self.spu_pool
.get_or_try_init(|| -> Result<Arc<SpuPool>, FluvioError> {
let pool = run_block_on(SpuPool::start(self.config.clone(), self.socket.clone()));
Ok(Arc::new(pool?))
})
.map(|pool| pool.clone())
}
pub async fn topic_producer<S: Into<String>>(
&self,
topic: S,
) -> Result<TopicProducer, FluvioError> {
let topic = topic.into();
debug!(topic = &*topic, "Creating producer");
Ok(TopicProducer::new(topic, self.spu_pool()?))
}
pub async fn partition_consumer<S: Into<String>>(
&self,
topic: S,
partition: i32,
) -> Result<PartitionConsumer, FluvioError> {
let topic = topic.into();
debug!(topic = &*topic, "Creating consumer");
Ok(PartitionConsumer::new(topic, partition, self.spu_pool()?))
}
pub async fn admin(&self) -> FluvioAdmin {
FluvioAdmin::new(self.create_serial_client().await)
}
pub fn platform_version(&self) -> &semver::Version {
self.versions.platform_version()
}
async fn create_serial_client(&self) -> VersionedSerialSocket {
VersionedSerialSocket::new(
self.socket.clone(),
self.config.clone(),
self.versions.clone(),
)
}
}
fn check_platform_compatible(cluster_version: &Version) -> Result<(), FluvioError> {
let client_minimum_version = Version::parse(crate::MINIMUM_PLATFORM_VERSION)
.expect("MINIMUM_PLATFORM_VERSION must be semver");
if *cluster_version < client_minimum_version {
return Err(FluvioError::MinimumPlatformVersion {
cluster_version: cluster_version.clone(),
client_minimum_version,
});
}
Ok(())
}