-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_insert_mysql.rs
158 lines (104 loc) · 3.52 KB
/
batch_insert_mysql.rs
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
use tokio_sky::BatchProcessor;
#[tokio::main]
async fn main() {
let producer_factory = || Prod;
let producer_concurrency = 3;
let producer_router = RouterType::RoundRobin;
let producer_buffer_pool = 100;
let proc_factory = || Layer1Process;
let proc_concurrency = 3;
let proc_router = RouterType::Partition;
let proc_buffer_size = 10;
// Mysql Config
let database_url = "...";
let pool = mysql_async::Pool::new(database_url);
let batcher_factory = || MysqlBatcher::new(pool);
let batcher_concurrency = 2;
let batcher_buffer_size = 10;
let batcher_batch_size = 10;
let batcher_batch_timeout: BATCH_TIMEOUT;
// / processor-1 \
// / \
// producer-1 / -----> MysqlBatcher[fullname]
// producer-2 ---- processor-2
// producer-3 \ -----> MysqlBatcher[fullname]
// \ /
// \ processor-3 /
let safe_shutdown =
run_topology_1_with_batcher(
producer_factory,
producer_concurrency,
producer_router,
producer_buffer_pool,
proc_factory,
proc_concurrency,
proc_router,
proc_buffer_size,
batcher_factory,
batcher_concurrency,
batcher_buffer_size,
batcher_batch_size,
batcher_batch_timeout
);
// Safe Shutdown from (Producer) to (Layer_X_Processor)
safe_shutdown.send(());
}
struct Prod;
#[async_trait]
impl Producer<User> for Prod {
async fn init(&mut self) {}
async fn terminate(&mut self) {}
async fn drain(&mut self, _buffer: VecDeque<User>) {}
async fn fill_buffer(&mut self, buffer_size: usize) -> Result<VecDeque<User>, Terminate> {
Ok((0..buffer_size)
.into_iter()
.map(|i| {
User {
age: buffer_size % 14,
fullname: format!("fname_lname")
}
})
.collect::<VecDeque<User>>())
}
}
struct Layer1Process;
#[async_trait]
impl Processor<User, User> for Layer1Process {
async fn init(&mut self) {}
async fn terminate(&mut self) {}
async fn handle_message(&mut self, msg: User) -> ProcResult<User> {
// ordering - partitioning based on 'fullname'
let pk = msg.fullname.clone();
// Dispatch to Batcher
ProcResult::Dispatch(msg, pk)
}
}
struct MysqlBatcher {
pool: Pool
}
impl MysqlBatcher {
pub fn new(pool: Pool) -> MysqlBatcher {
MysqlBatcher { pool }
}
}
#[async_trait]
impl BatchProcessor<User> for MysqlBatcher {
async fn init(&mut self) { }
async fn terminate(&mut self) { }
async fn drain(&mut self, batch: Vec<User>) { }
async fn handle_batch(&mut self, batch: Vec<User>) -> Result<(), BatcherTerminate<User>> {
let conn = pool.get_conn().await.unwrap();
r"INSERT INTO user (age, fullname)
VALUES (:age, :fullname)"
.with(batch.iter().map(|user| params! {
"age" => user.customer_id,
"fullname" => user.fullname.as_ref()
}))
.batch(&mut conn)
.await?;
}
}
struct User {
age: u8,
fullname: Strnig
}