-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_log.go
136 lines (119 loc) · 4.25 KB
/
event_log.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
// Copyright 2016 The Cockroach 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. See the AUTHORS file
// for names of contributors.
package sql
import (
"encoding/json"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
// EventLogType represents an event type that can be recorded in the event log.
type EventLogType string
// NOTE: When you add a new event type here. Please manually add it to
// ui/app/util/eventTypes.ts so that it will be recognized in the UI.
const (
// EventLogCreateDatabase is recorded when a database is created.
EventLogCreateDatabase EventLogType = "create_database"
// EventLogDropDatabase is recorded when a database is dropped.
EventLogDropDatabase EventLogType = "drop_database"
// EventLogCreateTable is recorded when a table is created.
EventLogCreateTable EventLogType = "create_table"
// EventLogDropTable is recorded when a table is dropped.
EventLogDropTable EventLogType = "drop_table"
// EventLogAlterTable is recorded when a table is altered.
EventLogAlterTable EventLogType = "alter_table"
// EventLogCreateIndex is recorded when an index is created.
EventLogCreateIndex EventLogType = "create_index"
// EventLogDropIndex is recorded when an index is dropped.
EventLogDropIndex EventLogType = "drop_index"
// EventLogCreateView is recorded when a view is created.
EventLogCreateView EventLogType = "create_view"
// EventLogDropView is recorded when a view is dropped.
EventLogDropView EventLogType = "drop_view"
// EventLogReverseSchemaChange is recorded when an in-progress schema change
// encounters a problem and is reversed.
EventLogReverseSchemaChange EventLogType = "reverse_schema_change"
// EventLogFinishSchemaChange is recorded when a previously initiated schema
// change has completed.
EventLogFinishSchemaChange EventLogType = "finish_schema_change"
// EventLogNodeJoin is recorded when a node joins the cluster.
EventLogNodeJoin EventLogType = "node_join"
// EventLogNodeRestart is recorded when an existing node rejoins the cluster
// after being offline.
EventLogNodeRestart EventLogType = "node_restart"
// EventLogSetClusterSetting is recorded when a cluster setting is changed.
EventLogSetClusterSetting EventLogType = "set_cluster_setting"
)
// An EventLogger exposes methods used to record events to the event table.
type EventLogger struct {
InternalExecutor
}
// MakeEventLogger constructs a new EventLogger. A LeaseManager is required in
// order to correctly execute SQL statements.
func MakeEventLogger(leaseMgr *LeaseManager) EventLogger {
return EventLogger{InternalExecutor{
LeaseManager: leaseMgr,
}}
}
// InsertEventRecord inserts a single event into the event log as part of the
// provided transaction.
func (ev EventLogger) InsertEventRecord(
ctx context.Context,
txn *client.Txn,
eventType EventLogType,
targetID, reportingID int32,
info interface{},
) error {
// Record event record insertion in local log output.
txn.AddCommitTrigger(func() {
log.Infof(
ctx, "Event: %q, target: %d, info: %+v",
eventType,
targetID,
info,
)
})
const insertEventTableStmt = `
INSERT INTO system.eventlog (
timestamp, "eventType", "targetID", "reportingID", info
)
VALUES(
now(), $1, $2, $3, $4
)
`
args := []interface{}{
eventType,
targetID,
reportingID,
nil, // info
}
if info != nil {
infoBytes, err := json.Marshal(info)
if err != nil {
return err
}
args[3] = string(infoBytes)
}
rows, err := ev.ExecuteStatementInTransaction(
ctx, "log-event", txn, insertEventTableStmt, args...)
if err != nil {
return err
}
if rows != 1 {
return errors.Errorf("%d rows affected by log insertion; expected exactly one row affected.", rows)
}
return nil
}