-
Notifications
You must be signed in to change notification settings - Fork 0
/
chsim.zig
257 lines (215 loc) · 9.76 KB
/
chsim.zig
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
const std = @import("std");
const c = @cImport({
@cInclude("raylib.h");
});
const VirtualNode = struct {
id: u32,
parent_id: u32,
position: f32,
name: [5]u8,
ip: [16]u8,
};
const HashRing = struct {
virtual_nodes: std.ArrayList(VirtualNode),
virtual_nodes_per_node: u32,
allocator: std.mem.Allocator,
rng: std.rand.DefaultPrng,
fn init(allocator: std.mem.Allocator, virtual_nodes_per_node: u32) HashRing {
return HashRing{
.virtual_nodes = std.ArrayList(VirtualNode).init(allocator),
.virtual_nodes_per_node = virtual_nodes_per_node,
.allocator = allocator,
.rng = std.rand.DefaultPrng.init(@as(u64, @intCast(std.time.milliTimestamp()))),
};
}
fn deinit(self: *HashRing) void {
self.virtual_nodes.deinit();
}
fn addNode(self: *HashRing) !void {
const parent_id = @as(u32, @intCast(self.virtual_nodes.items.len / self.virtual_nodes_per_node));
var ip: [16]u8 = undefined;
_ = try std.fmt.bufPrint(&ip, "192.168.{d}.{d}", .{ self.rng.random().intRangeAtMost(u8, 101, 255), self.rng.random().intRangeAtMost(u8, 101, 254) });
ip[15] = 0;
var i: u32 = 0;
while (i < self.virtual_nodes_per_node) : (i += 1) {
var name: [5]u8 = undefined;
_ = try std.fmt.bufPrint(&name, "s{d}_{d}", .{ parent_id, i });
name[4] = 0; // Null-terminate the name
const position = @mod(HashRing.hash(&name) + @as(f32, @floatFromInt(i)) * (2 * std.math.pi / @as(f32, @floatFromInt(self.virtual_nodes_per_node))), 2 * std.math.pi);
const virtual_node = VirtualNode{
.id = @as(u32, @intCast(self.virtual_nodes.items.len)),
.parent_id = parent_id,
.position = position,
.name = name,
.ip = ip,
};
try self.virtual_nodes.append(virtual_node);
std.debug.print("Added node: {s} at position {d:.2}\n", .{ virtual_node.name, virtual_node.position });
}
self.sortVirtualNodes();
}
fn removeNode(self: *HashRing) void {
if (self.virtual_nodes.items.len >= self.virtual_nodes_per_node) {
var i: u32 = 0;
while (i < self.virtual_nodes_per_node) : (i += 1) {
_ = self.virtual_nodes.popOrNull();
}
}
self.sortVirtualNodes();
}
fn sortVirtualNodes(self: *HashRing) void {
if (self.virtual_nodes.items.len > 0) {
const items = self.virtual_nodes.items;
var i: usize = 0;
while (i < items.len - 1) : (i += 1) {
var j: usize = 0;
while (j < items.len - i - 1) : (j += 1) {
if (items[j].position > items[j + 1].position) {
const temp = items[j];
items[j] = items[j + 1];
items[j + 1] = temp;
}
}
}
}
}
fn compByPosition(_: void, a: VirtualNode, b: VirtualNode) bool {
return a.position < b.position;
}
fn hash(key: []const u8) f32 {
var h: u32 = 5381;
for (key) |char| {
h = ((h << 5) +% h) +% char;
}
h = h *% 2654435761;
return @as(f32, @floatFromInt(h)) / @as(f32, @floatFromInt(std.math.maxInt(u32))) * 2 * std.math.pi;
}
fn findNode(self: *HashRing, key: []const u8) ?*VirtualNode {
const hash_value = HashRing.hash(key);
std.debug.print("Finding node for key with hash value: {d:.2}\n", .{hash_value});
for (self.virtual_nodes.items) |*node| {
if (node.position >= hash_value) {
std.debug.print("Selected node: {s} at position {d:.2}\n", .{ node.name, node.position });
return node;
}
}
// If we've reached here, the hash_value is greater than all node positions,
// so we wrap around to the first node
if (self.virtual_nodes.items.len > 0) {
std.debug.print("Selected node (wrap-around): {s} at position {d:.2}\n", .{ self.virtual_nodes.items[0].name, self.virtual_nodes.items[0].position });
return &self.virtual_nodes.items[0];
}
return null;
}
};
const RequestState = struct {
active: bool = false,
timer: f32 = 0,
position: f32 = 0, // Changed from u32 to f32
target_node: ?*VirtualNode = null,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
c.InitWindow(1024, 768, "Consistent Hashing Simulation");
defer c.CloseWindow();
c.SetTargetFPS(60);
var hash_ring = HashRing.init(allocator, 3);
defer hash_ring.deinit();
// Add initial nodes
try hash_ring.addNode();
try hash_ring.addNode();
hash_ring.sortVirtualNodes();
var request_state = RequestState{};
while (!c.WindowShouldClose()) {
if (c.IsKeyPressed(c.KEY_A)) {
try hash_ring.addNode();
hash_ring.sortVirtualNodes();
}
if (c.IsKeyPressed(c.KEY_D)) {
hash_ring.removeNode();
}
if (c.IsKeyPressed(c.KEY_SPACE) and !request_state.active) {
request_state.active = true;
request_state.timer = 2.0;
var key_buf: [16]u8 = undefined;
const key = std.fmt.bufPrintZ(&key_buf, "key_{d}", .{hash_ring.rng.random().int(u32)}) catch |err| {
std.debug.print("Error generating key: {}\n", .{err});
continue;
};
request_state.position = HashRing.hash(key); // Use the hashed key position
std.debug.print("Generated request for key {s} at position: {d:.2}\n", .{ key, request_state.position });
request_state.target_node = hash_ring.findNode(key);
if (request_state.target_node) |target| {
std.debug.print("Request at {d:.2} routed to node {s} at position {d:.2}\n", .{ request_state.position, target.name, target.position });
}
}
if (request_state.active) {
request_state.timer -= c.GetFrameTime();
if (request_state.timer <= 0) {
request_state.active = false;
}
}
c.BeginDrawing();
defer c.EndDrawing();
c.ClearBackground(c.BLACK);
// Draw the hash ring
const center_x: f32 = 300;
const center_y: f32 = 384;
const radius: f32 = 250;
c.DrawCircleLines(@as(c_int, @intFromFloat(center_x)), @as(c_int, @intFromFloat(center_y)), radius, c.WHITE);
for (hash_ring.virtual_nodes.items) |vnode| {
const x = center_x + radius * @cos(vnode.position);
const y = center_y + radius * @sin(vnode.position);
c.DrawCircle(@as(c_int, @intFromFloat(x)), @as(c_int, @intFromFloat(y)), 3, c.PURPLE);
const label_x = x + 10 * @cos(vnode.position);
const label_y = y + 10 * @sin(vnode.position);
c.DrawText(&vnode.name, @as(c_int, @intFromFloat(label_x)), @as(c_int, @intFromFloat(label_y)), 10, c.WHITE);
}
// Draw request visualization
if (request_state.active) {
const request_x = center_x + radius * @cos(request_state.position);
const request_y = center_y + radius * @sin(request_state.position);
c.DrawCircle(@as(c_int, @intFromFloat(request_x)), @as(c_int, @intFromFloat(request_y)), 5, c.RED);
if (request_state.target_node) |target| {
// Draw an arc from request to target
var angle = request_state.position;
const arc_color = c.RED;
while (true) : (angle += 0.01) {
if (angle >= 2 * std.math.pi) angle -= 2 * std.math.pi;
const x = center_x + radius * @cos(angle);
const y = center_y + radius * @sin(angle);
c.DrawCircle(@as(c_int, @intFromFloat(x)), @as(c_int, @intFromFloat(y)), 2, arc_color);
if (@abs(angle - target.position) < 0.01 or
(angle > target.position and angle - target.position > std.math.pi)) break;
}
const target_x = center_x + radius * @cos(target.position);
const target_y = center_y + radius * @sin(target.position);
c.DrawCircle(@as(c_int, @intFromFloat(target_x)), @as(c_int, @intFromFloat(target_y)), 5, c.GREEN);
}
}
// Draw table
const table_x: c_int = 600;
const table_y: c_int = 50;
const row_height: c_int = 30;
const col_width: c_int = 200;
c.DrawText("Virtual Node", table_x, table_y, 20, c.WHITE);
c.DrawText("IP (Identifier)", table_x + col_width, table_y, 20, c.WHITE);
for (hash_ring.virtual_nodes.items, 0..) |vnode, i| {
const row_y = @as(c_int, @intCast(table_y + row_height * (@as(c_int, @intCast(i)) + 1)));
const highlight = request_state.active and request_state.target_node == &vnode;
if (highlight) {
c.DrawRectangle(table_x, row_y, col_width * 2, row_height, c.DARKPURPLE);
}
// Draw virtual node name (s0_0, s0_1, etc.)
c.DrawText(&vnode.name, table_x, row_y, 20, c.WHITE);
// Draw IP address
c.DrawText(&vnode.ip, table_x + col_width, row_y, 20, c.WHITE);
}
c.DrawText("Press 'A' to add a node", 10, 10, 20, c.WHITE);
c.DrawText("Press 'D' to delete a node", 10, 40, 20, c.WHITE);
c.DrawText("Press SPACE to send a request", 10, 70, 20, c.WHITE);
c.DrawText(c.TextFormat("Nodes: %d", @as(c_int, @intCast(hash_ring.virtual_nodes.items.len / hash_ring.virtual_nodes_per_node))), 10, 100, 20, c.WHITE);
}
}