mirror of https://github.com/grafana/loki
parent
c922e94708
commit
af93e2019c
@ -0,0 +1,45 @@ |
|||||||
|
package ingester |
||||||
|
|
||||||
|
import ( |
||||||
|
"github.com/grafana/logish/pkg/logproto" |
||||||
|
"github.com/pkg/errors" |
||||||
|
) |
||||||
|
|
||||||
|
const ( |
||||||
|
tmpNumEntries = 1024 |
||||||
|
) |
||||||
|
|
||||||
|
var ( |
||||||
|
ErrChunkFull = errors.New("Chunk full") |
||||||
|
ErrOutOfOrder = errors.New("Entry out of order") |
||||||
|
) |
||||||
|
|
||||||
|
type Chunk interface { |
||||||
|
SpaceFor(*logproto.Entry) bool |
||||||
|
Push(*logproto.Entry) error |
||||||
|
} |
||||||
|
|
||||||
|
func newChunk() Chunk { |
||||||
|
return &dumbChunk{} |
||||||
|
} |
||||||
|
|
||||||
|
type dumbChunk struct { |
||||||
|
entries []*logproto.Entry |
||||||
|
} |
||||||
|
|
||||||
|
func (c *dumbChunk) SpaceFor(_ *logproto.Entry) bool { |
||||||
|
return len(c.entries) == tmpNumEntries |
||||||
|
} |
||||||
|
|
||||||
|
func (c *dumbChunk) Push(entry *logproto.Entry) error { |
||||||
|
if len(c.entries) == tmpNumEntries { |
||||||
|
return ErrChunkFull |
||||||
|
} |
||||||
|
|
||||||
|
if len(c.entries) > 0 && c.entries[len(c.entries)-1].Timestamp.After(entry.Timestamp) { |
||||||
|
return ErrOutOfOrder |
||||||
|
} |
||||||
|
|
||||||
|
c.entries = append(c.entries, entry) |
||||||
|
return nil |
||||||
|
} |
||||||
@ -0,0 +1,40 @@ |
|||||||
|
package ingester |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
|
||||||
|
"github.com/grafana/logish/pkg/logproto" |
||||||
|
) |
||||||
|
|
||||||
|
const tmpMaxChunks = 3 |
||||||
|
|
||||||
|
type stream struct { |
||||||
|
// Newest chunk at chunks[0].
|
||||||
|
// Not thread-safe; assume accesses to this are locked by caller.
|
||||||
|
chunks []Chunk |
||||||
|
} |
||||||
|
|
||||||
|
func newStream() *stream { |
||||||
|
return &stream{} |
||||||
|
} |
||||||
|
|
||||||
|
func (s *stream) Push(ctx context.Context, entries []logproto.Entry) error { |
||||||
|
if len(s.chunks) == 0 { |
||||||
|
s.chunks = append(s.chunks, newChunk()) |
||||||
|
} |
||||||
|
|
||||||
|
for i := range entries { |
||||||
|
if !s.chunks[0].SpaceFor(&entries[i]) { |
||||||
|
s.chunks = append([]Chunk{newChunk()}, s.chunks...) |
||||||
|
} |
||||||
|
if err := s.chunks[0].Push(&entries[i]); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Temp; until we implement flushing, only keep N chunks in memory.
|
||||||
|
if len(s.chunks) > tmpMaxChunks { |
||||||
|
s.chunks = s.chunks[:tmpMaxChunks] |
||||||
|
} |
||||||
|
return nil |
||||||
|
} |
||||||
Loading…
Reference in new issue