Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Su <[email protected]>
  • Loading branch information
derekbit committed Nov 19, 2024
1 parent 4a19108 commit 7357f50
Showing 1 changed file with 41 additions and 34 deletions.
75 changes: 41 additions & 34 deletions pkg/spdk/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2138,52 +2138,60 @@ func (e *Engine) Resume(spdkClient *spdkclient.Client) (err error) {
}

// SwitchOverTarget function in the Engine struct is responsible for switching the engine's target to a new address.
func (e *Engine) SwitchOverTarget(spdkClient *spdkclient.Client, targetAddress string) (err error) {
e.log.Infof("Switching over engine to target address %s", targetAddress)
func (e *Engine) SwitchOverTarget(spdkClient *spdkclient.Client, newTargetAddress string) (err error) {
e.log.Infof("Switching over engine to target address %s", newTargetAddress)

Check warning on line 2142 in pkg/spdk/engine.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/engine.go#L2141-L2142

Added lines #L2141 - L2142 were not covered by tests

currentTargetAddress := ""

podIP, err := commonnet.GetIPForPod()
if err != nil {
return errors.Wrapf(err, "failed to get IP for pod for engine %s target switchover", e.Name)
}

Check warning on line 2149 in pkg/spdk/engine.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/engine.go#L2146-L2149

Added lines #L2146 - L2149 were not covered by tests

newTargetIP, newTargetPort, err := splitHostPort(newTargetAddress)
if err != nil {
return errors.Wrapf(err, "failed to split target address %s for engine %s target switchover", newTargetAddress, e.Name)
}

Check warning on line 2154 in pkg/spdk/engine.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/engine.go#L2151-L2154

Added lines #L2151 - L2154 were not covered by tests

e.Lock()
defer func() {
e.Unlock()

if err != nil {
e.log.WithError(err).Warnf("Failed to switch over engine to target address %s", targetAddress)
e.log.WithError(err).Warnf("Failed to switch over engine to target address %s", newTargetAddress)

Check warning on line 2161 in pkg/spdk/engine.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/engine.go#L2161

Added line #L2161 was not covered by tests

if disconnected, errCheck := e.isTargetDisconnected(); errCheck != nil {
e.log.WithError(errCheck).Warnf("Failed to check if target %s is disconnected", targetAddress)
e.log.WithError(errCheck).Warnf("Failed to check if target %s is disconnected", newTargetAddress)

Check warning on line 2164 in pkg/spdk/engine.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/engine.go#L2163-L2164

Added lines #L2163 - L2164 were not covered by tests
} else if disconnected {
if errConnect := e.connectTarget(currentTargetAddress); errConnect != nil {
e.log.WithError(errConnect).Warnf("Failed to connect target back to %s", currentTargetAddress)
} else {
e.log.Infof("Connected target back to %s", currentTargetAddress)

if errReload := e.reloadDevice(); errReload != nil {
e.log.WithError(errReload).Warnf("Failed to reload device mapper")
if currentTargetAddress != "" {
if errConnect := e.connectTarget(currentTargetAddress); errConnect != nil {
e.log.WithError(errConnect).Warnf("Failed to connect target back to %s", currentTargetAddress)

Check warning on line 2168 in pkg/spdk/engine.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/engine.go#L2166-L2168

Added lines #L2166 - L2168 were not covered by tests
} else {
e.log.Infof("Reloaded device mapper for connecting target back to %s", currentTargetAddress)
e.log.Infof("Connected target back to %s", currentTargetAddress)

if errReload := e.reloadDevice(); errReload != nil {
e.log.WithError(errReload).Warnf("Failed to reload device mapper")
} else {
e.log.Infof("Reloaded device mapper for connecting target back to %s", currentTargetAddress)
}

Check warning on line 2176 in pkg/spdk/engine.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/engine.go#L2170-L2176

Added lines #L2170 - L2176 were not covered by tests
}
}
}
} else {
e.ErrorMsg = ""

e.log.Infof("Switched over target to %s", targetAddress)
e.log.Infof("Switched over target to %s", newTargetAddress)

Check warning on line 2183 in pkg/spdk/engine.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/engine.go#L2183

Added line #L2183 was not covered by tests
}

e.UpdateCh <- nil
}()

podIP, err := commonnet.GetIPForPod()
if err != nil {
return err
}

initiator, err := nvme.NewInitiator(e.VolumeName, e.Nqn, nvme.HostProc)
if err != nil {
return errors.Wrapf(err, "failed to create initiator for engine %s target switchover", e.Name)
}

// Check if the engine is suspended before target switchover.
suspended, err := initiator.IsSuspended()
if err != nil {
return errors.Wrapf(err, "failed to check if engine %s is suspended", e.Name)
Expand All @@ -2192,43 +2200,42 @@ func (e *Engine) SwitchOverTarget(spdkClient *spdkclient.Client, targetAddress s
return fmt.Errorf("engine %s must be suspended before target switchover", e.Name)
}

// Load NVMe device info before target switchover.
if err := initiator.LoadNVMeDeviceInfo(initiator.TransportAddress, initiator.TransportServiceID, initiator.SubsystemNQN); err != nil {
if !nvme.IsValidNvmeDeviceNotFound(err) {
return errors.Wrapf(err, "failed to load NVMe device info for engine %s target switchover", e.Name)
}
}

currentTargetAddress = net.JoinHostPort(initiator.TransportAddress, initiator.TransportServiceID)
if e.isSwitchOverTargetRequired(currentTargetAddress, targetAddress) {
if isSwitchOverTargetRequired(currentTargetAddress, newTargetAddress) {

Check warning on line 2211 in pkg/spdk/engine.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/engine.go#L2211

Added line #L2211 was not covered by tests
if currentTargetAddress != "" {
if err := e.disconnectTarget(currentTargetAddress); err != nil {
return err
return errors.Wrapf(err, "failed to disconnect target %s for engine %s", currentTargetAddress, e.Name)

Check warning on line 2214 in pkg/spdk/engine.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/engine.go#L2214

Added line #L2214 was not covered by tests
}
}

if err := e.connectTarget(targetAddress); err != nil {
return err
if err := e.connectTarget(newTargetAddress); err != nil {
return errors.Wrapf(err, "failed to connect target %s for engine %s", newTargetAddress, e.Name)

Check warning on line 2219 in pkg/spdk/engine.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/engine.go#L2218-L2219

Added lines #L2218 - L2219 were not covered by tests
}
}

// Replace IP and Port with the new target address.
// No need to update TargetIP and TargetPort, because target is not delete yet.
targetIP, targetPort, err := splitHostPort(targetAddress)
if err != nil {
return errors.Wrapf(err, "failed to split target address %s", targetAddress)
}

e.IP = targetIP
e.Port = targetPort
// No need to update TargetIP, because old target is not delete yet.
e.IP = newTargetIP
e.Port = newTargetPort

Check warning on line 2226 in pkg/spdk/engine.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/engine.go#L2225-L2226

Added lines #L2225 - L2226 were not covered by tests

if targetIP == podIP {
e.TargetPort = targetPort
if newTargetIP == podIP {
e.TargetPort = newTargetPort
e.StandbyTargetPort = 0
} else {
e.StandbyTargetPort = e.TargetPort
e.TargetPort = 0
}

Check warning on line 2234 in pkg/spdk/engine.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/engine.go#L2228-L2234

Added lines #L2228 - L2234 were not covered by tests

e.log.Info("Reloading device mapper after target switchover")
if err := e.reloadDevice(); err != nil {
return err
return errors.Wrapf(err, "failed to reload device mapper after engine %s target switchover", e.Name)

Check warning on line 2238 in pkg/spdk/engine.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/engine.go#L2238

Added line #L2238 was not covered by tests
}

return nil
Expand Down Expand Up @@ -2394,7 +2401,7 @@ func (e *Engine) DeleteTarget(spdkClient *spdkclient.Client, superiorPortAllocat
return nil
}

func (e *Engine) isSwitchOverTargetRequired(oldTargetAddress, newTargetAddress string) bool {
func isSwitchOverTargetRequired(oldTargetAddress, newTargetAddress string) bool {

Check warning on line 2404 in pkg/spdk/engine.go

View check run for this annotation

Codecov / codecov/patch

pkg/spdk/engine.go#L2404

Added line #L2404 was not covered by tests
return oldTargetAddress != newTargetAddress
}

Expand Down

0 comments on commit 7357f50

Please sign in to comment.