Found this when PrintAll
is implemented in data_reader.go
. After add more logging, I found the id wrote is not the id read. we can see the index bytes are all zero except the count
INFO[0000] Meta:<id:15836903516738211145 type:1 precision:1000000 name:"s" tags:<key:"machine" value:"machine-01" > tags:<key:"os" value:"ubuntu" > > Entries:<Offset:9 BlockSize:34 > pkg=k.s.disk
INFO[0000] [10 57 8 201 154 195 202 244 250 254 227 219 1 16 1 24 192 132 61 34 1 115 42 12 10 2 111 115 18 6 117 98 117 110 116 117 42 21 10 7 109 97 99 104 105 110 101 18 10 109 97 99 104 105 110 101 45 48 49 18 4 8 9 16 34] pkg=k.s.disk
INFO[0000] write: id 15836903516738211145 pkg=k.s.disk
INFO[0000] write: index offset 0 pkg=k.s.disk
INFO[0000] write: index length 65 pkg=k.s.disk
INFO[0000] write: full bits of index pkg=k.s.disk
INFO[0000] [0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] pkg=k.s.disk
INFO[0000] read: size 153 idx offset 43 idx of idx offset 65 series count 1 pkg=k.s.disk
INFO[0000] read: full bits of index pkg=k.s.disk
INFO[0000] [0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0] pkg=k.s.disk
INFO[0000] read: id 4294967296 pkg=k.s.disk
INFO[0000] read: index offset 0 pkg=k.s.disk
INFO[0000] read: index length 0 pkg=k.s.disk
Print all data in /tmp/xephon680946347
size: 153 series count: 1
index size: 85
id: 4294967296 blocks: 0 meta: {%!s(uint64=0) %!s(int64=0) %!s(int64=0) map[]}
All data printed
The reason is in func (idx *LocalDataFileIndexWriter) WriteAll
we wrote to the marshaled bytes of IndexEntries (b
) instead of pre-allocated index
slice.
index := make([]byte, 4+IndexOfIndexUnitLength*len(ids))
binary.BigEndian.PutUint32(index[:4], uint32(len(ids)))
for i, id := range ids {
b, err := entries.Marshal()
start := 4 + i*IndexOfIndexUnitLength
binary.BigEndian.PutUint64(b[start:start+8], uint64(id))
binary.BigEndian.PutUint32(b[start+8:start+12], uint32(N))
}
After fixed this problem in 430db5f the print still got empty output, but another strange thing is when ReadAllIndexEntries
is called twice, the full bytes are printed twice, the problem is when looping use range
, golang makes a copy of the value, so the update of the value is not reflected to the value stored in the map, the solution is to use pointer, does it work if I have the following?
for k, v := range m1 {
v2 = m1[k]
v2.prop1 = "abc"
// what is m1[k] now?
}
INFO[0000] write: IndexEntries Meta:<id:15836903516738211145 type:1 precision:1000000 name:"s" tags:<key:"machine" value:"machine-01" > tags:<key:"os" value:"ubuntu" > > Entries:<Offset:9 BlockSize:34 > pkg=k.s.disk
INFO[0000] write: full bytes of IndexEntries pkg=k.s.disk
INFO[0000] [10 57 8 201 154 195 202 244 250 254 227 219 1 16 1 24 192 132 61 34 1 115 42 12 10 2 111 115 18 6 117 98 117 110 116 117 42 21 10 7 109 97 99 104 105 110 101 18 10 109 97 99 104 105 110 101 45 48 49 18 4 8 9 16 34] pkg=k.s.disk
INFO[0000] write: id 15836903516738211145 pkg=k.s.disk
INFO[0000] write: index offset 0 pkg=k.s.disk
INFO[0000] write: index length 65 pkg=k.s.disk
INFO[0000] write: full bytes of index pkg=k.s.disk
INFO[0000] [0 0 0 1 219 199 251 215 73 80 205 73 0 0 0 0 0 0 0 65] pkg=k.s.disk
INFO[0000] read: size 153 idx offset 43 idx of idx offset 65 series count 1 pkg=k.s.disk
INFO[0000] read: full bytes of index pkg=k.s.disk
INFO[0000] [219 199 251 215 73 80 205 73 0 0 0 0 0 0 0 65] pkg=k.s.disk
INFO[0000] read: id 15836903516738211145 pkg=k.s.disk
INFO[0000] read: index offset 0 pkg=k.s.disk
INFO[0000] read: index length 65 pkg=k.s.disk
INFO[0000] read: full bytes of IndexEntries pkg=k.s.disk
INFO[0000] [10 57 8 201 154 195 202 244 250 254 227 219 1 16 1 24 192 132 61 34 1 115 42 12 10 2 111 115 18 6 117 98 117 110 116 117 42 21 10 7 109 97 99 104 105 110 101 18 10 109 97 99 104 105 110 101 45 48 49 18 4 8 9 16 34] pkg=k.s.disk
Print all data in /tmp/xephon608830327
size: 153 series count: 1
index size: 85
INFO[0000] read: full bytes of IndexEntries pkg=k.s.disk
INFO[0000] [10 57 8 201 154 195 202 244 250 254 227 219 1 16 1 24 192 132 61 34 1 115 42 12 10 2 111 115 18 6 117 98 117 110 116 117 42 21 10 7 109 97 99 104 105 110 101 18 10 109 97 99 104 105 110 101 45 48 49 18 4 8 9 16 34] pkg=k.s.disk
id: 15836903516738211145 blocks: 0 meta: {%!s(uint64=0) %!s(int64=0) %!s(int64=0) map[]}
All data printed