Skip to content

Commit

Permalink
Remove AddRule of routing.Table
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Sep 10, 2019
1 parent 05541f8 commit 21b3220
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 47 deletions.
4 changes: 1 addition & 3 deletions pkg/router/route_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"net"

"github.com/google/uuid"
"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/skycoin/src/util/logging"

Expand Down Expand Up @@ -284,8 +283,7 @@ func (rm *routeManager) occupyRouteID(data []byte) ([]routing.RouteID, error) {

var ids = make([]routing.RouteID, n)
for i := range ids {
rule := routing.IntermediaryForwardRule(DefaultRouteKeepAlive, 0, 0, uuid.UUID{})
routeID, err := rm.rt.AddRule(rule)
routeID, err := rm.rt.ReserveKey()
if err != nil {
return nil, err
}
Expand Down
19 changes: 15 additions & 4 deletions pkg/router/route_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ func TestNewRouteManager(t *testing.T) {
defer clearRules()

expiredRule := routing.IntermediaryForwardRule(-10*time.Minute, 1, 3, uuid.New())
expiredID, err := rm.rt.AddRule(expiredRule)
expiredID, err := rm.rt.ReserveKey()
require.NoError(t, err)
err = rm.rt.SaveRule(expiredID, expiredRule)
require.NoError(t, err)

rule := routing.IntermediaryForwardRule(10*time.Minute, 2, 3, uuid.New())
id, err := rm.rt.AddRule(rule)
id, err := rm.rt.ReserveKey()
require.NoError(t, err)
err = rm.rt.SaveRule(id, rule)
require.NoError(t, err)

// rule should already be expired at this point due to the execution time.
Expand All @@ -71,7 +75,10 @@ func TestNewRouteManager(t *testing.T) {

pk, _ := cipher.GenerateKeyPair()
rule := routing.ConsumeRule(10*time.Minute, 1, pk, 2, 3)
_, err := rt.AddRule(rule)

id, err := rm.rt.ReserveKey()
require.NoError(t, err)
err = rm.rt.SaveRule(id, rule)
require.NoError(t, err)

loop := routing.Loop{Local: routing.Addr{Port: 3}, Remote: routing.Addr{PubKey: pk, Port: 3}}
Expand Down Expand Up @@ -156,8 +163,12 @@ func TestNewRouteManager(t *testing.T) {
proto := setup.NewSetupProtocol(in)

rule := routing.IntermediaryForwardRule(10*time.Minute, 1, 3, uuid.New())
id, err := rt.AddRule(rule)

id, err := rm.rt.ReserveKey()
require.NoError(t, err)
err = rm.rt.SaveRule(id, rule)
require.NoError(t, err)

assert.Equal(t, 1, rt.Count())

require.NoError(t, setup.DeleteRule(context.TODO(), proto, id))
Expand Down
6 changes: 5 additions & 1 deletion pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ func TestRouter_Serve(t *testing.T) {

// Add a FWD rule for r0.
fwdRule := routing.IntermediaryForwardRule(1*time.Hour, routing.RouteID(0), routing.RouteID(5), tp1.Entry.ID)
fwdRtID, err := r0.rm.rt.AddRule(fwdRule)
fwdRtID, err := r0.rm.rt.ReserveKey()
require.NoError(t, err)
err = r0.rm.rt.SaveRule(fwdRtID, fwdRule)
require.NoError(t, err)

require.NoError(t, err)

// Call handlePacket for r0 (this should in turn, use the rule we added).
Expand Down
29 changes: 3 additions & 26 deletions pkg/routing/routing_table.go → pkg/routing/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ import (
"math"
"sync"
"time"

"github.com/skycoin/skycoin/src/util/logging"
)

var log = logging.MustGetLogger("routing")

// DefaultGCInterval is the default duration for garbage collection of routing rules.
const DefaultGCInterval = 5 * time.Second

Expand All @@ -33,9 +29,6 @@ type Table interface {
// SaveRule sets RoutingRule for a given RouteID.
SaveRule(RouteID, Rule) error

// AddRule adds a new RoutingRules to the table and returns assigned RouteID.
AddRule(Rule) (RouteID, error)

// Rule returns RoutingRule with a given RouteID.
Rule(RouteID) (Rule, error)

Expand All @@ -58,8 +51,7 @@ type Table interface {
type memTable struct {
sync.RWMutex

config Config

config Config
nextID RouteID
rules map[RouteID]Rule
activity map[RouteID]time.Time
Expand Down Expand Up @@ -111,27 +103,12 @@ func (mt *memTable) ReserveKey() (RouteID, error) {
return mt.nextID, nil
}

func (mt *memTable) AddRule(rule Rule) (RouteID, error) {
routeID, err := mt.ReserveKey()
if err != nil {
return 0, err
}

if err := mt.SaveRule(routeID, rule); err != nil {
return 0, err
}

func (mt *memTable) SaveRule(routeID RouteID, rule Rule) error {
mt.Lock()
defer mt.Unlock()
mt.activity[routeID] = time.Now()

return mt.nextID, nil
}

func (mt *memTable) SaveRule(routeID RouteID, rule Rule) error {
mt.Lock()
mt.rules[routeID] = rule
mt.Unlock()
mt.activity[routeID] = time.Now()

return nil
}
Expand Down
27 changes: 20 additions & 7 deletions pkg/routing/routing_table_test.go → pkg/routing/table_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package routing

import (
"log"
"os"
"testing"
"time"
Expand Down Expand Up @@ -30,7 +31,10 @@ func RoutingTableSuite(t *testing.T, tbl Table) {
t.Helper()

rule := IntermediaryForwardRule(15*time.Minute, 1, 2, uuid.New())
id, err := tbl.AddRule(rule)

id, err := tbl.ReserveKey()
require.NoError(t, err)
err = tbl.SaveRule(id, rule)
require.NoError(t, err)

assert.Equal(t, 1, tbl.Count())
Expand All @@ -40,7 +44,10 @@ func RoutingTableSuite(t *testing.T, tbl Table) {
assert.Equal(t, rule, r)

rule2 := IntermediaryForwardRule(15*time.Minute, 2, 3, uuid.New())
id2, err := tbl.AddRule(rule2)

id2, err := tbl.ReserveKey()
require.NoError(t, err)
err = tbl.SaveRule(id2, rule2)
require.NoError(t, err)

assert.Equal(t, 2, tbl.Count())
Expand Down Expand Up @@ -72,13 +79,19 @@ func TestRoutingTableCleanup(t *testing.T) {
config: Config{GCInterval: DefaultGCInterval},
}

_, err := rt.AddRule(IntermediaryForwardRule(1*time.Hour, 1, 3, uuid.New()))
id0, err := rt.ReserveKey()
require.NoError(t, err)
err = rt.SaveRule(id0, IntermediaryForwardRule(1*time.Hour, 1, 3, uuid.New()))
require.NoError(t, err)

id, err := rt.AddRule(IntermediaryForwardRule(1*time.Hour, 2, 3, uuid.New()))
id1, err := rt.ReserveKey()
require.NoError(t, err)
err = rt.SaveRule(id1, IntermediaryForwardRule(1*time.Hour, 2, 3, uuid.New()))
require.NoError(t, err)

id2, err := rt.AddRule(IntermediaryForwardRule(-1*time.Hour, 3, 3, uuid.New()))
id2, err := rt.ReserveKey()
require.NoError(t, err)
err = rt.SaveRule(id2, IntermediaryForwardRule(-1*time.Hour, 3, 3, uuid.New()))
require.NoError(t, err)

// rule should already be expired at this point due to the execution time.
Expand All @@ -87,10 +100,10 @@ func TestRoutingTableCleanup(t *testing.T) {

assert.Equal(t, 3, rt.Count())

_, err = rt.Rule(id)
_, err = rt.Rule(id1)
require.NoError(t, err)

assert.NotNil(t, rt.activity[id])
assert.NotNil(t, rt.activity[id1])

rt.gc()
assert.Equal(t, 2, rt.Count())
Expand Down
10 changes: 7 additions & 3 deletions pkg/visor/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,13 @@ func (r *RPC) RoutingRule(key *routing.RouteID, rule *routing.Rule) error {

// AddRoutingRule adds a RoutingRule and returns a Key in which the rule is stored under.
func (r *RPC) AddRoutingRule(rule *routing.Rule, routeID *routing.RouteID) error {
var err error
*routeID, err = r.node.rt.AddRule(*rule)
return err
key, err := r.node.rt.ReserveKey()
if err != nil {
return err
}

*routeID = key
return r.node.rt.SaveRule(key, *rule)
}

// SetRoutingRule sets a routing rule.
Expand Down
11 changes: 8 additions & 3 deletions pkg/visor/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,15 @@ func NewMockRPCClient(r *rand.Rand, maxTps int, maxRules int) (cipher.PubKey, RP
}
lp := routing.Port(binary.BigEndian.Uint16(lpRaw[:]))
rp := routing.Port(binary.BigEndian.Uint16(rpRaw[:]))
fwdRID, err := rt.AddRule(nil)
fwdRID, err := rt.ReserveKey()
if err != nil {
panic(err)
}
fwdRule := routing.IntermediaryForwardRule(ruleKeepAlive, fwdRID, routing.RouteID(r.Uint32()), uuid.New())
if err := rt.SaveRule(fwdRID, fwdRule); err != nil {
panic(err)
}
appRID, err := rt.AddRule(nil)
appRID, err := rt.ReserveKey()
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -486,7 +486,12 @@ func (mc *mockRPCClient) RoutingRule(key routing.RouteID) (routing.Rule, error)

// AddRoutingRule implements RPCClient.
func (mc *mockRPCClient) AddRoutingRule(rule routing.Rule) (routing.RouteID, error) {
return mc.rt.AddRule(rule)
key, err := mc.rt.ReserveKey()
if err != nil {
return 0, err
}

return key, mc.rt.SaveRule(key, rule)
}

// SetRoutingRule implements RPCClient.
Expand Down

0 comments on commit 21b3220

Please sign in to comment.