<commit_before>#include <cstdint>
#include <sys/types.h>

#include <chrono>
#include <memory>
#include <thread>
#include <vector>

#include "raft_c_if.h"
#include "raft_shm.h"

using boost::interprocess::anonymous_instance;
using boost::interprocess::interprocess_mutex;

using namespace raft;

static void start_fsm_worker(RaftFSM* fsm);
static void run_fsm_worker(RaftFSM* fsm);

static void dispatch_fsm_apply(CallSlot<LogEntry, true>& slot);
static void dispatch_fsm_apply_cmd(CallSlot<LogEntry, true>& slot);
static void dispatch_fsm_snapshot(CallSlot<Filename, false>& slot);
static void dispatch_fsm_restore(CallSlot<Filename, false>& slot);

static void init_err_msgs();
static RaftFSM*    fsm;
static std::thread fsm_worker;

namespace alloc {

//pool_allocator< CallSlot<ApplyArgs, true> > apply;

}

static std::vector<const char*> err_msgs;

const char* raft_err_msg(RaftError err)
{
    if (err < err_msgs.size()) {
        return err_msgs.at(err);
    } else {
        return "Unknown error";
    }
}

pid_t raft_init(RaftFSM *fsm_, int argc, char *argv[])
{
    init_err_msgs();
    raft::process_args(argc, argv);
    raft::shm_init("raft", true);

    fsm = fsm_;
    raft::run_raft();
    zlog_info(shm_cat, "Started Raft process: pid %d.", raft_pid);
    raft::scoreboard->wait_for_raft(raft_pid);
    zlog_debug(shm_cat, "Raft is running.");
    start_fsm_worker(fsm);
    return raft_pid;
}

bool raft_is_leader()
{<commit_msg>Start FSM worker earlier, for restores.<commit_after>#include <cstdint>
#include <sys/types.h>

#include <chrono>
#include <memory>
#include <thread>
#include <vector>

#include "raft_c_if.h"
#include "raft_shm.h"

using boost::interprocess::anonymous_instance;
using boost::interprocess::interprocess_mutex;

using namespace raft;

static void start_fsm_worker(RaftFSM* fsm);
static void run_fsm_worker(RaftFSM* fsm);

static void dispatch_fsm_apply(CallSlot<LogEntry, true>& slot);
static void dispatch_fsm_apply_cmd(CallSlot<LogEntry, true>& slot);
static void dispatch_fsm_snapshot(CallSlot<Filename, false>& slot);
static void dispatch_fsm_restore(CallSlot<Filename, false>& slot);

static void init_err_msgs();
static RaftFSM*    fsm;
static std::thread fsm_worker;

namespace alloc {

//pool_allocator< CallSlot<ApplyArgs, true> > apply;

}

static std::vector<const char*> err_msgs;

const char* raft_err_msg(RaftError err)
{
    if (err < err_msgs.size()) {
        return err_msgs.at(err);
    } else {
        return "Unknown error";
    }
}

pid_t raft_init(RaftFSM *fsm_, int argc, char *argv[])
{
    init_err_msgs();
    raft::process_args(argc, argv);
    raft::shm_init("raft", true);

    fsm = fsm_;
    raft::run_raft();
    zlog_info(shm_cat, "Started Raft process: pid %d.", raft_pid);
    start_fsm_worker(fsm);
    raft::scoreboard->wait_for_raft(raft_pid);
    zlog_debug(shm_cat, "Raft is running.");
    return raft_pid;
}

bool raft_is_leader()
{
{'fraction_non_alphanumeric': 0.1300479123887748, 'fraction_numerical': 0.0, 'mean_word_length': 5.3960612691466086, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3893181', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1070, 'n_words': 235}