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

fix getAllCells when initial #157

Merged
merged 1 commit into from
Jun 5, 2024
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
95 changes: 13 additions & 82 deletions src/main/java/com/jd/jdbc/discovery/TopologyWatcherManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,11 @@

package com.jd.jdbc.discovery;

import com.jd.jdbc.common.util.CollectionUtils;
import com.jd.jdbc.context.IContext;
import com.jd.jdbc.sqlparser.support.logging.Log;
import com.jd.jdbc.sqlparser.support.logging.LogFactory;
import com.jd.jdbc.topo.TopoException;
import com.jd.jdbc.topo.TopoServer;
import com.jd.jdbc.util.threadpool.VtThreadFactoryBuilder;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

Expand All @@ -43,36 +34,30 @@ public enum TopologyWatcherManager {

private Map<String, TopologyWatcher> cellTopologyWatcherMap = null;

private Map<String, Set<String>> globalKeyspacesMap = null;

private final Lock lock = new ReentrantLock();

private static final Log LOGGER = LogFactory.getLog(TopologyWatcherManager.class);

private ScheduledThreadPoolExecutor scheduledExecutor;

TopologyWatcherManager() {
cellTopologyWatcherMap = new ConcurrentHashMap<>(16);
globalKeyspacesMap = new ConcurrentHashMap<>(16);

scheduledExecutor = new ScheduledThreadPoolExecutor(1, new VtThreadFactoryBuilder.DefaultThreadFactory("reload-cell-schedule", true));
scheduledExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
scheduledExecutor.setRemoveOnCancelPolicy(true);
}

public void startWatch(IContext ctx, TopoServer topoServer, String cell, String tabletKeyspace, TimeUnit timeUnit) {
public void startWatch(IContext ctx, TopoServer topoServer, String cell, String tabletKeyspace) {
lock.lock();
try {
String serverAddress = topoServer.getServerAddress();
if (!globalKeyspacesMap.containsKey(serverAddress)) {
globalKeyspacesMap.put(serverAddress, new HashSet<>());

startTickerReloadCell(ctx, topoServer, timeUnit);
if (!cellTopologyWatcherMap.containsKey(cell)) {
TopologyWatcher topologyWatcher = new TopologyWatcher(topoServer, cell, tabletKeyspace);
topologyWatcher.start(ctx);
cellTopologyWatcherMap.put(cell, topologyWatcher);
}
globalKeyspacesMap.get(serverAddress).add(tabletKeyspace);
} finally {
lock.unlock();
}
}

public void startWatch(IContext ctx, TopoServer topoServer, String cell, Set<String> keyspaces) {
lock.lock();
try {
if (!cellTopologyWatcherMap.containsKey(cell)) {
TopologyWatcher topologyWatcher = new TopologyWatcher(topoServer, cell, tabletKeyspace);
TopologyWatcher topologyWatcher = new TopologyWatcher(topoServer, cell, keyspaces);
topologyWatcher.start(ctx);
cellTopologyWatcherMap.put(cell, topologyWatcher);
}
Expand All @@ -89,68 +74,14 @@ public void watch(IContext ctx, String cell, String tabletKeyspace) {
}

public void close() {
closeScheduledExecutor();

for (Map.Entry<String, TopologyWatcher> entry : cellTopologyWatcherMap.entrySet()) {
TopologyWatcher topologyWatcher = entry.getValue();
topologyWatcher.close();
}
cellTopologyWatcherMap.clear();
globalKeyspacesMap.clear();
}

public void resetScheduledExecutor() {
closeScheduledExecutor();

scheduledExecutor = new ScheduledThreadPoolExecutor(1, new VtThreadFactoryBuilder.DefaultThreadFactory("reload-cell-schedule", true));
scheduledExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
scheduledExecutor.setRemoveOnCancelPolicy(true);
}

public void closeScheduledExecutor() {
scheduledExecutor.shutdownNow();
try {
int tryAgain = 3;
while (tryAgain > 0 && !scheduledExecutor.awaitTermination(1, TimeUnit.SECONDS)) {
tryAgain--;
}
} catch (InterruptedException e) {
// We're shutting down anyway, so just ignore.
}
}

public boolean isWatching(String cell) {
return cellTopologyWatcherMap.containsKey(cell);
}

public void startTickerReloadCell(IContext globalContext, TopoServer topoServer, TimeUnit timeUnit) {
scheduledExecutor.scheduleWithFixedDelay(() -> {
try {
tickerUpdateCells(globalContext, topoServer);
} catch (Throwable e) {
LOGGER.error("tickerUpdateCells error: " + e);
}
}, 5, 10, timeUnit);
}

private void tickerUpdateCells(IContext globalContext, TopoServer topoServer) throws TopoException {
String serverAddress = topoServer.getServerAddress();
Set<String> keyspaceSet = globalKeyspacesMap.get(serverAddress);
if (CollectionUtils.isEmpty(keyspaceSet)) {
throw new RuntimeException("not found keyspace in " + serverAddress + " of TopologyWatcherManager.globalKeyspacesMap .");
}
List<String> allCells = topoServer.getAllCells(globalContext);
for (String cell : allCells) {
if (!isWatching(cell)) {
lock.lock();
try {
TopologyWatcher topologyWatcher = new TopologyWatcher(topoServer, cell, keyspaceSet);
topologyWatcher.start(globalContext);
cellTopologyWatcherMap.put(cell, topologyWatcher);
} finally {
lock.unlock();
}
}
}
}
}
12 changes: 9 additions & 3 deletions src/main/java/com/jd/jdbc/evalengine/EvalEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ public static VtResultValue nullSafeAdd(VtResultValue value1, VtResultValue valu
BigDecimal bResult = b1.add(b2);
return VtResultValue.newVtResultValue(Query.Type.DECIMAL, bResult);
}
if (Query.Type.FLOAT64.equals(resultType)) {
BigDecimal f1 = (BigDecimal) ResultSetUtil.convertValue(value1, BigDecimal.class);
BigDecimal f2 = (BigDecimal) ResultSetUtil.convertValue(value2, BigDecimal.class);
BigDecimal fResult = f1.add(f2);
return VtResultValue.newVtResultValue(Query.Type.FLOAT64, fResult);
}
throw new SQLException("nullSafeAdd error");
}

Expand Down Expand Up @@ -861,7 +867,7 @@ public String string() {

public static class AnyExpr implements Expr {

private SQLExpr expr;
private final SQLExpr expr;

public AnyExpr(SQLExpr expr) {
this.expr = expr;
Expand Down Expand Up @@ -907,7 +913,7 @@ public void output(StringBuilder builder, boolean wrap, Map<String, com.jd.jdbc.
@AllArgsConstructor
public static class Column implements Expr {

private int offset;
private final int offset;

@Override
public EvalResult evaluate(ExpressionEnv env) throws SQLException {
Expand Down Expand Up @@ -958,7 +964,7 @@ public void output(StringBuilder builder, boolean wrap, Map<String, com.jd.jdbc.
@AllArgsConstructor
public static class TupleExpr implements Expr {

private List<Expr> tupleExpr;
private final List<Expr> tupleExpr;

@Override
public EvalResult evaluate(ExpressionEnv env) throws SQLException {
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/jd/jdbc/monitor/TopoServerCollector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2024 JD Project 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.
*/

package com.jd.jdbc.monitor;

import io.prometheus.client.Counter;

public class TopoServerCollector {

private static final Counter EXEC_COUNTER = Counter.build()
.name("TopoServer_get_all_cell_counter")
.labelNames("Address")
.help("TopoServer get all cells counter")
.register(MonitorServer.getCollectorRegistry());

private static final Counter NEW_CELLS_COUNTER = Counter.build()
.name("TopoServer_new_cell_counter")
.labelNames("Address")
.help("TopoServer add new cell counter")
.register(MonitorServer.getCollectorRegistry());

public static Counter getExecCounterCounter() {
return EXEC_COUNTER;
}

public static Counter geCellsCounter() {
return NEW_CELLS_COUNTER;
}
}
10 changes: 8 additions & 2 deletions src/main/java/com/jd/jdbc/topo/Topo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.jd.jdbc.topo;

import com.jd.jdbc.context.IContext;
import static com.jd.jdbc.topo.TopoExceptionCode.NO_IMPLEMENTATION;
import static com.jd.jdbc.topo.TopoServer.CELLS_PATH;
import static com.jd.jdbc.topo.TopoServer.CELL_INFO_FILE;
Expand All @@ -34,6 +35,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Topo {

Expand All @@ -45,7 +47,7 @@ public class Topo {

public static String TOPO_GLOBAL_ROOT = "/vitess/global";

public static TopoServer getTopoServer(TopoServerImplementType topoServerImplementType, String topoServerAddress) throws TopoException {
public static TopoServer getTopoServer(IContext ctx, TopoServerImplementType topoServerImplementType, String topoServerAddress) throws TopoException {
synchronized (Topo.class) {
registerFactory(topoServerImplementType);

Expand All @@ -58,6 +60,7 @@ public static TopoServer getTopoServer(TopoServerImplementType topoServerImpleme
throw new TopoException(Vtrpc.Code.UNKNOWN, "");
}
topoServers.put(topoServerAddress, topoServer);
topoServer.startTickerReloadCell(ctx);
return topoServer;
}
}
Expand Down Expand Up @@ -124,7 +127,10 @@ protected static TopoServer newWithFactory(TopoFactory topoFactory, String serve
topoServer.globalCell = conn;
topoServer.globalReadOnlyCell = connReadOnly;
topoServer.topoFactory = topoFactory;
topoServer.cells = new HashMap<>(16);
topoServer.cellsTopoConnMap = new HashMap<>(16);
topoServer.keyspaces = ConcurrentHashMap.newKeySet();
topoServer.cells = ConcurrentHashMap.newKeySet();
topoServer.localCell = "";
topoServer.serverAddress = serverAddress;
return topoServer;
}
Expand Down
Loading
Loading