-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Make actualDataNodes expose SPI that can define expression with custom rules and add GraalVM Truffle implementation #22899
Comments
When sharding by interval, it's really not easy for new users to understand the meaning of Groovy expressions. |
import org.apache.shardingsphere.infra.util.spi.lifecycle.SPIPostProcessor;
import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPI;
import java.util.Properties;
public interface AbstractActualdataNodes extends TypedSPI, SPIPostProcessor {
/**
*
* @return result real table list
*/
List<String> getActualDataNodes();
/**
* Get properties.
*
* @return properties
*/
Properties getProps();
}
rules:
- !SHARDING
tables:
t_order:
actualDataNodes:
type: SINGLE_TABLE
props:
table-prefix: t_order # The prefix of the real table
datetime-lower: 2022-10-01 # lower limit of time
datetime-upper: 2022-12-31 # time upper limit
datetime-pattern: yyyy-MM-dd # A string conforming to the format of java.time.format.DateTimeFormatter, used to convert datetime-lower and datetime-upper
table-suffix-pattern: _yyyyMM # The suffix of the real table, also follows the format of java.time.format.DateTimeFormatter
datetime-interval-amount: 1 # time interval
datetime-interval-unit: MONTHS # follow java.time.temporal.ChronoUnit
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAccessor;
import java.util.List;
import java.util.stream.LongStream;
public class DateTest {
@Test
void testDate() {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
TemporalAccessor start = dateTimeFormatter.parse("2022-10-01");
TemporalAccessor end = dateTimeFormatter.parse("2022-12-31");
if (!start.isSupported(ChronoField.NANO_OF_DAY) && start.isSupported(ChronoField.EPOCH_DAY)) {
LocalDate startTime = start.query(LocalDate::from);
LocalDate endTime = end.query(LocalDate::from);
List<String> actualDataNodes = LongStream.range(0, ChronoUnit.MONTHS.between(startTime, endTime.plusMonths(1)))
.mapToObj(startTime::plusMonths)
.map(localDate -> "t_order" + localDate.format(DateTimeFormatter.ofPattern("_yyyyMM")))
.toList();
assert actualDataNodes.equals(List.of("t_order_202210", "t_order_202211", "t_order_202212"));
}
}
} |
|
I think this could be also helpful to |
CREATE SHARDING TABLE RULE t_order_item (
DATANODES("ds_${0..1}.t_order_item_${0..1}"),
DATABASE_STRATEGY(TYPE="standard",SHARDING_COLUMN=user_id,SHARDING_ALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="ds_${user_id % 2}")))),
TABLE_STRATEGY(TYPE="standard",SHARDING_COLUMN=order_id,SHARDING_ALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="t_order_item_${order_id % 2}")))),
KEY_GENERATE_STRATEGY(COLUMN=another_id,TYPE(NAME="snowflake")),
AUDIT_STRATEGY (TYPE(NAME="DML_SHARDING_CONDITIONS"),ALLOW_HINT_DISABLE=true)
);
CREATE SHARDING TABLE RULE IF NOT EXISTS t_order_item (
DATANODES("ds_${0..1}.t_order_item_${0..1}"),
DATABASE_STRATEGY(TYPE="standard",SHARDING_COLUMN=user_id,SHARDING_ALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="ds_${user_id % 2}")))),
TABLE_STRATEGY(TYPE="standard",SHARDING_COLUMN=order_id,SHARDING_ALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="t_order_item_${order_id % 2}")))),
KEY_GENERATE_STRATEGY(COLUMN=another_id,TYPE(NAME="snowflake")),
AUDIT_STRATEGY (TYPE(NAME="DML_SHARDING_CONDITIONS"),ALLOW_HINT_DISABLE=true)
);
ALTER SHARDING TABLE RULE t_order_item (
DATANODES("ds_${0..3}.t_order_item${0..3}"),
DATABASE_STRATEGY(TYPE="standard",SHARDING_COLUMN=user_id,SHARDING_ALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="ds_${user_id % 4}")))),
TABLE_STRATEGY(TYPE="standard",SHARDING_COLUMN=order_id,SHARDING_ALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="t_order_item_${order_id % 4}")))),
KEY_GENERATE_STRATEGY(COLUMN=another_id,TYPE(NAME="snowflake")),
AUDIT_STRATEGY(TYPE(NAME="dml_sharding_conditions"),ALLOW_HINT_DISABLE=true)
);
SHOW SHARDING TABLE RULES;
SHOW SHARDING TABLE RULES FROM sharding_db; |
Oh, I'm sorry to hear that. Take care yourself in this tough time and hope you recover soon! |
|
|
Feature Request
For English only, other languages will not accept.
Please pay attention on issues you submitted, because we maybe need more details.
If no response anymore and we cannot make decision by current information, we will close it.
Please answer these questions before submitting your issue. Thanks!
Is your feature request related to a problem?
Describe the feature you would like.
Since it is impossible for most people to understand the syntax of Groovy in the first place, issues like The table generated by time fragments does not conform to the configuration logic #22884 can always be mentioned repeatedly, and the initiators of those issues have misunderstood the final results generated by certain Groovy expressions, similar results can actually be verified at https://groovyconsole.appspot.com/ .
If we can expose SPI for
actualDataNodes
, users can define special rules for expressions by implementing SPI, which will obviously help reduce some misunderstandings.The significance of this issue for Make ShardingSphere Proxy in GraalVM Native Image form available #21347 is that using
GroovyShell
directly means an additional class loader, which means that if single tests involvingGroovyShell
are directly executed under GraalVM Native Image, all are bound to fail. I've opened [GR-43010] Using Groovy classes under native-image results inUnsupportedFeatureError
oracle/graal#5522 and provided common unittests in ShardingSphere using GroovyShell.Once the SPI of actualDataNodes is exposed, we can introduce the implementation of GraalVM Truffle in the master branch of ShardingSphere, which should allow us to use JavaScript, Python, R, Ruby, and LLVM Language in
actualDataNodes
. And because of the use of GraalVM Truffle, we can imagine completing the nativeTest of ShardingSphere under GraalVM Native Image.For Groovy, I think it can be used unchanged, but we can also try to transfer the Groovy method call to Truffle's implementation of Espresso, which will distinguish the host JVM process from the guest JVM process to ensure that the nativeTest under GraalVM Native Image passes . But it is worth mentioning that Espresso is more limited than other Truffle API implementations.
I assume the YAML henceforth should be configured as such.
The text was updated successfully, but these errors were encountered: