-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheart.c
More file actions
162 lines (132 loc) · 3.15 KB
/
heart.c
File metadata and controls
162 lines (132 loc) · 3.15 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
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
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/statvfs.h>
#include <pthread.h>
#include <unistd.h>
#include "cs.h"
#include "common.h"
#include "storage.h"
#include "debug.h"
/*
* default value
* can be configured
*/
char meta_server[IP_BUF];
int heart_itval = HEART_ITVAL;
char storage_root[PATH_BUF] = STORAGE_ROOT;
/*
* total and available space (KB) in storage resided fs
*/
int get_space_info(uint32_t *total, uint32_t *avail)
{
struct statvfs stbuf;
if (statvfs(storage_root, &stbuf)) {
perror("statfs error in [get_avail_space]");
return -errno;
}
*total = stbuf.f_blocks * (stbuf.f_bsize / 1024);
*avail = stbuf.f_bavail * (stbuf.f_bsize / 1024);
return 0;
}
/*
* call os_heart with udp protocol
*/
int rpccall_os_heart(os_heart_in *arg, os_heart_out *res, char *ipp)
{
CLIENT *clnt;
clnt = clnt_create(meta_server, OS_PROG, OS_VERS, "udp");
if(!clnt) {
PDEBUG("%s: create client handle failed\n", ipp);
return -EREMOTE;
}
if(os_heart_1(arg, res, clnt) != RPC_SUCCESS) {
PDEBUG(clnt_sperror(clnt, "call rpc os_heart_1 error."));
return -EREMOTE;
}
clnt_destroy(clnt);
return 0;
}
int rpc_os_heart(os_heart_in *arg, os_heart_out *res, char *ipp)
{
int ret = rpccall_os_heart(arg, res, ipp);
if(ret < 0)
return ret;
return res->err;
}
void *heart_state()
{
uint32_t space;
os_heart_in arg;
os_heart_out res;
int ret;
while(1) {
ret = get_space_info(&arg.total, &arg.avail);
PTR_ERR(ret);
PDEBUG("[hear report]: total=%uKB, avail = %uKB\n", arg.total, arg.avail);
rpc_os_heart(&arg, &res, meta_server);
sleep(heart_itval);
}
}
void usage()
{
printf("dnfs-node -s server-ip [-r root-directory] [-i heart-interval]\n");
}
/*
* parse storage node's command line
*/
int parse_storage_cmd(char argc, char *argv[])
{
int i;
int ret;
int srvflag = 0;
for(i = 1; i < argc; i += 2) {
if(!strcmp(argv[i], "-s")) {
if(!is_ipv4(argv[i+1])) {
printf("invaild ip address.\n");
return -1;
}
strcpy(meta_server, argv[i+1]);
srvflag = 1;
}
else if(!strcmp(argv[i], "-r")) {
ret = check_directory(argv[i+1]);
IS_CALL_ERR(ret);
strcpy(storage_root, argv[i+1]);
}
else if(!strcmp(argv[i], "-i")) {
heart_itval = atoi(argv[i+1]);
}
else {
usage();
return -1;
}
}
if(!srvflag) {
usage();
return -1;
}
return 0;
}
int storage_init(int argc, char *argv[])
{
int ret;
/* parse command line */
ret = parse_storage_cmd(argc, argv);
IS_CALL_ERR(ret);
/* check if root exsits? */
ret = check_directory(storage_root);
IS_CALL_ERR(ret);
/* create send heart state thread */
pthread_t tid;
if((ret = pthread_create(&tid, NULL, heart_state, NULL)) != 0) {
perror("pthread_create error in [osd_init]");
return -1;
}
/* print initial information */
printf("metadata server: %s\n", meta_server);
printf("storage root dierctory: %s\n", storage_root);
printf("heart interval: %d seconds\n", heart_itval);
printf("Storage node initialized successfully!\n\n");
return 0;
}