提交 4b0528e7 编写于 作者: F freemine

sem_timedwait porting issue

上级 0fb3cf19
...@@ -31,12 +31,16 @@ ...@@ -31,12 +31,16 @@
#include "mnodeAcct.h" #include "mnodeAcct.h"
#include "dnodeTelemetry.h" #include "dnodeTelemetry.h"
static tsem_t tsExitSem; // sem_timedwait is NOT implemented on MacOSX
// thus, we use pthread_mutex_t/pthread_cond_t to simulate
static pthread_mutex_t tsExitLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t tsExitCond = PTHREAD_COND_INITIALIZER;
static volatile int tsExit = 0;
static pthread_t tsTelemetryThread; static pthread_t tsTelemetryThread;
#define TELEMETRY_SERVER "telemetry.taosdata.com" #define TELEMETRY_SERVER "telemetry.taosdata.com"
#define TELEMETRY_PORT 80 #define TELEMETRY_PORT 80
#define REPORT_INTERVAL 86400 #define REPORT_INTERVAL 86400
static void beginObject(SBufferWriter* bw) { static void beginObject(SBufferWriter* bw) {
tbufWriteChar(bw, '{'); tbufWriteChar(bw, '{');
...@@ -172,8 +176,8 @@ static void addMemoryInfo(SBufferWriter* bw) { ...@@ -172,8 +176,8 @@ static void addMemoryInfo(SBufferWriter* bw) {
static void addVersionInfo(SBufferWriter* bw) { static void addVersionInfo(SBufferWriter* bw) {
addStringField(bw, "version", version); addStringField(bw, "version", version);
addStringField(bw, "buildInfo", buildinfo); addStringField(bw, "buildInfo", buildinfo);
addStringField(bw, "gitInfo", gitinfo); addStringField(bw, "gitInfo", gitinfo);
addStringField(bw, "email", tsEmail); addStringField(bw, "email", tsEmail);
} }
static void addRuntimeInfo(SBufferWriter* bw) { static void addRuntimeInfo(SBufferWriter* bw) {
...@@ -248,12 +252,14 @@ static void* telemetryThread(void* param) { ...@@ -248,12 +252,14 @@ static void* telemetryThread(void* param) {
clock_gettime(CLOCK_REALTIME, &end); clock_gettime(CLOCK_REALTIME, &end);
end.tv_sec += 300; // wait 5 minutes before send first report end.tv_sec += 300; // wait 5 minutes before send first report
while (1) { while (!tsExit) {
if (sem_timedwait(&tsExitSem, &end) == 0) { int r = 0;
break; struct timespec ts = end;
} else if (errno != ETIMEDOUT) { pthread_mutex_lock(&tsExitLock);
continue; r = pthread_cond_timedwait(&tsExitCond, &tsExitLock, &ts);
} pthread_mutex_unlock(&tsExitLock);
if (r==0) break;
if (r!=ETIMEDOUT) continue;
if (sdbIsMaster()) { if (sdbIsMaster()) {
sendTelemetryReport(); sendTelemetryReport();
...@@ -269,12 +275,12 @@ static void dnodeGetEmail(char* filepath) { ...@@ -269,12 +275,12 @@ static void dnodeGetEmail(char* filepath) {
if (fd < 0) { if (fd < 0) {
return; return;
} }
if (taosRead(fd, (void *)tsEmail, TSDB_FQDN_LEN) < 0) { if (taosRead(fd, (void *)tsEmail, TSDB_FQDN_LEN) < 0) {
dError("failed to read %d bytes from file %s since %s", TSDB_FQDN_LEN, filepath, strerror(errno)); dError("failed to read %d bytes from file %s since %s", TSDB_FQDN_LEN, filepath, strerror(errno));
} }
taosClose(fd); taosClose(fd);
} }
int32_t dnodeInitTelemetry() { int32_t dnodeInitTelemetry() {
...@@ -282,13 +288,7 @@ int32_t dnodeInitTelemetry() { ...@@ -282,13 +288,7 @@ int32_t dnodeInitTelemetry() {
return 0; return 0;
} }
dnodeGetEmail("/usr/local/taos/email"); dnodeGetEmail("/usr/local/taos/email");
if (tsem_init(&tsExitSem, 0, 0) == -1) {
// just log the error, it is ok for telemetry to fail
dTrace("failed to create semaphore for telemetry, reason:%s", strerror(errno));
return 0;
}
pthread_attr_t attr; pthread_attr_t attr;
pthread_attr_init(&attr); pthread_attr_init(&attr);
...@@ -310,8 +310,11 @@ void dnodeCleanupTelemetry() { ...@@ -310,8 +310,11 @@ void dnodeCleanupTelemetry() {
} }
if (taosCheckPthreadValid(tsTelemetryThread)) { if (taosCheckPthreadValid(tsTelemetryThread)) {
tsem_post(&tsExitSem); pthread_mutex_lock(&tsExitLock);
tsExit = 1;
pthread_cond_signal(&tsExitCond);
pthread_mutex_unlock(&tsExitLock);
pthread_join(tsTelemetryThread, NULL); pthread_join(tsTelemetryThread, NULL);
tsem_destroy(&tsExitSem);
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册