This repository has been archived by the owner on Feb 18, 2021. It is now read-only.
/
inconn.go
207 lines (180 loc) · 6.33 KB
/
inconn.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
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package replicator
import (
"sync"
"time"
"github.com/uber-common/bark"
"github.com/uber/cherami-server/common"
"github.com/uber/cherami-server/common/metrics"
storeStream "github.com/uber/cherami-server/stream"
"github.com/uber/cherami-thrift/.generated/go/store"
)
type (
inConnection struct {
extUUID string
stream storeStream.BStoreOpenReadStreamInCall
msgCh <-chan *store.ReadMessageContent
logger bark.Logger
m3Client metrics.Client
destM3Client metrics.Client
metricsScope int
perDestMetricsScope int
creditsCh chan int32 // channel to pass credits from readCreditsStream to writeMsgsStream
creditFlowExpiration time.Time // credit expiration is used to close the stream if we don't receive any credit for some period of time
wg sync.WaitGroup
shutdownCh chan struct{}
}
)
const (
creditFlowTimeout = 10 * time.Minute
flushTimeout = 50 * time.Millisecond
)
func newInConnection(extUUID string, destPath string, stream storeStream.BStoreOpenReadStreamInCall, msgCh <-chan *store.ReadMessageContent, logger bark.Logger, m3Client metrics.Client, metricsScope int, perDestMetricsScope int) *inConnection {
localLogger := logger.WithFields(bark.Fields{
common.TagExt: extUUID,
common.TagDstPth: destPath,
`scope`: `inConnection`,
})
conn := &inConnection{
extUUID: extUUID,
stream: stream,
msgCh: msgCh,
logger: localLogger,
m3Client: m3Client,
destM3Client: metrics.NewClientWithTags(m3Client, metrics.Replicator, common.GetDestinationTags(destPath, localLogger)),
metricsScope: metricsScope,
perDestMetricsScope: perDestMetricsScope,
creditsCh: make(chan int32, 5),
creditFlowExpiration: time.Now().Add(creditFlowTimeout),
shutdownCh: make(chan struct{}),
}
return conn
}
func (conn *inConnection) open() {
conn.wg.Add(2)
go conn.writeMsgsStream()
go conn.readCreditsStream()
conn.logger.Info("in connection opened")
}
func (conn *inConnection) WaitUntilDone() {
conn.wg.Wait()
}
func (conn *inConnection) shutdown() {
close(conn.shutdownCh)
conn.logger.Info(`in connection shutdown`)
}
func (conn *inConnection) readCreditsStream() {
defer conn.wg.Done()
defer close(conn.creditsCh)
for {
msg, err := conn.stream.Read()
if err != nil {
conn.logger.WithField(common.TagErr, err).Info("read credit failed")
return
}
conn.m3Client.AddCounter(conn.metricsScope, metrics.ReplicatorInConnCreditsReceived, int64(msg.GetCredits()))
// send this to writeMsgsPump which keeps track of the local credits
// Make this non-blocking because writeMsgsStream could be closed before this
select {
case conn.creditsCh <- msg.GetCredits():
case <-conn.shutdownCh:
return
default:
conn.logger.
WithField(`channelLen`, len(conn.creditsCh)).
WithField(`credits`, msg.GetCredits()).
Warn(`Dropped credits because of blocked channel`)
}
}
}
func (conn *inConnection) writeMsgsStream() {
defer conn.wg.Done()
defer conn.stream.Done()
flushTicker := time.NewTicker(flushTimeout)
defer flushTicker.Stop()
var localCredits int32
for {
if localCredits == 0 {
select {
case credit, ok := <-conn.creditsCh:
if !ok {
conn.logger.Info(`internal credit channel closed`)
return
}
conn.extentCreditExpiration()
localCredits += credit
case <-time.After(creditFlowTimeout):
conn.logger.Warn("credit flow timeout")
if conn.isCreditFlowExpired() {
conn.logger.Warn("credit flow expired")
return
}
case <-conn.shutdownCh:
return
}
} else {
select {
case msg, ok := <-conn.msgCh:
if !ok {
conn.logger.Info("msg channel closed")
return
}
if err := conn.stream.Write(msg); err != nil {
conn.logger.Error("write msg failed")
return
}
if msg.GetType() == store.ReadMessageContentType_SEALED {
conn.logger.Info(`sealed msg read`)
}
conn.m3Client.IncCounter(conn.metricsScope, metrics.ReplicatorInConnMsgWritten)
// Update per destination metrics after msg is sent to local store (call is OpenReplicationRemoteRead)
// so they're most accurate.
if conn.metricsScope == metrics.OpenReplicationRemoteReadScope && msg.GetType() == store.ReadMessageContentType_MESSAGE {
conn.destM3Client.IncCounter(conn.perDestMetricsScope, metrics.ReplicatorInConnPerDestMsgWritten)
latency := time.Duration(time.Now().UnixNano() - msg.GetMessage().Message.GetEnqueueTimeUtc())
conn.destM3Client.RecordTimer(conn.perDestMetricsScope, metrics.ReplicatorInConnPerDestMsgLatency, latency)
}
localCredits--
case credit, ok := <-conn.creditsCh:
if !ok {
conn.logger.Info(`internal credit channel closed`)
return
}
conn.extentCreditExpiration()
localCredits += credit
case <-flushTicker.C:
if err := conn.stream.Flush(); err != nil {
conn.logger.Error(`flush msg failed`)
return
}
case <-conn.shutdownCh:
return
}
}
}
}
func (conn *inConnection) isCreditFlowExpired() (ret bool) {
ret = conn.creditFlowExpiration.Before(time.Now())
return
}
func (conn *inConnection) extentCreditExpiration() {
conn.creditFlowExpiration = time.Now().Add(creditFlowTimeout)
}