[ISSUE #3593] entrySet() replace keySet() (#3595)

* entrySet() replace keySet()

* update variable name

* Update AllocateMachineRoomNearby.java

* remove set

Co-authored-by: zh378814 <wb-zh378814@alibaba-inc.com>
Co-authored-by: yuz10 <845238369@qq.com>
This commit is contained in:
zhaohai
2021-12-09 15:13:13 +08:00
committed by GitHub
parent 50d45f23a1
commit 1e314f4100
15 changed files with 86 additions and 67 deletions
@@ -17,6 +17,7 @@
package org.apache.rocketmq.broker.filter;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentMap;
import org.apache.rocketmq.broker.BrokerController;
import org.apache.rocketmq.broker.BrokerPathConfigHelper;
@@ -158,8 +159,8 @@ public class ConsumerFilterManager extends ConfigManager {
}
public void unRegister(final String consumerGroup) {
for (String topic : filterDataByTopic.keySet()) {
this.filterDataByTopic.get(topic).unRegister(consumerGroup);
for (Entry<String, FilterDataMapByTopic> entry : filterDataByTopic.entrySet()) {
entry.getValue().unRegister(consumerGroup);
}
}
@@ -230,15 +231,15 @@ public class ConsumerFilterManager extends ConfigManager {
ConsumerFilterManager load = RemotingSerializable.fromJson(jsonString, ConsumerFilterManager.class);
if (load != null && load.filterDataByTopic != null) {
boolean bloomChanged = false;
for (String topic : load.filterDataByTopic.keySet()) {
FilterDataMapByTopic dataMapByTopic = load.filterDataByTopic.get(topic);
for (Entry<String, FilterDataMapByTopic> entry : load.filterDataByTopic.entrySet()) {
FilterDataMapByTopic dataMapByTopic = entry.getValue();
if (dataMapByTopic == null) {
continue;
}
for (String group : dataMapByTopic.getGroupFilterData().keySet()) {
for (Entry<String, ConsumerFilterData> groupEntry : dataMapByTopic.getGroupFilterData().entrySet()) {
ConsumerFilterData filterData = dataMapByTopic.getGroupFilterData().get(group);
ConsumerFilterData filterData = groupEntry.getValue();
if (filterData == null) {
continue;
@@ -246,7 +247,7 @@ public class ConsumerFilterManager extends ConfigManager {
try {
filterData.setCompiledExpression(
FilterFactory.INSTANCE.get(filterData.getExpressionType()).compile(filterData.getExpression())
FilterFactory.INSTANCE.get(filterData.getExpressionType()).compile(filterData.getExpression())
);
} catch (Exception e) {
log.error("load filter data error, " + filterData, e);
@@ -266,7 +267,7 @@ public class ConsumerFilterManager extends ConfigManager {
// we think all consumers are dead when load
long deadTime = System.currentTimeMillis() - 30 * 1000;
filterData.setDeadTime(
deadTime <= filterData.getBornTime() ? filterData.getBornTime() : deadTime
deadTime <= filterData.getBornTime() ? filterData.getBornTime() : deadTime
);
}
}
@@ -21,6 +21,7 @@ import org.apache.rocketmq.filter.expression.EvaluationContext;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
* Evaluation context from message.
@@ -49,8 +50,8 @@ public class MessageEvaluationContext implements EvaluationContext {
Map<String, Object> copy = new HashMap<String, Object>(properties.size(), 1);
for (String key : properties.keySet()) {
copy.put(key, properties.get(key));
for (Entry<String, String> entry : properties.entrySet()) {
copy.put(entry.getKey(), entry.getValue());
}
return copy;
@@ -18,6 +18,7 @@ package org.apache.rocketmq.client.consumer;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.rocketmq.client.ClientConfig;
import org.apache.rocketmq.client.QueryResult;
@@ -640,8 +641,8 @@ public class DefaultMQPushConsumer extends ClientConfig implements MQPushConsume
@Deprecated
public void setSubscription(Map<String, String> subscription) {
Map<String, String> subscriptionWithNamespace = new HashMap<String, String>();
for (String topic : subscription.keySet()) {
subscriptionWithNamespace.put(withNamespace(topic), subscription.get(topic));
for (Entry<String, String> topicEntry : subscription.entrySet()) {
subscriptionWithNamespace.put(withNamespace(topicEntry.getKey()), topicEntry.getValue());
}
this.subscription = subscriptionWithNamespace;
}
@@ -19,6 +19,7 @@ package org.apache.rocketmq.client.consumer.rebalance;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.consumer.AllocateMessageQueueStrategy;
@@ -115,9 +116,9 @@ public class AllocateMachineRoomNearby implements AllocateMessageQueueStrategy {
}
//2.allocate the rest mq to each machine room if there are no consumer alive in that machine room
for (String machineRoom : mr2Mq.keySet()) {
if (!mr2c.containsKey(machineRoom)) { // no alive consumer in the corresponding machine room, so all consumers share these queues
allocateResults.addAll(allocateMessageQueueStrategy.allocate(consumerGroup, currentCID, mr2Mq.get(machineRoom), cidAll));
for (Entry<String, List<MessageQueue>> machineRoomEntry : mr2Mq.entrySet()) {
if (!mr2c.containsKey(machineRoomEntry.getKey())) { // no alive consumer in the corresponding machine room, so all consumers share these queues
allocateResults.addAll(allocateMessageQueueStrategy.allocate(consumerGroup, currentCID, machineRoomEntry.getValue(), cidAll));
}
}
@@ -20,6 +20,7 @@ import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@@ -64,12 +65,12 @@ public class LocalFileOffsetStore implements OffsetStore {
if (offsetSerializeWrapper != null && offsetSerializeWrapper.getOffsetTable() != null) {
offsetTable.putAll(offsetSerializeWrapper.getOffsetTable());
for (MessageQueue mq : offsetSerializeWrapper.getOffsetTable().keySet()) {
AtomicLong offset = offsetSerializeWrapper.getOffsetTable().get(mq);
for (Entry<MessageQueue, AtomicLong> mqEntry : offsetSerializeWrapper.getOffsetTable().entrySet()) {
AtomicLong offset = mqEntry.getValue();
log.info("load consumer's offset, {} {} {}",
this.groupName,
mq,
offset.get());
this.groupName,
mqEntry.getKey(),
offset.get());
}
}
}
@@ -24,6 +24,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
@@ -276,26 +277,26 @@ public class Configuration {
}
private void merge(Properties from, Properties to) {
for (Object key : from.keySet()) {
Object fromObj = from.get(key), toObj = to.get(key);
for (Entry<Object, Object> next : from.entrySet()) {
Object fromObj = next.getValue(), toObj = to.get(next.getKey());
if (toObj != null && !toObj.equals(fromObj)) {
log.info("Replace, key: {}, value: {} -> {}", key, toObj, fromObj);
log.info("Replace, key: {}, value: {} -> {}", next.getKey(), toObj, fromObj);
}
to.put(key, fromObj);
to.put(next.getKey(), fromObj);
}
}
private void mergeIfExist(Properties from, Properties to) {
for (Object key : from.keySet()) {
if (!to.containsKey(key)) {
for (Entry<Object, Object> next : from.entrySet()) {
if (!to.containsKey(next.getKey())) {
continue;
}
Object fromObj = from.get(key), toObj = to.get(key);
Object fromObj = next.getValue(), toObj = to.get(next.getKey());
if (toObj != null && !toObj.equals(fromObj)) {
log.info("Replace, key: {}, value: {} -> {}", key, toObj, fromObj);
log.info("Replace, key: {}, value: {} -> {}", next.getKey(), toObj, fromObj);
}
to.put(key, fromObj);
to.put(next.getKey(), fromObj);
}
}
@@ -21,6 +21,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
@@ -50,10 +51,10 @@ public final class BeanUtils {
private static Map<Class<?>, Class<?>> wrapperMap = new HashMap<Class<?>, Class<?>>();
static {
for (final Class<?> primitiveClass : primitiveWrapperMap.keySet()) {
final Class<?> wrapperClass = primitiveWrapperMap.get(primitiveClass);
if (!primitiveClass.equals(wrapperClass)) {
wrapperMap.put(wrapperClass, primitiveClass);
for (Entry<Class<?>, Class<?>> primitiveClass : primitiveWrapperMap.entrySet()) {
final Class<?> wrapperClass = primitiveClass.getValue();
if (!primitiveClass.getKey().equals(wrapperClass)) {
wrapperMap.put(wrapperClass, primitiveClass.getKey());
}
}
wrapperMap.put(String.class, String.class);
@@ -22,6 +22,8 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.test.factory.MQMessageFactory;
@@ -54,9 +56,9 @@ public class MessageQueueMsg {
}
private void init() {
for (MessageQueue mq : msgsWithMQ.keySet()) {
msgsWithMQId.put(mq.getQueueId(), msgsWithMQ.get(mq));
msgBodys.addAll(MQMessageFactory.getMessageBody(msgsWithMQ.get(mq)));
for (Entry<MessageQueue, List<Object>> mqEntry : msgsWithMQ.entrySet()) {
msgsWithMQId.put(mqEntry.getKey().getQueueId(), mqEntry.getValue());
msgBodys.addAll(MQMessageFactory.getMessageBody(mqEntry.getValue()));
}
}
}
@@ -20,6 +20,7 @@ package org.apache.rocketmq.test.util;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
public class FileUtil {
@@ -68,9 +69,9 @@ public class FileUtil {
private String getPropertiesAsString(Properties properties) {
StringBuilder sb = new StringBuilder();
for (Object key : properties.keySet()) {
sb.append(key).append("=").append(properties.getProperty((String) key))
.append(lineSeperator);
for (Entry<Object, Object> keyEnty : properties.entrySet()) {
sb.append(keyEnty.getKey()).append("=").append((String) keyEnty.getValue())
.append(lineSeperator);
}
return sb.toString();
}
@@ -18,6 +18,7 @@
package org.apache.rocketmq.test.util;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import org.apache.log4j.Logger;
@@ -123,10 +124,10 @@ public class MQAdmin {
return false;
} else {
HashMap<String, BrokerData> brokers = clusterInfo.getBrokerAddrTable();
for (String brokerName : brokers.keySet()) {
HashMap<Long, String> brokerIps = brokers.get(brokerName).getBrokerAddrs();
for (long brokerId : brokerIps.keySet()) {
if (brokerIps.get(brokerId).contains(ip))
for (Entry<String, BrokerData> brokerEntry : brokers.entrySet()) {
HashMap<Long, String> brokerIps = brokerEntry.getValue().getBrokerAddrs();
for (Entry<Long, String> brokerIdEntry : brokerIps.entrySet()) {
if (brokerIdEntry.getValue().contains(ip))
return true;
}
}
@@ -23,6 +23,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.common.MixAll;
@@ -63,12 +64,12 @@ public class CommandUtil {
String masterAddr = brokerData.getBrokerAddrs().get(MixAll.MASTER_ID);
masterAndSlaveMap.put(masterAddr, new ArrayList<String>());
for (Long id : brokerData.getBrokerAddrs().keySet()) {
if (brokerData.getBrokerAddrs().get(id) == null || id == MixAll.MASTER_ID) {
for (Entry<Long, String> brokerAddrEntry : brokerData.getBrokerAddrs().entrySet()) {
if (brokerAddrEntry.getValue() == null || brokerAddrEntry.getKey() == MixAll.MASTER_ID) {
continue;
}
masterAndSlaveMap.get(masterAddr).add(brokerData.getBrokerAddrs().get(id));
masterAndSlaveMap.get(masterAddr).add(brokerAddrEntry.getValue());
}
}
@@ -20,7 +20,9 @@ package org.apache.rocketmq.tools.command.broker;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
@@ -83,19 +85,19 @@ public class GetBrokerConfigCommand implements SubCommand {
Map<String, List<String>> masterAndSlaveMap
= CommandUtil.fetchMasterAndSlaveDistinguish(defaultMQAdminExt, clusterName);
for (String masterAddr : masterAndSlaveMap.keySet()) {
for (Entry<String, List<String>> masterAndSlaveEntry : masterAndSlaveMap.entrySet()) {
getAndPrint(
defaultMQAdminExt,
String.format("============Master: %s============\n", masterAddr),
masterAddr
defaultMQAdminExt,
String.format("============Master: %s============\n", masterAndSlaveEntry.getKey()),
masterAndSlaveEntry.getKey()
);
for (String slaveAddr : masterAndSlaveMap.get(masterAddr)) {
for (String slaveAddr : masterAndSlaveEntry.getValue()) {
getAndPrint(
defaultMQAdminExt,
String.format("============My Master: %s=====Slave: %s============\n", masterAddr, slaveAddr),
slaveAddr
defaultMQAdminExt,
String.format("============My Master: %s=====Slave: %s============\n", masterAndSlaveEntry.getKey(), slaveAddr),
slaveAddr
);
}
}
@@ -121,8 +123,8 @@ public class GetBrokerConfigCommand implements SubCommand {
return;
}
for (Object key : properties.keySet()) {
System.out.printf("%-50s= %s\n", key, properties.get(key));
for (Entry<Object, Object> entry : properties.entrySet()) {
System.out.printf("%-50s= %s\n", entry.getKey(), entry.getValue());
}
System.out.printf("%n");
@@ -20,12 +20,14 @@ import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.collections.CollectionUtils;
import org.apache.rocketmq.common.protocol.body.ClusterInfo;
import org.apache.rocketmq.common.protocol.route.BrokerData;
import org.apache.rocketmq.common.subscription.SubscriptionGroupConfig;
import org.apache.rocketmq.remoting.RPCHook;
import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
@@ -63,14 +65,14 @@ public class GetConsumerConfigSubCommand implements SubCommand {
List<ConsumerConfigInfo> consumerConfigInfoList = new ArrayList<>();
ClusterInfo clusterInfo = adminExt.examineBrokerClusterInfo();
Map<String, Set<String>> clusterAddrTable = clusterInfo.getClusterAddrTable();
for (String brokerName : clusterInfo.getBrokerAddrTable().keySet()) {
String clusterName = this.getClusterName(brokerName, clusterAddrTable);
String brokerAddress = clusterInfo.getBrokerAddrTable().get(brokerName).selectBrokerAddr();
for (Entry<String, BrokerData> brokerEntry : clusterInfo.getBrokerAddrTable().entrySet()) {
String clusterName = this.getClusterName(brokerEntry.getKey(), clusterAddrTable);
String brokerAddress = brokerEntry.getValue().selectBrokerAddr();
SubscriptionGroupConfig subscriptionGroupConfig = adminExt.examineSubscriptionGroupConfig(brokerAddress, groupName);
if (subscriptionGroupConfig == null) {
continue;
}
consumerConfigInfoList.add(new ConsumerConfigInfo(clusterName, brokerName, subscriptionGroupConfig));
consumerConfigInfoList.add(new ConsumerConfigInfo(clusterName, brokerEntry.getKey(), subscriptionGroupConfig));
}
if (CollectionUtils.isEmpty(consumerConfigInfoList)) {
return;
@@ -19,6 +19,7 @@ package org.apache.rocketmq.tools.command.export;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import com.alibaba.fastjson.JSON;
@@ -79,10 +80,10 @@ public class ExportConfigsCommand implements SubCommand {
Map<String, Properties> brokerConfigs = new HashMap<>();
Map<String, List<String>> masterAndSlaveMap
= CommandUtil.fetchMasterAndSlaveDistinguish(defaultMQAdminExt, clusterName);
for (String masterAddr : masterAndSlaveMap.keySet()) {
Properties masterProperties = defaultMQAdminExt.getBrokerConfig(masterAddr);
for (Entry<String, List<String>> masterAndSlaveEntry : masterAndSlaveMap.entrySet()) {
Properties masterProperties = defaultMQAdminExt.getBrokerConfig(masterAndSlaveEntry.getKey());
masterBrokerSize++;
slaveBrokerSize += masterAndSlaveMap.get(masterAddr).size();
slaveBrokerSize += masterAndSlaveEntry.getValue().size();
brokerConfigs.put(masterProperties.getProperty("brokerName"), needBrokerProprties(masterProperties));
}
@@ -20,7 +20,9 @@ package org.apache.rocketmq.tools.command.namesrv;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.rocketmq.remoting.RPCHook;
@@ -66,11 +68,11 @@ public class GetNamesrvConfigCommand implements SubCommand {
Map<String, Properties> nameServerConfigs = defaultMQAdminExt.getNameServerConfig(serverList);
for (String server : nameServerConfigs.keySet()) {
for (Entry<String, Properties> nameServerConfigEntry : nameServerConfigs.entrySet()) {
System.out.printf("============%s============\n",
server);
for (Object key : nameServerConfigs.get(server).keySet()) {
System.out.printf("%-50s= %s\n", key, nameServerConfigs.get(server).get(key));
nameServerConfigEntry.getKey());
for (Entry<Object, Object> entry : nameServerConfigEntry.getValue().entrySet()) {
System.out.printf("%-50s= %s\n", entry.getKey(), entry.getValue());
}
}
} catch (Exception e) {