Search in sources :

Example 1 with Worker

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);
}
Also used : Path(java.nio.file.Path) java.util(java.util) Core(tezc.core.Core) TransportRecord(tezc.core.record.TransportRecord) ClusterWorker(tezc.core.worker.clusterworker.ClusterWorker) TakeSnapshotEvent(tezc.core.worker.clusterworker.TakeSnapshotEvent) ByteBuffer(java.nio.ByteBuffer) NodeRecord(tezc.core.record.NodeRecord) Buffer(tezc.base.common.Buffer) ConfigCommand(tezc.core.cluster.state.command.ConfigCommand) IncomingConnEvent(tezc.core.node.IncomingConnEvent) Path(java.nio.file.Path) Command(tezc.core.cluster.state.command.Command) Files(java.nio.file.Files) RegisterCommand(tezc.core.cluster.state.command.RegisterCommand) ClusterRecord(tezc.core.record.ClusterRecord) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) Connection(tezc.core.connection.Connection) Collectors(java.util.stream.Collectors) UncheckedIOException(java.io.UncheckedIOException) RaftState(tezc.core.cluster.state.RaftState) Paths(java.nio.file.Paths) PeerNode(tezc.core.node.PeerNode) Worker(tezc.core.worker.Worker) RaftException(tezc.base.exception.RaftException) State(tezc.State) tezc.core.msg(tezc.core.msg) NoOPCommand(tezc.core.cluster.state.command.NoOPCommand) FileChannel(java.nio.channels.FileChannel) Files(java.nio.file.Files) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) RaftException(tezc.base.exception.RaftException)

Aggregations

IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ByteBuffer (java.nio.ByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 StandardOpenOption (java.nio.file.StandardOpenOption)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 State (tezc.State)1 Buffer (tezc.base.common.Buffer)1 RaftException (tezc.base.exception.RaftException)1 Core (tezc.core.Core)1 RaftState (tezc.core.cluster.state.RaftState)1 Command (tezc.core.cluster.state.command.Command)1 ConfigCommand (tezc.core.cluster.state.command.ConfigCommand)1 NoOPCommand (tezc.core.cluster.state.command.NoOPCommand)1 RegisterCommand (tezc.core.cluster.state.command.RegisterCommand)1 Connection (tezc.core.connection.Connection)1