forked from qunarcorp/qmq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/qunarcorp/qmq
- Loading branch information
Showing
32 changed files
with
332 additions
and
22 deletions.
There are no files selected for viewing
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
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
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
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
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
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
92 changes: 92 additions & 0 deletions
92
qmq-metaserver/src/main/java/qunar/tc/qmq/meta/management/ResetOffsetAction.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,92 @@ | ||
/* | ||
* Copyright 2018 Qunar, Inc. | ||
* | ||
* Licensed 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 qunar.tc.qmq.meta.management; | ||
|
||
import com.google.common.base.Strings; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import qunar.tc.qmq.meta.BrokerGroup; | ||
import qunar.tc.qmq.meta.model.SubjectRoute; | ||
import qunar.tc.qmq.meta.store.Store; | ||
import qunar.tc.qmq.netty.NettyClientConfig; | ||
import qunar.tc.qmq.netty.client.NettyClient; | ||
import qunar.tc.qmq.protocol.CommandCode; | ||
import qunar.tc.qmq.protocol.Datagram; | ||
import qunar.tc.qmq.protocol.RemotingHeader; | ||
import qunar.tc.qmq.utils.PayloadHolderUtils; | ||
import qunar.tc.qmq.utils.RetrySubjectUtils; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
public class ResetOffsetAction implements MetaManagementAction { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ResetOffsetAction.class); | ||
|
||
private final Store store; | ||
private final NettyClient client; | ||
|
||
public ResetOffsetAction(Store store) { | ||
this.client = NettyClient.getClient(); | ||
this.client.start(new NettyClientConfig()); | ||
this.store = store; | ||
} | ||
|
||
@Override | ||
public ActionResult<String> handleAction(HttpServletRequest req) { | ||
String subject = req.getParameter("subject"); | ||
String consumerGroup = req.getParameter("group"); | ||
int action = Integer.valueOf(req.getParameter("code")); | ||
|
||
if (Strings.isNullOrEmpty(subject) || Strings.isNullOrEmpty(consumerGroup)) { | ||
return ActionResult.error("subject and consumerGroup required"); | ||
} | ||
|
||
if (action != 1 && action != 2) { | ||
return ActionResult.error("action must 1 or 2, LATEST=1, EARLIEST=2"); | ||
} | ||
|
||
final SubjectRoute subjectRoute = store.selectSubjectRoute(RetrySubjectUtils.getRealSubject(subject)); | ||
if (subjectRoute == null) { | ||
return ActionResult.error("find no route"); | ||
} | ||
|
||
Datagram datagram = buildResetOffsetDatagram(subject, consumerGroup, action); | ||
for (final String brokerGroupName : subjectRoute.getBrokerGroups()) { | ||
try { | ||
final BrokerGroup brokerGroup = store.getBrokerGroup(brokerGroupName); | ||
client.sendSync(brokerGroup.getMaster(), datagram, 2000); | ||
} catch (Throwable e) { | ||
LOGGER.error("send consume manage request error, brokerGroupName={}", brokerGroupName, e); | ||
return ActionResult.error("reset failed: brokerGroupName=" + brokerGroupName); | ||
} | ||
} | ||
|
||
return ActionResult.ok("success"); | ||
} | ||
|
||
private Datagram buildResetOffsetDatagram(final String subject, final String consumerGroup, int code) { | ||
final Datagram datagram = new Datagram(); | ||
final RemotingHeader header = new RemotingHeader(); | ||
header.setCode(CommandCode.CONSUME_MANAGE); | ||
datagram.setHeader(header); | ||
datagram.setPayloadHolder(out -> { | ||
PayloadHolderUtils.writeString(subject, out); | ||
PayloadHolderUtils.writeString(consumerGroup, out); | ||
out.writeInt(code); | ||
}); | ||
return datagram; | ||
} | ||
} |
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
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
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
78 changes: 78 additions & 0 deletions
78
qmq-server/src/main/java/qunar/tc/qmq/processor/ConsumerManageProcessor.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,78 @@ | ||
/* | ||
* Copyright 2018 Qunar, Inc. | ||
* | ||
* Licensed 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 qunar.tc.qmq.processor; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import qunar.tc.qmq.base.ConsumeManageRequest; | ||
import qunar.tc.qmq.protocol.CommandCode; | ||
import qunar.tc.qmq.protocol.Datagram; | ||
import qunar.tc.qmq.protocol.RemotingCommand; | ||
import qunar.tc.qmq.store.Storage; | ||
import qunar.tc.qmq.util.RemotingBuilder; | ||
import qunar.tc.qmq.utils.PayloadHolderUtils; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* @author yunfeng.yang | ||
* @since 2017/11/22 | ||
*/ | ||
public class ConsumerManageProcessor extends AbstractRequestProcessor { | ||
private static final Logger LOG = LoggerFactory.getLogger(ConsumerManageProcessor.class); | ||
|
||
private final Storage store; | ||
|
||
public ConsumerManageProcessor(Storage store) { | ||
this.store = store; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Datagram> processRequest(ChannelHandlerContext ctx, RemotingCommand command) { | ||
final ConsumeManageRequest request = deserialize(command.getBody()); | ||
LOG.info("receive consumer manager request:{}", request); | ||
|
||
if (!checkRequest(request)) { | ||
final Datagram datagram = RemotingBuilder.buildEmptyResponseDatagram(CommandCode.PARAM_ERROR, command.getHeader()); | ||
ctx.writeAndFlush(datagram); | ||
return null; | ||
} | ||
store.updateConsumeQueue(request.getSubject(), request.getGroup(), request.getConsumerFromWhere()); | ||
|
||
final Datagram datagram = RemotingBuilder.buildEmptyResponseDatagram(CommandCode.SUCCESS, command.getHeader()); | ||
datagram.getHeader().setVersion(command.getHeader().getVersion()); | ||
ctx.writeAndFlush(datagram); | ||
return null; | ||
} | ||
|
||
private boolean checkRequest(ConsumeManageRequest request) { | ||
return request != null && request.getSubject() != null && request.getGroup() != null; | ||
} | ||
|
||
private ConsumeManageRequest deserialize(ByteBuf buf) { | ||
String subject = PayloadHolderUtils.readString(buf); | ||
String consumerGroup = PayloadHolderUtils.readString(buf); | ||
int code = buf.readInt(); | ||
ConsumeManageRequest request = new ConsumeManageRequest(); | ||
request.setSubject(subject); | ||
request.setGroup(consumerGroup); | ||
request.setConsumerFromWhere(code); | ||
return request; | ||
} | ||
} |
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
Oops, something went wrong.