forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
116 lines (103 loc) · 3.46 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// 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 seghandler
import (
"context"
"fmt"
"sort"
"github.com/scionproto/scion/go/lib/ctrl/path_mgmt"
"github.com/scionproto/scion/go/lib/ctrl/seg"
"github.com/scionproto/scion/go/lib/hiddenpath"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/pathdb"
"github.com/scionproto/scion/go/lib/revcache"
)
// SegWithHP is a segment with hidden path cfg ids.
type SegWithHP struct {
Seg *seg.Meta
HPGroup hiddenpath.GroupId
}
func (s *SegWithHP) String() string {
return fmt.Sprintf("{Seg: %v, HPGroup: %s}", s.Seg, s.HPGroup)
}
// SegStats provides statistics about segment insertion/updates.
type SegStats struct {
// InsertedSegs are the log IDs of the inserted segments.
InsertedSegs []string
// UpdatedSegs are the log IDs of the updated segments.
UpdatedSegs []string
}
// Total returns the total amount of updates and inserts.
func (s SegStats) Total() int {
return len(s.InsertedSegs) + len(s.UpdatedSegs)
}
// Log logs the statistics with the given logger.
func (s *SegStats) Log(logger log.Logger) {
if len(s.InsertedSegs) > 0 {
logger.Debug("Segments inserted in DB", "segments", s.InsertedSegs)
}
if len(s.UpdatedSegs) > 0 {
logger.Debug("Segments updated in DB", "segments", s.UpdatedSegs)
}
}
// Storage is used to store segments and revocations.
type Storage interface {
StoreSegs(context.Context, []*SegWithHP) (SegStats, error)
StoreRevs(context.Context, []*path_mgmt.SignedRevInfo) error
}
// DefaultStorage wraps path DB and revocation cache and offers
// convenience methods that implement the Storage interface.
type DefaultStorage struct {
PathDB pathdb.PathDB
RevCache revcache.RevCache
}
// StoreSegs stores the given segments in the pathdb in a transaction.
func (s *DefaultStorage) StoreSegs(ctx context.Context, segs []*SegWithHP) (SegStats, error) {
tx, err := s.PathDB.BeginTransaction(ctx, nil)
if err != nil {
return SegStats{}, err
}
defer tx.Rollback()
// Sort to prevent sql deadlock.
sort.Slice(segs, func(i, j int) bool {
return segs[i].Seg.Segment.GetLoggingID() < segs[j].Seg.Segment.GetLoggingID()
})
segStats := SegStats{}
for _, seg := range segs {
stats, err := tx.InsertWithHPCfgIDs(ctx, seg.Seg, convertHPGroupID(seg.HPGroup))
if err != nil {
return SegStats{}, err
}
if stats.Inserted > 0 {
segStats.InsertedSegs = append(segStats.InsertedSegs, seg.Seg.Segment.GetLoggingID())
} else if stats.Updated > 0 {
segStats.UpdatedSegs = append(segStats.UpdatedSegs, seg.Seg.Segment.GetLoggingID())
}
}
if err := tx.Commit(); err != nil {
return SegStats{}, err
}
segStats.Log(log.FromCtx(ctx))
return segStats, nil
}
// StoreRevs stores the given revocations in the revocation cache.
func (s *DefaultStorage) StoreRevs(ctx context.Context,
revs []*path_mgmt.SignedRevInfo) error {
for _, rev := range revs {
if _, err := s.RevCache.Insert(ctx, rev); err != nil {
return err
}
}
return nil
}