-
Notifications
You must be signed in to change notification settings - Fork 22
/
Get.d
341 lines (269 loc) · 12.4 KB
/
Get.d
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
/*******************************************************************************
Client DHT Get v0 request handler.
Copyright:
Copyright (c) 2017 sociomantic labs GmbH. All rights reserved.
License:
Boost Software License Version 1.0. See LICENSE.txt for details.
*******************************************************************************/
module dhtproto.client.request.internal.Get;
/*******************************************************************************
Imports
*******************************************************************************/
import ocean.transition;
import ocean.core.VersionCheck;
import ocean.util.log.Logger;
/*******************************************************************************
Module logger
*******************************************************************************/
static private Logger log;
static this ( )
{
log = Log.lookup("dhtproto.client.request.internal.Get");
}
/*******************************************************************************
Get request implementation.
Note that request structs act simply as namespaces for the collection of
symbols required to implement a request. They are never instantiated and
have no fields or non-static functions.
The client expects several things to be present in a request struct:
1. The static constants request_type and request_code
2. The UserSpecifiedParams struct, containing all user-specified request
setup (including a notifier)
3. The Notifier delegate type
4. Optionally, the Controller type (if the request can be controlled,
after it has begun)
5. The handler() function
6. The all_finished_notifier() function
The RequestCore mixin provides items 1 and 2.
*******************************************************************************/
public struct Get
{
import dhtproto.common.Get;
public import dhtproto.client.request.Get;
import dhtproto.common.RequestCodes;
import swarm.neo.client.mixins.RequestCore;
import swarm.neo.client.RequestHandlers;
import swarm.neo.client.RequestOnConn;
import swarm.neo.request.Command;
import dhtproto.client.internal.SharedResources;
import ocean.io.select.protocol.generic.ErrnoIOException: IOError;
/***************************************************************************
Data which the request needs while it is progress. An instance of this
struct is stored per connection on which the request runs and is passed
to the request handler.
***************************************************************************/
private static struct SharedWorking
{
/// Enum indicating the ways in which the request may end.
public enum Result
{
Failure, // Default value; unknown error (presumably in client)
Timeout, // Request timed out in client before completion
NoNode, // No node responsible for key
Error, // Node or I/O error
NoRecord, // Record not found
Got // Got record
}
/// The way in which the request ended. Used by the finished notifier to
/// decide what kind of notification (if any) to send to the user.
Result result;
}
/***************************************************************************
Data which each request-on-conn needs while it is progress. An instance
of this struct is stored per connection on which the request runs and is
passed to the request handler.
***************************************************************************/
private static struct Working
{
// Dummy (not required by this request)
}
/***************************************************************************
Request core. Mixes in the types `NotificationInfo`, `Notifier`,
`Params`, `Context` plus the static constants `request_type` and
`request_code`.
***************************************************************************/
mixin RequestCore!(RequestType.SingleNode, RequestCode.Get, 0, Args,
SharedWorking, Notification);
/***************************************************************************
Request handler. Called from RequestOnConn.runHandler().
FIXME: Note that the logic for retrying the request on other nodes which
previously covered the hash has not been properly tested. This will
require a full neo implementation of the Redistribute request. See
https://github.com/sociomantic-tsunami/dhtnode/issues/21
Params:
use_node = delegate to get an EventDispatcher for the node with the
specified address
context_blob = untyped chunk of data containing the serialized
context of the request which is to be handled
***************************************************************************/
public static void handler ( UseNodeDg use_node, void[] context_blob )
{
auto context = Get.getContext(context_blob);
context.shared_working.result = SharedWorking.Result.Failure;
auto shared_resources = SharedResources.fromObject(
context.shared_resources);
scope acquired_resources = shared_resources.new RequestResources;
// Try getting the record from the nodes responsible for the hash,
// querying them in newest -> oldest order
bool get_called;
shared_resources.node_hash_ranges.getFromNode(
context.user_params.args.key,
acquired_resources.getNodeHashRangeBuffer(), use_node,
( RequestOnConn.EventDispatcher conn )
{
get_called = true;
return getFromNode(conn, context);
}
);
if ( !get_called )
{
// Bail out if no nodes cover the record's hash
context.shared_working.result = SharedWorking.Result.NoNode;
return;
}
}
/***************************************************************************
Tries to gets the record from the specified node.
Params:
conn = event dispatcher for the connection to send the record to
context = deserialized request context, including record/value
Returns:
true to try another node, false if finished (the record was fetched
or an error occurred). All error cases abort the request (return
false), as it is not possible to know if the node where the error
occurred has the record or not.
***************************************************************************/
private static bool getFromNode ( RequestOnConn.EventDispatcher conn,
Get.Context* context )
{
try
{
// Send request info to node
conn.send(
( conn.Payload payload )
{
payload.add(Get.cmd.code);
payload.add(Get.cmd.ver);
payload.addArray(context.user_params.args.channel);
payload.add(context.user_params.args.key);
}
);
static if (!hasFeaturesFrom!("swarm", 4, 7))
conn.flush();
// Receive supported code from node
auto supported = conn.receiveValue!(SupportedStatus)();
if ( !Get.handleSupportedCodes(supported, context,
conn.remote_address) )
{
// Request not supported; abort further handling.
context.shared_working.result = SharedWorking.Result.Error;
return false;
}
else
{
// Request supported; read result code from node.
auto result = conn.receiveValue!(MessageType)();
with ( MessageType ) switch ( result )
{
case Got:
context.shared_working.result =
SharedWorking.Result.Got;
// Receive record value from node.
conn.receive(
( const(void)[] const_payload )
{
Const!(void)[] payload = const_payload;
auto value =
conn.message_parser.getArray!(void)(payload);
Notification n;
n.received = RequestDataInfo(context.request_id,
value);
Get.notify(context.user_params, n);
}
);
return false;
case NoRecord:
context.shared_working.result =
SharedWorking.Result.NoRecord;
return true;
case WrongNode:
context.shared_working.result =
SharedWorking.Result.Error;
// The node is not reponsible for the key. Notify the user.
Notification n;
n.wrong_node = RequestNodeInfo(context.request_id,conn.remote_address);
Get.notify(context.user_params, n);
return false;
case Error:
context.shared_working.result =
SharedWorking.Result.Error;
// The node returned an error code. Notify the user.
Notification n;
n.node_error = RequestNodeInfo(context.request_id,
conn.remote_address);
Get.notify(context.user_params, n);
return false;
default:
log.warn("Received unknown message code {} from node "
~ "in response to Get request. Treating as "
~ "Error.", result);
goto case Error;
}
}
assert(false);
}
catch ( RequestOnConn.AbortException e )
{
context.shared_working.result = SharedWorking.Result.Timeout;
throw e;
}
catch ( IOError e )
{
context.shared_working.result = SharedWorking.Result.Error;
// A connection error occurred. Notify the user.
auto info = RequestNodeExceptionInfo(context.request_id,
conn.remote_address, e);
Notification n;
n.node_disconnected = info;
Get.notify(context.user_params, n);
return false;
}
assert(false);
}
/***************************************************************************
Request finished notifier. Called from Request.handlerFinished().
Params:
context_blob = untyped chunk of data containing the serialized
context of the request which is finishing
***************************************************************************/
public static void all_finished_notifier ( void[] context_blob )
{
auto context = Get.getContext(context_blob);
Notification n;
with ( SharedWorking.Result ) switch ( context.shared_working.result )
{
case NoRecord:
n.no_record = RequestInfo(context.request_id);
break;
case Timeout:
n.timed_out = RequestInfo(context.request_id);
break;
case NoNode:
n.no_node = RequestInfo(context.request_id);
break;
case Failure:
n.failure = RequestInfo(context.request_id);
break;
case Got:
// Got notification was already handled in getFromNode(), where
// the value received from the node is available.
case Error:
// Error notification was already handled in getFromNode(),
// where we have access to the node's address &/ exception.
return;
default:
assert(false);
}
Get.notify(context.user_params, n);
}
}