use of tezc.core.worker.Worker in project tezcRaft by tezc.
the class Cluster method openStores.
/**
* Open stores
*
* Called prior to open a cluster. This method reads cluster log from
* file storage if exists.
*
* @throws IOException on any IO error
*/
private void openStores() throws IOException {
List<Path> paths = Files.walk(path).filter(Files::isRegularFile).filter(p -> p.toString().endsWith(".store")).collect(Collectors.toList());
List<MappedStore> tmp = new ArrayList<>();
for (Path path : paths) {
try {
MappedStore store = new MappedStore(worker, path, storeSize);
/*
* This could happen on an race condition, store was created
* just before used first time. So, this store does not contain
* any data, we simply delete it.
*/
if (store.getBaseIndex() == -1) {
store.delete();
worker.logWarn("Inconsistent " + "log file is deleted", store.getPath());
continue;
}
tmp.add(store);
} catch (Exception e) {
worker.logError(e);
}
}
/*
* Store files should be sequential, if we detect files out of other,
* sequential ones will be kept, rest will be deleted. As we will start
* as follower, leader will send us the missing log anyway.
*/
tmp.sort(Comparator.comparingLong(MappedStore::getBaseIndex));
for (int i = 1; i < tmp.size(); i++) {
long base = tmp.get(i).getBaseIndex();
long prevEnd = tmp.get(i - 1).getEndIndex();
if (base != prevEnd + 1) {
List<MappedStore> sub = tmp.subList(i, tmp.size());
sub.forEach(store -> {
try {
store.delete();
worker.logWarn("Inconsistent " + "log file is deleted", store.getPath());
} catch (Exception e) {
worker.logError(e);
}
});
sub.clear();
break;
}
}
/*
* First log store must match with the snapshot index, if not, store
* files are inconsistent(in case of a race condition, manual log file
* deletion etc..)
*
* As we will start
* as follower, leader will send us the missing log anyway.
*/
if (tmp.size() > 0 && tmp.get(0).getBaseIndex() != snapshotIndex + 1) {
tmp.forEach(store -> {
try {
store.delete();
worker.logWarn("Inconsistent " + "log file is deleted", store.getPath());
} catch (Exception e) {
worker.logError(e);
}
});
tmp.clear();
}
stores.addAll(tmp);
}
Aggregations