-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add LogManager to manager topic read/write
- Loading branch information
1 parent
af23de0
commit 859572d
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
distmq-broker/src/main/java/com/github/wenweihu86/distmq/broker/BrokerStateMachine.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 |
---|---|---|
@@ -1,22 +1,70 @@ | ||
package com.github.wenweihu86.distmq.broker; | ||
|
||
import com.github.wenweihu86.distmq.broker.log.LogManager; | ||
import com.github.wenweihu86.distmq.client.api.BrokerMessage; | ||
import com.github.wenweihu86.raft.StateMachine; | ||
import org.apache.commons.io.FileUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Created by wenweihu86 on 2017/6/17. | ||
*/ | ||
public class BrokerStateMachine implements StateMachine { | ||
private static final Logger LOG = LoggerFactory.getLogger(BrokerStateMachine.class); | ||
private String messageDir; | ||
private LogManager logManager; | ||
|
||
public BrokerStateMachine() { | ||
String dataDir = GlobalConf.getInstance().getDataDir(); | ||
this.messageDir = dataDir + File.separator + "message"; | ||
} | ||
|
||
@Override | ||
public void writeSnapshot(String snapshotDir) { | ||
try { | ||
File messageDirFile = new File(messageDir); | ||
File snapshotDirFile = new File(snapshotDir); | ||
if (snapshotDirFile.exists()) { | ||
FileUtils.deleteDirectory(snapshotDirFile); | ||
} | ||
if (messageDirFile.exists()) { | ||
FileUtils.copyDirectory(messageDirFile, snapshotDirFile); | ||
} | ||
} catch (IOException ex) { | ||
LOG.warn("snapshot failed"); | ||
} | ||
} | ||
|
||
@Override | ||
public void readSnapshot(String snapshotDir) { | ||
try { | ||
File mqDirFile = new File(messageDir); | ||
if (mqDirFile.exists()) { | ||
FileUtils.deleteDirectory(mqDirFile); | ||
} | ||
File snapshotDirFile = new File(snapshotDir); | ||
if (snapshotDirFile.exists()) { | ||
FileUtils.copyDirectory(snapshotDirFile, mqDirFile); | ||
} | ||
logManager = new LogManager(messageDir); | ||
} catch (IOException ex) { | ||
LOG.error("readSnapshot error"); | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
|
||
@Override | ||
public void apply(byte[] dataBytes) { | ||
try { | ||
BrokerMessage.SendMessageRequest request = BrokerMessage.SendMessageRequest.parseFrom(dataBytes); | ||
// TODO: 找到segment log,写入消息 | ||
} catch (Exception ex) { | ||
LOG.warn("apply exception:", ex); | ||
} | ||
} | ||
|
||
} |
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
61 changes: 61 additions & 0 deletions
61
distmq-broker/src/main/java/com/github/wenweihu86/distmq/broker/log/LogManager.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,61 @@ | ||
package com.github.wenweihu86.distmq.broker.log; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by wenweihu86 on 2017/6/20. | ||
*/ | ||
public class LogManager { | ||
private static final Logger LOG = LoggerFactory.getLogger(LogManager.class); | ||
private String logDir; | ||
private Map<String, Map<Integer, SegmentedLog>> topicLogMap; | ||
|
||
public LogManager(String logDir) { | ||
this.topicLogMap = new HashMap<>(); | ||
this.logDir = logDir; | ||
File dirFile = new File(logDir); | ||
if (!dirFile.exists()) { | ||
dirFile.mkdirs(); | ||
} | ||
File[] topicDirs = dirFile.listFiles(); | ||
if (topicDirs != null) { | ||
for (File topicDir : topicDirs) { | ||
if (!topicDir.isDirectory()) { | ||
LOG.warn("{} is not directory", topicDir.getAbsolutePath()); | ||
continue; | ||
} | ||
LOG.info("Loading log from " + topicDir.getAbsolutePath()); | ||
if (!this.topicLogMap.containsKey(topicDir)) { | ||
this.topicLogMap.put(topicDir.getName(), new HashMap<Integer, SegmentedLog>()); | ||
} | ||
Map<Integer, SegmentedLog> queueMap = this.topicLogMap.get(topicDir.getName()); | ||
File[] queueDirs = topicDir.listFiles(); | ||
if (queueDirs != null) { | ||
for (File queueDir : queueDirs) { | ||
if (!queueDir.isDirectory()) { | ||
LOG.warn("{} is not directory", queueDir.getAbsolutePath()); | ||
continue; | ||
} | ||
Integer queueId = Integer.valueOf(queueDir.getName()); | ||
String fullQueuePath = logDir + File.separator + topicDir + File.separator + queueDir; | ||
SegmentedLog queueLog = new SegmentedLog(fullQueuePath); | ||
queueMap.put(queueId, queueLog); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public SegmentedLog getOrCreateQueueLog(String topic, int queue) { | ||
// TODO: | ||
// 需要读取zookeeper中topic/queue信息来判断该queue是否应该存在本broker集群分片 | ||
// zookeeper存储结构为/distmq/topics/topicName/queueId -> brokerShardingId | ||
return null; | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
data_dir = "./data" | ||
default_queue_num_per_topic = 8 | ||
max_segment_size = 100000000 # 100m | ||
|
||
[local_server] | ||
|