forked from apache/rocketmq
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUE apache#7064] [RIP-66-1] Support KV(RocksDB) Storage for Metada…
…ta (apache#7092) * typo int readme[ecosystem] * rocksdb metadata * add unit test * fix testOffsetPersistInMemory * fix unit test * fix unit test * remove unused import * move RocksDBOffsetSerialize to broker moudle * Fix bazel build scripts Signed-off-by: Li Zhanhui <[email protected]> * Flag QueryMsgByKeyIT as flaky as it fails at frequency: 5 out of 32 Signed-off-by: Li Zhanhui <[email protected]> * change public to private of some inner method --------- Signed-off-by: Li Zhanhui <[email protected]> Co-authored-by: Li Zhanhui <[email protected]>
- Loading branch information
1 parent
c73d8ee
commit 3a6ef04
Showing
35 changed files
with
2,473 additions
and
86 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
102 changes: 102 additions & 0 deletions
102
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,102 @@ | ||
/* | ||
* 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.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); | ||
} | ||
} |
Oops, something went wrong.