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
//
// Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
use crate::error::Error;
use pravega_client_shared::*;
use pravega_wire_protocol::commands::{Command, EventCommand};
use pravega_wire_protocol::wire_commands::Replies;

use tokio::sync::oneshot;
use tracing::warn;
use uuid::Uuid;

#[derive(Debug)]
pub(crate) enum Incoming {
    AppendEvent(PendingEvent),
    ServerReply(ServerReply),
    Reconnect(WriterInfo),
    Reset(ScopedSegment),
    Close(),
}

#[derive(new, Debug)]
pub(crate) struct ServerReply {
    pub(crate) segment: ScopedSegment,
    pub(crate) reply: Replies,
}

#[derive(new, Debug)]
pub(crate) struct WriterInfo {
    pub(crate) segment: ScopedSegment,
    pub(crate) connection_id: Option<Uuid>,
    pub(crate) writer_id: WriterId,
}

#[derive(Debug)]
pub(crate) enum RoutingInfo {
    RoutingKey(Option<String>),
    Segment(ScopedSegment),
}

#[derive(Debug)]
pub(crate) struct PendingEvent {
    pub(crate) routing_info: RoutingInfo,
    pub(crate) data: Vec<u8>,
    pub(crate) conditional_offset: Option<i64>,
    pub(crate) oneshot_sender: oneshot::Sender<Result<(), Error>>,
    pub(crate) flush_oneshot_sender: Option<oneshot::Sender<Result<(), Error>>>,
}

impl PendingEvent {
    pub(crate) const MAX_WRITE_SIZE: usize = 8 * 1024 * 1024 + 8;
    pub(crate) fn new(
        routing_info: RoutingInfo,
        data: Vec<u8>,
        conditional_offset: Option<i64>,
        oneshot_sender: oneshot::Sender<Result<(), Error>>,
        flush_oneshot_sender: Option<oneshot::Sender<Result<(), Error>>>,
    ) -> Option<Self> {
        Some(PendingEvent {
            routing_info,
            data,
            conditional_offset,
            oneshot_sender,
            flush_oneshot_sender,
        })
    }

    pub(crate) fn with_header_flush(
        routing_info: RoutingInfo,
        data: Vec<u8>,
        conditional_offset: Option<i64>,
        oneshot_sender: oneshot::Sender<Result<(), Error>>,
        flush_oneshot_sender: Option<oneshot::Sender<Result<(), Error>>>,
    ) -> Option<PendingEvent> {
        let cmd = EventCommand { data };
        match cmd.write_fields() {
            Ok(data) => PendingEvent::new(
                routing_info,
                data,
                conditional_offset,
                oneshot_sender,
                flush_oneshot_sender,
            ),
            Err(e) => {
                warn!("failed to serialize event to event command, sending this error back to caller");
                oneshot_sender
                    .send(Err(Error::InternalFailure {
                        msg: format!("Failed to serialize event: {:?}", e),
                    }))
                    .expect("send error to caller");
                None
            }
        }
    }

    pub(crate) fn with_header(
        routing_info: RoutingInfo,
        data: Vec<u8>,
        conditional_offset: Option<i64>,
        oneshot_sender: oneshot::Sender<Result<(), Error>>,
    ) -> Option<PendingEvent> {
        PendingEvent::with_header_flush(routing_info, data, conditional_offset, oneshot_sender, None)
    }

    pub(crate) fn without_header_flush(
        routing_info: RoutingInfo,
        data: Vec<u8>,
        conditional_offset: Option<i64>,
        oneshot_sender: oneshot::Sender<Result<(), Error>>,
        flush_oneshot_sender: Option<oneshot::Sender<Result<(), Error>>>,
    ) -> Option<PendingEvent> {
        PendingEvent::new(
            routing_info,
            data,
            conditional_offset,
            oneshot_sender,
            flush_oneshot_sender,
        )
    }

    pub(crate) fn without_header(
        routing_info: RoutingInfo,
        data: Vec<u8>,
        conditional_offset: Option<i64>,
        oneshot_sender: oneshot::Sender<Result<(), Error>>,
    ) -> Option<PendingEvent> {
        PendingEvent::without_header_flush(routing_info, data, conditional_offset, oneshot_sender, None)
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.data.is_empty()
    }
}