static void _manage_requests(struct gridd_client_pool_s *pool) { struct event_client_s *ec; EXTRA_ASSERT(pool != NULL); while (pool->active_count < pool->active_max) { ec = g_async_queue_try_pop(pool->pending_clients); if (NULL == ec) return; EXTRA_ASSERT(ec->client != NULL); if (!gridd_client_start(ec->client)) { GError *err = gridd_client_error(ec->client); if (NULL != err) { GRID_WARN("STARTUP Client fd=%d [%s] : (%d) %s", gridd_client_fd(ec->client), gridd_client_url(ec->client), err->code, err->message); g_clear_error(&err); } else { GRID_WARN("STARTUP Client fd=%d [%s] : already started", gridd_client_fd(ec->client), gridd_client_url(ec->client)); EXTRA_ASSERT(err != NULL); } event_client_free(ec); } else if (!event_client_monitor(pool, ec)) event_client_free(ec); } }
GError* sqlx_remote_execute_packed_DESTROY_many(gchar **targets, GByteArray *sid, GByteArray *req) { (void) sid; GError *err = NULL, *local_err = NULL; GPtrArray *targets_ok = g_ptr_array_new(); struct client_s **clients = gridd_client_create_many(targets, req, NULL, NULL); metautils_gba_unref(req); req = NULL; if (clients == NULL) { err = NEWERROR(0, "Failed to create gridd clients"); return err; } gridd_clients_start(clients); err = gridd_clients_loop(clients); for (gchar **cursor = targets; cursor && *cursor; cursor++) { metautils_str_clean(cursor); } if (err) goto end; for (struct client_s **p = clients; p && *p; p++) { if (!(local_err = gridd_client_error(*p))) { g_ptr_array_add(targets_ok, g_strdup(gridd_client_url(*p))); continue; } if (local_err->code == CODE_CONTAINER_NOTFOUND || local_err->code == CODE_NOT_FOUND) { g_clear_error(&local_err); g_ptr_array_add(targets_ok, g_strdup(gridd_client_url(*p))); continue; } GRID_DEBUG("Database destruction attempts failed: (%d) %s", local_err->code, local_err->message); if (!err) g_propagate_prefixed_error(&err, local_err, "Failed to destroy base on %s: ", gridd_client_url(*p)); else g_clear_error(&local_err); } for (guint i = 0; i < targets_ok->len; i++) { targets[i] = g_ptr_array_index(targets_ok, i); } end: g_ptr_array_free(targets_ok, TRUE); gridd_clients_free(clients); return err; }
GError* gridd_client_step(struct client_s *client) { struct pollfd pfd = {-1, 0, 0}; if (!_client_to_pollfd(client, &pfd)) { return NULL; } int rc = metautils_syscall_poll(&pfd, 1, 1000); if (rc == 0) { GTimeVal now; g_get_current_time(&now); gridd_client_expire(client, &now); return NULL; } if (rc < 0) return NEWERROR(errno, "poll errno=%d %s", errno, strerror(errno)); if (pfd.revents & POLLERR) { GError *err = socket_get_error(pfd.fd); g_prefix_error(&err, "%s: ", gridd_client_url(client)); gridd_client_fail(client, err); g_clear_error(&err); } else gridd_client_react(client); return NULL; }
static void _manage_timeouts(struct gridd_client_pool_s *pool) { GTimeVal now; struct event_client_s *ec; if (pool->active_count <= 0) return; g_get_current_time(&now); if (pool->last_timeout_check == now.tv_sec) return; for (int i=0; i<pool->active_clients_size ;i++) { if (!(ec = pool->active_clients[i])) continue; EXTRA_ASSERT(ec->client != NULL); EXTRA_ASSERT(i == gridd_client_fd(ec->client)); if (gridd_client_expired(ec->client, &now)) { GRID_INFO("EXPIRED Client fd=%d [%s]", i, gridd_client_url(ec->client)); _pool_unmonitor(pool, i); event_client_free(ec); } } }
GError* gridd_client_step(struct gridd_client_s *client) { int rc; struct pollfd pfd = {-1, 0, 0}; if (!_client_to_pollfd(client, &pfd)) return NULL; retry: rc = metautils_syscall_poll(&pfd, 1, 1000); if (rc == 0) { gridd_client_expire(client, oio_ext_monotonic_time ()); return NULL; } if (rc < 0) { if (errno == EINTR) goto retry; return NEWERROR(errno, "poll errno=%d %s", errno, strerror(errno)); } if (pfd.revents & POLLERR) { GError *err = socket_get_error(pfd.fd); g_prefix_error(&err, "%s: ", gridd_client_url(client)); gridd_client_fail(client, err); g_clear_error(&err); } else if (pfd.revents & (POLLIN|POLLOUT)) { gridd_client_react(client); } return NULL; }
void gridd_clients_start(struct client_s **clients) { if (unlikely(NULL == clients)) return; for (; *clients ;++clients) { if (gridd_client_start(*clients)) continue; GError *err = gridd_client_error(*clients); GRID_WARN("STARTUP failed fd=%d [%s] : (%d) %s", gridd_client_fd(*clients), gridd_client_url(*clients), err ? err->code : 0, err ? err->message : "?"); if (err) g_clear_error(&err); } }
static void _manage_one_event(struct gridd_client_pool_s *pool, int fd, int evt) { struct event_client_s *ec = pool->active_clients[fd]; _pool_unmonitor(pool, fd); EXTRA_ASSERT(ec != NULL); EXTRA_ASSERT(ec->client != NULL); EXTRA_ASSERT(fd == gridd_client_fd(ec->client)); if ((evt & EPOLLERR) || (evt & EPOLLHUP)) { GRID_DEBUG("%s CLIENT [%s] fd=%d cnx error", __FUNCTION__, gridd_client_url(ec->client), gridd_client_fd(ec->client)); event_client_free(ec); } else { gridd_client_react(ec->client); if (gridd_client_finished(ec->client)) event_client_free(ec); else if (!event_client_monitor(pool, ec)) event_client_free(ec); } }
GError * gridd_clients_step(struct client_s **clients) { struct client_s ** _lookup_client(int fd, struct client_s **ppc) { struct client_s *c; for (; (c = *ppc) ;ppc++) { if (gridd_client_fd(c) == fd) return ppc; } return ppc; } guint i, j; int rc; struct client_s *last, **plast; GTimeVal now; guint nbclients; EXTRA_ASSERT(clients != NULL); nbclients = g_strv_length((gchar**)clients); EXTRA_ASSERT(nbclients > 0); struct pollfd pfd[nbclients]; for (j=0,plast=clients; NULL != (last = *plast) ;plast++) { if (_client_to_pollfd(last, pfd+j)) j++; } if (!j) return NULL; /* Wait for an event to happen */ if (!(rc = poll(pfd, j, 100))) { g_get_current_time(&now); _clients_expire(clients, &now); return NULL; } if (rc < 0) return NEWERROR(errno, "poll error (%s)", strerror(errno)); /* Then manage each event */ for (plast=clients,i=0; i<j ;i++) { if (!pfd[i].revents) continue; /* Find the client for this pollfd */ plast = _lookup_client(pfd[i].fd, plast); EXTRA_ASSERT(plast != NULL); last = *plast; /* Manage the poll() event */ if (pfd[i].revents & POLLERR) { GError *err = socket_get_error(pfd[i].fd); g_prefix_error(&err, "%s: ", gridd_client_url(last)); gridd_client_fail(last, err); g_clear_error(&err); } else gridd_client_react(last); } /* Now check for expired clients */ g_get_current_time(&now); _clients_expire(clients, &now); return NULL; }