Revision 0c8299f1 src/logdb/LogDB.cc

View differences:

src/logdb/LogDB.cc
31 31
/* -------------------------------------------------------------------------- */
32 32
/* -------------------------------------------------------------------------- */
33 33

  
34
int LogDB::init_cb(void *nil, int num, char **values, char **names)
35
{
36
    if ( values[0] != 0 )
37
    {
38
        next_index = atoi(values[0]) + 1;
39
    }
40

  
41
    return 0;
42
}
43

  
44
LogDB::LogDB(SqlDB * _db):db(_db), next_index(0)
45
{
46
    ostringstream   oss;
47

  
48
    pthread_mutex_init(&mutex, 0);
49

  
50
    set_callback(static_cast<Callbackable::Callback>(&LogDB::init_cb));
51

  
52
    oss << "SELECT MAX(log_index) FROM logdb";
53

  
54
    db->exec_rd(oss,this);
55

  
56
    unset_callback();
57
};
58

  
59
LogDB::~LogDB()
60
{
61
    std::map<unsigned int, LogDBRequest *>::iterator it;
62

  
63
    for ( it = requests.begin(); it != requests.end(); ++it )
64
    {
65
        delete it->second;
66
    }
67

  
68
    delete db;
69
};
70

  
71
/* -------------------------------------------------------------------------- */
72
/* -------------------------------------------------------------------------- */
73

  
34 74
LogDBRequest * LogDB::get_request(unsigned int index)
35 75
{
36 76
    std::map<unsigned int, LogDBRequest *>::iterator it;
......
82 122
    ZoneServer * server = 0;
83 123
    unsigned int term   = 0;
84 124

  
125
    unsigned int num_servers;
126

  
85 127
    bool is_leader;
86 128

  
87
    if ( server_id != -1 )
129
    if ( server_id != -1 && zpool != 0 )
88 130
    {
89 131
        zone = zpool->get(zone_id, true);
90 132

  
91 133
        if ( zone != 0  )
92 134
        {
93
            if ( zone->servers_size() > 1 )
135
            num_servers = zone->servers_size();
136

  
137
            if ( num_servers > 1 )
94 138
            {
95 139
                server = zone->get_server(server_id);
96 140
                term   = server->get_term();
......
128 172
    // Insert log entry in the database and replicate on followers
129 173
    // -------------------------------------------------------------------------
130 174

  
131
    if ( ! is_leader )
175
    if ( !is_leader )
132 176
    {
133 177
        NebulaLog::log("DBM", Log::ERROR,"Tried to modify DB being a follower");
134 178
        return -1;
......
147 191

  
148 192
    LogDBRequest * lr = select(next_index);
149 193

  
194
    lr->to_commit(num_servers/2);
195

  
150 196
    requests.insert(std::make_pair(next_index, lr));
151 197

  
152 198
    next_index++;

Also available in: Unified diff