forked from facebookarchive/scribe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_default.cpp
More file actions
147 lines (122 loc) · 3.92 KB
/
Copy pathenv_default.cpp
File metadata and controls
147 lines (122 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) 2007-2008 Facebook
//
// 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.
//
// See accompanying file LICENSE or visit the Scribe site at:
// http://developers.facebook.com/scribe/
//
// @author Bobby Johnson
// @author James Wang
// @author Jason Sobel
// @author Avinash Lakshman
// @author Anthony Giardullo
#include "common.h"
#include "scribe_server.h"
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::thrift::server;
using namespace apache::thrift::concurrency;
using namespace scribe::thrift;
using namespace scribe::concurrency;
using boost::shared_ptr;
/*
* Network configuration and directory services
*/
bool scribe::network_config::getService(const std::string& serviceName,
const std::string& options,
server_vector_t& _return) {
return false;
}
/*
* Concurrency mechanisms
*/
shared_ptr<ReadWriteMutex> scribe::concurrency::createReadWriteMutex() {
return shared_ptr<ReadWriteMutex>(new ReadWriteMutex());
}
/*
* Time functions
*/
unsigned long scribe::clock::nowInMsec() {
// There is a minor race condition between the 2 calls below,
// but the chance is really small.
// Get current time in timeval
struct timeval tv;
gettimeofday(&tv, NULL);
// Get current time in sec
time_t sec = time(NULL);
return ((unsigned long)sec) * 1000 + (tv.tv_usec / 1000);
}
/*
* Hash functions
*/
uint32_t scribe::integerhash::hash32(uint32_t key) {
return key;
}
uint32_t scribe::strhash::hash32(const char *s) {
// Use the djb2 hash (http://www.cse.yorku.ca/~oz/hash.html)
if (s == NULL) {
return 0;
}
uint32_t hash = 5381;
int c;
while ((c = *s++)) {
hash = ((hash << 5) + hash) + c; // hash * 33 + c
}
return hash;
}
/*
* Starting a scribe server.
*/
// note: this function uses global g_Handler.
void scribe::startServer() {
boost::shared_ptr<TProcessor> processor(new scribeProcessor(g_Handler));
/* This factory is for binary compatibility. */
boost::shared_ptr<TProtocolFactory> protocol_factory(
new TBinaryProtocolFactory(0, 0, false, false)
);
boost::shared_ptr<ThreadManager> thread_manager;
if (g_Handler->numThriftServerThreads > 1) {
// create a ThreadManager to process incoming calls
thread_manager = ThreadManager::newSimpleThreadManager(
g_Handler->numThriftServerThreads
);
shared_ptr<PosixThreadFactory> thread_factory(new PosixThreadFactory());
thread_manager->threadFactory(thread_factory);
thread_manager->start();
}
shared_ptr<TNonblockingServer> server(new TNonblockingServer(
processor,
protocol_factory,
g_Handler->port,
thread_manager
));
g_Handler->setServer(server);
LOG_OPER("Starting scribe server on port %lu", g_Handler->port);
fflush(stderr);
// throttle concurrent connections
unsigned long mconn = g_Handler->getMaxConn();
if (mconn > 0) {
LOG_OPER("Throttle max_conn to %lu", mconn);
server->setMaxConnections(mconn);
server->setOverloadAction(T_OVERLOAD_CLOSE_ON_ACCEPT);
}
server->serve();
// this function never returns
}
/*
* Stopping a scribe server.
*/
void scribe::stopServer() {
exit(0);
}