forked from cockroachdb/replicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinks.go
149 lines (130 loc) · 4.09 KB
/
sinks.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
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package main
import (
"bufio"
"context"
"database/sql"
"fmt"
"log"
"net/http"
"strings"
"sync"
"github.com/cockroachdb/cockroach-go/crdb"
_ "github.com/lib/pq"
)
// Sinks holds a map of all known sinks.
type Sinks struct {
// Is this mutex overkill? Is it needed? There should never be any writes
// to the map after initialization. But guidance here is fuzzy, so I'll keep
// it in.
sync.RWMutex
// endpoints can have multiple tables
sinksByTableByEndpoint map[string]map[string]*Sink
}
// CreateSinks creates a new table sink and populates it based on the pass in
// config.
func CreateSinks(db *sql.DB, config Config) (*Sinks, error) {
sinks := &Sinks{
sinksByTableByEndpoint: make(map[string]map[string]*Sink),
}
for _, entry := range config {
if err := sinks.AddSink(db, entry); err != nil {
return nil, err
}
}
return sinks, nil
}
// AddSink creates and adds a new sink to the sinks map.
func (s *Sinks) AddSink(db *sql.DB, entry ConfigEntry) error {
s.Lock()
defer s.Unlock()
sourceTable := strings.ToLower(strings.TrimSpace(entry.SourceTable))
destinationDB := strings.ToLower(strings.TrimSpace(entry.DestinationDatabase))
destinationTable := strings.ToLower(strings.TrimSpace(entry.DestinationTable))
endpoint := strings.ToLower(strings.TrimSpace(entry.Endpoint))
// First check to make sure the endpoint exists, if it doesn't create one.
var sinksByTable map[string]*Sink
var exist bool
if sinksByTable, exist = s.sinksByTableByEndpoint[endpoint]; !exist {
sinksByTable = make(map[string]*Sink)
s.sinksByTableByEndpoint[endpoint] = sinksByTable
}
// Check for a double table
if _, exist := sinksByTable[sourceTable]; exist {
return fmt.Errorf("Duplicate table configuration entry found: %s", sourceTable)
}
sink, err := CreateSink(db, sourceTable, destinationDB, destinationTable, endpoint)
if err != nil {
return err
}
sinksByTable[sourceTable] = sink
s.sinksByTableByEndpoint[endpoint] = sinksByTable
return nil
}
// FindSink returns a sink for a given table name and endpoint.
func (s *Sinks) FindSink(endpoint string, table string) *Sink {
s.RLock()
defer s.RUnlock()
sinksByTable, exist := s.sinksByTableByEndpoint[endpoint]
if !exist {
return nil
}
result, _ := sinksByTable[table]
return result
}
// GetAllSinksByEndpoint gets a list of all known sinks.
func (s *Sinks) GetAllSinksByEndpoint(endpoint string) []*Sink {
s.RLock()
defer s.RUnlock()
var allSinks []*Sink
if sinksByTable, exists := s.sinksByTableByEndpoint[endpoint]; exists {
for _, sink := range sinksByTable {
allSinks = append(allSinks, sink)
}
}
return allSinks
}
// HandleResolvedRequest parses and applies all the resolved upserts.
func (s *Sinks) HandleResolvedRequest(
db *sql.DB, rURL resolvedURL, w http.ResponseWriter, r *http.Request,
) {
scanner := bufio.NewScanner(r.Body)
defer r.Body.Close()
for scanner.Scan() {
next, err := parseResolvedLine(scanner.Bytes(), rURL.endpoint)
if err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Start the transation
if err := crdb.ExecuteTx(context.Background(), db, nil, func(tx *sql.Tx) error {
// Get the previous resolved
prev, err := getPreviousResolved(tx, rURL.endpoint)
if err != nil {
return err
}
log.Printf("%s: resolved - timestamp %d.%d", next.endpoint, next.nanos, next.logical)
// Find all rows to update and upsert them.
allSinks := s.GetAllSinksByEndpoint(rURL.endpoint)
for _, sink := range allSinks {
if err := sink.UpdateRows(tx, prev, next); err != nil {
return err
}
}
// Write the updated resolved.
return next.writeUpdated(tx)
}); err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}