forked from tikv/client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCDCClient.java
More file actions
215 lines (187 loc) · 7.97 KB
/
CDCClient.java
File metadata and controls
215 lines (187 loc) · 7.97 KB
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
package org.tikv.cdc;
import com.google.common.base.Preconditions;
import com.google.common.collect.Range;
import com.google.common.collect.TreeMultiset;
import io.grpc.ManagedChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tikv.common.TiSession;
import org.tikv.common.key.Key;
import org.tikv.common.region.TiRegion;
import org.tikv.common.util.RangeSplitter;
import org.tikv.common.util.RangeSplitter.RegionTask;
import org.tikv.kvproto.Cdcpb.Event.Row;
import org.tikv.kvproto.Coprocessor.KeyRange;
import org.tikv.kvproto.Kvrpcpb;
public class CDCClient implements AutoCloseable {
private static final Logger LOGGER = LoggerFactory.getLogger(CDCClient.class);
private final TiSession session;
private final KeyRange keyRange;
private final CDCConfig config;
private final BlockingQueue<CDCEvent> eventsBuffer;
private final TreeMap<Long, RegionCDCClient> regionClients = new TreeMap<>();
private final Map<Long, Long> regionToResolvedTs = new HashMap<>();
private final TreeMultiset<Long> resolvedTsSet = TreeMultiset.create();
private boolean started = false;
public CDCClient(final TiSession session, final KeyRange keyRange) {
this(session, keyRange, new CDCConfig());
}
public CDCClient(final TiSession session, final KeyRange keyRange, final CDCConfig config) {
Preconditions.checkState(
session.getConf().getIsolationLevel().equals(Kvrpcpb.IsolationLevel.SI),
"Unsupported Isolation Level"); // only support SI for now
this.session = session;
this.keyRange = keyRange;
this.config = config;
eventsBuffer = new ArrayBlockingQueue<>(config.getEventBufferSize());
}
public synchronized void start(final long startTs) {
Preconditions.checkState(!started, "Client is already started");
try {
applyKeyRange(keyRange, startTs);
} catch (final Throwable e) {
LOGGER.error("failed to start:", e);
}
started = true;
}
public synchronized Row get() throws InterruptedException {
final CDCEvent event = eventsBuffer.poll(100, TimeUnit.MILLISECONDS);
if (event != null) {
switch (event.eventType) {
case ROW:
return event.row;
case RESOLVED_TS:
handleResolvedTs(event.regionId, event.resolvedTs);
break;
case ERROR:
handleErrorEvent(event.regionId, event.error);
break;
}
}
return null;
}
public synchronized long getMinResolvedTs() {
return resolvedTsSet.firstEntry().getElement();
}
public synchronized long getMaxResolvedTs() {
return resolvedTsSet.lastEntry().getElement();
}
public synchronized void close() {
removeRegions(regionClients.keySet());
}
private synchronized void applyKeyRange(final KeyRange keyRange, final long timestamp) {
final RangeSplitter splitter = RangeSplitter.newSplitter(session.getRegionManager());
final Iterator<TiRegion> newRegionsIterator =
splitter
.splitRangeByRegion(Arrays.asList(keyRange))
.stream()
.map(RegionTask::getRegion)
.sorted((a, b) -> Long.compare(a.getId(), b.getId()))
.iterator();
final Iterator<RegionCDCClient> oldRegionsIterator = regionClients.values().iterator();
final ArrayList<TiRegion> regionsToAdd = new ArrayList<>();
final ArrayList<Long> regionsToRemove = new ArrayList<>();
TiRegion newRegion = newRegionsIterator.hasNext() ? newRegionsIterator.next() : null;
RegionCDCClient oldRegionClient =
oldRegionsIterator.hasNext() ? oldRegionsIterator.next() : null;
while (newRegion != null && oldRegionClient != null) {
if (newRegion.getId() == oldRegionClient.getRegion().getId()) {
// check if should refresh region
if (!oldRegionClient.isRunning()) {
regionsToRemove.add(newRegion.getId());
regionsToAdd.add(newRegion);
}
newRegion = newRegionsIterator.hasNext() ? newRegionsIterator.next() : null;
oldRegionClient = oldRegionsIterator.hasNext() ? oldRegionsIterator.next() : null;
} else if (newRegion.getId() < oldRegionClient.getRegion().getId()) {
regionsToAdd.add(newRegion);
newRegion = newRegionsIterator.hasNext() ? newRegionsIterator.next() : null;
} else {
regionsToRemove.add(oldRegionClient.getRegion().getId());
oldRegionClient = oldRegionsIterator.hasNext() ? oldRegionsIterator.next() : null;
}
}
while (newRegion != null) {
regionsToAdd.add(newRegion);
newRegion = newRegionsIterator.hasNext() ? newRegionsIterator.next() : null;
}
while (oldRegionClient != null) {
regionsToRemove.add(oldRegionClient.getRegion().getId());
oldRegionClient = oldRegionsIterator.hasNext() ? oldRegionsIterator.next() : null;
}
removeRegions(regionsToRemove);
addRegions(regionsToAdd, timestamp);
LOGGER.info("keyRange applied");
}
private synchronized void addRegions(final Iterable<TiRegion> regions, final long timestamp) {
LOGGER.info("add regions: {}, timestamp: {}", regions, timestamp);
for (final TiRegion region : regions) {
if (overlapWithRegion(region)) {
final String address =
session
.getRegionManager()
.getStoreById(region.getLeader().getStoreId())
.getStore()
.getAddress();
final ManagedChannel channel =
session.getChannelFactory().getChannel(address, session.getPDClient().getHostMapping());
try {
final RegionCDCClient client =
new RegionCDCClient(region, keyRange, channel, eventsBuffer::offer, config);
regionClients.put(region.getId(), client);
regionToResolvedTs.put(region.getId(), timestamp);
resolvedTsSet.add(timestamp);
client.start(timestamp);
} catch (final Exception e) {
LOGGER.error("failed to add region(regionId: {}, reason: {})", region.getId(), e);
throw new RuntimeException(e);
}
}
}
}
private synchronized void removeRegions(final Iterable<Long> regionIds) {
LOGGER.info("remove regions: {}", regionIds);
for (final long regionId : regionIds) {
final RegionCDCClient regionClient = regionClients.remove(regionId);
if (regionClient != null) {
try {
regionClient.close();
} catch (final Exception e) {
LOGGER.error("failed to close region client, region id: {}, error: {}", regionId, e);
} finally {
resolvedTsSet.remove(regionToResolvedTs.remove(regionId));
regionToResolvedTs.remove(regionId);
}
}
}
}
private boolean overlapWithRegion(final TiRegion region) {
final Range<Key> regionRange =
Range.closedOpen(Key.toRawKey(region.getStartKey()), Key.toRawKey(region.getEndKey()));
final Range<Key> clientRange =
Range.closedOpen(Key.toRawKey(keyRange.getStart()), Key.toRawKey(keyRange.getEnd()));
final Range<Key> intersection = regionRange.intersection(clientRange);
return !intersection.isEmpty();
}
private void handleResolvedTs(final long regionId, final long resolvedTs) {
LOGGER.info("handle resolvedTs: {}, regionId: {}", resolvedTs, regionId);
resolvedTsSet.remove(regionToResolvedTs.replace(regionId, resolvedTs));
resolvedTsSet.add(resolvedTs);
}
private void handleErrorEvent(final long regionId, final Throwable error) {
LOGGER.info("handle error: {}, regionId: {}", error, regionId);
final TiRegion region = regionClients.get(regionId).getRegion();
session.getRegionManager().onRequestFail(region); // invalidate cache for corresponding region
removeRegions(Arrays.asList(regionId));
applyKeyRange(keyRange, getMinResolvedTs()); // reapply the whole keyRange
}
}