Skip to content

Commit 23fdda1

Browse files
BraveYimeoer
authored andcommitted
nydusify(feat): support for specifing log file and concurrently processing external model manifests
Signed-off-by: Yang Kaiyong <[email protected]>
1 parent 9b91552 commit 23fdda1

File tree

6 files changed

+112
-25
lines changed

6 files changed

+112
-25
lines changed

contrib/nydusify/cmd/nydusify.go

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -193,22 +193,7 @@ func main() {
193193
}
194194

195195
// global options
196-
app.Flags = []cli.Flag{
197-
&cli.BoolFlag{
198-
Name: "debug",
199-
Aliases: []string{"D"},
200-
Required: false,
201-
Value: false,
202-
Usage: "Enable debug log level, overwrites the 'log-level' option",
203-
EnvVars: []string{"DEBUG_LOG_LEVEL"}},
204-
&cli.StringFlag{
205-
Name: "log-level",
206-
Aliases: []string{"l"},
207-
Value: "info",
208-
Usage: "Set log level (panic, fatal, error, warn, info, debug, trace)",
209-
EnvVars: []string{"LOG_LEVEL"},
210-
},
211-
}
196+
app.Flags = getGlobalFlags()
212197

213198
app.Commands = []*cli.Command{
214199
{
@@ -1439,4 +1424,38 @@ func setupLogLevel(c *cli.Context) {
14391424
}
14401425

14411426
logrus.SetLevel(logLevel)
1427+
1428+
if c.String("log-file") != "" {
1429+
f, err := os.OpenFile(c.String("log-file"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
1430+
if err != nil {
1431+
logrus.Errorf("failed to open log file: %+v", err)
1432+
return
1433+
}
1434+
logrus.SetOutput(f)
1435+
}
1436+
}
1437+
1438+
func getGlobalFlags() []cli.Flag {
1439+
return []cli.Flag{
1440+
&cli.BoolFlag{
1441+
Name: "debug",
1442+
Aliases: []string{"D"},
1443+
Required: false,
1444+
Value: false,
1445+
Usage: "Enable debug log level, overwrites the 'log-level' option",
1446+
EnvVars: []string{"DEBUG_LOG_LEVEL"}},
1447+
&cli.StringFlag{
1448+
Name: "log-level",
1449+
Aliases: []string{"l"},
1450+
Value: "info",
1451+
Usage: "Set log level (panic, fatal, error, warn, info, debug, trace)",
1452+
EnvVars: []string{"LOG_LEVEL"},
1453+
},
1454+
&cli.StringFlag{
1455+
Name: "log-file",
1456+
Required: false,
1457+
Usage: "Write logs to a file",
1458+
EnvVars: []string{"LOG_FILE"},
1459+
},
1460+
}
14421461
}

contrib/nydusify/cmd/nydusify_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import (
1212
"os"
1313
"testing"
1414

15+
"github.com/agiledragon/gomonkey/v2"
16+
"github.com/sirupsen/logrus"
17+
"github.com/stretchr/testify/assert"
1518
"github.com/stretchr/testify/require"
1619
"github.com/urfave/cli/v2"
1720
)
@@ -341,3 +344,50 @@ func TestGetPrefetchPatterns(t *testing.T) {
341344
require.NoError(t, err)
342345
require.Equal(t, "/", patterns)
343346
}
347+
348+
func TestGetGlobalFlags(t *testing.T) {
349+
flags := getGlobalFlags()
350+
require.Equal(t, 3, len(flags))
351+
}
352+
353+
func TestSetupLogLevelWithLogFile(t *testing.T) {
354+
logFilePath := "test_log_file.log"
355+
defer os.Remove(logFilePath)
356+
357+
c := &cli.Context{}
358+
359+
patches := gomonkey.ApplyMethodSeq(c, "String", []gomonkey.OutputCell{
360+
{Values: []interface{}{"info"}, Times: 1},
361+
{Values: []interface{}{"test_log_file.log"}, Times: 2},
362+
})
363+
defer patches.Reset()
364+
setupLogLevel(c)
365+
366+
file, err := os.Open(logFilePath)
367+
assert.NoError(t, err)
368+
assert.NotNil(t, file)
369+
file.Close()
370+
371+
logrusOutput := logrus.StandardLogger().Out
372+
assert.NotNil(t, logrusOutput)
373+
374+
logrus.Info("This is a test log message")
375+
content, err := os.ReadFile(logFilePath)
376+
assert.NoError(t, err)
377+
assert.Contains(t, string(content), "This is a test log message")
378+
}
379+
380+
func TestSetupLogLevelWithInvalidLogFile(t *testing.T) {
381+
382+
c := &cli.Context{}
383+
384+
patches := gomonkey.ApplyMethodSeq(c, "String", []gomonkey.OutputCell{
385+
{Values: []interface{}{"info"}, Times: 1},
386+
{Values: []interface{}{"test/test_log_file.log"}, Times: 2},
387+
})
388+
defer patches.Reset()
389+
setupLogLevel(c)
390+
391+
logrusOutput := logrus.StandardLogger().Out
392+
assert.NotNil(t, logrusOutput)
393+
}

contrib/nydusify/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ require (
3939
require (
4040
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
4141
github.com/AdamKorcz/go-118-fuzz-build v0.0.0-20231105174938-2b5cbb29f3e2 // indirect
42-
github.com/BraveY/snapshotter-converter v0.0.6 // indirect
42+
github.com/BraveY/snapshotter-converter v0.0.5 // indirect
4343
github.com/CloudNativeAI/model-spec v0.0.2 // indirect
4444
github.com/Microsoft/go-winio v0.6.2 // indirect
4545
github.com/Microsoft/hcsshim v0.11.5 // indirect

contrib/nydusify/pkg/converter/converter.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ func convertModelArtifact(ctx context.Context, opt Opt) error {
262262
ChunkSize: opt.ChunkSize,
263263
FromDir: contextDir,
264264
AttributesPath: attributesPath,
265-
Crc32: true,
266265
}
267266
_, externalBlobDigest, err := packWithAttributes(ctx, packOption, tmpDir)
268267
if err != nil {

contrib/nydusify/pkg/external/modctl/remote.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010
"io"
1111
"os"
1212
"strconv"
13+
"sync"
1314

1415
"github.com/dragonflyoss/nydus/contrib/nydusify/pkg/snapshotter/external/backend"
1516
"github.com/dragonflyoss/nydus/contrib/nydusify/pkg/utils"
1617
"github.com/pkg/errors"
1718
"github.com/sirupsen/logrus"
19+
"golang.org/x/sync/errgroup"
1820

1921
modelspec "github.com/CloudNativeAI/model-spec/specs-go/v1"
2022
pkgPvd "github.com/dragonflyoss/nydus/contrib/nydusify/pkg/provider"
@@ -80,14 +82,31 @@ func initRemoteHandler(handler *RemoteHandler) error {
8082
}
8183

8284
func (handler *RemoteHandler) Handle(ctx context.Context) (*backend.Backend, []backend.FileAttribute, error) {
83-
var fileAttrs []backend.FileAttribute
85+
var (
86+
fileAttrs []backend.FileAttribute
87+
mu sync.Mutex
88+
eg *errgroup.Group
89+
)
90+
eg, ctx = errgroup.WithContext(ctx)
91+
eg.SetLimit(5)
92+
8493
for idx, layer := range handler.manifest.Layers {
85-
fa, err := handler.handle(ctx, layer, int32(idx))
86-
if err != nil {
87-
return nil, nil, errors.Wrap(err, "handle layer failed")
88-
}
89-
fileAttrs = append(fileAttrs, fa...)
94+
eg.Go(func() error {
95+
fa, err := handler.handle(ctx, layer, int32(idx))
96+
if err != nil {
97+
return err
98+
}
99+
mu.Lock()
100+
fileAttrs = append(fileAttrs, fa...)
101+
mu.Unlock()
102+
return nil
103+
})
90104
}
105+
106+
if err := eg.Wait(); err != nil {
107+
return nil, nil, errors.Wrap(err, "wait for handle failed")
108+
}
109+
91110
bkd, err := handler.backend()
92111
if err != nil {
93112
return nil, nil, errors.Wrap(err, "get backend failed")

contrib/nydusify/pkg/snapshotter/external/external.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func RemoteHandle(ctx context.Context, opts Options) error {
9595
attributes = append(attributes, Attribute{
9696
Pattern: p,
9797
})
98-
logrus.Infof("file attr: %s, file_mode: %o", p, file.Mode)
98+
logrus.Debugf("file attr: %s, file_mode: %o", p, file.Mode)
9999
}
100100

101101
backendBytes, err := json.MarshalIndent(bkd, "", " ")

0 commit comments

Comments
 (0)