Skip to content

Commit

Permalink
fix log range (#949) (#951)
Browse files Browse the repository at this point in the history
* fix log range
  • Loading branch information
zhongzichao authored Nov 25, 2022
1 parent e8b9462 commit 3832b63
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/apiserver/controller/log/cluster_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func GetPFJobLogs(ctx *logger.RequestContext, request GetMixedLogRequest) (schem

// GetLogs return mixed logs
func GetLogs(ctx *logger.RequestContext, request GetMixedLogRequest) (schema.JobLogInfo, error) {
ctx.Logging().Debugf("Get mixed logs by request: %v", request)
ctx.Logging().Debugf("Get mixed logs by request: %+v", request)
runtimeSvc, err := runtime.GetOrCreateRuntime(request.ClusterInfo)
if err != nil {
err = fmt.Errorf("get cluster client failed. error:%s", err.Error())
Expand Down
20 changes: 16 additions & 4 deletions pkg/apiserver/router/v1/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ import (
)

const (
defaultMemory = "100MB"
defaultMemory = "100M"
defaultLineLimit = "1000"
maxLineLimit = 1000000
maxSizeLimit = "1G"
)

type LogRouter struct {
Expand Down Expand Up @@ -221,7 +223,11 @@ func constructJobLogRequest(ctx *logger.RequestContext, request *http.Request) (
// lineLimit, check by resource
lineLimit := request.URL.Query().Get(util.QueryKeyLineLimit)
if lineLimit != "" {
_, err = strconv.Atoi(lineLimit)
lineLimitInt, err := strconv.Atoi(lineLimit)
if lineLimitInt <= 0 || lineLimitInt > maxLineLimit {
log.Warnf("lineLimit is out of range")
lineLimit = defaultLineLimit
}
if err != nil {
err = fmt.Errorf("resource[%s] request param lineLimit value failed, error:%s", logRequest.Name, err.Error())
ctx.Logging().Errorln(err)
Expand All @@ -234,6 +240,7 @@ func constructJobLogRequest(ctx *logger.RequestContext, request *http.Request) (

// SizeLimit
sizeLimit := request.URL.Query().Get(util.QueryKeySizeLimit)
defaultSizeLimit, _ := resources.ParseQuantity(defaultMemory)
var memory resources.Quantity
if sizeLimit != "" {
memory, err = resources.ParseQuantity(sizeLimit)
Expand All @@ -242,10 +249,15 @@ func constructJobLogRequest(ctx *logger.RequestContext, request *http.Request) (
ctx.Logging().Errorln(err)
return logRequest, err
}
maxSizeLimitRes, _ := resources.ParseQuantity(maxSizeLimit)
if memory.AsInt64() <= 0 || memory.AsInt64() > maxSizeLimitRes.AsInt64() {
memory = defaultSizeLimit
}
} else {
memory, _ = resources.ParseQuantity(defaultMemory)
memory = defaultSizeLimit
}
logRequest.SizeLimit = memory.AsInt64()
sizeLimitInt := memory.AsInt64()
logRequest.SizeLimit = sizeLimitInt
logRequest.ResourceType = request.URL.Query().Get(util.QueryKeyType)
logRequest.Framework = request.URL.Query().Get(util.QueryKeyFramework)

Expand Down
20 changes: 20 additions & 0 deletions pkg/apiserver/router/v1/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ func TestLogRouter_GetJobLog(t *testing.T) {
wantErr: false,
responseCode: 400,
},
{
name: "wrong sizelimit negative",
args: args{
ctx: &logger.RequestContext{UserName: mockUserName},
req: &Req{
Name: MockJobID,
Namespace: "default",
ClusterName: MockClusterName,
ResourceType: string(pfschema.TypePodJob),
Framework: "",
LineLimit: "-200",
SizeLimit: "-100k",
readFromTail: true,
},
router: router,
getMixedMock: true,
},
wantErr: false,
responseCode: 200,
},
{
name: "normal",
args: args{
Expand Down
11 changes: 5 additions & 6 deletions pkg/common/utils/logic_paging.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ func (l *LogPage) Paging(content string, logContentLineNum int) string {
logContentLineNum = len(strings.Split(strings.TrimRight(content, "\n"), "\n"))
}
contentLength := len(content)

log.Debugf("get logs index, bytesLoaded %d, lineNumber %d, LogFilePosition %s",
int64(contentLength), logContentLineNum, l.LogFilePosition)
log.Debugf("Paging: get logs index, bytesLoaded %d, lineNumber %d, LogFilePosition %s, "+
"lineLimit %d, sizeLimit %d", int64(contentLength), logContentLineNum, l.LogFilePosition, l.LineLimit, l.SizeLimit)
startIndex, endIndex := l.Dividing(logContentLineNum, contentLength)
// split log
finalContent := SplitLog(content, startIndex, endIndex, false)
Expand All @@ -50,10 +49,10 @@ func (l *LogPage) SlicePaging(content []string) []string {
contentByteLength += len(c)
}
logContentLineNum := len(content)
log.Debugf("get logs index, bytesLoaded %d, lineNumber %d, LogFilePosition %s",
contentByteLength, logContentLineNum, l.LogFilePosition)
log.Debugf("slicePaging: get logs index, bytesLoaded %d, lineNumber %d, LogFilePosition %s, "+
"lineLimit %d, sizeLimit %d", contentByteLength, logContentLineNum, l.LogFilePosition, l.LineLimit, l.SizeLimit)
startIndex, endIndex := l.Dividing(logContentLineNum, contentByteLength)

log.Debugf("startIndex %d, endIndex %d", startIndex, endIndex)
var finalContent []string
if startIndex == -1 && endIndex == -1 {
finalContent = content[:]
Expand Down
4 changes: 2 additions & 2 deletions pkg/job/runtime_v2/kubernetes_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,13 +630,13 @@ func formatAllEventLogs(events []corev1.Event, logPage utils.LogPage) []string {
if events[i].InvolvedObject.Name != events[j].InvolvedObject.Name {
return events[i].InvolvedObject.Name < events[j].InvolvedObject.Name
}
return events[i].EventTime.Before(&events[j].EventTime)
return events[i].CreationTimestamp.Before(&events[j].CreationTimestamp)
})
var formatedEvents []string
for _, event := range events {
//Type-Reason-Timestamp-Message
str := fmt.Sprintf("type: %s\treason: %s\teventsTime: %s \tmessage: %s",
event.Type, event.Reason, event.EventTime, event.Message)
event.Type, event.Reason, event.CreationTimestamp.Format("2006-01-02 15:04:05"), event.Message)
formatedEvents = append(formatedEvents, str)
}
formatedEvents = logPage.SlicePaging(formatedEvents)
Expand Down

0 comments on commit 3832b63

Please sign in to comment.