This repository has been archived by the owner on Jul 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 151
/
mutation_logs.go
258 lines (238 loc) · 8.08 KB
/
mutation_logs.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
257
258
// Copyright 2018 Google Inc. All Rights Reserved.
//
// 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 mutationstorage
import (
"context"
"database/sql"
"time"
"github.com/golang/glog"
"github.com/golang/protobuf/proto"
"github.com/google/keytransparency/core/mutator"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/google/keytransparency/core/api/v1/keytransparency_go_proto"
)
// quantum is the fidelity of the Timestamp column.
// See https://dev.mysql.com/doc/refman/8.0/en/fractional-seconds.html
const quantum = time.Microsecond
// SetWritable enables or disables new writes from going to logID.
func (m *Mutations) SetWritable(ctx context.Context, directoryID string, logID int64, enabled bool) error {
result, err := m.db.ExecContext(ctx,
`UPDATE Logs SET Enabled = ? WHERE DirectoryID = ? AND LogID = ?;`,
enabled, directoryID, logID)
if err != nil {
return err
}
rows, err := result.RowsAffected()
if err != nil {
return err
}
if rows == 0 {
return status.Errorf(codes.NotFound, "log %d not found for directory %v", logID, directoryID)
}
return err
}
// AddLogs creates and adds new logs for writing to a directory.
func (m *Mutations) AddLogs(ctx context.Context, directoryID string, logIDs ...int64) error {
glog.Infof("mutationstorage: AddLog(%v, %v)", directoryID, logIDs)
for _, logID := range logIDs {
// TODO(gdbelvin): Use INSERT IGNORE to allow this function to be retried.
// TODO(gdbelvin): Migrate to a MySQL Docker image for unit tests.
// MySQL and SQLite do not have the same syntax for INSERT IGNORE.
if _, err := m.db.ExecContext(ctx,
`INSERT INTO Logs (DirectoryID, LogID, Enabled) Values(?, ?, ?);`,
directoryID, logID, true); err != nil {
return err
}
}
return nil
}
// Send writes mutations to the leading edge (by sequence number) of the mutations table.
// Returns the logID/watermark pair that was written, or nil if nothing was written.
// TODO(gbelvin): Make updates a slice.
func (m *Mutations) Send(ctx context.Context, directoryID string, logID int64, updates ...*pb.EntryUpdate) (time.Time, error) {
glog.Infof("mutationstorage: Send(%v, <mutation>)", directoryID)
if len(updates) == 0 {
return time.Time{}, nil
}
updateData := make([][]byte, 0, len(updates))
for _, u := range updates {
data, err := proto.Marshal(u)
if err != nil {
return time.Time{}, err
}
updateData = append(updateData, data)
}
// TODO(gbelvin): Implement retry with backoff for retryable errors if
// we get timestamp contention.
ts := time.Now().Truncate(quantum)
if err := m.send(ctx, ts, directoryID, logID, updateData...); err != nil {
return time.Time{}, err
}
return ts, nil
}
// ListLogs returns a list of all logs for directoryID, optionally filtered for writable logs.
func (m *Mutations) ListLogs(ctx context.Context, directoryID string, writable bool) ([]int64, error) {
var query string
if writable {
query = `SELECT LogID from Logs WHERE DirectoryID = ? AND Enabled = True;`
} else {
query = `SELECT LogID from Logs WHERE DirectoryID = ?;`
}
var logIDs []int64
rows, err := m.db.QueryContext(ctx, query, directoryID)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var logID int64
if err := rows.Scan(&logID); err != nil {
return nil, err
}
logIDs = append(logIDs, logID)
}
if err := rows.Err(); err != nil {
return nil, err
}
if len(logIDs) == 0 {
return nil, status.Errorf(codes.NotFound, "no log found for directory %v", directoryID)
}
return logIDs, nil
}
// ts must be greater than all other timestamps currently recorded for directoryID.
func (m *Mutations) send(ctx context.Context, ts time.Time, directoryID string,
logID int64, mData ...[]byte) (ret error) {
tx, err := m.db.BeginTx(ctx,
&sql.TxOptions{Isolation: sql.LevelSerializable})
if err != nil {
return err
}
defer func() {
if ret != nil {
if err := tx.Rollback(); err != nil {
ret = status.Errorf(codes.Internal, "%v, and could not rollback: %v", ret, err)
}
}
}()
var maxTime sql.NullTime
if err := tx.QueryRowContext(ctx,
`SELECT MAX(Time) FROM Queue WHERE DirectoryID = ? AND LogID = ?;`,
directoryID, logID).Scan(&maxTime); err != nil {
return status.Errorf(codes.Internal, "could not find max timestamp: %v", err)
}
if !ts.After(maxTime.Time) {
return status.Errorf(codes.Aborted,
"current timestamp: %v, want > max-timestamp of queued mutations: %v", ts, maxTime)
}
for i, data := range mData {
if _, err = tx.ExecContext(ctx,
`INSERT INTO Queue (DirectoryID, LogID, Time, LocalID, Mutation) VALUES (?, ?, ?, ?, ?);`,
directoryID, logID, ts, i, data); err != nil {
return status.Errorf(codes.Internal, "failed inserting into queue: %v", err)
}
}
return tx.Commit()
}
// HighWatermark returns the highest watermark +1 in logID that is less than or
// equal to batchSize items greater than start.
func (m *Mutations) HighWatermark(ctx context.Context, directoryID string, logID int64,
start time.Time, batchSize int32) (int32, time.Time, error) {
startQuery := start.Add(quantum - 1).Truncate(quantum)
var count int32
var high sql.NullTime
if err := m.db.QueryRowContext(ctx,
`SELECT COUNT(*), MAX(T1.Time) FROM
(
SELECT Q.Time FROM Queue as Q
WHERE Q.DirectoryID = ? AND Q.LogID = ? AND Q.Time >= ?
ORDER BY Q.Time ASC
LIMIT ?
) AS T1`,
directoryID, logID, startQuery, batchSize).
Scan(&count, &high); err != nil {
return 0, start, err
}
if count == 0 {
// When there are no rows, return the start time as the highest timestamp.
return 0, start, nil
}
return count, high.Time.Add(1 * time.Microsecond), nil
}
// ReadLog reads all mutations in logID between [low, high).
// ReadLog may return more rows than batchSize in order to fetch all the rows at a particular timestamp.
func (m *Mutations) ReadLog(ctx context.Context, directoryID string,
logID int64, low, high time.Time, batchSize int32) ([]*mutator.LogMessage, error) {
// Advance the low and high marks to the next highest quantum to preserve read semantics.
low = low.Add(quantum - 1).Truncate(quantum)
high = high.Add(quantum - 1).Truncate(quantum)
rows, err := m.db.QueryContext(ctx,
`SELECT Time, LocalID, Mutation FROM Queue
WHERE DirectoryID = ? AND LogID = ? AND Time >= ? AND Time < ?
ORDER BY Time, LocalID ASC
LIMIT ?;`,
directoryID, logID, low, high, batchSize)
if err != nil {
return nil, err
}
defer rows.Close()
msgs, err := readQueueMessages(rows)
if err != nil {
return nil, err
}
// Read the rest of the LocalIDs in the last row.
if len(msgs) > 0 {
last := msgs[len(msgs)-1]
restRows, err := m.db.QueryContext(ctx,
`SELECT Time, LocalID, Mutation FROM Queue
WHERE DirectoryID = ? AND LogID = ? AND Time = ? AND LocalID > ?
ORDER BY LocalID ASC;`,
directoryID, logID, last.ID, last.LocalID)
if err != nil {
return nil, err
}
defer restRows.Close()
rest, err := readQueueMessages(restRows)
if err != nil {
return nil, err
}
msgs = append(msgs, rest...)
}
return msgs, nil
}
func readQueueMessages(rows *sql.Rows) ([]*mutator.LogMessage, error) {
results := make([]*mutator.LogMessage, 0)
for rows.Next() {
var timestamp time.Time
var localID int64
var mData []byte
if err := rows.Scan(×tamp, &localID, &mData); err != nil {
return nil, err
}
entryUpdate := new(pb.EntryUpdate)
if err := proto.Unmarshal(mData, entryUpdate); err != nil {
return nil, err
}
results = append(results, &mutator.LogMessage{
ID: timestamp,
LocalID: localID,
Mutation: entryUpdate.Mutation,
ExtraData: entryUpdate.Committed,
})
}
if err := rows.Err(); err != nil {
return nil, err
}
return results, nil
}