// WritePoints writes points over UDP transport. func (u *UDP) WritePoints(p *coordinator.WritePointsRequest) (err error) { var addr *net.UDPAddr var con *net.UDPConn addr, err = net.ResolveUDPAddr("udp", u.addr) if err != nil { return } con, err = net.DialUDP("udp", nil, addr) if err != nil { return } defer con.Close() for _, p := range p.Points { _, err = con.Write([]byte(p.String())) if err != nil { return } } return }
func TestBufferedPointsWriter(t *testing.T) { db := "db0" rp := "rp0" capacity := 10000 writePointsIntoCnt := 0 pointsWritten := []models.Point{} reset := func() { writePointsIntoCnt = 0 pointsWritten = pointsWritten[:0] } fakeWriter := &fakePointsWriter{ WritePointsIntoFn: func(req *coordinator.IntoWriteRequest) error { writePointsIntoCnt++ pointsWritten = append(pointsWritten, req.Points...) return nil }, } w := coordinator.NewBufferedPointsWriter(fakeWriter, db, rp, capacity) // Test that capacity and length are correct for new buffered writer. if w.Cap() != capacity { t.Fatalf("exp %d, got %d", capacity, w.Cap()) } else if w.Len() != 0 { t.Fatalf("exp %d, got %d", 0, w.Len()) } // Test flushing an empty buffer. if err := w.Flush(); err != nil { t.Fatal(err) } else if writePointsIntoCnt > 0 { t.Fatalf("exp 0, got %d", writePointsIntoCnt) } // Test writing zero points. if err := w.WritePointsInto(&coordinator.IntoWriteRequest{ Database: db, RetentionPolicy: rp, Points: []models.Point{}, }); err != nil { t.Fatal(err) } else if writePointsIntoCnt > 0 { t.Fatalf("exp 0, got %d", writePointsIntoCnt) } else if w.Len() > 0 { t.Fatalf("exp 0, got %d", w.Len()) } // Test writing single large bunch of points points. req := coordinator.WritePointsRequest{ Database: db, RetentionPolicy: rp, } numPoints := int(float64(capacity) * 5.5) for i := 0; i < numPoints; i++ { req.AddPoint("cpu", float64(i), time.Unix(0, 0).Add(time.Duration(i)*time.Second), nil) } r := coordinator.IntoWriteRequest(req) if err := w.WritePointsInto(&r); err != nil { t.Fatal(err) } else if writePointsIntoCnt != 5 { t.Fatalf("exp 5, got %d", writePointsIntoCnt) } else if w.Len() != capacity/2 { t.Fatalf("exp %d, got %d", capacity/2, w.Len()) } else if len(pointsWritten) != numPoints-capacity/2 { t.Fatalf("exp %d, got %d", numPoints-capacity/2, len(pointsWritten)) } if err := w.Flush(); err != nil { t.Fatal(err) } else if writePointsIntoCnt != 6 { t.Fatalf("exp 6, got %d", writePointsIntoCnt) } else if w.Len() != 0 { t.Fatalf("exp 0, got %d", w.Len()) } else if len(pointsWritten) != numPoints { t.Fatalf("exp %d, got %d", numPoints, len(pointsWritten)) } else if !reflect.DeepEqual(r.Points, pointsWritten) { t.Fatal("points don't match") } reset() // Test writing points one at a time. for i, _ := range r.Points { if err := w.WritePointsInto(&coordinator.IntoWriteRequest{ Database: db, RetentionPolicy: rp, Points: r.Points[i : i+1], }); err != nil { t.Fatal(err) } } if err := w.Flush(); err != nil { t.Fatal(err) } else if writePointsIntoCnt != 6 { t.Fatalf("exp 6, got %d", writePointsIntoCnt) } else if w.Len() != 0 { t.Fatalf("exp 0, got %d", w.Len()) } else if len(pointsWritten) != numPoints { t.Fatalf("exp %d, got %d", numPoints, len(pointsWritten)) } else if !reflect.DeepEqual(r.Points, pointsWritten) { t.Fatal("points don't match") } }