forked from facebookarchive/nfusr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NfsConnectionPool.h
105 lines (92 loc) · 3.27 KB
/
NfsConnectionPool.h
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
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#pragma once
#include <condition_variable>
#include <memory>
#include <mutex>
#include <vector>
#include "NfsConnection.h"
#include "logger.h"
#include "ClientStats.h"
/// @brief Hold a target connection URL and current connection state.
class ConnectionTarget {
public:
explicit ConnectionTarget(
std::shared_ptr<std::string> url,
bool connected = false)
: url_(url), connected_(connected) {}
std::shared_ptr<std::string> getUrl() const {
return url_;
}
bool getConnected() const {
return connected_;
}
void setConnected(bool connected) {
connected_ = connected;
}
void setBlacklisted(std::chrono::duration<int> duration) {
blacklistedUntil_ = std::chrono::steady_clock::now() + duration;
}
bool isBlacklisted() {
return std::chrono::steady_clock::now() < blacklistedUntil_;
}
private:
std::shared_ptr<std::string> url_;
bool connected_;
std::chrono::time_point<std::chrono::steady_clock> blacklistedUntil_;
};
/// @brief NfsConnectionPool maintains a set of NfsConnections with servers.
///
/// This class attempts to keep some number of simultaneous connections open
/// out of a (possibly larger) set of server URLs. When a connection fails,
/// it attempts to set up a new one from the pool of unattached URLs.
///
/// The handy get() method returns a connection to use. Currently it simply
/// round-robins over live connections. This might benefit from more clever
/// scheduling in future.
class NfsConnectionPool {
public:
NfsConnectionPool(
std::vector<std::string>& urls,
const char* hostscript,
const int scriptRefreshSeconds,
std::shared_ptr<nfusr::Logger> logger,
std::shared_ptr<ClientStats> stats,
unsigned simultaneousConnections,
int nfsTimeoutMs);
~NfsConnectionPool();
std::shared_ptr<NfsConnection> get();
void failed(std::shared_ptr<NfsConnection> conn);
private:
std::vector<std::string> runScript(const char * scriptPath);
std::shared_ptr<nfusr::Logger> logger_;
std::shared_ptr<ClientStats> stats_;
// lock_ protects following members:
std::mutex lock_;
std::vector<std::shared_ptr<NfsConnection>> liveConnections_;
std::vector<std::shared_ptr<NfsConnection>> zombieConnections_;
std::vector<ConnectionTarget> targets_;
unsigned next_;
// end of members protected by _lock.
unsigned liveTarget_; // number of live connections we want to maintain.
int nfsTimeoutMs_;
void reaper();
std::thread reaperThread_;
bool terminateReaper_;
std::mutex reaperMutex_;
std::condition_variable reaperCondvar_;
void dumpStats(std::shared_ptr<nfusr::Logger> logger, const char *prefix);
// scriptMutex_ protects terminalScript_
std::mutex scriptMutex_;
std::condition_variable scriptCondvar_;
std::thread scriptThread_; // a thread to run script and update targets_
bool terminateScript_;
const size_t nUrls_; // number of urls initially passed in from command line
void updateTargets(const char* hostscript, const int scriptRefreshSeconds);
};