Skip to content

Commit

Permalink
review fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Sadokhov committed Jul 18, 2023
1 parent 4912f5b commit f1e9d07
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 66 deletions.
28 changes: 14 additions & 14 deletions net/net-socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const char *unix_socket_path(const char *directory, const char *owner, uint16_t
static struct sockaddr_un path;

if (snprintf(path.sun_path, sizeof(path.sun_path), "%s/%s/%d", directory, owner, port) >= sizeof(path.sun_path)) {
vkprintf(4, "Too long UNIX socket path: \"%s/%s/%d\": %zu bytes exceeds\n", directory, owner, port, sizeof(path.sun_path));
kprintf("Too long UNIX socket path: \"%s/%s/%d\": %zu bytes exceeds\n", directory, owner, port, sizeof(path.sun_path));
return NULL;
}

Expand All @@ -68,13 +68,13 @@ int prepare_unix_socket_directory(const char *directory, const char *username, c

struct passwd *passwd = getpwnam(username);
if (!passwd) {
vkprintf(4, "Cannot getpwnam() for %s: %s\n", username, strerror(errno));
vkprintf(1, "Cannot getpwnam() for %s: %s\n", username, strerror(errno));
return -1;
}

struct group *group = getgrnam(groupname);
if (!group) {
vkprintf(4, "Cannot getgrnam() for %s: %s\n", groupname, strerror(errno));
vkprintf(1, "Cannot getgrnam() for %s: %s\n", groupname, strerror(errno));
return -1;
}

Expand All @@ -84,7 +84,7 @@ int prepare_unix_socket_directory(const char *directory, const char *username, c
vkprintf(4, "Trying to create UNIX socket directory: \"%s\"\n", directory);
const mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
if (mkdir(directory, dirmode) == -1 && errno != EEXIST) {
vkprintf(4, "Cannot mkdir() UNIX socket directory: \"%s\": %s\n", directory, strerror(errno));
vkprintf(1, "Cannot mkdir() UNIX socket directory: \"%s\": %s\n", directory, strerror(errno));
return -1;
}
}
Expand All @@ -93,15 +93,15 @@ int prepare_unix_socket_directory(const char *directory, const char *username, c
}

if (dirfd == -1) {
vkprintf(4, "Cannot open() UNIX socket directory: \"%s\": %s\n", directory, strerror(errno));
vkprintf(1, "Cannot open() UNIX socket directory: \"%s\": %s\n", directory, strerror(errno));
return -1;
}

int groupdirfd = openat(dirfd, group->gr_name, O_DIRECTORY);
if (groupdirfd == -1) {
if (errno == ENOENT) {
if (mkdirat(dirfd, group->gr_name, S_IRUSR) == -1 && errno != EEXIST) {
vkprintf(4, "Cannot mkdirat() UNIX socket group directory: \"%s/%s\": %s\n", directory, groupname, strerror(errno));
vkprintf(1, "Cannot mkdirat() UNIX socket group directory: \"%s/%s\": %s\n", directory, groupname, strerror(errno));
close(dirfd);
return -1;
}
Expand All @@ -110,23 +110,23 @@ int prepare_unix_socket_directory(const char *directory, const char *username, c
}

if (groupdirfd == -1) {
vkprintf(4, "Cannot openat() UNIX socket group directory: \"%s/%s\": %s\n", directory, groupname, strerror(errno));
vkprintf(1, "Cannot openat() UNIX socket group directory: \"%s/%s\": %s\n", directory, groupname, strerror(errno));
close(dirfd);
return -1;
}
}

struct stat groupdirst;
if (fstat(groupdirfd, &groupdirst) == -1) {
vkprintf(4, "Cannot fstatat() UNIX socket group directory: \"%s/%s\": %s\n", directory, groupname, strerror(errno));
vkprintf(1, "Cannot fstatat() UNIX socket group directory: \"%s/%s\": %s\n", directory, groupname, strerror(errno));
close(groupdirfd);
close(dirfd);
return -1;
}

if (groupdirst.st_uid != passwd->pw_uid || groupdirst.st_gid != group->gr_gid) {
if (fchown(groupdirfd, passwd->pw_uid, group->gr_gid)) {
vkprintf(4, "Cannot fchown() UNIX socket group directory: \"%s/%s\": %s\n", directory, groupname, strerror(errno));
vkprintf(1, "Cannot fchown() UNIX socket group directory: \"%s/%s\": %s\n", directory, groupname, strerror(errno));
close(groupdirfd);
close(dirfd);
return -1;
Expand All @@ -136,7 +136,7 @@ int prepare_unix_socket_directory(const char *directory, const char *username, c
const mode_t groupdirmode = S_IRWXU | S_IRGRP | S_IXGRP;
if ((groupdirst.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) != groupdirmode) {
if (fchmod(groupdirfd, groupdirmode) == -1) {
vkprintf(4, "Cannot fchmod() UNIX socket owner directory: \"%s/%s\": %s\n", directory, username, strerror(errno));
vkprintf(1, "Cannot fchmod() UNIX socket owner directory: \"%s/%s\": %s\n", directory, username, strerror(errno));
close(groupdirfd);
close(dirfd);
return -1;
Expand Down Expand Up @@ -257,7 +257,7 @@ int server_socket(int port, struct in_addr in_addr, int backlog, int mode) {
addr.sin_port = htons(port);
addr.sin_addr = in_addr;
if (bind(sfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
vkprintf(4, "bind(%s:%d): %s\n", inet_ntoa(in_addr), port, strerror(errno));
kprintf("bind(%s:%d): %s\n", inet_ntoa(in_addr), port, strerror(errno));
close(sfd);
return -1;
}
Expand All @@ -270,7 +270,7 @@ int server_socket(int port, struct in_addr in_addr, int backlog, int mode) {
addr.sin6_addr = in6addr_any;

if (bind(sfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
vkprintf(4, "bind(%s:%d): %s\n", inet_ntoa(in_addr), port, strerror(errno));
kprintf("bind(%s:%d): %s\n", inet_ntoa(in_addr), port, strerror(errno));
close(sfd);
return -1;
}
Expand Down Expand Up @@ -307,7 +307,7 @@ int server_socket_unix(const struct sockaddr_un *addr, int backlog, int mode) {
unlink(addr->sun_path);

if (bind(fd, (struct sockaddr *) addr, sizeof(*addr)) == -1) {
vkprintf(4, "bind(%s): %s\n", addr->sun_path, strerror(errno));
kprintf("bind(%s): %s\n", addr->sun_path, strerror(errno));
close(fd);
return -1;
}
Expand Down Expand Up @@ -407,7 +407,7 @@ int client_socket_unix(const struct sockaddr_un *addr, int mode) {
socket_maximize_sndbuf(fd, 0);
socket_maximize_rcvbuf(fd, 0);
if (connect(fd, (struct sockaddr *) addr, sizeof(*addr)) == -1 && errno != EINPROGRESS) {
vkprintf(4, "Cannot connect() to \"%s\": %s\n", addr->sun_path, strerror(errno));
kprintf("Cannot connect() to \"%s\": %s\n", addr->sun_path, strerror(errno));
close(fd);
return -1;
}
Expand Down
8 changes: 4 additions & 4 deletions net/net-tcp-rpc-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ int tcp_rpcc_parse_execute (struct connection *c) {
}
assert (rwm_fetch_lookup (&c->in, &D->packet_len, 4) == 4);
if (D->packet_len <= 0 || (D->packet_len & 3) || (D->packet_len > TCP_RPCC_FUNC(c)->max_packet_len && TCP_RPCC_FUNC(c)->max_packet_len > 0)) {
tvkprintf(net_connections, 4, "error while parsing packet: bad packet length %d\n", D->packet_len);
kprintf("error while parsing packet: bad packet length %d\n", D->packet_len);
c->status = conn_error;
c->error = -1;
return 0;
Expand All @@ -250,7 +250,7 @@ int tcp_rpcc_parse_execute (struct connection *c) {
continue;
}
if (D->packet_len < 16) {
tvkprintf(net_connections, 4, "error while parsing packet: bad packet length %d\n", D->packet_len);
kprintf("error while parsing packet: bad packet length %d\n", D->packet_len);
c->status = conn_error;
c->error = -1;
return 0;
Expand All @@ -273,7 +273,7 @@ int tcp_rpcc_parse_execute (struct connection *c) {
assert (rwm_fetch_data_back (&msg, &crc32, 4) == 4);
D->packet_crc32 = rwm_custom_crc32 (&msg, D->packet_len - 4, D->custom_crc_partial);
if (crc32 != D->packet_crc32) {
tvkprintf(net_connections, 4, "error while parsing packet: crc32 = %08x != %08x\n", D->packet_crc32, crc32);
kprintf("error while parsing packet: crc32 = %08x != %08x\n", D->packet_crc32, crc32);
c->status = conn_error;
c->error = -1;
rwm_free (&msg);
Expand All @@ -291,7 +291,7 @@ int tcp_rpcc_parse_execute (struct connection *c) {
int res = -1;

if (D->packet_num != D->in_packet_num) {
tvkprintf(net_connections, 4, "error while parsing packet: got packet num %d, expected %d\n", D->packet_num, D->in_packet_num);
kprintf("error while parsing packet: got packet num %d, expected %d\n", D->packet_num, D->in_packet_num);
c->status = conn_error;
c->error = -1;
rwm_free (&msg);
Expand Down
4 changes: 2 additions & 2 deletions server/php-engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ int hts_func_execute(connection *c, int op) {
return -501;
}

vkprintf (1, "start https execute: connection #%d, op=%d, header_size=%d, data_size=%d, http_version=%d\n",
vkprintf (1, "start http server execute: connection #%d, op=%d, header_size=%d, data_size=%d, http_version=%d\n",
c->fd, op, D->header_size, D->data_size, D->http_ver);

if (!vk::any_of_equal(D->query_type, htqt_get, htqt_post, htqt_head)) {
Expand Down Expand Up @@ -890,7 +890,7 @@ static void send_rpc_error(connection *c, long long req_id, int error_code, cons
}

int rpcx_execute(connection *c, int op, raw_message *raw) {
vkprintf(1, "rpc execute: fd=%d, op=%d, len=%d\n", c->fd, op, raw->total_bytes);
vkprintf(2, "rpc execute: fd=%d, op=%d, len=%d\n", c->fd, op, raw->total_bytes);

int len = raw->total_bytes;

Expand Down
30 changes: 18 additions & 12 deletions server/php-master.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ int changed = 0;
int failed = 0;
int socket_fd = -1;
int to_exit = 0;
int general_worker_to_kill = 0, general_worker_to_run = 0;
int general_workers_to_kill = 0, general_workers_to_run = 0;
int job_workers_to_kill = 0, job_workers_to_run = 0;
long long generation;
int receive_fd_attempts_cnt = 0;
Expand Down Expand Up @@ -1173,7 +1173,7 @@ int php_master_http_execute(struct connection *c, int op) {
void run_master_off_in_graceful_shutdown() {
kprintf("master off in graceful shutdown\n");
assert(state == master_state::off_in_graceful_shutdown);
general_worker_to_kill = vk::singleton<WorkersControl>::get().get_running_count(WorkerType::general_worker);
general_workers_to_kill = vk::singleton<WorkersControl>::get().get_running_count(WorkerType::general_worker);
if (all_http_workers_killed()) {
if (all_job_workers_killed()) {
to_exit = 1;
Expand All @@ -1198,7 +1198,7 @@ void run_master_off_in_graceful_restart() {

if (other->to_kill_generation > me->generation) {
// old master kills as many workers as new master told
general_worker_to_kill = other->to_kill;
general_workers_to_kill = other->to_kill;
}

if (all_http_workers_killed()) {
Expand Down Expand Up @@ -1298,7 +1298,7 @@ void run_master_on() {
if (done) {
const auto &control = vk::singleton<WorkersControl>::get();
const int total_workers = control.get_alive_count(WorkerType::general_worker) + (other->is_alive ? other->running_http_workers_n + other->dying_http_workers_n : 0);
general_worker_to_run = std::max(0, int{control.get_count(WorkerType::general_worker)} - total_workers);
general_workers_to_run = std::max(0, int{control.get_count(WorkerType::general_worker)} - total_workers);
job_workers_to_run = control.get_count(WorkerType::job_worker) - control.get_alive_count(WorkerType::job_worker);

if (other->is_alive) {
Expand Down Expand Up @@ -1360,7 +1360,6 @@ void check_and_instance_cache_try_swap_memory() {
}

static void cron() {
tvkprintf(master_process, 2, "master process cron work\n");
if (!other->is_alive || in_old_master_on_restart()) {
// write stats at the beginning to avoid spikes in graphs
send_data_to_statsd_with_prefix(vk::singleton<ServerConfig>::get().get_statsd_prefix(), stats_tag_kphp_server);
Expand Down Expand Up @@ -1407,6 +1406,12 @@ static void cron() {
instance_cache_purge_expired_elements();
check_and_instance_cache_try_swap_memory();
confdata_binlog_update_cron();
tvkprintf(master_process, 3, "master process cron work [utime = %llu, stime = %llu, alive_workers_count = %d]. "
"General workers details [running general workers = %d, waiting general workers = %d, ready for accept general workers = %d]. "
"Job workers details [running job workers = %d, waiting job workers = %d, ready for accept job workers = %d].\n",
utime, stime, alive_workers_count,
general_workers_stat.running_workers, general_workers_stat.waiting_workers, general_workers_stat.ready_for_accept_workers,
job_workers_stat.running_workers, job_workers_stat.waiting_workers, job_workers_stat.ready_for_accept_workers);
}

auto get_steady_tp_ms_now() noexcept {
Expand Down Expand Up @@ -1473,7 +1478,7 @@ WorkerType run_master() {
changed = 0;
failed = 0;
to_exit = 0;
general_worker_to_kill = general_worker_to_run = 0;
general_workers_to_kill = general_workers_to_run = 0;
job_workers_to_kill = job_workers_to_run = 0;

update_workers();
Expand Down Expand Up @@ -1513,24 +1518,25 @@ WorkerType run_master() {

me->generation = generation;

if (general_worker_to_kill != 0 || general_worker_to_run != 0 || job_workers_to_kill != 0 || job_workers_to_run != 0) {
tvkprintf(master_process, 2, "[general_worker_to_kill = %d] [general_worker_to_run = %d] [job_workers_to_kill = %d] [job_workers_to_run = %d]\n",
general_worker_to_kill, general_worker_to_run, job_workers_to_kill, job_workers_to_run);
if (general_workers_to_kill != 0 || general_workers_to_run != 0 || job_workers_to_kill != 0 || job_workers_to_run != 0) {
tvkprintf(master_process, 2, "[general_workers_to_kill = %d] [general_workers_to_run = %d] [job_workers_to_kill = %d] [job_workers_to_run = %d]\n",
general_workers_to_kill, general_workers_to_run, job_workers_to_kill, job_workers_to_run);
}

for (int i = 0; i < job_workers_to_kill; ++i) {
kill_worker(WorkerType::job_worker);
}
for (int i = 0; i < job_workers_to_run; ++i) {
for (int i = 0; i < job_workers_to_run && !failed; ++i) {
if (run_worker(WorkerType::job_worker)) {
tvkprintf(job_workers, 1, "launched new job worker with pid = %d\n", pid);
return WorkerType::job_worker;
}
}

for (int i = 0; i < general_worker_to_kill; ++i) {
for (int i = 0; i < general_workers_to_kill; ++i) {
kill_worker(WorkerType::general_worker);
}
for (int i = 0; i < general_worker_to_run; ++i) {
for (int i = 0; i < general_workers_to_run && !failed; ++i) {
if (run_worker(WorkerType::general_worker)) {
return WorkerType::general_worker;
}
Expand Down
21 changes: 7 additions & 14 deletions server/php-runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "server/server-stats.h"
#include "server/signal-handlers.h"

DEFINE_VERBOSITY(php_code);
DEFINE_VERBOSITY(php_runner);

query_stats_t query_stats;
long long query_stats_id = 1;
Expand Down Expand Up @@ -145,7 +145,7 @@ PhpScript::PhpScript(size_t mem_size, double oom_handling_memory_ratio, size_t s
, oom_handling_memory_ratio(oom_handling_memory_ratio)
, run_mem(static_cast<char *>(mmap(nullptr, mem_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)))
, script_stack(stack_size) {
tvkprintf(php_code, 1, "initialize PHP-script\n");
tvkprintf(php_runner, 1, "initialize PHP-script\n");
// fprintf (stderr, "PHPScriptBase: constructor\n");
// fprintf (stderr, "[%p -> %p] [%p -> %p]\n", run_stack, run_stack_end, run_mem, run_mem + mem_size);
}
Expand Down Expand Up @@ -314,18 +314,11 @@ void PhpScript::finish() noexcept {
kphp_tracing::on_php_script_finish_terminated();
}

if (save_state == run_state_t::error) {
switch (error_type) {
case script_error_t::memory_limit:
kprintf("Detailed memory stats: total allocations = %zd, total memory allocated = %zd, huge memory pieces = %zd, small memory pieces = %zd, defragmentation calls = %zd\n",
script_mem_stats.total_allocations, script_mem_stats.total_memory_allocated, script_mem_stats.huge_memory_pieces, script_mem_stats.small_memory_pieces, script_mem_stats.defragmentation_calls);
break;
case script_error_t::timeout:
kprintf("Script timeout value = %d\n", script_timeout);
break ;
default:
break;
}
if (error_type == script_error_t::memory_limit || script_mem_stats.real_memory_used > max_memory / 2) {
kprintf("Detailed memory stats: total allocations = %zd, total memory allocated = %zd, huge memory pieces = %zd, small memory pieces = %zd, defragmentation calls = %zd,"
"real memory used = %zd, max real memory used = %zd, memory used = %zd, max memory used = %zd, memory_limit = %zd\n",
script_mem_stats.total_allocations, script_mem_stats.total_memory_allocated, script_mem_stats.huge_memory_pieces, script_mem_stats.small_memory_pieces, script_mem_stats.defragmentation_calls,
script_mem_stats.real_memory_used, script_mem_stats.max_real_memory_used, script_mem_stats.memory_used, script_mem_stats.max_memory_used, script_mem_stats.memory_limit);
}

const size_t buf_size = 5000;
Expand Down
2 changes: 1 addition & 1 deletion server/php-runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "server/php-query-data.h"
#include "server/ucontext-portable.h"

DECLARE_VERBOSITY(php_code);
DECLARE_VERBOSITY(php_runner);

enum class run_state_t {
finished,
Expand Down
Loading

0 comments on commit f1e9d07

Please sign in to comment.