Skip to content

Commit 41a1e4f

Browse files
author
Abtahi
committed
xdrive: grow segment size until delete latency rises
1 parent 2a5ec3d commit 41a1e4f

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

‎transport/internet/xdrive/drive.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ type driveStorage struct {
6565

6666
flightMu sync.Mutex
6767
flights map[string]*listFlight
68+
69+
delMu sync.Mutex
70+
delAvg time.Duration
6871
}
6972

7073
type listFlight struct {
@@ -536,6 +539,12 @@ func (s *driveStorage) getInline(ctx context.Context, id string) ([]byte, error)
536539
}
537540

538541
func (s *driveStorage) deleteID(ctx context.Context, flat, id string) error {
542+
start := time.Now()
543+
defer func() {
544+
s.delMu.Lock()
545+
s.delAvg = (s.delAvg + time.Since(start)) / 2
546+
s.delMu.Unlock()
547+
}()
539548
status, payload, err := s.do(ctx, http.MethodDelete,
540549
s.filesURL+"/"+url.PathEscape(id)+"?supportsAllDrives=true", "", nil)
541550
if err != nil {
@@ -550,6 +559,12 @@ func (s *driveStorage) deleteID(ctx context.Context, flat, id string) error {
550559
}
551560
}
552561

562+
func (s *driveStorage) deleteAvg() time.Duration {
563+
s.delMu.Lock()
564+
defer s.delMu.Unlock()
565+
return s.delAvg
566+
}
567+
553568
func (s *driveStorage) Delete(ctx context.Context, name string) error {
554569
flat := flatten(name)
555570

‎transport/internet/xdrive/wal.go‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type walWriter struct {
5252
mu sync.Mutex
5353
buf []byte
5454
seq int64
55+
segBytes int
5556
lastSize int
5657
held int
5758
closed bool
@@ -60,11 +61,12 @@ type walWriter struct {
6061

6162
func newWALWriter(ctx context.Context, storage Storage, prefix string, p params) *walWriter {
6263
w := &walWriter{
63-
ctx: ctx,
64-
storage: storage,
65-
prefix: prefix,
66-
params: p,
67-
sem: make(chan struct{}, p.concurrency),
64+
ctx: ctx,
65+
storage: storage,
66+
prefix: prefix,
67+
params: p,
68+
segBytes: p.segmentBytes,
69+
sem: make(chan struct{}, p.concurrency),
6870
}
6971
go w.flushLoop()
7072
return w
@@ -82,7 +84,7 @@ func (w *walWriter) Write(p []byte) (int, error) {
8284
}
8385

8486
w.buf = append(w.buf, p...)
85-
for len(w.buf) >= w.segmentBytes {
87+
for len(w.buf) >= w.segBytes {
8688
if err := w.flushLocked(); err != nil {
8789
return 0, err
8890
}
@@ -134,8 +136,8 @@ func (w *walWriter) flushLocked() error {
134136
}
135137

136138
n := len(w.buf)
137-
if n > w.segmentBytes {
138-
n = w.segmentBytes
139+
if n > w.segBytes {
140+
n = w.segBytes
139141
}
140142

141143
chunk := make([]byte, n)
@@ -151,6 +153,15 @@ func (w *walWriter) flushLocked() error {
151153
w.lastSize = len(w.buf)
152154
w.held = 0
153155

156+
if w.segBytes < 4*1024*1024 {
157+
if d, ok := w.storage.(interface{ deleteAvg() time.Duration }); ok && d.deleteAvg() < 2*time.Second {
158+
w.segBytes *= 2
159+
if w.segBytes > 4*1024*1024 {
160+
w.segBytes = 4 * 1024 * 1024
161+
}
162+
}
163+
}
164+
154165
w.wg.Add(1)
155166
w.mu.Unlock()
156167
select {

0 commit comments

Comments
 (0)