int se_reqwrite(sereq *r) { sereqarg *arg = &r->arg; svlog *log = r->arg.log; se *e = se_of(r->object); /* set lsn */ sl_prepare(&e->lp, log, arg->lsn); /* log write */ if (! arg->recover) { sltx tl; sl_begin(&e->lp, &tl); int rc = sl_write(&tl, log); if (ssunlikely(rc == -1)) { sl_rollback(&tl); r->rc = -1; return -1; } sl_commit(&tl); } /* commit */ if (sslikely(arg->vlsn_generate)) arg->vlsn = sx_vlsn(&e->xm); uint64_t now = ss_utime(); svlogindex *i = (svlogindex*)log->index.s; svlogindex *end = (svlogindex*)log->index.p; while (i < end) { sedb *db = i->ptr; sitx ti; si_begin(&ti, &db->index, arg->vlsn, now, log, i); si_write(&ti, arg->recover); si_commit(&ti); i++; } return 0; }
static int se_run(setask *t, seworker *w) { si_plannertrace(&t->plan, &w->trace); sedb *db = t->db; se *e = (se*)db->o.env; uint64_t vlsn = sx_vlsn(&e->xm); return si_execute(&db->index, &w->dc, &t->plan, vlsn); }
static int so_execute(sotask *t, soworker *w) { si_plannertrace(&t->plan, &w->trace); sodb *db = t->db; so *e = (so*)db->o.env; uint64_t vlsn = sx_vlsn(&e->xm); return si_execute(&db->index, &db->r, &w->dc, &t->plan, vlsn); }
static void* se_worker(void *arg) { ssthread *self = arg; se *e = self->arg; ss_thread_setname(self, "worker"); ss_vfsioprio_low(&e->vfs); scworker *w = sc_workerpool_pop(&e->scheduler.wp, &e->r); if (ssunlikely(w == NULL)) return NULL; for (;;) { int rc = se_active(e); if (ssunlikely(rc == 0)) break; rc = sc_step(&e->scheduler, w, sx_vlsn(&e->xm)); if (ssunlikely(rc == -1)) break; if (ssunlikely(rc == 0)) ss_sleep(10000000); /* 10ms */ } sc_workerpool_push(&e->scheduler.wp, w); return NULL; }
static int se_schedule(sescheduler *s, setask *task, seworker *w) { ss_trace(&w->trace, "%s", "schedule"); si_planinit(&task->plan); uint64_t now = ss_utime(); se *e = (se*)s->env; sedb *db; srzone *zone = se_zoneof(e); assert(zone != NULL); task->checkpoint_complete = 0; task->backup_complete = 0; task->rotate = 0; task->req = 0; task->gc = 0; task->db = NULL; ss_mutexlock(&s->lock); /* asynchronous reqs dispatcher */ if (s->req == 0) { switch (zone->async) { case 2: if (se_reqqueue(e) == 0) break; case 1: s->req = 1; task->req = zone->async; ss_mutexunlock(&s->lock); return 0; } } /* log gc and rotation */ if (s->rotate == 0) { task->rotate = 1; s->rotate = 1; } /* checkpoint */ int in_progress = 0; int rc; checkpoint: if (s->checkpoint) { task->plan.plan = SI_CHECKPOINT; task->plan.a = s->checkpoint_lsn; rc = se_schedule_plan(s, &task->plan, &db); switch (rc) { case 1: s->workers_branch++; se_dbref(db, 1); task->db = db; task->gc = 1; ss_mutexunlock(&s->lock); return 1; case 2: /* work in progress */ in_progress = 1; break; case 0: /* complete checkpoint */ s->checkpoint = 0; s->checkpoint_lsn_last = s->checkpoint_lsn; s->checkpoint_lsn = 0; task->checkpoint_complete = 1; break; } } /* apply zone policy */ switch (zone->mode) { case 0: /* compact_index */ case 1: /* compact_index + branch_count prio */ assert(0); break; case 2: /* checkpoint */ { if (in_progress) { ss_mutexunlock(&s->lock); return 0; } uint64_t lsn = sr_seq(&e->seq, SR_LSN); s->checkpoint_lsn = lsn; s->checkpoint = 1; goto checkpoint; } default: /* branch + compact */ assert(zone->mode == 3); } /* database shutdown-drop */ if (s->workers_gc_db < zone->gc_db_prio) { ss_spinlock(&e->dblock); db = NULL; if (ssunlikely(e->db_shutdown.n > 0)) { db = (sedb*)so_listfirst(&e->db_shutdown); if (se_dbgarbage(db)) { so_listdel(&e->db_shutdown, &db->o); } else { db = NULL; } } ss_spinunlock(&e->dblock); if (ssunlikely(db)) { if (db->dropped) task->plan.plan = SI_DROP; else task->plan.plan = SI_SHUTDOWN; s->workers_gc_db++; se_dbref(db, 1); task->db = db; ss_mutexunlock(&s->lock); return 1; } } /* backup */ if (s->backup && (s->workers_backup < zone->backup_prio)) { /* backup procedure. * * state 0 (start) * ------- * * a. disable log gc * b. mark to start backup (state 1) * * state 1 (background, delayed start) * ------- * * a. create backup_path/<bsn.incomplete> directory * b. create database directories * c. create log directory * d. state 2 * * state 2 (background, copy) * ------- * * a. schedule and execute node backup which bsn < backup_bsn * b. state 3 * * state 3 (background, completion) * ------- * * a. rotate log file * b. copy log files * c. enable log gc, schedule gc * d. rename <bsn.incomplete> into <bsn> * e. set last backup, set COMPLETE * */ if (s->backup == 1) { /* state 1 */ rc = se_backupstart(s); if (ssunlikely(rc == -1)) { se_backuperror(s); goto backup_error; } s->backup = 2; } /* state 2 */ task->plan.plan = SI_BACKUP; task->plan.a = s->backup_bsn; rc = se_schedule_plan(s, &task->plan, &db); switch (rc) { case 1: s->workers_backup++; se_dbref(db, 1); task->db = db; ss_mutexunlock(&s->lock); return 1; case 2: /* work in progress */ break; case 0: /* state 3 */ rc = se_backupcomplete(s, w); if (ssunlikely(rc == -1)) { se_backuperror(s); goto backup_error; } s->backup_events++; task->gc = 1; task->backup_complete = 1; break; } backup_error:; } /* garbage-collection */ if (s->gc) { if (s->workers_gc < zone->gc_prio) { task->plan.plan = SI_GC; task->plan.a = sx_vlsn(&e->xm); task->plan.b = zone->gc_wm; rc = se_schedule_plan(s, &task->plan, &db); switch (rc) { case 1: s->workers_gc++; se_dbref(db, 1); task->db = db; ss_mutexunlock(&s->lock); return 1; case 2: /* work in progress */ break; case 0: /* state 3 */ s->gc = 0; s->gc_last = now; break; } } } else { if (zone->gc_prio && zone->gc_period) { if ( (now - s->gc_last) >= ((uint64_t)zone->gc_period * 1000000) ) { s->gc = 1; } } } /* index aging */ if (s->age) { if (s->workers_branch < zone->branch_prio) { task->plan.plan = SI_AGE; task->plan.a = zone->branch_age * 1000000; /* ms */ task->plan.b = zone->branch_age_wm; rc = se_schedule_plan(s, &task->plan, &db); switch (rc) { case 1: s->workers_branch++; se_dbref(db, 1); task->db = db; ss_mutexunlock(&s->lock); return 1; case 0: s->age = 0; s->age_last = now; break; } } } else { if (zone->branch_prio && zone->branch_age_period) { if ( (now - s->age_last) >= ((uint64_t)zone->branch_age_period * 1000000) ) { s->age = 1; } } } /* branching */ if (s->workers_branch < zone->branch_prio) { /* schedule branch task using following * priority: * * a. peek node with the largest in-memory index * which is equal or greater then branch * watermark. * If nothing is found, stick to b. * * b. peek node with the largest in-memory index, * which has oldest update time. * * c. if no branch work is needed, schedule a * compaction job * */ task->plan.plan = SI_BRANCH; task->plan.a = zone->branch_wm; rc = se_schedule_plan(s, &task->plan, &db); if (rc == 1) { s->workers_branch++; se_dbref(db, 1); task->db = db; task->gc = 1; ss_mutexunlock(&s->lock); return 1; } } /* compaction */ task->plan.plan = SI_COMPACT; task->plan.a = zone->compact_wm; task->plan.b = zone->compact_mode; rc = se_schedule_plan(s, &task->plan, &db); if (rc == 1) { se_dbref(db, 1); task->db = db; ss_mutexunlock(&s->lock); return 1; } ss_mutexunlock(&s->lock); return 0; }
int se_scheduler_branch(void *arg) { sedb *db = arg; se *e = se_of(&db->o); srzone *z = se_zoneof(e); seworker stub; se_workerstub_init(&stub); int rc; while (1) { uint64_t vlsn = sx_vlsn(&e->xm); siplan plan = { .explain = SI_ENONE, .plan = SI_BRANCH, .a = z->branch_wm, .b = 0, .c = 0, .node = NULL }; rc = si_plan(&db->index, &plan); if (rc == 0) break; rc = si_execute(&db->index, &stub.dc, &plan, vlsn); if (ssunlikely(rc == -1)) break; } se_workerstub_free(&stub, &db->r); return rc; } int se_scheduler_compact(void *arg) { sedb *db = arg; se *e = se_of(&db->o); srzone *z = se_zoneof(e); seworker stub; se_workerstub_init(&stub); int rc; while (1) { uint64_t vlsn = sx_vlsn(&e->xm); siplan plan = { .explain = SI_ENONE, .plan = SI_COMPACT, .a = z->compact_wm, .b = z->compact_mode, .c = 0, .node = NULL }; rc = si_plan(&db->index, &plan); if (rc == 0) break; rc = si_execute(&db->index, &stub.dc, &plan, vlsn); if (ssunlikely(rc == -1)) break; } se_workerstub_free(&stub, &db->r); return rc; } int se_scheduler_checkpoint(void *arg) { se *o = arg; sescheduler *s = &o->sched; uint64_t lsn = sr_seq(&o->seq, SR_LSN); ss_mutexlock(&s->lock); s->checkpoint_lsn = lsn; s->checkpoint = 1; ss_mutexunlock(&s->lock); return 0; }
int se_scheduler_branch(void *arg) { sedb *db = arg; se *e = se_of(&db->o); srzone *z = se_zoneof(e); seworker *w = se_workerpool_pop(&e->sched.workers, &e->r); if (ssunlikely(w == NULL)) return -1; int rc; while (1) { uint64_t vlsn = sx_vlsn(&e->xm); uint64_t vlsn_lru = si_lru_vlsn(&db->index); siplan plan = { .explain = SI_ENONE, .plan = SI_BRANCH, .a = z->branch_wm, .b = 0, .c = 0, .node = NULL }; rc = si_plan(&db->index, &plan); if (rc == 0) break; rc = si_execute(&db->index, &w->dc, &plan, vlsn, vlsn_lru); if (ssunlikely(rc == -1)) break; } se_workerpool_push(&e->sched.workers, w); return rc; } int se_scheduler_compact(void *arg) { sedb *db = arg; se *e = se_of(&db->o); srzone *z = se_zoneof(e); seworker *w = se_workerpool_pop(&e->sched.workers, &e->r); if (ssunlikely(w == NULL)) return -1; int rc; while (1) { uint64_t vlsn = sx_vlsn(&e->xm); uint64_t vlsn_lru = si_lru_vlsn(&db->index); siplan plan = { .explain = SI_ENONE, .plan = SI_COMPACT, .a = z->compact_wm, .b = z->compact_mode, .c = 0, .node = NULL }; rc = si_plan(&db->index, &plan); if (rc == 0) break; rc = si_execute(&db->index, &w->dc, &plan, vlsn, vlsn_lru); if (ssunlikely(rc == -1)) break; } se_workerpool_push(&e->sched.workers, w); return rc; } int se_scheduler_compact_index(void *arg) { sedb *db = arg; se *e = se_of(&db->o); srzone *z = se_zoneof(e); seworker *w = se_workerpool_pop(&e->sched.workers, &e->r); if (ssunlikely(w == NULL)) return -1; int rc; while (1) { uint64_t vlsn = sx_vlsn(&e->xm); uint64_t vlsn_lru = si_lru_vlsn(&db->index); siplan plan = { .explain = SI_ENONE, .plan = SI_COMPACT_INDEX, .a = z->branch_wm, .b = 0, .c = 0, .node = NULL }; rc = si_plan(&db->index, &plan); if (rc == 0) break; rc = si_execute(&db->index, &w->dc, &plan, vlsn, vlsn_lru); if (ssunlikely(rc == -1)) break; } se_workerpool_push(&e->sched.workers, w); return rc; } int se_scheduler_anticache(void *arg) { se *o = arg; sescheduler *s = &o->sched; uint64_t asn = sr_seq(&o->seq, SR_ASNNEXT); ss_mutexlock(&s->lock); s->anticache_asn = asn; s->anticache_storage = o->conf.anticache; s->anticache = 1; ss_mutexunlock(&s->lock); return 0; }