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

"addTablet" filters the Tablet with MySQL port 0 #101

Merged
merged 1 commit into from
Jul 31, 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
12 changes: 11 additions & 1 deletion src/main/java/com/jd/jdbc/discovery/HealthCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.jd.jdbc.queryservice.IQueryService;
import com.jd.jdbc.sqlparser.support.logging.Log;
import com.jd.jdbc.sqlparser.support.logging.LogFactory;
import com.jd.jdbc.sqlparser.utils.StringUtils;
import com.jd.jdbc.topo.topoproto.TopoProto;
import com.jd.jdbc.util.threadpool.impl.VtHealthCheckExecutorService;
import io.netty.util.internal.StringUtil;
Expand Down Expand Up @@ -224,7 +225,8 @@ public List<Topodata.Tablet> getHealthyTablets(String keyspace) {
}

public void addTablet(Topodata.Tablet tablet) {
if (tablet.getPortMapMap().get("grpc") == null) {
if (checkTabletInfoMissing(tablet)) {
log.error("tablet Information missing,tablet = " + tablet);
return;
}
Query.Target target = Query.Target.newBuilder().setKeyspace(tablet.getKeyspace()).setShard(tablet.getShard()).setTabletType(tablet.getType()).build();
Expand Down Expand Up @@ -529,4 +531,12 @@ public void waitForAllServingTablets(IContext ctx, List<Query.Target> targetList
}
}
}

private boolean checkTabletInfoMissing(Topodata.Tablet tablet) {
return StringUtils.isEmpty(tablet.getKeyspace()) || StringUtils.isEmpty(tablet.getShard())
|| Objects.equals(Topodata.TabletType.UNRECOGNIZED, tablet.getType())
|| StringUtils.isEmpty(tablet.getHostname()) || StringUtils.isEmpty(tablet.getMysqlHostname())
|| tablet.getMysqlPort() == 0 || Objects.equals(Topodata.TabletAlias.getDefaultInstance(), tablet.getAlias())
|| tablet.getPortMapMap().get("grpc") == null || tablet.getPortMapMap().get("grpc") == 0;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/jd/jdbc/pool/HikariUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static HikariConfig getHikariConfig(Topodata.Tablet tablet, String user,
hikariConfig.setScheduledExecutor(HOUSEKEEPER_EXECUTOR);
hikariConfig.setMetricsTrackerFactory(METRICS_TRACKER_FACTORY);
if (logger.isDebugEnabled()) {
logger.debug("hikariConfig:nativeUrl=" + nativeUrl + " poolName=" + TopoProto.getPoolName(tablet) + " schema=" + realSchema);
logger.debug("hikariConfig:nativeUrl=" + nativeUrl + " poolName=" + TopoProto.getPoolName(tablet));
}
return hikariConfig;
}
Expand Down
79 changes: 77 additions & 2 deletions src/test/java/com/jd/jdbc/discovery/HealthCheckTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class HealthCheckTest extends TestSuite {

private final Map<String, Integer> portMap = new HashMap<>();

private int defaultMysqlPort = 3358;

@Rule
public GrpcCleanupRule grpcCleanup;

Expand Down Expand Up @@ -434,7 +436,7 @@ public void testHealthCheckOnNextAfterRemove() throws IOException, InterruptedEx
}

@Test
public void teestHealthCheckTimeout() throws IOException, InterruptedException {
public void testHealthCheckTimeout() throws IOException, InterruptedException {
printComment("7. HealthCheck Test when health check timeout");
printComment("a. Get Health");
HealthCheck hc = getHealthCheck();
Expand Down Expand Up @@ -765,6 +767,78 @@ public void testUnhealthyReplicaAsSecondsBehind() throws IOException, Interrupte
printOk();
}

@Test
public void testMysqlPort0to3358() throws IOException, InterruptedException {
printComment("14. HealthCheck Test in Tablet MySQL port changed from 0 to 3358");
printComment("a. Get Health");
HealthCheck hc = getHealthCheck();

printComment("b. Add a no-serving replica Tablet (MysqlPort = 0)");
defaultMysqlPort = 0;
MockTablet mockTablet = buildMockTablet("cell1", 0, "a", "k", "s", portMap, Topodata.TabletType.REPLICA);
hc.addTablet(mockTablet.getTablet());
Thread.sleep(200);
Assert.assertEquals("Wrong Tablet data", 0, hc.getHealthByAliasCopy().size());
Assert.assertEquals("Wrong Healthy Tablet data", 0, hc.getHealthyCopy().size());

printComment("c. replace a no-serving replica Tablet (MysqlPort = 3358)");
defaultMysqlPort = 3358;
Topodata.Tablet tablet = buildTablet("cell1", 0, "a", "k", "s", portMap, Topodata.TabletType.REPLICA);
hc.replaceTablet(mockTablet.getTablet(), tablet);
Thread.sleep(200);
Assert.assertEquals("Wrong Tablet data", 1, hc.getHealthByAliasCopy().size());
Assert.assertEquals("Wrong Healthy Tablet data", 0, hc.getHealthyCopy().size());

printComment("d. Modify the status of REPLICA Tablet to serving");
sendOnNextMessage(mockTablet, Topodata.TabletType.REPLICA, true, 0, 0.5, 10);
Thread.sleep(200);

Assert.assertEquals("Wrong Tablet data", 1, hc.getHealthByAliasCopy().size());
Assert.assertEquals("Wrong Healthy Tablet data", 1, hc.getHealthyCopy().size());
List<TabletHealthCheck> hcList = hc.getHealthyTabletStats(createTarget(Topodata.TabletType.REPLICA));
Assert.assertEquals("Wrong number of healthy replica tablets", 1, hcList.size());

closeQueryService(mockTablet);
printOk();
}

@Test
public void testMysqlPort3358to0() throws IOException, InterruptedException {
printComment("15. HealthCheck Test in Tablet MySQL port changed from 3358 to 0");
printComment("a. Get Health");
HealthCheck hc = getHealthCheck();

printComment("b. Add a no-serving Tablet(MysqlPort = 3358)");
MockTablet mockTablet = buildMockTablet("cell", 0, "a", "k", "s", portMap, Topodata.TabletType.REPLICA);
hc.addTablet(mockTablet.getTablet());

Assert.assertEquals("Wrong Tablet data", 1, hc.getHealthByAliasCopy().size());
Assert.assertEquals("Wrong Healthy Tablet data", 0, hc.getHealthyCopy().size());
Thread.sleep(200);

printComment("c. Modify the status of Tablet to serving");
sendOnNextMessage(mockTablet, Topodata.TabletType.REPLICA, true, 0, 0.5, 1);

Thread.sleep(200);

Assert.assertEquals("Wrong Tablet data", 1, hc.getHealthByAliasCopy().size());
Assert.assertEquals("Wrong Healthy Tablet data", 1, hc.getHealthyCopy().size());
List<TabletHealthCheck> hcList = hc.getHealthyTabletStats(createTarget(Topodata.TabletType.REPLICA));
Assert.assertEquals("Wrong number of healthy replica tablets", 1, hcList.size());

printComment("d. replace a no-serving replica Tablet (MysqlPort = 0)");
defaultMysqlPort = 0;
Topodata.Tablet tablet = buildTablet("cell1", 0, "a", "k", "s", portMap, Topodata.TabletType.REPLICA);
hc.replaceTablet(mockTablet.getTablet(), tablet);
Thread.sleep(6000);

Assert.assertEquals("Wrong Tablet data", 0, hc.getHealthByAliasCopy().size());
Assert.assertEquals("Wrong Healthy Tablet data", 0, hc.getHealthyCopy().size());

closeQueryService(mockTablet);
printOk();
}

private void startWatchTopo(String keyspaceName, TopoServer topoServer, String... cells) {
for (String cell : cells) {
TopologyWatcherManager.INSTANCE.startWatch(globalContext, topoServer, cell, keyspaceName);
Expand Down Expand Up @@ -818,7 +892,8 @@ private MockTablet buildMockTablet(String cell, Integer uid, String hostName, St
private Topodata.Tablet buildTablet(String cell, Integer uid, String hostName, String keyspaceName, String shard, Map<String, Integer> portMap, Topodata.TabletType type) {
Topodata.TabletAlias tabletAlias = Topodata.TabletAlias.newBuilder().setCell(cell).setUid(uid).build();

Topodata.Tablet.Builder tabletBuilder = Topodata.Tablet.newBuilder().setHostname(hostName).setAlias(tabletAlias).setKeyspace(keyspaceName).setShard(shard).setType(type);
Topodata.Tablet.Builder tabletBuilder = Topodata.Tablet.newBuilder()
.setHostname(hostName).setAlias(tabletAlias).setKeyspace(keyspaceName).setShard(shard).setType(type).setMysqlHostname(hostName).setMysqlPort(defaultMysqlPort);
for (Map.Entry<String, Integer> portEntry : portMap.entrySet()) {
tabletBuilder.putPortMap(portEntry.getKey(), portEntry.getValue());
}
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/com/jd/jdbc/queryservice/MockQueryServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ private void notifyAll(HealthCheckMessage message) {
break;
default:
observers.forEach(observer -> {
observer.onNext(message.getMessage());
try {
observer.onNext(message.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
});
System.out.println("server: receive message: " + message.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ public void init() {
vtMaximumPoolSize = (random.nextInt(150) + 1) + "";
vtConnectionTimeout = (random.nextInt(50000) + 250) + "";
vtIdleTimeout = (random.nextInt(600000) + 10000) + "";

propsUrl =
"vtMinimumIdle=" + vtMinimumIdle + ";vtMaximumPoolSize=" + vtMaximumPoolSize + ";vtConnectionInitSql=select 1;vtConnectionTestQuery=select 1;vtConnectionTimeout=" + vtConnectionTimeout +
";vtIdleTimeout=" + vtIdleTimeout;

propsUrl = "vtMinimumIdle=" + vtMinimumIdle + ";vtMaximumPoolSize=" + vtMaximumPoolSize + ";vtConnectionInitSql=select 1;vtConnectionTestQuery=select 1;vtConnectionTimeout=" + vtConnectionTimeout + ";vtIdleTimeout=" + vtIdleTimeout;
}

public void test0() throws NoSuchFieldException, IllegalAccessException {
Expand All @@ -108,7 +104,6 @@ public void test0() throws NoSuchFieldException, IllegalAccessException {
Assert.assertEquals("select 1", hikariConfig.getConnectionTestQuery() + "");
Assert.assertEquals(vtConnectionTimeout, hikariConfig.getConnectionTimeout() + "");
Assert.assertEquals(vtIdleTimeout, hikariConfig.getIdleTimeout() + "");

}

public void testInit() {
Expand Down Expand Up @@ -143,7 +138,6 @@ public void testHikari() throws Exception {
conn.close();
}


@Test
public void testDruid1() throws Exception {
print();
Expand Down Expand Up @@ -183,7 +177,6 @@ public void testDruid2() throws Exception {
connection.close();
}


@Test
public void testdbcp() throws Exception {
print();
Expand All @@ -201,7 +194,6 @@ public void testdbcp() throws Exception {
connection.close();
}


@Test
public void testc3p0() throws Exception {
print();
Expand Down Expand Up @@ -256,8 +248,7 @@ public void testDefaultConnectionPoolSize() throws NoSuchFieldException, Illegal
printNormal("testDefaultConnectionPoolSize>>>>>");
}

public void checkInnerPoolSize(String url, Topodata.TabletType type, int expectedMin, int expectedMax)
throws SQLException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InterruptedException {
public void checkInnerPoolSize(String url, Topodata.TabletType type, int expectedMin, int expectedMax) throws SQLException, NoSuchFieldException, IllegalAccessException {
printInfo("url: " + url);

Connection conn = DriverManager.getConnection(url);
Expand Down
Loading