Skip to content

Commit

Permalink
[ISSUE apache#4384] Expand RocketMQ Topic/Group attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
xdkxlk authored and drpmma committed May 27, 2022
1 parent 52e059e commit 49585ef
Show file tree
Hide file tree
Showing 10 changed files with 479 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/
package org.apache.rocketmq.common;

import org.apache.rocketmq.common.attribute.Attribute;
import org.apache.rocketmq.common.attribute.EnumAttribute;

import java.util.HashMap;
import java.util.Map;
import org.apache.rocketmq.common.attribute.Attribute;
import org.apache.rocketmq.common.attribute.EnumAttribute;
import org.apache.rocketmq.common.attribute.TopicMessageType;

import static com.google.common.collect.Sets.newHashSet;

Expand All @@ -31,10 +31,17 @@ public class TopicAttributes {
newHashSet("BatchCQ", "SimpleCQ"),
"SimpleCQ"
);
public static final EnumAttribute TOPIC_MESSAGE_TYPE_ATTRIBUTE = new EnumAttribute(
"message.type",
true,
TopicMessageType.topicMessageTypeSet(),
TopicMessageType.NORMAL.getValue()
);
public static final Map<String, Attribute> ALL;

static {
ALL = new HashMap<>();
ALL.put(QUEUE_TYPE_ATTRIBUTE.getName(), QUEUE_TYPE_ATTRIBUTE);
ALL.put(TOPIC_MESSAGE_TYPE_ATTRIBUTE.getName(), TOPIC_MESSAGE_TYPE_ATTRIBUTE);
}
}
76 changes: 62 additions & 14 deletions common/src/main/java/org/apache/rocketmq/common/TopicConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,30 @@
*/
package org.apache.rocketmq.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.annotation.JSONField;
import java.util.Map;
import java.util.Objects;
import org.apache.rocketmq.common.attribute.TopicMessageType;
import org.apache.rocketmq.common.constant.PermName;

import java.util.Map;
import static org.apache.rocketmq.common.TopicAttributes.TOPIC_MESSAGE_TYPE_ATTRIBUTE;

public class TopicConfig {
private static final String SEPARATOR = " ";
public static int defaultReadQueueNums = 16;
public static int defaultWriteQueueNums = 16;
private static final TypeReference<Map<String, String>> ATTRIBUTES_TYPE_REFERENCE = new TypeReference<Map<String, String>>() {
};
private String topicName;
private int readQueueNums = defaultReadQueueNums;
private int writeQueueNums = defaultWriteQueueNums;
private int perm = PermName.PERM_READ | PermName.PERM_WRITE;
private TopicFilterType topicFilterType = TopicFilterType.SINGLE_TAG;
private int topicSysFlag = 0;
private boolean order = false;
// Field attributes should not have ' ' char in key or value, otherwise will lead to decode failure.
private Map<String, String> attributes;

public TopicConfig() {
Expand Down Expand Up @@ -69,7 +78,9 @@ public TopicConfig(TopicConfig other) {
this.topicFilterType = other.topicFilterType;
this.topicSysFlag = other.topicSysFlag;
this.order = other.order;
this.attributes = other.attributes;
}

public String encode() {
StringBuilder sb = new StringBuilder();
//[0]
Expand All @@ -86,15 +97,18 @@ public String encode() {
sb.append(SEPARATOR);
//[4]
sb.append(this.topicFilterType);

// Leave the encode/decode [attributes] out for now
sb.append(SEPARATOR);
//[5]
if (attributes != null) {
sb.append(JSON.toJSONString(attributes));
}

return sb.toString();
}

public boolean decode(final String in) {
String[] strs = in.split(SEPARATOR);
if (strs != null && strs.length >= 5) {
if (strs.length >= 5) {
this.topicName = strs[0];

this.readQueueNums = Integer.parseInt(strs[1]);
Expand All @@ -105,6 +119,14 @@ public boolean decode(final String in) {

this.topicFilterType = TopicFilterType.valueOf(strs[4]);

if (strs.length >= 6) {
try {
this.attributes = JSON.parseObject(strs[5], ATTRIBUTES_TYPE_REFERENCE.getType());
} catch (Exception e) {
// ignore exception when parse failed, cause map's key/value can have ' ' char.
}
}

return true;
}

Expand Down Expand Up @@ -175,30 +197,56 @@ public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}

@JSONField(serialize = false, deserialize = false)
public TopicMessageType getTopicMessageType() {
if (attributes == null) {
return TopicMessageType.NORMAL;
}
String content = attributes.get(TOPIC_MESSAGE_TYPE_ATTRIBUTE.getName());
if (content == null) {
return TopicMessageType.NORMAL;
}
return TopicMessageType.valueOf(content);
}

@JSONField(serialize = false, deserialize = false)
public void setTopicMessageType(TopicMessageType topicMessageType) {
attributes.put(TOPIC_MESSAGE_TYPE_ATTRIBUTE.getName(), topicMessageType.getValue());
}

@Override
public boolean equals(Object o) {
if (this == o)
if (this == o) {
return true;
if (o == null || getClass() != o.getClass())
}
if (o == null || getClass() != o.getClass()) {
return false;
}

TopicConfig that = (TopicConfig) o;

if (readQueueNums != that.readQueueNums)
if (readQueueNums != that.readQueueNums) {
return false;
if (writeQueueNums != that.writeQueueNums)
}
if (writeQueueNums != that.writeQueueNums) {
return false;
if (perm != that.perm)
}
if (perm != that.perm) {
return false;
if (topicSysFlag != that.topicSysFlag)
}
if (topicSysFlag != that.topicSysFlag) {
return false;
if (order != that.order)
}
if (order != that.order) {
return false;
if (topicName != null ? !topicName.equals(that.topicName) : that.topicName != null)
}
if (!Objects.equals(topicName, that.topicName)) {
return false;
if (topicFilterType != that.topicFilterType)
}
if (topicFilterType != that.topicFilterType) {
return false;
return attributes != null ? attributes.equals(that.attributes) : that.attributes == null;
}
return Objects.equals(attributes, that.attributes);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.attribute;

import com.google.common.collect.Sets;
import java.util.Set;

public enum TopicMessageType {
UNSPECIFIED("UNSPECIFIED"),
NORMAL("NORMAL"),
FIFO("FIFO"),
DELAY("DELAY"),
TRANSACTION("TRANSACTION");

private final String value;
TopicMessageType(String value) {
this.value = value;
}

public static Set<String> topicMessageTypeSet() {
return Sets.newHashSet(UNSPECIFIED.value, NORMAL.value, FIFO.value, DELAY.value, TRANSACTION.value);
}

public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.Arrays;
import java.util.concurrent.TimeUnit;

public class CustomizedRetryPolicy {
// 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 "CustomizedRetryPolicy{" +
"next=" + Arrays.toString(next) +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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;

/**
* next delay time = min(max, initial * multiplier^reconsumeTimes)
*/
public class ExponentialRetryPolicy {
private long initial = TimeUnit.SECONDS.toMillis(5);
private long max = TimeUnit.HOURS.toMillis(2);
private long multiplier = 2;

public long getInitial() {
return initial;
}

public void setInitial(long initial) {
this.initial = initial;
}

public long getMax() {
return max;
}

public void setMax(long max) {
this.max = max;
}

public long getMultiplier() {
return multiplier;
}

public void setMultiplier(long multiplier) {
this.multiplier = multiplier;
}

@Override
public String toString() {
return "ExponentialRetryPolicy{" +
"initial=" + initial +
", max=" + max +
", multiplier=" + multiplier +
'}';
}
}
Loading

0 comments on commit 49585ef

Please sign in to comment.