/* * Function used to register a table for sync on a group of peers * */ void peers_register_table(struct peers *peers, struct stktable *table) { struct shared_table *st; struct peer * curpeer; struct peer_session *ps; st = (struct shared_table *)calloc(1,sizeof(struct shared_table)); st->table = table; st->next = peers->tables; st->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); peers->tables = st; for (curpeer = peers->remote; curpeer; curpeer = curpeer->next) { ps = (struct peer_session *)calloc(1,sizeof(struct peer_session)); ps->table = st; ps->peer = curpeer; if (curpeer->local) st->local_session = ps; ps->next = st->sessions; ps->reconnect = now_ms; st->sessions = ps; peers->peers_fe->maxconn += 3; } peers->peers_fe->listen->maxconn = peers->peers_fe->maxconn; st->sync_task = task_new(); st->sync_task->process = process_peer_sync; st->sync_task->expire = TICK_ETERNITY; st->sync_task->context = (void *)st; table->sync_task =st->sync_task; signal_register_task(0, table->sync_task, 0); task_wakeup(st->sync_task, TASK_WOKEN_INIT); }
/* * Callback to release a session with a peer */ static void peer_session_release(struct stream_interface *si) { struct task *t = (struct task *)si->owner; struct session *s = (struct session *)t->context; struct peer_session *ps = (struct peer_session *)si->conn.data_ctx; /* si->conn.data_ctx is not a peer session */ if (si->applet.st0 < PEER_SESSION_SENDSUCCESS) return; /* peer session identified */ if (ps) { if (ps->session == s) { ps->session = NULL; if (ps->flags & PEER_F_LEARN_ASSIGN) { /* unassign current peer for learning */ ps->flags &= ~(PEER_F_LEARN_ASSIGN); ps->table->flags &= ~(SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS); /* reschedule a resync */ ps->table->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); } /* reset teaching and learning flags to 0 */ ps->flags &= PEER_TEACH_RESET; ps->flags &= PEER_LEARN_RESET; } task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG); } }
/* * Callback to release a session with a peer */ static void peer_session_release(struct appctx *appctx) { struct stream_interface *si = appctx->owner; struct stream *s = si_strm(si); struct peer_session *ps = (struct peer_session *)appctx->ctx.peers.ptr; /* appctx->ctx.peers.ptr is not a peer session */ if (appctx->st0 < PEER_SESS_ST_SENDSUCCESS) return; /* peer session identified */ if (ps) { if (ps->stream == s) { ps->stream = NULL; ps->appctx = NULL; if (ps->flags & PEER_F_LEARN_ASSIGN) { /* unassign current peer for learning */ ps->flags &= ~(PEER_F_LEARN_ASSIGN); ps->table->flags &= ~(SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS); /* reschedule a resync */ ps->table->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); } /* reset teaching and learning flags to 0 */ ps->flags &= PEER_TEACH_RESET; ps->flags &= PEER_LEARN_RESET; } task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG); } }
/* * Use this function to force a close of a peer session */ static void peer_session_forceshutdown(struct stream * stream) { struct appctx *appctx = NULL; struct peer_session *ps; int i; for (i = 0; i <= 1; i++) { appctx = objt_appctx(stream->si[i].end); if (!appctx) continue; if (appctx->applet != &peer_applet) continue; break; } if (!appctx) return; ps = (struct peer_session *)appctx->ctx.peers.ptr; /* we're killing a connection, we must apply a random delay before * retrying otherwise the other end will do the same and we can loop * for a while. */ if (ps) ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000)); /* call release to reinit resync states if needed */ peer_session_release(appctx); appctx->st0 = PEER_SESS_ST_END; appctx->ctx.peers.ptr = NULL; task_wakeup(stream->task, TASK_WOKEN_MSG); }
/* * this function disables health-check servers so that the process will quickly be ignored * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace * time will not be used since it would already not listen anymore to the socket. */ void soft_stop(void) { struct proxy *p; struct peers *prs; stopping = 1; p = proxy; tv_update_date(0,1); /* else, the old time before select will be used */ while (p) { if (p->state != PR_STSTOPPED) { Warning("Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace); send_log(p, LOG_WARNING, "Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace); p->stop_time = tick_add(now_ms, p->grace); } if (p->table.size && p->table.sync_task) task_wakeup(p->table.sync_task, TASK_WOKEN_MSG); /* wake every proxy task up so that they can handle the stopping */ task_wakeup(p->task, TASK_WOKEN_MSG); p = p->next; } prs = peers; while (prs) { stop_proxy((struct proxy *)prs->peers_fe); prs = prs->next; } /* signal zero is used to broadcast the "stopping" event */ signal_handler(0); }
/* * this function disables health-check servers so that the process will quickly be ignored * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace * time will not be used since it would already not listen anymore to the socket. */ void soft_stop(void) { struct proxy *p; stopping = 1; p = proxy; tv_update_date(0,1); /* else, the old time before select will be used */ while (p) { if (p->state != PR_STSTOPPED) { Warning("Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace); send_log(p, LOG_WARNING, "Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace); p->stop_time = tick_add(now_ms, p->grace); } p = p->next; } }
/* * IO Handler to handle message exchance with a peer */ static void peer_io_handler(struct stream_interface *si) { struct task *t= (struct task *)si->owner; struct session *s = (struct session *)t->context; struct peers *curpeers = (struct peers *)s->fe->parent; int reql = 0; int repl = 0; while (1) { switchstate: switch(si->applet.st0) { case PEER_SESSION_ACCEPT: si->conn.data_ctx = NULL; si->applet.st0 = PEER_SESSION_GETVERSION; /* fall through */ case PEER_SESSION_GETVERSION: reql = bo_getline(si->ob, trash, trashlen); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) goto out; si->applet.st0 = PEER_SESSION_END; goto switchstate; } if (trash[reql-1] != '\n') { si->applet.st0 = PEER_SESSION_END; goto switchstate; } else if (reql > 1 && (trash[reql-2] == '\r')) trash[reql-2] = 0; else trash[reql-1] = 0; bo_skip(si->ob, reql); /* test version */ if (strcmp(PEER_SESSION_PROTO_NAME " 1.0", trash) != 0) { si->applet.st0 = PEER_SESSION_EXIT; si->applet.st1 = PEER_SESSION_ERRVERSION; /* test protocol */ if (strncmp(PEER_SESSION_PROTO_NAME " ", trash, strlen(PEER_SESSION_PROTO_NAME)+1) != 0) si->applet.st1 = PEER_SESSION_ERRPROTO; goto switchstate; } si->applet.st0 = PEER_SESSION_GETHOST; /* fall through */ case PEER_SESSION_GETHOST: reql = bo_getline(si->ob, trash, trashlen); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) goto out; si->applet.st0 = PEER_SESSION_END; goto switchstate; } if (trash[reql-1] != '\n') { si->applet.st0 = PEER_SESSION_END; goto switchstate; } else if (reql > 1 && (trash[reql-2] == '\r')) trash[reql-2] = 0; else trash[reql-1] = 0; bo_skip(si->ob, reql); /* test hostname match */ if (strcmp(localpeer, trash) != 0) { si->applet.st0 = PEER_SESSION_EXIT; si->applet.st1 = PEER_SESSION_ERRHOST; goto switchstate; } si->applet.st0 = PEER_SESSION_GETPEER; /* fall through */ case PEER_SESSION_GETPEER: { struct peer *curpeer; char *p; reql = bo_getline(si->ob, trash, trashlen); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) goto out; si->applet.st0 = PEER_SESSION_END; goto switchstate; } if (trash[reql-1] != '\n') { /* Incomplete line, we quit */ si->applet.st0 = PEER_SESSION_END; goto switchstate; } else if (reql > 1 && (trash[reql-2] == '\r')) trash[reql-2] = 0; else trash[reql-1] = 0; bo_skip(si->ob, reql); /* parse line "<peer name> <pid>" */ p = strchr(trash, ' '); if (!p) { si->applet.st0 = PEER_SESSION_EXIT; si->applet.st1 = PEER_SESSION_ERRPROTO; goto switchstate; } *p = 0; /* lookup known peer */ for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) { if (strcmp(curpeer->id, trash) == 0) break; } /* if unknown peer */ if (!curpeer) { si->applet.st0 = PEER_SESSION_EXIT; si->applet.st1 = PEER_SESSION_ERRPEER; goto switchstate; } si->conn.data_ctx = curpeer; si->applet.st0 = PEER_SESSION_GETTABLE; /* fall through */ } case PEER_SESSION_GETTABLE: { struct peer *curpeer = (struct peer *)si->conn.data_ctx; struct shared_table *st; struct peer_session *ps = NULL; unsigned long key_type; size_t key_size; char *p; reql = bo_getline(si->ob, trash, trashlen); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) goto out; si->conn.data_ctx = NULL; si->applet.st0 = PEER_SESSION_END; goto switchstate; } /* Re init si->conn.data_ctx to null, to handle correctly a release case */ si->conn.data_ctx = NULL; if (trash[reql-1] != '\n') { /* Incomplete line, we quit */ si->applet.st0 = PEER_SESSION_END; goto switchstate; } else if (reql > 1 && (trash[reql-2] == '\r')) trash[reql-2] = 0; else trash[reql-1] = 0; bo_skip(si->ob, reql); /* Parse line "<table name> <type> <size>" */ p = strchr(trash, ' '); if (!p) { si->applet.st0 = PEER_SESSION_EXIT; si->applet.st1 = PEER_SESSION_ERRPROTO; goto switchstate; } *p = 0; key_type = (unsigned long)atol(p+1); p = strchr(p+1, ' '); if (!p) { si->conn.data_ctx = NULL; si->applet.st0 = PEER_SESSION_EXIT; si->applet.st1 = PEER_SESSION_ERRPROTO; goto switchstate; } key_size = (size_t)atoi(p); for (st = curpeers->tables; st; st = st->next) { /* If table name matches */ if (strcmp(st->table->id, trash) == 0) { /* If key size mismatches */ if (key_size != st->table->key_size) { si->applet.st0 = PEER_SESSION_EXIT; si->applet.st1 = PEER_SESSION_ERRSIZE; goto switchstate; } /* If key type mismatches */ if (key_type != st->table->type) { si->applet.st0 = PEER_SESSION_EXIT; si->applet.st1 = PEER_SESSION_ERRTYPE; goto switchstate; } /* lookup peer session of current peer */ for (ps = st->sessions; ps; ps = ps->next) { if (ps->peer == curpeer) { /* If session already active, replaced by new one */ if (ps->session && ps->session != s) { if (ps->peer->local) { /* Local connection, reply a retry */ si->applet.st0 = PEER_SESSION_EXIT; si->applet.st1 = PEER_SESSION_TRYAGAIN; goto switchstate; } peer_session_forceshutdown(ps->session); } ps->session = s; break; } } break; } } /* If table not found */ if (!st){ si->applet.st0 = PEER_SESSION_EXIT; si->applet.st1 = PEER_SESSION_ERRTABLE; goto switchstate; } /* If no peer session for current peer */ if (!ps) { si->applet.st0 = PEER_SESSION_EXIT; si->applet.st1 = PEER_SESSION_ERRPEER; goto switchstate; } si->conn.data_ctx = ps; si->applet.st0 = PEER_SESSION_SENDSUCCESS; /* fall through */ } case PEER_SESSION_SENDSUCCESS:{ struct peer_session *ps = (struct peer_session *)si->conn.data_ctx; repl = snprintf(trash, trashlen, "%d\n", PEER_SESSION_SUCCESSCODE); repl = bi_putblk(si->ib, trash, repl); if (repl <= 0) { if (repl == -1) goto out; si->applet.st0 = PEER_SESSION_END; goto switchstate; } /* Register status code */ ps->statuscode = PEER_SESSION_SUCCESSCODE; /* Awake main task */ task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG); /* Init cursors */ ps->teaching_origin =ps->lastpush = ps->lastack = ps->pushack = 0; ps->pushed = ps->update; /* Init confirm counter */ ps->confirm = 0; /* reset teaching and learning flags to 0 */ ps->flags &= PEER_TEACH_RESET; ps->flags &= PEER_LEARN_RESET; /* if current peer is local */ if (ps->peer->local) { /* if table need resyncfrom local and no process assined */ if ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMLOCAL && !(ps->table->flags & SHTABLE_F_RESYNC_ASSIGN)) { /* assign local peer for a lesson, consider lesson already requested */ ps->flags |= PEER_F_LEARN_ASSIGN; ps->table->flags |= (SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS); } } else if ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMREMOTE && !(ps->table->flags & SHTABLE_F_RESYNC_ASSIGN)) { /* assign peer for a lesson */ ps->flags |= PEER_F_LEARN_ASSIGN; ps->table->flags |= SHTABLE_F_RESYNC_ASSIGN; } /* switch to waiting message state */ si->applet.st0 = PEER_SESSION_WAITMSG; goto switchstate; } case PEER_SESSION_CONNECT: { struct peer_session *ps = (struct peer_session *)si->conn.data_ctx; /* Send headers */ repl = snprintf(trash, trashlen, PEER_SESSION_PROTO_NAME " 1.0\n%s\n%s %d\n%s %lu %d\n", ps->peer->id, localpeer, (int)getpid(), ps->table->table->id, ps->table->table->type, (int)ps->table->table->key_size); if (repl >= trashlen) { si->applet.st0 = PEER_SESSION_END; goto switchstate; } repl = bi_putblk(si->ib, trash, repl); if (repl <= 0) { if (repl == -1) goto out; si->applet.st0 = PEER_SESSION_END; goto switchstate; } /* switch to the waiting statuscode state */ si->applet.st0 = PEER_SESSION_GETSTATUS; /* fall through */ } case PEER_SESSION_GETSTATUS: { struct peer_session *ps = (struct peer_session *)si->conn.data_ctx; if (si->ib->flags & CF_WRITE_PARTIAL) ps->statuscode = PEER_SESSION_CONNECTEDCODE; reql = bo_getline(si->ob, trash, trashlen); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) goto out; si->applet.st0 = PEER_SESSION_END; goto switchstate; } if (trash[reql-1] != '\n') { /* Incomplete line, we quit */ si->applet.st0 = PEER_SESSION_END; goto switchstate; } else if (reql > 1 && (trash[reql-2] == '\r')) trash[reql-2] = 0; else trash[reql-1] = 0; bo_skip(si->ob, reql); /* Register status code */ ps->statuscode = atoi(trash); /* Awake main task */ task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG); /* If status code is success */ if (ps->statuscode == PEER_SESSION_SUCCESSCODE) { /* Init cursors */ ps->teaching_origin = ps->lastpush = ps->lastack = ps->pushack = 0; ps->pushed = ps->update; /* Init confirm counter */ ps->confirm = 0; /* reset teaching and learning flags to 0 */ ps->flags &= PEER_TEACH_RESET; ps->flags &= PEER_LEARN_RESET; /* If current peer is local */ if (ps->peer->local) { /* Init cursors to push a resync */ ps->teaching_origin = ps->pushed = ps->table->table->update; /* flag to start to teach lesson */ ps->flags |= PEER_F_TEACH_PROCESS; } else if ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMREMOTE && !(ps->table->flags & SHTABLE_F_RESYNC_ASSIGN)) { /* If peer is remote and resync from remote is needed, and no peer currently assigned */ /* assign peer for a lesson */ ps->flags |= PEER_F_LEARN_ASSIGN; ps->table->flags |= SHTABLE_F_RESYNC_ASSIGN; } } else { /* Status code is not success, abort */ si->applet.st0 = PEER_SESSION_END; goto switchstate; } si->applet.st0 = PEER_SESSION_WAITMSG; /* fall through */ } case PEER_SESSION_WAITMSG: { struct peer_session *ps = (struct peer_session *)si->conn.data_ctx; char c; int totl = 0; reql = bo_getblk(si->ob, (char *)&c, sizeof(c), totl); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) { /* nothing to read */ goto incomplete; } si->applet.st0 = PEER_SESSION_END; goto switchstate; } totl += reql; if ((c & 0x80) || (c == 'D')) { /* Here we have data message */ unsigned int pushack; struct stksess *ts; struct stksess *newts; struct stktable_key stkey; int srvid; uint32_t netinteger; /* Compute update remote version */ if (c & 0x80) { pushack = ps->pushack + (unsigned int)(c & 0x7F); } else { reql = bo_getblk(si->ob, (char *)&netinteger, sizeof(netinteger), totl); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) { goto incomplete; } si->applet.st0 = PEER_SESSION_END; goto switchstate; } totl += reql; pushack = ntohl(netinteger); } /* read key */ if (ps->table->table->type == STKTABLE_TYPE_STRING) { /* type string */ stkey.key = stkey.data.buf; reql = bo_getblk(si->ob, (char *)&netinteger, sizeof(netinteger), totl); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) { goto incomplete; } si->applet.st0 = PEER_SESSION_END; goto switchstate; } totl += reql; stkey.key_len = ntohl(netinteger); reql = bo_getblk(si->ob, stkey.key, stkey.key_len, totl); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) { goto incomplete; } si->applet.st0 = PEER_SESSION_END; goto switchstate; } totl += reql; } else if (ps->table->table->type == STKTABLE_TYPE_INTEGER) { /* type integer */ stkey.key_len = (size_t)-1; stkey.key = &stkey.data.integer; reql = bo_getblk(si->ob, (char *)&netinteger, sizeof(netinteger), totl); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) { goto incomplete; } si->applet.st0 = PEER_SESSION_END; goto switchstate; } totl += reql; stkey.data.integer = ntohl(netinteger); } else { /* type ip */ stkey.key_len = (size_t)-1; stkey.key = stkey.data.buf; reql = bo_getblk(si->ob, (char *)&stkey.data.buf, ps->table->table->key_size, totl); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) { goto incomplete; } si->applet.st0 = PEER_SESSION_END; goto switchstate; } totl += reql; } /* read server id */ reql = bo_getblk(si->ob, (char *)&netinteger, sizeof(netinteger), totl); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) { goto incomplete; } si->applet.st0 = PEER_SESSION_END; goto switchstate; } totl += reql; srvid = ntohl(netinteger); /* update entry */ newts = stksess_new(ps->table->table, &stkey); if (newts) { /* lookup for existing entry */ ts = stktable_lookup(ps->table->table, newts); if (ts) { /* the entry already exist, we can free ours */ stktable_touch(ps->table->table, ts, 0); stksess_free(ps->table->table, newts); } else { struct eb32_node *eb; /* create new entry */ ts = stktable_store(ps->table->table, newts, 0); ts->upd.key= (++ps->table->table->update)+(2^31); eb = eb32_insert(&ps->table->table->updates, &ts->upd); if (eb != &ts->upd) { eb32_delete(eb); eb32_insert(&ps->table->table->updates, &ts->upd); } } /* update entry */ if (srvid && stktable_data_ptr(ps->table->table, ts, STKTABLE_DT_SERVER_ID)) stktable_data_cast(stktable_data_ptr(ps->table->table, ts, STKTABLE_DT_SERVER_ID), server_id) = srvid; ps->pushack = pushack; } } else if (c == 'R') { /* Reset message: remote need resync */ /* reinit counters for a resync */ ps->lastpush = 0; ps->teaching_origin = ps->pushed = ps->table->table->update; /* reset teaching flags to 0 */ ps->flags &= PEER_TEACH_RESET; /* flag to start to teach lesson */ ps->flags |= PEER_F_TEACH_PROCESS; } else if (c == 'F') { /* Finish message, all known updates have been pushed by remote */ /* and remote is up to date */ /* If resync is in progress with remote peer */ if (ps->flags & PEER_F_LEARN_ASSIGN) { /* unassign current peer for learning */ ps->flags &= ~PEER_F_LEARN_ASSIGN; ps->table->flags &= ~(SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS); /* Consider table is now up2date, resync resync no more needed from local neither remote */ ps->table->flags |= (SHTABLE_F_RESYNC_LOCAL|SHTABLE_F_RESYNC_REMOTE); } /* Increase confirm counter to launch a confirm message */ ps->confirm++; } else if (c == 'c') { /* confirm message, remote peer is now up to date with us */ /* If stopping state */ if (stopping) { /* Close session, push resync no more needed */ ps->flags |= PEER_F_TEACH_COMPLETE; si->applet.st0 = PEER_SESSION_END; goto switchstate; } /* reset teaching flags to 0 */ ps->flags &= PEER_TEACH_RESET; } else if (c == 'C') { /* Continue message, all known updates have been pushed by remote */ /* but remote is not up to date */ /* If resync is in progress with current peer */ if (ps->flags & PEER_F_LEARN_ASSIGN) { /* unassign current peer */ ps->flags &= ~PEER_F_LEARN_ASSIGN; ps->table->flags &= ~(SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS); /* flag current peer is not up 2 date to try from an other */ ps->flags |= PEER_F_LEARN_NOTUP2DATE; /* reschedule a resync */ ps->table->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG); } ps->confirm++; } else if (c == 'A') { /* ack message */ uint32_t netinteger; reql = bo_getblk(si->ob, (char *)&netinteger, sizeof(netinteger), totl); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) { goto incomplete; } si->applet.st0 = PEER_SESSION_END; goto switchstate; } totl += reql; /* Consider remote is up to date with "acked" version */ ps->update = ntohl(netinteger); } else { /* Unknown message */ si->applet.st0 = PEER_SESSION_END; goto switchstate; } /* skip consumed message */ bo_skip(si->ob, totl); /* loop on that state to peek next message */ continue; incomplete: /* Nothing to read, now we start to write */ /* Confirm finished or partial messages */ while (ps->confirm) { /* There is a confirm messages to send */ repl = bi_putchr(si->ib, 'c'); if (repl <= 0) { /* no more write possible */ if (repl == -1) goto out; si->applet.st0 = PEER_SESSION_END; goto switchstate; } ps->confirm--; } /* Need to request a resync */ if ((ps->flags & PEER_F_LEARN_ASSIGN) && (ps->table->flags & SHTABLE_F_RESYNC_ASSIGN) && !(ps->table->flags & SHTABLE_F_RESYNC_PROCESS)) { /* Current peer was elected to request a resync */ repl = bi_putchr(si->ib, 'R'); if (repl <= 0) { /* no more write possible */ if (repl == -1) goto out; si->applet.st0 = PEER_SESSION_END; goto switchstate; } ps->table->flags |= SHTABLE_F_RESYNC_PROCESS; } /* It remains some updates to ack */ if (ps->pushack != ps->lastack) { uint32_t netinteger; trash[0] = 'A'; netinteger = htonl(ps->pushack); memcpy(&trash[1], &netinteger, sizeof(netinteger)); repl = bi_putblk(si->ib, trash, 1+sizeof(netinteger)); if (repl <= 0) { /* no more write possible */ if (repl == -1) goto out; si->applet.st0 = PEER_SESSION_END; goto switchstate; } ps->lastack = ps->pushack; } if (ps->flags & PEER_F_TEACH_PROCESS) { /* current peer was requested for a lesson */ if (!(ps->flags & PEER_F_TEACH_STAGE1)) { /* lesson stage 1 not complete */ struct eb32_node *eb; eb = eb32_lookup_ge(&ps->table->table->updates, ps->pushed+1); while (1) { int msglen; struct stksess *ts; if (!eb) { /* flag lesson stage1 complete */ ps->flags |= PEER_F_TEACH_STAGE1; eb = eb32_first(&ps->table->table->updates); if (eb) ps->pushed = eb->key - 1; break; } ts = eb32_entry(eb, struct stksess, upd); msglen = peer_prepare_datamsg(ts, ps, trash, trashlen); if (msglen) { /* message to buffer */ repl = bi_putblk(si->ib, trash, msglen); if (repl <= 0) { /* no more write possible */ if (repl == -1) goto out; si->applet.st0 = PEER_SESSION_END; goto switchstate; } ps->lastpush = ps->pushed = ts->upd.key; } eb = eb32_next(eb); } } /* !TEACH_STAGE1 */ if (!(ps->flags & PEER_F_TEACH_STAGE2)) { /* lesson stage 2 not complete */ struct eb32_node *eb; eb = eb32_lookup_ge(&ps->table->table->updates, ps->pushed+1); while (1) { int msglen; struct stksess *ts; if (!eb || eb->key > ps->teaching_origin) { /* flag lesson stage1 complete */ ps->flags |= PEER_F_TEACH_STAGE2; ps->pushed = ps->teaching_origin; break; } ts = eb32_entry(eb, struct stksess, upd); msglen = peer_prepare_datamsg(ts, ps, trash, trashlen); if (msglen) { /* message to buffer */ repl = bi_putblk(si->ib, trash, msglen); if (repl <= 0) { /* no more write possible */ if (repl == -1) goto out; si->applet.st0 = PEER_SESSION_END; goto switchstate; } ps->lastpush = ps->pushed = ts->upd.key; } eb = eb32_next(eb); } } /* !TEACH_STAGE2 */ if (!(ps->flags & PEER_F_TEACH_FINISHED)) { /* process final lesson message */ repl = bi_putchr(si->ib, ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FINISHED) ? 'F' : 'C'); if (repl <= 0) { /* no more write possible */ if (repl == -1) goto out; si->applet.st0 = PEER_SESSION_END; goto switchstate; } /* flag finished message sent */ ps->flags |= PEER_F_TEACH_FINISHED; } /* !TEACH_FINISHED */ } /* TEACH_PROCESS */ if (!(ps->flags & PEER_F_LEARN_ASSIGN) && (int)(ps->pushed - ps->table->table->localupdate) < 0) { /* Push local updates, only if no learning in progress (to avoid ping-pong effects) */ struct eb32_node *eb; eb = eb32_lookup_ge(&ps->table->table->updates, ps->pushed+1); while (1) { int msglen; struct stksess *ts; /* push local updates */ if (!eb) { eb = eb32_first(&ps->table->table->updates); if (!eb || ((int)(eb->key - ps->pushed) <= 0)) { ps->pushed = ps->table->table->localupdate; break; } } if ((int)(eb->key - ps->table->table->localupdate) > 0) { ps->pushed = ps->table->table->localupdate; break; } ts = eb32_entry(eb, struct stksess, upd); msglen = peer_prepare_datamsg(ts, ps, trash, trashlen); if (msglen) { /* message to buffer */ repl = bi_putblk(si->ib, trash, msglen); if (repl <= 0) { /* no more write possible */ if (repl == -1) goto out; si->applet.st0 = PEER_SESSION_END; goto switchstate; } ps->lastpush = ps->pushed = ts->upd.key; } eb = eb32_next(eb); } } /* ! LEARN_ASSIGN */ /* noting more to do */ goto out; } case PEER_SESSION_EXIT: repl = snprintf(trash, trashlen, "%d\n", si->applet.st1); if (bi_putblk(si->ib, trash, repl) == -1) goto out; si->applet.st0 = PEER_SESSION_END; /* fall through */ case PEER_SESSION_END: { si_shutw(si); si_shutr(si); si->ib->flags |= CF_READ_NULL; goto quit; } } } out: si_update(si); si->ob->flags |= CF_READ_DONTWAIT; /* we don't want to expire timeouts while we're processing requests */ si->ib->rex = TICK_ETERNITY; si->ob->wex = TICK_ETERNITY; quit: return; }
/* * Task processing function to manage re-connect and peer session * tasks wakeup on local update. */ static struct task *process_peer_sync(struct task * task) { struct shared_table *st = (struct shared_table *)task->context; struct peer_session *ps; task->expire = TICK_ETERNITY; if (!stopping) { /* Normal case (not soft stop)*/ if (((st->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMLOCAL) && (!nb_oldpids || tick_is_expired(st->resync_timeout, now_ms)) && !(st->flags & SHTABLE_F_RESYNC_ASSIGN)) { /* Resync from local peer needed no peer was assigned for the lesson and no old local peer found or resync timeout expire */ /* flag no more resync from local, to try resync from remotes */ st->flags |= SHTABLE_F_RESYNC_LOCAL; /* reschedule a resync */ st->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); } /* For each session */ for (ps = st->sessions; ps; ps = ps->next) { /* For each remote peers */ if (!ps->peer->local) { if (!ps->session) { /* no active session */ if (ps->statuscode == 0 || ps->statuscode == PEER_SESSION_SUCCESSCODE || ((ps->statuscode == PEER_SESSION_CONNECTCODE || ps->statuscode == PEER_SESSION_CONNECTEDCODE) && tick_is_expired(ps->reconnect, now_ms))) { /* connection never tried * or previous session established with success * or previous session failed during connection * and reconnection timer is expired */ /* retry a connect */ ps->session = peer_session_create(ps->peer, ps); } else if (ps->statuscode == PEER_SESSION_CONNECTCODE || ps->statuscode == PEER_SESSION_CONNECTEDCODE) { /* If previous session failed during connection * but reconnection timer is not expired */ /* reschedule task for reconnect */ task->expire = tick_first(task->expire, ps->reconnect); } /* else do nothing */ } /* !ps->session */ else if (ps->statuscode == PEER_SESSION_SUCCESSCODE) { /* current session is active and established */ if (((st->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMREMOTE) && !(st->flags & SHTABLE_F_RESYNC_ASSIGN) && !(ps->flags & PEER_F_LEARN_NOTUP2DATE)) { /* Resync from a remote is needed * and no peer was assigned for lesson * and current peer may be up2date */ /* assign peer for the lesson */ ps->flags |= PEER_F_LEARN_ASSIGN; st->flags |= SHTABLE_F_RESYNC_ASSIGN; /* awake peer session task to handle a request of resync */ task_wakeup(ps->session->task, TASK_WOKEN_MSG); } else if ((int)(ps->pushed - ps->table->table->localupdate) < 0) { /* awake peer session task to push local updates */ task_wakeup(ps->session->task, TASK_WOKEN_MSG); } /* else do nothing */ } /* SUCCESSCODE */ } /* !ps->peer->local */ } /* for */ /* Resync from remotes expired: consider resync is finished */ if (((st->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMREMOTE) && !(st->flags & SHTABLE_F_RESYNC_ASSIGN) && tick_is_expired(st->resync_timeout, now_ms)) { /* Resync from remote peer needed * no peer was assigned for the lesson * and resync timeout expire */ /* flag no more resync from remote, consider resync is finished */ st->flags |= SHTABLE_F_RESYNC_REMOTE; } if ((st->flags & SHTABLE_RESYNC_STATEMASK) != SHTABLE_RESYNC_FINISHED) { /* Resync not finished*/ /* reschedule task to resync timeout, to ended resync if needed */ task->expire = tick_first(task->expire, st->resync_timeout); } } /* !stopping */ else { /* soft stop case */ if (task->state & TASK_WOKEN_SIGNAL) { /* We've just recieved the signal */ if (!(st->flags & SHTABLE_F_DONOTSTOP)) { /* add DO NOT STOP flag if not present */ jobs++; st->flags |= SHTABLE_F_DONOTSTOP; } /* disconnect all connected peers */ for (ps = st->sessions; ps; ps = ps->next) { if (ps->session) { peer_session_forceshutdown(ps->session); ps->session = NULL; } } } ps = st->local_session; if (ps->flags & PEER_F_TEACH_COMPLETE) { if (st->flags & SHTABLE_F_DONOTSTOP) { /* resync of new process was complete, current process can die now */ jobs--; st->flags &= ~SHTABLE_F_DONOTSTOP; } } else if (!ps->session) { /* If session is not active */ if (ps->statuscode == 0 || ps->statuscode == PEER_SESSION_SUCCESSCODE || ps->statuscode == PEER_SESSION_CONNECTEDCODE || ps->statuscode == PEER_SESSION_TRYAGAIN) { /* connection never tried * or previous session was successfully established * or previous session tcp connect success but init state incomplete * or during previous connect, peer replies a try again statuscode */ /* connect to the peer */ ps->session = peer_session_create(ps->peer, ps); } else { /* Other error cases */ if (st->flags & SHTABLE_F_DONOTSTOP) { /* unable to resync new process, current process can die now */ jobs--; st->flags &= ~SHTABLE_F_DONOTSTOP; } } } else if (ps->statuscode == PEER_SESSION_SUCCESSCODE && (int)(ps->pushed - ps->table->table->localupdate) < 0) { /* current session active and established awake session to push remaining local updates */ task_wakeup(ps->session->task, TASK_WOKEN_MSG); } } /* stopping */ /* Wakeup for re-connect */ return task; }
/* * Create a new peer session in assigned state (connect will start automatically) */ static struct session *peer_session_create(struct peer *peer, struct peer_session *ps) { struct listener *l = ((struct proxy *)peer->peers->peers_fe)->listen; struct proxy *p = (struct proxy *)l->frontend; /* attached frontend */ struct session *s; struct http_txn *txn; struct task *t; if ((s = pool_alloc2(pool2_session)) == NULL) { /* disable this proxy for a while */ Alert("out of memory in event_accept().\n"); goto out_close; } LIST_ADDQ(&sessions, &s->list); LIST_INIT(&s->back_refs); s->flags = SN_ASSIGNED|SN_ADDR_SET; s->term_trace = 0; /* if this session comes from a known monitoring system, we want to ignore * it as soon as possible, which means closing it immediately for TCP. */ if ((t = task_new()) == NULL) { /* disable this proxy for a while */ Alert("out of memory in event_accept().\n"); goto out_free_session; } ps->reconnect = tick_add(now_ms, MS_TO_TICKS(5000)); ps->statuscode = PEER_SESSION_CONNECTCODE; t->process = l->handler; t->context = s; t->nice = l->nice; memcpy(&s->si[1].conn.addr.to, &peer->addr, sizeof(s->si[1].conn.addr.to)); s->task = t; s->listener = l; /* Note: initially, the session's backend points to the frontend. * This changes later when switching rules are executed or * when the default backend is assigned. */ s->be = s->fe = p; s->req = s->rep = NULL; /* will be allocated later */ s->si[0].conn.t.sock.fd = -1; s->si[0].conn.flags = CO_FL_NONE; s->si[0].owner = t; s->si[0].state = s->si[0].prev_state = SI_ST_EST; s->si[0].err_type = SI_ET_NONE; s->si[0].err_loc = NULL; s->si[0].release = NULL; s->si[0].send_proxy_ofs = 0; set_target_client(&s->si[0].conn.target, l); s->si[0].exp = TICK_ETERNITY; s->si[0].flags = SI_FL_NONE; if (s->fe->options2 & PR_O2_INDEPSTR) s->si[0].flags |= SI_FL_INDEP_STR; stream_int_register_handler(&s->si[0], &peer_applet); s->si[0].applet.st0 = PEER_SESSION_CONNECT; s->si[0].conn.data_ctx = (void *)ps; s->si[1].conn.t.sock.fd = -1; /* just to help with debugging */ s->si[1].conn.flags = CO_FL_NONE; s->si[1].owner = t; s->si[1].state = s->si[1].prev_state = SI_ST_ASS; s->si[1].conn_retries = p->conn_retries; s->si[1].err_type = SI_ET_NONE; s->si[1].err_loc = NULL; s->si[1].release = NULL; s->si[1].send_proxy_ofs = 0; set_target_proxy(&s->si[1].conn.target, s->be); si_prepare_conn(&s->si[1], peer->proto, peer->data); s->si[1].exp = TICK_ETERNITY; s->si[1].flags = SI_FL_NONE; if (s->be->options2 & PR_O2_INDEPSTR) s->si[1].flags |= SI_FL_INDEP_STR; session_init_srv_conn(s); set_target_proxy(&s->target, s->be); s->pend_pos = NULL; /* init store persistence */ s->store_count = 0; s->stkctr1_entry = NULL; s->stkctr2_entry = NULL; /* FIXME: the logs are horribly complicated now, because they are * defined in <p>, <p>, and later <be> and <be>. */ s->logs.logwait = 0; s->do_log = NULL; /* default error reporting function, may be changed by analysers */ s->srv_error = default_srv_error; s->uniq_id = 0; s->unique_id = NULL; txn = &s->txn; /* Those variables will be checked and freed if non-NULL in * session.c:session_free(). It is important that they are * properly initialized. */ txn->sessid = NULL; txn->srv_cookie = NULL; txn->cli_cookie = NULL; txn->uri = NULL; txn->req.cap = NULL; txn->rsp.cap = NULL; txn->hdr_idx.v = NULL; txn->hdr_idx.size = txn->hdr_idx.used = 0; if ((s->req = pool_alloc2(pool2_channel)) == NULL) goto out_fail_req; /* no memory */ s->req->buf.size = global.tune.bufsize; channel_init(s->req); s->req->prod = &s->si[0]; s->req->cons = &s->si[1]; s->si[0].ib = s->si[1].ob = s->req; s->req->flags |= CF_READ_ATTACHED; /* the producer is already connected */ /* activate default analysers enabled for this listener */ s->req->analysers = l->analysers; /* note: this should not happen anymore since there's always at least the switching rules */ if (!s->req->analysers) { channel_auto_connect(s->req);/* don't wait to establish connection */ channel_auto_close(s->req);/* let the producer forward close requests */ } s->req->rto = s->fe->timeout.client; s->req->wto = s->be->timeout.server; if ((s->rep = pool_alloc2(pool2_channel)) == NULL) goto out_fail_rep; /* no memory */ s->rep->buf.size = global.tune.bufsize; channel_init(s->rep); s->rep->prod = &s->si[1]; s->rep->cons = &s->si[0]; s->si[0].ob = s->si[1].ib = s->rep; s->rep->rto = s->be->timeout.server; s->rep->wto = s->fe->timeout.client; s->req->rex = TICK_ETERNITY; s->req->wex = TICK_ETERNITY; s->req->analyse_exp = TICK_ETERNITY; s->rep->rex = TICK_ETERNITY; s->rep->wex = TICK_ETERNITY; s->rep->analyse_exp = TICK_ETERNITY; t->expire = TICK_ETERNITY; s->rep->flags |= CF_READ_DONTWAIT; /* it is important not to call the wakeup function directly but to * pass through task_wakeup(), because this one knows how to apply * priorities to tasks. */ task_wakeup(t, TASK_WOKEN_INIT); l->nbconn++; /* warning! right now, it's up to the handler to decrease this */ p->feconn++;/* beconn will be increased later */ jobs++; if (!(s->listener->options & LI_O_UNLIMITED)) actconn++; totalconn++; return s; /* Error unrolling */ out_fail_rep: pool_free2(pool2_channel, s->req); out_fail_req: task_free(t); out_free_session: LIST_DEL(&s->list); pool_free2(pool2_session, s); out_close: return s; }
/* This function is called on a read event from a listening socket, corresponding * to an accept. It tries to accept as many connections as possible, and for each * calls the listener's accept handler (generally the frontend's accept handler). */ int stream_sock_accept(int fd) { struct listener *l = fdtab[fd].owner; struct proxy *p = l->frontend; int max_accept = global.tune.maxaccept; int cfd; int ret; if (unlikely(l->nbconn >= l->maxconn)) { listener_full(l); return 0; } if (global.cps_lim && !(l->options & LI_O_UNLIMITED)) { int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0); if (unlikely(!max)) { /* frontend accept rate limit was reached */ limit_listener(l, &global_listener_queue); task_schedule(global_listener_queue_task, tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0))); return 0; } if (max_accept > max) max_accept = max; } if (p && p->fe_sps_lim) { int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0); if (unlikely(!max)) { /* frontend accept rate limit was reached */ limit_listener(l, &p->listener_queue); task_schedule(p->task, tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))); return 0; } if (max_accept > max) max_accept = max; } /* Note: if we fail to allocate a connection because of configured * limits, we'll schedule a new attempt worst 1 second later in the * worst case. If we fail due to system limits or temporary resource * shortage, we try again 100ms later in the worst case. */ while (max_accept--) { struct sockaddr_storage addr; socklen_t laddr = sizeof(addr); if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) { limit_listener(l, &global_listener_queue); task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */ return 0; } if (unlikely(p && p->feconn >= p->maxconn)) { limit_listener(l, &p->listener_queue); return 0; } cfd = accept(fd, (struct sockaddr *)&addr, &laddr); if (unlikely(cfd == -1)) { switch (errno) { case EAGAIN: case EINTR: case ECONNABORTED: return 0; /* nothing more to accept */ case ENFILE: if (p) send_log(p, LOG_EMERG, "Proxy %s reached system FD limit at %d. Please check system tunables.\n", p->id, maxfd); limit_listener(l, &global_listener_queue); task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */ return 0; case EMFILE: if (p) send_log(p, LOG_EMERG, "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", p->id, maxfd); limit_listener(l, &global_listener_queue); task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */ return 0; case ENOBUFS: case ENOMEM: if (p) send_log(p, LOG_EMERG, "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", p->id, maxfd); limit_listener(l, &global_listener_queue); task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */ return 0; default: return 0; } } if (unlikely(cfd >= global.maxsock)) { send_log(p, LOG_EMERG, "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n", p->id); close(cfd); limit_listener(l, &global_listener_queue); task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */ return 0; } /* increase the per-process number of cumulated connections */ if (!(l->options & LI_O_UNLIMITED)) { update_freq_ctr(&global.conn_per_sec, 1); if (global.conn_per_sec.curr_ctr > global.cps_max) global.cps_max = global.conn_per_sec.curr_ctr; actconn++; } jobs++; totalconn++; l->nbconn++; if (l->counters) { if (l->nbconn > l->counters->conn_max) l->counters->conn_max = l->nbconn; } ret = l->accept(l, cfd, &addr); if (unlikely(ret <= 0)) { /* The connection was closed by session_accept(). Either * we just have to ignore it (ret == 0) or it's a critical * error due to a resource shortage, and we must stop the * listener (ret < 0). */ if (!(l->options & LI_O_UNLIMITED)) actconn--; jobs--; l->nbconn--; if (ret == 0) /* successful termination */ continue; limit_listener(l, &global_listener_queue); task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */ return 0; } if (l->nbconn >= l->maxconn) { listener_full(l); return 0; } } /* end of while (p->feconn < p->maxconn) */ return 0; }
/* * this function enables proxies when there are enough free sessions, * or stops them when the table is full. It is designed to be called from the * select_loop(). It adjusts the date of next expiration event during stop * time if appropriate. */ void maintain_proxies(int *next) { struct proxy *p; struct listener *l; unsigned int wait; p = proxy; /* if there are enough free sessions, we'll activate proxies */ if (actconn < global.maxconn) { for (; p; p = p->next) { /* check the various reasons we may find to block the frontend */ if (p->feconn >= p->maxconn) goto do_block; if (p->fe_sps_lim && (wait = next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 1))) { /* we're blocking because a limit was reached on the number of * requests/s on the frontend. We want to re-check ASAP, which * means in 1 ms before estimated expiration date, because the * timer will have settled down. Note that we may already be in * IDLE state here. */ *next = tick_first(*next, tick_add(now_ms, wait)); goto do_block; } /* OK we have no reason to block, so let's unblock if we were blocking */ if (p->state == PR_STIDLE) { for (l = p->listen; l != NULL; l = l->next) enable_listener(l); p->state = PR_STRUN; } continue; do_block: if (p->state == PR_STRUN) { for (l = p->listen; l != NULL; l = l->next) disable_listener(l); p->state = PR_STIDLE; } } } else { /* block all proxies */ while (p) { if (p->state == PR_STRUN) { for (l = p->listen; l != NULL; l = l->next) disable_listener(l); p->state = PR_STIDLE; } p = p->next; } } if (stopping) { p = proxy; while (p) { if (p->state != PR_STSTOPPED) { int t; t = tick_remain(now_ms, p->stop_time); if (t == 0) { Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n", p->id, p->cum_feconn, p->cum_beconn); send_log(p, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n", p->id, p->cum_feconn, p->cum_beconn); stop_proxy(p); /* try to free more memory */ pool_gc2(); } else { *next = tick_first(*next, p->stop_time); } } p = p->next; } } return; }
/* * IO Handler to handle message exchance with a peer */ static void peer_io_handler(struct appctx *appctx) { struct stream_interface *si = appctx->owner; struct stream *s = si_strm(si); struct peers *curpeers = (struct peers *)strm_fe(s)->parent; int reql = 0; int repl = 0; while (1) { switchstate: switch(appctx->st0) { case PEER_SESS_ST_ACCEPT: appctx->ctx.peers.ptr = NULL; appctx->st0 = PEER_SESS_ST_GETVERSION; /* fall through */ case PEER_SESS_ST_GETVERSION: reql = bo_getline(si_oc(si), trash.str, trash.size); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) goto out; appctx->st0 = PEER_SESS_ST_END; goto switchstate; } if (trash.str[reql-1] != '\n') { appctx->st0 = PEER_SESS_ST_END; goto switchstate; } else if (reql > 1 && (trash.str[reql-2] == '\r')) trash.str[reql-2] = 0; else trash.str[reql-1] = 0; bo_skip(si_oc(si), reql); /* test version */ if (strcmp(PEER_SESSION_PROTO_NAME " 1.0", trash.str) != 0) { appctx->st0 = PEER_SESS_ST_EXIT; appctx->st1 = PEER_SESS_SC_ERRVERSION; /* test protocol */ if (strncmp(PEER_SESSION_PROTO_NAME " ", trash.str, strlen(PEER_SESSION_PROTO_NAME)+1) != 0) appctx->st1 = PEER_SESS_SC_ERRPROTO; goto switchstate; } appctx->st0 = PEER_SESS_ST_GETHOST; /* fall through */ case PEER_SESS_ST_GETHOST: reql = bo_getline(si_oc(si), trash.str, trash.size); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) goto out; appctx->st0 = PEER_SESS_ST_END; goto switchstate; } if (trash.str[reql-1] != '\n') { appctx->st0 = PEER_SESS_ST_END; goto switchstate; } else if (reql > 1 && (trash.str[reql-2] == '\r')) trash.str[reql-2] = 0; else trash.str[reql-1] = 0; bo_skip(si_oc(si), reql); /* test hostname match */ if (strcmp(localpeer, trash.str) != 0) { appctx->st0 = PEER_SESS_ST_EXIT; appctx->st1 = PEER_SESS_SC_ERRHOST; goto switchstate; } appctx->st0 = PEER_SESS_ST_GETPEER; /* fall through */ case PEER_SESS_ST_GETPEER: { struct peer *curpeer; char *p; reql = bo_getline(si_oc(si), trash.str, trash.size); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) goto out; appctx->st0 = PEER_SESS_ST_END; goto switchstate; } if (trash.str[reql-1] != '\n') { /* Incomplete line, we quit */ appctx->st0 = PEER_SESS_ST_END; goto switchstate; } else if (reql > 1 && (trash.str[reql-2] == '\r')) trash.str[reql-2] = 0; else trash.str[reql-1] = 0; bo_skip(si_oc(si), reql); /* parse line "<peer name> <pid>" */ p = strchr(trash.str, ' '); if (!p) { appctx->st0 = PEER_SESS_ST_EXIT; appctx->st1 = PEER_SESS_SC_ERRPROTO; goto switchstate; } *p = 0; /* lookup known peer */ for (curpeer = curpeers->remote; curpeer; curpeer = curpeer->next) { if (strcmp(curpeer->id, trash.str) == 0) break; } /* if unknown peer */ if (!curpeer) { appctx->st0 = PEER_SESS_ST_EXIT; appctx->st1 = PEER_SESS_SC_ERRPEER; goto switchstate; } appctx->ctx.peers.ptr = curpeer; appctx->st0 = PEER_SESS_ST_GETTABLE; /* fall through */ } case PEER_SESS_ST_GETTABLE: { struct peer *curpeer = (struct peer *)appctx->ctx.peers.ptr; struct shared_table *st; struct peer_session *ps = NULL; unsigned long key_type; size_t key_size; char *p; reql = bo_getline(si_oc(si), trash.str, trash.size); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) goto out; appctx->ctx.peers.ptr = NULL; appctx->st0 = PEER_SESS_ST_END; goto switchstate; } /* Re init appctx->ctx.peers.ptr to null, to handle correctly a release case */ appctx->ctx.peers.ptr = NULL; if (trash.str[reql-1] != '\n') { /* Incomplete line, we quit */ appctx->st0 = PEER_SESS_ST_END; goto switchstate; } else if (reql > 1 && (trash.str[reql-2] == '\r')) trash.str[reql-2] = 0; else trash.str[reql-1] = 0; bo_skip(si_oc(si), reql); /* Parse line "<table name> <type> <size>" */ p = strchr(trash.str, ' '); if (!p) { appctx->st0 = PEER_SESS_ST_EXIT; appctx->st1 = PEER_SESS_SC_ERRPROTO; goto switchstate; } *p = 0; key_type = (unsigned long)atol(p+1); p = strchr(p+1, ' '); if (!p) { appctx->ctx.peers.ptr = NULL; appctx->st0 = PEER_SESS_ST_EXIT; appctx->st1 = PEER_SESS_SC_ERRPROTO; goto switchstate; } key_size = (size_t)atoi(p); for (st = curpeers->tables; st; st = st->next) { /* If table name matches */ if (strcmp(st->table->id, trash.str) == 0) { /* Check key size mismatches, except for strings * which may be truncated as long as they fit in * a buffer. */ if (key_size != st->table->key_size && (key_type != STKTABLE_TYPE_STRING || 1 + 4 + 4 + key_size - 1 >= trash.size)) { appctx->st0 = PEER_SESS_ST_EXIT; appctx->st1 = PEER_SESS_SC_ERRSIZE; goto switchstate; } /* If key type mismatches */ if (key_type != st->table->type) { appctx->st0 = PEER_SESS_ST_EXIT; appctx->st1 = PEER_SESS_SC_ERRTYPE; goto switchstate; } /* lookup peer stream of current peer */ for (ps = st->sessions; ps; ps = ps->next) { if (ps->peer == curpeer) { /* If stream already active, replaced by new one */ if (ps->stream && ps->stream != s) { if (ps->peer->local) { /* Local connection, reply a retry */ appctx->st0 = PEER_SESS_ST_EXIT; appctx->st1 = PEER_SESS_SC_TRYAGAIN; goto switchstate; } peer_session_forceshutdown(ps->stream); } ps->stream = s; ps->appctx = appctx; break; } } break; } } /* If table not found */ if (!st){ appctx->st0 = PEER_SESS_ST_EXIT; appctx->st1 = PEER_SESS_SC_ERRTABLE; goto switchstate; } /* If no peer session for current peer */ if (!ps) { appctx->st0 = PEER_SESS_ST_EXIT; appctx->st1 = PEER_SESS_SC_ERRPEER; goto switchstate; } appctx->ctx.peers.ptr = ps; appctx->st0 = PEER_SESS_ST_SENDSUCCESS; /* fall through */ } case PEER_SESS_ST_SENDSUCCESS: { struct peer_session *ps = (struct peer_session *)appctx->ctx.peers.ptr; repl = snprintf(trash.str, trash.size, "%d\n", PEER_SESS_SC_SUCCESSCODE); repl = bi_putblk(si_ic(si), trash.str, repl); if (repl <= 0) { if (repl == -1) goto full; appctx->st0 = PEER_SESS_ST_END; goto switchstate; } /* Register status code */ ps->statuscode = PEER_SESS_SC_SUCCESSCODE; /* Awake main task */ task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG); /* Init cursors */ ps->teaching_origin =ps->lastpush = ps->lastack = ps->pushack = 0; ps->pushed = ps->update; /* Init confirm counter */ ps->confirm = 0; /* reset teaching and learning flags to 0 */ ps->flags &= PEER_TEACH_RESET; ps->flags &= PEER_LEARN_RESET; /* if current peer is local */ if (ps->peer->local) { /* if table need resyncfrom local and no process assined */ if ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMLOCAL && !(ps->table->flags & SHTABLE_F_RESYNC_ASSIGN)) { /* assign local peer for a lesson, consider lesson already requested */ ps->flags |= PEER_F_LEARN_ASSIGN; ps->table->flags |= (SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS); } } else if ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMREMOTE && !(ps->table->flags & SHTABLE_F_RESYNC_ASSIGN)) { /* assign peer for a lesson */ ps->flags |= PEER_F_LEARN_ASSIGN; ps->table->flags |= SHTABLE_F_RESYNC_ASSIGN; } /* switch to waiting message state */ appctx->st0 = PEER_SESS_ST_WAITMSG; goto switchstate; } case PEER_SESS_ST_CONNECT: { struct peer_session *ps = (struct peer_session *)appctx->ctx.peers.ptr; /* Send headers */ repl = snprintf(trash.str, trash.size, PEER_SESSION_PROTO_NAME " 1.0\n%s\n%s %d\n%s %lu %d\n", ps->peer->id, localpeer, (int)getpid(), ps->table->table->id, ps->table->table->type, (int)ps->table->table->key_size); if (repl >= trash.size) { appctx->st0 = PEER_SESS_ST_END; goto switchstate; } repl = bi_putblk(si_ic(si), trash.str, repl); if (repl <= 0) { if (repl == -1) goto full; appctx->st0 = PEER_SESS_ST_END; goto switchstate; } /* switch to the waiting statuscode state */ appctx->st0 = PEER_SESS_ST_GETSTATUS; /* fall through */ } case PEER_SESS_ST_GETSTATUS: { struct peer_session *ps = (struct peer_session *)appctx->ctx.peers.ptr; if (si_ic(si)->flags & CF_WRITE_PARTIAL) ps->statuscode = PEER_SESS_SC_CONNECTEDCODE; reql = bo_getline(si_oc(si), trash.str, trash.size); if (reql <= 0) { /* closed or EOL not found */ if (reql == 0) goto out; appctx->st0 = PEER_SESS_ST_END; goto switchstate; } if (trash.str[reql-1] != '\n') { /* Incomplete line, we quit */ appctx->st0 = PEER_SESS_ST_END; goto switchstate; } else if (reql > 1 && (trash.str[reql-2] == '\r')) trash.str[reql-2] = 0; else trash.str[reql-1] = 0; bo_skip(si_oc(si), reql); /* Register status code */ ps->statuscode = atoi(trash.str); /* Awake main task */ task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG); /* If status code is success */ if (ps->statuscode == PEER_SESS_SC_SUCCESSCODE) { /* Init cursors */ ps->teaching_origin = ps->lastpush = ps->lastack = ps->pushack = 0; ps->pushed = ps->update; /* Init confirm counter */ ps->confirm = 0; /* reset teaching and learning flags to 0 */ ps->flags &= PEER_TEACH_RESET; ps->flags &= PEER_LEARN_RESET; /* If current peer is local */ if (ps->peer->local) { /* Init cursors to push a resync */ ps->teaching_origin = ps->pushed = ps->table->table->update; /* flag to start to teach lesson */ ps->flags |= PEER_F_TEACH_PROCESS; } else if ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FROMREMOTE && !(ps->table->flags & SHTABLE_F_RESYNC_ASSIGN)) { /* If peer is remote and resync from remote is needed, and no peer currently assigned */ /* assign peer for a lesson */ ps->flags |= PEER_F_LEARN_ASSIGN; ps->table->flags |= SHTABLE_F_RESYNC_ASSIGN; } } else { /* Status code is not success, abort */ appctx->st0 = PEER_SESS_ST_END; goto switchstate; } appctx->st0 = PEER_SESS_ST_WAITMSG; /* fall through */ } case PEER_SESS_ST_WAITMSG: { struct peer_session *ps = (struct peer_session *)appctx->ctx.peers.ptr; struct stksess *ts, *newts = NULL; char c; int totl = 0; reql = bo_getblk(si_oc(si), (char *)&c, sizeof(c), totl); if (reql <= 0) /* closed or EOL not found */ goto incomplete; totl += reql; if ((c & 0x80) || (c == 'D')) { /* Here we have data message */ unsigned int pushack; int srvid; uint32_t netinteger; /* Compute update remote version */ if (c & 0x80) { pushack = ps->pushack + (unsigned int)(c & 0x7F); } else { reql = bo_getblk(si_oc(si), (char *)&netinteger, sizeof(netinteger), totl); if (reql <= 0) /* closed or EOL not found */ goto incomplete; totl += reql; pushack = ntohl(netinteger); } /* Read key. The string keys are read in two steps, the first step * consists in reading whatever fits into the table directly into * the pre-allocated key. The second step consists in simply * draining all exceeding data. This can happen for example after a * config reload with a smaller key size for the stick table than * what was previously set, or when facing the impossibility to * allocate a new stksess (for example when the table is full with * "nopurge"). */ if (ps->table->table->type == STKTABLE_TYPE_STRING) { unsigned int to_read, to_store; /* read size first */ reql = bo_getblk(si_oc(si), (char *)&netinteger, sizeof(netinteger), totl); if (reql <= 0) /* closed or EOL not found */ goto incomplete; totl += reql; to_store = 0; to_read = ntohl(netinteger); if (to_read + totl > si_ob(si)->size) { /* impossible to read a key this large, abort */ reql = -1; goto incomplete; } newts = stksess_new(ps->table->table, NULL); if (newts) to_store = MIN(to_read, ps->table->table->key_size - 1); /* we read up to two blocks, the first one goes into the key, * the rest is drained into the trash. */ if (to_store) { reql = bo_getblk(si_oc(si), (char *)newts->key.key, to_store, totl); if (reql <= 0) /* closed or incomplete */ goto incomplete; newts->key.key[reql] = 0; totl += reql; to_read -= reql; } if (to_read) { reql = bo_getblk(si_oc(si), trash.str, to_read, totl); if (reql <= 0) /* closed or incomplete */ goto incomplete; totl += reql; } } else if (ps->table->table->type == STKTABLE_TYPE_INTEGER) { reql = bo_getblk(si_oc(si), (char *)&netinteger, sizeof(netinteger), totl); if (reql <= 0) /* closed or EOL not found */ goto incomplete; newts = stksess_new(ps->table->table, NULL); if (newts) { netinteger = ntohl(netinteger); memcpy(newts->key.key, &netinteger, sizeof(netinteger)); } totl += reql; } else { /* type ip or binary */ newts = stksess_new(ps->table->table, NULL); reql = bo_getblk(si_oc(si), newts ? (char *)newts->key.key : trash.str, ps->table->table->key_size, totl); if (reql <= 0) /* closed or EOL not found */ goto incomplete; totl += reql; } /* read server id */ reql = bo_getblk(si_oc(si), (char *)&netinteger, sizeof(netinteger), totl); if (reql <= 0) /* closed or EOL not found */ goto incomplete; totl += reql; srvid = ntohl(netinteger); /* update entry */ if (newts) { /* lookup for existing entry */ ts = stktable_lookup(ps->table->table, newts); if (ts) { /* the entry already exist, we can free ours */ stktable_touch(ps->table->table, ts, 0); stksess_free(ps->table->table, newts); newts = NULL; } else { struct eb32_node *eb; /* create new entry */ ts = stktable_store(ps->table->table, newts, 0); newts = NULL; /* don't reuse it */ ts->upd.key= (++ps->table->table->update)+(2^31); eb = eb32_insert(&ps->table->table->updates, &ts->upd); if (eb != &ts->upd) { eb32_delete(eb); eb32_insert(&ps->table->table->updates, &ts->upd); } } /* update entry */ if (srvid && stktable_data_ptr(ps->table->table, ts, STKTABLE_DT_SERVER_ID)) stktable_data_cast(stktable_data_ptr(ps->table->table, ts, STKTABLE_DT_SERVER_ID), server_id) = srvid; ps->pushack = pushack; } } else if (c == 'R') { /* Reset message: remote need resync */ /* reinit counters for a resync */ ps->lastpush = 0; ps->teaching_origin = ps->pushed = ps->table->table->update; /* reset teaching flags to 0 */ ps->flags &= PEER_TEACH_RESET; /* flag to start to teach lesson */ ps->flags |= PEER_F_TEACH_PROCESS; } else if (c == 'F') { /* Finish message, all known updates have been pushed by remote */ /* and remote is up to date */ /* If resync is in progress with remote peer */ if (ps->flags & PEER_F_LEARN_ASSIGN) { /* unassign current peer for learning */ ps->flags &= ~PEER_F_LEARN_ASSIGN; ps->table->flags &= ~(SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS); /* Consider table is now up2date, resync resync no more needed from local neither remote */ ps->table->flags |= (SHTABLE_F_RESYNC_LOCAL|SHTABLE_F_RESYNC_REMOTE); } /* Increase confirm counter to launch a confirm message */ ps->confirm++; } else if (c == 'c') { /* confirm message, remote peer is now up to date with us */ /* If stopping state */ if (stopping) { /* Close session, push resync no more needed */ ps->flags |= PEER_F_TEACH_COMPLETE; appctx->st0 = PEER_SESS_ST_END; goto switchstate; } /* reset teaching flags to 0 */ ps->flags &= PEER_TEACH_RESET; } else if (c == 'C') { /* Continue message, all known updates have been pushed by remote */ /* but remote is not up to date */ /* If resync is in progress with current peer */ if (ps->flags & PEER_F_LEARN_ASSIGN) { /* unassign current peer */ ps->flags &= ~PEER_F_LEARN_ASSIGN; ps->table->flags &= ~(SHTABLE_F_RESYNC_ASSIGN|SHTABLE_F_RESYNC_PROCESS); /* flag current peer is not up 2 date to try from an other */ ps->flags |= PEER_F_LEARN_NOTUP2DATE; /* reschedule a resync */ ps->table->resync_timeout = tick_add(now_ms, MS_TO_TICKS(5000)); task_wakeup(ps->table->sync_task, TASK_WOKEN_MSG); } ps->confirm++; } else if (c == 'A') { /* ack message */ uint32_t netinteger; reql = bo_getblk(si_oc(si), (char *)&netinteger, sizeof(netinteger), totl); if (reql <= 0) /* closed or EOL not found */ goto incomplete; totl += reql; /* Consider remote is up to date with "acked" version */ ps->update = ntohl(netinteger); } else { /* Unknown message */ appctx->st0 = PEER_SESS_ST_END; goto switchstate; } /* skip consumed message */ bo_skip(si_oc(si), totl); /* loop on that state to peek next message */ goto switchstate; incomplete: /* we get here when a bo_getblk() returns <= 0 in reql */ /* first, we may have to release newts */ if (newts) { stksess_free(ps->table->table, newts); newts = NULL; } if (reql < 0) { /* there was an error */ appctx->st0 = PEER_SESS_ST_END; goto switchstate; } /* Nothing to read, now we start to write */ /* Confirm finished or partial messages */ while (ps->confirm) { /* There is a confirm messages to send */ repl = bi_putchr(si_ic(si), 'c'); if (repl <= 0) { /* no more write possible */ if (repl == -1) goto full; appctx->st0 = PEER_SESS_ST_END; goto switchstate; } ps->confirm--; } /* Need to request a resync */ if ((ps->flags & PEER_F_LEARN_ASSIGN) && (ps->table->flags & SHTABLE_F_RESYNC_ASSIGN) && !(ps->table->flags & SHTABLE_F_RESYNC_PROCESS)) { /* Current peer was elected to request a resync */ repl = bi_putchr(si_ic(si), 'R'); if (repl <= 0) { /* no more write possible */ if (repl == -1) goto full; appctx->st0 = PEER_SESS_ST_END; goto switchstate; } ps->table->flags |= SHTABLE_F_RESYNC_PROCESS; } /* It remains some updates to ack */ if (ps->pushack != ps->lastack) { uint32_t netinteger; trash.str[0] = 'A'; netinteger = htonl(ps->pushack); memcpy(&trash.str[1], &netinteger, sizeof(netinteger)); repl = bi_putblk(si_ic(si), trash.str, 1+sizeof(netinteger)); if (repl <= 0) { /* no more write possible */ if (repl == -1) goto full; appctx->st0 = PEER_SESS_ST_END; goto switchstate; } ps->lastack = ps->pushack; } if (ps->flags & PEER_F_TEACH_PROCESS) { /* current peer was requested for a lesson */ if (!(ps->flags & PEER_F_TEACH_STAGE1)) { /* lesson stage 1 not complete */ struct eb32_node *eb; eb = eb32_lookup_ge(&ps->table->table->updates, ps->pushed+1); while (1) { int msglen; struct stksess *ts; if (!eb) { /* flag lesson stage1 complete */ ps->flags |= PEER_F_TEACH_STAGE1; eb = eb32_first(&ps->table->table->updates); if (eb) ps->pushed = eb->key - 1; break; } ts = eb32_entry(eb, struct stksess, upd); msglen = peer_prepare_datamsg(ts, ps, trash.str, trash.size); if (msglen) { /* message to buffer */ repl = bi_putblk(si_ic(si), trash.str, msglen); if (repl <= 0) { /* no more write possible */ if (repl == -1) goto full; appctx->st0 = PEER_SESS_ST_END; goto switchstate; } ps->lastpush = ps->pushed = ts->upd.key; } eb = eb32_next(eb); } } /* !TEACH_STAGE1 */ if (!(ps->flags & PEER_F_TEACH_STAGE2)) { /* lesson stage 2 not complete */ struct eb32_node *eb; eb = eb32_lookup_ge(&ps->table->table->updates, ps->pushed+1); while (1) { int msglen; struct stksess *ts; if (!eb || eb->key > ps->teaching_origin) { /* flag lesson stage1 complete */ ps->flags |= PEER_F_TEACH_STAGE2; ps->pushed = ps->teaching_origin; break; } ts = eb32_entry(eb, struct stksess, upd); msglen = peer_prepare_datamsg(ts, ps, trash.str, trash.size); if (msglen) { /* message to buffer */ repl = bi_putblk(si_ic(si), trash.str, msglen); if (repl <= 0) { /* no more write possible */ if (repl == -1) goto full; appctx->st0 = PEER_SESS_ST_END; goto switchstate; } ps->lastpush = ps->pushed = ts->upd.key; } eb = eb32_next(eb); } } /* !TEACH_STAGE2 */ if (!(ps->flags & PEER_F_TEACH_FINISHED)) { /* process final lesson message */ repl = bi_putchr(si_ic(si), ((ps->table->flags & SHTABLE_RESYNC_STATEMASK) == SHTABLE_RESYNC_FINISHED) ? 'F' : 'C'); if (repl <= 0) { /* no more write possible */ if (repl == -1) goto full; appctx->st0 = PEER_SESS_ST_END; goto switchstate; } /* flag finished message sent */ ps->flags |= PEER_F_TEACH_FINISHED; } /* !TEACH_FINISHED */ } /* TEACH_PROCESS */ if (!(ps->flags & PEER_F_LEARN_ASSIGN) && (int)(ps->pushed - ps->table->table->localupdate) < 0) { /* Push local updates, only if no learning in progress (to avoid ping-pong effects) */ struct eb32_node *eb; eb = eb32_lookup_ge(&ps->table->table->updates, ps->pushed+1); while (1) { int msglen; struct stksess *ts; /* push local updates */ if (!eb) { eb = eb32_first(&ps->table->table->updates); if (!eb || ((int)(eb->key - ps->pushed) <= 0)) { ps->pushed = ps->table->table->localupdate; break; } } if ((int)(eb->key - ps->table->table->localupdate) > 0) { ps->pushed = ps->table->table->localupdate; break; } ts = eb32_entry(eb, struct stksess, upd); msglen = peer_prepare_datamsg(ts, ps, trash.str, trash.size); if (msglen) { /* message to buffer */ repl = bi_putblk(si_ic(si), trash.str, msglen); if (repl <= 0) { /* no more write possible */ if (repl == -1) goto full; appctx->st0 = PEER_SESS_ST_END; goto switchstate; } ps->lastpush = ps->pushed = ts->upd.key; } eb = eb32_next(eb); } } /* ! LEARN_ASSIGN */ /* noting more to do */ goto out; } case PEER_SESS_ST_EXIT: repl = snprintf(trash.str, trash.size, "%d\n", appctx->st1); if (bi_putblk(si_ic(si), trash.str, repl) == -1) goto full; appctx->st0 = PEER_SESS_ST_END; /* fall through */ case PEER_SESS_ST_END: { si_shutw(si); si_shutr(si); si_ic(si)->flags |= CF_READ_NULL; goto out; } } } out: si_oc(si)->flags |= CF_READ_DONTWAIT; return; full: si_applet_cant_put(si); goto out; }
/* * Create a new peer session in assigned state (connect will start automatically) */ static struct stream *peer_session_create(struct peer *peer, struct peer_session *ps) { struct listener *l = LIST_NEXT(&peer->peers->peers_fe->conf.listeners, struct listener *, by_fe); struct proxy *p = (struct proxy *)l->frontend; /* attached frontend */ struct appctx *appctx; struct session *sess; struct stream *s; struct task *t; struct connection *conn; ps->reconnect = tick_add(now_ms, MS_TO_TICKS(5000)); ps->statuscode = PEER_SESS_SC_CONNECTCODE; s = NULL; appctx = appctx_new(&peer_applet); if (!appctx) goto out_close; appctx->st0 = PEER_SESS_ST_CONNECT; appctx->ctx.peers.ptr = (void *)ps; sess = session_new(p, l, &appctx->obj_type); if (!sess) { Alert("out of memory in peer_session_create().\n"); goto out_free_appctx; } if ((t = task_new()) == NULL) { Alert("out of memory in peer_session_create().\n"); goto out_free_sess; } t->nice = l->nice; if ((s = stream_new(sess, t, &appctx->obj_type)) == NULL) { Alert("Failed to initialize stream in peer_session_create().\n"); goto out_free_task; } /* The tasks below are normally what is supposed to be done by * fe->accept(). */ s->flags = SF_ASSIGNED|SF_ADDR_SET; /* applet is waiting for data */ si_applet_cant_get(&s->si[0]); appctx_wakeup(appctx); /* initiate an outgoing connection */ si_set_state(&s->si[1], SI_ST_ASS); /* automatically prepare the stream interface to connect to the * pre-initialized connection in si->conn. */ if (unlikely((conn = conn_new()) == NULL)) goto out_free_strm; conn_prepare(conn, peer->proto, peer->xprt); si_attach_conn(&s->si[1], conn); conn->target = s->target = &s->be->obj_type; memcpy(&conn->addr.to, &peer->addr, sizeof(conn->addr.to)); s->do_log = NULL; s->uniq_id = 0; s->res.flags |= CF_READ_DONTWAIT; l->nbconn++; /* warning! right now, it's up to the handler to decrease this */ p->feconn++;/* beconn will be increased later */ jobs++; if (!(s->sess->listener->options & LI_O_UNLIMITED)) actconn++; totalconn++; ps->appctx = appctx; ps->stream = s; return s; /* Error unrolling */ out_free_strm: LIST_DEL(&s->list); pool_free2(pool2_stream, s); out_free_task: task_free(t); out_free_sess: session_free(sess); out_free_appctx: appctx_free(appctx); out_close: return s; }
/* * This is the proxy management task. It enables proxies when there are enough * free sessions, or stops them when the table is full. It is designed to be * called as a task which is woken up upon stopping or when rate limiting must * be enforced. */ struct task *manage_proxy(struct task *t) { struct proxy *p = t->context; int next = TICK_ETERNITY; unsigned int wait; /* We should periodically try to enable listeners waiting for a * global resource here. */ /* first, let's check if we need to stop the proxy */ if (unlikely(stopping && p->state != PR_STSTOPPED)) { int t; t = tick_remain(now_ms, p->stop_time); if (t == 0) { Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n", p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn); send_log(p, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n", p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn); stop_proxy(p); /* try to free more memory */ pool_gc2(); } else { next = tick_first(next, p->stop_time); } } /* the rest below is just for frontends */ if (!(p->cap & PR_CAP_FE)) goto out; /* check the various reasons we may find to block the frontend */ if (unlikely(p->feconn >= p->maxconn)) { if (p->state == PR_STREADY) p->state = PR_STFULL; goto out; } /* OK we have no reason to block, so let's unblock if we were blocking */ if (p->state == PR_STFULL) p->state = PR_STREADY; if (p->fe_sps_lim && (wait = next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))) { /* we're blocking because a limit was reached on the number of * requests/s on the frontend. We want to re-check ASAP, which * means in 1 ms before estimated expiration date, because the * timer will have settled down. */ next = tick_first(next, tick_add(now_ms, wait)); goto out; } /* The proxy is not limited so we can re-enable any waiting listener */ if (!LIST_ISEMPTY(&p->listener_queue)) dequeue_all_listeners(&p->listener_queue); out: t->expire = next; task_queue(t); return t; }