metaConfig.setDataPath(this.getDataPath());
final String topic = "SamsaMasterBrokerUnitTest";
metaConfig.getTopics().add(topic);
metaConfig.setNumPartitions(5);
metaConfig.setMaxSegmentSize(1024 * 1024);
final MetaMorphosisBroker metaBroker = new MetaMorphosisBroker(metaConfig);
final IdWorker idWorker = metaBroker.getIdWorker();
final byte[] data = new byte[1024];
// ��Ҫ��recover��offset��Ϣ
final List<MessageInfo> allMsgs = new ArrayList<MessageInfo>();
final Random random = new Random();
// ��������
for (int i = 0; i < 20000; i++) {
// ��5������������
final int partition = i % 4;
final int step = i;
final MessageStore store = metaBroker.getStoreManager().getOrCreateMessageStore(topic, partition);
final long msgId = idWorker.nextId();
store.append(msgId, new PutCommand(topic, partition, data, null, 0, 0), new AppendCallback() {
@Override
public void appendComplete(final Location location) {
// �������1044����Ϊlocation��offset��������Ϣ�����
allMsgs.add(new MessageInfo(msgId, location.getOffset() + 1044, partition));
}
});
store.flush();
}
// ��������Ϣ�����ѡ��20��
final List<MessageInfo> offsetInfos = new ArrayList<SamsaMasterBrokerUnitTest.MessageInfo>();
for (int i = 0; i < 20; i++) {
offsetInfos.add(allMsgs.get(random.nextInt(allMsgs.size())));
}
// ģ�ⶩ��������offset���Ա��ƫ
this.mockConsumersOffset(topic, metaBroker, offsetInfos);
System.out.println("Add messages done");
try {
final Properties props = new Properties();
props.setProperty("recoverOffset", "true");
props.setProperty("slave", "localhost:8121");
props.setProperty("recoverParallel", "false");
assertTrue(metaBroker.getBrokerZooKeeper().getZkConfig().zkEnable);
this.broker.init(metaBroker, props);
// recover����ʱ�Ȳ�������zk
assertFalse(metaBroker.getBrokerZooKeeper().getZkConfig().zkEnable);
// ������meta broker
metaBroker.start();
// ��ʼrecover
this.broker.start();
// ȷ���Ƿ�ȫ����ƫ�ɹ�
final String consumerId = "SamsaMasterBrokerUnitTest";
final int brokerId = metaBroker.getMetaConfig().getBrokerId();
// ����consumer�ڵ���Ϣ���Ա�recover
final String consumersPath = metaBroker.getBrokerZooKeeper().getMetaZookeeper().consumersPath;
final ZkClient zkClient = metaBroker.getBrokerZooKeeper().getZkClient();
int consumerIdCounter = 0;
for (final MessageInfo msgInfo : offsetInfos) {
final int consumerIndex = consumerIdCounter++;
final String offsetPath =
consumersPath + "/" + consumerId + consumerIndex + "/offsets/" + topic + "/" + brokerId + "-"
+ msgInfo.partition;
assertTrue(zkClient.exists(offsetPath));
final String dataStr = ZkUtils.readDataMaybeNull(zkClient, offsetPath);
assertNotNull(dataStr);
final OffsetInfo offsetInfo = SamsaMasterBroker.readOffsetInfo(offsetPath, dataStr);
System.out.println(msgInfo + " " + dataStr);
assertEquals(msgInfo.msgId, offsetInfo.msgId);
assertEquals(msgInfo.offset, offsetInfo.offset);
}
// ȷ�ϵ��������ƫ��0
final String offsetPath = consumersPath + "/" + consumerId + "/offsets/" + topic + "/" + brokerId + "-" + 4;
assertTrue(zkClient.exists(offsetPath));
final String dataStr = ZkUtils.readDataMaybeNull(zkClient, offsetPath);
assertNotNull(dataStr);
final OffsetInfo offsetInfo = SamsaMasterBroker.readOffsetInfo(offsetPath, dataStr);
assertEquals(-1, offsetInfo.msgId);
assertEquals(0, offsetInfo.offset);
}
finally {
if (metaBroker != null) {
metaBroker.stop();
}
this.broker.stop();
this.stopMockSlave();
}