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

COLIBRI backend continuation #3787

Merged
merged 38 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
bc8aa7d
refactor, move private function to bottom
juagargi Jun 12, 2020
aaf3728
fix schema, typo
juagargi Jun 12, 2020
f8f45c3
add method to segment.Reservation.
juagargi Jun 12, 2020
cfa8ad7
Token.ToRaw for convenience
juagargi Jun 12, 2020
54a9b34
Indices.Sort to put the indices in consecutive order
juagargi Jun 15, 2020
d7f8f4d
modify DB interface. Add active_index to schema.
juagargi Jun 15, 2020
238bc24
add and adapt unit tests
juagargi Jun 15, 2020
22e7c21
changes in Reservation.NewIndex.
juagargi Jun 15, 2020
582c1e0
more db functions
juagargi Jun 16, 2020
626ec76
fix segment reservation schema.
juagargi Jun 16, 2020
cca2d65
segment.Path.String() to use it in log statements
juagargi Jun 17, 2020
652effe
change field names to src_ia,dst_ia. List seg. rsvs
juagargi Jun 17, 2020
6b1c5fe
rename fcn in backend interface
juagargi Jun 17, 2020
e619240
refactor test, add test,fix get from src/dst
juagargi Jun 17, 2020
28aefe6
NewSegmentRsvWithID to DB interface
juagargi Jun 17, 2020
2da4894
test NewSegmentRsvWithID
juagargi Jun 17, 2020
f103c57
adding more functions to sqlite
juagargi Jun 18, 2020
c6d8459
normalize after rebase
juagargi Jun 18, 2020
753058a
fix bug, more tests
juagargi Jun 18, 2020
7529e6c
signature in DB interface. Fix bug
juagargi Jun 19, 2020
35ffc63
refactor test
juagargi Jun 19, 2020
d987f17
change get reservations from ingress/egress, add test
juagargi Jun 19, 2020
0343083
schema: path is unique
juagargi Jun 19, 2020
b617f96
happy linter
juagargi Jun 19, 2020
135ec26
fix update index, add test
juagargi Jun 19, 2020
f2ab5fb
rename function in DB interface
juagargi Jun 19, 2020
262b51f
fix update index in db
juagargi Jun 19, 2020
222a9ac
add a new PersistSegmentRsv function
juagargi Jun 23, 2020
29066fa
remove superfluous db functions.
juagargi Jun 23, 2020
4b4bfd1
extend test
juagargi Jun 23, 2020
1e3f636
simplify placeholders to ?
juagargi Jun 23, 2020
ed8e377
insert all indices in one trip
juagargi Jun 23, 2020
08538ef
refactor / cleanup
juagargi Jun 23, 2020
f0651c5
DeleteSegmentRsv
juagargi Jun 23, 2020
717538d
fix Indices.Sort; add test
juagargi Jun 24, 2020
6a6e1c0
add comments.
juagargi Jun 24, 2020
7864977
Token modifications.
juagargi Jun 25, 2020
04dc1e2
complete Token modifications
juagargi Jun 25, 2020
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
12 changes: 11 additions & 1 deletion go/cs/reservation/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
Expand All @@ -14,3 +14,13 @@ go_library(
"//go/lib/spath:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["index_test.go"],
embed = [":go_default_library"],
deps = [
"//go/lib/colibri/reservation:go_default_library",
"@com_github_stretchr_testify//require:go_default_library",
],
)
4 changes: 3 additions & 1 deletion go/cs/reservation/e2e/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Index struct {
Idx reservation.IndexNumber
Expiration time.Time
AllocBW reservation.BWCls // also present in the token
Token reservation.Token
Token *reservation.Token
}

type Indices []Index
Expand All @@ -37,3 +37,5 @@ var _ base.IndicesInterface = (*Indices)(nil)
func (idxs Indices) Len() int { return len(idxs) }
func (idxs Indices) GetIndexNumber(i int) reservation.IndexNumber { return idxs[i].Idx }
func (idxs Indices) GetExpiration(i int) time.Time { return idxs[i].Expiration }
func (idxs Indices) GetAllocBW(i int) reservation.BWCls { return idxs[i].AllocBW }
func (idxs Indices) GetToken(i int) *reservation.Token { return idxs[i].Token }
17 changes: 17 additions & 0 deletions go/cs/reservation/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type IndicesInterface interface {
Len() int
GetIndexNumber(i int) reservation.IndexNumber
GetExpiration(i int) time.Time
GetAllocBW(i int) reservation.BWCls
GetToken(i int) *reservation.Token
}

// ValidateIndices checks that the indices follow consecutive index numbers, their expiration
Expand Down Expand Up @@ -62,6 +64,21 @@ func ValidateIndices(indices IndicesInterface) error {
}
lastExpiration = indices.GetExpiration(i)
lastIndexNumber = indices.GetIndexNumber(i)
token := indices.GetToken(i)
if token != nil {
if token.Idx != lastIndexNumber {
return serrors.New("inconsistent token", "token_index_number", token.Idx,
"expected", lastIndexNumber)
}
if token.ExpirationTick != reservation.TickFromTime(lastExpiration) {
return serrors.New("inconsistent token", "token_expiration_tick",
token.ExpirationTick, "expected", reservation.TickFromTime(lastExpiration))
}
if token.BWCls != indices.GetAllocBW(i) {
return serrors.New("inconsistent token", "token_bw_class", token.BWCls,
"expected", indices.GetAllocBW(i))
}
}
}
return nil
}
Expand Down
150 changes: 150 additions & 0 deletions go/cs/reservation/index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright 2020 ETH Zurich, Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package reservation

import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/scionproto/scion/go/lib/colibri/reservation"
)

func TestValidateIndices(t *testing.T) {
idxs := make(Indices, 0)
// up to 3 indices per expiration time
expTime := time.Unix(1, 0)
idx, err := idxs.NewIndex(expTime)
require.NoError(t, err)
require.Equal(t, reservation.IndexNumber(0), idx)
idx, err = idxs.NewIndex(expTime)
require.NoError(t, err)
require.Equal(t, reservation.IndexNumber(1), idx)
idx, err = idxs.NewIndex(expTime)
require.NoError(t, err)
require.Equal(t, reservation.IndexNumber(2), idx)
idxs = idxs[:1]
// exp time is less
_, err = idxs.NewIndex(expTime.Add(-1 * time.Millisecond))
require.Error(t, err)
require.Len(t, idxs, 1)
// exp time is same
idx, err = idxs.NewIndex(expTime)
require.NoError(t, err)
require.Len(t, idxs, 2)
require.True(t, idxs[1].Idx == idx)
require.True(t, idxs[1].Expiration == expTime)
require.Equal(t, reservation.IndexNumber(1), idx)
idx, err = idxs.NewIndex(expTime)
require.NoError(t, err)
require.Len(t, idxs, 3)
require.True(t, idxs[2].Idx == idx)
require.True(t, idxs[2].Expiration == expTime)
require.Equal(t, reservation.IndexNumber(2), idx)
// too many indices for the same exp time
_, err = idxs.NewIndex(expTime)
require.Error(t, err)
require.Len(t, idxs, 3)
// exp time is greater
expTime = expTime.Add(time.Second)
idx, err = idxs.NewIndex(expTime)
require.NoError(t, err)
require.Len(t, idxs, 4)
require.True(t, idxs[3].Idx == idx)
require.True(t, idxs[3].Expiration == expTime)
require.Equal(t, reservation.IndexNumber(3), idx)
// index number rollover
idxs = Indices{}
idxs.NewIndex(expTime)
require.Len(t, idxs, 1)
idxs[0].Idx = idxs[0].Idx.Sub(1)
idx, err = idxs.NewIndex(expTime)
require.NoError(t, err)
require.True(t, idxs[1].Idx == idx)
require.True(t, idxs[1].Expiration == expTime)
require.Equal(t, reservation.IndexNumber(0), idx)
// more than 16 indices
idxs = Indices{}
for i := 0; i < 16; i++ {
expTime := time.Unix(int64(i), 0)
_, err = idxs.NewIndex(expTime)
require.NoError(t, err)
}
require.Len(t, idxs, 16)
_, err = idxs.NewIndex(expTime.Add(time.Hour))
require.Error(t, err)
// exp time is before
idxs = Indices{}
expTime = time.Unix(1, 0)
idxs.NewIndex(expTime)
idxs.NewIndex(expTime)
idxs[1].Expiration = expTime.Add(-1 * time.Second)
err = ValidateIndices(idxs)
require.Error(t, err)
// non consecutive indices
idxs = Indices{}
expTime = time.Unix(1, 0)
idxs.NewIndex(expTime)
idxs.NewIndex(expTime)
idxs[1].Idx = 2
err = ValidateIndices(idxs)
require.Error(t, err)
// more than three indices per exp time
idxs = Indices{}
idxs.NewIndex(expTime)
idxs.NewIndex(expTime)
idxs.NewIndex(expTime)
require.Len(t, idxs, 3)
err = ValidateIndices(idxs)
require.NoError(t, err)
_, err = idxs.NewIndex(expTime)
require.Error(t, err)
}

type Index struct {
Idx reservation.IndexNumber
Expiration time.Time
}

type Indices []Index

var _ IndicesInterface = (*Indices)(nil)

func (idxs Indices) Len() int { return len(idxs) }
func (idxs Indices) GetIndexNumber(i int) reservation.IndexNumber { return idxs[i].Idx }
func (idxs Indices) GetExpiration(i int) time.Time { return idxs[i].Expiration }
func (idxs Indices) GetAllocBW(i int) reservation.BWCls { return reservation.BWCls(0) }
func (idxs Indices) GetToken(i int) *reservation.Token { return nil }

func (idxs *Indices) NewIndex(expTime time.Time) (reservation.IndexNumber, error) {
idx := reservation.IndexNumber(0)
if len(*idxs) > 0 {
idx = (*idxs)[len(*idxs)-1].Idx.Add(1)
}
index := Index{
Idx: idx,
Expiration: expTime,
}
newIndices := make(Indices, len(*idxs)+1)
copy(newIndices, *idxs)
newIndices[len(newIndices)-1] = index
err := ValidateIndices(newIndices)
if err != nil {
return reservation.IndexNumber(0), err
}
*idxs = newIndices
return idx, nil
}
3 changes: 3 additions & 0 deletions go/cs/reservation/reservationdbtest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ go_library(
"//go/cs/reservation/segment:go_default_library",
"//go/cs/reservation/segmenttest:go_default_library",
"//go/cs/reservationstorage/backend:go_default_library",
"//go/lib/addr:go_default_library",
"//go/lib/colibri/reservation:go_default_library",
"//go/lib/common:go_default_library",
"//go/lib/xtest:go_default_library",
"@com_github_stretchr_testify//require:go_default_library",
],
Expand Down
Loading