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

pathdb: Change GetNextQuery to include src and policy #2956

Merged
merged 1 commit into from
Aug 6, 2019
Merged
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
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