You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2020/04/27 04:28:09 UTC
[GitHub] [shardingsphere] tristaZero opened a new pull request #5333: Create Datetime sharding algorithm
tristaZero opened a new pull request #5333:
URL: https://github.com/apache/shardingsphere/pull/5333
Ref #5282
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] SanmuWangZJU commented on a change in pull request #5333: Create Datetime sharding algorithm
Posted by GitBox <gi...@apache.org>.
SanmuWangZJU commented on a change in pull request #5333:
URL: https://github.com/apache/shardingsphere/pull/5333#discussion_r415546332
##########
File path: sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/strategy/algorithm/sharding/DatetimeShardingAlgorithm.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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 org.apache.shardingsphere.core.strategy.algorithm.sharding;
+
+import com.google.common.base.Preconditions;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
+import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;
+import org.apache.shardingsphere.api.sharding.standard.StandardShardingAlgorithm;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Collection;
+import java.util.Date;
+import java.util.LinkedHashSet;
+import java.util.Properties;
+
+/**
+ * Datetime sharding algorithm.
+ *
+ * <p>Shard by `y = floor(x/v)` algorithm, which means y begins from 0.
+ * v is `PARTITION_VOLUME`, and the minimum time unit is 1 sec.
+ * `SINCE_DATETIME` decides the beginning datetime to shard. </p>
+ */
+public final class DatetimeShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>> {
+
+ private static final String PARTITION_VOLUME = "partition.volume";
+
+ private static final String SINCE_DATETIME = "since.datetime";
+
+ private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Review comment:
`SimpleDateFormat` is not thread safe, is it possible to call `SimpleDateFormat.parse()` concurrently? if so, perhaps use `LocalDateTime` and `DateTimeFormatter` is better
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] tristaZero commented on a change in pull request #5333: Create Datetime sharding algorithm
Posted by GitBox <gi...@apache.org>.
tristaZero commented on a change in pull request #5333:
URL: https://github.com/apache/shardingsphere/pull/5333#discussion_r415588422
##########
File path: sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/strategy/algorithm/sharding/DatetimeShardingAlgorithm.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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 org.apache.shardingsphere.core.strategy.algorithm.sharding;
+
+import com.google.common.base.Preconditions;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
+import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;
+import org.apache.shardingsphere.api.sharding.standard.StandardShardingAlgorithm;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Collection;
+import java.util.Date;
+import java.util.LinkedHashSet;
+import java.util.Properties;
+
+/**
+ * Datetime sharding algorithm.
+ *
+ * <p>Shard by `y = floor(x/v)` algorithm, which means y begins from 0.
+ * v is `PARTITION_VOLUME`, and the minimum time unit is 1 sec.
+ * `SINCE_DATETIME` decides the beginning datetime to shard. </p>
+ */
+public final class DatetimeShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>> {
+
+ private static final String PARTITION_VOLUME = "partition.volume";
+
+ private static final String SINCE_DATETIME = "since.datetime";
+
+ private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Review comment:
@ SanmuWangZJU
> `SimpleDateFormat` is not thread safe, is it possible to call `SimpleDateFormat.parse()` concurrently? if so, perhaps use `LocalDateTime` and `DateTimeFormatter` is better
Hi, Thanks for your suggestion which helps raize #5341, welcome your review and comments.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] coveralls commented on pull request #5333: Create Datetime sharding algorithm
Posted by GitBox <gi...@apache.org>.
coveralls commented on pull request #5333:
URL: https://github.com/apache/shardingsphere/pull/5333#issuecomment-619716332
## Pull Request Test Coverage Report for [Build 11274](https://coveralls.io/builds/30358017)
* **30** of **39** **(76.92%)** changed or added relevant lines in **1** file are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage increased (+**0.04%**) to **57.152%**
---
| Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
| :-----|--------------|--------|---: |
| [sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/strategy/algorithm/sharding/DatetimeShardingAlgorithm.java](https://coveralls.io/builds/30358017/source?filename=sharding-core%2Fsharding-core-common%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fshardingsphere%2Fcore%2Fstrategy%2Falgorithm%2Fsharding%2FDatetimeShardingAlgorithm.java#L62) | 30 | 39 | 76.92%
<!-- | **Total:** | **30** | **39** | **76.92%** | -->
| Totals | [](https://coveralls.io/builds/30358017) |
| :-- | --: |
| Change from base [Build 11273](https://coveralls.io/builds/30356822): | 0.04% |
| Covered Lines: | 11887 |
| Relevant Lines: | 20799 |
---
##### 💛 - [Coveralls](https://coveralls.io)
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] tristaZero commented on a change in pull request #5333: Create Datetime sharding algorithm
Posted by GitBox <gi...@apache.org>.
tristaZero commented on a change in pull request #5333:
URL: https://github.com/apache/shardingsphere/pull/5333#discussion_r415530449
##########
File path: sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/strategy/algorithm/sharding/DatetimeShardingAlgorithm.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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 org.apache.shardingsphere.core.strategy.algorithm.sharding;
+
+import com.google.common.base.Preconditions;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
+import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;
+import org.apache.shardingsphere.api.sharding.standard.StandardShardingAlgorithm;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Collection;
+import java.util.Date;
+import java.util.LinkedHashSet;
+import java.util.Properties;
+
+/**
+ * Datetime sharding algorithm.
+ *
+ * <p>Shard by `y = floor(x/v)` algorithm, which means y begins from 0.
+ * v is `PARTITION_VOLUME`, and the minimum time unit is 1 sec.
+ * `SINCE_DATETIME` decides the beginning datetime to shard. </p>
+ */
+public final class DatetimeShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>> {
+
+ private static final String PARTITION_VOLUME = "partition.volume";
+
+ private static final String SINCE_DATETIME = "since.datetime";
+
+ private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+ private static final int SECOND_UNIT = 1000;
+
+ @Getter
+ @Setter
+ private Properties properties = new Properties();
+
+ @Override
+ public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Comparable<?>> shardingValue) {
+ checkProperties();
+ for (String each : availableTargetNames) {
+ if (each.endsWith(doSharding(parseDate(shardingValue.getValue())))) {
+ return each;
+ }
+ }
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Collection<String> doSharding(final Collection<String> availableTargetNames, final RangeShardingValue<Comparable<?>> shardingValue) {
+ checkProperties();
+ Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
+ for (long i = parseDate(shardingValue.getValueRange().lowerEndpoint()); i <= parseDate(shardingValue.getValueRange().upperEndpoint()); i++) {
+ for (String each : availableTargetNames) {
+ if (each.endsWith(doSharding(i))) {
+ result.add(each);
+ }
+ }
+ if (result.size() == availableTargetNames.size()) {
+ return result;
+ }
+ }
Review comment:
I will consider it in my next PR
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] codecov-io commented on pull request #5333: Create Datetime sharding algorithm
Posted by GitBox <gi...@apache.org>.
codecov-io commented on pull request #5333:
URL: https://github.com/apache/shardingsphere/pull/5333#issuecomment-619715387
# [Codecov](https://codecov.io/gh/apache/shardingsphere/pull/5333?src=pr&el=h1) Report
> Merging [#5333](https://codecov.io/gh/apache/shardingsphere/pull/5333?src=pr&el=desc) into [master](https://codecov.io/gh/apache/shardingsphere/commit/548d3e3233edbc604c6564e4c5ea0e6a76c34ba8&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `61.53%`.
[](https://codecov.io/gh/apache/shardingsphere/pull/5333?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #5333 +/- ##
============================================
+ Coverage 53.60% 53.61% +0.01%
Complexity 412 412
============================================
Files 1158 1159 +1
Lines 20760 20799 +39
Branches 3752 3761 +9
============================================
+ Hits 11128 11152 +24
- Misses 8916 8925 +9
- Partials 716 722 +6
```
| [Impacted Files](https://codecov.io/gh/apache/shardingsphere/pull/5333?src=pr&el=tree) | Coverage Δ | Complexity Δ | |
|---|---|---|---|
| [.../algorithm/sharding/DatetimeShardingAlgorithm.java](https://codecov.io/gh/apache/shardingsphere/pull/5333/diff?src=pr&el=tree#diff-c2hhcmRpbmctY29yZS9zaGFyZGluZy1jb3JlLWNvbW1vbi9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvc2hhcmRpbmdzcGhlcmUvY29yZS9zdHJhdGVneS9hbGdvcml0aG0vc2hhcmRpbmcvRGF0ZXRpbWVTaGFyZGluZ0FsZ29yaXRobS5qYXZh) | `61.53% <61.53%> (ø)` | `0.00 <0.00> (?)` | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/shardingsphere/pull/5333?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/shardingsphere/pull/5333?src=pr&el=footer). Last update [548d3e3...0c81a16](https://codecov.io/gh/apache/shardingsphere/pull/5333?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] terrymanu commented on a change in pull request #5333: Create Datetime sharding algorithm
Posted by GitBox <gi...@apache.org>.
terrymanu commented on a change in pull request #5333:
URL: https://github.com/apache/shardingsphere/pull/5333#discussion_r415502763
##########
File path: sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/strategy/algorithm/sharding/DatetimeShardingAlgorithm.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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 org.apache.shardingsphere.core.strategy.algorithm.sharding;
+
+import com.google.common.base.Preconditions;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
+import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;
+import org.apache.shardingsphere.api.sharding.standard.StandardShardingAlgorithm;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Collection;
+import java.util.Date;
+import java.util.LinkedHashSet;
+import java.util.Properties;
+
+/**
+ * Datetime sharding algorithm.
+ *
+ * <p>Shard by `y = floor(x/v)` algorithm, which means y begins from 0.
+ * v is `PARTITION_VOLUME`, and the minimum time unit is 1 sec.
+ * `SINCE_DATETIME` decides the beginning datetime to shard. </p>
+ */
+public final class DatetimeShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>> {
+
+ private static final String PARTITION_VOLUME = "partition.volume";
+
+ private static final String SINCE_DATETIME = "since.datetime";
Review comment:
use `epoch` maybe better than `since.datetime`.
Just make consist with `SnowflakeKeyGenerateAlgorithm`
##########
File path: sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/strategy/algorithm/sharding/DatetimeShardingAlgorithm.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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 org.apache.shardingsphere.core.strategy.algorithm.sharding;
+
+import com.google.common.base.Preconditions;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
+import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;
+import org.apache.shardingsphere.api.sharding.standard.StandardShardingAlgorithm;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Collection;
+import java.util.Date;
+import java.util.LinkedHashSet;
+import java.util.Properties;
+
+/**
+ * Datetime sharding algorithm.
+ *
+ * <p>Shard by `y = floor(x/v)` algorithm, which means y begins from 0.
+ * v is `PARTITION_VOLUME`, and the minimum time unit is 1 sec.
+ * `SINCE_DATETIME` decides the beginning datetime to shard. </p>
+ */
+public final class DatetimeShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>> {
+
+ private static final String PARTITION_VOLUME = "partition.volume";
Review comment:
use `partition.seconds` maybe better than `partition.volume`.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] terrymanu commented on a change in pull request #5333: Create Datetime sharding algorithm
Posted by GitBox <gi...@apache.org>.
terrymanu commented on a change in pull request #5333:
URL: https://github.com/apache/shardingsphere/pull/5333#discussion_r415512924
##########
File path: sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/strategy/algorithm/sharding/DatetimeShardingAlgorithm.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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 org.apache.shardingsphere.core.strategy.algorithm.sharding;
+
+import com.google.common.base.Preconditions;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
+import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;
+import org.apache.shardingsphere.api.sharding.standard.StandardShardingAlgorithm;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Collection;
+import java.util.Date;
+import java.util.LinkedHashSet;
+import java.util.Properties;
+
+/**
+ * Datetime sharding algorithm.
+ *
+ * <p>Shard by `y = floor(x/v)` algorithm, which means y begins from 0.
+ * v is `PARTITION_VOLUME`, and the minimum time unit is 1 sec.
+ * `SINCE_DATETIME` decides the beginning datetime to shard. </p>
+ */
+public final class DatetimeShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>> {
+
+ private static final String PARTITION_VOLUME = "partition.volume";
+
+ private static final String SINCE_DATETIME = "since.datetime";
+
+ private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+ private static final int SECOND_UNIT = 1000;
+
+ @Getter
+ @Setter
+ private Properties properties = new Properties();
+
+ @Override
+ public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Comparable<?>> shardingValue) {
+ checkProperties();
+ for (String each : availableTargetNames) {
+ if (each.endsWith(doSharding(parseDate(shardingValue.getValue())))) {
+ return each;
+ }
+ }
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Collection<String> doSharding(final Collection<String> availableTargetNames, final RangeShardingValue<Comparable<?>> shardingValue) {
+ checkProperties();
+ Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
+ for (long i = parseDate(shardingValue.getValueRange().lowerEndpoint()); i <= parseDate(shardingValue.getValueRange().upperEndpoint()); i++) {
+ for (String each : availableTargetNames) {
+ if (each.endsWith(doSharding(i))) {
+ result.add(each);
+ }
+ }
+ if (result.size() == availableTargetNames.size()) {
+ return result;
+ }
+ }
Review comment:
It is pretty slow if loop for every second, could we consider about increase for one partition range once?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org