forked from facebookarchive/scribe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvDefault.cpp
More file actions
166 lines (138 loc) · 4.35 KB
/
EnvDefault.cpp
File metadata and controls
166 lines (138 loc) · 4.35 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// 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 "ScribeServer.h"
using namespace scribe::thrift;
using namespace scribe::concurrency;
namespace scribe {
/*
* Network configuration and directory services
*/
bool network_config::getService(const string& serviceName,
const string& options,
ServerVector* servers) {
return false;
}
/*
* Concurrency mechanisms
*/
shared_ptr<ReadWriteMutex> concurrency::createReadWriteMutex() {
return shared_ptr<ReadWriteMutex>(new ReadWriteMutex());
}
/*
* Time functions
*/
unsigned long 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
*/
size_t integerhash::operator()(const uint32_t x) const {
return (size_t)hash32(x);
}
uint32_t integerhash::hash32(uint32_t key) {
return key;
}
size_t strhash::operator()(const string &x) const {
return (size_t)hash32(x.c_str());
}
size_t strhash::operator()(const char *x) const {
return (size_t)hash32(x);
}
uint32_t 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 startServer() {
shared_ptr<TProcessor> processor(new scribeProcessor(g_handler));
/* This factory is for binary compatibility. */
shared_ptr<TProtocolFactory> protocolFactory(
new TBinaryProtocolFactory(0, 0, false, false)
);
shared_ptr<ThreadManager> threadManager;
if (g_handler->numThriftServerThreads > 1) {
// create a ThreadManager to process incoming calls
threadManager = ThreadManager::newSimpleThreadManager(
g_handler->numThriftServerThreads,
g_handler->getMaxConcurrentRequests()
);
shared_ptr<PosixThreadFactory> threadFactory(new PosixThreadFactory());
threadManager->threadFactory(threadFactory);
threadManager->start();
}
shared_ptr<TNonblockingServer> server(new TNonblockingServer(
processor,
protocolFactory,
g_handler->port,
threadManager
));
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 until server->stop() is called
}
/*
* Stopping a scribe server.
*/
void stopServer(shared_ptr<TNonblockingServer>& server) {
server->stop(); // actually does nothing
// this is the only way to stop a TNonblockingServer for now
exit(0);
}
/**
* Starting a background thread to check system memory. When memory exceeds
* limit, crash server.
*/
void startMemCheckerThread(unsigned long cycle,
float rssRatio,
float swapRatio) {
}
} //! namespace scribe