Skip to content

Commit 106a547

Browse files
authored
Refactor workerId/workerPid impl (#5772)
* Remove process_id/process_type, Change to using worker_id/worker_type * Optimize code, remove SwooleG.pid * Fix tests
1 parent e42d6d3 commit 106a547

25 files changed

+166
-189
lines changed

core-tests/src/core/log.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "test_core.h"
22
#include "swoole_file.h"
3+
#include "swoole_process_pool.h"
34
#include <regex>
45
#include <vector>
56

@@ -8,11 +9,11 @@ using namespace swoole;
89
const char *file = "/tmp/swoole_log_test.log";
910

1011
TEST(log, level) {
11-
std::vector<int> processTypes = {SW_PROCESS_MASTER, SW_PROCESS_MANAGER, SW_PROCESS_WORKER, SW_PROCESS_TASKWORKER};
12+
std::vector<int> processTypes = {SW_MASTER, SW_MANAGER, SW_WORKER, SW_TASK_WORKER};
1213

13-
int originType = swoole_get_process_type();
14+
int originType = swoole_get_worker_type();
1415
for (auto iter = processTypes.begin(); iter != processTypes.end(); iter++) {
15-
SwooleG.process_type = *iter;
16+
swoole_set_worker_type(*iter);
1617
sw_logger()->reset();
1718
sw_logger()->set_level(SW_LOG_NOTICE);
1819
sw_logger()->open(file);
@@ -30,7 +31,7 @@ TEST(log, level) {
3031
ASSERT_TRUE(content->contains(SW_STRL("hello notice")));
3132
ASSERT_TRUE(content->contains(SW_STRL("hello warning")));
3233

33-
SwooleG.process_type = originType;
34+
swoole_set_worker_type(originType);
3435
}
3536
}
3637

@@ -89,7 +90,7 @@ TEST(log, date_with_microseconds) {
8990
sw_logger()->close();
9091
unlink(file);
9192

92-
std::regex e("\\[\\S+\\s\\d{2}:\\d{2}:\\d{2}\\<\\.(\\d+)\\>\\s@\\d+\\.\\d+\\]\tWARNING\thello world");
93+
std::regex e("\\[\\S+\\s\\d{2}:\\d{2}:\\d{2}\\<\\.(\\d+)\\>\\s%\\d+\\.\\d+\\]\tWARNING\thello world");
9394
ASSERT_TRUE(std::regex_search(content->value(), e));
9495
}
9596

core-tests/src/server/http.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ static Server *test_proxy_server() {
379379
session_id = req->info.fd;
380380
conn = server->get_connection_by_session_id(session_id);
381381

382-
SwooleG.process_id = server->worker_num;
382+
swoole_set_worker_id(server->worker_num);
383383

384384
llhttp_t parser = {};
385385
llhttp_settings_t settings = {};

core-tests/src/server/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ TEST(server, forward_message) {
17841784
EventData msg;
17851785
SessionId client_fd = req->info.fd;
17861786
Server::task_pack(&msg, &client_fd, sizeof(client_fd));
1787-
EXPECT_TRUE(serv->send_pipe_message(1 - swoole_get_process_id(), &msg));
1787+
EXPECT_TRUE(serv->send_pipe_message(1 - swoole_get_worker_id(), &msg));
17881788
return SW_OK;
17891789
};
17901790

ext-src/swoole_process.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,8 @@ void php_swoole_process_clean() {
620620
}
621621
}
622622
#ifndef SW_THREAD
623-
if (swoole_get_process_type() != SW_PROCESS_USERWORKER) {
624-
swoole_set_process_type(0);
623+
if (swoole_get_worker_type() != SW_USER_WORKER) {
624+
swoole_set_worker_type(0);
625625
}
626626
#endif
627627
}
@@ -661,7 +661,8 @@ int php_swoole_process_start(Worker *process, zval *zobject) {
661661
}
662662

663663
php_swoole_process_clean();
664-
swoole_set_process_id(process->id);
664+
swoole_set_worker_id(process->id);
665+
swoole_set_worker_pid(getpid());
665666
SwooleWG.worker = process;
666667

667668
zend_update_property_long(swoole_process_ce, SW_Z8_OBJ_P(zobject), ZEND_STRL("pid"), process->pid);

ext-src/swoole_process_pool.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static void process_pool_onWorkerStart(ProcessPool *pool, Worker *worker) {
169169
zend_update_property_long(swoole_process_pool_ce, SW_Z8_OBJ_P(zobject), ZEND_STRL("workerPid"), getpid());
170170
zend_update_property_long(swoole_process_pool_ce, SW_Z8_OBJ_P(zobject), ZEND_STRL("workerId"), worker->id);
171171

172-
swoole_set_process_type(SW_PROCESS_WORKER);
172+
swoole_set_worker_type(SW_WORKER);
173173
SwooleG.enable_coroutine = pp->enable_coroutine;
174174

175175
if (pp->onWorkerStart) {
@@ -262,7 +262,7 @@ static void process_pool_onStart(ProcessPool *pool) {
262262
zend_update_property_long(swoole_process_pool_ce, SW_Z8_OBJ_P(zobject), ZEND_STRL("master_pid"), getpid());
263263
zend_update_property_bool(swoole_process_pool_ce, SW_Z8_OBJ_P(zobject), ZEND_STRL("running"), true);
264264

265-
swoole_set_process_type(SW_PROCESS_MASTER);
265+
swoole_set_worker_type(SW_MASTER);
266266
SwooleG.enable_coroutine = false;
267267

268268
if (pp->onStart == nullptr) {

ext-src/swoole_server.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3665,7 +3665,7 @@ static PHP_METHOD(swoole_server, getClientInfo) {
36653665
}
36663666

36673667
#ifdef SW_USE_OPENSSL
3668-
if (conn->ssl_client_cert && conn->ssl_client_cert_pid == swoole_get_process_pid()) {
3668+
if (conn->ssl_client_cert && conn->ssl_client_cert_pid == swoole_get_worker_pid()) {
36693669
add_assoc_stringl(return_value, "ssl_client_cert", conn->ssl_client_cert->str, conn->ssl_client_cert->length);
36703670
}
36713671
#endif

include/swoole.h

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -545,15 +545,6 @@ enum swDNSLookupFlag {
545545

546546
extern thread_local char sw_error[SW_ERROR_MSG_SIZE];
547547

548-
enum swProcessType {
549-
SW_PROCESS_MASTER = 1,
550-
SW_PROCESS_WORKER = 2,
551-
SW_PROCESS_MANAGER = 3,
552-
SW_PROCESS_EVENTWORKER = 2,
553-
SW_PROCESS_TASKWORKER = 4,
554-
SW_PROCESS_USERWORKER = 5,
555-
};
556-
557548
enum swPipeType {
558549
SW_PIPE_WORKER = 0,
559550
SW_PIPE_MASTER = 1,
@@ -685,10 +676,6 @@ struct ThreadGlobal {
685676
uint16_t id;
686677
uint8_t type;
687678
int32_t error;
688-
#ifdef SW_THREAD
689-
uint8_t process_type;
690-
uint32_t process_id;
691-
#endif
692679
String *buffer_stack;
693680
Reactor *reactor;
694681
Timer *timer;
@@ -750,10 +737,7 @@ struct Global {
750737
uchar enable_coroutine : 1;
751738
uchar print_backtrace_on_error : 1;
752739

753-
uint8_t process_type;
754-
uint32_t process_id;
755740
TaskId current_task_id;
756-
pid_t pid;
757741

758742
int signal_fd;
759743
bool signal_alarm;
@@ -845,42 +829,6 @@ static inline void swoole_set_thread_type(uint8_t type) {
845829
SwooleTG.type = type;
846830
}
847831

848-
static inline swoole::WorkerId swoole_get_process_id(void) {
849-
#ifdef SW_THREAD
850-
return SwooleTG.process_id;
851-
#else
852-
return SwooleG.process_id;
853-
#endif
854-
}
855-
856-
static inline pid_t swoole_get_process_pid(void) {
857-
return SwooleG.pid;
858-
}
859-
860-
static inline void swoole_set_process_id(swoole::WorkerId id) {
861-
#ifdef SW_THREAD
862-
SwooleTG.process_id = id;
863-
#else
864-
SwooleG.process_id = id;
865-
#endif
866-
}
867-
868-
static inline void swoole_set_process_type(int type) {
869-
#ifdef SW_THREAD
870-
SwooleTG.process_type = type;
871-
#else
872-
SwooleG.process_type = type;
873-
#endif
874-
}
875-
876-
static inline int swoole_get_process_type(void) {
877-
#ifdef SW_THREAD
878-
return SwooleTG.process_type;
879-
#else
880-
return SwooleG.process_type;
881-
#endif
882-
}
883-
884832
static inline uint32_t swoole_pagesize() {
885833
return SwooleG.pagesize;
886834
}

include/swoole_process_pool.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,31 @@ enum swWorkerStatus {
3737
SW_WORKER_EXIT = 3,
3838
};
3939

40+
enum swWorkerType {
41+
SW_MASTER = 1,
42+
SW_WORKER = 2,
43+
SW_MANAGER = 3,
44+
SW_EVENT_WORKER = 2,
45+
SW_TASK_WORKER = 4,
46+
SW_USER_WORKER = 5,
47+
};
48+
4049
enum swIPCMode {
4150
SW_IPC_NONE = 0,
4251
SW_IPC_UNIXSOCK = 1,
4352
SW_IPC_MSGQUEUE = 2,
4453
SW_IPC_SOCKET = 3,
4554
};
4655

47-
namespace swoole {
56+
SW_API swoole::WorkerId swoole_get_worker_id();
57+
SW_API pid_t swoole_get_worker_pid();
58+
SW_API int swoole_get_worker_type();
59+
SW_API void swoole_set_worker_pid(pid_t pid);
60+
SW_API void swoole_set_worker_id(swoole::WorkerId worker_id);
61+
SW_API void swoole_set_worker_type(int type);
62+
SW_API char swoole_get_worker_symbol();
4863

64+
namespace swoole {
4965
enum WorkerMessageType {
5066
SW_WORKER_MESSAGE_STOP = 1,
5167
};
@@ -62,7 +78,6 @@ struct WorkerStopMessage {
6278
};
6379

6480
class ExitStatus {
65-
private:
6681
pid_t pid_;
6782
int status_;
6883

@@ -107,6 +122,8 @@ struct Worker;
107122

108123
struct WorkerGlobal {
109124
WorkerId id;
125+
uint8_t type;
126+
pid_t pid;
110127
bool shutdown;
111128
bool running;
112129
uint32_t max_request;
@@ -354,11 +371,11 @@ struct ProcessPool {
354371
}
355372

356373
bool is_master() {
357-
return swoole_get_process_type() == SW_PROCESS_MASTER;
374+
return swoole_get_worker_type() == SW_MASTER;
358375
}
359376

360377
bool is_worker() {
361-
return swoole_get_process_type() == SW_PROCESS_WORKER;
378+
return swoole_get_worker_type() == SW_WORKER;
362379
}
363380

364381
/**
@@ -451,6 +468,3 @@ extern SW_THREAD_LOCAL swoole::WorkerGlobal SwooleWG;
451468
static inline swoole::Worker *sw_worker() {
452469
return SwooleWG.worker;
453470
}
454-
455-
SW_API swoole::WorkerId swoole_get_worker_id();
456-
SW_API void swoole_set_worker_id(swoole::WorkerId worker_id);

include/swoole_server.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ class Server {
709709
DISPATCH_RESULT_USERFUNC_FALLBACK = -3,
710710
};
711711

712+
// deprecated, will be removed in the next minor version
712713
enum HookType {
713714
HOOK_MASTER_START,
714715
HOOK_MASTER_TIMER,
@@ -1168,7 +1169,7 @@ class Server {
11681169
}
11691170

11701171
EventData *get_task_result() {
1171-
return &(task_results[swoole_get_process_id()]);
1172+
return &(task_results[swoole_get_worker_id()]);
11721173
}
11731174

11741175
WorkerId get_task_src_worker_id(EventData *task) {
@@ -1288,7 +1289,7 @@ class Server {
12881289
}
12891290

12901291
bool if_forward_message(Session *session) {
1291-
return session->reactor_id != swoole_get_process_id();
1292+
return session->reactor_id != swoole_get_worker_id();
12921293
}
12931294

12941295
Worker *get_worker(uint16_t worker_id);
@@ -1328,27 +1329,27 @@ class Server {
13281329
}
13291330

13301331
bool is_master() {
1331-
return swoole_get_process_type() == SW_PROCESS_MASTER;
1332+
return swoole_get_worker_type() == SW_MASTER;
13321333
}
13331334

13341335
bool is_worker() {
1335-
return swoole_get_process_type() == SW_PROCESS_EVENTWORKER;
1336+
return swoole_get_worker_type() == SW_EVENT_WORKER;
13361337
}
13371338

13381339
bool is_event_worker() {
13391340
return is_worker();
13401341
}
13411342

13421343
bool is_task_worker() {
1343-
return swoole_get_process_type() == SW_PROCESS_TASKWORKER;
1344+
return swoole_get_worker_type() == SW_TASK_WORKER;
13441345
}
13451346

13461347
bool is_manager() {
1347-
return swoole_get_process_type() == SW_PROCESS_MANAGER;
1348+
return swoole_get_worker_type() == SW_MANAGER;
13481349
}
13491350

13501351
bool is_user_worker() {
1351-
return swoole_get_process_type() == SW_PROCESS_USERWORKER;
1352+
return swoole_get_worker_type() == SW_USER_WORKER;
13521353
}
13531354

13541355
bool is_worker_thread() {

src/core/base.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,6 @@ void swoole_init(void) {
169169
// random seed
170170
srandom(time(nullptr));
171171

172-
SwooleG.pid = getpid();
173-
174172
if (!SwooleG.logger) {
175173
SwooleG.logger = new Logger();
176174
}
@@ -350,7 +348,6 @@ pid_t swoole_fork(int flags) {
350348

351349
pid_t pid = fork();
352350
if (pid == 0) {
353-
SwooleG.pid = getpid();
354351
if (flags & SW_FORK_DAEMON) {
355352
return pid;
356353
}

0 commit comments

Comments
 (0)