Skip to content

Commit

Permalink
pathdb: Change GetNextQuery to include src and policy (#2956)
Browse files Browse the repository at this point in the history
For #2454 we need to include src in the NextQuery table.
For policies in path lookups we need policy.

Note that existing deployments need to update the database, this is best done by nuking the NextQuery table and create it fresh.

Migration instructions:
Upgrade sqlite DB:
`sqlite3 gen-cache/sd1-ff00_0_111.path.db < go/lib/pathdb/sqlite/migrate_7_8.up.sql`

Contributes #2454
  • Loading branch information
lukedirtwalker authored Aug 6, 2019
1 parent e31ecef commit f8c2754
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 90 deletions.
9 changes: 8 additions & 1 deletion go/lib/pathdb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"helpers.go",
"metrics.go",
"pathdb.go",
],
importpath = "github.com/scionproto/scion/go/lib/pathdb",
visibility = ["//visibility:public"],
deps = [
"//go/lib/addr:go_default_library",
"//go/lib/common:go_default_library",
"//go/lib/ctrl/seg:go_default_library",
"//go/lib/infra/modules/cleaner:go_default_library",
"//go/lib/infra/modules/db:go_default_library",
"//go/lib/pathdb/query:go_default_library",
"//go/lib/pathpol:go_default_library",
"//go/lib/prom:go_default_library",
"@com_github_opentracing_opentracing_go//:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
Expand All @@ -22,12 +25,16 @@ go_library(

go_test(
name = "go_default_test",
srcs = ["metrics_test.go"],
srcs = [
"helpers_test.go",
"metrics_test.go",
],
embed = [":go_default_library"],
deps = [
"//go/lib/pathdb/pathdbtest:go_default_library",
"//go/lib/pathdb/sqlite:go_default_library",
"//go/lib/xtest:go_default_library",
"@com_github_smartystreets_goconvey//convey:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
],
)
49 changes: 49 additions & 0 deletions go/lib/pathdb/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2019 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 pathdb

import (
"crypto/sha256"
"encoding/json"

"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/pathpol"
)

// PolicyHashFunc is a function that hashes a policy.
type PolicyHashFunc func(policy *pathpol.Policy) (PolicyHash, error)

// PolicyHash is the hash of a policy.
type PolicyHash []byte

// HashPolicy is a default implementation of a policy hash function. It creates
// a sha256 hash of the json serialized policy.
func HashPolicy(policy *pathpol.Policy) (PolicyHash, error) {
pol := policy
if pol == nil {
pol = &pathpol.Policy{}
}
jsonPol, err := json.Marshal(pol)
if err != nil {
return nil, err
}
h := sha256.New()
h.Write(jsonPol)
return h.Sum(nil), nil
}

func (h PolicyHash) String() string {
return common.RawBytes(h).String()
}
28 changes: 28 additions & 0 deletions go/lib/pathdb/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2019 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 pathdb

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestHashing(t *testing.T) {
h, err := HashPolicy(nil)
if assert.NoError(t, err) {
assert.NotNil(t, h)
}
}
13 changes: 8 additions & 5 deletions go/lib/pathdb/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/scionproto/scion/go/lib/ctrl/seg"
"github.com/scionproto/scion/go/lib/infra/modules/db"
"github.com/scionproto/scion/go/lib/pathdb/query"
"github.com/scionproto/scion/go/lib/pathpol"
"github.com/scionproto/scion/go/lib/prom"
)

Expand Down Expand Up @@ -241,22 +242,24 @@ func (db *metricsExecutor) GetAll(ctx context.Context) (<-chan query.ResultOrErr
}

func (db *metricsExecutor) InsertNextQuery(ctx context.Context,
dst addr.IA, nextQuery time.Time) (bool, error) {
src, dst addr.IA, policy *pathpol.Policy, nextQuery time.Time) (bool, error) {

var ok bool
var err error
db.metrics.Observe(ctx, promOpInsertNextQuery, func(ctx context.Context) error {
ok, err = db.pathDB.InsertNextQuery(ctx, dst, nextQuery)
ok, err = db.pathDB.InsertNextQuery(ctx, src, dst, policy, nextQuery)
return err
})
return ok, err
}

func (db *metricsExecutor) GetNextQuery(ctx context.Context, dst addr.IA) (*time.Time, error) {
var t *time.Time
func (db *metricsExecutor) GetNextQuery(ctx context.Context, src, dst addr.IA,
policy *pathpol.Policy) (time.Time, error) {

var t time.Time
var err error
db.metrics.Observe(ctx, promOpGetNextQuery, func(ctx context.Context) error {
t, err = db.pathDB.GetNextQuery(ctx, dst)
t, err = db.pathDB.GetNextQuery(ctx, src, dst, policy)
return err
})
return t, err
Expand Down
1 change: 1 addition & 0 deletions go/lib/pathdb/mock_pathdb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go_library(
"//go/lib/ctrl/seg:go_default_library",
"//go/lib/pathdb:go_default_library",
"//go/lib/pathdb/query:go_default_library",
"//go/lib/pathpol:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
],
)
55 changes: 28 additions & 27 deletions go/lib/pathdb/mock_pathdb/pathdb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions go/lib/pathdb/pathdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/scionproto/scion/go/lib/infra/modules/cleaner"
"github.com/scionproto/scion/go/lib/infra/modules/db"
"github.com/scionproto/scion/go/lib/pathdb/query"
"github.com/scionproto/scion/go/lib/pathpol"
)

// Read defines all read operations of the path DB.
Expand All @@ -40,9 +41,9 @@ type Read interface {
// channel, which means the channel must be fully drained to guarantee the destruction of the
// goroutine.
GetAll(context.Context) (<-chan query.ResultOrErr, error)
// GetNextQuery returns the nextQuery timestamp for the given dst,
// or nil if it hasn't been queried.
GetNextQuery(ctx context.Context, dst addr.IA) (*time.Time, error)
// GetNextQuery returns the nextQuery timestamp for the given src-dst pair
// and policy , or a zero time if it hasn't been queried.
GetNextQuery(ctx context.Context, src, dst addr.IA, policy *pathpol.Policy) (time.Time, error)
}

// Write defines all write operations of the path DB.
Expand All @@ -60,9 +61,10 @@ type Write interface {
// Returns the number of deleted segments.
DeleteExpired(ctx context.Context, now time.Time) (int, error)
// InsertNextQuery inserts or updates the timestamp nextQuery for the given
// dst. Returns true if an insert/update happened or false if the stored
// timestamp is already newer.
InsertNextQuery(ctx context.Context, dst addr.IA, nextQuery time.Time) (bool, error)
// src-dst pair and policy. Returns true if an insert/update happened or
// false if the stored timestamp is already newer.
InsertNextQuery(ctx context.Context, src, dst addr.IA, policy *pathpol.Policy,
nextQuery time.Time) (bool, error)
}

// ReadWrite defines all read an write operations of the path DB.
Expand Down
1 change: 1 addition & 0 deletions go/lib/pathdb/pathdbtest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
"//go/lib/ctrl/seg/mock_seg:go_default_library",
"//go/lib/pathdb:go_default_library",
"//go/lib/pathdb/query:go_default_library",
"//go/lib/pathpol:go_default_library",
"//go/lib/spath:go_default_library",
"//go/lib/xtest:go_default_library",
"//go/proto:go_default_library",
Expand Down
Loading

0 comments on commit f8c2754

Please sign in to comment.