Search in sources :

Example 6 with NodeOptions

use of io.dingodb.raft.option.NodeOptions in project dingo by dingodb.

the class NodeImpl method bootstrap.

public boolean bootstrap(final BootstrapOptions opts) throws InterruptedException {
    if (opts.getLastLogIndex() > 0 && (opts.getGroupConf().isEmpty() || opts.getFsm() == null)) {
        LOG.error("Invalid arguments for bootstrap, groupConf={}, fsm={}, lastLogIndex={}.", opts.getGroupConf(), opts.getFsm(), opts.getLastLogIndex());
        return false;
    }
    if (opts.getGroupConf().isEmpty()) {
        LOG.error("Bootstrapping an empty node makes no sense.");
        return false;
    }
    Requires.requireNonNull(opts.getServiceFactory(), "Null jraft service factory");
    this.serviceFactory = opts.getServiceFactory();
    // Term is not an option since changing it is very dangerous
    final long bootstrapLogTerm = opts.getLastLogIndex() > 0 ? 1 : 0;
    final LogId bootstrapId = new LogId(opts.getLastLogIndex(), bootstrapLogTerm);
    this.options = new NodeOptions();
    this.raftOptions = this.options.getRaftOptions();
    this.metrics = new NodeMetrics(opts.isEnableMetrics());
    this.options.setFsm(opts.getFsm());
    this.options.setLogUri(opts.getLogUri());
    this.options.setRaftMetaUri(opts.getRaftMetaUri());
    this.options.setSnapshotUri(opts.getSnapshotUri());
    this.configManager = new ConfigurationManager();
    // Create fsmCaller at first as logManager needs it to report error
    this.fsmCaller = new FSMCallerImpl();
    if (!initLogStorage()) {
        LOG.error("Fail to init log storage.");
        return false;
    }
    if (!initMetaStorage()) {
        LOG.error("Fail to init meta storage.");
        return false;
    }
    if (this.currTerm == 0) {
        this.currTerm = 1;
        if (!this.metaStorage.setTermAndVotedFor(1, new PeerId())) {
            LOG.error("Fail to set term.");
            return false;
        }
    }
    if (opts.getFsm() != null && !initFSMCaller(bootstrapId)) {
        LOG.error("Fail to init fsm caller.");
        return false;
    }
    final LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
    entry.getId().setTerm(this.currTerm);
    entry.setPeers(opts.getGroupConf().listPeers());
    entry.setLearners(opts.getGroupConf().listLearners());
    final List<LogEntry> entries = new ArrayList<>();
    entries.add(entry);
    final BootstrapStableClosure bootstrapDone = new BootstrapStableClosure();
    this.logManager.appendEntries(entries, bootstrapDone);
    if (!bootstrapDone.await().isOk()) {
        LOG.error("Fail to append configuration.");
        return false;
    }
    if (opts.getLastLogIndex() > 0) {
        if (!initSnapshotStorage()) {
            LOG.error("Fail to init snapshot storage.");
            return false;
        }
        final SynchronizedClosure snapshotDone = new SynchronizedClosure();
        this.snapshotExecutor.doSnapshot(snapshotDone);
        if (!snapshotDone.await().isOk()) {
            LOG.error("Fail to save snapshot, status={}.", snapshotDone.getStatus());
            return false;
        }
    }
    if (this.logManager.getFirstLogIndex() != opts.getLastLogIndex() + 1) {
        throw new IllegalStateException("First and last log index mismatch");
    }
    if (opts.getLastLogIndex() > 0) {
        if (this.logManager.getLastLogIndex() != opts.getLastLogIndex()) {
            throw new IllegalStateException("Last log index mismatch");
        }
    } else {
        if (this.logManager.getLastLogIndex() != opts.getLastLogIndex() + 1) {
            throw new IllegalStateException("Last log index mismatch");
        }
    }
    return true;
}
Also used : SynchronizedClosure(io.dingodb.raft.closure.SynchronizedClosure) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) NodeOptions(io.dingodb.raft.option.NodeOptions) LogId(io.dingodb.raft.entity.LogId) ConfigurationManager(io.dingodb.raft.conf.ConfigurationManager) LogEntry(io.dingodb.raft.entity.LogEntry) PeerId(io.dingodb.raft.entity.PeerId)

Aggregations

NodeOptions (io.dingodb.raft.option.NodeOptions)6 PeerId (io.dingodb.raft.entity.PeerId)4 Endpoint (io.dingodb.raft.util.Endpoint)4 Configuration (io.dingodb.raft.conf.Configuration)2 Store (io.dingodb.store.row.metadata.Store)2 RocksRawKVStore (io.dingodb.store.row.storage.RocksRawKVStore)2 File (java.io.File)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 Node (io.dingodb.raft.Node)1 RaftGroupService (io.dingodb.raft.RaftGroupService)1 RouteTable (io.dingodb.raft.RouteTable)1 SynchronizedClosure (io.dingodb.raft.closure.SynchronizedClosure)1 ConfigurationManager (io.dingodb.raft.conf.ConfigurationManager)1 LogEntry (io.dingodb.raft.entity.LogEntry)1 LogId (io.dingodb.raft.entity.LogId)1 RaftLogStorageOptions (io.dingodb.raft.option.RaftLogStorageOptions)1 RpcServer (io.dingodb.raft.rpc.RpcServer)1 BytesUtil (io.dingodb.raft.util.BytesUtil)1 Describer (io.dingodb.raft.util.Describer)1 CoordinatorContext (io.dingodb.server.coordinator.context.CoordinatorContext)1