Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact(UUID): include replica address while creating UUID hash #344

Merged
merged 3 commits into from
Feb 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ install: true
services:
- docker
go:
- 1.13.x
- 1.14.7
env:
global:
- GOARCH=$(go env GOARCH)
Expand Down
26 changes: 10 additions & 16 deletions replica/replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"time"

units "github.com/docker/go-units"
guuid "github.com/google/uuid"
"github.com/openebs/jiva/types"
"github.com/openebs/jiva/util"
"github.com/openebs/sparse-tools/sparse"
Expand Down Expand Up @@ -149,34 +148,32 @@ const (
OpReplace = "replace"
)

func CreateTempReplica() (*Replica, error) {
func CreateTempReplica(s *Server) (*Replica, error) {
var err error
if err = os.Mkdir(Dir, 0700); err != nil && !os.IsExist(err) {
if err = os.Mkdir(s.Dir, 0700); err != nil && !os.IsExist(err) {
return nil, err
}

r := &Replica{
dir: Dir,
dir: s.Dir,
ReplicaStartTime: StartTime,
mode: types.INIT,
}
if err = r.initRevisionCounter(); err != nil {
logrus.Errorf("Error in initializing revision counter while creating temp replica")
return nil, err
}

if r.info, err = ReadInfo(r.dir); err != nil {
return nil, err
}
if r.info.UUID == "" {
r.info.UUID = guuid.New().String()
r.writeVolumeMetaData(r.info.Dirty, r.info.Rebuilding)

if err = r.initRevisionCounter(); err != nil {
logrus.Errorf("Error in initializing revision counter while creating temp replica")
return nil, err
}
return r, nil
}

func CreateTempServer() (*Server, error) {
func CreateTempServer(s *Server) (*Server, error) {
return &Server{
dir: Dir,
Dir: s.Dir,
}, nil
}

Expand Down Expand Up @@ -1206,9 +1203,6 @@ func (r *Replica) readMetadata() (bool, error) {
if r.info.Head == "" {
return false, fmt.Errorf("r.info.Head is nil")
}
if r.info.UUID == "" {
r.info.UUID = guuid.New().String()
}
file = fileMap[r.info.Head+metadataSuffix]
} else if strings.HasSuffix(file.Name(), metadataSuffix) {
if parent, err = r.readDiskData(file.Name()); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion replica/replica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ func (s *TestSuite) TestUpdateLUNMap(c *C) {
c.Assert(err, IsNil)
server := &Server{
r: r,
dir: dir,
Dir: dir,
}
// Fill data for S0
lunMapS0 := []int{1, 3, 5, 6}
Expand Down
5 changes: 2 additions & 3 deletions replica/rest/replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"strconv"

"github.com/gorilla/mux"
"github.com/openebs/jiva/replica"
"github.com/openebs/jiva/sync/rebuild"
"github.com/openebs/jiva/types"
"github.com/openebs/jiva/util"
Expand Down Expand Up @@ -119,7 +118,7 @@ func (s *Server) GetVolUsage(rw http.ResponseWriter, req *http.Request) error {

func (s *Server) GetRebuildInfo(rw http.ResponseWriter, req *http.Request) error {
apiContext := api.GetApiContext(req)
info := rebuild.GetRebuildInfo()
info := rebuild.GetRebuildInfo(s.s.Dir)
resp := &RebuildInfoOutput{
Resource: client.Resource{
Type: "rebuildinfo",
Expand Down Expand Up @@ -155,7 +154,7 @@ func (s *Server) SetLogging(rw http.ResponseWriter, req *http.Request) error {
}
logrus.Infof("SetLogging to %v", input.LogToFile)

return s.doOp(req, util.SetLogging(replica.Dir, input.LogToFile))
return s.doOp(req, util.SetLogging(s.s.Dir, input.LogToFile))
}

func (s *Server) SetRebuilding(rw http.ResponseWriter, req *http.Request) error {
Expand Down
47 changes: 34 additions & 13 deletions replica/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
package replica

import (
"crypto/sha1"
"errors"
"fmt"
"os"
"sync"
"time"

fibmap "github.com/frostschutz/go-fibmap"
guuid "github.com/google/uuid"
inject "github.com/openebs/jiva/error-inject"
"github.com/openebs/jiva/types"
"github.com/sirupsen/logrus"
Expand All @@ -44,15 +46,15 @@ const (
type State string

var ActionChannel chan string
var Dir string

var StartTime time.Time

type Server struct {
sync.RWMutex
r *Replica
ReplicaAddress string
ServerType string
dir string
Dir string
defaultSectorSize int64
backing *BackingFile
//This channel is used to montitor the IO connection
Expand All @@ -65,11 +67,10 @@ type Server struct {

func NewServer(address, dir string, sectorSize int64, serverType string) *Server {
ActionChannel = make(chan string, 5)
Dir = dir
StartTime = time.Now()
return &Server{
ReplicaAddress: address,
dir: dir,
Dir: dir,
defaultSectorSize: sectorSize,
ServerType: serverType,
MonitorChannel: make(chan struct{}),
Expand Down Expand Up @@ -129,7 +130,7 @@ func (s *Server) createTempFile(filePath string) (*os.File, error) {
}

func (s *Server) isExtentSupported() error {
filePath := s.dir + "/tmpFile.tmp"
filePath := s.Dir + "/tmpFile.tmp"
file, err := s.createTempFile(filePath)
if err != nil {
return err
Expand Down Expand Up @@ -159,25 +160,45 @@ func (s *Server) isExtentSupported() error {
return file.Close()
}

func (s *Server) initUUID() error {
var err error
r := &Replica{
dir: s.Dir,
ReplicaStartTime: StartTime,
mode: types.INIT,
}
if r.info, err = ReadInfo(s.Dir); err != nil {
return err
}
if r.info.UUID == "" {
UUID := guuid.New().String() + s.ReplicaAddress
hash := sha1.New()
hash.Write([]byte(UUID))
r.info.UUID = fmt.Sprintf("%x", hash.Sum(nil))
r.writeVolumeMetaData(r.info.Dirty, r.info.Rebuilding)
kmova marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}

func (s *Server) Create(size int64) error {
s.Lock()
defer s.Unlock()
if err := s.isExtentSupported(); err != nil {
return err
}
defer s.initUUID()
kmova marked this conversation as resolved.
Show resolved Hide resolved
state, _ := s.Status()

if state != Initial {
fmt.Println("STATE = ", state)
return nil
}

size = s.getSize(size)
sectorSize := s.getSectorSize()

logrus.Infof("Creating volume %s, size %d/%d", s.dir, size, sectorSize)
logrus.Infof("Creating volume %s, size %d/%d", s.Dir, size, sectorSize)
// Preload is not needed over here since the volume is being newly created
r, err := New(false, size, sectorSize, s.dir, s.backing, s.ServerType)
r, err := New(false, size, sectorSize, s.Dir, s.backing, s.ServerType)
if err != nil {
return err
}
Expand All @@ -196,8 +217,8 @@ func (s *Server) Open() error {
_, info := s.Status()
size := s.getSize(info.Size)
sectorSize := s.getSectorSize()
logrus.Infof("Opening volume %s, size %d/%d", s.dir, size, sectorSize)
r, err := New(s.preload, size, sectorSize, s.dir, s.backing, s.ServerType)
logrus.Infof("Opening volume %s, size %d/%d", s.Dir, size, sectorSize)
r, err := New(s.preload, size, sectorSize, s.Dir, s.backing, s.ServerType)
if err != nil {
logrus.Errorf("Error %v during open", err)
return err
Expand Down Expand Up @@ -271,7 +292,7 @@ func (s *Server) UpdateCloneInfo(snapName, revCount string) error {

func (s *Server) Status() (State, Info) {
if s.r == nil {
info, err := ReadInfo(s.dir)
info, err := ReadInfo(s.Dir)
if os.IsNotExist(err) {
return Initial, Info{}
} else if err != nil {
Expand All @@ -291,7 +312,7 @@ func (s *Server) Status() (State, Info) {
}

func (s *Server) PrevStatus() (State, Info) {
info, err := ReadInfo(s.dir)
info, err := ReadInfo(s.Dir)
if os.IsNotExist(err) {
return Initial, Info{}
} else if err != nil {
Expand Down Expand Up @@ -327,7 +348,7 @@ func (s *Server) Stats() *types.Stats {
// GetRevisionCounter reads the revison counter
func (s *Server) GetRevisionCounter() (int64, error) {
tmpReplica := Replica{
dir: Dir,
dir: s.Dir,
}
err := tmpReplica.initRevisionCounter()
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions sync/rebuild/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package rebuild
import (
"path"

"github.com/openebs/jiva/replica"
"github.com/openebs/jiva/types"
"github.com/openebs/jiva/util"
)
Expand Down Expand Up @@ -47,13 +46,13 @@ func SetStatus(disk, status string) {

// GetRebuildInfo returns the updated SyncInfo such as total
// used size of snapshots and size of individual snapshots.
func GetRebuildInfo() *types.SyncInfo {
func GetRebuildInfo(dir string) *types.SyncInfo {
if Info == nil {
return nil
}
var totSize int64
for i, snap := range Info.Snapshots {
size := util.GetFileActualSize(path.Join(replica.Dir, snap.Name))
size := util.GetFileActualSize(path.Join(dir, snap.Name))
if size == -1 {
Info.Snapshots[i].WOSize = "NA"
} else {
Expand Down
10 changes: 5 additions & 5 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func find(list []string, item string) int {
return -1
}

func (t *Task) AddQuorumReplica(replicaAddress string, _ *replica.Server) error {
func (t *Task) AddQuorumReplica(replicaAddress string, s *replica.Server) error {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
Register:
Expand All @@ -108,8 +108,8 @@ Register:
}
addr := strings.Split(replicaAddress, "://")
parts := strings.Split(addr[1], ":")
Replica, _ := replica.CreateTempReplica()
server, _ := replica.CreateTempServer()
Replica, _ := replica.CreateTempReplica(s)
server, _ := replica.CreateTempServer(s)

if volume.ReplicaCount == 0 {
revisionCount := Replica.GetRevisionCounter()
Expand Down Expand Up @@ -243,11 +243,11 @@ func (t *Task) AddReplica(replicaAddress string, s *replica.Server) error {
}
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
Replica, err := replica.CreateTempReplica()
Replica, err := replica.CreateTempReplica(s)
if err != nil {
return fmt.Errorf("failed to create temp replica, error: %s", err.Error())
}
server, err := replica.CreateTempServer()
server, err := replica.CreateTempServer(s)
if err != nil {
return fmt.Errorf("failed to create temp server, error: %s", err.Error())
}
Expand Down