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

Implement GOB (and avro) encoder and parser #240

Merged
merged 78 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
f083c86
gob parser is added. auto module is updated to include the gob parser
RoshiniNara Jun 13, 2022
78f679e
Gob encoder is added and auto module is updated to include the gob en…
RoshiniNara Jun 13, 2022
f68e890
Auto module is updated.
RoshiniNara Jun 20, 2022
49adf69
Auto module is updated.
RoshiniNara Jun 20, 2022
f4d85cb
Update auto.go
RoshiniNara Jun 21, 2022
4391ad8
gog_test file (not compiling due to import errors)
RoshiniNara Jun 21, 2022
91987ee
Update gob_test.go
RoshiniNara Jun 22, 2022
6b09045
Encoder updated in test and the source
vnragavan Jun 25, 2022
1da420a
updated test and gob file - test is still failing due to container mi…
vnragavan Jun 26, 2022
ad030ed
encoder_test is successful
vnragavan Jun 26, 2022
6590a5c
Updated author info
vnragavan Jun 26, 2022
8bdb13f
GOB parser tests sucessful
vnragavan Jun 26, 2022
5702385
Updated author information
vnragavan Jun 26, 2022
d873b70
Parser interface is fixed. Tests are updated.
vnragavan Jun 27, 2022
96cb4ba
Parser test function name updated.
RoshiniNara Jun 27, 2022
ea16343
Configurations files for gob encoder and parser.
vnragavan Jun 27, 2022
857f23f
Merge branch 'primary' of https://github.com/RoshiniNara/skogul into …
vnragavan Jun 27, 2022
49c2745
Merge branch 'telenornms:primary' into primary
RoshiniNara Jun 30, 2022
a4d78cf
Avro encoder and parser added
RoshiniNara Jul 3, 2022
85383e5
Merge branch 'primary' of https://github.com/RoshiniNara/skogul into …
RoshiniNara Jul 3, 2022
ceec5f6
Avro parser edited
RoshiniNara Jul 3, 2022
8efd3ac
old comment removed
RoshiniNara Jul 3, 2022
af6abdd
Update gob.go
RoshiniNara Jul 4, 2022
942b919
Update avro.go
RoshiniNara Jul 4, 2022
c75fce0
Update avro.go
RoshiniNara Jul 4, 2022
552377c
Update go.mod
RoshiniNara Jul 4, 2022
e2e5063
Update go.mod
RoshiniNara Jul 4, 2022
9fe8aa6
Update avro_schema
RoshiniNara Jul 4, 2022
d474900
Update avro_schema
RoshiniNara Jul 4, 2022
46f39ef
Update avro.go
RoshiniNara Jul 5, 2022
1b27ffa
Update avro.go
RoshiniNara Jul 5, 2022
ff3cfea
Update avro.go
RoshiniNara Jul 5, 2022
30d0934
Update avro.go
RoshiniNara Jul 5, 2022
68dd30a
Update gob.go
RoshiniNara Jul 14, 2022
7b8cc29
Update gob_test.go
RoshiniNara Jul 14, 2022
695551a
Update gob.go
RoshiniNara Jul 14, 2022
585be79
Update gob.go
RoshiniNara Jul 14, 2022
c20fd08
Update gob_test.go
RoshiniNara Jul 14, 2022
a2f5f1a
Update gob_test.go
RoshiniNara Jul 14, 2022
4c3a541
Update auto.go
RoshiniNara Jul 14, 2022
86da966
Update avro.go
RoshiniNara Jul 14, 2022
f6cfa2d
Update gob.go
RoshiniNara Jul 14, 2022
2de3246
Update avro.go
RoshiniNara Jul 14, 2022
51daef8
go.mod and go.sum are updated
RoshiniNara Aug 7, 2022
ea08481
added "fmt" and
RoshiniNara Aug 7, 2022
7640715
Renamed the "GOBskogul" to "gob"
RoshiniNara Aug 9, 2022
7538f1f
Added comment for replacement and removed the extra lines.
RoshiniNara Aug 9, 2022
f014600
Changed the naming convention and added the helper strings as per the…
RoshiniNara Aug 9, 2022
e7ab84f
Moved the GOB files to docs/examples/gob
RoshiniNara Aug 9, 2022
289ad38
moved to the new directory
RoshiniNara Aug 10, 2022
1a6e70d
avro schema moved to /doc/examples/avro
RoshiniNara Aug 10, 2022
ad77db5
Upgraded to Go 1.19 and ran "make fmtfix"
RoshiniNara Aug 10, 2022
da86f13
Changed the name of the file from https to http and added the comment…
RoshiniNara Aug 10, 2022
ebec6c2
sync.once is added to avro encoder and parser. Also, error condition…
RoshiniNara Aug 11, 2022
adfd3c1
sync once change added
RoshiniNara Aug 11, 2022
f91a3d7
Update avro.go
RoshiniNara Aug 12, 2022
1440ce0
Update avro.go
RoshiniNara Aug 12, 2022
1dd7340
Update avro.go
RoshiniNara Aug 15, 2022
d1c3fe4
Update avro.go
RoshiniNara Aug 15, 2022
d78264e
avro parser error fixed
RoshiniNara Aug 15, 2022
b49561f
error fixed : removed
RoshiniNara Aug 15, 2022
333d957
update auto.go
RoshiniNara Aug 16, 2022
4dda447
Updated eith the errors in auto file
RoshiniNara Aug 16, 2022
f355580
Removed the error message in line#47
RoshiniNara Aug 16, 2022
523e7b8
avro tests (fails due to timestamp error)
RoshiniNara Aug 17, 2022
75d43b9
avro updates
RoshiniNara Aug 19, 2022
fa94198
avro test updated
RoshiniNara Aug 19, 2022
cb08c54
avro test failes due to incorrect arguments
RoshiniNara Aug 19, 2022
1f218e8
avro encoder test passed
RoshiniNara Aug 20, 2022
eda0ad3
added encoder test to thge parser terst module
RoshiniNara Aug 22, 2022
939de68
parser test updated
RoshiniNara Aug 22, 2022
0cd37e6
avro encoder removed
RoshiniNara Aug 22, 2022
ad221e1
changed pass by reference to the avro parser
RoshiniNara Aug 23, 2022
8a188d3
Added pass by reference to the AVRO encoder
RoshiniNara Aug 23, 2022
6eb1709
Update avro.go
RoshiniNara Aug 23, 2022
25ff855
Update avro.go
RoshiniNara Aug 23, 2022
b8695dc
Delete avro_testdata.json
RoshiniNara Aug 23, 2022
d0fbc26
Delete avro_testdata.json
RoshiniNara Aug 23, 2022
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
3 changes: 2 additions & 1 deletion common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ This is needed because the math package doesn't handle 32-bit variants.

The pattern is:


seeeeeee e0000000 00000000 00000000
01234567 01234567 01234567 01234567

Expand All @@ -52,11 +51,13 @@ Value of 0, exponent of 1's, mean infinity, sign will determine, well, the
sign (e.g. Inf vs -Inf).

Could also set this as:

uvneginf = 0b11111111100000000000000000000000
uvinf = 0b01111111100000000000000000000000

Hex equivalents are just weird, so I left it as binary after first figuring
out the hex encoding...

uvneginf = 0xFF800000
uvinf = 0x7F800000

Expand Down
33 changes: 33 additions & 0 deletions docs/examples/avro/avro_schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"type": "record",
"name": "simple",
"namespace": "org.hamba.avro",
"values": "string",
"fields": [
{
"name": "Metrics", "type": {
"type": "array",
"items": {
"type": "record",
"name": "metrics",
"fields": [
{
"name": "Metadata",
"type": {
"type": "map",
"values": ["string", "int", "long", "float", "double" ]
}
},
{
"name": "Data",
"type": {
"type": "map",
"values": ["string", "int", "long", "float", "double" ]
}
}
]
}
}
}
]
}
15 changes: 15 additions & 0 deletions docs/examples/gob/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
GOB examples
==============

WARNING: The GOB implementation is very much "MVP" and hast not _yet_
been tested extensively.

http_gob_to_stdout.json
--------------------
Receives the GOB data format and parses the GOB and prints the output in the stdout.
All communication is http.

tester_to_http_gob.json
--------------------
Encodes the incoming json data into the GOB data format.
All communication is http.
17 changes: 17 additions & 0 deletions docs/examples/gob/http_gob_to_stdout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"receivers": {
"r": {
"type": "http",
"address": "localhost:1339",
"handlers": {
"/": "something"
}
}
},
"handlers": {
"something": {
"parser": "gob",
"sender": "print"
}
}
}
25 changes: 25 additions & 0 deletions docs/examples/gob/tester_to_http_gob.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"receivers": {
"r": {
"type": "test",
"delay": "1s",
"metrics": 2,
"values": 2,
"threads": 1,
"handler": "something"
}
},
"handlers": {
"something": {
"parser": "json",
"sender": "tohttps"
}
},
"senders": {
"tohttps": {
"type": "http",
"encoder": "gob",
"url": "http://localhost:1339/"
}
}
}
16 changes: 14 additions & 2 deletions encoder/auto.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
* skogul, Encoder automation
*
* Copyright (c) 2019-2020 Telenor Norge AS
* Copyright (c) 2022 Telenor Norge AS
* Author(s):
* - Roshini NarasimhaRaghavan([email protected])
* - Kristian Lyngstøl <[email protected]>
RoshiniNara marked this conversation as resolved.
Show resolved Hide resolved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
Expand Down Expand Up @@ -54,4 +54,16 @@ func init() {
Help: "Encodes the standard Skogul JSON format with indentation.",
AutoMake: true,
})
Auto.Add(skogul.Module{
Name: "gob",
Alloc: func() interface{} { return &GOB{} },
Help: "Encodes the GOB format.",
AutoMake: true,
})
Auto.Add(skogul.Module{
Name: "avro",
Alloc: func() interface{} { return &AVRO{} },
Help: "Encodes the avro format.",
})

}
61 changes: 61 additions & 0 deletions encoder/avro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* skogul, test avro encoder
*
* Copyright (c) 2022 Telenor Norge AS
* Author:
* - Roshini Narasimha Raghavan <[email protected]>
* - Kristian Lyngstøl <[email protected]>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/

package encoder

import (
"fmt"
"os"
"sync"

"github.com/hamba/avro"
"github.com/telenornms/skogul"
)

type AVRO struct {
Schema string
s avro.Schema
err error
once sync.Once
}

func (x *AVRO) Encode(c *skogul.Container) ([]byte, error) {
x.once.Do(func() {
b, err := os.ReadFile(x.Schema)
x.err = err
if x.err == nil {
x.s = avro.MustParse(string(b))
if x.s == nil {
x.err = fmt.Errorf("parsed schema is nil")
}
}
})
if x.err != nil {
return nil, x.err
}
return avro.Marshal(x.s, c)
}

func (x *AVRO) EncodeMetric(m *skogul.Metric) ([]byte, error) {
return nil, fmt.Errorf("not supported")
}
28 changes: 28 additions & 0 deletions encoder/gob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package encoder

import (
"bytes"
"encoding/gob"

"github.com/telenornms/skogul"
)

type GOB struct{}

// encode the content in the skogul container as a gob format
func (x GOB) Encode(c *skogul.Container) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(*c)
b := buf.Bytes()
return b, err
}

// encode the metrics in the skogul container as a gob format
func (x GOB) EncodeMetric(m *skogul.Metric) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(*m)
b := buf.Bytes()
return b, err
}
128 changes: 128 additions & 0 deletions encoder/gob_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* skogul, test gob encoder
*
* Copyright (c) 2022 Telenor Norge AS
* Author:
* - Roshini Narasimha Raghavan <[email protected]>
* - Kristian Lyngstøl <[email protected]>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/

package encoder_test

import (
"bytes"
"io/ioutil"
"strings"
"testing"

"github.com/telenornms/skogul"
"github.com/telenornms/skogul/encoder"
"github.com/telenornms/skogul/parser"
)

// TestGOBEncode tests encoding of a simple GOB format from a skogul
// container
func TestGOBEncode(t *testing.T) {
testGOB(t, "./testdata/testdata.gob", true)
}

func testGOB(t *testing.T, file string, match bool) {
t.Helper()
c, orig := parseGOB(t, file)
b, err := encoder.GOB{}.Encode(c)

if err != nil {
t.Errorf("Encoding %s failed: %v", file, err)
return
}
if len(b) <= 0 {
t.Errorf("Encoding %s failed: zero length data", file)
return
}
if !match {
return
}

//The test gob data is generated using a separate program and there are differences in 3 locations compared to the encoded data.
//These differences are fixed by replacing the differences with exact matches before the comparison.
sorig := string(orig)
var origstring1 string
var old1 string
var old2 string
var old3 string
var new1 string
var new2 string
var new3 string
var result1, result2, result3 []byte
var n int
origstring1 = sorig
old1 = "\x1d"
old2 = "\x0e"
old3 = "main"
n = 1
new1 = "\x1f"
new2 = "\x10"
new3 = "skogul"
result1 = bytes.Replace([]byte(origstring1), []byte(old1), []byte(new1), n)
result2 = bytes.Replace([]byte(result1), []byte(old2), []byte(new2), n)
result3 = bytes.Replace([]byte(result2), []byte(old3), []byte(new3), n)
RoshiniNara marked this conversation as resolved.
Show resolved Hide resolved

sorig_trim := string(result3)
snew := string(b)

if len(sorig) < 2 {
t.Logf("Encoding %s failed: original pre-encoded length way too short. Shouldn't happen.", file)
t.FailNow()
}

result := strings.Compare(sorig_trim, snew)
if result != 0 {
t.Errorf("Encoding %s failed: original and newly encoded container doesn't match", file)
t.Logf("orig:\n'%s'", sorig_trim)
t.Logf("new:\n'%s'", snew)
t.Logf("result\n %d", result)
return
}

}

func parseGOB(t *testing.T, file string) (*skogul.Container, []byte) {
t.Helper()

b, err := ioutil.ReadFile(file)

if err != nil {
t.Logf("Failed to read test data file: %v", err)
t.FailNow()
return nil, nil
}
container, err := parser.GOB{}.Parse(b)

if err != nil {
t.Logf("Failed to parse GOB data: %v", err)
t.FailNow()
return nil, nil
}

if container == nil || container.Metrics == nil || len(container.Metrics) == 0 {
t.Logf("Expected parsed GOB to return a container with at least 1 metric")
t.FailNow()
return nil, nil
}
return container, b

}
Binary file added encoder/testdata/testdata.gob
Binary file not shown.
21 changes: 13 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ module github.com/telenornms/skogul
go 1.18

require (
github.com/eclipse/paho.mqtt.golang v1.3.5
github.com/eclipse/paho.mqtt.golang v1.4.1
github.com/go-sql-driver/mysql v1.6.0
github.com/gogo/protobuf v1.3.2
github.com/lib/pq v1.10.4
github.com/segmentio/kafka-go v0.4.31
github.com/hamba/avro v1.8.0
github.com/lib/pq v1.10.6
github.com/segmentio/kafka-go v0.4.32
github.com/sirupsen/logrus v1.8.1
google.golang.org/protobuf v1.28.0
)

require (
github.com/gorilla/websocket v1.4.2 // indirect
github.com/klauspost/compress v1.14.2 // indirect
github.com/pierrec/lz4/v4 v4.1.14 // indirect
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pierrec/lz4/v4 v4.1.15 // indirect
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect
)
Loading