opengris-scaler 1.12.7__cp313-cp313-musllinux_1_2_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of opengris-scaler might be problematic. Click here for more details.

Files changed (234) hide show
  1. opengris_scaler-1.12.7.dist-info/METADATA +729 -0
  2. opengris_scaler-1.12.7.dist-info/RECORD +234 -0
  3. opengris_scaler-1.12.7.dist-info/WHEEL +5 -0
  4. opengris_scaler-1.12.7.dist-info/entry_points.txt +9 -0
  5. opengris_scaler-1.12.7.dist-info/licenses/LICENSE +201 -0
  6. opengris_scaler-1.12.7.dist-info/licenses/LICENSE.spdx +7 -0
  7. opengris_scaler-1.12.7.dist-info/licenses/NOTICE +8 -0
  8. opengris_scaler.libs/libcapnp-1-61c06778.1.0.so +0 -0
  9. opengris_scaler.libs/libgcc_s-2298274a.so.1 +0 -0
  10. opengris_scaler.libs/libkj-1-21b63b70.1.0.so +0 -0
  11. opengris_scaler.libs/libstdc++-08d5c7eb.so.6.0.33 +0 -0
  12. scaler/CMakeLists.txt +11 -0
  13. scaler/__init__.py +14 -0
  14. scaler/about.py +5 -0
  15. scaler/client/__init__.py +0 -0
  16. scaler/client/agent/__init__.py +0 -0
  17. scaler/client/agent/client_agent.py +210 -0
  18. scaler/client/agent/disconnect_manager.py +27 -0
  19. scaler/client/agent/future_manager.py +112 -0
  20. scaler/client/agent/heartbeat_manager.py +74 -0
  21. scaler/client/agent/mixins.py +89 -0
  22. scaler/client/agent/object_manager.py +98 -0
  23. scaler/client/agent/task_manager.py +64 -0
  24. scaler/client/client.py +635 -0
  25. scaler/client/future.py +252 -0
  26. scaler/client/object_buffer.py +129 -0
  27. scaler/client/object_reference.py +25 -0
  28. scaler/client/serializer/__init__.py +0 -0
  29. scaler/client/serializer/default.py +16 -0
  30. scaler/client/serializer/mixins.py +38 -0
  31. scaler/cluster/__init__.py +0 -0
  32. scaler/cluster/cluster.py +115 -0
  33. scaler/cluster/combo.py +148 -0
  34. scaler/cluster/object_storage_server.py +45 -0
  35. scaler/cluster/scheduler.py +83 -0
  36. scaler/config/__init__.py +0 -0
  37. scaler/config/defaults.py +87 -0
  38. scaler/config/loader.py +95 -0
  39. scaler/config/mixins.py +15 -0
  40. scaler/config/section/__init__.py +0 -0
  41. scaler/config/section/cluster.py +56 -0
  42. scaler/config/section/native_worker_adapter.py +44 -0
  43. scaler/config/section/object_storage_server.py +7 -0
  44. scaler/config/section/scheduler.py +53 -0
  45. scaler/config/section/symphony_worker_adapter.py +47 -0
  46. scaler/config/section/top.py +13 -0
  47. scaler/config/section/webui.py +16 -0
  48. scaler/config/types/__init__.py +0 -0
  49. scaler/config/types/object_storage_server.py +45 -0
  50. scaler/config/types/worker.py +57 -0
  51. scaler/config/types/zmq.py +79 -0
  52. scaler/entry_points/__init__.py +0 -0
  53. scaler/entry_points/cluster.py +133 -0
  54. scaler/entry_points/object_storage_server.py +41 -0
  55. scaler/entry_points/scheduler.py +135 -0
  56. scaler/entry_points/top.py +286 -0
  57. scaler/entry_points/webui.py +26 -0
  58. scaler/entry_points/worker_adapter_native.py +137 -0
  59. scaler/entry_points/worker_adapter_symphony.py +102 -0
  60. scaler/io/__init__.py +0 -0
  61. scaler/io/async_binder.py +85 -0
  62. scaler/io/async_connector.py +95 -0
  63. scaler/io/async_object_storage_connector.py +185 -0
  64. scaler/io/mixins.py +154 -0
  65. scaler/io/sync_connector.py +68 -0
  66. scaler/io/sync_object_storage_connector.py +185 -0
  67. scaler/io/sync_subscriber.py +83 -0
  68. scaler/io/utility.py +31 -0
  69. scaler/io/ymq/CMakeLists.txt +98 -0
  70. scaler/io/ymq/__init__.py +0 -0
  71. scaler/io/ymq/_ymq.pyi +96 -0
  72. scaler/io/ymq/_ymq.so +0 -0
  73. scaler/io/ymq/bytes.h +114 -0
  74. scaler/io/ymq/common.h +29 -0
  75. scaler/io/ymq/configuration.h +60 -0
  76. scaler/io/ymq/epoll_context.cpp +185 -0
  77. scaler/io/ymq/epoll_context.h +85 -0
  78. scaler/io/ymq/error.h +132 -0
  79. scaler/io/ymq/event_loop.h +55 -0
  80. scaler/io/ymq/event_loop_thread.cpp +64 -0
  81. scaler/io/ymq/event_loop_thread.h +46 -0
  82. scaler/io/ymq/event_manager.h +81 -0
  83. scaler/io/ymq/file_descriptor.h +203 -0
  84. scaler/io/ymq/interruptive_concurrent_queue.h +169 -0
  85. scaler/io/ymq/io_context.cpp +98 -0
  86. scaler/io/ymq/io_context.h +44 -0
  87. scaler/io/ymq/io_socket.cpp +299 -0
  88. scaler/io/ymq/io_socket.h +121 -0
  89. scaler/io/ymq/iocp_context.cpp +102 -0
  90. scaler/io/ymq/iocp_context.h +83 -0
  91. scaler/io/ymq/logging.h +163 -0
  92. scaler/io/ymq/message.h +15 -0
  93. scaler/io/ymq/message_connection.h +16 -0
  94. scaler/io/ymq/message_connection_tcp.cpp +672 -0
  95. scaler/io/ymq/message_connection_tcp.h +96 -0
  96. scaler/io/ymq/network_utils.h +179 -0
  97. scaler/io/ymq/pymod_ymq/bytes.h +113 -0
  98. scaler/io/ymq/pymod_ymq/exception.h +124 -0
  99. scaler/io/ymq/pymod_ymq/gil.h +15 -0
  100. scaler/io/ymq/pymod_ymq/io_context.h +166 -0
  101. scaler/io/ymq/pymod_ymq/io_socket.h +285 -0
  102. scaler/io/ymq/pymod_ymq/message.h +99 -0
  103. scaler/io/ymq/pymod_ymq/python.h +153 -0
  104. scaler/io/ymq/pymod_ymq/ymq.cpp +23 -0
  105. scaler/io/ymq/pymod_ymq/ymq.h +357 -0
  106. scaler/io/ymq/readme.md +114 -0
  107. scaler/io/ymq/simple_interface.cpp +80 -0
  108. scaler/io/ymq/simple_interface.h +24 -0
  109. scaler/io/ymq/tcp_client.cpp +367 -0
  110. scaler/io/ymq/tcp_client.h +75 -0
  111. scaler/io/ymq/tcp_operations.h +41 -0
  112. scaler/io/ymq/tcp_server.cpp +410 -0
  113. scaler/io/ymq/tcp_server.h +79 -0
  114. scaler/io/ymq/third_party/concurrentqueue.h +3747 -0
  115. scaler/io/ymq/timed_queue.h +272 -0
  116. scaler/io/ymq/timestamp.h +102 -0
  117. scaler/io/ymq/typedefs.h +20 -0
  118. scaler/io/ymq/utils.h +34 -0
  119. scaler/io/ymq/ymq.py +130 -0
  120. scaler/object_storage/CMakeLists.txt +50 -0
  121. scaler/object_storage/__init__.py +0 -0
  122. scaler/object_storage/constants.h +11 -0
  123. scaler/object_storage/defs.h +14 -0
  124. scaler/object_storage/io_helper.cpp +44 -0
  125. scaler/object_storage/io_helper.h +9 -0
  126. scaler/object_storage/message.cpp +56 -0
  127. scaler/object_storage/message.h +130 -0
  128. scaler/object_storage/object_manager.cpp +126 -0
  129. scaler/object_storage/object_manager.h +52 -0
  130. scaler/object_storage/object_storage_server.cpp +359 -0
  131. scaler/object_storage/object_storage_server.h +126 -0
  132. scaler/object_storage/object_storage_server.so +0 -0
  133. scaler/object_storage/pymod_object_storage_server.cpp +104 -0
  134. scaler/protocol/__init__.py +0 -0
  135. scaler/protocol/capnp/__init__.py +0 -0
  136. scaler/protocol/capnp/_python.py +6 -0
  137. scaler/protocol/capnp/common.capnp +63 -0
  138. scaler/protocol/capnp/message.capnp +216 -0
  139. scaler/protocol/capnp/object_storage.capnp +52 -0
  140. scaler/protocol/capnp/status.capnp +73 -0
  141. scaler/protocol/introduction.md +105 -0
  142. scaler/protocol/python/__init__.py +0 -0
  143. scaler/protocol/python/common.py +135 -0
  144. scaler/protocol/python/message.py +726 -0
  145. scaler/protocol/python/mixins.py +13 -0
  146. scaler/protocol/python/object_storage.py +118 -0
  147. scaler/protocol/python/status.py +279 -0
  148. scaler/protocol/worker.md +228 -0
  149. scaler/scheduler/__init__.py +0 -0
  150. scaler/scheduler/allocate_policy/__init__.py +0 -0
  151. scaler/scheduler/allocate_policy/allocate_policy.py +9 -0
  152. scaler/scheduler/allocate_policy/capability_allocate_policy.py +280 -0
  153. scaler/scheduler/allocate_policy/even_load_allocate_policy.py +159 -0
  154. scaler/scheduler/allocate_policy/mixins.py +55 -0
  155. scaler/scheduler/controllers/__init__.py +0 -0
  156. scaler/scheduler/controllers/balance_controller.py +65 -0
  157. scaler/scheduler/controllers/client_controller.py +131 -0
  158. scaler/scheduler/controllers/config_controller.py +31 -0
  159. scaler/scheduler/controllers/graph_controller.py +424 -0
  160. scaler/scheduler/controllers/information_controller.py +81 -0
  161. scaler/scheduler/controllers/mixins.py +201 -0
  162. scaler/scheduler/controllers/object_controller.py +147 -0
  163. scaler/scheduler/controllers/scaling_controller.py +86 -0
  164. scaler/scheduler/controllers/task_controller.py +373 -0
  165. scaler/scheduler/controllers/worker_controller.py +168 -0
  166. scaler/scheduler/object_usage/__init__.py +0 -0
  167. scaler/scheduler/object_usage/object_tracker.py +131 -0
  168. scaler/scheduler/scheduler.py +253 -0
  169. scaler/scheduler/task/__init__.py +0 -0
  170. scaler/scheduler/task/task_state_machine.py +92 -0
  171. scaler/scheduler/task/task_state_manager.py +61 -0
  172. scaler/ui/__init__.py +0 -0
  173. scaler/ui/constants.py +9 -0
  174. scaler/ui/live_display.py +118 -0
  175. scaler/ui/memory_window.py +146 -0
  176. scaler/ui/setting_page.py +47 -0
  177. scaler/ui/task_graph.py +370 -0
  178. scaler/ui/task_log.py +83 -0
  179. scaler/ui/utility.py +35 -0
  180. scaler/ui/webui.py +125 -0
  181. scaler/ui/worker_processors.py +85 -0
  182. scaler/utility/__init__.py +0 -0
  183. scaler/utility/debug.py +19 -0
  184. scaler/utility/event_list.py +63 -0
  185. scaler/utility/event_loop.py +58 -0
  186. scaler/utility/exceptions.py +42 -0
  187. scaler/utility/formatter.py +44 -0
  188. scaler/utility/graph/__init__.py +0 -0
  189. scaler/utility/graph/optimization.py +27 -0
  190. scaler/utility/graph/topological_sorter.py +11 -0
  191. scaler/utility/graph/topological_sorter_graphblas.py +174 -0
  192. scaler/utility/identifiers.py +105 -0
  193. scaler/utility/logging/__init__.py +0 -0
  194. scaler/utility/logging/decorators.py +25 -0
  195. scaler/utility/logging/scoped_logger.py +33 -0
  196. scaler/utility/logging/utility.py +183 -0
  197. scaler/utility/many_to_many_dict.py +123 -0
  198. scaler/utility/metadata/__init__.py +0 -0
  199. scaler/utility/metadata/profile_result.py +31 -0
  200. scaler/utility/metadata/task_flags.py +30 -0
  201. scaler/utility/mixins.py +13 -0
  202. scaler/utility/network_util.py +7 -0
  203. scaler/utility/one_to_many_dict.py +72 -0
  204. scaler/utility/queues/__init__.py +0 -0
  205. scaler/utility/queues/async_indexed_queue.py +37 -0
  206. scaler/utility/queues/async_priority_queue.py +70 -0
  207. scaler/utility/queues/async_sorted_priority_queue.py +45 -0
  208. scaler/utility/queues/indexed_queue.py +114 -0
  209. scaler/utility/serialization.py +9 -0
  210. scaler/version.txt +1 -0
  211. scaler/worker/__init__.py +0 -0
  212. scaler/worker/agent/__init__.py +0 -0
  213. scaler/worker/agent/heartbeat_manager.py +107 -0
  214. scaler/worker/agent/mixins.py +137 -0
  215. scaler/worker/agent/processor/__init__.py +0 -0
  216. scaler/worker/agent/processor/object_cache.py +107 -0
  217. scaler/worker/agent/processor/processor.py +279 -0
  218. scaler/worker/agent/processor/streaming_buffer.py +28 -0
  219. scaler/worker/agent/processor_holder.py +145 -0
  220. scaler/worker/agent/processor_manager.py +365 -0
  221. scaler/worker/agent/profiling_manager.py +109 -0
  222. scaler/worker/agent/task_manager.py +150 -0
  223. scaler/worker/agent/timeout_manager.py +19 -0
  224. scaler/worker/preload.py +84 -0
  225. scaler/worker/worker.py +264 -0
  226. scaler/worker_adapter/__init__.py +0 -0
  227. scaler/worker_adapter/native.py +154 -0
  228. scaler/worker_adapter/symphony/__init__.py +0 -0
  229. scaler/worker_adapter/symphony/callback.py +45 -0
  230. scaler/worker_adapter/symphony/heartbeat_manager.py +79 -0
  231. scaler/worker_adapter/symphony/message.py +24 -0
  232. scaler/worker_adapter/symphony/task_manager.py +288 -0
  233. scaler/worker_adapter/symphony/worker.py +205 -0
  234. scaler/worker_adapter/symphony/worker_adapter.py +142 -0
@@ -0,0 +1,410 @@
1
+ #include "scaler/io/ymq/tcp_server.h"
2
+
3
+ #ifdef __linux__
4
+ #include <netinet/in.h>
5
+ #include <netinet/tcp.h>
6
+ #include <sys/epoll.h>
7
+ #include <sys/socket.h>
8
+ #include <unistd.h>
9
+ #endif // __linux__
10
+ #ifdef _WIN32
11
+ // clang-format off
12
+ #include <windows.h>
13
+ #include <winsock2.h>
14
+ // clang-format on
15
+ #endif // _WIN32
16
+
17
+ #include <expected>
18
+ #include <memory>
19
+
20
+ #include "scaler/io/ymq/error.h"
21
+ #include "scaler/io/ymq/event_loop_thread.h"
22
+ #include "scaler/io/ymq/event_manager.h"
23
+ #include "scaler/io/ymq/io_socket.h"
24
+ #include "scaler/io/ymq/message_connection_tcp.h"
25
+ #include "scaler/io/ymq/network_utils.h"
26
+
27
+ namespace scaler {
28
+ namespace ymq {
29
+
30
+ #ifdef _WIN32
31
+ void TcpServer::prepareAcceptSocket()
32
+ {
33
+ _newConn = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
34
+ if (_newConn == INVALID_SOCKET) {
35
+ const int myErrno = GetErrorCode();
36
+ switch (myErrno) {
37
+ case WSAENOBUFS:
38
+ case WSAEMFILE:
39
+ case WSAENETDOWN:
40
+ case WSAEPROVIDERFAILEDINIT:
41
+ unrecoverableError({
42
+ Error::ErrorCode::ConfigurationError,
43
+ "Originated from",
44
+ "socket(2)",
45
+ "Errno is",
46
+ strerror(myErrno),
47
+ });
48
+ break;
49
+ case WSANOTINITIALISED:
50
+ case WSAEINPROGRESS:
51
+ case WSAEAFNOSUPPORT:
52
+ default:
53
+ unrecoverableError({
54
+ Error::ErrorCode::CoreBug,
55
+ "Originated from",
56
+ "socket(2)",
57
+ "Errno is",
58
+ strerror(myErrno),
59
+ });
60
+ break;
61
+ }
62
+ }
63
+
64
+ // TODO: Think about RawSocket abstraction that implements read/write/accept/connect/etc.
65
+ DWORD bytesReturned = 0;
66
+ const size_t requiredRedundantSpace = 16;
67
+ if (!_acceptExFunc(
68
+ _serverFd,
69
+ _newConn,
70
+ _buffer,
71
+ 0,
72
+ sizeof(sockaddr_in) + requiredRedundantSpace,
73
+ sizeof(sockaddr_in) + requiredRedundantSpace,
74
+ &bytesReturned,
75
+ _eventManager.get())) {
76
+ const int myErrno = GetErrorCode();
77
+ if (myErrno != ERROR_IO_PENDING) {
78
+ CloseAndZeroSocket(_newConn);
79
+ unrecoverableError({
80
+ Error::ErrorCode::CoreBug,
81
+ "Originated from",
82
+ "AcceptEx",
83
+ "Errno is",
84
+ strerror(myErrno),
85
+ "_serverFd",
86
+ _serverFd,
87
+ });
88
+ }
89
+ return;
90
+ }
91
+ // acceptEx never succeed.
92
+ }
93
+ #endif // _WIN32
94
+
95
+ int TcpServer::createAndBindSocket()
96
+ {
97
+ #ifdef __linux__
98
+ int server_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
99
+ if (server_fd == -1) {
100
+ unrecoverableError({
101
+ Error::ErrorCode::ConfigurationError,
102
+ "Originated from",
103
+ "socket(2)",
104
+ "Errno is",
105
+ strerror(errno),
106
+ "_serverFd",
107
+ _serverFd,
108
+ });
109
+
110
+ // _onBindReturn(std::unexpected(Error {Error::ErrorCode::ConfigurationError}));
111
+ return -1;
112
+ }
113
+ #endif // __linux__
114
+ #ifdef _WIN32
115
+ auto server_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
116
+ if (server_fd == -1) {
117
+ const int myErrno = GetErrorCode();
118
+ switch (myErrno) {
119
+ case WSAENOBUFS:
120
+ case WSAEMFILE:
121
+ case WSAENETDOWN:
122
+ case WSAEPROVIDERFAILEDINIT:
123
+ unrecoverableError({
124
+ Error::ErrorCode::ConfigurationError,
125
+ "Originated from",
126
+ "socket(2)",
127
+ "Errno is",
128
+ strerror(myErrno),
129
+ });
130
+ break;
131
+ case WSANOTINITIALISED:
132
+ case WSAEINPROGRESS:
133
+ case WSAEAFNOSUPPORT:
134
+ default:
135
+ unrecoverableError({
136
+ Error::ErrorCode::CoreBug,
137
+ "Originated from",
138
+ "socket(2)",
139
+ "Errno is",
140
+ myErrno,
141
+ });
142
+ break;
143
+ }
144
+ return -1;
145
+ }
146
+ unsigned long turnOnNonBlocking = 1;
147
+ ioctlsocket(server_fd, FIONBIO, &turnOnNonBlocking);
148
+ #endif // _WIN32
149
+
150
+ int optval = 1;
151
+ if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&optval, sizeof(optval)) == -1) {
152
+ _logger.log(
153
+ Logger::LoggingLevel::error,
154
+ "Originated from",
155
+ "setsockopt(2)",
156
+ "Errno is",
157
+ strerror(GetErrorCode()) // ,
158
+ );
159
+ CloseAndZeroSocket(server_fd);
160
+ _onBindReturn(std::unexpected(Error {Error::ErrorCode::SetSockOptNonFatalFailure}));
161
+ return -1;
162
+ }
163
+
164
+ if (bind(server_fd, &_addr, sizeof(_addr)) == -1) {
165
+ CloseAndZeroSocket(server_fd);
166
+ unrecoverableError({
167
+ Error::ErrorCode::ConfigurationError,
168
+ "Originated from",
169
+ "bind(2)",
170
+ "Errno is",
171
+ strerror(GetErrorCode()),
172
+ "server_fd",
173
+ server_fd,
174
+ });
175
+
176
+ return -1;
177
+ }
178
+
179
+ if (listen(server_fd, SOMAXCONN) == -1) {
180
+ CloseAndZeroSocket(server_fd);
181
+ unrecoverableError({
182
+ Error::ErrorCode::ConfigurationError,
183
+ "Originated from",
184
+ "listen(2)",
185
+ "Errno is",
186
+ strerror(GetErrorCode()),
187
+ "server_fd",
188
+ server_fd,
189
+ });
190
+
191
+ return -1;
192
+ }
193
+
194
+ return server_fd;
195
+ }
196
+
197
+ TcpServer::TcpServer(
198
+ std::shared_ptr<EventLoopThread> eventLoopThread,
199
+ std::string localIOSocketIdentity,
200
+ sockaddr addr,
201
+ BindReturnCallback onBindReturn) noexcept
202
+ : _eventLoopThread(eventLoopThread)
203
+ , _onBindReturn(std::move(onBindReturn))
204
+ , _serverFd {}
205
+ , _addr(std::move(addr))
206
+ , _localIOSocketIdentity(std::move(localIOSocketIdentity))
207
+ , _eventManager(std::make_unique<EventManager>())
208
+ #ifdef _WIN32
209
+ , _acceptExFunc {}
210
+ , _newConn {}
211
+ , _buffer {}
212
+ #endif // _WIN32
213
+ {
214
+ _eventManager->onRead = [this] { this->onRead(); };
215
+ _eventManager->onWrite = [this] { this->onWrite(); };
216
+ _eventManager->onClose = [this] { this->onClose(); };
217
+ _eventManager->onError = [this] { this->onError(); };
218
+ }
219
+
220
+ void TcpServer::onCreated()
221
+ {
222
+ _serverFd = createAndBindSocket();
223
+ if (_serverFd == -1) {
224
+ _serverFd = 0;
225
+ return;
226
+ }
227
+ #ifdef __linux__
228
+ _eventLoopThread->_eventLoop.addFdToLoop(_serverFd, EPOLLIN | EPOLLET, this->_eventManager.get());
229
+ #endif // __linux__
230
+ #ifdef _WIN32
231
+ // Events and EventManager are not used here.
232
+ _eventLoopThread->_eventLoop.addFdToLoop(_serverFd, 0, nullptr);
233
+ // Retrieve AcceptEx pointer once
234
+ GUID guidAcceptEx = WSAID_ACCEPTEX;
235
+ DWORD bytesReturned = 0;
236
+ if (WSAIoctl(
237
+ _serverFd,
238
+ SIO_GET_EXTENSION_FUNCTION_POINTER,
239
+ &guidAcceptEx,
240
+ sizeof(guidAcceptEx),
241
+ &_acceptExFunc,
242
+ sizeof(_acceptExFunc),
243
+ &bytesReturned,
244
+ nullptr,
245
+ nullptr) == SOCKET_ERROR) {
246
+ unrecoverableError({
247
+ Error::ErrorCode::CoreBug,
248
+ "Originated from",
249
+ "WSAIoctl",
250
+ "Errno is",
251
+ strerror(GetErrorCode()),
252
+ "_serverFd",
253
+ _serverFd,
254
+ });
255
+ }
256
+ prepareAcceptSocket();
257
+ #endif // _WIN32
258
+
259
+ _onBindReturn({});
260
+ }
261
+
262
+ void TcpServer::onRead()
263
+ {
264
+ #ifdef __linux__
265
+ while (true) {
266
+ sockaddr remoteAddr {};
267
+ socklen_t remoteAddrLen = sizeof(remoteAddr);
268
+
269
+ int fd = accept4(_serverFd, &remoteAddr, &remoteAddrLen, SOCK_NONBLOCK | SOCK_CLOEXEC);
270
+ if (fd < 0) {
271
+ const int myErrno = errno;
272
+ switch (myErrno) {
273
+ // Not an error
274
+ // case EWOULDBLOCK: // same as EAGAIN
275
+ case EAGAIN:
276
+ case ECONNABORTED: return;
277
+
278
+ case ENOTSOCK:
279
+ case EOPNOTSUPP:
280
+ case EINVAL:
281
+ case EBADF:
282
+ unrecoverableError({
283
+ Error::ErrorCode::CoreBug,
284
+ "Originated from",
285
+ "accept4(2)",
286
+ "Errno is",
287
+ strerror(myErrno),
288
+ "_serverFd",
289
+ _serverFd,
290
+ });
291
+ break;
292
+
293
+ case EINTR:
294
+ unrecoverableError({
295
+ Error::ErrorCode::SignalNotSupported,
296
+ "Originated from",
297
+ "accept4(2)",
298
+ "Errno is",
299
+ strerror(myErrno),
300
+ });
301
+ break;
302
+
303
+ // config
304
+ case EMFILE:
305
+ case ENFILE:
306
+ case ENOBUFS:
307
+ case ENOMEM:
308
+ case EFAULT:
309
+ case EPERM:
310
+ case EPROTO:
311
+ case ENOSR:
312
+ case ESOCKTNOSUPPORT:
313
+ case EPROTONOSUPPORT:
314
+ case ETIMEDOUT:
315
+ default:
316
+ unrecoverableError({
317
+ Error::ErrorCode::ConfigurationError,
318
+ "Originated from",
319
+ "accept4(2)",
320
+ "Errno is",
321
+ strerror(myErrno),
322
+ });
323
+ break;
324
+ }
325
+ }
326
+
327
+ if (remoteAddrLen > sizeof(remoteAddr)) {
328
+ unrecoverableError({
329
+ Error::ErrorCode::IPv6NotSupported,
330
+ "Originated from",
331
+ "accept4(2)",
332
+ "remoteAddrLen",
333
+ remoteAddrLen,
334
+ "sizeof(remoteAddr)",
335
+ sizeof(remoteAddr),
336
+ });
337
+ }
338
+
339
+ std::string id = this->_localIOSocketIdentity;
340
+ auto sock = this->_eventLoopThread->_identityToIOSocket.at(id);
341
+ sock->onConnectionCreated(setNoDelay(fd), getLocalAddr(fd), remoteAddr, false);
342
+ }
343
+ #endif
344
+ #ifdef _WIN32
345
+ if (setsockopt(
346
+ _newConn, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, reinterpret_cast<char*>(&_serverFd), sizeof(_serverFd)) ==
347
+ SOCKET_ERROR) {
348
+ CloseAndZeroSocket(_serverFd);
349
+ CloseAndZeroSocket(_newConn);
350
+ unrecoverableError({
351
+ Error::ErrorCode::ConfigurationError,
352
+ "Originated from",
353
+ "setsockopt(SO_UPDATE_ACCEPT_CONTEXT)",
354
+ "Errno is",
355
+ strerror(GetErrorCode()),
356
+ "_serverFd",
357
+ _serverFd,
358
+ });
359
+ }
360
+
361
+ unsigned long mode = 1;
362
+ if (ioctlsocket(_newConn, FIONBIO, &mode) == SOCKET_ERROR) {
363
+ const int myErrno = GetErrorCode();
364
+ switch (myErrno) {
365
+ case WSANOTINITIALISED:
366
+ case WSAEINPROGRESS:
367
+ case WSAENOTSOCK:
368
+ unrecoverableError({
369
+ Error::ErrorCode::CoreBug,
370
+ "Originated from",
371
+ "ioctlsocket(FIONBIO)",
372
+ "Errno is",
373
+ strerror(myErrno),
374
+ "_newConn",
375
+ _newConn,
376
+ });
377
+
378
+ case WSAENETDOWN:
379
+ case WSAEFAULT:
380
+ default:
381
+ unrecoverableError({
382
+ Error::ErrorCode::ConfigurationError,
383
+ "Originated from",
384
+ "ioctlsocket(FIONBIO)",
385
+ "Errno is",
386
+ strerror(myErrno),
387
+ "_newConn",
388
+ _newConn,
389
+ });
390
+ break;
391
+ }
392
+ }
393
+
394
+ const auto& id = this->_localIOSocketIdentity;
395
+ auto& sock = this->_eventLoopThread->_identityToIOSocket.at(id);
396
+ sock->onConnectionCreated(setNoDelay(_newConn), getLocalAddr(_newConn), getRemoteAddr(_newConn), false);
397
+ prepareAcceptSocket();
398
+ #endif // _WIN32
399
+ }
400
+
401
+ TcpServer::~TcpServer() noexcept
402
+ {
403
+ if (_serverFd != 0) {
404
+ _eventLoopThread->_eventLoop.removeFdFromLoop(_serverFd);
405
+ CloseAndZeroSocket(_serverFd);
406
+ }
407
+ }
408
+
409
+ } // namespace ymq
410
+ } // namespace scaler
@@ -0,0 +1,79 @@
1
+ #pragma once
2
+
3
+ // C++
4
+ #ifdef __linux__
5
+ #include <sys/socket.h>
6
+ #endif // __linux__
7
+ #ifdef _WIN32
8
+ // clang-format off
9
+ #include <winsock2.h>
10
+ #include <mswsock.h>
11
+ // clang-format on
12
+ #endif // _WIN32
13
+
14
+ #include <memory>
15
+
16
+ // First-party
17
+ #include "scaler/io/ymq/configuration.h"
18
+ #include "scaler/io/ymq/logging.h"
19
+
20
+ namespace scaler {
21
+ namespace ymq {
22
+
23
+ class EventLoopThread;
24
+ class EventManager;
25
+
26
+ class TcpServer {
27
+ public:
28
+ using BindReturnCallback = Configuration::BindReturnCallback;
29
+
30
+ TcpServer(
31
+ std::shared_ptr<EventLoopThread> eventLoop,
32
+ std::string localIOSocketIdentity,
33
+ sockaddr addr,
34
+ BindReturnCallback onBindReturn) noexcept;
35
+ TcpServer(const TcpServer&) = delete;
36
+ TcpServer& operator=(const TcpServer&) = delete;
37
+ ~TcpServer() noexcept;
38
+
39
+ void onCreated();
40
+ std::shared_ptr<EventLoopThread> _eventLoopThread;
41
+
42
+ private:
43
+ // Implementation defined method. accept(3) should happen here.
44
+ // This function will call user defined onAcceptReturn()
45
+ // It will handle error it can handle. If it is unreasonable to
46
+ // handle the error here, pass it to onAcceptReturn()
47
+ void onRead();
48
+ void onWrite() {}
49
+ void onClose() { printf("%s\n", __PRETTY_FUNCTION__); }
50
+ void onError() { printf("%s\n", __PRETTY_FUNCTION__); }
51
+
52
+ int createAndBindSocket();
53
+
54
+ BindReturnCallback _onBindReturn;
55
+ // Because here we need to pass the sizeof _serverFd to setsockopt.
56
+ #ifdef _WIN32
57
+ SOCKET _serverFd;
58
+ #endif
59
+ #ifdef __linux__
60
+ int _serverFd;
61
+ #endif
62
+ sockaddr _addr;
63
+ std::string _localIOSocketIdentity;
64
+
65
+ std::unique_ptr<EventManager> _eventManager; // will copy the `onRead()` to itself
66
+
67
+ Logger _logger;
68
+
69
+ #ifdef _WIN32
70
+ // We break the decl rule here because otherwise the implementation would be messy.
71
+ LPFN_ACCEPTEX _acceptExFunc;
72
+ SOCKET _newConn;
73
+ char _buffer[128];
74
+ void prepareAcceptSocket();
75
+ #endif // _WIN32
76
+ };
77
+
78
+ } // namespace ymq
79
+ } // namespace scaler