-
Notifications
You must be signed in to change notification settings - Fork 0
/
defs.h
248 lines (181 loc) · 5.49 KB
/
defs.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
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
// Copyright (C) 2016, 2017 Alexey Khrabrov, Bogdan Simion
//
// Distributed under the terms of the GNU General Public License.
//
// This file is part of Assignment 3, CSC469, Fall 2017.
//
// This is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this file. If not, see <http://www.gnu.org/licenses/>.
// Definition of the communication protocol
#ifndef _DEFS_H_
#define _DEFS_H_
#include <stdint.h>
// Message types
typedef enum {
MSG_NONE = 0,
// Locating key-value server for a given key
MSG_LOCATE_REQ,
MSG_LOCATE_RESP,
// GET/PUT operations
MSG_OPERATION_REQ,
MSG_OPERATION_RESP,
// Control requests (for failure detection and recovery purposes) serviced by the metadata server
// NOTE: mserver control requests do not require a response
MSG_MSERVER_CTRL_REQ,
// Control requests (for failure detection and recovery purposes) serviced by a key-value server
MSG_SERVER_CTRL_REQ,
MSG_SERVER_CTRL_RESP,
MSG_TYPE_MAX,
// "packed" enum means that it has the least possible (hence platform-independent) size, 1 byte in this case
} __attribute__((packed)) msg_type;
// For logging purposes
__attribute__((unused))// to suppress possible "unused variable" warnings
static const char *msg_type_str[MSG_TYPE_MAX] = {
"NONE",
"LOCATE request",
"LOCATE response",
"OPERATION request",
"OPERATION response",
"MSERVER CTRL request",
"SERVER CTRL request",
"SERVER CTRL response"
};
// A "magic number" in the message header (for checking consistency)
#define HDR_MAGIC 0x7B
// Maximum length of a message
#define MAX_MSG_LEN 2048
// A common header for all messages
typedef struct _msg_hdr {
char magic;
msg_type type;
uint16_t length;
// "packed" struct means there is no padding between the fields (so that the layout is platform-independent)
} __attribute__((packed)) msg_hdr;
// We use 128-bit (16-byte) MD5 hash as a key
#define KEY_SIZE 16
// "Locate" request: get server address for a particular key
typedef struct _locate_request {
msg_hdr hdr;
char key[KEY_SIZE];
} __attribute__((packed)) locate_request;
typedef struct _locate_response {
msg_hdr hdr;
uint16_t port;
char host_name[];
} __attribute__((packed)) locate_response;
// Key-value server request - GET or PUT operation
// Operation types
typedef enum {
// Useful for testing communication between clients and servers
// Also used as an "end of sequence" message when sending a set of keys during UPDATE-PRIMARY
OP_NOOP,
OP_GET,
OP_PUT,
OP_TYPE_MAX
} __attribute__((packed)) op_type;
__attribute__((unused))
static const char *op_type_str[OP_TYPE_MAX] = {
"NOOP",
"GET",
"PUT"
};
// Possible results of an operation
typedef enum {
SUCCESS,
SERVER_FAILURE,
KEY_NOT_FOUND,
OUT_OF_SPACE,// not enough memory to store an item
OP_STATUS_MAX
} __attribute__((packed)) op_status;
__attribute__((unused))
static const char *op_status_str[OP_STATUS_MAX] = {
"Success",
"Server failure",
"Key not found",
"Out of space"
};
typedef struct _operation_request {
msg_hdr hdr;
char key[KEY_SIZE];
op_type type;
char value[];
} __attribute__((packed)) operation_request;
typedef struct _operation_response {
msg_hdr hdr;
op_status status;
char value[];
} __attribute__((packed)) operation_response;
// Control requests serviced by the metadata server
// Request types (as described in the assignment handout)
typedef enum {
HEARTBEAT,// for failure detection
UPDATED_PRIMARY,
UPDATE_PRIMARY_FAILED,
UPDATED_SECONDARY,
UPDATE_SECONDARY_FAILED,
MSERVER_CTRLREQ_TYPE_MAX
} __attribute__((packed)) mserver_ctrlreq_type;
__attribute__((unused))
static const char *mserver_ctrlreq_type_str[MSERVER_CTRLREQ_TYPE_MAX] = {
"HEARTBEAT",
"UPDATED-PRIMARY",
"UPDATE-PRIMARY failed",
"UPDATED-SECONDARY",
"UPDATE-SECONDARY failed"
};
typedef struct _mserver_ctrl_request {
msg_hdr hdr;
mserver_ctrlreq_type type;
uint16_t server_id;
} __attribute__((packed)) mserver_ctrl_request;
// Control requests serviced by key-value servers
// Request types (as described in the assignment handout)
typedef enum {
SET_SECONDARY,
UPDATE_PRIMARY,
UPDATE_SECONDARY,
SWITCH_PRIMARY,
SHUTDOWN,// for gracefully terminating the servers
SERVER_CTRLREQ_TYPE_MAX
} __attribute__((packed)) server_ctrlreq_type;
__attribute__((unused))
static const char *server_ctrlreq_type_str[SERVER_CTRLREQ_TYPE_MAX] = {
"SET-SECONDARY",
"UPDATE-PRIMARY",
"UPDATE-SECONDARY",
"SWITCH-PRIMARY",
"SHUTDOWN"
};
// Request status
typedef enum {
CTRLREQ_SUCCESS,
CTRLREQ_FAILURE,
SERVER_CTRLREQ_STATUS_MAX
} __attribute__((packed)) server_ctrlreq_status;
__attribute__((unused))
static const char *server_ctrlreq_status_str[SERVER_CTRLREQ_STATUS_MAX] = {
"Success",
"Failure"
};
typedef struct _server_ctrl_request {
msg_hdr hdr;
server_ctrlreq_type type;
// Server location (for {SET|UPDATE}_{PRIMARY|SECONDARY} requests)
uint16_t port;
char host_name[];
} __attribute__((packed)) server_ctrl_request;
typedef struct _server_ctrl_response {
msg_hdr hdr;
server_ctrlreq_status status;
} __attribute__((packed)) server_ctrl_response;
#endif// _DEFS_H_