diff --git a/server/store/datastore/log.go b/server/store/datastore/log.go index 1835a40c2..ca102b27a 100644 --- a/server/store/datastore/log.go +++ b/server/store/datastore/log.go @@ -27,9 +27,11 @@ import ( // Too large a value results in `pq: got XX parameters but PostgreSQL only supports 65535 parameters`. const pgBatchSize = 1000 +// LogFind returns the log entries of a step in the order the agent produced +// them, which is the order their line numbers carry. func (s storage) LogFind(step *model.Step) ([]*model.LogEntry, error) { var logEntries []*model.LogEntry - return logEntries, s.engine.Asc("id").Where("step_id = ?", step.ID).Find(&logEntries) + return logEntries, s.engine.Asc("line").Where("step_id = ?", step.ID).Find(&logEntries) } func (s storage) LogAppend(_ *model.Step, logEntries []*model.LogEntry) error { diff --git a/server/store/datastore/log_test.go b/server/store/datastore/log_test.go index 1c331ff78..993130340 100644 --- a/server/store/datastore/log_test.go +++ b/server/store/datastore/log_test.go @@ -96,3 +96,36 @@ func TestLogAppend(t *testing.T) { assert.NoError(t, err) assert.Len(t, _logEntries, len(logEntries)+1) } + +func TestLogFindOrdersByLine(t *testing.T) { + store, closer := newTestStore(t, new(model.Step), new(model.LogEntry)) + defer closer() + + step := model.Step{ + ID: 1, + } + + // The first batch is written through a node holding the id cache 4000+, + // the second one through a node holding the lower cache 30+. + assert.NoError(t, store.LogAppend(&step, []*model.LogEntry{ + {ID: 4000, StepID: step.ID, Data: []byte("first"), Line: 0, Time: 0}, + {ID: 4001, StepID: step.ID, Data: []byte("second"), Line: 1, Time: 10}, + })) + assert.NoError(t, store.LogAppend(&step, []*model.LogEntry{ + {ID: 30, StepID: step.ID, Data: []byte("third"), Line: 2, Time: 20}, + {ID: 31, StepID: step.ID, Data: []byte("fourth"), Line: 3, Time: 30}, + })) + + logEntries, err := store.LogFind(&step) + assert.NoError(t, err) + + lines := make([]int, 0, len(logEntries)) + data := make([]string, 0, len(logEntries)) + for _, logEntry := range logEntries { + lines = append(lines, logEntry.Line) + data = append(data, string(logEntry.Data)) + } + + assert.Equal(t, []int{0, 1, 2, 3}, lines) + assert.Equal(t, []string{"first", "second", "third", "fourth"}, data) +}