未验证 提交 cc47fb18 编写于 作者: 何延龙 提交者: GitHub

Merge pull request #59 from goerzh/heartbeat

Heartbeat
......@@ -30,7 +30,7 @@ fi
if test "$PHP_SKYWALKING" != "no"; then
CXXFLAGS+=" -std=c++11 -Isrc/report/deps/boost "
CXXFLAGS+=" -std=c++11 -Isrc/report/deps/boost -DDEBUG"
PHP_REQUIRE_CXX()
KYWALKING_LIBS=`pkg-config --cflags --libs protobuf grpc++ grpc`
......
......@@ -170,6 +170,7 @@ ZEND_BEGIN_MODULE_GLOBALS(skywalking)
zval curl_header_send;
int version;
int header_version;
int register_retry;
ZEND_END_MODULE_GLOBALS(skywalking)
extern ZEND_DECLARE_MODULE_GLOBALS(skywalking);
......
......@@ -80,6 +80,7 @@ PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("skywalking.log_path", "/tmp", PHP_INI_ALL, OnUpdateString, log_path, zend_skywalking_globals, skywalking_globals)
STD_PHP_INI_ENTRY("skywalking.grpc", "127.0.0.1:11800", PHP_INI_ALL, OnUpdateString, grpc, zend_skywalking_globals, skywalking_globals)
STD_PHP_INI_ENTRY("skywalking.header_version", "2", PHP_INI_ALL, OnUpdateLong, header_version, zend_skywalking_globals, skywalking_globals)
STD_PHP_INI_ENTRY("skywalking.register_retry", "10", PHP_INI_ALL, OnUpdateLong, register_retry, zend_skywalking_globals, skywalking_globals)
PHP_INI_END()
/* }}} */
......@@ -715,26 +716,23 @@ static char *get_page_request_peer() {
* @return return_type
// */
static char* _get_current_machine_ip(){
char *ip;
ip = (char *) emalloc(sizeof(char) * 100);
bzero(ip, 100);
char *ip;
zval *carrier = NULL;
ip = (char *)emalloc(sizeof(char)*100);
bzero(ip, 100);
carrier = &PG(http_globals)[TRACK_VARS_SERVER];
if (strcasecmp("cli", sapi_module.name) == 0){
strcpy(ip, "127.0.0.1");
}else{
char hname[128];
struct hostent *hent;
gethostname(hname, sizeof(hname));
hent = gethostbyname(hname);
ip = inet_ntoa(*(struct in_addr*)(hent->h_addr_list[0]));
}
if (strcasecmp("cli", sapi_module.name) == 0) {
strcpy(ip, "127.0.0.1");
} else {
char hname[128];
struct hostent *hent;
gethostname(hname, sizeof(hname));
hent = gethostbyname(hname);
if (hent == NULL) {
strcpy(ip, "127.0.0.1");
} else {
ip = inet_ntoa(*(struct in_addr *) (hent->h_addr_list[0]));
}
}
return ip;
}
......@@ -886,7 +884,7 @@ static void module_init() {
}
i++;
} while (application_id == -100000 && i <= 3);
} while (application_id == -100000 && i <= SKYWALKING_G(register_retry));
if (application_id == -100000) {
sky_close = 1;
......@@ -913,7 +911,7 @@ static void module_init() {
sleep(1);
}
i++;
} while (application_instance == -100000 && i <= 3);
} while (application_instance == -100000 && i <= SKYWALKING_G(register_retry));
if (application_instance == -100000) {
......@@ -932,7 +930,12 @@ PHP_MINIT_FUNCTION (skywalking) {
/* If you have INI entries, uncomment these lines
*/
if (SKYWALKING_G(enable)) {
module_init();
if (strcasecmp("cli", sapi_module.name) == 0) {
sky_close = 1;
} else {
module_init();
}
if (sky_close == 1) {
return SUCCESS;
}
......
......@@ -62,7 +62,9 @@ public:
ServiceRegisterMapping reply;
ClientContext context;
#ifdef DEBUG
std::cout << "Register service: "<< boost_uuid << std::endl;
#endif
Status status = stub_->doServiceRegister(&context, request, &reply);
if (status.ok()) {
......@@ -116,11 +118,20 @@ public:
Status status = stub_->doServiceInstanceRegister(&context, request, &reply);
#ifdef DEBUG
if (!status.ok()) {
std::cout << "status code: " << status.error_code() << std::endl;
std::cout << "err message: " << status.error_message() << std::endl;
std::cout << "err detail: " << status.error_details() << std::endl;
}
#endif
if (status.ok()) {
for (int i = 0; i < reply.serviceinstances_size(); i++) {
const KeyIntValuePair &kv = reply.serviceinstances(i);
// std::cout << "Register Instance:"<< std::endl;
// std::cout << kv.key() << ": " << kv.value() << std::endl;
#ifdef DEBUG
std::cout << "Register Instance: "<< kv.key() << ": " << kv.value() << std::endl;
#endif
if (kv.key() == uuid) {
return kv.value();
......
......@@ -46,8 +46,8 @@ OBJS = ../grpc/language-agent-v2/.libs/trace.o ../grpc/language-agent-v2/.libs/t
../grpc/register/.libs/InstancePing-grpc.o ../grpc/common/.libs/common.o ../grpc/common/.libs/common-grpc.o \
../grpc/common/.libs/trace-common.o ../grpc/common/.libs/trace-common-grpc.o
report_client: $(OBJS)
$(CXX) report_client.cpp $^ $(LDFLAGS) -Ideps/boost $(CXXFLAGS) -o $@
report_client: $(OBJS) report_client.cc
$(CXX) $^ $(LDFLAGS) -Ideps/boost $(CXXFLAGS) -o $@
clean:
rm -rf *.o *.pb.cc *.pb.h report_client \
......
......@@ -59,6 +59,8 @@ using grpc::Status;
using grpc::ClientWriter;
using json = nlohmann::json;
using namespace std::chrono;
class GreeterClient {
public:
GreeterClient(std::shared_ptr<Channel> channel)
......@@ -113,14 +115,14 @@ int main(int argc, char **argv) {
if (std::strncmp("-h", argv[i], sizeof(argv[i]) - 1) == 0 ||
std::strncmp("--help", argv[i], sizeof(argv[i]) - 1) == 0) {
std::cout << "report_client grpc log_path" << std::endl;
std::cout << "e.g. report_client 120.0.0.1:11800 /tmp" << std::endl;
std::cout << "e.g. report_client 127.0.0.1:11800 /tmp" << std::endl;
return 0;
}
}
if (argc == 1) {
std::cout << "report_client grpc log_path" << std::endl;
std::cout << "e.g. report_client 120.0.0.1:11800 /tmp" << std::endl;
std::cout << "e.g. report_client 127.0.0.1:11800 /tmp" << std::endl;
return 0;
}
......@@ -130,6 +132,12 @@ int main(int argc, char **argv) {
std::map<int, std::string> instanceUUID;
std::map<int, long> sendTime;
milliseconds guard;
guard = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
);
while (1) {
struct dirent *dir;
......@@ -140,22 +148,24 @@ int main(int argc, char **argv) {
}
// heartbeat
for (auto &i: instancePid) {
struct timeval tv;
gettimeofday(&tv, NULL);
if(tv.tv_sec - sendTime[i.first] > 40) {
kill(instancePid[i.first], 0);
if ((duration_cast<milliseconds>(system_clock::now().time_since_epoch()) - guard) > seconds(60)) {
guard = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
for (auto &i: instancePid) {
struct timeval tv;
gettimeofday(&tv, NULL);
// if (tv.tv_sec - sendTime[i.first] > 40) {
// kill(instancePid[i.first], 0);
// }
sendTime[i.first] = tv.tv_sec;
std::cout << "send heartbeat, instance id: " << i.first << std::endl;
ServiceInstancePingPkg request;
request.set_serviceinstanceid(i.first);
request.set_time(tv.tv_sec * 1000 + tv.tv_usec / 1000);
request.set_serviceinstanceuuid(instanceUUID[i.first]);
greeter.heartbeat(request);
}
sendTime[i.first] = tv.tv_sec;
std::cout << "send heartbeat ..." << std::endl;
ServiceInstancePingPkg request;
request.set_serviceinstanceid(i.first);
request.set_time(tv.tv_sec*1000 + tv.tv_usec/1000);
request.set_serviceinstanceuuid(instanceUUID[i.first]);
greeter.heartbeat(request);
}
while ((dir = readdir(dp)) != NULL) {
......@@ -331,7 +341,7 @@ int main(int argc, char **argv) {
}
closedir(dp);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::this_thread::sleep_for(std::chrono::seconds(1));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册