Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add statistics for hinted handoff #4265

Merged
merged 3 commits into from
Sep 29, 2015
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [#4196](https://github.com/influxdb/influxdb/pull/4196): Export tsdb.Iterator
- [#4198](https://github.com/influxdb/influxdb/pull/4198): Add basic cluster-service stats
- [#4262](https://github.com/influxdb/influxdb/pull/4262): Allow configuration of UDP retention policy
- [#4265](https://github.com/influxdb/influxdb/pull/4265): Add statistics for Hinted-Handoff

### Bugfixes
- [#4166](https://github.com/influxdb/influxdb/pull/4166): Fix parser error on invalid SHOW
Expand Down
46 changes: 42 additions & 4 deletions services/hh/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hh

import (
"encoding/binary"
"expvar"
"fmt"
"io/ioutil"
"log"
Expand All @@ -11,10 +12,17 @@ import (
"sync"
"time"

"github.com/influxdb/influxdb"
"github.com/influxdb/influxdb/models"
"github.com/influxdb/influxdb/tsdb"
)

const (
pointsHint = "points_hint"
pointsWrite = "points_write"
bytesWrite = "bytes_write"
)

type Processor struct {
mu sync.RWMutex

Expand All @@ -26,6 +34,10 @@ type Processor struct {
queues map[uint64]*queue
writer shardWriter
Logger *log.Logger

// Shard-level and node-level HH stats.
shardStatMaps map[uint64]*expvar.Map
nodeStatMaps map[uint64]*expvar.Map
}

type ProcessorOptions struct {
Expand All @@ -35,10 +47,12 @@ type ProcessorOptions struct {

func NewProcessor(dir string, writer shardWriter, options ProcessorOptions) (*Processor, error) {
p := &Processor{
dir: dir,
queues: map[uint64]*queue{},
writer: writer,
Logger: log.New(os.Stderr, "[handoff] ", log.LstdFlags),
dir: dir,
queues: map[uint64]*queue{},
writer: writer,
Logger: log.New(os.Stderr, "[handoff] ", log.LstdFlags),
shardStatMaps: make(map[uint64]*expvar.Map),
nodeStatMaps: make(map[uint64]*expvar.Map),
}
p.setOptions(options)

Expand Down Expand Up @@ -101,6 +115,11 @@ func (p *Processor) addQueue(nodeID uint64) (*queue, error) {
return nil, err
}
p.queues[nodeID] = queue

// Create node stats for this queue.
key := fmt.Sprintf("hh_processor:node:%d", nodeID)
tags := map[string]string{"nodeID": strconv.FormatUint(nodeID, 10)}
p.nodeStatMaps[nodeID] = influxdb.NewStatistics(key, "hh_processor", tags)
return queue, nil
}

Expand All @@ -113,6 +132,10 @@ func (p *Processor) WriteShard(shardID, ownerID uint64, points []models.Point) e
}
}

// Update stats
p.updateShardStats(shardID, pointsHint, int64(len(points)))
p.nodeStatMaps[ownerID].Add(pointsHint, int64(len(points)))

b := p.marshalWrite(shardID, points)
return queue.Append(b)
}
Expand Down Expand Up @@ -159,6 +182,8 @@ func (p *Processor) Process() error {
res <- nil
break
}
p.updateShardStats(shardID, pointsWrite, int64(len(points)))
p.nodeStatMaps[nodeID].Add(pointsWrite, int64(len(points)))

// If we get here, the write succeeded so advance the queue to the next item
if err := q.Advance(); err != nil {
Expand All @@ -170,6 +195,8 @@ func (p *Processor) Process() error {

// Update how many bytes we've sent
limiter.Update(len(buf))
p.updateShardStats(shardID, bytesWrite, int64(len(buf)))
p.nodeStatMaps[nodeID].Add(bytesWrite, int64(len(buf)))

// Block to maintain the throughput rate
time.Sleep(limiter.Delay())
Expand Down Expand Up @@ -206,6 +233,17 @@ func (p *Processor) unmarshalWrite(b []byte) (uint64, []models.Point, error) {
return ownerID, points, err
}

func (p *Processor) updateShardStats(shardID uint64, stat string, inc int64) {
m, ok := p.shardStatMaps[shardID]
if !ok {
key := fmt.Sprintf("hh_processor:shard:%d", shardID)
tags := map[string]string{"shardID": strconv.FormatUint(shardID, 10)}
p.shardStatMaps[shardID] = influxdb.NewStatistics(key, "hh_processor", tags)
m = p.shardStatMaps[shardID]
}
m.Add(stat, inc)
}

func (p *Processor) PurgeOlderThan(when time.Duration) error {
p.mu.Lock()
defer p.mu.Unlock()
Expand Down
27 changes: 23 additions & 4 deletions services/hh/service.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
package hh

import (
"expvar"
"fmt"
"io"
"log"
"os"
"strings"
"sync"
"time"

"github.com/influxdb/influxdb"
"github.com/influxdb/influxdb/models"
)

var ErrHintedHandoffDisabled = fmt.Errorf("hinted handoff disabled")

const (
writeShardReq = "write_shard_req"
writeShardReqPoints = "write_shard_req_points"
processReq = "process_req"
processReqFail = "process_req_fail"
)

type Service struct {
mu sync.RWMutex
wg sync.WaitGroup
closing chan struct{}

Logger *log.Logger
cfg Config
statMap *expvar.Map
Logger *log.Logger
cfg Config

ShardWriter shardWriter

Expand All @@ -36,9 +47,13 @@ type shardWriter interface {

// NewService returns a new instance of Service.
func NewService(c Config, w shardWriter) *Service {
key := strings.Join([]string{"hh", c.Dir}, ":")
tags := map[string]string{"path": c.Dir}

s := &Service{
cfg: c,
Logger: log.New(os.Stderr, "[handoff] ", log.LstdFlags),
cfg: c,
statMap: influxdb.NewStatistics(key, "hh", tags),
Logger: log.New(os.Stderr, "[handoff] ", log.LstdFlags),
}
processor, err := NewProcessor(c.Dir, w, ProcessorOptions{
MaxSize: c.MaxSize,
Expand Down Expand Up @@ -93,6 +108,8 @@ func (s *Service) SetLogger(l *log.Logger) {

// WriteShard queues the points write for shardID to node ownerID to handoff queue
func (s *Service) WriteShard(shardID, ownerID uint64, points []models.Point) error {
s.statMap.Add(writeShardReq, 1)
s.statMap.Add(writeShardReqPoints, int64(len(points)))
if !s.cfg.Enabled {
return ErrHintedHandoffDisabled
}
Expand All @@ -109,7 +126,9 @@ func (s *Service) retryWrites() {
case <-s.closing:
return
case <-ticker.C:
s.statMap.Add(processReq, 1)
if err := s.HintedHandoff.Process(); err != nil && err != io.EOF {
s.statMap.Add(processReqFail, 1)
s.Logger.Printf("retried write failed: %v", err)
}
}
Expand Down