Skip to content

Commit

Permalink
Convert ClientMetadata map types to []metadata.NameValue (#176)
Browse files Browse the repository at this point in the history
* Convert ClientMetadata map types to []metadata.NameValue
* Update meta tests
* Add metadata package and NameValue type
  • Loading branch information
stephen-soltesz authored Aug 22, 2019
1 parent 47d59b5 commit 03f7328
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 21 deletions.
7 changes: 7 additions & 0 deletions metadata/namevalue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package metadata

// NameValue is a BigQuery-compatible type for ClientMetadata "name"/"value" pairs.
type NameValue struct {
Name string
Value string
}
4 changes: 2 additions & 2 deletions ndt5/control/data.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package control

import (
"github.com/m-lab/ndt-server/ndt5/meta"
"github.com/m-lab/ndt-server/metadata"
"github.com/m-lab/ndt-server/ndt5/ndt"
)

Expand All @@ -11,5 +11,5 @@ type ArchivalData struct {
UUID string
Protocol ndt.ConnectionType
MessageProtocol string
ClientMetadata meta.ArchivalData `json:",omitempty" bigquery:"-"`
ClientMetadata []metadata.NameValue `json:",omitempty"`
}
10 changes: 4 additions & 6 deletions ndt5/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"time"

"github.com/m-lab/ndt-server/metadata"
"github.com/m-lab/ndt-server/ndt5/metrics"
"github.com/m-lab/ndt-server/ndt5/ndt"
"github.com/m-lab/ndt-server/ndt5/protocol"
Expand All @@ -14,20 +15,17 @@ import (
// maxClientMessages is the maximum allowed messages we will accept from a client.
var maxClientMessages = 20

// ArchivalData contains all meta data reported by the client.
type ArchivalData map[string]string

// ManageTest runs the meta tests. If the given ctx is canceled or the meta test
// takes longer than 15sec, then ManageTest will return after the next ReceiveMessage.
// The given protocolMessager should have its own connection timeout to prevent
// "slow drip" clients holding the connection open indefinitely.
func ManageTest(ctx context.Context, m protocol.Messager, s ndt.Server) (ArchivalData, error) {
func ManageTest(ctx context.Context, m protocol.Messager, s ndt.Server) ([]metadata.NameValue, error) {
localCtx, localCancel := context.WithTimeout(ctx, 15*time.Second)
defer localCancel()

var err error
var message []byte
results := map[string]string{}
results := []metadata.NameValue{}
connType := s.ConnectionType().String()

err = m.SendMessage(protocol.TestPrepare, []byte{})
Expand Down Expand Up @@ -62,7 +60,7 @@ func ManageTest(ctx context.Context, m protocol.Messager, s ndt.Server) (Archiva
if len(value) > 255 {
value = value[:255]
}
results[name] = value
results = append(results, metadata.NameValue{Name: name, Value: value})
}
if localCtx.Err() != nil {
log.Println("META context error:", localCtx.Err())
Expand Down
11 changes: 6 additions & 5 deletions ndt5/meta/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"testing"

"github.com/m-lab/ndt-server/metadata"
"github.com/m-lab/ndt-server/ndt5/ndt"
"github.com/m-lab/ndt-server/ndt5/protocol"
)
Expand Down Expand Up @@ -72,7 +73,7 @@ func TestManageTest(t *testing.T) {
name string
ctx context.Context
m protocol.Messager
want ArchivalData
want []metadata.NameValue
wantErr bool
}{
{
Expand All @@ -83,7 +84,7 @@ func TestManageTest(t *testing.T) {
{msg: []byte("a:b")},
},
},
want: map[string]string{"a": "b"},
want: []metadata.NameValue{{Name: "a", Value: "b"}},
},
{
name: "truncate-name-to-63-bytes",
Expand All @@ -93,7 +94,7 @@ func TestManageTest(t *testing.T) {
{msg: append(len64, []byte(":b")...)},
},
},
want: map[string]string{string(len64[:63]): "b"},
want: []metadata.NameValue{{Name: string(len64[:63]), Value: "b"}},
},
{
name: "truncate-value-to-255-bytes",
Expand All @@ -103,7 +104,7 @@ func TestManageTest(t *testing.T) {
{msg: append([]byte("a:"), len256...)},
},
},
want: map[string]string{"a": string(len256[:255])},
want: []metadata.NameValue{{Name: "a", Value: string(len256[:255])}},
},
{
name: "receive-error",
Expand All @@ -123,7 +124,7 @@ func TestManageTest(t *testing.T) {
{msg: []byte("this-key-has-no-colon-separator")},
},
},
want: map[string]string{},
want: []metadata.NameValue{},
},
}
for _, tt := range tests {
Expand Down
9 changes: 6 additions & 3 deletions ndt7/model/archivaldata.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package model

import "time"
import (
"time"

"github.com/m-lab/ndt-server/metadata"
)

// ArchivalData saves all instantaneous measurements over the lifetime of a test.
type ArchivalData struct {
Expand All @@ -9,6 +13,5 @@ type ArchivalData struct {
EndTime time.Time
ServerMeasurements []Measurement
ClientMeasurements []Measurement
// TODO(m-lab/ndt-server/issues/151): remove bigquery tag.
ClientMetadata map[string]string `json:",omitempty" bigquery:"-"`
ClientMetadata []metadata.NameValue `json:",omitempty"`
}
2 changes: 1 addition & 1 deletion ndt7/results/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func newFile(datadir, what, uuid string) (*File, error) {
// indicate whether this is a spec.SubtestDownload or a spec.SubtestUpload
// ndt7 measurement.
func OpenFor(request *http.Request, conn *websocket.Conn, datadir string, what spec.SubtestKind) (*File, error) {
meta := make(metadata)
meta := make(metadata, 0)
netConn := conn.UnderlyingConn()
id, err := fdcache.GetUUID(netConn)
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions ndt7/results/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ package results
import (
"net/url"
"regexp"

meta "github.com/m-lab/ndt-server/metadata"
)

// metadata contains ndt7 metadata.
type metadata map[string]string
type metadata []meta.NameValue

// serverKeyRe is a regexp that matches any server related key.
var serverKeyRe = regexp.MustCompile("^server_")

// initMetadata initializes |*meta| from |values| provided from the original
// request query string.
func initMetadata(m *metadata, values url.Values) {
for k, v := range values {
if matches := serverKeyRe.MatchString(k); matches {
for name, values := range values {
if matches := serverKeyRe.MatchString(name); matches {
continue // We MUST skip variables reserved to the server
}
(*m)[k] = v[0]
*m = append(*m, meta.NameValue{Name: name, Value: values[0]})
}
}

0 comments on commit 03f7328

Please sign in to comment.