mirror of
				https://github.com/ClusterCockpit/cc-metric-store.git
				synced 2025-11-04 10:45:07 +01:00 
			
		
		
		
	test de/serialization; fix bugs
This commit is contained in:
		@@ -2,7 +2,6 @@ package checkpoints
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bufio"
 | 
						"bufio"
 | 
				
			||||||
	"encoding/binary"
 | 
					 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"reflect"
 | 
						"reflect"
 | 
				
			||||||
@@ -38,20 +37,16 @@ type Metrics struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Checkpoint) Serialize(w io.Writer) error {
 | 
					func (c *Checkpoint) Serialize(w io.Writer) error {
 | 
				
			||||||
	// Write magic value (8 byte):
 | 
					 | 
				
			||||||
	if _, err := w.Write([]byte(magicValue)); err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// The header:
 | 
						// The header:
 | 
				
			||||||
	buf := make([]byte, 0, writeBufferSize*2)
 | 
						buf := make([]byte, 0, writeBufferSize*2)
 | 
				
			||||||
 | 
						buf = append(buf, magicValue...)
 | 
				
			||||||
	buf = encodeBytes(buf, c.Reserved)
 | 
						buf = encodeBytes(buf, c.Reserved)
 | 
				
			||||||
	buf = encodeUint64(buf, c.From)
 | 
						buf = encodeUint64(buf, c.From)
 | 
				
			||||||
	buf = encodeUint64(buf, c.To)
 | 
						buf = encodeUint64(buf, c.To)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// The rest:
 | 
						// The rest:
 | 
				
			||||||
	buf, err := c.Root.serialize(w, buf)
 | 
						var err error
 | 
				
			||||||
	if err != nil {
 | 
						if buf, err = c.Root.serialize(w, buf); err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -99,7 +94,7 @@ func (c *CheckpointLevel) serialize(w io.Writer, buf []byte) ([]byte, error) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *Metrics) serialize(w io.Writer, buf []byte) ([]byte, error) {
 | 
					func (m *Metrics) serialize(w io.Writer, buf []byte) ([]byte, error) {
 | 
				
			||||||
	buf = encodeBytes(m.Reserved, buf)           // Reserved
 | 
						buf = encodeBytes(buf, m.Reserved)           // Reserved
 | 
				
			||||||
	buf = encodeUint64(buf, m.Frequency)         // Frequency
 | 
						buf = encodeUint64(buf, m.Frequency)         // Frequency
 | 
				
			||||||
	buf = encodeUint64(buf, m.From)              // First measurmenet timestamp
 | 
						buf = encodeUint64(buf, m.From)              // First measurmenet timestamp
 | 
				
			||||||
	buf = encodeUint32(buf, uint32(len(m.Data))) // Number of elements
 | 
						buf = encodeUint32(buf, uint32(len(m.Data))) // Number of elements
 | 
				
			||||||
@@ -114,7 +109,7 @@ func (m *Metrics) serialize(w io.Writer, buf []byte) ([]byte, error) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Checkpoint) Deserialize(r *bufio.Reader) error {
 | 
					func (c *Checkpoint) Deserialize(r *bufio.Reader) error {
 | 
				
			||||||
	buf := make([]byte, 8, 16384)
 | 
						buf := make([]byte, len(magicValue), 16384)
 | 
				
			||||||
	if _, err := io.ReadFull(r, buf); err != nil {
 | 
						if _, err := io.ReadFull(r, buf); err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -151,19 +146,22 @@ func (c *CheckpointLevel) deserialize(buf []byte, r *bufio.Reader) error {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	c.Metrics = make(map[string]*Metrics, n)
 | 
						if n != 0 {
 | 
				
			||||||
	for i := 0; i < int(n); i++ {
 | 
							c.Metrics = make(map[string]*Metrics, n)
 | 
				
			||||||
		key, err := decodeString(buf, r)
 | 
							for i := 0; i < int(n); i++ {
 | 
				
			||||||
		if err != nil {
 | 
								key, err := decodeString(buf, r)
 | 
				
			||||||
			return err
 | 
								if err != nil {
 | 
				
			||||||
		}
 | 
									return fmt.Errorf("decoding metric key: %w", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		metrics := &Metrics{}
 | 
								// log.Printf("decoding metric %#v...\n", key)
 | 
				
			||||||
		if err := metrics.deserialize(buf, r); err != nil {
 | 
								metrics := &Metrics{}
 | 
				
			||||||
			return err
 | 
								if err := metrics.deserialize(buf, r); err != nil {
 | 
				
			||||||
		}
 | 
									return fmt.Errorf("decoding metric for key %#v: %w", key, err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		c.Metrics[key] = metrics
 | 
								c.Metrics[key] = metrics
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Sublevels:
 | 
						// Sublevels:
 | 
				
			||||||
@@ -171,19 +169,22 @@ func (c *CheckpointLevel) deserialize(buf []byte, r *bufio.Reader) error {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	c.Sublevels = make(map[string]*CheckpointLevel, n)
 | 
						if n != 0 {
 | 
				
			||||||
	for i := 0; i < int(n); i++ {
 | 
							c.Sublevels = make(map[string]*CheckpointLevel, n)
 | 
				
			||||||
		key, err := decodeString(buf, r)
 | 
							for i := 0; i < int(n); i++ {
 | 
				
			||||||
		if err != nil {
 | 
								key, err := decodeString(buf, r)
 | 
				
			||||||
			return err
 | 
								if err != nil {
 | 
				
			||||||
		}
 | 
									return fmt.Errorf("decoding sublevel key: %w", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		sublevel := &CheckpointLevel{}
 | 
								// log.Printf("decoding sublevel %#v...\n", key)
 | 
				
			||||||
		if err := sublevel.deserialize(buf, r); err != nil {
 | 
								sublevel := &CheckpointLevel{}
 | 
				
			||||||
			return err
 | 
								if err := sublevel.deserialize(buf, r); err != nil {
 | 
				
			||||||
		}
 | 
									return fmt.Errorf("decoding sublevel for key %#v: %w", key, err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		c.Sublevels[key] = sublevel
 | 
								c.Sublevels[key] = sublevel
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
@@ -212,7 +213,7 @@ func (m *Metrics) deserialize(buf []byte, r *bufio.Reader) error {
 | 
				
			|||||||
	elmsize := unsafe.Sizeof(x)
 | 
						elmsize := unsafe.Sizeof(x)
 | 
				
			||||||
	bytes = memstore.RequestBytes(int(n) * int(elmsize))
 | 
						bytes = memstore.RequestBytes(int(n) * int(elmsize))
 | 
				
			||||||
	if _, err := io.ReadFull(r, bytes); err != nil {
 | 
						if _, err := io.ReadFull(r, bytes); err != nil {
 | 
				
			||||||
		return err
 | 
							return fmt.Errorf("reading payload (n=%d, elmsize=%d): %w", n, elmsize, err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sh := (*reflect.SliceHeader)(unsafe.Pointer(&bytes))
 | 
						sh := (*reflect.SliceHeader)(unsafe.Pointer(&bytes))
 | 
				
			||||||
@@ -222,26 +223,32 @@ func (m *Metrics) deserialize(buf []byte, r *bufio.Reader) error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func encodeBytes(buf []byte, bytes []byte) []byte {
 | 
					func encodeBytes(buf []byte, bytes []byte) []byte {
 | 
				
			||||||
	buf = encodeUint32(buf, uint32(len(bytes)))
 | 
						buf = encodeUint32(buf, uint32(len(bytes)))
 | 
				
			||||||
	buf = append(buf, bytes...)
 | 
						return append(buf, bytes...)
 | 
				
			||||||
	return buf
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func encodeString(buf []byte, str string) []byte {
 | 
					func encodeString(buf []byte, str string) []byte {
 | 
				
			||||||
	buf = encodeUint32(buf, uint32(len(str)))
 | 
						buf = encodeUint32(buf, uint32(len(str)))
 | 
				
			||||||
	buf = append(buf, str...)
 | 
						return append(buf, str...)
 | 
				
			||||||
	return buf
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func encodeUint64(buf []byte, i uint64) []byte {
 | 
					func encodeUint64(buf []byte, i uint64) []byte {
 | 
				
			||||||
	buf = append(buf, 0, 0, 0, 0, 0, 0, 0, 0) // 8 bytes
 | 
						return append(buf,
 | 
				
			||||||
	binary.LittleEndian.PutUint64(buf, i)
 | 
							byte((i>>0)&0xff),
 | 
				
			||||||
	return buf[8:]
 | 
							byte((i>>8)&0xff),
 | 
				
			||||||
 | 
							byte((i>>16)&0xff),
 | 
				
			||||||
 | 
							byte((i>>24)&0xff),
 | 
				
			||||||
 | 
							byte((i>>32)&0xff),
 | 
				
			||||||
 | 
							byte((i>>40)&0xff),
 | 
				
			||||||
 | 
							byte((i>>48)&0xff),
 | 
				
			||||||
 | 
							byte((i>>56)&0xff))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func encodeUint32(buf []byte, i uint32) []byte {
 | 
					func encodeUint32(buf []byte, i uint32) []byte {
 | 
				
			||||||
	buf = append(buf, 0, 0, 0, 0) // 4 bytes
 | 
						return append(buf,
 | 
				
			||||||
	binary.LittleEndian.PutUint32(buf, i)
 | 
							byte((i>>0)&0xff),
 | 
				
			||||||
	return buf[4:]
 | 
							byte((i>>8)&0xff),
 | 
				
			||||||
 | 
							byte((i>>16)&0xff),
 | 
				
			||||||
 | 
							byte((i>>24)&0xff))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func decodeBytes(buf []byte, r *bufio.Reader) ([]byte, error) {
 | 
					func decodeBytes(buf []byte, r *bufio.Reader) ([]byte, error) {
 | 
				
			||||||
@@ -250,9 +257,13 @@ func decodeBytes(buf []byte, r *bufio.Reader) ([]byte, error) {
 | 
				
			|||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if len == 0 {
 | 
				
			||||||
 | 
							return nil, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bytes := make([]byte, len)
 | 
						bytes := make([]byte, len)
 | 
				
			||||||
	if _, err := io.ReadFull(r, bytes); err != nil {
 | 
						if _, err := io.ReadFull(r, bytes); err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, fmt.Errorf("decoding %d bytes: %w", len, err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return bytes, nil
 | 
						return bytes, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -283,7 +294,14 @@ func decodeUint64(buf []byte, r *bufio.Reader) (uint64, error) {
 | 
				
			|||||||
		return 0, err
 | 
							return 0, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return binary.LittleEndian.Uint64(buf), nil
 | 
						return (uint64(buf[0]) << 0) |
 | 
				
			||||||
 | 
							(uint64(buf[1]) << 8) |
 | 
				
			||||||
 | 
							(uint64(buf[2]) << 16) |
 | 
				
			||||||
 | 
							(uint64(buf[3]) << 24) |
 | 
				
			||||||
 | 
							(uint64(buf[4]) << 32) |
 | 
				
			||||||
 | 
							(uint64(buf[5]) << 40) |
 | 
				
			||||||
 | 
							(uint64(buf[6]) << 48) |
 | 
				
			||||||
 | 
							(uint64(buf[7]) << 56), nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func decodeUint32(buf []byte, r *bufio.Reader) (uint32, error) {
 | 
					func decodeUint32(buf []byte, r *bufio.Reader) (uint32, error) {
 | 
				
			||||||
@@ -293,5 +311,8 @@ func decodeUint32(buf []byte, r *bufio.Reader) (uint32, error) {
 | 
				
			|||||||
		return 0, err
 | 
							return 0, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return binary.LittleEndian.Uint32(buf), nil
 | 
						return (uint32(buf[0]) << 0) |
 | 
				
			||||||
 | 
							(uint32(buf[1]) << 8) |
 | 
				
			||||||
 | 
							(uint32(buf[2]) << 16) |
 | 
				
			||||||
 | 
							(uint32(buf[3]) << 24), nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										109
									
								
								internal/checkpoints/checkpoints_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								internal/checkpoints/checkpoints_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,109 @@
 | 
				
			|||||||
 | 
					package checkpoints
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"bufio"
 | 
				
			||||||
 | 
						"bytes"
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
 | 
						"reflect"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/ClusterCockpit/cc-metric-store/internal/types"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestIntEncoding(t *testing.T) {
 | 
				
			||||||
 | 
						buf := make([]byte, 0, 100)
 | 
				
			||||||
 | 
						x1 := uint64(0x0102030405060708)
 | 
				
			||||||
 | 
						buf = encodeUint64(buf, x1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						x2, err := decodeUint64(make([]byte, 0, 10), bufio.NewReader(bytes.NewReader(buf)))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if x1 != x2 {
 | 
				
			||||||
 | 
							t.Fatalf("uint64: x1 != x2: %x != %x", x1, x2)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						buf = buf[0:0:100]
 | 
				
			||||||
 | 
						x3 := uint32(0xabcde)
 | 
				
			||||||
 | 
						buf = encodeUint32(buf, x3)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						x4, err := decodeUint32(make([]byte, 0, 10), bufio.NewReader(bytes.NewReader(buf)))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if x3 != x4 {
 | 
				
			||||||
 | 
							t.Fatalf("uint32: x3 != x4: %x != %x", x3, x4)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestIdentity(t *testing.T) {
 | 
				
			||||||
 | 
						input := &Checkpoint{
 | 
				
			||||||
 | 
							Reserved: []byte("Hallo Welt"),
 | 
				
			||||||
 | 
							From:     1234,
 | 
				
			||||||
 | 
							To:       5678,
 | 
				
			||||||
 | 
							Root: &CheckpointLevel{
 | 
				
			||||||
 | 
								Reserved: nil,
 | 
				
			||||||
 | 
								Sublevels: map[string]*CheckpointLevel{
 | 
				
			||||||
 | 
									"host1": {
 | 
				
			||||||
 | 
										Reserved: []byte("some text..."),
 | 
				
			||||||
 | 
										Metrics: map[string]*Metrics{
 | 
				
			||||||
 | 
											"flops": {
 | 
				
			||||||
 | 
												Reserved:  []byte("blablabla"),
 | 
				
			||||||
 | 
												Frequency: 60,
 | 
				
			||||||
 | 
												From:      12345,
 | 
				
			||||||
 | 
												Data:      []types.Float{1.1, 2.2, 3.3, 4.4, 5.5, 6.6},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
											"xyz": {
 | 
				
			||||||
 | 
												Frequency: 10,
 | 
				
			||||||
 | 
												From:      123,
 | 
				
			||||||
 | 
												Data:      []types.Float{1.2, 3.4, 5.6, 7.8},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									"host2": {
 | 
				
			||||||
 | 
										Sublevels: map[string]*CheckpointLevel{
 | 
				
			||||||
 | 
											"cpu0": {
 | 
				
			||||||
 | 
												Metrics: map[string]*Metrics{
 | 
				
			||||||
 | 
													"abc": {
 | 
				
			||||||
 | 
														Frequency: 42,
 | 
				
			||||||
 | 
														From:      420,
 | 
				
			||||||
 | 
														Data:      []types.Float{1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15.},
 | 
				
			||||||
 | 
													},
 | 
				
			||||||
 | 
												},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						disk := &bytes.Buffer{}
 | 
				
			||||||
 | 
						if err := input.Serialize(disk); err != nil {
 | 
				
			||||||
 | 
							t.Fatal("serialization failed:", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// fmt.Printf("disk.Len() = %d\n", disk.Len())
 | 
				
			||||||
 | 
						// fmt.Printf("disk.Bytes() = %#v", disk.Bytes())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						output := &Checkpoint{}
 | 
				
			||||||
 | 
						if err := output.Deserialize(bufio.NewReader(disk)); err != nil {
 | 
				
			||||||
 | 
							t.Fatal("deserialization failed:", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if !reflect.DeepEqual(input, output) {
 | 
				
			||||||
 | 
							input_json, err := json.Marshal(input)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								log.Fatal(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							output_json, err := json.Marshal(output)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								log.Fatal(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							log.Printf("a: %#v", string(input_json))
 | 
				
			||||||
 | 
							log.Printf("b: %#v", string(output_json))
 | 
				
			||||||
 | 
							t.Fatal("x != deserialize(serialize(x))")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user