mirrored from https://chromium.googlesource.com/infra/luci/luci-go
/
db.go
141 lines (124 loc) · 4.39 KB
/
db.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
// Copyright 2020 The LUCI 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 db defines common database interface.
package db
import (
"context"
"fmt"
"go.chromium.org/luci/server/tq/internal/reminder"
)
// DB abstracts out specific storage implementation.
type DB interface {
// Kind identifies this particular database implementation.
//
// Among other things it is used in Cloud Tasks messages involved in the
// implementation of the distributed sweeping.
Kind() string
// Defer defers the execution of the callback until the transaction lands.
//
// Panics if called outside of a transaction.
//
// The callback will receive the original non-transactional context.
Defer(context.Context, func(context.Context))
// SaveReminder persists reminder in a transaction context.
//
// Tags retriable errors as transient.
SaveReminder(context.Context, *reminder.Reminder) error
// DeleteReminder deletes reminder in a non-transaction context.
DeleteReminder(context.Context, *reminder.Reminder) error
// FetchRemindersMeta fetches Reminders with Ids in [low..high) range.
//
// RawPayload of Reminders should not be fetched.
// Both fresh & stale reminders should be fetched.
// The reminders should be returned in order of ascending Id.
//
// In case of error, partial result of fetched Reminders so far should be
// returned alongside the error. The caller will later call this method again
// to fetch the remaining of Reminders in range of [<lastReturned.Id+1> .. high).
FetchRemindersMeta(ctx context.Context, low, high string, limit int) ([]*reminder.Reminder, error)
// FetchReminderRawPayloads fetches raw payloads of a batch of Reminders.
//
// The Reminder objects are re-used in the returned batch.
// If any Reminder is no longer found, it is silently omitted in the returned
// batch.
// In case of any other error, partial result of fetched Reminders so far
// should be returned alongside the error.
FetchReminderRawPayloads(context.Context, []*reminder.Reminder) ([]*reminder.Reminder, error)
}
// Impl knows how to instantiate DB instances.
type Impl struct {
// Kind identifies this particular DB implementation.
//
// Must match Kind() of the produced DB instance.
Kind string
// ProbeForTxn "probes" a context for an active transaction, returning a DB
// that can be used to transactionally submit reminders or nil if this is not
// a transactional context.
ProbeForTxn func(context.Context) DB
// NonTxn returns an instance of DB that can be used outside of
// transactions.
//
// This is used by the sweeper to enumerate reminders.
NonTxn func(context.Context) DB
}
var impls []Impl
// Register registers a database implementation.
//
// Must be called during init() time.
func Register(db Impl) {
if db.Kind == "" {
panic("Kind must not be empty")
}
for _, impl := range impls {
if impl.Kind == db.Kind {
panic(fmt.Sprintf("DB %q is already registered", db.Kind))
}
}
impls = append(impls, db)
}
// Kinds returns IDs of registered database implementations.
func Kinds() []string {
kinds := make([]string, len(impls))
for i, impl := range impls {
kinds[i] = impl.Kind
}
return kinds
}
// TxnDB returns a Database that matches the context or nil.
//
// The process has a list of database engines registered via Register. Given
// a context, TxnDB examines if it carries a transaction with any of the
// registered DBs.
//
// Panics if more than one database matches the context.
func TxnDB(ctx context.Context) (db DB) {
for _, impl := range impls {
if d := impl.ProbeForTxn(ctx); d != nil {
if db != nil {
panic(fmt.Sprintf("multiple databases match the context: %q and %q", db.Kind(), d.Kind()))
}
db = d
}
}
return
}
// NonTxnDB returns a database with given ID or nil if not registered.
func NonTxnDB(ctx context.Context, id string) DB {
for _, impl := range impls {
if impl.Kind == id {
return impl.NonTxn(ctx)
}
}
return nil
}