Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion http/write_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package http
import (
"compress/gzip"
"context"
goerrors "errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -190,7 +191,8 @@ func (h *WriteHandler) handleWrite(w http.ResponseWriter, r *http.Request) {
requestBytes = parsed.RawSize

if err := h.PointsWriter.WritePoints(ctx, org.ID, bucket.ID, parsed.Points); err != nil {
if partialErr, ok := err.(tsdb.PartialWriteError); ok {
var partialErr tsdb.PartialWriteError
if goerrors.As(err, &partialErr) {
h.HandleHTTPError(ctx, &errors.Error{
Code: errors.EUnprocessableEntity,
Op: opWriteHandler,
Expand All @@ -200,6 +202,7 @@ func (h *WriteHandler) handleWrite(w http.ResponseWriter, r *http.Request) {
return
}

h.log.Error("Error handling Write", zap.Error(err))
h.HandleHTTPError(ctx, &errors.Error{
Code: errors.EInternal,
Op: opWriteHandler,
Expand Down
3 changes: 2 additions & 1 deletion tsdb/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,9 @@ func (s *Shard) WritePoints(ctx context.Context, points []models.Point) (rErr er
var writeError error
s.stats.writes.Observe(float64(len(points)))
defer func() {
if rErr != nil {
if rErr != nil && !errors.Is(rErr, context.DeadlineExceeded) && !errors.Is(rErr, context.Canceled) {
s.stats.writesErr.Observe(float64(len(points)))
s.logger.Error("writing points to engine failed", zap.Error(rErr))
}
}()

Expand Down
22 changes: 10 additions & 12 deletions v1/coordinator/points_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import (
)

var (
// ErrTimeout is returned when a write times out.
ErrTimeout = errors.New("timeout")

// ErrWriteFailed is returned when no writes succeeded.
ErrWriteFailed = errors.New("write failed")
)
Expand Down Expand Up @@ -459,26 +456,28 @@ func (w *PointsWriter) WritePointsPrivileged(
return err
}

// Set a timeout using the context object.
ctx, cancel := context.WithTimeout(ctx, w.WriteTimeout)
defer cancel()

// Write each shard in it's own goroutine and return as soon as one fails.
ch := make(chan error, len(shardMappings.Points))
for shardID, points := range shardMappings.Points {
go func(shard *meta.ShardInfo, database, retentionPolicy string, points []models.Point) {
err := w.writeToShard(ctx, shard, database, retentionPolicy, points)
if err == nil {
w.stats.pointsWriteOk.Observe(float64(len(points)))
} else {
} else if !errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, context.Canceled) {
w.stats.pointsWriteErr.Observe(float64(len(points)))
w.Logger.Error("writing points to shard failed", zap.Error(err))
}
if err == tsdb.ErrShardDeletion {
if errors.Is(err, tsdb.ErrShardDeletion) {
err = tsdb.PartialWriteError{Reason: fmt.Sprintf("shard %d is pending deletion", shard.ID), Dropped: len(points)}
}
ch <- err
}(shardMappings.Shards[shardID], database, retentionPolicy, points)
}

timeout := time.NewTimer(w.WriteTimeout)
defer timeout.Stop()

if err == nil && shardMappings.Dropped() > 0 {
w.stats.pointsWriteDropped.Observe(float64(shardMappings.Dropped()))
err = tsdb.PartialWriteError{Reason: shardMappings.SummariseDropped(),
Expand All @@ -492,11 +491,10 @@ func (w *PointsWriter) WritePointsPrivileged(
select {
case <-w.closing:
return ErrWriteFailed
case <-timeout.C:
w.stats.timeout.Inc()
// return timeout error to caller
return ErrTimeout
case err := <-ch:
if errors.Is(err, context.Canceled) {
w.stats.timeout.Inc()
}
if err != nil {
return err
}
Expand Down