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

WaitForXxx method tuning #15

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
8 changes: 3 additions & 5 deletions ecs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ const DefaultWaitForInterval = 5
// Default timeout value for WaitForXXX method
const DefaultTimeout = 60


// Default timeout strategy
var DefaultStrategy = util.AttemptStrategy{
Min: 5,
Total: 60 * time.Second,
Delay: 5 * time.Second,
Min: 5,
Total: 60 * time.Second,
Delay: 5 * time.Second,
}


// A Client represents a client of ECS services
type Client struct {
AccessKeyId string //Access Key Id
Expand Down
2 changes: 1 addition & 1 deletion ecs/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestECSDescribe(t *testing.T) {
}
instances, _, err := client.DescribeInstances(&args1)
if err != nil {
t.Errorf("Failed to describe instance %s %s", region.RegionId, zone.ZoneId)
t.Errorf("Failed to describe instance %s %s , %v", region.RegionId, zone.ZoneId, err)
} else {
for _, instance := range instances {
t.Logf("Instance: %++v", instance)
Expand Down
46 changes: 25 additions & 21 deletions ecs/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ecs

import (
"github.com/denverdino/aliyungo/util"
"time"
)

// Types of disks
Expand Down Expand Up @@ -37,6 +36,11 @@ const (
DiskStatusAll = DiskStatus("All") //Default
)

var FinalDiskStatus = map[DiskStatus]bool{
DiskStatusInUse: true,
DiskStatusAvailable: true,
}

// A DescribeDisksArgs defines the arguments to describe disks
type DescribeDisksArgs struct {
RegionId Region
Expand Down Expand Up @@ -241,32 +245,32 @@ func (client *Client) ModifyDiskAttribute(args *ModifyDiskAttributeArgs) error {
return err
}

// WaitForDisk waits for disk to given status
func (client *Client) WaitForDisk(regionId Region, diskId string, status DiskStatus, timeout int) error {
if timeout <= 0 {
timeout = DefaultTimeout
}
args := DescribeDisksArgs{
RegionId: regionId,
DiskIds: []string{diskId},
}
// WaitForDisk waits for disk to the final status
func (client *Client) WaitForDisk(regionId Region, diskId string, strategy util.AttemptStrategy) (status interface{}, err error) {

fn := func() (bool, interface{}, error) {

args := DescribeDisksArgs{
RegionId: regionId,
DiskIds: []string{diskId},
}

for {
disks, _, err := client.DescribeDisks(&args)
if err != nil {
return err
return false, util.StatusNotAvailable, err
}

if disks == nil || len(disks) == 0 {
return getECSErrorFromString("Not found")
}
if disks[0].Status == status {
break
return false, getECSErrorFromString("Not found"), nil
}
timeout = timeout - DefaultWaitForInterval
if timeout <= 0 {
return getECSErrorFromString("Timeout")

if FinalDiskStatus[disks[0].Status] {
return true, disks[0].Status, nil
}
time.Sleep(DefaultWaitForInterval * time.Second)
return false, util.StatusNotAvailable, nil
}
return nil

status, e1 := util.LoopCall(strategy, fn)

return status, e1
}
14 changes: 7 additions & 7 deletions ecs/disks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ func TestDiskCreationAndDeletion(t *testing.T) {
} else {
t.Logf("Instance: %++v %v", instance, err)
}
err = client.WaitForDisk(instance.RegionId, diskId, DiskStatusInUse, 0)
if err != nil {
t.Fatalf("Failed to wait for disk %s to status %s: %v", diskId, DiskStatusInUse, err)
status, err := client.WaitForDisk(instance.RegionId, diskId, DefaultStrategy)
if err != nil || status != DiskStatusInUse {
t.Fatalf("Failed to wait for disk %s to status %s, current status %s: %v", diskId, DiskStatusInUse, status, err)
}
err = client.DetachDisk(instance.InstanceId, diskId)
if err != nil {
t.Errorf("Failed to detach disk: %v", err)
} else {
t.Logf("Detach disk %s to instance %s successfully", diskId, instance.InstanceId)
t.Logf("Detach disk %s to instance %s successfully, current status: %s", diskId, instance.InstanceId, status)
}

err = client.WaitForDisk(instance.RegionId, diskId, DiskStatusAvailable, 0)
if err != nil {
t.Fatalf("Failed to wait for disk %s to status %s: %v", diskId, DiskStatusAvailable, err)
status, err = client.WaitForDisk(instance.RegionId, diskId, DefaultStrategy)
if err != nil || status != DiskStatusAvailable {
t.Fatalf("Failed to wait for disk %s to status %s, current status %s: %v", diskId, DiskStatusAvailable, status, err)
}
}
err = client.DeleteDisk(diskId)
Expand Down
19 changes: 9 additions & 10 deletions ecs/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
Stopping = InstanceStatus("Stopping")
)

var FinalStatus = map[InstanceStatus]bool {
var FinalInstanceStatus = map[InstanceStatus]bool{
Running: true,
Stopped: true,
}
Expand Down Expand Up @@ -198,26 +198,25 @@ func (client *Client) DescribeInstanceAttribute(instanceId string) (instance *In
}

// WaitForInstance waits for instance to given status
func (client *Client) WaitForInstance(instanceId string, strategy util.AttemptStrategy)(status interface{}, err error) {
func (client *Client) WaitForInstance(instanceId string, strategy util.AttemptStrategy) (status interface{}, err error) {

fn := func() (bool,interface{},error) {
fn := func() (bool, interface{}, error) {

instance, err := client.DescribeInstanceAttribute(instanceId)
if err != nil {
return false, "" , err
return false, util.StatusNotAvailable, err
}
if FinalStatus[instance.Status] {
return true,instance.Status,nil
if FinalInstanceStatus[instance.Status] {
return true, instance.Status, nil
}
return false, "" , nil
return false, util.StatusNotAvailable, nil
}

status,e1 := util.LoopCall(strategy,fn);
status, e1 := util.LoopCall(strategy, fn)

return status,e1
return status, e1
}


type DescribeInstanceVncUrlArgs struct {
RegionId Region
InstanceId string
Expand Down
15 changes: 7 additions & 8 deletions ecs/instances_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package ecs

import (
"testing"
"github.com/denverdino/aliyungo/util"
"testing"
"time"
)

var defaultInstanceStrategy = util.AttemptStrategy{
Min: 5,
Total: 120 * time.Second,
Delay: 5 * time.Second,
}

Min: 5,
Total: 120 * time.Second,
Delay: 5 * time.Second,
}

func TestECSInstance(t *testing.T) {

Expand All @@ -35,7 +34,7 @@ func TestECSInstance(t *testing.T) {
t.Errorf("Failed to start instance %s: %v", TestInstanceId, err)
}
status, err = client.WaitForInstance(TestInstanceId, defaultInstanceStrategy)
if err != nil || status != Running {
if err != nil || status != Running {
t.Errorf("Instance %s is failed to start: %v", TestInstanceId, err)
}
t.Logf("Instance %s is running successfully.", TestInstanceId)
Expand Down Expand Up @@ -105,7 +104,7 @@ func TestECSInstanceCreationAndDeletion(t *testing.T) {
}
status, err = client.WaitForInstance(instanceId, defaultInstanceStrategy)

if err != nil || status != Stopped {
if err != nil || status != Stopped {
t.Errorf("Instance %s is failed to stop: %v", instanceId, err)
}
t.Logf("Instance %s is stopped successfully.", instanceId)
Expand Down
42 changes: 23 additions & 19 deletions ecs/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package ecs

import (
"github.com/denverdino/aliyungo/util"
"time"
)

type AllocatePublicIpAddressArgs struct {
Expand Down Expand Up @@ -102,6 +101,11 @@ const (
EipStatusAvailable = EipStatus("Available")
)

var FinalNetworkStatus = map[EipStatus]bool{
EipStatusInUse: true,
EipStatusAvailable: true,
}

type DescribeEipAddressesArgs struct {
RegionId Region
Status EipStatus //enum Associating | Unassociating | InUse | Available
Expand Down Expand Up @@ -200,27 +204,27 @@ func (client *Client) ReleaseEipAddress(allocationId string) error {
}

// WaitForVSwitchAvailable waits for VSwitch to given status
func (client *Client) WaitForEip(regionId Region, allocationId string, status EipStatus, timeout int) error {
if timeout <= 0 {
timeout = DefaultTimeout
}
args := DescribeEipAddressesArgs{
RegionId: regionId,
AllocationId: allocationId,
}
for {
func (client *Client) WaitForEip(regionId Region, allocationId string, strategy util.AttemptStrategy) (status interface{}, err error) {

fn := func() (bool, interface{}, error) {

args := DescribeEipAddressesArgs{
RegionId: regionId,
AllocationId: allocationId,
}

vpcs, _, err := client.DescribeEipAddresses(&args)
if err != nil {
return err
}
if vpcs[0].Status == status {
break
return false, util.StatusNotAvailable, err
}
timeout = timeout - DefaultWaitForInterval
if timeout <= 0 {
return getECSErrorFromString("Timeout")

if FinalNetworkStatus[vpcs[0].Status] {
return true, vpcs[0].Status, nil
}
time.Sleep(DefaultWaitForInterval * time.Second)
return false, util.StatusNotAvailable, nil
}
return nil

status, e1 := util.LoopCall(strategy, fn)

return status, e1
}
12 changes: 6 additions & 6 deletions ecs/networks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,25 @@ func testEipAddress(t *testing.T, client *Client, regionId Region, instanceId st
}
t.Logf("EIP address: %s, AllocationId: %s", ipAddr, allocationId)

err = client.WaitForEip(regionId, allocationId, EipStatusAvailable, 0)
if err != nil {
status, err := client.WaitForEip(regionId, allocationId, DefaultStrategy)
if err != nil || status != EipStatusAvailable {
t.Errorf("Failed to wait EIP %s: %v", allocationId, err)
}

err = client.AssociateEipAddress(allocationId, instanceId)
if err != nil {
t.Errorf("Failed to associate EIP address: %v", err)
}
err = client.WaitForEip(regionId, allocationId, EipStatusInUse, 0)
if err != nil {
status, err = client.WaitForEip(regionId, allocationId, DefaultStrategy)
if err != nil || status != EipStatusInUse {
t.Errorf("Failed to wait EIP %s: %v", allocationId, err)
}
err = client.UnassociateEipAddress(allocationId, instanceId)
if err != nil {
t.Errorf("Failed to unassociate EIP address: %v", err)
}
err = client.WaitForEip(regionId, allocationId, EipStatusAvailable, 0)
if err != nil {
status, err = client.WaitForEip(regionId, allocationId, DefaultStrategy)
if err != nil || status != EipStatusAvailable {
t.Errorf("Failed to wait EIP %s: %v", allocationId, err)
}
err = client.ReleaseEipAddress(allocationId)
Expand Down
35 changes: 17 additions & 18 deletions ecs/route_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ecs

import (
"github.com/denverdino/aliyungo/util"
"time"
)

type DescribeRouteTablesArgs struct {
Expand Down Expand Up @@ -108,21 +107,21 @@ func (client *Client) DeleteRouteEntry(args *DeleteRouteEntryArgs) error {
}

// WaitForAllRouteEntriesAvailable waits for all route entries to Available status
func (client *Client) WaitForAllRouteEntriesAvailable(vrouterId string, routeTableId string, timeout int) error {
if timeout <= 0 {
timeout = DefaultTimeout
}
args := DescribeRouteTablesArgs{
VRouterId: vrouterId,
RouteTableId: routeTableId,
}
for {
func (client *Client) WaitForAllRouteEntriesAvailable(vrouterId string, routeTableId string, strategy util.AttemptStrategy) (status interface{}, err error) {

fn := func() (bool, interface{}, error) {

args := DescribeRouteTablesArgs{
VRouterId: vrouterId,
RouteTableId: routeTableId,
}

routeTables, _, err := client.DescribeRouteTables(&args)

if err != nil {
return err
return false, util.StatusNotAvailable, err
}

sucess := true

loop:
Expand All @@ -135,13 +134,13 @@ func (client *Client) WaitForAllRouteEntriesAvailable(vrouterId string, routeTab
}
}
if sucess {
break
}
timeout = timeout - DefaultWaitForInterval
if timeout <= 0 {
return getECSErrorFromString("Timeout")
return true, util.StatusNotAvailable, nil
}
time.Sleep(DefaultWaitForInterval * time.Second)

return false, util.StatusNotAvailable, nil
}
return nil

status, e1 := util.LoopCall(strategy, fn)

return status, e1
}
2 changes: 1 addition & 1 deletion ecs/route_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func testRouteTable(t *testing.T, client *Client, regionId Region, vpcId string,
t.Logf("RouteTables of VRouter %s: %++v", vrouterId, routeTables)
}

err = client.WaitForAllRouteEntriesAvailable(vrouterId, routeTableId, 60)
_, err = client.WaitForAllRouteEntriesAvailable(vrouterId, routeTableId, DefaultStrategy)
if err != nil {
t.Errorf("Failed to wait route entries: %v", err)
}
Expand Down
Loading