aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/fdispatch/program/fdispatch.cpp
blob: 9dd4e81f91a2e6c269040eb028a4af8dc19838c9 (plain) (blame)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "fdispatch.h" 
#include "engineadapter.h"
#include "rpc.h"
#include <vespa/searchcore/fdispatch/search/querycacheutil.h>
#include <vespa/searchcore/fdispatch/search/nodemanager.h>
#include <vespa/searchcore/util/eventloop.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/config/helper/configgetter.hpp>
#include <vespa/vespalib/net/crypto_engine.h>

#include <vespa/log/log.h>
LOG_SETUP(".fdispatch");

#ifndef V_TAG
#define V_TAG "NOTAG"
#endif

using search::fs4transport::FS4PersistentPacketStreamer;
using vespa::config::search::core::FdispatchrcConfig;
using vespa::config::search::core::internal::InternalFdispatchrcType;
using vespalib::compression::CompressionConfig;

char FastS_VersionTag[] = V_TAG;

namespace fdispatch
{

FastS_FNETAdapter::FastS_FNETAdapter(FastS_AppContext *appCtx)
    : _appCtx(appCtx),
      _nodeManager(),
      _timeKeeper(NULL),
      _transport(NULL),
      _last_now(0.0),
      _live_counter(0),
      _task()
{ }

FastS_FNETAdapter::~FastS_FNETAdapter()
{
    fini();
}

void
FastS_FNETAdapter::init()
{
    _nodeManager = _appCtx->GetNodeManager();
    _timeKeeper  = _appCtx->GetTimeKeeper();
    _transport   = _appCtx->GetFNETTransport();
    _last_now    = _timeKeeper->GetTime();
    _task.reset(new MyTask(_transport->GetScheduler(), *this));
    _task->ScheduleNow();
}

void
FastS_FNETAdapter::perform()
{
    double now = _timeKeeper->GetTime();
    double delta = now - _last_now;
    if (delta >= 3.0) {
        LOG(warning, "FNET loop high latency: %.3f", delta);
    }
    _last_now = now;
    ++_live_counter;
    _nodeManager->CheckEvents(_timeKeeper);
}

void
FastS_FNETAdapter::fini()
{
    if (_task) {
        _task->Kill();
        _task.reset();
    }
}

Fdispatch::~Fdispatch()
{
    if (_transportServer) {
        _transportServer->shutDown(); // sync shutdown
    }
    _FNET_adapter.fini();
    if (_nodeManager) {
        _nodeManager->ShutdownConfig();
    }
    if (_transport && _transportStarted) {
        _transport->ShutDown(true); // sync shutdown
    }
    if (_rpc) {
        _rpc->ShutDown(); // sync shutdown
    }

    LOG(debug, "Will close threadpool");
    _mypool->Close();
    _executor.shutdown().sync();
    LOG(debug, "Has closed threadpool");
    _transportServer.reset();
    _engineAdapter.reset();
    _nodeManager.reset();
    _transport.reset();
    _rpc.reset();
    _mypool.reset();
}

FNET_Transport *
Fdispatch::GetFNETTransport()
{
    return _transport.get();
}

FNET_Scheduler *
Fdispatch::GetFNETScheduler()
{
    return (_transport) ? _transport->GetScheduler() : nullptr;
}

FastS_NodeManager *
Fdispatch::GetNodeManager()
{
    return _nodeManager.get();
}

FastS_DataSetCollection *
Fdispatch::GetDataSetCollection()
{
    return ( _nodeManager) ? _nodeManager->GetDataSetCollection() : nullptr;
}

FastOS_ThreadPool *
Fdispatch::GetThreadPool()
{
    return _mypool.get();
}

bool
Fdispatch::Failed()
{
    return ( (_transportServer && _transportServer->isFailed())) || _needRestart;
}

bool
Fdispatch::CheckTempFail()
{
    bool ret;
    bool failflag = _nodeManager->GetTempFail();
    unsigned int FNETLiveCounter;

    ret = true;

    FNETLiveCounter = _FNET_adapter.GetLiveCounter();
    if (FNETLiveCounter == _lastFNETLiveCounter) {
        if (_FNETLiveCounterFailed) {
            failflag = true;                // Still failure
        } else if (!_FNETLiveCounterDanger) {
            _FNETLiveCounterDanger = true;
            _FNETLiveCounterDangerStart.SetNow();
        } else if (_FNETLiveCounterDangerStart.MilliSecsToNow() >= 6000) {
            LOG(error, "fdispatch::Fdispatch::CheckTempFail: FNET inactive for 6 seconds, deadlock ?");
            _FNETLiveCounterFailed = true;      // Note that we failed
            failflag = true;                // Force temporary failure
        } else if (_FNETLiveCounterDangerStart.MilliSecsToNow() >= 3000 &&
                   !_FNETLiveCounterWarned) {
            _FNETLiveCounterWarned = true;
            LOG(warning, "fdispatch::Fdispatch::CheckTempFail: FNET inactive for 3 seconds");
        }
    } else {
        if (_FNETLiveCounterFailed || _FNETLiveCounterWarned) {
            LOG(warning, "fdispatch::Fdispatch::CheckTempFail: FNET active again");
        }
        _FNETLiveCounterFailed = false;
        _FNETLiveCounterWarned = false;
        _FNETLiveCounterDanger = false;
        _lastFNETLiveCounter = FNETLiveCounter;
    }

    if (failflag == _tempFail)
        return ret;

    if (_transportServer) {
        if (failflag) {
            _transportServer->setListen(false);
            LOG(error, "Disabling fnet server interface");
        } else {
            _transportServer->setListen(true);
            LOG(info, "Reenabling fnet server interface");
        }
    }
    _tempFail = failflag;
    return ret;
}


/**
 * Make the httpd and Monitor, and let a Thread execute each.
 * Set up stuff as specified in the fdispatch-rc-file.
 */
Fdispatch::Fdispatch(const config::ConfigUri &configUri)
    : _executor(1, 128 * 1024),
      _mypool(),
      _engineAdapter(),
      _transportServer(),
      _componentConfig(),
      _nodeManager(),
      _transport(),
      _FNET_adapter(this),
      _rpc(),
      _config(),
      _configUri(configUri),
      _fdispatchrcFetcher(configUri.getContext()),
      _rndGen(),
      _partition(0),
      _tempFail(false),
      _FNETLiveCounterDanger(false),
      _FNETLiveCounterWarned(false),
      _FNETLiveCounterFailed(false),
      _transportStarted(false),
      _lastFNETLiveCounter(false),
      _FNETLiveCounterDangerStart(),
      _timeouts(0u),
      _checkLimit(0u),
      _healthPort(0),
      _needRestart(false)
{
    int64_t cfgGen = -1;
    _config = config::ConfigGetter<FdispatchrcConfig>::
              getConfig(cfgGen, _configUri.getConfigId(), _configUri.getContext());
    LOG(config, "fdispatch version %s (RPC-port: %d, transport at %d)",
                FastS_VersionTag, _config->frtport, _config->ptport);

    _componentConfig.addConfig(vespalib::ComponentConfigProducer::Config("fdispatch", cfgGen,
                                       "config only obtained at startup"));
    _fdispatchrcFetcher.subscribe<FdispatchrcConfig>(configUri.getConfigId(), this);
    _fdispatchrcFetcher.start();
}

namespace {

bool needRestart(const FdispatchrcConfig & curr, const FdispatchrcConfig & next)
{
    if (curr.frtport != next.frtport) {
        LOG(warning, "FRT port has changed from %d to %d.", curr.frtport, next.frtport);
        return true;
    }
    if (curr.ptport != next.ptport) {
        LOG(warning, "PT port has changed from %d to %d.", curr.ptport, next.ptport);
        return true;
    }
    if (curr.healthport != next.healthport) {
        LOG(warning, "Health port has changed from %d to %d.", curr.healthport, next.healthport);
        return true;
    }
    return false;
}

}

void Fdispatch::configure(std::unique_ptr<FdispatchrcConfig> cfg)
{
    if (cfg && _config) {
        if ( needRestart(*_config, *cfg) ) {
            LOG(warning, "Will restart by abort now.");
            _needRestart.store(true);
        }
    }
}

namespace {

CompressionConfig::Type
convert(InternalFdispatchrcType::Packetcompresstype type)
{
    switch (type) {
      case InternalFdispatchrcType::LZ4: return CompressionConfig::LZ4;
      default: return CompressionConfig::LZ4;
    }
}

}

bool
Fdispatch::Init()
{
    int  maxthreads;

    _tempFail = false;
    _FNETLiveCounterDanger = false;
    _FNETLiveCounterWarned = false;
    _FNETLiveCounterFailed = false;
    _lastFNETLiveCounter = 0;
    _timeouts = 0;
    _checkLimit = 60;

    FS4PersistentPacketStreamer::Instance.SetCompressionLimit(_config->packetcompresslimit);
    FS4PersistentPacketStreamer::Instance.SetCompressionLevel(_config->packetcompresslevel);
    FS4PersistentPacketStreamer::Instance.SetCompressionType(convert(_config->packetcompresstype));
    

    LOG(debug, "Creating FNET transport");
    _transport = std::make_unique<FNET_Transport>(_config->transportthreads);

    // grab node slowness limit defaults

    FastS_DataSetDesc::SetDefaultSlowQueryLimitFactor(_config->defaultslowquerylimitfactor);
    FastS_DataSetDesc::SetDefaultSlowQueryLimitBias(_config->defaultslowquerylimitbias);
    FastS_DataSetDesc::SetDefaultSlowDocsumLimitFactor(_config->defaultslowdocsumlimitfactor);
    FastS_DataSetDesc::SetDefaultSlowDocsumLimitBias(_config->defaultslowdocsumlimitbias);

    maxthreads = _config->maxthreads;
    _mypool = std::make_unique<FastOS_ThreadPool>(256 * 1024, maxthreads);

    // Max interval betw read from socket.
    FastS_TimeOut::_val[FastS_TimeOut::maxSockSilent] = _config->maxsocksilent;

    if (_transport) {
        _transport->SetIOCTimeOut((uint32_t) (FastS_TimeOut::_val[FastS_TimeOut::maxSockSilent] * 1000.0));
    }

    char timestr[40];
    FastS_TimeOut::WriteTime(timestr, sizeof(timestr), FastS_TimeOut::_val[FastS_TimeOut::maxSockSilent]);
    LOG(debug, "VERBOSE: Max time between successful read from a socket: %s", timestr);

    FastS_QueryCacheUtil::_systemMaxHits = std::numeric_limits<int>::max();
    LOG(debug, "VERBOSE: maxhits: %d", FastS_QueryCacheUtil::_systemMaxHits);

    FastS_QueryCacheUtil::_maxOffset = std::numeric_limits<int>::max();
    const uint32_t linesize = 1;
    if (FastS_QueryCacheUtil::_systemMaxHits < linesize
        && FastS_QueryCacheUtil::_maxOffset < linesize - FastS_QueryCacheUtil::_systemMaxHits) {
        LOG(warning, "maxoffset must be >= %d! (overriding config value)", linesize - FastS_QueryCacheUtil::_systemMaxHits);
        FastS_QueryCacheUtil::_maxOffset = linesize - FastS_QueryCacheUtil::_systemMaxHits;
    }
    LOG(debug, "VERBOSE: maxoffset: %d", FastS_QueryCacheUtil::_maxOffset);

    _partition = _config->partition;

    int ptportnum = _config->ptport;

    LOG(debug, "Using port number %d", ptportnum);

    _nodeManager = std::make_unique<FastS_NodeManager>(_componentConfig, this, _partition);

    GetFNETTransport()->SetTCPNoDelay(_config->transportnodelay);

    if (ptportnum == 0) {
        throw vespalib::IllegalArgumentException("fdispatchrc.ptportnum must be non-zero, most likely an issue with config delivery.");
    }

    _engineAdapter = std::make_unique<fdispatch::EngineAdapter>(this, _mypool.get());
    _transportServer = std::make_unique<TransportServer>(*_engineAdapter, *_engineAdapter, *_engineAdapter, ptportnum, search::engine::TransportServer::DEBUG_ALL);
    _transportServer->setTCPNoDelay(_config->transportnodelay);

    if (!_transportServer->start()) {
        _transportServer.reset();
        _engineAdapter.reset();
        LOG(error, "CRITICAL: Failed to init upwards FNET transport on port %d", ptportnum);
        return false;
    }

    _nodeManager->SubscribePartMap(_configUri);

    if (_config->frtport != 0) {
        _rpc = std::make_unique<FastS_fdispatch_RPC>(this);
        if (!_rpc->Init(_config->frtport, _configUri.getConfigId())) {
            LOG(error, "RPC init failed");
            _rpc.reset();
        }
    } else {
        _rpc.reset();
    }

    // Kick off fdispatch administrative threads.
    if (_transport) {
        _FNET_adapter.init();
        bool rc = _transport->Start(_mypool.get());
        if (rc) {
            LOG(debug, "Started FNET transport");
            _transportStarted = true;
        } else {
            LOG(error, "Failed to start FNET transport");
        }
    }
    FastOS_Thread::Sleep(1000);
    if (_rpc) {
        _rpc->Start();
    }
    _healthPort = _config->healthport;
    return true;
}

uint32_t
Fdispatch::getDispatchLevel()
{
    return _config->dispatchlevel;
}
    
}