Skip to content

Commit

Permalink
refactor: replace some common functions
Browse files Browse the repository at this point in the history
Signed-off-by: Chin-Ya Huang <chin-ya.huang@suse.com>
  • Loading branch information
c3y1huang authored and David Ko committed Dec 21, 2023
1 parent 7ed4ba4 commit 1a5cdbf
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 37 deletions.
8 changes: 4 additions & 4 deletions app/cmd/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/sirupsen/logrus"

"github.com/longhorn/longhorn-engine/pkg/util"
lhutils "github.com/longhorn/go-common-libs/utils"
)

var (
Expand All @@ -20,7 +20,7 @@ func addShutdown(f func() (err error)) {
}

hooks = append(hooks, f)
logrus.Debugf("Added shutdown func %v", util.GetFunctionName(f))
logrus.Debugf("Added shutdown func %v", lhutils.GetFunctionPath(f))
}

func registerShutdown() {
Expand All @@ -31,9 +31,9 @@ func registerShutdown() {
logrus.Warnf("Received signal %v to shutdown", s)
exitCode := 0
for _, hook := range hooks {
logrus.Warnf("Starting to execute registered shutdown func %v", util.GetFunctionName(hook))
logrus.Warnf("Starting to execute registered shutdown func %v", lhutils.GetFunctionPath(hook))
if err := hook(); err != nil {
logrus.WithError(err).Warnf("Failed to execute hook %v", util.GetFunctionName(hook))
logrus.WithError(err).Warnf("Failed to execute hook %v", lhutils.GetFunctionPath(hook))
exitCode = 1
}
}
Expand Down
4 changes: 3 additions & 1 deletion app/cmd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/sirupsen/logrus"
"github.com/urfave/cli"

lhutils "github.com/longhorn/go-common-libs/utils"

"github.com/longhorn/longhorn-engine/pkg/controller/client"
"github.com/longhorn/longhorn-engine/pkg/sync"
"github.com/longhorn/longhorn-engine/pkg/types"
Expand Down Expand Up @@ -384,7 +386,7 @@ func lsSnapshot(c *cli.Context) error {
}

snapshots = util.Filter(snapshots, func(i string) bool {
return util.Contains(chain, i)
return lhutils.Contains(chain, i)
})
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/docker/go-units v0.3.3
github.com/gofrs/flock v0.8.1
github.com/golang/protobuf v1.5.3
github.com/google/uuid v1.3.0
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/longhorn/backupstore v0.0.0-20231025000449-5316e8b5320d
Expand Down Expand Up @@ -41,6 +40,7 @@ require (
github.com/gammazero/workerpool v1.1.3 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/jmespath/go-jmespath v0.3.0 // indirect
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"google.golang.org/grpc/codes"

lhns "github.com/longhorn/go-common-libs/ns"
lhutils "github.com/longhorn/go-common-libs/utils"

"github.com/longhorn/longhorn-engine/pkg/types"
"github.com/longhorn/longhorn-engine/pkg/util"
Expand Down Expand Up @@ -197,7 +198,7 @@ func (c *Controller) Snapshot(name string, labels map[string]string) (string, er
defer c.Unlock()

if name == "" {
name = util.UUID()
name = lhutils.UUID()
}

if _, err := c.backend.RemainSnapshots(); err != nil {
Expand Down Expand Up @@ -345,7 +346,7 @@ func (c *Controller) addReplicaNoLock(newBackend types.Backend, address string,
}

if snapshot && mode != types.ERR {
uuid := util.UUID()
uuid := lhutils.UUID()
created := util.Now()

if _, err := c.backend.RemainSnapshots(); err != nil {
Expand Down
8 changes: 5 additions & 3 deletions pkg/sync/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (

"github.com/longhorn/backupstore"

replicaClient "github.com/longhorn/longhorn-engine/pkg/replica/client"
lhutils "github.com/longhorn/go-common-libs/utils"

"github.com/longhorn/longhorn-engine/pkg/types"
"github.com/longhorn/longhorn-engine/pkg/util"

replicaClient "github.com/longhorn/longhorn-engine/pkg/replica/client"
diskutil "github.com/longhorn/longhorn-engine/pkg/util/disk"
)

Expand Down Expand Up @@ -198,7 +200,7 @@ func (t *Task) RestoreBackup(backup string, credential map[string]string, concur
return fmt.Errorf("BUG: replicas %+v of DR volume should contains at least 2 disk files: the volume head and a snapshot storing restore data", replicas)
}
if snapshotDiskName == "" {
snapshotDiskName = diskutil.GenerateSnapshotDiskName(util.UUID())
snapshotDiskName = diskutil.GenerateSnapshotDiskName(lhutils.UUID())
}
case 2: // the volume head and the only system snapshot.
for _, s := range snapshots {
Expand Down
26 changes: 0 additions & 26 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ import (
"net/url"
"os"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"syscall"
"time"

"github.com/google/uuid"
"github.com/gorilla/handlers"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
Expand All @@ -28,8 +25,6 @@ var (
MaximumVolumeNameSize = 64
validVolumeName = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_.-]+$`)

HostProc = "/host/proc"

unixDomainSocketDirectoryInContainer = "/host/var/lib/longhorn/unix-domain-socket/"
)

Expand Down Expand Up @@ -85,10 +80,6 @@ func GetPortFromAddress(address string) (int, error) {
return port, nil
}

func UUID() string {
return uuid.New().String()
}

func Filter(list []string, check func(string) bool) []string {
result := make([]string, 0, len(list))
for _, i := range list {
Expand All @@ -99,15 +90,6 @@ func Filter(list []string, check func(string) bool) []string {
return result
}

func Contains(arr []string, val string) bool {
for _, a := range arr {
if a == val {
return true
}
}
return false
}

type filteredLoggingHandler struct {
filteredPaths map[string]struct{}
handler http.Handler
Expand Down Expand Up @@ -290,14 +272,6 @@ func ResolveBackingFilepath(fileOrDirpath string) (string, error) {
return fileOrDirpath, nil
}

func GetFunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}

func RandomID(randomIDLenth int) string {
return UUID()[:randomIDLenth]
}

func GetAddresses(volumeName, address string, dataServerProtocol types.DataServerProtocol) (string, string, string, int, error) {
switch dataServerProtocol {
case types.DataServerProtocolTCP:
Expand Down

0 comments on commit 1a5cdbf

Please sign in to comment.