Skip to content

Commit 6379c5f

Browse files
feat: Integrated Java KV interface
1 parent 1778957 commit 6379c5f

13 files changed

Lines changed: 284 additions & 25 deletions

File tree

scripts/deploy_cluster/1.install_remote_env.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ def os_system(command):
2424
# make ../install tar.gz
2525
os_system_sure("rm -rf install.tar.gz")
2626
CRAC_INSTALL_DIR = "/usr/jdk_crac"
27-
os_system_sure(f"cp -r {CRAC_INSTALL_DIR} ../install/inner/jdk_crac")
27+
28+
# 判断一下 ../install/inner/jdk_crac 存不存在,如果不存在则 cp, 存在则跳过
29+
dst = "../install/inner/jdk_crac"
30+
if not os.path.exists(dst):
31+
os_system(f"cp -r {CRAC_INSTALL_DIR} {dst}")
32+
else:
33+
print(f"{dst} 已存在,跳过复制。")
34+
35+
# os_system_sure(f"cp -r {CRAC_INSTALL_DIR} ../install/inner/jdk_crac")
2836
os_system_sure("tar -czvf install.tar.gz -C ../install .")
2937

3038
def deploy_to_nodes():
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
nodes:
2-
9:
3-
addr: 192.168.31.9:2500
2+
4:
3+
addr: 192.168.31.109:2500
44
spec:
55
- meta
66
- master
7-
10:
8-
addr: 192.168.31.240:2500
7+
11:
8+
addr: 192.168.31.138:2500
99
spec:
1010
- meta
1111
- worker

src/general/m_appmeta_manager/http.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ async fn call_app_fn(Path((app, func)): Path<(String, String)>, body: String) ->
8484
}
8585
}
8686

87+
// KV DEBUG
8788
async fn upload_app(mut multipart: Multipart) -> Response {
8889
tracing::debug!("upload_app called");
8990
// only worker can upload app

src/general/m_appmeta_manager/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ impl<'de> Deserialize<'de> for FnMetaYaml {
138138
.ok_or_else(|| serde::de::Error::custom("not a map"))?;
139139
// let calls = map.remove("calls").ok_or_else(|| serde::de::Error::missing_field("calls"))?;
140140
let mut calls = vec![];
141+
142+
// KV DEBUG
141143
fn parse_http_call<'de, D: Deserializer<'de>>(
142144
map: &serde_yaml::Value,
143145
) -> Result<HttpCall, D::Error> {
@@ -695,6 +697,7 @@ impl AppMetaManager {
695697
// let appdir = self.fs_layer.concat_app_dir(app);
696698
let appmeta = self.fs_layer.read_app_meta(tmpapp).await?;
697699

700+
// KV DEBUG
698701
// TODO: 2.check project dir
699702
// 3. if java, take snapshot
700703
if let AppType::Jar = appmeta.app_type {
@@ -736,6 +739,8 @@ impl AppMetaManager {
736739
.await
737740
.is_some())
738741
}
742+
743+
// KV DEBUG
739744
pub async fn app_uploaded(&self, appname: String, data: Bytes) -> WSResult<()> {
740745
// 1. tmpapp name & dir
741746
// TODO: fobidden tmpapp public access
@@ -777,6 +782,7 @@ impl AppMetaManager {
777782
};
778783

779784
// 3. check meta
785+
tracing::debug!("begin check meta");
780786
let res = self.construct_tmp_app(&tmpapp).await;
781787
let appmeta = match res {
782788
Err(e) => {

src/general/m_os/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ impl OperatingSystem {
111111
pub fn app_path(&self, app: &str) -> PathBuf {
112112
self.view.appmeta_manager().fs_layer.concat_app_dir(app)
113113
}
114+
115+
// KV DEBUG
114116
pub fn start_process(&self, p: OsProcessType) -> process::Child {
115117
let (mut binding, log_file) = match p {
116118
OsProcessType::JavaApp(app) => {

src/general/network/proto_src/kv.proto

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,25 @@ message KvPairs{
4141

4242
message KvResponse{
4343
message KvResponse{
44-
repeated KvPair kvs=1;
44+
repeated KvPair kvs=1;
4545
}
4646
oneof resp {
4747
KvResponse common_resp=1;
4848
uint32 lock_id=2;
4949
}
5050
}
5151

52+
message KvResponses{
53+
repeated KvResponse responses=1;
54+
}
55+
5256
message KvRequests{
5357
string app=1;
5458
string func=2;
5559
repeated KvRequest requests=3;
5660
int64 prev_kv_opeid=4;
5761
}
5862

59-
message KvResponses{
60-
repeated KvResponse responses=1;
61-
}
62-
6363
// message MetaKvRequest{
6464
// KvRequest request=1;
6565
// }

src/general/network/rpc_model.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,15 @@ pub async fn call<Req: ReqMsg>(
8787
// register the call back
8888
let (wait_tx, wait_rx) = oneshot::channel();
8989
let next_task = NEXT_TASK_ID.fetch_add(1, Ordering::SeqCst);
90+
tracing::debug!("insert into CALL_MAP next_task:{:?}", next_task.clone());
9091
let _ = CALL_MAP.write().insert(next_task, wait_tx);
92+
tracing::debug!("insert after CALL_MAP.write(): {:?}", CALL_MAP.write());
9193

9294
// send the request
9395
let mut buf = BytesMut::with_capacity(req.encoded_len() + 8);
9496
buf.put_i32(req.encoded_len() as i32);
9597
buf.put_i32(next_task as i32);
98+
buf.put_i32(2);
9699
req.encode(&mut buf).unwrap();
97100

98101
tracing::debug!("send request: {:?} with len: {}", req, buf.len() - 8);
@@ -122,15 +125,16 @@ pub async fn call<Req: ReqMsg>(
122125
// Disconnected,
123126
// }
124127

125-
struct ConnState {
128+
#[derive(Debug)]
129+
pub struct ConnState {
126130
/// record the waiters
127131
// Connecting(Vec<oneshot::Sender<tokio::sync::mpsc::Sender<Vec<u8>>>>),
128-
tx: tokio::sync::mpsc::Sender<Vec<u8>>,
132+
pub tx: tokio::sync::mpsc::Sender<Vec<u8>>,
129133
}
130134

131135
lazy_static! {
132136
/// This is an example for using doc comment attributes
133-
static ref CONN_MAP: RwLock<HashMap<HashValue,ConnState>> = RwLock::new(HashMap::new());
137+
pub static ref CONN_MAP: RwLock<HashMap<HashValue,ConnState>> = RwLock::new(HashMap::new());
134138

135139
static ref CALL_MAP: RwLock<HashMap<u32,oneshot::Sender<Vec<u8>>>> = RwLock::new(HashMap::new());
136140

@@ -147,13 +151,19 @@ async fn listen_task<R: RpcCustom>(socket: tokio::net::UnixStream) {
147151
let Some((conn, rx)) =
148152
listen_task_ext::verify_remote::<R>(&mut sockrx, &mut len, &mut buf).await
149153
else {
150-
tracing::debug!("verify failed");
154+
tracing::warn!("verify failed");
151155
return;
152156
};
153157

158+
tracing::debug!("verify_remote 结束");
159+
154160
listen_task_ext::spawn_send_loop(rx, socktx);
155161

162+
tracing::debug!("spawn_send_loop 结束");
163+
156164
listen_task_ext::read_loop::<R>(conn, &mut sockrx, &mut len, &mut buf).await;
165+
166+
tracing::debug!("read_loop 结束");
157167
}
158168

159169
pub(super) mod listen_task_ext {
@@ -172,8 +182,8 @@ pub(super) mod listen_task_ext {
172182

173183
pub(super) async fn verify_remote<R: RpcCustom>(
174184
sockrx: &mut OwnedReadHalf,
175-
len: &mut usize,
176-
buf: &mut [u8],
185+
len: &mut usize, // 0
186+
buf: &mut [u8], // 0
177187
) -> Option<(HashValue, Receiver<Vec<u8>>)> {
178188
async fn verify_remote_inner<R: RpcCustom>(
179189
sockrx: &mut OwnedReadHalf,
@@ -188,25 +198,33 @@ pub(super) mod listen_task_ext {
188198

189199
let verify_msg_len = consume_i32(0, buf, len);
190200

201+
tracing::debug!("len: {}, verify_msg_len: {}", len, verify_msg_len);
202+
191203
// println!("waiting for verify msg {}", verify_msg_len);
192204
if !wait_for_len(sockrx, len, verify_msg_len, buf).await {
193205
tracing::warn!("failed to read verify msg");
194206
return None;
195207
}
196208
// println!("wait done");
197209

210+
tracing::debug!("wait_for_len 完成");
211+
198212
let Some(id) = R::verify(&buf[4..4 + verify_msg_len]).await else {
199-
tracing::warn!("verify failed");
213+
tracing::warn!("verify failed in verify_remote_inner");
200214
return None;
201215
};
202216
let (tx, rx) = tokio::sync::mpsc::channel(10);
203217

218+
// 确定一下为什么 conn_map 里面有上一次连接 id, 需要找这个 conn_map 在哪里都被调用了
204219
let mut write_conn_map = CONN_MAP.write();
220+
tracing::debug!("write_conn_map: {:?}", write_conn_map);
205221
if write_conn_map.contains_key(&id) {
206222
tracing::warn!("conflict conn id: {:?}", id);
207223
return None;
208224
}
209225
let _ = write_conn_map.insert(id.clone(), ConnState { tx });
226+
tracing::debug!("insert into CALL_MAP id:{:?}", id.clone());
227+
tracing::debug!("insert after CALL_MAP.write(): {:?}", write_conn_map);
210228

211229
// println!("verify success");
212230
Some((id, rx))
@@ -230,6 +248,7 @@ pub(super) mod listen_task_ext {
230248
*len = 0;
231249
let mut offset = 0;
232250
loop {
251+
233252
let (msg_len, msg_id, taskid) = {
234253
let buf = &mut buf[offset..];
235254
if !wait_for_len(socket, len, 9, buf).await {
@@ -243,6 +262,8 @@ pub(super) mod listen_task_ext {
243262
consume_i32(5, buf, len) as u32,
244263
)
245264
};
265+
266+
tracing::debug!("2 len: {}, msg_len: {}, msg_id: {}, taskid: {}", len, msg_len, msg_id, taskid);
246267

247268
{
248269
if buf.len() < offset + msg_len {
@@ -269,12 +290,15 @@ pub(super) mod listen_task_ext {
269290
};
270291

271292
let msg = buf[..msg_len].to_vec();
293+
tracing::debug!("msg: {:?}", msg);
272294
cb.send(msg).unwrap();
273295
}
274296

275297
// update the buf meta
276298
offset += msg_len;
277299
*len -= msg_len;
300+
301+
tracing::debug!("1 len: {}, msg_len: {}, msg_id: {}, taskid: {}", len, msg_len, msg_id, taskid);
278302
}
279303

280304
// match socket.read(buf).await {
@@ -331,6 +355,7 @@ pub(super) mod listen_task_ext {
331355
return false;
332356
}
333357
// println!("recv: {:?}", buf[..n]);
358+
tracing::debug!("len += {}", n);
334359
*len += n;
335360
}
336361
Err(e) => {

src/worker/func/shared/java.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99

1010
use super::process::PID;
1111

12+
// KV DEBUG
1213
pub(super) fn cold_start(app: &str, os: &OperatingSystem) -> WSResult<process::Child> {
1314
tracing::debug!("java cold start {}", app);
1415
let p = os.start_process(OsProcessType::JavaApp(app.to_owned()));

src/worker/func/shared/process.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ impl ProcessInstance {
147147
true
148148
}
149149

150+
// KV DEBUG
150151
pub async fn wait_for_verify(&self) -> proc_proto::AppStarted {
151152
if let Some(v) = self.state.0.read().0.as_connected() {
152153
return v.clone();

src/worker/func/shared/process_instance_man_related.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use std::time::Duration;
33
use tokio::process::Command;
44

55
use crate::{
6-
general::m_appmeta_manager::AppType,
6+
general::{
7+
m_appmeta_manager::{AppType},
8+
network::rpc_model::{self, HashValue},
9+
},
710
result::{WSResult, WsFuncError},
811
worker::func::{
912
m_instance_manager::{EachAppCache, InstanceManager},
@@ -15,6 +18,7 @@ use super::{process::ProcessInstance, SharedInstance};
1518

1619
impl InstanceManager {
1720
pub async fn update_checkpoint(&self, app_name: &str, restart: bool) -> WSResult<()> {
21+
tracing::debug!("start update_checkpoint");
1822
async fn debug_port_left() {
1923
tracing::debug!("debug port left");
2024
// only for test
@@ -40,6 +44,7 @@ impl InstanceManager {
4044
.into());
4145
};
4246
// state 2 connecting, make others wait
47+
tracing::debug!("state 2 connecting, make others wait");
4348
{
4449
proc_ins.before_checkpoint();
4550
tokio::time::sleep(Duration::from_secs(3)).await;
@@ -51,6 +56,11 @@ impl InstanceManager {
5156
AppType::Jar => java::take_snapshot(app_name, self.view.os()).await,
5257
AppType::Wasm => unreachable!(),
5358
}
59+
60+
// 打完快照手动 close 一下
61+
tracing::debug!("打完快照手动 close 一下, 移除CONN_MAP中的残余 app");
62+
rpc_model::close_conn(&HashValue::Str(app_name.to_string().clone()));
63+
5464
}
5565
// recover by criu
5666
tokio::time::sleep(Duration::from_secs(3)).await;
@@ -80,6 +90,7 @@ impl InstanceManager {
8090
Ok(())
8191
}
8292

93+
// KV DEBUG
8394
pub async fn make_checkpoint_for_app(&self, app: &str) -> WSResult<()> {
8495
tracing::debug!("make checkpoint for app: {}", app);
8596
let p = self.get_process_instance(&AppType::Jar, app);

0 commit comments

Comments
 (0)