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
//!
//! Library for producing and consuming Volga messages.
//!
use crate::{Error, Result};
use futures_util::StreamExt;
use serde::{Deserialize, Serialize};

pub mod consumer;
pub mod log_query;
pub mod producer;

type WebSocketStream =
    tokio_tungstenite::WebSocketStream<tokio_native_tls::TlsStream<tokio::net::TcpStream>>;

/// Volga stream persistence
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq)]
pub enum Persistence {
    /// Persist messages to disk
    #[serde(rename = "disk")]
    Disk,
    /// Store messages in RAM
    #[serde(rename = "ram")]
    RAM,
}

/// Format of the data on the volga topic
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq)]
pub enum Format {
    /// JSON format
    #[serde(rename = "json")]
    JSON,
    /// String encoded
    #[serde(rename = "string")]
    String,
}

impl Default for Format {
    fn default() -> Self {
        Self::JSON
    }
}

/// Behavior when topic doesn't exist
#[derive(Clone, Copy, Debug)]
// #[serde(rename_all = "kebab-case")]
pub enum OnNoExists {
    Create(CreateOptions),
    Wait,
    Fail,
}

impl Serialize for OnNoExists {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeMap;
        let mut map = serializer.serialize_map(None)?;
        map.serialize_entry(
            "on-no-exists",
            match self {
                Self::Wait => "wait",
                Self::Fail => "fail",
                Self::Create(_) => "create",
            },
        )?;
        if let Self::Create(create_opts) = self {
            map.serialize_entry("create-options", create_opts)?;
        }

        map.end()
    }
}

/// Local or NAT connection
#[derive(Clone, Copy, Debug, Serialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub enum Location {
    Local,
    ChildSite,
    Parent,
}

/// Volga options for consumers and producers
#[derive(Clone, Copy, Debug, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct CreateOptions {
    /// Number of replicas in the cluster
    pub replication_factor: u32,
    /// Volga stream persistence
    pub persistence: Persistence,

    /// number of 1Mb chunks
    pub num_chunks: usize,

    /// Volga format
    pub format: Format,

    /// Delete topic after call producers and consumers disconnect
    pub ephemeral: bool,
}

impl Default for CreateOptions {
    fn default() -> Self {
        Self {
            replication_factor: 1,
            persistence: Persistence::Disk,
            format: Format::default(),
            num_chunks: 100,
            ephemeral: false,
        }
    }
}

async fn get_binary_response(ws: &mut WebSocketStream) -> Result<Vec<u8>> {
    loop {
        let resp = ws
            .next()
            .await
            .ok_or_else(|| Error::Volga(Some("Expected websocket message".to_string())))??;

        match resp {
            tokio_tungstenite::tungstenite::Message::Pong(_) => continue,
            tokio_tungstenite::tungstenite::Message::Binary(m) => return Ok(m),
            tokio_tungstenite::tungstenite::Message::Close(_) => {
                return Err(Error::Volga(Some("closed".to_string())));
            }
            msg => {
                return Err(Error::Volga(Some(format!(
                    "Unexpected message type: '{}'",
                    msg
                ))))
            }
        }
    }
}

async fn get_ok_volga_response(ws: &mut WebSocketStream) -> Result<()> {
    let msg = get_binary_response(ws).await?;
    let resp: VolgaResponse = serde_json::from_slice(&msg)?;
    tracing::trace!("volga response {:?}", resp);
    match resp.result {
        VolgaResult::Ok => Ok(()),
        VolgaResult::Error => Err(Error::Volga(resp.info)),
    }
}

#[derive(Debug, Deserialize)]
enum VolgaResult {
    #[serde(rename = "ok")]
    Ok,
    #[serde(rename = "error")]
    Error,
}

#[derive(Debug, Deserialize)]
struct VolgaResponse {
    result: VolgaResult,
    info: Option<String>,
}

#[cfg(test)]
mod test {
    #[test]
    fn on_no_exists() {
        let wait = serde_json::to_string(&super::OnNoExists::Wait).unwrap();
        assert_eq!(&wait, r#"{"on-no-exists":"wait"}"#);

        let fail = serde_json::to_string(&super::OnNoExists::Fail).unwrap();
        assert_eq!(&fail, r#"{"on-no-exists":"fail"}"#);

        let create = serde_json::to_string(&super::OnNoExists::Create(Default::default())).unwrap();
        assert_eq!(
            &create,
            r#"{"on-no-exists":"create","create-options":{"replication-factor":1,"persistence":"disk","num-chunks":100,"format":"json","ephemeral":false}}"#
        );
    }
}