kotoba 0.1.3

GP2-based Graph Rewriting Language - ISO GQL-compliant queries, MVCC+Merkle persistence, and distributed execution
Documentation
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! ホスティングサーバー
//!
//! このモジュールはデプロイされたアプリケーションをホストするHTTPサーバーを提供します。
//! WebAssemblyランタイムと統合され、グローバル分散実行を実現します。

use kotoba_core::types::{Result, Value};
use crate::deploy::controller::DeployController;
use crate::deploy::runtime::{DeployRuntime, RuntimeManager};
use crate::deploy::scaling::LoadBalancer;
use crate::deploy::network::NetworkManager;
// use crate::http::server::HttpServer; // 簡易実装では使用しない
// use crate::http::ir::{HttpConfig, ServerConfig, RouteConfig, MiddlewareConfig};

// 簡易HTTPサーバー実装
#[derive(Debug)]
pub struct HttpServer {
    config: HttpConfig,
}

#[derive(Debug)]
pub struct HttpConfig {
    pub host: String,
    pub port: u16,
}

#[derive(Debug)]
pub struct ServerConfig {
    pub host: String,
    pub port: u16,
}

impl HttpConfig {
    pub fn new(config: ServerConfig) -> Self {
        Self {
            host: config.host,
            port: config.port,
        }
    }
}

impl HttpServer {
    pub async fn new(_config: HttpConfig, _mvcc: Arc<dyn std::any::Any>, _merkle: Arc<dyn std::any::Any>, _rewrite_engine: Arc<dyn std::any::Any>) -> Result<Self> {
        Ok(Self {
            config: _config,
        })
    }
}
use std::sync::Arc;
use std::collections::HashMap;
use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use std::time::SystemTime;

/// ホスティングサーバー
pub struct HostingServer {
    /// HTTPサーバー
    http_server: HttpServer,
    /// ランタイムマネージャー
    runtime_manager: RuntimeManager,
    /// ロードバランサー
    load_balancer: Arc<LoadBalancer>,
    /// ネットワークマネージャー
    network_manager: Arc<NetworkManager>,
    /// ホストされたアプリケーション
    hosted_apps: Arc<std::sync::RwLock<HashMap<String, HostedApp>>>,
}

/// ホストされたアプリケーション
#[derive(Debug, Clone)]
pub struct HostedApp {
    /// アプリケーションID
    pub id: String,
    /// デプロイメントID
    pub deployment_id: String,
    /// インスタンスID
    pub instance_id: String,
    /// ドメイン
    pub domain: String,
    /// ポート
    pub port: u16,
    /// 作成時刻
    pub created_at: SystemTime,
    /// 最終アクセス時刻
    pub last_access: SystemTime,
    /// リクエスト数
    pub request_count: u64,
}

impl HostingServer {
    /// 新しいホスティングサーバーを作成
    pub async fn new(
        runtime_manager: RuntimeManager,
        load_balancer: Arc<LoadBalancer>,
        network_manager: Arc<NetworkManager>,
    ) -> Result<Self> {
        // HTTP設定を作成
        let http_config = HttpConfig::new(ServerConfig {
            host: "0.0.0.0".to_string(),
            port: 8080,
        });

        // HTTPサーバーを作成(モック依存関係)
        let mvcc = Arc::new(kotoba_storage::storage::MVCCManager::new());
        let merkle = Arc::new(kotoba_storage::storage::MerkleDAG::new());
        let rewrite_engine = Arc::new(kotoba_rewrite::rewrite::RewriteEngine::new());

        let http_server = HttpServer::new(http_config, Arc::new(()), Arc::new(()), Arc::new(())).await?;

        Ok(Self {
            http_server,
            runtime_manager,
            load_balancer,
            network_manager,
            hosted_apps: Arc::new(std::sync::RwLock::new(HashMap::new())),
        })
    }

    /// アプリケーションをホスト
    pub async fn host_application(
        &self,
        deployment_id: &str,
        instance_id: &str,
        domain: &str,
        port: u16,
    ) -> Result<String> {
        let app_id = format!("app-{}-{}", deployment_id, SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)?
            .as_secs());

        let hosted_app = HostedApp {
            id: app_id.clone(),
            deployment_id: deployment_id.to_string(),
            instance_id: instance_id.to_string(),
            domain: domain.to_string(),
            port,
            created_at: SystemTime::now(),
            last_access: SystemTime::now(),
            request_count: 0,
        };

        self.hosted_apps.write().unwrap().insert(app_id.clone(), hosted_app);

        println!("✅ Application {} hosted on {}:{}", deployment_id, domain, port);
        Ok(app_id)
    }

    /// リクエストを処理
    pub async fn handle_request(
        &self,
        domain: &str,
        path: &str,
        method: &str,
        body: Option<&[u8]>,
    ) -> Result<Vec<u8>> {
        // ドメインからアプリケーションを検索
        let apps = self.hosted_apps.read().unwrap();
        let app = apps.values()
            .find(|a| a.domain == domain)
            .ok_or_else(|| {
                kotoba_core::types::KotobaError::InvalidArgument(format!("No application found for domain {}", domain))
            })?;

        // アクセスを記録
        drop(apps);
        let mut apps = self.hosted_apps.write().unwrap();
        if let Some(app) = apps.get_mut(&app.id) {
            app.last_access = SystemTime::now();
            app.request_count += 1;
        }

        // ランタイムでリクエストを処理 (簡易実装)
        let params = vec![path.len() as i32];
        let result = self.runtime_manager.call(&app.instance_id, "handle_request", &params).await?;

        // レスポンスを構築
        let response = format!(
            "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHello from {} at {}",
            app.deployment_id, path
        );

        Ok(response.into_bytes())
    }

    /// サーバーを開始
    pub async fn start(&self) -> Result<()> {
        let address = format!("{}:{}", "127.0.0.1", 8080);
        println!("Starting Hosting Server on {}", address);

        let listener = TcpListener::bind(&address).await?;
        println!("Hosting Server started successfully");

        // リクエスト処理ループ
        loop {
            let (mut socket, addr) = listener.accept().await?;
            println!("New connection from: {}", addr);

            let hosted_apps = self.hosted_apps.clone();
            let runtime_manager = self.runtime_manager.clone();

            tokio::spawn(async move {
                let mut buffer = [0; 8192];

                loop {
                    let n = match socket.read(&mut buffer).await {
                        Ok(n) if n == 0 => return, // Connection closed
                        Ok(n) => n,
                        Err(e) => {
                            eprintln!("Failed to read from socket: {}", e);
                            return;
                        }
                    };

                    let request = String::from_utf8_lossy(&buffer[..n]);
                    let response = process_http_request(&request, &hosted_apps, &runtime_manager).await;

                    if let Err(e) = socket.write_all(&response).await {
                        eprintln!("Failed to write to socket: {}", e);
                        return;
                    }
                }
            });
        }
    }

    /// ホストされたアプリケーションを取得
    pub fn get_hosted_apps(&self) -> HashMap<String, HostedApp> {
        self.hosted_apps.read().unwrap().clone()
    }

    /// アプリケーションを削除
    pub fn remove_application(&self, app_id: &str) -> Result<()> {
        let mut apps = self.hosted_apps.write().unwrap();
        if apps.remove(app_id).is_some() {
            println!("✅ Application {} removed", app_id);
            Ok(())
        } else {
            Err(kotoba_core::types::KotobaError::InvalidArgument(format!("Application {} not found", app_id)))
        }
    }
}

/// HTTPリクエストを処理
async fn process_http_request(
    request: &str,
    hosted_apps: &Arc<std::sync::RwLock<HashMap<String, HostedApp>>>,
    runtime_manager: &RuntimeManager,
) -> Vec<u8> {
    let lines: Vec<&str> = request.lines().collect();
    if lines.is_empty() {
        return b"HTTP/1.1 400 Bad Request\r\n\r\n".to_vec();
    }

    // リクエストラインをパース
    let request_line = lines[0];
    let parts: Vec<&str> = request_line.split_whitespace().collect();
    if parts.len() < 2 {
        return b"HTTP/1.1 400 Bad Request\r\n\r\n".to_vec();
    }

    let method = parts[0];
    let path = parts[1];

    // Hostヘッダーを探す
    let mut host = "localhost";
    for line in &lines[1..] {
        if line.to_lowercase().starts_with("host:") {
            if let Some(h) = line.split(':').nth(1) {
                host = h.trim();
            }
            break;
        }
    }

    // アプリケーションを検索
    let apps = hosted_apps.read().unwrap();
    let app = apps.values().find(|a| a.domain == host);

    match app {
        Some(app) => {
            // ランタイムでリクエストを処理 (簡易実装)
            let params = vec![path.len() as i32];
            match runtime_manager.call(&app.instance_id, "handle_request", &params).await {
                Ok(_) => {
                    let response = format!(
                        "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHello from {} at {}\r\n",
                        app.deployment_id, path
                    );
                    response.into_bytes()
                }
                Err(e) => {
                    let response = format!(
                        "HTTP/1.1 500 Internal Server Error\r\n\r\nError: {}\r\n",
                        e
                    );
                    response.into_bytes()
                }
            }
        }
        None => {
            b"HTTP/1.1 404 Not Found\r\n\r\nApplication not found\r\n".to_vec()
        }
    }
}

/// ホスティングマネージャー
pub struct HostingManager {
    hosting_server: Arc<HostingServer>,
    network_manager: Arc<NetworkManager>,
}

impl HostingManager {
    /// 新しいマネージャーを作成
    pub fn new(
        hosting_server: Arc<HostingServer>,
        network_manager: Arc<NetworkManager>,
    ) -> Self {
        Self {
            hosting_server,
            network_manager,
        }
    }

    /// デプロイメントをホスト
    pub async fn host_deployment(
        &self,
        deployment_id: &str,
        instance_id: &str,
        domain: &str,
    ) -> Result<String> {
        // 最適なポートを割り当て
        let port = self.find_available_port()?;

        // ネットワーク設定を更新
        self.network_manager.add_domain_to_network(domain, port).await?;

        // アプリケーションをホスト
        self.hosting_server.host_application(deployment_id, instance_id, domain, port).await
    }

    /// 利用可能なポートを探す
    fn find_available_port(&self) -> Result<u16> {
        // 簡易実装:動的に利用可能なポートを探す
        // 実際の実装ではポート管理システムが必要
        Ok(8081) // 固定ポート(本番では動的割り当て)
    }

    /// デプロイメントをアンホスト
    pub fn unhost_deployment(&self, app_id: &str) -> Result<()> {
        self.hosting_server.remove_application(app_id)
    }

    /// ホスティング統計を取得
    pub fn get_hosting_stats(&self) -> HostingStats {
        let apps = self.hosting_server.get_hosted_apps();
        let total_apps = apps.len();
        let total_requests: u64 = apps.values().map(|a| a.request_count).sum();

        HostingStats {
            total_applications: total_apps,
            total_requests,
            active_connections: 0, // 実際の実装でカウント
        }
    }
}

/// ホスティング統計
#[derive(Debug, Clone)]
pub struct HostingStats {
    pub total_applications: usize,
    pub total_requests: u64,
    pub active_connections: u32,
}

/// メインのホスティングサーバー実行関数
pub async fn run_hosting_server_system(
    runtime_manager: RuntimeManager,
    load_balancer: Arc<LoadBalancer>,
    network_manager: Arc<NetworkManager>,
) -> Result<()> {
    let hosting_server = Arc::new(HostingServer::new(
        runtime_manager,
        load_balancer,
        network_manager.clone(),
    ).await?);

    let hosting_manager = HostingManager::new(
        hosting_server.clone(),
        network_manager,
    );

    println!("Kotoba Hosting System initialized");
    println!("Stats: {:?}", hosting_manager.get_hosting_stats());

    // サーバーを開始
    hosting_server.start().await?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::deploy::scaling::LoadBalancingAlgorithm;

    #[test]
    fn test_hosting_server_creation() {
        // モック依存関係を作成
        let controller = Arc::new(DeployController::new(
            Arc::new(crate::execution::QueryExecutor::new()),
            Arc::new(crate::planner::QueryPlanner::new()),
            Arc::new(crate::rewrite::RewriteEngine::new()),
            Arc::new(crate::deploy::scaling::ScalingEngine::new(
                crate::deploy::config::ScalingConfig {
                    min_instances: 1,
                    max_instances: 10,
                    cpu_threshold: 70.0,
                    memory_threshold: 80.0,
                    policy: crate::deploy::config::ScalingPolicy::CpuBased,
                    cooldown_period: 300,
                }
            )),
            Arc::new(crate::deploy::network::NetworkManager::new()),
        ));

        let runtime = Arc::new(DeployRuntime::new(controller));
        let runtime_manager = RuntimeManager::new(runtime);
        let load_balancer = Arc::new(LoadBalancer::new(LoadBalancingAlgorithm::RoundRobin));
        let network_manager = Arc::new(NetworkManager::new());

        // 非同期テストは別途実行
        assert!(true); // 依存関係の作成確認
    }

    #[test]
    fn test_hosting_stats() {
        let stats = HostingStats {
            total_applications: 5,
            total_requests: 1000,
            active_connections: 50,
        };

        assert_eq!(stats.total_applications, 5);
        assert_eq!(stats.total_requests, 1000);
    }
}