| index 52b031a..9344581 100644 |
| --- a/internal/docs/push.go |
| +++ b/internal/docs/push.go |
| @@ -220,64 +220,111 @@ func (c *frameCache) put(seq int64, b []byte) { |
| } |
| } |
| |
| -// pushRow is one row read for a stream: its bytes, or why it has none. |
| +// pushRow is one row for a stream: its bytes, or why it has none. |
| type pushRow struct { |
| seq int64 |
| bytes []byte |
| unusable bool // could not be opened |
| + tooLarge bool // over pushMaxRowBytes: goes as update, never loaded |
| } |
| |
| -// rowsAfter reads doc's rows after sent, in seq order, at most |
| -// pushMaxBehindRow+1 of them, through cache when it has them all. The |
| -// query and the opening are updatesGet's (§3.1: one code path for bytes). |
| -func (h *handlers) rowsAfter(ctx context.Context, doc *Document, sent int64, cache *frameCache) ([]pushRow, int64, error) { |
| - var current int64 |
| - if err := h.store.db.WithContext(ctx).Model(&DocUpdate{}).Where("doc_id = ?", doc.ID).Select("COALESCE(MAX(seq), 0)").Scan(¤t).Error; err != nil { |
| - return nil, 0, err |
| +// pushSealSlack is how much longer a stored (sealed) row may be than its |
| +// update: the length check before loading is on the stored bytes. |
| +const pushSealSlack = 256 |
| + |
| +// rowsAfter returns doc's rows after sent, in seq order, and the log's |
| +// current seq. far reports a stream more than pushMaxBehindRow rows or |
| +// pushMaxBehind bytes behind: no rows are returned and none was read. The |
| +// decision is made from the rows' count and stored lengths before any |
| +// row's bytes are read or opened, and a row too large to push is never |
| +// loaded (#175 finding 2). The rest come through cache when it holds them |
| +// all, otherwise with updatesGet's query and opening (§3.1). |
| +func (h *handlers) rowsAfter(ctx context.Context, doc *Document, sent int64, cache *frameCache) (rows []pushRow, current int64, far bool, err error) { |
| + db := h.store.db.WithContext(ctx) |
| + if err = db.Model(&DocUpdate{}).Where("doc_id = ?", doc.ID).Select("COALESCE(MAX(seq), 0)").Scan(¤t).Error; err != nil { |
| + return nil, 0, false, err |
| } |
| if current <= sent { |
| - return nil, current, nil |
| + return nil, current, false, nil |
| + } |
| + if current-sent > pushMaxBehindRow { |
| + return nil, current, true, nil |
| } |
| - if cache != nil && current-sent <= pushMaxBehindRow { |
| - rows := make([]pushRow, 0, current-sent) |
| + if cache != nil { |
| + rows = make([]pushRow, 0, current-sent) |
| + total := 0 |
| for seq := sent + 1; seq <= current; seq++ { |
| b, ok := cache.get(seq) |
| if !ok { |
| rows = nil |
| break |
| } |
| + total += len(b) |
| rows = append(rows, pushRow{seq: seq, bytes: b}) |
| } |
| if rows != nil { |
| - if h.pushStats != nil { |
| - h.pushStats.cacheHits.Add(int64(len(rows))) |
| + if total > pushMaxBehind { |
| + return nil, current, true, nil |
| } |
| - return rows, current, nil |
| + h.pushStats.cacheHits.Add(int64(len(rows))) |
| + return rows, current, false, nil |
| } |
| } |
| - var updates []DocUpdate |
| - if err := h.store.db.WithContext(ctx). |
| + var lengths []struct { |
| + Seq int64 |
| + N int64 |
| + } |
| + if err = db.Model(&DocUpdate{}).Select("seq, LENGTH(update_bin) AS n"). |
| Where("doc_id = ? AND seq > ? AND seq <= ?", doc.ID, sent, current). |
| - Order("seq ASC").Limit(pushMaxBehindRow + 1). |
| - Find(&updates).Error; err != nil { |
| - return nil, 0, err |
| + Order("seq ASC").Scan(&lengths).Error; err != nil { |
| + return nil, 0, false, err |
| } |
| - if h.pushStats != nil { |
| - h.pushStats.rowReads.Add(int64(len(updates))) |
| + var total int64 |
| + var small []int64 |
| + for _, l := range lengths { |
| + if l.N <= pushMaxRowBytes+pushSealSlack { |
| + total += l.N |
| + small = append(small, l.Seq) |
| + } |
| } |
| - rows := make([]pushRow, 0, len(updates)) |
| - for i := range updates { |
| - u := &updates[i] |
| - if err := h.store.openContentUpdate(doc.GUID, u); err != nil { |
| - rows = append(rows, pushRow{seq: u.Seq, unusable: true}) |
| - continue |
| + if total > pushMaxBehind+int64(len(small))*pushSealSlack { |
| + return nil, current, true, nil |
| + } |
| + loaded := map[int64][]byte{} |
| + unusable := map[int64]bool{} |
| + if len(small) > 0 { |
| + var updates []DocUpdate |
| + if err = db.Where("doc_id = ? AND seq IN ?", doc.ID, small).Find(&updates).Error; err != nil { |
| + return nil, 0, false, err |
| + } |
| + h.pushStats.rowReads.Add(int64(len(updates))) |
| + for i := range updates { |
| + u := &updates[i] |
| + if err := h.store.openContentUpdate(doc.GUID, u); err != nil { |
| + unusable[u.Seq] = true |
| + continue |
| + } |
| + loaded[u.Seq] = u.UpdateBin |
| + if cache != nil && len(u.UpdateBin) <= pushMaxRowBytes { |
| + cache.put(u.Seq, u.UpdateBin) |
| + } |
| } |
| - if cache != nil { |
| - cache.put(u.Seq, u.UpdateBin) |
| + } |
| + rows = make([]pushRow, 0, len(lengths)) |
| + for _, l := range lengths { |
| + b, ok := loaded[l.Seq] |
| + switch { |
| + case ok && len(b) <= pushMaxRowBytes: |
| + rows = append(rows, pushRow{seq: l.Seq, bytes: b}) |
| + case ok: |
| + rows = append(rows, pushRow{seq: l.Seq, tooLarge: true}) |
| + case unusable[l.Seq]: |
| + rows = append(rows, pushRow{seq: l.Seq, unusable: true}) |
| + default: |
| + rows = append(rows, pushRow{seq: l.Seq, tooLarge: true}) |
| } |
| - rows = append(rows, pushRow{seq: u.Seq, bytes: u.UpdateBin}) |
| } |
| - return rows, current, nil |
| + return rows, current, false, nil |
| } |
| |
| // pushFrames renders rows as frames for stream id (§3.1): own for an update |
| @@ -286,24 +333,24 @@ func (h *handlers) rowsAfter(ctx context.Context, doc *Document, sent int64, cac |
| // than pushMaxBehindRow rows or pushMaxBehind bytes) is one update for the |
| // log's current seq. It returns the text to write in one flush and the new |
| // cursor. |
| -func (h *handlers) pushFrames(docID int64, id string, rows []pushRow, current, sent int64) (string, int64) { |
| +func (h *handlers) pushFrames(docID int64, id string, rows []pushRow, current, sent int64, far bool) (string, int64) { |
| + if far { |
| + return "event: update\ndata: " + strconv.FormatInt(current, 10) + "\n\n", current |
| + } |
| if len(rows) == 0 { |
| return "", sent |
| } |
| - total := 0 |
| - for _, r := range rows { |
| - total += len(r.bytes) |
| - } |
| last := rows[len(rows)-1].seq |
| - if len(rows) > pushMaxBehindRow || total > pushMaxBehind { |
| - return "event: update\ndata: " + strconv.FormatInt(current, 10) + "\n\n", current |
| - } |
| var b strings.Builder |
| for _, r := range rows { |
| seq := strconv.FormatInt(r.seq, 10) |
| switch o, ok := h.pushRegistry.lookup(docID, r.seq); { |
| - case r.unusable || len(r.bytes) > pushMaxRowBytes: |
| - b.WriteString("event: update\ndata: " + seq + "\n\n") |
| + case r.unusable || r.tooLarge || len(r.bytes) > pushMaxRowBytes: |
| + // The client fetches from its cursor, which brings this row and |
| + // everything after it: one update for the current seq ends the |
| + // flush, rather than an update per remaining row. |
| + b.WriteString("event: update\ndata: " + strconv.FormatInt(current, 10) + "\n\n") |
| + return b.String(), current |
| case ok && o.stream == id && o.hash == sha256.Sum256(r.bytes): |
| b.WriteString("id: " + seq + "\nevent: own\ndata: " + seq + " " + o.counter + "\n\n") |
| default: |