-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile_submitter.cpp
More file actions
330 lines (286 loc) · 9.4 KB
/
tile_submitter.cpp
File metadata and controls
330 lines (286 loc) · 9.4 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*------------------------------------------------------------------------------
*
* Program to submit tile render requests directly to the queue.
*
* Author: matt.amos@mapquest.com
* Author: kevin.kreiser@mapquest.com
*
* Copyright 2010-1 Mapquest, Inc. All Rights reserved.
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*-----------------------------------------------------------------------------*/
#include "config.hpp"
#include "tile_protocol.hpp"
#include "logging/logger.hpp"
#include "dqueue/distributed_queue.hpp"
#include "tile_path_grammar.hpp"
#include <boost/optional.hpp>
#include <boost/format.hpp>
#include <boost/noncopyable.hpp>
#include <boost/program_options.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/microsec_time_clock.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iostream>
#include <fstream>
using rendermq::tile_protocol;
using rendermq::cmdRenderBulk;
using boost::optional;
using boost::shared_ptr;
using std::string;
using std::ifstream;
namespace po = boost::program_options;
namespace bt = boost::posix_time;
namespace qi = boost::spirit::qi;
// interface to grab a bunch of tiles from somewhere. this
// might be fed in from stdin, read from a file or a database
// or basically wherever.
struct bunch_of_tiles
: public boost::noncopyable
{
virtual ~bunch_of_tiles() {}
// gets the next tile from the bunch, or none if there are
// no tiles left to do.
virtual optional<tile_protocol> next() = 0;
};
void oops(const tile_protocol &tile)
{
LOG_ERROR(boost::format("Not supposed to receive anything back "
"from broker, but got %1%!")
% tile);
}
void runner_poll(dqueue::runner &runner, long timeout)
{
zmq::pollitem_t items [] = {
{ NULL, 0, ZMQ_POLLIN, 0 },
{ NULL, 0, ZMQ_POLLIN, 0 },
};
// for the moment assume there's only one pollitem for the distributed queue
assert(runner.num_pollitems() == 2);
runner.fill_pollitems(&items[0]);
// poll
try {
zmq::poll(&items[0], 2, timeout);
} catch (const zmq::error_t &) {
// ignore and loop...
return;
}
runner.handle_pollitems(&items[0]);
}
void settle(dqueue::runner &runner)
{
LOG_INFO("Waiting for queue to settle");
while (runner.queue_length() >= 1000000)
{
runner_poll(runner, 1000);
}
}
void enqueue_tiles(shared_ptr<bunch_of_tiles> tiles,
const string &dqueue_config,
size_t short_queue_length,
long pause_time,
zmq::context_t &ctx)
{
dqueue::runner runner(dqueue_config, ctx);
bool queue_short = false;
LOG_INFO("Setting up session for submitting jobs...");
// setup the queue runner
runner.default_handler(
dqueue::runner::handler_function_t(&oops));
// settle the queue runner - give it some time to contact
// brokers, etc...
settle(runner);
LOG_INFO("Ready to start submitting jobs.");
// while there's jobs to be done, and the queue isn't too
// large, stick 'em in the queue.
while (true)
{
//short circuit if we have no tiles left
optional<tile_protocol> mtile = tiles->next();
if (!mtile)
{
break;
}
// loop while the queue is long, waiting for an opportunity
// to insert this tile...
while (!queue_short)
{
// wait until something happens
runner_poll(runner, -1);
queue_short = runner.queue_length() < short_queue_length;
}
//submit this tile
tile_protocol tile(mtile.get());
// clip to metatile
tile.x &= ~0x7;
tile.y &= ~0x7;
// ensure there is no response expected
tile.id = -1;
// set to lowest priority
tile.status = cmdRenderBulk;
// submit the tile
try
{
runner.put_job(tile);
}
catch (const dqueue::broker_error &e)
{
LOG_ERROR(boost::format("Queue error sending job %1% to queue: %2%.") % tile % e.what());
}
catch (const std::exception &e)
{
LOG_ERROR(boost::format("Runtime error sending job %1% to queue: %2%.") % tile % e.what());
}
catch ( ... )
{
LOG_ERROR(boost::format("Unknown error sending job %1% to queue.") % tile);
}
// do a short poll, just so that the queue length keeps
// getting updated.
bt::ptime wait_until = bt::microsec_clock::local_time() + bt::milliseconds(pause_time / 1000);
while (bt::microsec_clock::local_time() < wait_until)
{
runner_poll(runner, pause_time);
queue_short = runner.queue_length() < short_queue_length;
}
}
}
struct read_from_iostream
: public bunch_of_tiles
{
read_from_iostream(std::istream &in)
: m_in(in), m_line_count(0)
{
}
~read_from_iostream() {}
optional<tile_protocol> next()
{
if (m_in.eof() || m_in.bad())
{
return optional<tile_protocol>();
}
else
{
char line[1024];
m_in.getline(line, (sizeof line) - 1);
m_line_count += 1;
if (m_in.gcount() == (sizeof line) - 1)
{
// line read probably too long. stop processing here to
// avoid injecting garbage into the queue.
LOG_ERROR(boost::format("Found line %1% is longer than %2% characters, "
"which is longer than expected for a tile path. "
"Assuming this is a mistake and exiting now.")
% m_line_count % ((sizeof line) - 1));
return optional<tile_protocol>();
}
const string sline(line);
tile_protocol tile;
bool result = qi::parse(sline.begin(), sline.end(), m_grammar, tile);
if (result)
{
return optional<tile_protocol>(tile);
}
else
{
LOG_ERROR(boost::format("Line %1% does not match the expected tile path "
"format, e.g: /map/0/0/0.jpg.")
% m_line_count);
return optional<tile_protocol>();
}
}
}
private:
std::istream &m_in;
size_t m_line_count;
rendermq::tile_path_grammar<std::string::const_iterator> m_grammar;
};
int main(int argc, char *argv[])
{
std::string dqueue_config = "dqueue.conf";
size_t short_queue_length = 2000;
long pause_time = 5000;
po::options_description cmdline_options("Tile submitter\n"
"Version: " VERSION "\n"
"\n"
"Generic options:");
cmdline_options.add_options()
("help,h", "Print this help message")
("queue-config,C",
po::value<std::string>(&dqueue_config)->default_value("dqueue.conf"),
"Path to the dqueue configuration file.")
("short-length,l",
po::value<size_t>(&short_queue_length)->default_value(2000),
"How short does the queue have to be to continue submitting jobs.")
("pause-time,t",
po::value<long>(&pause_time)->default_value(5000),
"How long to wait between submitting jobs in microseconds. Use this "
"to alter the steady-state rate at which jobs are put in the queue.")
("input-file,f", po::value<std::string>(),
"Required input file, use '-' to read from stdin.")
;
po::variables_map vm;
try
{
po::store(po::command_line_parser(argc, argv)
.options(cmdline_options)
.run(), vm);
po::notify(vm);
if (vm.count("help"))
{
std::cerr << cmdline_options << "\n";
return EXIT_SUCCESS;
}
}
catch (std::exception &e)
{
std::cerr << e.what() << "\n";
return EXIT_FAILURE;
}
if (!vm.count("queue-config") || !vm.count("short-length"))
{
std::cerr << cmdline_options << "\n";
return EXIT_FAILURE;
}
shared_ptr<bunch_of_tiles> tiles;
ifstream in;
if (vm.count("input-file") == 0)
{
std::cerr << "No input-file option provided.\n";
std::cerr << cmdline_options << "\n";
return EXIT_FAILURE;
}
string file_name(vm["input-file"].as<std::string>());
if (file_name != "-")
{
in.open(file_name.c_str(), ifstream::in);
if (in.fail())
{
LOG_ERROR(boost::format("Cannot open file `%1%' for reading.") % file_name);
return EXIT_FAILURE;
}
tiles = shared_ptr<bunch_of_tiles>(new read_from_iostream(in));
}
else
{
tiles = shared_ptr<bunch_of_tiles>(new read_from_iostream(std::cin));
}
zmq::context_t ctx(1);
enqueue_tiles(tiles, dqueue_config, short_queue_length, pause_time, ctx);
return 0;
}