Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
naoki9911 committed Mar 27, 2024
1 parent af8f99b commit f759eea
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 54 deletions.
10 changes: 6 additions & 4 deletions cmd/ctr-cli/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -198,7 +197,10 @@ func Action(c *cli.Context) error {
return err
}
defer manifestFile.Close()
manifestFile.Write(manifestBytes)
_, err = manifestFile.Write(manifestBytes)
if err != nil {
return err
}

logger.Infof("manifest is written to %q", manifestPath)

Expand All @@ -207,7 +209,7 @@ func Action(c *cli.Context) error {
}

// convert layer.tar to dimg
tempDir, err := ioutil.TempDir("/tmp/ctr-cli", "*")
tempDir, err := os.MkdirTemp("/tmp/ctr-cli", "*")
if err != nil {
return err
}
Expand All @@ -228,7 +230,7 @@ func Action(c *cli.Context) error {
}
}

tempDiffDir, err := ioutil.TempDir("/tmp/ctr-cli", "*")
tempDiffDir, err := os.MkdirTemp("/tmp/ctr-cli", "*")
if err != nil {
return err
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/ctr-cli/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ var Flags = []cli.Flag{

func LoadImage(snClient *sns.Client, ctx context.Context, imageName, imageVersion string, image *image.Di3FSImage) error {
cs := snClient.CtrClient.ContentStore()
cs.Delete(ctx, image.Header.ManifestDigest)
err := content.WriteBlob(ctx, cs, image.Header.ManifestDigest.Hex(), bytes.NewReader(image.ManifestBytes),
err := cs.Delete(ctx, image.Header.ManifestDigest)
if err != nil {
log.G(ctx).Infof("%s is already removed: %v", image.Header.ManifestDigest, err)
}
err = content.WriteBlob(ctx, cs, image.Header.ManifestDigest.Hex(), bytes.NewReader(image.ManifestBytes),
v1.Descriptor{
Size: int64(len(image.ManifestBytes)),
Digest: image.Header.ManifestDigest,
Expand Down
10 changes: 8 additions & 2 deletions cmd/fuse-diff/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ func writeMemProfile(fn string, sigs <-chan os.Signal) {
log.Printf("Create: %v", err)
continue
}
pprof.WriteHeapProfile(f)
err = pprof.WriteHeapProfile(f)
if err != nil {
log.Printf("failed WriteHeapProfile: %v", err)
}
if err := f.Close(); err != nil {
log.Printf("close %v", err)
}
Expand Down Expand Up @@ -83,7 +86,10 @@ func main() {
fmt.Println(err)
os.Exit(3)
}
pprof.StartCPUProfile(f)
err = pprof.StartCPUProfile(f)
if err != nil {
log.Fatalf("failed to start CPUProfile: %v", err)
}
defer pprof.StopCPUProfile()
}
if *memprofile != "" {
Expand Down
5 changes: 4 additions & 1 deletion cmd/patch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ func applyPatch(basePath, newPath string, dirEntry *image.FileEntry, img *image.
}
defer newFile.Close()

io.Copy(newFile, patchReader)
_, err = io.Copy(newFile, patchReader)
if err != nil {
return err
}
} else if dirEntry.Type == image.FILE_ENTRY_FILE_DIFF {
var patchReader io.Reader
logger.Debugf("applying diff to %q from image(offset=%d size=%d)", newFilePath, dirEntry.Offset, dirEntry.CompressedSize)
Expand Down
12 changes: 10 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,12 @@ func handleGetUpdateData(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
return
}
tempFile.Write(diffDataBytes.Bytes())
_, err = tempFile.Write(diffDataBytes.Bytes())
if err != nil {
logger.Errorf("failed to write to tmp file: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
lowerFileName = tempFile.Name()
logger.Infof("temp file saved at %s", lowerFileName)
tempFile.Close()
Expand Down Expand Up @@ -289,5 +294,8 @@ func main() {
http.HandleFunc("/diffData/add", handlePostDiffData)
http.HandleFunc("/diffData/cleanup", handleDeleteDiffData)
logger.Info("started")
http.ListenAndServe(":8081", nil)
err = http.ListenAndServe(":8081", nil)
if err != nil {
logger.Fatalf("failed to start server: %v", err)
}
}
6 changes: 5 additions & 1 deletion cmd/snapshotter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,14 @@ func (c *Client) startSnapshotter() {
WithField("socket", c.snSocketPath).
Info("di3fs snapshotter service started")

c.snSnapshotter.Walk(c.ctx, func(ctx context.Context, i snapshots.Info) error {
err = c.snSnapshotter.Walk(c.ctx, func(ctx context.Context, i snapshots.Info) error {
log.G(ctx).WithField("snapshots.Info", i).Info("walking")
return nil
})
if err != nil {
log.G(c.ctx).Errorf("failed to walk snapshots")
return
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
Expand Down
50 changes: 42 additions & 8 deletions cmd/snapshotter/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,12 @@ func (o *snapshotter) Stat(ctx context.Context, key string) (snapshots.Info, err
if err != nil {
return snapshots.Info{}, err
}
defer t.Rollback()
defer func() {
err = t.Rollback()
if err != nil {
log.G(ctx).Errorf("failed to Rollback")
}
}()
_, info, _, err := storage.GetInfo(ctx, key)
if err != nil {
return snapshots.Info{}, err
Expand All @@ -203,7 +208,10 @@ func (o *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpath

info, err = storage.UpdateInfo(ctx, info, fieldpaths...)
if err != nil {
t.Rollback()
err = t.Rollback()
if err != nil {
log.G(ctx).Errorf("failed to Rollback")
}
return snapshots.Info{}, err
}

Expand All @@ -229,7 +237,10 @@ func (o *snapshotter) Usage(ctx context.Context, key string) (snapshots.Usage, e
return snapshots.Usage{}, err
}
id, info, usage, err := storage.GetInfo(ctx, key)
t.Rollback() // transaction no longer needed at this point.
errRB := t.Rollback() // transaction no longer needed at this point.
if errRB != nil {
log.G(ctx).Errorf("failed to Rollback")
}

if err != nil {
return snapshots.Usage{}, err
Expand Down Expand Up @@ -326,7 +337,10 @@ func (o *snapshotter) Mounts(ctx context.Context, key string) ([]mount.Mount, er
return nil, err
}
s, err := storage.GetSnapshot(ctx, key)
t.Rollback()
errRB := t.Rollback()
if errRB != nil {
log.G(ctx).Errorf("failed to Rollback: %v", err)
}
if err != nil {
return nil, fmt.Errorf("failed to get active mount: %w", err)
}
Expand Down Expand Up @@ -431,7 +445,12 @@ func (o *snapshotter) Walk(ctx context.Context, fn snapshots.WalkFunc, fs ...str
if err != nil {
return err
}
defer t.Rollback()
defer func() {
errRB := t.Rollback()
if errRB != nil {
log.G(ctx).Errorf("failed to Rollback: %v", err)
}
}()
return storage.WalkInfo(ctx, fn, fs...)
}

Expand Down Expand Up @@ -466,7 +485,12 @@ func (o *snapshotter) cleanupDirectories(ctx context.Context, cleanupCommitted b
return nil, err
}

defer t.Rollback()
defer func() {
errRB := t.Rollback()
if errRB != nil {
log.G(ctx).Errorf("failed to Rollback: %v", err)
}
}()
return o.getCleanupDirectories(ctx, t, cleanupCommitted)
}

Expand Down Expand Up @@ -702,7 +726,12 @@ func (o *snapshotter) prepareRemoteSnapshot(ctx context.Context, key string, lab
if err != nil {
return err
}
defer t.Rollback()
defer func() {
errRB := t.Rollback()
if errRB != nil {
log.G(ctx).Errorf("failed to Rollback: %v", err)
}
}()
id, _, _, err := storage.GetInfo(ctx, key)
if err != nil {
return err
Expand All @@ -725,7 +754,12 @@ func (o *snapshotter) checkAvailability(ctx context.Context, key string) bool {
log.G(ctx).WithError(err).Warn("failed to get transaction")
return false
}
defer t.Rollback()
defer func() {
errRB := t.Rollback()
if errRB != nil {
log.G(ctx).Errorf("failed to Rollback: %v", err)
}
}()

eg, egCtx := errgroup.WithContext(ctx)
for cKey := key; cKey != ""; {
Expand Down
3 changes: 1 addition & 2 deletions cmd/stats/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path"
"strings"
Expand Down Expand Up @@ -106,7 +105,7 @@ func compressWithGzipFromFile(path string) ([]byte, error) {
if err != nil {
return nil, err
}
fileBytes, err := ioutil.ReadAll(file)
fileBytes, err := io.ReadAll(file)
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/containerd/ttrpc v1.1.0 // indirect
github.com/containerd/typeurl v1.0.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
github.com/gogo/googleapis v1.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand All @@ -40,8 +41,9 @@ require (
github.com/opencontainers/runc v1.1.2 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect
github.com/opencontainers/selinux v1.10.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.opencensus.io v0.23.0 // indirect
Expand All @@ -50,4 +52,5 @@ require (
golang.org/x/text v0.5.0 // indirect
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
Expand Down
33 changes: 4 additions & 29 deletions pkg/algorithm/dijkstra.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func NewNode(name string) *Node {
}

// ノードに次の接続先を示したエッジを追加する
func (self *Node) AddEdge(edge *Edge) {
self.edges = append(self.edges, edge)
func (n *Node) AddEdge(edge *Edge) {
n.edges = append(n.edges, edge)
}

func (self *Node) GetName() string {
return self.name
func (n *Node) GetName() string {
return n.name
}

// エッジ
Expand Down Expand Up @@ -185,28 +185,3 @@ func (self *DirectedGraph) nextNode() (next *Node, err error) {

return
}

func main() {
// 有向グラフを作る
g := NewDirectedGraph()

// グラフを定義していく
g.Add("1.23.1", "1.23.2", 1)
g.Add("1.23.2", "1.23.3", 1)
g.Add("1.23.3", "1.23.4", 1)
g.Add("1.23.1", "1.23.3", 1)

// "s" ノードから "z" ノードへの最短経路を得る
path, err := g.ShortestPath("1.23.1", "1.23.4")

// 経路が見つからなければ終了
if err != nil {
fmt.Println("Goal not found")
return
}

// 見つかった経路からノードとコストを表示する
for _, node := range path {
fmt.Printf("ノード: %v, コスト: %v\n", node.name, node.cost)
}
}
24 changes: 24 additions & 0 deletions pkg/algorithm/dijkstra_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package algorithm_test

import (
"testing"

"github.com/naoki9911/fuse-diff-containerd/pkg/algorithm"
"github.com/stretchr/testify/assert"
)

func TestDijkstra(t *testing.T) {
g := algorithm.NewDirectedGraph()

g.Add("1.23.1", "1.23.2", 1)
g.Add("1.23.2", "1.23.3", 1)
g.Add("1.23.3", "1.23.4", 1)
g.Add("1.23.1", "1.23.3", 1)

path, err := g.ShortestPath("1.23.1", "1.23.4")
assert.Equal(t, nil, err)
assert.Equal(t, 3, len(path))
assert.Equal(t, "1.23.1", path[0].GetName())
assert.Equal(t, "1.23.3", path[1].GetName())
assert.Equal(t, "1.23.4", path[2].GetName())
}
5 changes: 4 additions & 1 deletion pkg/snapshotter/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ func NewClient() (*Client, error) {

c.SnClient = c.CtrClient.SnapshotService("di3fs")
c.SnImageStorePath = filepath.Join(c.SnRootPath, "images")
os.MkdirAll(c.SnImageStorePath, 0o644)
err = os.MkdirAll(c.SnImageStorePath, 0o644)
if err != nil {
return nil, err
}

return c, nil
}
2 changes: 1 addition & 1 deletion pkg/snapshotter/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func CreateSnapshot(ctx context.Context, ss snapshots.Snapshotter, manifestDiges
})
err = ss.Commit(ctx, dimgDigest.String(), randId, opts, optsWithMount)
if err != nil {
log.G(ctx).Errorf("failed to commit snapshot :%w", err)
log.G(ctx).Errorf("failed to commit snapshot : %v", err)
return err
}
log.G(ctx).Debug("commit done")
Expand Down

0 comments on commit f759eea

Please sign in to comment.