Skip to content

Commit

Permalink
Add sharding table rule and test (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhfeng authored Oct 21, 2021
1 parent 3864a82 commit 8b6a219
Show file tree
Hide file tree
Showing 10 changed files with 428 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.jboss.jandex.DotName;

import io.quarkiverse.shardingsphere.jdbc.ShardingsphereConfig;
import io.quarkiverse.shardingsphere.jdbc.ShardingsphereJdbcRecorder;
import io.quarkus.agroal.spi.JdbcDataSourceBuildItem;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
Expand All @@ -28,9 +29,10 @@ FeatureBuildItem feature() {
}

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
@Record(ExecutionTime.RUNTIME_INIT)
void generateShardingSphereDataSource(ShardingsphereJdbcRecorder recorder,
List<JdbcDataSourceBuildItem> dataSourceBuildItems,
ShardingsphereConfig config,
BuildProducer<SyntheticBeanBuildItem> syntheticBeanBuildItemBuildProducer) {
List<String> dataSources = dataSourceBuildItems
.stream().filter(ds -> !ds.isDefault()).map(ds -> ds.getName()).collect(Collectors.toList());
Expand All @@ -41,7 +43,7 @@ void generateShardingSphereDataSource(ShardingsphereJdbcRecorder recorder,
.scope(Singleton.class)
.setRuntimeInit()
.unremovable()
.supplier(recorder.shardingsphereDataSourceSupplier(dataSources));
.supplier(recorder.shardingsphereDataSourceSupplier(config, dataSources));

syntheticBeanBuildItemBuildProducer.produce(configurator.done());
}
Expand Down
4 changes: 4 additions & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.quarkiverse.shardingsphere.jdbc.it;

public class Account {
private int account_id;
private int user_id;
private String status;

public int getAccount_id() {
return account_id;
}

public void setAccount_id(int account_id) {
this.account_id = account_id;
}

public int getUser_id() {
return user_id;
}

public void setUser_id(int user_id) {
this.user_id = user_id;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,91 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 io.quarkiverse.shardingsphere.jdbc.it;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.inject.Named;
import javax.sql.DataSource;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;

@Path("/shardingsphere-jdbc")
@ApplicationScoped
public class ShardingsphereJdbcResource {
// add some rest methods here
@Inject
DataSource dataSource;

@Named("ds")
DataSource h2_ds;

void onStart(@Observes StartupEvent ev) throws Exception {
createAccountTable();
}

void onStop(@Observes ShutdownEvent ev) throws Exception {
dropAccountTable();
}

private void createAccountTable() throws SQLException {
String sql = "CREATE TABLE t_account (account_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (account_id))";
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
statement.executeUpdate(sql);
}
}

private void dropAccountTable() throws SQLException {
String sql = "DROP TABLE t_account";
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
statement.executeUpdate(sql);
}
}

@POST
@Path("/account")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public Integer createAccount(Account account) throws Exception {
int result = 0;

String sql = "INSERT INTO t_account (account_id, user_id, status) VALUES (?, ?, ?)";
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setInt(1, account.getAccount_id());
statement.setInt(2, account.getUser_id());
statement.setString(3, account.getStatus());
result = statement.executeUpdate();
}

return result;
}

@GET
public String hello() {
return "Hello shardingsphere-jdbc";
@Path("/account/{tbl}")
public Integer count(@PathParam("tbl") String table) throws Exception {
String sql = "SELECT COUNT(*) from " + table;
try (Connection connection = h2_ds.getConnection();
Statement statement = connection.createStatement()) {
ResultSet result = statement.executeQuery(sql);
if (result.next()) {
return result.getInt(1);
} else {
return -1;
}
}
}
}
}
40 changes: 33 additions & 7 deletions integration-tests/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
# H2 DataSource
quarkus.datasource.ds1.db-kind=h2
quarkus.datasource.ds1.username=sa
quarkus.datasource.ds1.jdbc.url=jdbc:h2:tcp://localhost/ds1
quarkus.datasource.ds.db-kind=h2
quarkus.datasource.ds.username=sa
quarkus.datasource.ds.jdbc.url=jdbc:h2:mem:ds

quarkus.datasource.ds2.db-kind=h2
quarkus.datasource.ds2.username=sa
quarkus.datasource.ds2.jdbc.url=jdbc:h2:tcp://localhost/ds2
# ShardingSphere sharding tables
quarkus.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=ds.t_order_$->{0..1}
quarkus.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column=order_id
quarkus.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name=t-order-inline
quarkus.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.column=order_id
quarkus.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.key-generator-name=snowflake

# Sharding Sphere
quarkus.shardingsphere.rules.sharding.tables.t_order_item.actual-data-nodes=ds.t_order_item_$->{0..1}
quarkus.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-column=order_id
quarkus.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-algorithm-name=t-order-item-inline
quarkus.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.column=order_item_id
quarkus.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.key-generator-name=snowflake

quarkus.shardingsphere.rules.sharding.tables.t_account.actual-data-nodes=ds.t_account_$->{0..1}
quarkus.shardingsphere.rules.sharding.tables.t_account.table-strategy.standard.sharding-algorithm-name=t-account-inline
quarkus.shardingsphere.rules.sharding.tables.t_account.key-generate-strategy.column=account_id
quarkus.shardingsphere.rules.sharding.tables.t_account.key-generate-strategy.key-generator-name=snowflake

quarkus.shardingsphere.rules.sharding.default-sharding-column=account_id
quarkus.shardingsphere.rules.sharding.binding-tables=t_order,t_order_item
quarkus.shardingsphere.rules.sharding.broadcast-tables=t_address

quarkus.shardingsphere.rules.sharding.sharding-algorithms.t-order-inline.type=INLINE
quarkus.shardingsphere.rules.sharding.sharding-algorithms.t-order-inline.props.algorithm-expression=t_order_$->{order_id % 2}
quarkus.shardingsphere.rules.sharding.sharding-algorithms.t-order-item-inline.type=INLINE
quarkus.shardingsphere.rules.sharding.sharding-algorithms.t-order-item-inline.props.algorithm-expression=t_order_item_$->{order_id % 2}
quarkus.shardingsphere.rules.sharding.sharding-algorithms.t-account-inline.type=INLINE
quarkus.shardingsphere.rules.sharding.sharding-algorithms.t-account-inline.props.algorithm-expression=t_account_$->{account_id % 2}

quarkus.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
quarkus.shardingsphere.rules.sharding.key-generators.snowflake.props.worker-id=123
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,29 @@
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.h2.H2DatabaseTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;

@QuarkusTest
@QuarkusTestResource(H2DatabaseTestResource.class)
public class ShardingsphereJdbcResourceTest {

@Test
public void testHelloEndpoint() {
public void test() {
given()
.when().get("/shardingsphere-jdbc")
.body("{\"account_id\":1, \"user_id\":1, \"status\":\"true\"}").contentType(ContentType.JSON)
.post("/shardingsphere-jdbc/account")
.then()
.statusCode(200)
.body(is("Hello shardingsphere-jdbc"));
.body(is("1"));

given().get("/shardingsphere-jdbc/account/t_account_0")
.then()
.statusCode(200)
.body(is("0"));

given().get("/shardingsphere-jdbc/account/t_account_1")
.then()
.statusCode(200)
.body(is("1"));
}
}
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.version>2.3.0.Final</quarkus.version>
<antlr.version>4.7.2</antlr.version> <!-- it needs to align with the antlr4-runtime version in shardingsphere -->
<shardingsphere.version>5.0.0-beta</shardingsphere.version>
</properties>
<dependencyManagement>
Expand All @@ -40,6 +41,17 @@
<artifactId>shardingsphere-jdbc-core</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-infra-common</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>${antlr.version}</version>
</dependency>

</dependencies>
</dependencyManagement>
<build>
Expand Down
4 changes: 4 additions & 0 deletions runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-infra-common</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Loading

0 comments on commit 8b6a219

Please sign in to comment.