2012-01-19 16:49:54 +00:00
|
|
|
/*
|
|
|
|
* logserver.h
|
|
|
|
*
|
|
|
|
* Copyright 2009-2012 Yahoo! Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2010-01-23 02:13:59 +00:00
|
|
|
#ifndef _LOGSERVER_H_
|
|
|
|
#define _LOGSERVER_H_
|
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "datatuple.h"
|
|
|
|
|
|
|
|
#include <stasis/transactional.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2012-02-22 23:12:28 +00:00
|
|
|
#include "blsm.h"
|
2010-03-17 21:51:26 +00:00
|
|
|
|
2010-01-23 02:13:59 +00:00
|
|
|
#define STATS_ENABLED 1
|
|
|
|
|
|
|
|
#ifdef STATS_ENABLED
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <map>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct pthread_item;
|
|
|
|
|
|
|
|
struct pthread_data {
|
|
|
|
std::queue<pthread_item> *idleth_queue;
|
|
|
|
std::queue<int> *ready_queue;
|
|
|
|
std::queue<int> *work_queue;
|
|
|
|
pthread_mutex_t * qlock;
|
|
|
|
|
|
|
|
pthread_cond_t *selcond;
|
2010-02-23 23:54:44 +00:00
|
|
|
int* self_pipe;
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
pthread_cond_t * th_cond;
|
|
|
|
pthread_mutex_t * th_mut;
|
|
|
|
|
|
|
|
int *workitem; //id of the socket to work
|
|
|
|
|
2012-02-22 22:57:02 +00:00
|
|
|
blsm *ltable;
|
2010-01-23 02:13:59 +00:00
|
|
|
bool *sys_alive;
|
|
|
|
|
|
|
|
#ifdef STATS_ENABLED
|
|
|
|
int num_reqs;
|
|
|
|
struct timeval start_tv, stop_tv;
|
|
|
|
double work_time;
|
|
|
|
std::map<std::string, int> num_reqsc;
|
|
|
|
std::map<std::string, double> work_timec;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pthread_item{
|
|
|
|
pthread_t * th_handle;
|
|
|
|
pthread_data *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct serverth_data
|
|
|
|
{
|
|
|
|
int *server_socket;
|
|
|
|
int server_port;
|
|
|
|
std::queue<pthread_item> *idleth_queue;
|
|
|
|
std::queue<int> *ready_queue;
|
|
|
|
|
|
|
|
pthread_cond_t *selcond;
|
2010-02-23 23:54:44 +00:00
|
|
|
int * self_pipe;
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2010-02-10 21:49:50 +00:00
|
|
|
pthread_mutex_t *qlock;
|
2010-01-23 02:13:59 +00:00
|
|
|
};
|
|
|
|
|
2010-02-10 21:49:50 +00:00
|
|
|
void * thread_work_fn( void *);
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
class logserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
logserver(int nthreads, int server_port){
|
|
|
|
this->nthreads = nthreads;
|
|
|
|
this->server_port = server_port;
|
|
|
|
|
|
|
|
qlock = new pthread_mutex_t;
|
|
|
|
pthread_mutex_init(qlock,0);
|
|
|
|
|
|
|
|
ltable = 0;
|
|
|
|
|
|
|
|
#ifdef STATS_ENABLED
|
|
|
|
num_selevents = 0;
|
|
|
|
num_selcalls = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
~logserver()
|
|
|
|
{
|
|
|
|
delete qlock;
|
|
|
|
}
|
|
|
|
|
2012-02-22 22:57:02 +00:00
|
|
|
void startserver(blsm *ltable);
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
void stopserver();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
//main loop of server
|
|
|
|
//accept connections, assign jobs to threads
|
|
|
|
void eventLoop();
|
|
|
|
|
|
|
|
int server_port;
|
|
|
|
|
2010-01-27 23:34:33 +00:00
|
|
|
size_t nthreads;
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
bool sys_alive;
|
|
|
|
|
|
|
|
int serversocket; //server socket file descriptor
|
|
|
|
|
|
|
|
std::queue<int> ready_queue; //connections to go inside select
|
|
|
|
std::queue<int> work_queue; //connections to be processed by worker threads
|
|
|
|
std::queue<pthread_item> idleth_queue;
|
|
|
|
pthread_mutex_t *qlock;
|
|
|
|
|
|
|
|
pthread_t server_thread;
|
|
|
|
serverth_data *sdata;
|
|
|
|
pthread_cond_t *selcond; //server loop cond
|
2010-02-23 23:54:44 +00:00
|
|
|
int * self_pipe; // write a byte to self_pipe[1] to wake up select().
|
2010-01-23 02:13:59 +00:00
|
|
|
std::vector<pthread_item *> th_list; // list of threads
|
|
|
|
|
2012-02-22 22:57:02 +00:00
|
|
|
blsm *ltable;
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
#ifdef STATS_ENABLED
|
|
|
|
int num_reqs;
|
|
|
|
int num_selevents;
|
|
|
|
int num_selcalls;
|
|
|
|
struct timeval start_tv, stop_tv;
|
|
|
|
double tot_threadwork_time;
|
|
|
|
double tot_time;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|