Prechádzať zdrojové kódy

bug fix: LeaseAttrs lost on lease-renew

Eugene Yakubovich 10 rokov pred
rodič
commit
f55b707d9b
2 zmenil súbory, kde vykonal 22 pridanie a 11 odobranie
  1. 12 10
      subnet/subnet.go
  2. 10 1
      subnet/subnet_test.go

+ 12 - 10
subnet/subnet.go

@@ -106,21 +106,28 @@ func findLeaseByIP(leases []SubnetLease, pubIP ip.IP4) *SubnetLease {
 	return nil
 }
 
-func (sm *SubnetManager) tryAcquireLease(extIP ip.IP4, attrs []byte) (ip.IP4Net, error) {
+func (sm *SubnetManager) tryAcquireLease(extIP ip.IP4, attrs *LeaseAttrs) (ip.IP4Net, error) {
 	var err error
 	sm.leases, err = sm.getLeases()
 	if err != nil {
 		return ip.IP4Net{}, err
 	}
 
+	attrBytes, err := json.Marshal(attrs)
+	if err != nil {
+		log.Errorf("marshal failed: %#v, %v", attrs, err)
+		return ip.IP4Net{}, err
+	}
+
 	// try to reuse a subnet if there's one that matches our IP
 	if l := findLeaseByIP(sm.leases, extIP); l != nil {
-		resp, err := sm.registry.updateSubnet(l.Network.StringSep(".", "-"), string(attrs), subnetTTL)
+		resp, err := sm.registry.updateSubnet(l.Network.StringSep(".", "-"), string(attrBytes), subnetTTL)
 		if err != nil {
 			return ip.IP4Net{}, err
 		}
 
 		sm.myLease.Network = l.Network
+		sm.myLease.Attrs = *attrs
 		sm.leaseExp = *resp.Node.Expiration
 		return l.Network, nil
 	}
@@ -131,10 +138,11 @@ func (sm *SubnetManager) tryAcquireLease(extIP ip.IP4, attrs []byte) (ip.IP4Net,
 		return ip.IP4Net{}, err
 	}
 
-	resp, err := sm.registry.createSubnet(sn.StringSep(".", "-"), string(attrs), subnetTTL)
+	resp, err := sm.registry.createSubnet(sn.StringSep(".", "-"), string(attrBytes), subnetTTL)
 	switch {
 	case err == nil:
 		sm.myLease.Network = sn
+		sm.myLease.Attrs = *attrs
 		sm.leaseExp = *resp.Node.Expiration
 		return sn, nil
 
@@ -148,14 +156,8 @@ func (sm *SubnetManager) tryAcquireLease(extIP ip.IP4, attrs []byte) (ip.IP4Net,
 }
 
 func (sm *SubnetManager) acquireLeaseOnce(attrs *LeaseAttrs, cancel chan bool) (ip.IP4Net, error) {
-	attrBytes, err := json.Marshal(attrs)
-	if err != nil {
-		log.Errorf("marshal failed: %#v, %v", attrs, err)
-		return ip.IP4Net{}, err
-	}
-
 	for i := 0; i < registerRetries; i++ {
-		sn, err := sm.tryAcquireLease(attrs.PublicIP, attrBytes)
+		sn, err := sm.tryAcquireLease(attrs.PublicIP, attrs)
 		switch {
 		case err != nil:
 			return ip.IP4Net{}, err

+ 10 - 1
subnet/subnet_test.go

@@ -1,6 +1,7 @@
 package subnet
 
 import (
+	"encoding/json"
 	"fmt"
 	"testing"
 	"time"
@@ -280,7 +281,15 @@ func TestRenewLease(t *testing.T) {
 	for _, n := range msr.subnets.Nodes {
 		if n.Key == sn.StringSep(".", "-") {
 			if n.Expiration.Before(time.Now()) {
-				t.Fatalf("Failed to renew lease")
+				t.Error("Failed to renew lease: expiration did not advance")
+			}
+			a := LeaseAttrs{}
+			if err := json.Unmarshal([]byte(n.Value), &a); err != nil {
+				t.Errorf("Failed to JSON-decode LeaseAttrs: %v", err)
+				return
+			}
+			if a.PublicIP != attrs.PublicIP {
+				t.Errorf("LeaseAttrs changed: was %v, now %v", attrs.PublicIP, a.PublicIP)
 			}
 			return
 		}