This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 301
/
federationin.go
256 lines (227 loc) · 8.34 KB
/
federationin.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2020 the Exposure Notifications Server authors
//
// 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 database is a database interface to federation in.
package database
import (
"context"
"errors"
"fmt"
"time"
"github.com/google/exposure-notifications-server/internal/federationin/model"
"github.com/google/exposure-notifications-server/internal/pb/federation"
"github.com/google/exposure-notifications-server/pkg/database"
pgx "github.com/jackc/pgx/v4"
)
type FederationInDB struct {
db *database.DB
}
func New(db *database.DB) *FederationInDB {
return &FederationInDB{
db: db,
}
}
// FinalizeSyncFn is used to finalize a historical sync record.
type FinalizeSyncFn func(state *federation.FetchState, q *model.FederationInQuery, totalInserted int) error
type queryRowFn func(ctx context.Context, query string, args ...interface{}) pgx.Row
// Lock acquires lock with given name that times out after ttl. Returns an UnlockFn that can be used to unlock the lock. ErrAlreadyLocked will be returned if there is already a lock in use.
func (db *FederationInDB) Lock(ctx context.Context, lockID string, ttl time.Duration) (database.UnlockFn, error) {
return db.db.Lock(ctx, lockID, ttl)
}
// GetFederationInQuery returns a query for given queryID. If not found, ErrNotFound will be returned.
func (db *FederationInDB) GetFederationInQuery(ctx context.Context, queryID string) (*model.FederationInQuery, error) {
var query *model.FederationInQuery
if err := db.db.InTx(ctx, pgx.ReadCommitted, func(tx pgx.Tx) error {
var err error
query, err = getFederationInQuery(ctx, queryID, tx.QueryRow)
return err
}); err != nil {
return nil, fmt.Errorf("get federation in query: %w", err)
}
return query, nil
}
func getFederationInQuery(ctx context.Context, queryID string, queryRow queryRowFn) (*model.FederationInQuery, error) {
row := queryRow(ctx, `
SELECT
query_id, server_addr, oidc_audience, include_regions, exclude_regions,
only_local_provenance, only_travelers,
last_timestamp, primary_cursor, last_revised_timestamp, revised_cursor
FROM
FederationInQuery
WHERE
query_id=$1
`, queryID)
var lastTimestamp, revisedTimestamp *time.Time
var lastCursor, revisedCursor *string
// See https://www.opsdash.com/blog/postgres-arrays-golang.html for working with Postgres arrays in Go.
q := model.FederationInQuery{}
if err := row.Scan(&q.QueryID, &q.ServerAddr, &q.Audience, &q.IncludeRegions, &q.ExcludeRegions,
&q.OnlyLocalProvenance, &q.OnlyTravelers,
&lastTimestamp, &lastCursor, &revisedTimestamp, &revisedCursor); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, database.ErrNotFound
}
return nil, fmt.Errorf("scanning results: %w", err)
}
if lastTimestamp != nil {
q.LastTimestamp = *lastTimestamp
}
if lastCursor != nil {
q.LastCursor = *lastCursor
}
if revisedTimestamp != nil {
q.LastRevisedTimestamp = *revisedTimestamp
}
if revisedCursor != nil {
q.LastRevisedCursor = *revisedCursor
}
return &q, nil
}
// AddFederationInQuery adds a FederationInQuery entity. It will overwrite a query with matching q.queryID if it exists.
func (db *FederationInDB) AddFederationInQuery(ctx context.Context, q *model.FederationInQuery) error {
return db.db.InTx(ctx, pgx.ReadCommitted, func(tx pgx.Tx) error {
query := `
INSERT INTO
FederationInQuery
(query_id, server_addr, oidc_audience, include_regions, exclude_regions, only_local_provenance, only_travelers)
VALUES
($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT
(query_id)
DO UPDATE
SET server_addr = $2, oidc_audience = $3, include_regions = $4, exclude_regions = $5, only_local_provenance = $6, only_travelers = $7
`
_, err := tx.Exec(ctx, query, q.QueryID, q.ServerAddr, q.Audience, q.IncludeRegions, q.ExcludeRegions, q.OnlyLocalProvenance, q.OnlyTravelers)
if err != nil {
return fmt.Errorf("upserting federation query: %w", err)
}
return nil
})
}
// GetFederationInSync returns a federation sync record for given syncID. If not found, ErrNotFound will be returned.
func (db *FederationInDB) GetFederationInSync(ctx context.Context, syncID int64) (*model.FederationInSync, error) {
var sync *model.FederationInSync
if err := db.db.InTx(ctx, pgx.ReadCommitted, func(tx pgx.Tx) error {
var err error
sync, err = getFederationInSync(ctx, syncID, tx.QueryRow)
return err
}); err != nil {
return nil, fmt.Errorf("get federation in sync: %w", err)
}
return sync, nil
}
func getFederationInSync(ctx context.Context, syncID int64, queryRowContext queryRowFn) (*model.FederationInSync, error) {
row := queryRowContext(ctx, `
SELECT
sync_id, query_id, started, completed, insertions, max_timestamp, max_revised_timestamp
FROM
FederationInSync
WHERE
sync_id=$1
`, syncID)
s := model.FederationInSync{}
var (
completed, max, maxRevised *time.Time
insertions *int
)
if err := row.Scan(&s.SyncID, &s.QueryID, &s.Started, &completed, &insertions, &max, &maxRevised); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, database.ErrNotFound
}
return nil, fmt.Errorf("scanning results: %w", err)
}
if completed != nil {
s.Completed = *completed
}
if max != nil {
s.MaxTimestamp = *max
}
if maxRevised != nil {
s.MaxRevisedTimestamp = *maxRevised
}
if insertions != nil {
s.Insertions = *insertions
}
return &s, nil
}
// unixToTimestamp converts a unix timestamp into a time.Time
func unixToTimestamp(unixTS int64) *time.Time {
ts := time.Unix(unixTS, 0).UTC().Truncate(time.Second)
return &ts
}
// StartFederationInSync stores a historical record of a query sync starting. It returns a FederationInSync key, and a FinalizeSyncFn that must be invoked to finalize the historical record.
func (db *FederationInDB) StartFederationInSync(ctx context.Context, q *model.FederationInQuery, started time.Time) (int64, FinalizeSyncFn, error) {
conn, err := db.db.Pool.Acquire(ctx)
if err != nil {
return 0, nil, fmt.Errorf("acquiring connection: %w", err)
}
defer conn.Release()
// startedTime is used internally to compute a Duration between here and when finalize function below is executed.
// This allows the finalize function to not request a completed Time parameter.
startedTimer := time.Now()
var syncID int64
row := conn.QueryRow(ctx, `
INSERT INTO
FederationInSync
(query_id, started)
VALUES
($1, $2)
RETURNING sync_id
`, q.QueryID, started)
if err := row.Scan(&syncID); err != nil {
return 0, nil, fmt.Errorf("fetching sync_id: %w", err)
}
finalize := func(state *federation.FetchState, q *model.FederationInQuery, totalInserted int) error {
completed := started.Add(time.Since(startedTimer))
return db.db.InTx(ctx, pgx.ReadCommitted, func(tx pgx.Tx) error {
// Special case: when no keys are pulled, the maxTimestamp will be 0, so we don't update the
// FederationQuery in this case to prevent it from going back and fetching old keys from the past.
if totalInserted > 0 {
q.UpdateFetchState(state)
_, err := tx.Exec(ctx, `
UPDATE
FederationInQuery
SET
last_timestamp = $1, primary_cursor = $2,
last_revised_timestamp = $3, revised_cursor = $4
WHERE
query_id = $5
`, q.LastTimestamp, q.LastCursor, q.LastRevisedTimestamp, q.LastRevisedCursor, q.QueryID)
if err != nil {
return fmt.Errorf("updating federation query state: %w", err)
}
}
var max, maxRevised *time.Time
if totalInserted > 0 {
max = unixToTimestamp(state.KeyCursor.Timestamp)
maxRevised = unixToTimestamp(state.RevisedKeyCursor.Timestamp)
}
_, err = tx.Exec(ctx, `
UPDATE
FederationInSync
SET
completed = $1,
insertions = $2,
max_timestamp = $3,
max_revised_timestamp = $4
WHERE
sync_id = $5
`, completed, totalInserted, max, maxRevised, syncID)
if err != nil {
return fmt.Errorf("updating federation sync: %w", err)
}
return nil
})
}
return syncID, finalize, nil
}