-
Notifications
You must be signed in to change notification settings - Fork 17
/
nodes.c
296 lines (288 loc) · 7.94 KB
/
nodes.c
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
/********************************************************************/
/* Copyright (C) SSE-USTC, 2012-2013 */
/* */
/* FILE NAME : nodes.c */
/* PRINCIPAL AUTHOR : Mengning */
/* SUBSYSTEM NAME : cloud */
/* MODULE NAME : nodes */
/* LANGUAGE : C */
/* TARGET ENVIRONMENT : Linux */
/* DATE OF FIRST RELEASE : 2013/1/4 */
/* DESCRIPTION : server nodes admin module */
/********************************************************************/
/*
* Revision log:
*
* Created by Mengning,2013/1/4
* Consistent-Hash support ,by Mengning,2013/1/9
*
*/
#include "nodes.h"
#include "socketwrapper.h"
#include "protocol.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#define MAX_BUF_LEN 1024
/* tell other nodes who am i */
int BroadcastMyself(int fd,char * addr,int port);
/* Init cloud nodes info */
tCluster * InitCluster()
{
tCluster * cluster = (tCluster *)malloc(sizeof(tCluster));
cluster->SumOfNodes = 0;
int i;
for(i=0;i<MAX_NODE_NUM;i++)
{
cluster->nodes[i] = NULL;
}
cluster->pHead = NULL;
cluster->pTail = NULL;
return cluster;
}
/* Destroy Cluster */
int DestroyCluster(tCluster * cluster)
{
int i = 0;
tNode* pNode = cluster->nodes[i];
while(1)
{
i = pNode->hash;
free(pNode);
pNode = cluster->nodes[i];
if(pNode->hash >= MAX_NODE_NUM)
{
break;
}
}
free(cluster);
return 0;
}
int GetHashValue(tCluster * cluster,int SumOfNodes,int * start)
{
int i = 0;
int max=0,prev=0,next=0;
while(i < MAX_NODE_NUM)
{
tNode* pNode = cluster->nodes[i];
if(max < pNode->hash - i)
{
max = pNode->hash - i;
prev = i;
next = pNode->hash;
}
i = pNode->hash;
}
*start = prev;
return (next - prev)%2 == 0 ? (next - prev)/2 + prev : (next - prev - 1)/2 + prev;
}
/* Get cloud nodes info */
tNode * GetNode(tCluster * cluster,int hash)
{
return cluster->nodes[hash];
}
/* Add cloud nodes info */
int AddNode(tCluster * cluster,char * addr,int port)
{
debug("AddNode %s:%d\n",addr,port);
int i = 0;
if(cluster->SumOfNodes >= MAX_NODE_NUM)
{
return -1;
}
tNode* pNode = (tNode*)malloc(sizeof(tNode));
pNode->pNext = NULL;
memcpy(pNode->addr,addr,strlen(addr));
pNode->addr[strlen(addr)] = '\0';
pNode->port = port;
pNode->fd = -1;
if(cluster->SumOfNodes == 0)
{
pNode->hash = MAX_NODE_NUM;
debug("AddNode start:%d hash:%d\n",i,pNode->hash);
for(i=0;i<=MAX_NODE_NUM;i++)
{
cluster->nodes[i] = pNode;
}
cluster->pHead = pNode;
cluster->pTail = pNode;
}
else
{
int start;
pNode->hash = GetHashValue(cluster,cluster->SumOfNodes,&start);
debug("AddNode start:%d hash:%d\n",start,pNode->hash);
for(i=start;i<pNode->hash;i++)
{
cluster->nodes[i] = pNode;
}
(cluster->pTail)->pNext = pNode;
cluster->pTail = pNode;
}
cluster->SumOfNodes ++;
return 0;
}
/* Register cloud nodes info */
int RemoveNode(tCluster * cluster,int hash,char * addr,int port,int fd)
{
/* add here in future */
return -1;
}
/* Register And Load cloud nodes info */
tCluster * RegisterAndLoadClusterNodes(char * addr,int port)
{
int i;
int DataNum = -1;
char Buf[MAX_BUF_LEN] = "\0";
int BufSize = MAX_BUF_LEN;
char szMsg[MAX_BUF_LEN] = "\0";
char ppData[MAX_DATA_NUM][MAX_DATA_LEN] = {0};
tServiceHandler h = -1;
h = OpenRemoteService(IP_ADDR,PORT);
if(addr == NULL)
{
DataNum = 0;
}
else
{
DataNum = 1;
sprintf(ppData[0],"%s %d\0",addr,port);
debug("RegisterAndLoadClusterNodes :%s\n",ppData[0]);
}
FormatDataN(Buf,&BufSize,CTRL_REG_CMD,ppData,DataNum);
SendData(h,Buf,BufSize);
RecvData(h,Buf,&BufSize);
int cmd = -1;
ParseDataN(Buf,BufSize,&cmd,&DataNum,ppData);
if(cmd != CTRL_REG_RSP || DataNum > MAX_DATA_NUM || DataNum < 2)
{
fprintf(stderr,"AddCloudNodes Error,%s:%d\n",__FILE__,__LINE__);
return NULL;
}
tCluster * cluster = InitCluster();
AddClusterNodes(cluster,ppData,DataNum);
CloseRemoteService(h);
debug("RegisterAndLoadClusterNodes END!\n");
return cluster;
}
/* add recved buf(nodes info) to cluster */
int AddClusterNodes(tCluster * cluster,char ppData[MAX_DATA_NUM][MAX_DATA_LEN],int DataNum)
{
if(cluster == NULL || ppData == NULL)
{
fprintf(stderr,"AddClusterNodes Error,%s:%d\n",__FILE__,__LINE__);
return -1;
}
if(DataNum == 0)
{
return 0;
}
int i;
for(i=0;i<DataNum;i++)
{
char addr[MAX_DATA_LEN];
int port;
sscanf(ppData[i],"%s%d",addr,&port);
debug("pasrer %s:%d\n",addr,port);
AddNode(cluster,addr,port);
}
return 0;
}
/* Cluseter(Nodes info) to ppData array */
int ClusterNodesInfo(tCluster * cluster,char ppData[MAX_DATA_NUM][MAX_DATA_LEN],int *NodeNum)
{
if(*NodeNum < cluster->SumOfNodes)
{
fprintf(stderr,"CloudNodesInfo Error,%s:%d\n",__FILE__,__LINE__);
return -1;
}
*NodeNum = cluster->SumOfNodes;
int i = 0;
#if 0
int hash = 0;
while(hash < MAX_NODE_NUM)
{
tNode* pNode = cluster->nodes[hash];
sprintf(ppData[i],"%s %d\0",pNode->addr,pNode->port);
debug("ClusterNodesInfo :%s\n",ppData[i]);
hash = pNode->hash;
i++;
}
#endif
tNode* pNode = cluster->pHead;
while(pNode != NULL)
{
sprintf(ppData[i],"%s %d\0",pNode->addr,pNode->port);
pNode = pNode->pNext;
i++;
}
return 0;
}
int ConnectDataNode(tCluster* cluster,char * addr,int port,char * filename)
{
int i = 0;
while(i < MAX_NODE_NUM)
{
tNode * pNode = (tNode*)GetNode(cluster,i);
if(strcmp(addr,pNode->addr) == 0 && pNode->port == port)
{
pNode->fd = 0;/* don't need socket */
}
else
{
debug("RemoteDBCreate %s:%d\n",pNode->addr,pNode->port);
pNode->fd = RemoteDBCreate(filename,pNode->addr,pNode->port);
if(pNode->fd == -1)
{
return -1;
}
if(strcmp(IP_ADDR,pNode->addr) == 0 && pNode->port == PORT)
{
;
}
else
{
BroadcastMyself(pNode->fd,addr,port);
}
}
i = pNode->hash;
}
return 0;
}
int DisconnectDataNode(tCluster* cluster)
{
int i = 0;
while(i < MAX_NODE_NUM)
{
tNode * pNode = (tNode*)GetNode(cluster,i);
if(pNode->fd > 0)
{
RemoteDBDelete(pNode->fd);
}
i = pNode->hash;
}
return 0;
}
/* tell other nodes who am i */
int BroadcastMyself(int h,char * addr,int port)
{
int DataNum = -1;
char Buf[MAX_BUF_LEN] = "\0";
int BufSize = MAX_BUF_LEN;
char szMsg[MAX_BUF_LEN] = "\0";
char ppData[MAX_DATA_NUM][MAX_DATA_LEN] = {0};
DataNum = 1;
sprintf(ppData[0],"%s %d\0",addr,port);
FormatDataN(Buf,&BufSize,CTRL_REG_CMD,ppData,DataNum);
SendData(h,Buf,BufSize);
RecvData(h,Buf,&BufSize);
int cmd = -1;
ParseDataN(Buf,BufSize,&cmd,&DataNum,ppData);
if(cmd != CTRL_REG_RSP || DataNum > MAX_DATA_NUM || DataNum < 2)
{
fprintf(stderr,"BroadcastMyself Error,%s:%d\n",__FILE__,__LINE__);
return -1;
}
return 0;
}