Packet* zblock2packet(ZBlock *zb, uint32_t size) { Packet *p; if(zb == nil) return nil; p = packetalloc(); packetappend(p, zb->data, size); return p; }
int myvtwrite(VtConn *z, uchar score[VtScoreSize], uint type, uchar *buf, int n) { WriteReq wr; if(nwritethread == 0){ n = vtwrite(z, score, type, buf, n); if(n < 0) sysfatal("vtwrite: %r"); return n; } wr.p = packetalloc(); packetappend(wr.p, buf, n); packetsha1(wr.p, score); memmove(wr.score, score, VtScoreSize); wr.type = type; nsend++; send(writechan, &wr); return 0; }
static void ventiserver(void *v) { Packet *p; VtReq *r; char err[ERRMAX]; uint ms; int ok; struct map *m; USED(v); threadsetname("ventiserver"); trace(TraceWork, "start"); while((r = vtgetreq(ventisrv)) != nil){ trace(TraceWork, "finish"); trace(TraceWork, "start request %F", &r->tx); trace(TraceRpc, "<- %F", &r->tx); r->rx.msgtype = r->tx.msgtype+1; addstat(StatRpcTotal, 1); if(0) print("req (arenas[0]=%p sects[0]=%p) %F\n", mainindex->arenas[0], mainindex->sects[0], &r->tx); switch(r->tx.msgtype){ default: vtrerror(r, "unknown request"); break; case VtTread: ms = msec(); m = findscore(r->tx.score); //fprint(2, "findscore says %p\n", m); if (m) { //fprint(2, "Found the block\n"); r->rx.data = packetalloc(); packetappend(r->rx.data, m->data, m->len); r->rx.blocktype = m->blocktype; } else { r->rx.data = nil; } ms = msec() - ms; addstat2(StatRpcRead, 1, StatRpcReadTime, ms); if(r->rx.data == nil){ addstat(StatRpcReadFail, 1); rerrstr(err, sizeof err); vtrerror(r, err); }else{ addstat(StatRpcReadBytes, packetsize(r->rx.data)); addstat(StatRpcReadOk, 1); addstat2(StatRpcReadCached, 1, StatRpcReadCachedTime, ms); r->rx.msgtype = VtRread; r->rx.error = nil; } break; case VtTwrite: if(readonly){ vtrerror(r, "read only"); break; } p = r->tx.data; r->tx.data = nil; addstat(StatRpcWriteBytes, packetsize(p)); ms = msec(); /* todo: check for overflow of file */ ok = putscore(p, r->rx.score, r->tx.blocktype); ms = msec() - ms; addstat2(StatRpcWrite, 1, StatRpcWriteTime, ms); if(ok < 0){ addstat(StatRpcWriteFail, 1); rerrstr(err, sizeof err); vtrerror(r, err); } else { r->rx.msgtype = VtRwrite; r->rx.error = nil; } break; case VtTsync: /* nonsense. Write synchronously. For now. Later, have a helper thread and VtTsync will just write a Fence to it and wait for it to come back. */ break; } trace(TraceRpc, "-> %F", &r->rx); vtrespond(r); trace(TraceWork, "start"); } threadexitsall(0); }