Skip to content

Commit

Permalink
split merge function
Browse files Browse the repository at this point in the history
Signed-off-by: Naoki MATSUMOTO <[email protected]>
  • Loading branch information
naoki9911 committed Apr 7, 2024
1 parent c580aa8 commit 49fd705
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
25 changes: 25 additions & 0 deletions pkg/image/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"sync"
"time"

"github.com/klauspost/compress/zstd"
"github.com/naoki9911/fuse-diff-containerd/pkg/benchmark"
"github.com/naoki9911/fuse-diff-containerd/pkg/bsdiffx"
"github.com/naoki9911/fuse-diff-containerd/pkg/utils"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -903,3 +905,26 @@ func MergeCdimg(lowerCdimg, upperCdimg string, merged io.Writer, mc MergeConfig)
}
return &header, nil
}

func MergeDimgsWithLinear(dimgs []*DimgEntry, tmpDir string, mc MergeConfig) (*DimgEntry, error) {
lowerDimg := dimgs[len(dimgs)-1]
for idx := len(dimgs) - 2; idx >= 0; idx-- {
upperDimg := dimgs[idx]
mergedDimgPath := filepath.Join(tmpDir, utils.GetRandomId("merge")+".dimg")
mergedFile, err := os.Create(mergedDimgPath)
if err != nil {
return nil, fmt.Errorf("failed to create temporary dimg %s: %v", mergedDimgPath, err)
}
defer mergedFile.Close()

logger.Infof("merge %s and %s", lowerDimg.Digest(), upperDimg.Digest())
header, err := MergeDimg(lowerDimg.Path, upperDimg.Path, mergedFile, mc)
if err != nil {
return nil, fmt.Errorf("failed to merge dimgs: %v", err)
}
lowerDimg.DimgHeader = *header
lowerDimg.Path = mergedDimgPath
}

return lowerDimg, nil
}
34 changes: 12 additions & 22 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,29 +191,19 @@ func (ds *DiffServer) handleGetUpdateData(w http.ResponseWriter, r *http.Request
}
logger.Infof("Dimgs are sent to client %s", dimgsMsg)

lowerDimg := selectedDimgPaths[len(selectedDimgPaths)-1]
for idx := len(selectedDimgPaths) - 2; idx >= 0; idx-- {
upperDimg := selectedDimgPaths[idx]
mergedDimgPath := filepath.Join(imageStorePath, utils.GetRandomId("d4c-server")+".dimg")
mergedFile, err := os.Create(mergedDimgPath)
if err != nil {
logger.Errorf("failed to create temporarly dimg %s: %v", mergedDimgPath, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
defer mergedFile.Close()

logger.Infof("merge %s and %s", lowerDimg.Digest(), upperDimg.Digest())
header, err := image.MergeDimg(lowerDimg.Path, upperDimg.Path, mergedFile, ds.mergeConfig)
if err != nil {
logger.Errorf("failed to merge dimgs: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
lowerDimg.DimgHeader = *header
lowerDimg.Path = mergedDimgPath
tmpDir := filepath.Join(imageStorePath, utils.GetRandomId("merge-tmp"))
err = os.Mkdir(tmpDir, 0755)
if err != nil {
logger.Errorf("failed to create temporary directory %s: %v", tmpDir, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
resDimg, err := image.MergeDimgsWithLinear(selectedDimgPaths, tmpDir, ds.mergeConfig)
if err != nil {
logger.Errorf("failed to merge: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
resDimg := lowerDimg

resDimgFile, err := image.OpenDimgFile(resDimg.Path)
if err != nil {
Expand Down

0 comments on commit 49fd705

Please sign in to comment.