Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor jetcd watch #112

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/main/java/com/jd/jdbc/key/CurrentShard.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,26 @@
import java.util.concurrent.ConcurrentHashMap;

public class CurrentShard implements Shard {
private static final Map<String, List<Topodata.ShardReference>> map = new ConcurrentHashMap<>();
private static final Map<String, List<Topodata.ShardReference>> SHARDREFERENCE_MAP = new ConcurrentHashMap<>();

public static void setShardReferences(final String keyspace, final List<Topodata.ShardReference> shardReferences) {
map.put(keyspace, shardReferences);
SHARDREFERENCE_MAP.put(keyspace, shardReferences);
}

public static void setShardReferences(final String keyspace, final Topodata.SrvKeyspace srvKeyspace) {
List<Topodata.SrvKeyspace.KeyspacePartition> partitionsList = srvKeyspace.getPartitionsList();
List<Topodata.ShardReference> shardReferencesList = null;
for (Topodata.SrvKeyspace.KeyspacePartition keyspacePartition : partitionsList) {
if (!Topodata.TabletType.MASTER.equals(keyspacePartition.getServedType())) {
continue;
}
shardReferencesList = keyspacePartition.getShardReferencesList();
}
SHARDREFERENCE_MAP.put(keyspace, shardReferencesList);
}

@Override
public List<Topodata.ShardReference> getShardReferences(final String keyspace, final int shardNumber) {
return map.get(keyspace);
return SHARDREFERENCE_MAP.get(keyspace);
}
}
49 changes: 29 additions & 20 deletions src/main/java/com/jd/jdbc/monitor/SrvKeyspaceCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@
package com.jd.jdbc.monitor;

import com.google.common.collect.Lists;
import com.jd.jdbc.common.util.Crc32Utill;
import com.jd.jdbc.srvtopo.ResilientServer;
import com.jd.jdbc.topo.TopoServer;
import io.prometheus.client.Collector;
import io.prometheus.client.Counter;
import io.prometheus.client.GaugeMetricFamily;
import io.vitess.proto.Topodata;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class SrvKeyspaceCollector extends Collector {
private static final List<String> LABEL_NAMES = Lists.newArrayList("Cell", "Keyspace", "MD5");
private static final List<String> LABEL_NAMES = Lists.newArrayList("Keyspace");

private static final String COLLECT_NAME = "SrvKeyspaceCollector";

Expand Down Expand Up @@ -56,6 +60,12 @@ public final class SrvKeyspaceCollector extends Collector {
.help("SrvKeyspaceTask error counter info")
.register(MonitorServer.getCollectorRegistry());

private static final Counter SRV_KEYSPACE_TASK_UPDATE_COUNTER = Counter.build()
.name("SrvKeyspaceTask_update_counter_total")
.labelNames("Keyspace", "Cell")
.help("SrvKeyspaceTask update counter")
.register(MonitorServer.getCollectorRegistry());

private static final SrvKeyspaceCollector srvKeyspaceCollector = new SrvKeyspaceCollector();

private final List<ResilientServer> resilientServerList = new ArrayList<>();
Expand Down Expand Up @@ -83,34 +93,33 @@ public static Counter getSrvKeyspaceTaskErrorCounter() {
return SRV_KEYSPACE_TASK_ERROR_COUNTER;
}

public static Counter getSrvKeyspaceTaskUpdateCounter() {
return SRV_KEYSPACE_TASK_UPDATE_COUNTER;
}

@Override
public List<MetricFamilySamples> collect() {
Map<String, Topodata.SrvKeyspace> srvkeyspaceMapCopy = TopoServer.getSrvkeyspaceMapCopy();
if (srvkeyspaceMapCopy.isEmpty()) {
return null;
}
GaugeMetricFamily labeledGauge = new GaugeMetricFamily(COLLECT_NAME, COLLECT_HELP, LABEL_NAMES);
for (ResilientServer resilientServer : resilientServerList) {
List<Info> infoList = resilientServer.getSrvKeyspaceCollectorInfo();
for (Info info : infoList) {
List<String> labelValues = Lists.newArrayList(info.cell, info.keyspace, info.md5);
labeledGauge.addMetric(labelValues, Math.abs(info.md5.hashCode()));
for (Map.Entry<String, Topodata.SrvKeyspace> entry : srvkeyspaceMapCopy.entrySet()) {
String keyspace = entry.getKey();
Topodata.SrvKeyspace srvKeyspace = entry.getValue();
long crc32;
if (srvKeyspace == null) {
crc32 = 0L;
} else {
crc32 = Crc32Utill.checksumByCrc32(srvKeyspace.toString().getBytes());
}
List<String> labelValues = Collections.singletonList(keyspace);
labeledGauge.addMetric(labelValues, (double) crc32);
}
return Collections.singletonList(labeledGauge);
}

public void add(final ResilientServer resilientServer) {
resilientServerList.add(resilientServer);
}

public static class Info {
private final String md5;

private final String cell;

private final String keyspace;

public Info(final String md5, final String cell, final String keyspace) {
this.md5 = md5;
this.cell = cell;
this.keyspace = keyspace;
}
}
}
37 changes: 28 additions & 9 deletions src/main/java/com/jd/jdbc/queryservice/TabletDialer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,63 @@

package com.jd.jdbc.queryservice;

import com.jd.jdbc.util.threadpool.JdkUtil;
import com.jd.jdbc.util.threadpool.VtThreadFactoryBuilder;
import com.jd.jdbc.util.threadpool.impl.TabletNettyExecutorService;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.netty.NettyChannelBuilder;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.vitess.proto.Topodata;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

public class TabletDialer {

private static final NioEventLoopGroup EVENT_EXECUTORS = new NioEventLoopGroup(JdkUtil.getQueryExecutorCorePoolSize(), VtThreadFactoryBuilder.build("NioEventLoop-"));

/**
* TabletQueryService Cache.
*/
private static final Map<String, IParentQueryService> TABLETQUERYSERVICECACHE = new ConcurrentHashMap<>(128 + 1);
private static final Map<String, IParentQueryService> TABLET_QUERY_SERVICE_CACHE = new ConcurrentHashMap<>(128 + 1);

public static IParentQueryService dial(final Topodata.Tablet tablet) {
final String addr = tablet.getHostname() + ":" + tablet.getPortMapMap().get("grpc");
if (TABLETQUERYSERVICECACHE.containsKey(addr)) {
return TABLETQUERYSERVICECACHE.get(addr);
if (TABLET_QUERY_SERVICE_CACHE.containsKey(addr)) {
return TABLET_QUERY_SERVICE_CACHE.get(addr);
}

ManagedChannel channel = ManagedChannelBuilder.forTarget(addr).usePlaintext().keepAliveTimeout(10, TimeUnit.SECONDS).keepAliveTime(10, TimeUnit.SECONDS).keepAliveWithoutCalls(true).build();
ManagedChannel channel = NettyChannelBuilder.forTarget(addr).usePlaintext()
.offloadExecutor(TabletNettyExecutorService.getNettyExecutorService())
.executor(TabletNettyExecutorService.getNettyExecutorService())
.channelType(NioSocketChannel.class)
.eventLoopGroup(EVENT_EXECUTORS)
.keepAliveTimeout(10, TimeUnit.SECONDS).keepAliveTime(10, TimeUnit.SECONDS).keepAliveWithoutCalls(true).build();

IParentQueryService combinedQueryService = new CombinedQueryService(channel, tablet);
TABLETQUERYSERVICECACHE.putIfAbsent(addr, combinedQueryService);
TABLET_QUERY_SERVICE_CACHE.putIfAbsent(addr, combinedQueryService);
return combinedQueryService;
}

public static void close(final Topodata.Tablet tablet) {
final String addr = tablet.getHostname() + ":" + tablet.getPortMapMap().get("grpc");
TABLETQUERYSERVICECACHE.remove(addr);
TABLET_QUERY_SERVICE_CACHE.remove(addr);
}

protected static void registerTabletCache(final Topodata.Tablet tablet, final IParentQueryService combinedQueryService) {
final String addr = tablet.getHostname() + ":" + tablet.getPortMapMap().get("grpc");
TABLETQUERYSERVICECACHE.putIfAbsent(addr, combinedQueryService);
TABLET_QUERY_SERVICE_CACHE.putIfAbsent(addr, combinedQueryService);
}

protected static void clearTabletCache() {
TABLETQUERYSERVICECACHE.clear();
TABLET_QUERY_SERVICE_CACHE.clear();
}

private void shutdown() {
if (EVENT_EXECUTORS.isShuttingDown()) {
return;
}
EVENT_EXECUTORS.shutdownGracefully();
}
}
Loading
Loading