-
Notifications
You must be signed in to change notification settings - Fork 11.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b0a27b
commit 80e259e
Showing
23 changed files
with
1,813 additions
and
79 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
103 changes: 103 additions & 0 deletions
103
broker/src/main/java/org/apache/rocketmq/broker/offset/RocksDBConsumerOffsetManager.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,103 @@ | ||
/* | ||
* 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.broker.offset; | ||
|
||
import java.io.File; | ||
import java.util.Iterator; | ||
import java.util.Map.Entry; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
import org.apache.rocketmq.broker.BrokerController; | ||
import org.apache.rocketmq.client.consumer.store.RocksDBOffsetSerializeWrapper; | ||
import org.apache.rocketmq.common.config.RocksDBConfigManager; | ||
import org.apache.rocketmq.common.utils.DataConverter; | ||
import org.rocksdb.WriteBatch; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import com.alibaba.fastjson.serializer.SerializerFeature; | ||
|
||
public class RocksDBConsumerOffsetManager extends ConsumerOffsetManager { | ||
|
||
public RocksDBConsumerOffsetManager(BrokerController brokerController) { | ||
super(brokerController); | ||
this.rocksDBConfigManager = new RocksDBConfigManager(this.brokerController.getMessageStoreConfig().getMemTableFlushInterval()); | ||
} | ||
|
||
@Override | ||
public boolean load() { | ||
return this.rocksDBConfigManager.load(configFilePath(), this::decode0); | ||
} | ||
|
||
@Override | ||
public boolean stop() { | ||
return this.rocksDBConfigManager.stop(); | ||
} | ||
|
||
@Override | ||
protected void removeConsumerOffset(String topicAtGroup) { | ||
try { | ||
byte[] keyBytes = topicAtGroup.getBytes(DataConverter.charset); | ||
this.rocksDBConfigManager.delete(keyBytes); | ||
} catch (Exception e) { | ||
LOG.error("kv remove consumerOffset Failed, {}", topicAtGroup); | ||
} | ||
} | ||
|
||
@Override | ||
protected void decode0(final byte[] key, final byte[] body) { | ||
String topicAtGroup = new String(key, DataConverter.charset); | ||
RocksDBOffsetSerializeWrapper wrapper = JSON.parseObject(body, RocksDBOffsetSerializeWrapper.class); | ||
|
||
this.offsetTable.put(topicAtGroup, wrapper.getOffsetTable()); | ||
LOG.info("load exist local offset, {}, {}", topicAtGroup, wrapper.getOffsetTable()); | ||
} | ||
|
||
@Override | ||
public String configFilePath() { | ||
return this.brokerController.getMessageStoreConfig().getStorePathRootDir() + File.separator + "config" + File.separator + "consumerOffsets" + File.separator; | ||
} | ||
|
||
@Override | ||
public synchronized void persist() { | ||
WriteBatch writeBatch = new WriteBatch(); | ||
try { | ||
Iterator<Entry<String, ConcurrentMap<Integer, Long>>> iterator = this.offsetTable.entrySet().iterator(); | ||
while (iterator.hasNext()) { | ||
Entry<String, ConcurrentMap<Integer, Long>> entry = iterator.next(); | ||
putWriteBatch(writeBatch, entry.getKey(), entry.getValue()); | ||
|
||
if (writeBatch.getDataSize() >= 4 * 1024) { | ||
this.rocksDBConfigManager.batchPutWithWal(writeBatch); | ||
} | ||
} | ||
this.rocksDBConfigManager.batchPutWithWal(writeBatch); | ||
this.rocksDBConfigManager.flushWAL(); | ||
} catch (Exception e) { | ||
LOG.error("consumer offset persist Failed", e); | ||
} finally { | ||
writeBatch.close(); | ||
} | ||
} | ||
|
||
private void putWriteBatch(final WriteBatch writeBatch, final String topicGroupName, final ConcurrentMap<Integer, Long> offsetMap) throws Exception { | ||
byte[] keyBytes = topicGroupName.getBytes(DataConverter.charset); | ||
RocksDBOffsetSerializeWrapper wrapper = new RocksDBOffsetSerializeWrapper(); | ||
wrapper.setOffsetTable(offsetMap); | ||
byte[] valueBytes = JSON.toJSONBytes(wrapper, SerializerFeature.BrowserCompatible); | ||
writeBatch.put(keyBytes, valueBytes); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
broker/src/main/java/org/apache/rocketmq/broker/offset/RocksDBLmqConsumerOffsetManager.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,103 @@ | ||
/* | ||
* 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.broker.offset; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.apache.rocketmq.broker.BrokerController; | ||
import org.apache.rocketmq.common.MixAll; | ||
import org.apache.rocketmq.remoting.protocol.RemotingSerializable; | ||
|
||
public class RocksDBLmqConsumerOffsetManager extends RocksDBConsumerOffsetManager { | ||
private ConcurrentHashMap<String, Long> lmqOffsetTable = new ConcurrentHashMap<>(512); | ||
|
||
public RocksDBLmqConsumerOffsetManager(BrokerController brokerController) { | ||
super(brokerController); | ||
} | ||
|
||
@Override | ||
public long queryOffset(final String group, final String topic, final int queueId) { | ||
if (!MixAll.isLmq(group)) { | ||
return super.queryOffset(group, topic, queueId); | ||
} | ||
// topic@group | ||
String key = topic + TOPIC_GROUP_SEPARATOR + group; | ||
Long offset = lmqOffsetTable.get(key); | ||
if (offset != null) { | ||
return offset; | ||
} | ||
return -1; | ||
} | ||
|
||
@Override | ||
public Map<Integer, Long> queryOffset(final String group, final String topic) { | ||
if (!MixAll.isLmq(group)) { | ||
return super.queryOffset(group, topic); | ||
} | ||
Map<Integer, Long> map = new HashMap<>(); | ||
// topic@group | ||
String key = topic + TOPIC_GROUP_SEPARATOR + group; | ||
Long offset = lmqOffsetTable.get(key); | ||
if (offset != null) { | ||
map.put(0, offset); | ||
} | ||
return map; | ||
} | ||
|
||
@Override | ||
public void commitOffset(final String clientHost, final String group, final String topic, final int queueId, | ||
final long offset) { | ||
if (!MixAll.isLmq(group)) { | ||
super.commitOffset(clientHost, group, topic, queueId, offset); | ||
return; | ||
} | ||
// topic@group | ||
String key = topic + TOPIC_GROUP_SEPARATOR + group; | ||
lmqOffsetTable.put(key, offset); | ||
} | ||
|
||
@Override | ||
public String encode() { | ||
return this.encode(false); | ||
} | ||
|
||
@Override | ||
public void decode(String jsonString) { | ||
if (jsonString != null) { | ||
RocksDBLmqConsumerOffsetManager obj = RemotingSerializable.fromJson(jsonString, RocksDBLmqConsumerOffsetManager.class); | ||
if (obj != null) { | ||
super.setOffsetTable(obj.getOffsetTable()); | ||
this.lmqOffsetTable = obj.lmqOffsetTable; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public String encode(final boolean prettyFormat) { | ||
return RemotingSerializable.toJson(this, prettyFormat); | ||
} | ||
|
||
public ConcurrentHashMap<String, Long> getLmqOffsetTable() { | ||
return lmqOffsetTable; | ||
} | ||
|
||
public void setLmqOffsetTable(ConcurrentHashMap<String, Long> lmqOffsetTable) { | ||
this.lmqOffsetTable = lmqOffsetTable; | ||
} | ||
} |
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.