forked from apache/rocketmq
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUE apache#4384] Add RetryPolicy interface
- Loading branch information
Showing
8 changed files
with
306 additions
and
77 deletions.
There are no files selected for viewing
60 changes: 0 additions & 60 deletions
60
common/src/main/java/org/apache/rocketmq/common/subscription/CustomizedRetryPolicy.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
common/src/main/java/org/apache/rocketmq/common/subscription/LegacyRetryPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* 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.rocketmq.common.subscription; | ||
|
||
import com.google.common.base.MoreObjects; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* LegacyRetryPolicy is aim to make group's behavior compatible with messageDelayLevel | ||
* | ||
* @see <a href="https://github.com/apache/rocketmq/blob/3bd4b2b2f61a824196f19b03146e2c929c62777b/store/src/main/java/org/apache/rocketmq/store/config/MessageStoreConfig.java#L137">org.apache.rocketmq.store.config.MessageStoreConfig</a> | ||
*/ | ||
public class LegacyRetryPolicy implements RetryPolicy { | ||
// 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h | ||
private long[] next = new long[] { | ||
TimeUnit.SECONDS.toMillis(1), | ||
TimeUnit.SECONDS.toMillis(5), | ||
TimeUnit.SECONDS.toMillis(10), | ||
TimeUnit.SECONDS.toMillis(30), | ||
TimeUnit.MINUTES.toMillis(1), | ||
TimeUnit.MINUTES.toMillis(2), | ||
TimeUnit.MINUTES.toMillis(3), | ||
TimeUnit.MINUTES.toMillis(4), | ||
TimeUnit.MINUTES.toMillis(5), | ||
TimeUnit.MINUTES.toMillis(6), | ||
TimeUnit.MINUTES.toMillis(7), | ||
TimeUnit.MINUTES.toMillis(8), | ||
TimeUnit.MINUTES.toMillis(9), | ||
TimeUnit.MINUTES.toMillis(10), | ||
TimeUnit.MINUTES.toMillis(20), | ||
TimeUnit.MINUTES.toMillis(30), | ||
TimeUnit.HOURS.toMillis(1), | ||
TimeUnit.HOURS.toMillis(2) | ||
}; | ||
|
||
public long[] getNext() { | ||
return next; | ||
} | ||
|
||
public void setNext(long[] next) { | ||
this.next = next; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("next", next) | ||
.toString(); | ||
} | ||
|
||
/** | ||
* Index = reconsumeTimes + 2 is compatible logic, cause old delayLevelTable starts from index 1, | ||
* and old index is reconsumeTime + 3 | ||
* | ||
* @param reconsumeTimes Message reconsumeTimes {@link org.apache.rocketmq.common.message.MessageExt#getReconsumeTimes} | ||
* @param timeUnit {@link TimeUnit} | ||
* @see <a href="https://github.com/apache/rocketmq/blob/3bddd514646826253a239f95959c14840a87034a/broker/src/main/java/org/apache/rocketmq/broker/processor/AbstractSendMessageProcessor.java#L210">org.apache.rocketmq.broker.processor.AbstractSendMessageProcessor</a> | ||
* @see <a href="https://github.com/apache/rocketmq/blob/3bddd514646826253a239f95959c14840a87034a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java#L242">org.apache.rocketmq.store.DefaultMessageStore</a> | ||
*/ | ||
@Override | ||
public long nextDelayDuration(int reconsumeTimes, TimeUnit timeUnit) { | ||
if (reconsumeTimes < 0) { | ||
reconsumeTimes = 0; | ||
} | ||
int index = reconsumeTimes + 2; | ||
if (index >= next.length) { | ||
index = next.length - 1; | ||
} | ||
long nextDelayDurationInMillis = next[index]; | ||
return timeUnit.convert(nextDelayDurationInMillis, TimeUnit.MILLISECONDS); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
common/src/main/java/org/apache/rocketmq/common/subscription/RetryPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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.rocketmq.common.subscription; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public interface RetryPolicy { | ||
/** | ||
* Compute message's next delay duration by specify reconsumeTimes | ||
* | ||
* @param reconsumeTimes Message reconsumeTimes | ||
* @param timeUnit Given timeUnit | ||
* @return Message's nextDelayDuration in given timeUnit | ||
*/ | ||
long nextDelayDuration(int reconsumeTimes, TimeUnit timeUnit); | ||
} |
44 changes: 44 additions & 0 deletions
44
common/src/test/java/org/apache/rocketmq/common/subscription/ExponentialRetryPolicyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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.rocketmq.common.subscription; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ExponentialRetryPolicyTest { | ||
|
||
@Test | ||
public void testNextDelayDuration() { | ||
ExponentialRetryPolicy exponentialRetryPolicy = new ExponentialRetryPolicy(); | ||
long actual = exponentialRetryPolicy.nextDelayDuration(0, TimeUnit.MILLISECONDS); | ||
assertThat(actual).isEqualTo(TimeUnit.SECONDS.toMillis(5)); | ||
actual = exponentialRetryPolicy.nextDelayDuration(10, TimeUnit.MILLISECONDS); | ||
assertThat(actual).isEqualTo(TimeUnit.SECONDS.toMillis(1024 * 5)); | ||
} | ||
|
||
@Test | ||
public void testNextDelayDurationOutOfRange() { | ||
ExponentialRetryPolicy exponentialRetryPolicy = new ExponentialRetryPolicy(); | ||
long actual = exponentialRetryPolicy.nextDelayDuration(-1, TimeUnit.MILLISECONDS); | ||
assertThat(actual).isEqualTo(TimeUnit.SECONDS.toMillis(5)); | ||
actual = exponentialRetryPolicy.nextDelayDuration(100, TimeUnit.MILLISECONDS); | ||
assertThat(actual).isEqualTo(TimeUnit.HOURS.toMillis(2)); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
common/src/test/java/org/apache/rocketmq/common/subscription/GroupRetryPolicyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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.rocketmq.common.subscription; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class GroupRetryPolicyTest { | ||
|
||
@Test | ||
public void testGetRetryPolicy() { | ||
GroupRetryPolicy groupRetryPolicy = new GroupRetryPolicy(); | ||
RetryPolicy retryPolicy = groupRetryPolicy.getRetryPolicy(); | ||
assertThat(retryPolicy).isInstanceOf(LegacyRetryPolicy.class); | ||
groupRetryPolicy.setType(GroupRetryPolicyType.EXPONENTIAL); | ||
retryPolicy = groupRetryPolicy.getRetryPolicy(); | ||
assertThat(retryPolicy).isInstanceOf(LegacyRetryPolicy.class); | ||
|
||
groupRetryPolicy.setType(GroupRetryPolicyType.CUSTOMIZED); | ||
groupRetryPolicy.setLegacyRetryPolicy(new LegacyRetryPolicy()); | ||
retryPolicy = groupRetryPolicy.getRetryPolicy(); | ||
assertThat(retryPolicy).isInstanceOf(LegacyRetryPolicy.class); | ||
|
||
groupRetryPolicy.setType(GroupRetryPolicyType.EXPONENTIAL); | ||
groupRetryPolicy.setExponentialRetryPolicy(new ExponentialRetryPolicy()); | ||
retryPolicy = groupRetryPolicy.getRetryPolicy(); | ||
assertThat(retryPolicy).isInstanceOf(ExponentialRetryPolicy.class); | ||
|
||
groupRetryPolicy.setType(null); | ||
retryPolicy = groupRetryPolicy.getRetryPolicy(); | ||
assertThat(retryPolicy).isInstanceOf(LegacyRetryPolicy.class); | ||
} | ||
} |
Oops, something went wrong.