-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalkthrough.md.resolved
More file actions
119 lines (94 loc) · 3.25 KB
/
walkthrough.md.resolved
File metadata and controls
119 lines (94 loc) · 3.25 KB
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
# VoteNow - Implementasi 3 Mode Konsistensi
## Ringkasan Perubahan
VoteNow mendukung **Strong, Weak, dan Eventual Consistency** dalam satu aplikasi. Mode dipilih via query parameter `?consistency=strong|weak|eventual`.
## File yang Dimodifikasi
### Backend
| File | Perubahan |
|------|-----------|
| [consistencyService.js](file:///c:/Akmal/Tugas_Kuliah/VoteNow/backend/services/consistencyService.js) | **[NEW]** Service untuk simulasi 3 node + replication logic |
| [Poll.js](file:///c:/Akmal/Tugas_Kuliah/VoteNow/backend/models/Poll.js) | Tambah field `lastModified` dan `version` |
| [pollController.js](file:///c:/Akmal/Tugas_Kuliah/VoteNow/backend/controllers/pollController.js) | [voteOnPoll](file:///c:/Akmal/Tugas_Kuliah/VoteNow/backend/controllers/pollController.js#160-255) dengan consistency param |
| [server.js](file:///c:/Akmal/Tugas_Kuliah/VoteNow/backend/server.js) | Endpoint `/api/nodes/*` |
---
## API Endpoints Baru
### Vote dengan Consistency Mode
```
POST /api/polls/:id/vote?consistency=strong|weak|eventual
```
### Cek Status Node
```
GET /api/nodes/status?pollId={optional}
```
### Trigger Manual Sync
```
POST /api/nodes/sync
```
---
## Cara Testing
### 1. Start Server
```bash
cd backend
npm run dev
```
### 2. Test Strong Consistency
```powershell
# Vote
Invoke-WebRequest -Uri "http://localhost:5000/api/polls/{pollId}/vote?consistency=strong" `
-Method POST -ContentType "application/json" `
-Body '{"optionIndex": 0, "voterId": "test1"}'
# Cek node status - semua harus sama
Invoke-WebRequest -Uri "http://localhost:5000/api/nodes/status?pollId={pollId}"
```
### 3. Test Weak Consistency
```powershell
# Vote - langsung return, sync async di background
Invoke-WebRequest -Uri "http://localhost:5000/api/polls/{pollId}/vote?consistency=weak" `
-Method POST -ContentType "application/json" `
-Body '{"optionIndex": 0, "voterId": "test2"}'
```
### 4. Test Eventual Consistency
```powershell
# Vote
Invoke-WebRequest -Uri "http://localhost:5000/api/polls/{pollId}/vote?consistency=eventual" `
-Method POST -ContentType "application/json" `
-Body '{"optionIndex": 0, "voterId": "test3"}'
# Tunggu 5 detik, semua node akan sync
Start-Sleep -Seconds 5
Invoke-WebRequest -Uri "http://localhost:5000/api/nodes/status?pollId={pollId}"
```
---
## Perilaku Mode
| Mode | Respons | Node States |
|------|---------|-------------|
| **Strong** | Tunggu semua node sync | Selalu konsisten |
| **Weak** | Langsung return | Mungkin berbeda sementara |
| **Eventual** | Langsung return | Konsisten setelah 5 detik |
---
## Contoh Response
### Vote Response (dengan replicationStatus)
```json
{
"message": "Vote recorded successfully",
"poll": { ... },
"consistency": "strong",
"replicationStatus": {
"mode": "strong",
"allNodesSynced": true,
"nodesUpdated": ["NodeA", "NodeB", "NodeC"]
}
}
```
### Node Status Response
```json
{
"timestamp": 1734054600000,
"backgroundSyncActive": true,
"syncInterval": "5 seconds",
"nodes": {
"NodeA": { "lastSync": 1734054600000, "pollsTracked": 1, "pollState": {...} },
"NodeB": { "lastSync": 1734054600000, "pollsTracked": 1, "pollState": {...} },
"NodeC": { "lastSync": 1734054600000, "pollsTracked": 1, "pollState": {...} }
},
"pollConsistency": { "pollId": "xxx", "isConsistent": true }
}
```