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
use std::{thread, time};
use std::collections::VecDeque;
use crate::client::circular_queue::CircularQueue;
use super::{Cxn, InsertCommand, TectonicError};
type InsertQueue = CircularQueue<InsertCommand>;
pub struct CxnPool {
cxns: Vec<Cxn>,
host: String,
port: String,
available_workers: VecDeque<usize>,
queue: InsertQueue,
}
impl CxnPool {
pub fn new(n_workers: usize, host: &str, port: &str, capacity: usize) -> Result<Self, TectonicError> {
let mut cxns = vec![];
let mut workers = VecDeque::new();
let queue = CircularQueue::with_capacity(capacity);
for i in 0..n_workers {
let cxn = Cxn::new(host, port)?;
cxns.push(cxn);
workers.push_back(i);
}
Ok(CxnPool{
cxns,
host: host.to_owned(),
port: port.to_owned(),
available_workers: workers,
queue: queue,
})
}
pub fn create_db(&mut self, dbname: &str) -> Result<String, TectonicError> {
info!("Creating db {}", dbname);
self.cmd(&format!("CREATE {}\n", dbname))
}
pub fn cmd(&mut self, command: &str) -> Result<String, TectonicError> {
let n = self.available_workers.pop_front();
let n = match n {
Some(n) => n,
None => {
warn!("Growing CxnPool to {}", self.cxns.len());
self.cxns.push(Cxn::new(&self.host, &self.port)?);
self.cxns.len() - 1
}
};
let result = self.cxns[n].cmd(command);
let ret = match result {
Err(TectonicError::ConnectionError) => {
thread::sleep(time::Duration::from_secs(1));
self.cxns[n] = Cxn::new(&self.host, &self.port)?;
error!("REPLACING CXN");
result
}
_ => result,
};
self.available_workers.push_back(n);
ret
}
pub fn insert(&mut self, cmd: &InsertCommand) -> Result<(), TectonicError> {
let n = self.available_workers.pop_front();
let n = match n {
Some(n) => n,
None => {
self.cxns.push(Cxn::new(&self.host, &self.port)?);
warn!("Growing CxnPool to {}", self.cxns.len());
self.cxns.len() - 1
}
};
for c in cmd.clone().into_string() {
let result = self.cxns[n].cmd(&c);
match result {
Err(TectonicError::ConnectionError) => {
thread::sleep(time::Duration::from_secs(1));
{
self.queue.push(cmd.clone());
}
self.cxns[n] = Cxn::new(&self.host, &self.port)?;
error!("Replacing cxn.");
self.available_workers.push_back(n);
return Err(TectonicError::ConnectionError);
},
Err(TectonicError::DBNotFoundError(ref dbname)) => {
let _ = self.create_db(dbname);
{
self.queue.push(cmd.clone());
}
self.available_workers.push_back(n);
return Err(TectonicError::DBNotFoundError(dbname.to_owned()));
},
Err(e) => {
self.available_workers.push_back(n);
return Err(e)
},
_ => (),
}
}
self.available_workers.push_back(n);
{
let ins_cmd = self.queue.pop();
if let Some(i) = ins_cmd {
let _ = self.insert(&i)?;
}
}
Ok(())
}
}