2017-04-19 21:44:31 +03:00
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
# ifndef LOG_DB_REQUEST_H_
# define LOG_DB_REQUEST_H_
# include <string>
# include <sstream>
2017-04-20 17:13:41 +03:00
# include "SyncRequest.h"
2017-04-19 21:44:31 +03:00
/**
* This class represents a log entry replication request . The replication request
* is synchronous : once it has been replicated in a majority of followers the
* client is notified ( SqlDB : : exec_wr ( ) call ) and DB updated .
*/
class LogDBRequest : public SyncRequest
{
public :
2017-04-20 17:13:41 +03:00
LogDBRequest ( unsigned int i , unsigned int t , const std : : ostringstream & o ) ;
2017-04-21 23:32:30 +03:00
LogDBRequest ( unsigned int i , unsigned int t , unsigned int pi ,
unsigned int pt , const char * s ) ;
2017-04-19 21:44:31 +03:00
virtual ~ LogDBRequest ( ) { } ;
/**
* This function decrements the number of remaining server to replicate
* this entry . If it reaches 0 , the client is notified
* @ return number of replicas for this log
*/
2017-04-20 17:13:41 +03:00
int replicated ( ) ;
2017-04-19 21:44:31 +03:00
2017-04-20 17:13:41 +03:00
/* ---------------------------------------------------------------------- */
/* Class access methods */
/* ---------------------------------------------------------------------- */
unsigned int index ( )
{
return _index ;
} ;
2017-04-19 21:44:31 +03:00
2017-04-21 23:32:30 +03:00
unsigned int prev_index ( )
{
return _prev_index ;
} ;
2017-04-20 17:13:41 +03:00
unsigned int term ( )
{
return _term ;
} ;
2017-04-19 21:44:31 +03:00
2017-04-21 23:32:30 +03:00
unsigned int prev_term ( )
{
return _prev_term ;
} ;
2017-04-20 17:13:41 +03:00
const std : : string & sql ( )
{
return _sql ;
} ;
2017-04-19 21:44:31 +03:00
2017-04-25 12:49:52 +03:00
int replicas ( )
{
return _replicas ;
}
int to_commit ( )
{
return _to_commit ;
}
void to_commit ( int c )
{
_to_commit = c ;
}
2017-04-21 23:32:30 +03:00
/**
* Function to lock the request
*/
void lock ( )
{
pthread_mutex_lock ( & mutex ) ;
} ;
/**
* Function to unlock the request
*/
void unlock ( )
{
pthread_mutex_unlock ( & mutex ) ;
} ;
2017-04-25 12:49:52 +03:00
2017-04-19 21:44:31 +03:00
private :
pthread_mutex_t mutex ;
/**
* Index for this log entry
*/
2017-04-20 17:13:41 +03:00
unsigned int _index ;
2017-04-19 21:44:31 +03:00
2017-04-21 23:32:30 +03:00
unsigned int _prev_index ;
2017-04-19 21:44:31 +03:00
/**
* Term where this log entry was generated
*/
2017-04-20 17:13:41 +03:00
unsigned int _term ;
2017-04-19 21:44:31 +03:00
2017-04-21 23:32:30 +03:00
unsigned int _prev_term ;
2017-04-19 21:44:31 +03:00
/**
* SQL command to exec in the DB to update ( INSERT , REPLACE , DROP )
*/
2017-04-20 17:13:41 +03:00
std : : string _sql ;
2017-04-19 21:44:31 +03:00
/**
* Remaining number of servers that need to replicate this record to commit
* it . Initialized to ( Number_Servers - 1 ) / 2
*/
2017-04-25 12:49:52 +03:00
int _to_commit ;
2017-04-19 21:44:31 +03:00
/**
* Total number of replicas for this entry
*/
2017-04-25 12:49:52 +03:00
int _replicas ;
2017-04-19 21:44:31 +03:00
} ;
# endif /*LOG_DB_REQUEST_H_*/