-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_balancer.h
121 lines (107 loc) · 3.91 KB
/
load_balancer.h
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
/* Copyright 2023 <Cretu Mihnea Tudor> */
#ifndef LOAD_BALANCER_H_
#define LOAD_BALANCER_H_
#include "server.h"
#define NUMBER_OF_REPLICAS 3
typedef struct load_balancer {
int servers_count;
int capacity;
server_memory **servers;
} load_balancer;
/**
* init_load_balancer() - initializes the memory for a new load balancer and its fields and
* returns a pointer to it
*
* Return: pointer to the load balancer struct
*/
load_balancer *init_load_balancer();
/**
* free_load_balancer() - frees the memory of every field that is related to the
* load balancer (servers, hashring)
*
* @arg1: Load balancer to free
*/
void free_load_balancer(load_balancer *main);
/**
* load_store() - Stores the key-value pair inside the system.
* @arg1: Load balancer which distributes the work.
* @arg2: Key represented as a string.
* @arg3: Value represented as a string.
* @arg4: This function will RETURN via this parameter
* the server ID which stores the object.
*
* The load balancer will use Consistent Hashing to distribute the
* load across the servers. The chosen server ID will be returned
* using the last parameter.
*
* Hint:
* Search the hashring associated to the load balancer to find the server where the entry
* should be stored and call the function to store the entry on the respective server.
*
*/
void loader_store(load_balancer *main, char *key, char *value, int *server_id);
/**
* load_retrieve() - Gets a value associated with the key.
* @arg1: Load balancer which distributes the work.
* @arg2: Key represented as a string.
* @arg3: This function will RETURN the server ID
which stores the value via this parameter.
*
* The load balancer will search for the server which should posess the
* value associated to the key. The server will return NULL in case
* the key does NOT exist in the system.
*
* Hint:
* Search the hashring associated to the load balancer to find the server where the entry
* should be stored and call the function to store the entry on the respective server.
*/
char *loader_retrieve(load_balancer *main, char *key, int *server_id);
/**
* load_add_server() - Adds a new server to the system.
* @arg1: Load balancer which distributes the work.
* @arg2: ID of the new server.
*
* The load balancer will generate 3 replica labels and it will
* place them inside the hash ring. The neighbor servers will
* distribute some the objects to the added server.
*
* Hint:
* Resize the servers array to add a new one.
* Add each label in the hashring in its appropiate position.
* Do not forget to resize the hashring and redistribute the objects
* after each label add (the operations will be done 3 times, for each replica).
*/
void loader_add_server(load_balancer *main, int server_id);
/**
* load_remove_server() - Removes a specific server from the system.
* @arg1: Load balancer which distributes the work.
* @arg2: ID of the removed server.
*
* The load balancer will distribute ALL objects stored on the
* removed server and will delete ALL replicas from the hash ring.
*
*/
void loader_remove_server(load_balancer *main, int server_id);
/**
* loader_servers_sort() - Sorts the servers in the hash ring by their hash / id
* @arg1: Load balancer which distributes the work.
*/
void loader_servers_sort(load_balancer *main);
/**
* move_objects_for_remove() - Moves the objects from one server to another
* @arg1: Server from which the objects will be moved
* @arg2: Server to which the objects will be moved
*/
void move_objects_for_remove(server_memory *server_from,
server_memory *server_to);
/**
* get_keys() - Returns the keys from a server
* @arg1: Server from which the keys will be returned
*/
char **get_keys(server_memory *server_from);
/**
* get_values() - Returns the values from a server
* @arg1: Server from which the values will be returned
*/
char **get_values(server_memory *server_from);
#endif /* LOAD_BALANCER_H_ */