提交 f508f131 编写于 作者: J Jaskey 提交者: yukon

[ROCKETMQ-107] Fix possible concurrency problem on ServiceState when consumer...

[ROCKETMQ-107] Fix possible concurrency problem on ServiceState when consumer start/shutdown, closes apache/incubator-rocketmq#68
上级 c183e0d4
...@@ -70,7 +70,7 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner { ...@@ -70,7 +70,7 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner {
private final RPCHook rpcHook; private final RPCHook rpcHook;
private final ArrayList<ConsumeMessageHook> consumeMessageHookList = new ArrayList<ConsumeMessageHook>(); private final ArrayList<ConsumeMessageHook> consumeMessageHookList = new ArrayList<ConsumeMessageHook>();
private final ArrayList<FilterMessageHook> filterMessageHookList = new ArrayList<FilterMessageHook>(); private final ArrayList<FilterMessageHook> filterMessageHookList = new ArrayList<FilterMessageHook>();
private ServiceState serviceState = ServiceState.CREATE_JUST; private volatile ServiceState serviceState = ServiceState.CREATE_JUST;
private MQClientInstance mQClientFactory; private MQClientInstance mQClientFactory;
private PullAPIWrapper pullAPIWrapper; private PullAPIWrapper pullAPIWrapper;
private OffsetStore offsetStore; private OffsetStore offsetStore;
...@@ -161,7 +161,8 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner { ...@@ -161,7 +161,8 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner {
return this.pullSyncImpl(mq, subExpression, offset, maxNums, false, timeout); return this.pullSyncImpl(mq, subExpression, offset, maxNums, false, timeout);
} }
private PullResult pullSyncImpl(MessageQueue mq, String subExpression, long offset, int maxNums, boolean block, long timeout) private PullResult pullSyncImpl(MessageQueue mq, String subExpression, long offset, int maxNums, boolean block,
long timeout)
throws MQClientException, RemotingException, MQBrokerException, InterruptedException { throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
this.makeSureStateOK(); this.makeSureStateOK();
...@@ -365,7 +366,8 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner { ...@@ -365,7 +366,8 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner {
pull(mq, subExpression, offset, maxNums, pullCallback, this.defaultMQPullConsumer.getConsumerPullTimeoutMillis()); pull(mq, subExpression, offset, maxNums, pullCallback, this.defaultMQPullConsumer.getConsumerPullTimeoutMillis());
} }
public void pull(MessageQueue mq, String subExpression, long offset, int maxNums, PullCallback pullCallback, long timeout) public void pull(MessageQueue mq, String subExpression, long offset, int maxNums, PullCallback pullCallback,
long timeout)
throws MQClientException, RemotingException, InterruptedException { throws MQClientException, RemotingException, InterruptedException {
this.pullAsyncImpl(mq, subExpression, offset, maxNums, pullCallback, false, timeout); this.pullAsyncImpl(mq, subExpression, offset, maxNums, pullCallback, false, timeout);
} }
...@@ -449,7 +451,8 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner { ...@@ -449,7 +451,8 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner {
return defaultMQPullConsumer; return defaultMQPullConsumer;
} }
public void pullBlockIfNotFound(MessageQueue mq, String subExpression, long offset, int maxNums, PullCallback pullCallback) public void pullBlockIfNotFound(MessageQueue mq, String subExpression, long offset, int maxNums,
PullCallback pullCallback)
throws MQClientException, RemotingException, InterruptedException { throws MQClientException, RemotingException, InterruptedException {
this.pullAsyncImpl(mq, subExpression, offset, maxNums, pullCallback, true, this.pullAsyncImpl(mq, subExpression, offset, maxNums, pullCallback, true,
this.getDefaultMQPullConsumer().getConsumerPullTimeoutMillis()); this.getDefaultMQPullConsumer().getConsumerPullTimeoutMillis());
...@@ -510,7 +513,7 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner { ...@@ -510,7 +513,7 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner {
} }
} }
public void shutdown() { public synchronized void shutdown() {
switch (this.serviceState) { switch (this.serviceState) {
case CREATE_JUST: case CREATE_JUST:
break; break;
...@@ -528,7 +531,7 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner { ...@@ -528,7 +531,7 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner {
} }
} }
public void start() throws MQClientException { public synchronized void start() throws MQClientException {
switch (this.serviceState) { switch (this.serviceState) {
case CREATE_JUST: case CREATE_JUST:
this.serviceState = ServiceState.START_FAILED; this.serviceState = ServiceState.START_FAILED;
...@@ -593,6 +596,7 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner { ...@@ -593,6 +596,7 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner {
default: default:
break; break;
} }
} }
private void checkConfig() throws MQClientException { private void checkConfig() throws MQClientException {
...@@ -662,7 +666,8 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner { ...@@ -662,7 +666,8 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner {
this.offsetStore.updateOffset(mq, offset, false); this.offsetStore.updateOffset(mq, offset, false);
} }
public MessageExt viewMessage(String msgId) throws RemotingException, MQBrokerException, InterruptedException, MQClientException { public MessageExt viewMessage(String msgId)
throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
this.makeSureStateOK(); this.makeSureStateOK();
return this.mQClientFactory.getMQAdminImpl().viewMessage(msgId); return this.mQClientFactory.getMQAdminImpl().viewMessage(msgId);
} }
...@@ -692,6 +697,8 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner { ...@@ -692,6 +697,8 @@ public class DefaultMQPullConsumerImpl implements MQConsumerInner {
return serviceState; return serviceState;
} }
//Don't use this deprecated setter, which will be removed soon.
@Deprecated
public void setServiceState(ServiceState serviceState) { public void setServiceState(ServiceState serviceState) {
this.serviceState = serviceState; this.serviceState = serviceState;
} }
......
...@@ -97,7 +97,7 @@ public class DefaultMQPushConsumerImpl implements MQConsumerInner { ...@@ -97,7 +97,7 @@ public class DefaultMQPushConsumerImpl implements MQConsumerInner {
private final long consumerStartTimestamp = System.currentTimeMillis(); private final long consumerStartTimestamp = System.currentTimeMillis();
private final ArrayList<ConsumeMessageHook> consumeMessageHookList = new ArrayList<ConsumeMessageHook>(); private final ArrayList<ConsumeMessageHook> consumeMessageHookList = new ArrayList<ConsumeMessageHook>();
private final RPCHook rpcHook; private final RPCHook rpcHook;
private ServiceState serviceState = ServiceState.CREATE_JUST; private volatile ServiceState serviceState = ServiceState.CREATE_JUST;
private MQClientInstance mQClientFactory; private MQClientInstance mQClientFactory;
private PullAPIWrapper pullAPIWrapper; private PullAPIWrapper pullAPIWrapper;
private volatile boolean pause = false; private volatile boolean pause = false;
...@@ -515,7 +515,7 @@ public class DefaultMQPushConsumerImpl implements MQConsumerInner { ...@@ -515,7 +515,7 @@ public class DefaultMQPushConsumerImpl implements MQConsumerInner {
} }
} }
public void shutdown() { public synchronized void shutdown() {
switch (this.serviceState) { switch (this.serviceState) {
case CREATE_JUST: case CREATE_JUST:
break; break;
...@@ -535,7 +535,7 @@ public class DefaultMQPushConsumerImpl implements MQConsumerInner { ...@@ -535,7 +535,7 @@ public class DefaultMQPushConsumerImpl implements MQConsumerInner {
} }
} }
public void start() throws MQClientException { public synchronized void start() throws MQClientException {
switch (this.serviceState) { switch (this.serviceState) {
case CREATE_JUST: case CREATE_JUST:
log.info("the consumer [{}] start beginning. messageModel={}, isUnitMode={}", this.defaultMQPushConsumer.getConsumerGroup(), log.info("the consumer [{}] start beginning. messageModel={}, isUnitMode={}", this.defaultMQPushConsumer.getConsumerGroup(),
...@@ -615,9 +615,7 @@ public class DefaultMQPushConsumerImpl implements MQConsumerInner { ...@@ -615,9 +615,7 @@ public class DefaultMQPushConsumerImpl implements MQConsumerInner {
} }
this.updateTopicSubscribeInfoWhenSubscriptionChanged(); this.updateTopicSubscribeInfoWhenSubscriptionChanged();
this.mQClientFactory.sendHeartbeatToAllBrokerWithLock(); this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
this.mQClientFactory.rebalanceImmediately(); this.mQClientFactory.rebalanceImmediately();
} }
...@@ -855,7 +853,8 @@ public class DefaultMQPushConsumerImpl implements MQConsumerInner { ...@@ -855,7 +853,8 @@ public class DefaultMQPushConsumerImpl implements MQConsumerInner {
this.consumeMessageService.updateCorePoolSize(corePoolSize); this.consumeMessageService.updateCorePoolSize(corePoolSize);
} }
public MessageExt viewMessage(String msgId) throws RemotingException, MQBrokerException, InterruptedException, MQClientException { public MessageExt viewMessage(String msgId)
throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
return this.mQClientFactory.getMQAdminImpl().viewMessage(msgId); return this.mQClientFactory.getMQAdminImpl().viewMessage(msgId);
} }
...@@ -1014,7 +1013,9 @@ public class DefaultMQPushConsumerImpl implements MQConsumerInner { ...@@ -1014,7 +1013,9 @@ public class DefaultMQPushConsumerImpl implements MQConsumerInner {
return serviceState; return serviceState;
} }
public void setServiceState(ServiceState serviceState) { //Don't use this deprecated setter, which will be removed soon.
@Deprecated
public synchronized void setServiceState(ServiceState serviceState) {
this.serviceState = serviceState; this.serviceState = serviceState;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册